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 can only read threads on its
28 * own server, so candidates stay within the composer's environment.
29 */
30export function matchComposerThreadItems(input: {
31 shells: ReadonlyArray<ComposerThreadCandidate>;
32 environmentId: EnvironmentId;
33 excludeThreadId: ThreadId | null;
34 query: string;
35 /** Recency is measured against this time. */
36 now: number;
37}): ComposerThreadItem[] {
38 const query = foldSearchQuery(input.query);
39 if (query.length === 0) return [];
40 const candidates = input.shells.filter(
41 (shell) =>
42 shell.environmentId === input.environmentId &&
43 shell.id !== input.excludeThreadId &&
44 shell.archivedAt === null &&
45 shell.lineage.relationshipToParent !== "subagent",
46 );
47 return rankSearchItems(candidates, (shell, options) =>
48 scoreThreadSearch(query, threadSearchSubject(shell, null), input.now, {
49 ...options,
50 picks: false,
51 }),
52 )
53 .slice(0, COMPOSER_THREAD_RESULT_LIMIT)
54 .map(({ item: shell }) => ({
55 id: `thread:${shell.environmentId}:${shell.id}`,
56 type: "thread",
57 thread: { environmentId: shell.environmentId, threadId: shell.id },
58 label: shell.title,
59 description: "Thread",
60 }));
61}
62
63export interface ComposerEnvironmentCandidate {
64 readonly environmentId: EnvironmentId;
65 readonly label: string;
66 readonly machine: EnvironmentMachineKind;
67}
68
69export interface ComposerEnvironmentItem {
70 readonly id: string;
71 readonly type: "environment";
72 readonly environmentId: EnvironmentId;
73 readonly machine: EnvironmentMachineKind;
74 readonly label: string;
75 readonly description: string;
76}
77
78/**
79 * The user's machines the `@` picker offers, so the agent can link or work there. The
80 * composer's own environment is left out. Like threads, they need a query: bare `@` stays a
81 * file picker.
82 */
83export function matchComposerEnvironmentItems(input: {
84 environments: ReadonlyArray<ComposerEnvironmentCandidate>;
85 environmentId: EnvironmentId;
86 query: string;
87}): ComposerEnvironmentItem[] {
88 const query = input.query.trim().toLowerCase();
89 if (query.length === 0) return [];
90 return input.environments
91 .filter(
92 (environment) =>
93 environment.environmentId !== input.environmentId &&
94 environment.label.toLowerCase().includes(query),
95 )
96 .map((environment) => ({
97 id: `environment:${environment.environmentId}`,
98 type: "environment",
99 environmentId: environment.environmentId,
100 machine: environment.machine,
101 label: environment.label,
102 description: "Machine",
103 }));
104}