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

composerThreadItems.ts

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