The best way to control Claude Code, Codex and more from any computer or phone

main
ts 50 lines 1.97 KB
Raw
1 import type { OrchestrationV2SearchThreadInput, RunId } from "@t3tools/contracts";
2
3 export type ThreadFindStart = NonNullable<OrchestrationV2SearchThreadInput["start"]>;
4 export type ThreadFindPositionReader = (query: string) => ThreadFindStart | undefined;
5
6 export interface ThreadFindMatch {
7 readonly entryId: string;
8 readonly runId: RunId | null;
9 readonly occurrence: number;
10 }
11
12 function clampThreadFindIndex(index: number, total: number): number {
13 if (total <= 0 || !Number.isFinite(index) || index < 0) return 0;
14 return Math.min(Math.trunc(index), total - 1);
15 }
16
17 export function stepThreadFindIndex(index: number, total: number, delta: number): number {
18 if (total <= 0) return 0;
19 const clamped = clampThreadFindIndex(index, total);
20 return (((clamped + delta) % total) + total) % total;
21 }
22
23 export function formatThreadFindCount(index: number, total: number): string {
24 return total <= 0 ? "0/0" : `${clampThreadFindIndex(index, total) + 1}/${total}`;
25 }
26
27 export interface ThreadFindAnnouncement {
28 readonly navigationId: number;
29 readonly text: string;
30 /** The label shows the outcome of `navigationId`: a count, a search or a failure. */
31 readonly settled: boolean;
32 /** "Searching…" or a failure: spoken, but a later count for the same navigation is too. */
33 readonly provisional: boolean;
34 }
35
36 /**
37 * What the find counter's live region says: the settled outcome of each
38 * user search or step, once. Background refreshes during streaming keep the
39 * navigation, so they stay silent. A new query passes through "Searching…",
40 * so its count is spoken even when it equals the last one.
41 */
42 export function nextThreadFindAnnouncement(
43 previous: ThreadFindAnnouncement | null,
44 next: ThreadFindAnnouncement,
45 ): ThreadFindAnnouncement {
46 if (!next.settled) return previous ?? { ...next, text: "" };
47 if (previous?.navigationId !== next.navigationId) return next;
48 if (previous.settled && !previous.provisional) return previous;
49 return previous.text === next.text ? previous : next;
50 }