Open History to see the last commit
1import type {
2 OrchestrationV2ProjectedTurnItem,
3 OrchestrationV2ThreadProjection,
4} from "@t3tools/contracts";
5import { isOrchestrationV2TurnItemVisible } from "@t3tools/shared/orchestrationV2Timeline";
6
7export type ThreadHistoryMeta = {
8 readonly historyCursor: string | null;
9 readonly hasMoreHistory: boolean;
10 readonly loading: boolean;
11 readonly error: string | null;
12 readonly expanded: boolean;
13 readonly latestLocalTurnOrdinal: number | null;
14};
15
16export const EMPTY_THREAD_HISTORY_META: ThreadHistoryMeta = {
17 historyCursor: null,
18 hasMoreHistory: false,
19 loading: false,
20 error: null,
21 expanded: false,
22 latestLocalTurnOrdinal: null,
23};
24
25export function isActiveHistoryRequestCursor(
26 requestCursor: string,
27 history: { readonly historyCursor: string | null },
28): boolean {
29 return history.historyCursor === requestCursor;
30}
31
32export function clearActiveHistoryLoading(
33 requestCursor: string,
34 history: ThreadHistoryMeta,
35): ThreadHistoryMeta {
36 if (!isActiveHistoryRequestCursor(requestCursor, history) || !history.loading) {
37 return history;
38 }
39 return { ...history, loading: false };
40}
41
42function projectedItemKey(row: OrchestrationV2ProjectedTurnItem): string {
43 return `${row.sourceThreadId}:${row.sourceItemId}`;
44}
45
46function renumberVisible(
47 rows: ReadonlyArray<OrchestrationV2ProjectedTurnItem>,
48): OrchestrationV2ProjectedTurnItem[] {
49 return rows.map((row, position) => (row.position === position ? row : { ...row, position }));
50}
51
52export function mergeOlderHistoryIntoProjection(
53 projection: OrchestrationV2ThreadProjection,
54 olderItems: ReadonlyArray<OrchestrationV2ProjectedTurnItem>,
55 work?: { readonly anchorId: string; readonly nextCursor: string | null },
56): OrchestrationV2ThreadProjection {
57 if (olderItems.length === 0 && work === undefined) {
58 return projection;
59 }
60
61 const start =
62 work === undefined
63 ? -1
64 : projection.visibleTurnItems.findIndex((row) => row.sourceItemId === work.anchorId);
65 const anchor = projection.visibleTurnItems[start];
66 const deferred = anchor?.item.deferredWork;
67 if (work !== undefined && deferred === undefined) return projection;
68 const currentByKey = new Map(
69 projection.visibleTurnItems.map((row) => [projectedItemKey(row), row]),
70 );
71 const turnItemById = new Map(projection.turnItems.map((item) => [String(item.id), item]));
72 const pageKeys = new Set<string>();
73 const page: OrchestrationV2ProjectedTurnItem[] = [];
74 for (const pageRow of olderItems) {
75 if (
76 work &&
77 anchor &&
78 deferred &&
79 (pageRow.sourceThreadId !== anchor.sourceThreadId ||
80 pageRow.item.ordinal < anchor.item.ordinal ||
81 pageRow.item.ordinal > deferred.lastOrdinal)
82 )
83 continue;
84 const key = projectedItemKey(pageRow);
85 if (pageKeys.has(key)) continue;
86 let row = currentByKey.get(key) ?? pageRow;
87 if (
88 !currentByKey.has(key) &&
89 (row.visibility === "local" || row.sourceThreadId === projection.thread.id)
90 ) {
91 const currentItem = turnItemById.get(String(row.sourceItemId));
92 if (currentItem !== undefined) {
93 if (
94 currentItem.type !== "run_interrupt_request" ||
95 !isOrchestrationV2TurnItemVisible({
96 item: currentItem,
97 runs: projection.runs,
98 attempts: projection.attempts,
99 items: projection.turnItems,
100 })
101 )
102 continue;
103 row = { ...row, item: currentItem };
104 }
105 }
106 pageKeys.add(key);
107 page.push(row);
108 if (
109 (row.visibility === "local" || row.sourceThreadId === projection.thread.id) &&
110 !turnItemById.has(String(row.sourceItemId))
111 )
112 turnItemById.set(String(row.sourceItemId), row.item);
113 }
114 if (page.length === 0 && work === undefined) return projection;
115 let visible: OrchestrationV2ProjectedTurnItem[];
116 if (work && anchor && deferred) {
117 let end = start + 1;
118 while (end < projection.visibleTurnItems.length) {
119 const row = projection.visibleTurnItems[end]!;
120 if (
121 row.sourceThreadId !== anchor.sourceThreadId ||
122 row.visibility === "synthetic" ||
123 row.item.ordinal > deferred.lastOrdinal
124 )
125 break;
126 end += 1;
127 }
128 const item = { ...anchor.item };
129 if (work.nextCursor === null) delete item.deferredWork;
130 else item.deferredWork = { ...deferred, cursor: work.nextCursor };
131 const updated = { ...anchor, item };
132 const section = [
133 ...page,
134 ...projection.visibleTurnItems
135 .slice(start, end)
136 .filter((row) => !pageKeys.has(projectedItemKey(row))),
137 ];
138 const ordered = section
139 .map((row) => (row.sourceItemId === anchor.sourceItemId ? updated : row))
140 .sort((left, right) => left.item.ordinal - right.item.ordinal);
141 visible = [
142 ...projection.visibleTurnItems.slice(0, start),
143 ...ordered,
144 ...projection.visibleTurnItems.slice(end),
145 ];
146 if (anchor.visibility === "local") turnItemById.set(String(updated.item.id), updated.item);
147 } else {
148 visible = [
149 ...page,
150 ...projection.visibleTurnItems.filter((row) => !pageKeys.has(projectedItemKey(row))),
151 ];
152 }
153 if (
154 visible.length === projection.visibleTurnItems.length &&
155 visible.every((row, index) => row === projection.visibleTurnItems[index])
156 )
157 return projection;
158 return {
159 ...projection,
160 turnItems: [...turnItemById.values()],
161 visibleTurnItems: renumberVisible(visible),
162 };
163}
164
165export function applyHistoryPageMeta(
166 current: ThreadHistoryMeta,
167 page: {
168 readonly nextCursor: string | null;
169 readonly hasMoreHistory: boolean;
170 },
171): ThreadHistoryMeta {
172 return {
173 historyCursor: page.nextCursor,
174 hasMoreHistory: page.hasMoreHistory,
175 loading: false,
176 error: null,
177 expanded: true,
178 latestLocalTurnOrdinal: current.latestLocalTurnOrdinal,
179 };
180}
181
182export function shouldShowLoadEarlierControl(history: ThreadHistoryMeta): boolean {
183 return history.hasMoreHistory || history.error !== null;
184}