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 = [
116 ...page,
117 ...projection.visibleTurnItems.filter((row) => !pageKeys.has(projectedItemKey(row))),
118 ];
119 if (work && anchor && deferred) {
120 let end = start + 1;
121 while (end < projection.visibleTurnItems.length) {
122 const row = projection.visibleTurnItems[end]!;
123 if (
124 row.sourceThreadId !== anchor.sourceThreadId ||
125 row.visibility === "synthetic" ||
126 row.item.ordinal > deferred.lastOrdinal
127 )
128 break;
129 end += 1;
130 }
131 const item = { ...anchor.item };
132 if (work.nextCursor === null) delete item.deferredWork;
133 else item.deferredWork = { ...deferred, cursor: work.nextCursor };
134 const updated = { ...anchor, item };
135 const section = [
136 ...page,
137 ...projection.visibleTurnItems
138 .slice(start, end)
139 .filter((row) => !pageKeys.has(projectedItemKey(row))),
140 ];
141 const ordered = section
142 .map((row) => (row.sourceItemId === anchor.sourceItemId ? updated : row))
143 .sort((left, right) => left.item.ordinal - right.item.ordinal);
144 visible = [
145 ...projection.visibleTurnItems.slice(0, start),
146 ...ordered,
147 ...projection.visibleTurnItems.slice(end),
148 ];
149 if (anchor.visibility === "local") turnItemById.set(String(updated.item.id), updated.item);
150 }
151 if (
152 visible.length === projection.visibleTurnItems.length &&
153 visible.every((row, index) => row === projection.visibleTurnItems[index])
154 )
155 return projection;
156 return {
157 ...projection,
158 turnItems: [...turnItemById.values()],
159 visibleTurnItems: renumberVisible(visible),
160 };
161}
162
163export function applyHistoryPageMeta(
164 current: ThreadHistoryMeta,
165 page: {
166 readonly nextCursor: string | null;
167 readonly hasMoreHistory: boolean;
168 },
169): ThreadHistoryMeta {
170 return {
171 historyCursor: page.nextCursor,
172 hasMoreHistory: page.hasMoreHistory,
173 loading: false,
174 error: null,
175 expanded: true,
176 latestLocalTurnOrdinal: current.latestLocalTurnOrdinal,
177 };
178}
179
180export function shouldShowLoadEarlierControl(history: ThreadHistoryMeta): boolean {
181 return history.hasMoreHistory || history.error !== null;
182}