Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadHistoryMerge.ts

133 lines4.0 KB

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;
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 };
41​
42function projectedItemKey(row: OrchestrationV2ProjectedTurnItem): string {
43 return `${row.sourceThreadId}:${row.sourceItemId}`;
45​
46function renumberVisible(
47 rows: ReadonlyArray<OrchestrationV2ProjectedTurnItem>,
48): OrchestrationV2ProjectedTurnItem[] {
49 return rows.map((row, position) => (row.position === position ? row : { ...row, position }));
51​
52export function mergeOlderHistoryIntoProjection(
53 projection: OrchestrationV2ThreadProjection,
54 olderItems: ReadonlyArray<OrchestrationV2ProjectedTurnItem>,
55): OrchestrationV2ThreadProjection {
56 if (olderItems.length === 0) {
57 return projection;
58 }
59​
60 const currentByKey = new Map(
61 projection.visibleTurnItems.map((row) => [projectedItemKey(row), row]),
62 );
63 const turnItemById = new Map(projection.turnItems.map((item) => [String(item.id), item]));
64 const pageKeys = new Set<string>();
65 const page: OrchestrationV2ProjectedTurnItem[] = [];
66 for (const pageRow of olderItems) {
67 const key = projectedItemKey(pageRow);
68 if (pageKeys.has(key)) continue;
69 let row = currentByKey.get(key) ?? pageRow;
70 if (
71 !currentByKey.has(key) &&
72 (row.visibility === "local" || row.sourceThreadId === projection.thread.id)
73 ) {
74 const currentItem = turnItemById.get(String(row.sourceItemId));
75 if (currentItem !== undefined) {
76 if (
77 currentItem.type !== "run_interrupt_request" ||
78 !isOrchestrationV2TurnItemVisible({
79 item: currentItem,
80 runs: projection.runs,
81 attempts: projection.attempts,
82 items: projection.turnItems,
83 })
84 )
85 continue;
86 row = { ...row, item: currentItem };
87 }
88 }
89 pageKeys.add(key);
90 page.push(row);
91 if (
92 (row.visibility === "local" || row.sourceThreadId === projection.thread.id) &&
93 !turnItemById.has(String(row.sourceItemId))
94 )
95 turnItemById.set(String(row.sourceItemId), row.item);
96 }
97 if (page.length === 0) return projection;
98 const visible = [
99 ...page,
100 ...projection.visibleTurnItems.filter((row) => !pageKeys.has(projectedItemKey(row))),
101 ];
102 if (
103 visible.length === projection.visibleTurnItems.length &&
104 visible.every((row, index) => row === projection.visibleTurnItems[index])
105 )
106 return projection;
107 return {
108 ...projection,
109 turnItems: [...turnItemById.values()],
110 visibleTurnItems: renumberVisible(visible),
111 };
113​
114export function applyHistoryPageMeta(
115 current: ThreadHistoryMeta,
116 page: {
117 readonly nextCursor: string | null;
118 readonly hasMoreHistory: boolean;
119 },
120): ThreadHistoryMeta {
121 return {
122 historyCursor: page.nextCursor,
123 hasMoreHistory: page.hasMoreHistory,
124 loading: false,
125 error: null,
126 expanded: true,
127 latestLocalTurnOrdinal: current.latestLocalTurnOrdinal,
128 };
130​
131export function shouldShowLoadEarlierControl(history: ThreadHistoryMeta): boolean {
132 return history.hasMoreHistory || history.error !== null;