Open History to see the last commit
1import type { EnvironmentId, ScopedThreadRef, ThreadId } from "@t3tools/contracts";
2import { foldSearchQuery, rankSearchItems, scoreThreadSearch } from "@t3tools/shared/paletteSearch";
3
4import type { EnvironmentThreadShell } from "./state/models.ts";
5import { threadSearchSubject, type ThreadSearchShell } from "./state/threadSearch.ts";
6
7const COMPOSER_THREAD_RESULT_LIMIT = 5;
8
9export type ComposerThreadCandidate = ThreadSearchShell &
10 Pick<EnvironmentThreadShell, "archivedAt" | "lineage">;
11
12export interface ComposerThreadItem {
13 readonly id: string;
14 readonly type: "thread";
15 readonly thread: ScopedThreadRef;
16 readonly label: string;
17 readonly description: string;
18}
19
20/**
21 * Threads to reference from the composer, matched and ranked like the command
22 * palette, without subagent threads. The agent can only read threads on its
23 * own server, so candidates stay within the composer's environment.
24 */
25export function matchComposerThreadItems(input: {
26 shells: ReadonlyArray<ComposerThreadCandidate>;
27 environmentId: EnvironmentId;
28 excludeThreadId: ThreadId | null;
29 query: string;
30 /** Recency is measured against this time. */
31 now: number;
32}): ComposerThreadItem[] {
33 const query = foldSearchQuery(input.query);
34 if (query.length === 0) return [];
35 const candidates = input.shells.filter(
36 (shell) =>
37 shell.environmentId === input.environmentId &&
38 shell.id !== input.excludeThreadId &&
39 shell.archivedAt === null &&
40 shell.lineage.relationshipToParent !== "subagent",
41 );
42 return rankSearchItems(candidates, (shell, options) =>
43 scoreThreadSearch(query, threadSearchSubject(shell, null), input.now, {
44 ...options,
45 picks: false,
46 }),
47 )
48 .slice(0, COMPOSER_THREAD_RESULT_LIMIT)
49 .map(({ item: shell }) => ({
50 id: `thread:${shell.environmentId}:${shell.id}`,
51 type: "thread",
52 thread: { environmentId: shell.environmentId, threadId: shell.id },
53 label: shell.title,
54 description: "Thread",
55 }));
56}