The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | isListedThreadLineage, |
| 3 | type EnvironmentId, |
| 4 | type EnvironmentMachineKind, |
| 5 | type ScopedThreadRef, |
| 6 | type ThreadId, |
| 7 | } from "@t3tools/contracts"; |
| 8 | import { foldSearchQuery, rankSearchItems, scoreThreadSearch } from "@t3tools/shared/paletteSearch"; |
| 9 | |
| 10 | import type { EnvironmentThreadShell } from "./state/models.ts"; |
| 11 | import { threadSearchSubject, type ThreadSearchShell } from "./state/threadSearch.ts"; |
| 12 | import { mFallback, msg } from "gt-i18n"; |
| 13 | import type { MFunctionType } from "gt-i18n/types"; |
| 14 | |
| 15 | const COMPOSER_THREAD_RESULT_LIMIT = 5; |
| 16 | |
| 17 | export type ComposerThreadCandidate = ThreadSearchShell & |
| 18 | Pick<EnvironmentThreadShell, "archivedAt" | "lineage">; |
| 19 | |
| 20 | export 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 threads or other threads' side chats. The agent reads threads |
| 31 | * on its own server and on the machines it links to, so candidates stay within those, and a |
| 32 | * linked machine's threads say which machine they are on. |
| 33 | */ |
| 34 | export 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 | // Closing an unused side chat deletes it, so only one with a message is offered. |
| 50 | const isOwnSideChat = (shell: ComposerThreadCandidate) => |
| 51 | shell.lineage.sideChat === true && |
| 52 | shell.environmentId === input.environmentId && |
| 53 | shell.lineage.parentThreadId === input.excludeThreadId && |
| 54 | shell.latestUserMessageAt !== null; |
| 55 | const candidates = input.shells.filter( |
| 56 | (shell) => |
| 57 | (shell.environmentId === input.environmentId || |
| 58 | input.linkedEnvironments.has(shell.environmentId)) && |
| 59 | shell.id !== input.excludeThreadId && |
| 60 | shell.archivedAt === null && |
| 61 | (isListedThreadLineage(shell.lineage) || isOwnSideChat(shell)), |
| 62 | ); |
| 63 | return rankSearchItems(candidates, (shell, options) => |
| 64 | scoreThreadSearch(query, threadSearchSubject(shell, null), input.now, { |
| 65 | ...options, |
| 66 | picks: false, |
| 67 | }), |
| 68 | ) |
| 69 | .slice(0, COMPOSER_THREAD_RESULT_LIMIT) |
| 70 | .map(({ item: shell }) => { |
| 71 | const machine = |
| 72 | shell.environmentId === input.environmentId |
| 73 | ? undefined |
| 74 | : input.linkedEnvironments.get(shell.environmentId); |
| 75 | return { |
| 76 | id: `thread:${shell.environmentId}:${shell.id}`, |
| 77 | type: "thread", |
| 78 | thread: { environmentId: shell.environmentId, threadId: shell.id }, |
| 79 | label: shell.title, |
| 80 | description: isOwnSideChat(shell) |
| 81 | ? resolve(msg("Side chat")) |
| 82 | : machine === undefined |
| 83 | ? resolve(msg("Thread", { $context: "noun, search result type" })) |
| 84 | : resolve(msg("Thread on {machine}", { machine })), |
| 85 | }; |
| 86 | }); |
| 87 | } |
| 88 | |
| 89 | export interface ComposerEnvironmentCandidate { |
| 90 | readonly environmentId: EnvironmentId; |
| 91 | readonly label: string; |
| 92 | readonly machine: EnvironmentMachineKind; |
| 93 | } |
| 94 | |
| 95 | export interface ComposerEnvironmentItem { |
| 96 | readonly id: string; |
| 97 | readonly type: "environment"; |
| 98 | readonly environmentId: EnvironmentId; |
| 99 | readonly machine: EnvironmentMachineKind; |
| 100 | readonly label: string; |
| 101 | readonly description: string; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * The user's machines the `@` picker offers, so the agent can link or work there. The |
| 106 | * composer's own environment is left out. Like threads, they need a query: bare `@` stays a |
| 107 | * file picker. |
| 108 | */ |
| 109 | export function matchComposerEnvironmentItems( |
| 110 | input: { |
| 111 | environments: ReadonlyArray<ComposerEnvironmentCandidate>; |
| 112 | environmentId: EnvironmentId; |
| 113 | query: string; |
| 114 | }, |
| 115 | resolve: MFunctionType = mFallback, |
| 116 | ): ComposerEnvironmentItem[] { |
| 117 | const query = input.query.trim().toLowerCase(); |
| 118 | if (query.length === 0) return []; |
| 119 | return input.environments |
| 120 | .filter( |
| 121 | (environment) => |
| 122 | environment.environmentId !== input.environmentId && |
| 123 | environment.label.toLowerCase().includes(query), |
| 124 | ) |
| 125 | .map((environment) => ({ |
| 126 | id: `environment:${environment.environmentId}`, |
| 127 | type: "environment", |
| 128 | environmentId: environment.environmentId, |
| 129 | machine: environment.machine, |
| 130 | label: environment.label, |
| 131 | description: resolve(msg("Machine", { $context: "noun, search result type" })), |
| 132 | })); |
| 133 | } |