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