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

main
ts 113 lines 4.13 KB
Raw
1 import type { ArchivedSnapshotEntry } from "@t3tools/client-runtime/state/threads";
2 import {
3 scopeProject,
4 scopeThreadShell,
5 type EnvironmentProject,
6 type EnvironmentThreadShell,
7 } from "@t3tools/client-runtime/state/shell";
8 import { threadSearchSubject } from "@t3tools/client-runtime/state/thread-search";
9 import type { EnvironmentId } from "@t3tools/contracts";
10 import { foldSearchQuery, rankSearchItems, scoreThreadSearch } from "@t3tools/shared/paletteSearch";
11 import * as Arr from "effect/Array";
12 import * as Order from "effect/Order";
13
14 import { scopedProjectKey } from "../../lib/scopedEntities";
15
16 export type ArchivedThreadSortOrder = "newest" | "oldest";
17
18 export interface ArchivedThreadGroup {
19 readonly key: string;
20 readonly project: EnvironmentProject;
21 readonly threads: ReadonlyArray<EnvironmentThreadShell>;
22 }
23
24 function archiveTimestamp(thread: EnvironmentThreadShell): number {
25 const timestamp = Date.parse(thread.archivedAt ?? thread.updatedAt);
26 return Number.isNaN(timestamp) ? 0 : timestamp;
27 }
28
29 function matchesQuery(value: string | null, query: string): boolean {
30 return value?.toLocaleLowerCase().includes(query) ?? false;
31 }
32
33 export function buildArchivedThreadGroups(input: {
34 readonly snapshots: ReadonlyArray<ArchivedSnapshotEntry>;
35 readonly environmentLabels: Readonly<Record<string, string>>;
36 readonly environmentId: EnvironmentId | null;
37 readonly searchQuery: string;
38 readonly sortOrder: ArchivedThreadSortOrder;
39 }): ReadonlyArray<ArchivedThreadGroup> {
40 const query = input.searchQuery.trim().toLocaleLowerCase();
41 const foldedQuery = foldSearchQuery(input.searchQuery);
42 const groups: ArchivedThreadGroup[] = [];
43
44 for (const entry of input.snapshots) {
45 if (input.environmentId !== null && input.environmentId !== entry.environmentId) {
46 continue;
47 }
48
49 const environmentLabel = input.environmentLabels[entry.environmentId] ?? null;
50 const threadsByProjectId = new Map<string, EnvironmentThreadShell[]>();
51 for (const thread of entry.snapshot.threads) {
52 if (thread.archivedAt === null) {
53 continue;
54 }
55 const threads = threadsByProjectId.get(thread.projectId) ?? [];
56 threads.push(scopeThreadShell(entry.environmentId, thread));
57 threadsByProjectId.set(thread.projectId, threads);
58 }
59
60 for (const rawProject of entry.snapshot.projects) {
61 const project = scopeProject(entry.environmentId, rawProject);
62 const projectThreads = threadsByProjectId.get(project.id) ?? [];
63 const groupMatches =
64 query.length === 0 ||
65 matchesQuery(project.title, query) ||
66 matchesQuery(project.workspaceRoot, query) ||
67 matchesQuery(environmentLabel, query);
68 // Threads match as in every other thread search; the list keeps its own order.
69 const matchingThreads = groupMatches
70 ? projectThreads
71 : rankSearchItems(projectThreads, (thread, options) =>
72 scoreThreadSearch(foldedQuery, threadSearchSubject(thread, null), 0, {
73 ...options,
74 picks: false,
75 }),
76 ).map(({ item }) => item);
77
78 if (matchingThreads.length === 0) {
79 continue;
80 }
81
82 const timestampOrder = input.sortOrder === "newest" ? Order.flip(Order.Number) : Order.Number;
83 groups.push({
84 key: scopedProjectKey(project.environmentId, project.id),
85 project,
86 threads: Arr.sort(
87 matchingThreads,
88 Order.mapInput(
89 Order.Struct({ timestamp: timestampOrder, title: Order.String, id: Order.String }),
90 (thread: EnvironmentThreadShell) => ({
91 timestamp: archiveTimestamp(thread),
92 title: thread.title,
93 id: thread.id,
94 }),
95 ),
96 ),
97 });
98 }
99 }
100
101 const timestampOrder = input.sortOrder === "newest" ? Order.flip(Order.Number) : Order.Number;
102 return Arr.sort(
103 groups,
104 Order.mapInput(
105 Order.Struct({ timestamp: timestampOrder, title: Order.String, key: Order.String }),
106 (group: ArchivedThreadGroup) => ({
107 timestamp: group.threads[0] ? archiveTimestamp(group.threads[0]) : 0,
108 title: group.project.title,
109 key: group.key,
110 }),
111 ),
112 );
113 }