Open History to see the last commit
1import type {
2 EnvironmentId,
3 EnvironmentMachineKind,
4 ScopedThreadRef,
5 ThreadId,
6} from "@t3tools/contracts";
7import { foldSearchQuery, rankSearchItems, scoreThreadSearch } from "@t3tools/shared/paletteSearch";
8
9import type { EnvironmentThreadShell } from "./state/models.ts";
10import { threadSearchSubject, type ThreadSearchShell } from "./state/threadSearch.ts";
11
12const COMPOSER_THREAD_RESULT_LIMIT = 5;
13
14export type ComposerThreadCandidate = ThreadSearchShell &
15 Pick<EnvironmentThreadShell, "archivedAt" | "lineage">;
16
17export interface ComposerThreadItem {
18 readonly id: string;
19 readonly type: "thread";
20 readonly thread: ScopedThreadRef;
21 readonly label: string;
22 readonly description: string;
23}
24
25/**
26 * Threads to reference from the composer, matched and ranked like the command
27 * palette, without subagent threads. The agent reads threads on its own server
28 * and on the machines it links to, so candidates stay within those, and a linked
29 * machine's threads say which machine they are on.
30 */
31export function matchComposerThreadItems(input: {
32 shells: ReadonlyArray<ComposerThreadCandidate>;
33 environmentId: EnvironmentId;
34 /** The machines the composer's environment links to, by name. */
35 linkedEnvironments: ReadonlyMap<EnvironmentId, string>;
36 excludeThreadId: ThreadId | null;
37 query: string;
38 /** Recency is measured against this time. */
39 now: number;
40}): ComposerThreadItem[] {
41 const query = foldSearchQuery(input.query);
42 if (query.length === 0) return [];
43 const candidates = input.shells.filter(
44 (shell) =>
45 (shell.environmentId === input.environmentId ||
46 input.linkedEnvironments.has(shell.environmentId)) &&
47 shell.id !== input.excludeThreadId &&
48 shell.archivedAt === null &&
49 shell.lineage.relationshipToParent !== "subagent",
50 );
51 return rankSearchItems(candidates, (shell, options) =>
52 scoreThreadSearch(query, threadSearchSubject(shell, null), input.now, {
53 ...options,
54 picks: false,
55 }),
56 )
57 .slice(0, COMPOSER_THREAD_RESULT_LIMIT)
58 .map(({ item: shell }) => {
59 const machine =
60 shell.environmentId === input.environmentId
61 ? undefined
62 : input.linkedEnvironments.get(shell.environmentId);
63 return {
64 id: `thread:${shell.environmentId}:${shell.id}`,
65 type: "thread",
66 thread: { environmentId: shell.environmentId, threadId: shell.id },
67 label: shell.title,
68 description: machine === undefined ? "Thread" : `Thread on ${machine}`,
69 };
70 });
71}
72
73export interface ComposerEnvironmentCandidate {
74 readonly environmentId: EnvironmentId;
75 readonly label: string;
76 readonly machine: EnvironmentMachineKind;
77}
78
79export interface ComposerEnvironmentItem {
80 readonly id: string;
81 readonly type: "environment";
82 readonly environmentId: EnvironmentId;
83 readonly machine: EnvironmentMachineKind;
84 readonly label: string;
85 readonly description: string;
86}
87
88/**
89 * The user's machines the `@` picker offers, so the agent can link or work there. The
90 * composer's own environment is left out. Like threads, they need a query: bare `@` stays a
91 * file picker.
92 */
93export function matchComposerEnvironmentItems(input: {
94 environments: ReadonlyArray<ComposerEnvironmentCandidate>;
95 environmentId: EnvironmentId;
96 query: string;
97}): ComposerEnvironmentItem[] {
98 const query = input.query.trim().toLowerCase();
99 if (query.length === 0) return [];
100 return input.environments
101 .filter(
102 (environment) =>
103 environment.environmentId !== input.environmentId &&
104 environment.label.toLowerCase().includes(query),
105 )
106 .map((environment) => ({
107 id: `environment:${environment.environmentId}`,
108 type: "environment",
109 environmentId: environment.environmentId,
110 machine: environment.machine,
111 label: environment.label,
112 description: "Machine",
113 }));
114}