Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

composerThreadItems.ts

172 lines6.1 KB

1import {
2 isListedThreadLineage,
3 type EnvironmentId,
4 type EnvironmentMachineKind,
5 type ScopedThreadRef,
6 type ThreadId,
7} from "@t3tools/contracts";
8import { foldSearchQuery, rankSearchItems, scoreThreadSearch } from "@t3tools/shared/paletteSearch";
9​
10import type { EnvironmentProject, EnvironmentThreadShell } from "./state/models.ts";
11import { threadSearchSubject, type ThreadSearchShell } from "./state/threadSearch.ts";
12import { mFallback, msg } from "gt-i18n";
13import type { M } from "gt-i18n";
14​
15const COMPOSER_THREAD_RESULT_LIMIT = 5;
16​
17export type ComposerProjectCandidate = Pick<
18 EnvironmentProject,
19 "id" | "environmentId" | "title" | "workspaceRoot" | "archivedAt" | "isScratch"
20>;
21​
22export function matchComposerProjectItems(input: {
23 projects: ReadonlyArray<ComposerProjectCandidate>;
24 environmentId: EnvironmentId;
25 query: string;
26}) {
27 const query = foldSearchQuery(input.query);
28 if (query.length === 0) return [];
29 return input.projects
30 .filter(
31 (project) =>
32 project.environmentId === input.environmentId &&
33 !project.archivedAt &&
34 !project.isScratch &&
35 (foldSearchQuery(project.title).includes(query) ||
36 foldSearchQuery(project.workspaceRoot).includes(query)),
37 )
38 .slice(0, 5)
39 .map((project) => ({
40 id: `project:${project.environmentId}:${project.id}`,
41 type: "path" as const,
42 path: project.workspaceRoot,
43 label: project.title,
44 description: project.workspaceRoot,
45 }));
47​
48export type ComposerThreadCandidate = ThreadSearchShell &
49 Pick<EnvironmentThreadShell, "archivedAt" | "lineage">;
50​
51export interface ComposerThreadItem {
52 readonly id: string;
53 readonly type: "thread";
54 readonly thread: ScopedThreadRef;
55 readonly label: string;
56 readonly description: string;
58​
59/**
60 * Threads to reference from the composer, matched and ranked like the command
61 * palette, without subagent threads or other threads' side chats. The agent reads threads
62 * on its own server and on the machines it links to, so candidates stay within those, and a
63 * linked machine's threads say which machine they are on.
64 */
65export function matchComposerThreadItems(
66 input: {
67 shells: ReadonlyArray<ComposerThreadCandidate>;
68 environmentId: EnvironmentId;
69 /** The machines the composer's environment links to, by name. */
70 linkedEnvironments: ReadonlyMap<EnvironmentId, string>;
71 excludeThreadId: ThreadId | null;
72 query: string;
73 /** Recency is measured against this time. */
74 now: number;
75 },
76 resolve: M = mFallback,
77): ComposerThreadItem[] {
78 const query = foldSearchQuery(input.query);
79 if (query.length === 0) return [];
80 // Closing an unused side chat deletes it, so only one with a message is offered.
81 const isOwnSideChat = (shell: ComposerThreadCandidate) =>
82 shell.lineage.sideChat === true &&
83 shell.environmentId === input.environmentId &&
84 shell.lineage.parentThreadId === input.excludeThreadId &&
85 shell.latestUserMessageAt !== null;
86 const candidates = input.shells.filter(
87 (shell) =>
88 (shell.environmentId === input.environmentId ||
89 input.linkedEnvironments.has(shell.environmentId)) &&
90 !(shell.environmentId === input.environmentId && shell.id === input.excludeThreadId) &&
91 shell.archivedAt === null &&
92 (isListedThreadLineage(shell.lineage) || isOwnSideChat(shell)),
93 );
94 return rankSearchItems(candidates, (shell, options) =>
95 scoreThreadSearch(query, threadSearchSubject(shell, null), input.now, {
96 ...options,
97 picks: false,
98 }),
99 )
100 .slice(0, COMPOSER_THREAD_RESULT_LIMIT)
101 .map(({ item: shell }) => {
102 const machine =
103 shell.environmentId === input.environmentId
104 ? undefined
105 : input.linkedEnvironments.get(shell.environmentId);
106 return {
107 id: `thread:${shell.environmentId}:${shell.id}`,
108 type: "thread",
109 thread: { environmentId: shell.environmentId, threadId: shell.id },
110 label: shell.title,
111 description: isOwnSideChat(shell)
112 ? resolve(msg("Side chat"))
113 : machine === undefined
114 ? resolve(msg({ context: "noun, search result type" })`Thread`)
115 : resolve(msg`Thread on ${{ machine }}`),
116 };
117 });
119​
120export interface ComposerEnvironmentCandidate {
121 readonly environmentId: EnvironmentId;
122 readonly label: string;
123 readonly machine: EnvironmentMachineKind;
125​
126export interface ComposerEnvironmentItem {
127 readonly id: string;
128 readonly type: "environment";
129 readonly environmentId: EnvironmentId;
130 readonly machine: EnvironmentMachineKind;
131 readonly label: string;
132 readonly description: string;
134​
135/**
136 * The machines the `@` picker offers, so the agent can link or work there: the client's paired
137 * machines and those the composer's environment links to. The composer's own environment is
138 * left out. Like threads, they need a query: bare `@` stays a file picker.
139 */
140export function matchComposerEnvironmentItems(
141 input: {
142 environments: ReadonlyArray<ComposerEnvironmentCandidate>;
143 /** The machines the composer's environment links to, by name. */
144 linkedEnvironments: ReadonlyMap<EnvironmentId, string>;
145 environmentId: EnvironmentId;
146 query: string;
147 },
148 resolve: M = mFallback,
149): ComposerEnvironmentItem[] {
150 const query = input.query.trim().toLowerCase();
151 if (query.length === 0) return [];
152 const paired = new Set(input.environments.map((environment) => environment.environmentId));
153 const linkedOnly = Array.from(input.linkedEnvironments, ([environmentId, label]) => ({
154 environmentId,
155 label,
156 machine: "server" as const,
157 })).filter((environment) => !paired.has(environment.environmentId));
158 return [...input.environments, ...linkedOnly]
159 .filter(
160 (environment) =>
161 environment.environmentId !== input.environmentId &&
162 environment.label.toLowerCase().includes(query),
163 )
164 .map((environment) => ({
165 id: `environment:${environment.environmentId}`,
166 type: "environment",
167 environmentId: environment.environmentId,
168 machine: environment.machine,
169 label: environment.label,
170 description: resolve(msg({ context: "noun, search result type" })`Machine`),
171 }));