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

orchestrationV2BoundedSnapshot.ts

140 lines5.4 KB

1import type {
2 OrchestrationV2ProjectedTurnItem,
3 OrchestrationV2ThreadProjection,
4 OrchestrationV2TurnItem,
5 TurnItemId,
6} from "@t3tools/contracts";
7​
8function isLocalTimelineRow(
9 projection: Pick<OrchestrationV2ThreadProjection, "thread">,
10 row: OrchestrationV2ProjectedTurnItem,
11): boolean {
12 return row.visibility === "local" || row.sourceThreadId === projection.thread.id;
14​
15export function omitLocalVisibleTurnItems(
16 projection: OrchestrationV2ThreadProjection,
17): OrchestrationV2ThreadProjection | null {
18 let omitted = 0;
19 for (const row of projection.visibleTurnItems) {
20 if (!isLocalTimelineRow(projection, row)) continue;
21 // Same object, not merely the same id: restoring from the row must
22 // reproduce the original entry exactly.
23 if (projection.turnItems[omitted] !== row.item) return null;
24 omitted += 1;
25 }
26 if (omitted === 0) return null;
27 return { ...projection, turnItems: projection.turnItems.slice(omitted) };
29​
30/**
31 * Client half: call on a decoded snapshot that carries the
32 * `turnItemsOmitLocalVisible` marker, before reducers or caches see it.
33 */
34export function restoreLocalVisibleTurnItems(
35 projection: OrchestrationV2ThreadProjection,
36): OrchestrationV2ThreadProjection {
37 const local = projection.visibleTurnItems
38 .filter((row) => isLocalTimelineRow(projection, row))
39 .map((row) => row.item);
40 if (local.length === 0) return projection;
41 return { ...projection, turnItems: [...local, ...projection.turnItems] };
43​
44type CheckpointFiles = OrchestrationV2ThreadProjection["checkpoints"][number]["files"];
45​
46function sameCheckpointFiles(left: CheckpointFiles, right: CheckpointFiles): boolean {
47 if (left.length !== right.length) return false;
48 for (let index = 0; index < left.length; index += 1) {
49 const a = left[index]!;
50 const b = right[index]!;
51 if (
52 a.path !== b.path ||
53 a.kind !== b.kind ||
54 a.additions !== b.additions ||
55 a.deletions !== b.deletions ||
56 Object.keys(a).length !== Object.keys(b).length
57 ) {
58 return false;
59 }
60 }
61 return true;
63​
64/**
65 * Server half of the opt-in checkpoint compaction. A checkpoint turn item
66 * repeats its checkpoint's file list, which can hold thousands of entries.
67 * Items whose files equal those of their checkpoint in `projection.checkpoints`
68 * are sent with empty `files`; the returned ids tell the client which ones to
69 * refill with `restoreCheckpointItemFiles`. Returns null when nothing would be
70 * omitted.
71 */
72export function omitCheckpointItemFiles(projection: OrchestrationV2ThreadProjection): {
73 readonly projection: OrchestrationV2ThreadProjection;
74 readonly itemIds: ReadonlyArray<TurnItemId>;
75} | null {
76 const checkpointFiles = new Map<string, CheckpointFiles>();
77 for (const checkpoint of projection.checkpoints) {
78 if (checkpoint.files.length > 0) checkpointFiles.set(String(checkpoint.id), checkpoint.files);
79 }
80 if (checkpointFiles.size === 0) return null;
81 const itemIds = new Set<TurnItemId>();
82 const compact = (item: OrchestrationV2TurnItem): OrchestrationV2TurnItem => {
83 if (item.type !== "checkpoint" || item.files.length === 0) return item;
84 const files = checkpointFiles.get(String(item.checkpointId));
85 if (files === undefined || !sameCheckpointFiles(files, item.files)) return item;
86 itemIds.add(item.id);
87 return { ...item, files: [] };
88 };
89 const turnItems = projection.turnItems.map(compact);
90 const visibleTurnItems = projection.visibleTurnItems.map((row) => {
91 const item = compact(row.item);
92 return item === row.item ? row : { ...row, item };
93 });
94 if (itemIds.size === 0) return null;
95 return {
96 projection: { ...projection, turnItems, visibleTurnItems },
97 itemIds: [...itemIds],
98 };
100​
101/** Client half of `omitCheckpointItemFiles`, before reducers or caches see the projection. */
102function restoreCheckpointItemFiles(
103 projection: OrchestrationV2ThreadProjection,
104 itemIds: ReadonlyArray<TurnItemId>,
105): OrchestrationV2ThreadProjection {
106 if (itemIds.length === 0) return projection;
107 const omitted = new Set(itemIds.map(String));
108 const checkpointFiles = new Map(
109 projection.checkpoints.map((checkpoint) => [String(checkpoint.id), checkpoint.files]),
110 );
111 const restore = (item: OrchestrationV2TurnItem): OrchestrationV2TurnItem => {
112 if (item.type !== "checkpoint" || !omitted.has(String(item.id))) return item;
113 const files = checkpointFiles.get(String(item.checkpointId));
114 return files === undefined ? item : { ...item, files };
115 };
116 return {
117 ...projection,
118 turnItems: projection.turnItems.map(restore),
119 visibleTurnItems: projection.visibleTurnItems.map((row) => {
120 const item = restore(row.item);
121 return item === row.item ? row : { ...row, item };
122 }),
123 };
125​
126/** The usable projection of a decoded bounded snapshot, compact or not. */
127export function boundedSnapshotProjection(snapshot: {
128 readonly projection: OrchestrationV2ThreadProjection;
129 readonly turnItemsOmitLocalVisible?: true | undefined;
130 readonly checkpointFilesOmittedItemIds?: ReadonlyArray<TurnItemId> | undefined;
131}): OrchestrationV2ThreadProjection {
132 // Files first: restoring local items copies the visible rows' items.
133 const projection =
134 snapshot.checkpointFilesOmittedItemIds === undefined
135 ? snapshot.projection
136 : restoreCheckpointItemFiles(snapshot.projection, snapshot.checkpointFilesOmittedItemIds);
137 return snapshot.turnItemsOmitLocalVisible === true
138 ? restoreLocalVisibleTurnItems(projection)
139 : projection;