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

composerThreadItems.ts

50 lines1.5 KB

1import type { EnvironmentId, ScopedThreadRef, ThreadId } from "@t3tools/contracts";
2​
3const COMPOSER_THREAD_RESULT_LIMIT = 5;
4​
5export interface ComposerThreadCandidate {
6 readonly environmentId: EnvironmentId;
7 readonly id: ThreadId;
8 readonly title: string;
9 readonly updatedAt: string;
10 readonly archivedAt: string | null;
12​
13export interface ComposerThreadItem {
14 readonly id: string;
15 readonly type: "thread";
16 readonly thread: ScopedThreadRef;
17 readonly label: string;
18 readonly description: string;
20​
21/**
22 * The agent can only read threads on its own server, so candidates stay within the
23 * composer's environment.
24 */
25export function matchComposerThreadItems(input: {
26 shells: ReadonlyArray<ComposerThreadCandidate>;
27 environmentId: EnvironmentId;
28 excludeThreadId: ThreadId | null;
29 query: string;
30}): ComposerThreadItem[] {
31 const query = input.query.trim().toLowerCase();
32 if (query.length === 0) return [];
33 return input.shells
34 .filter(
35 (shell) =>
36 shell.environmentId === input.environmentId &&
37 shell.id !== input.excludeThreadId &&
38 shell.archivedAt === null &&
39 shell.title.toLowerCase().includes(query),
40 )
41 .sort((left, right) => right.updatedAt.localeCompare(left.updatedAt))
42 .slice(0, COMPOSER_THREAD_RESULT_LIMIT)
43 .map((shell) => ({
44 id: `thread:${shell.environmentId}:${shell.id}`,
45 type: "thread",
46 thread: { environmentId: shell.environmentId, threadId: shell.id },
47 label: shell.title,
48 description: "Thread",
49 }));