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

orchestrationV2BoundedSnapshot.ts

50 lines1.8 KB

1import type {
2 OrchestrationV2ProjectedTurnItem,
3 OrchestrationV2ThreadProjection,
4} from "@t3tools/contracts";
5​
6import { restoreSnapshotBodies } from "./orchestrationV2DedupedBodies.ts";
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 // Require object identity so row restoration reproduces the exact entry.
22 if (projection.turnItems[omitted] !== row.item) return null;
23 omitted += 1;
24 }
25 if (omitted === 0) return null;
26 return { ...projection, turnItems: projection.turnItems.slice(omitted) };
28​
29/** Restore marked snapshots before reducers or caches see them. */
30export function restoreLocalVisibleTurnItems(
31 projection: OrchestrationV2ThreadProjection,
32): OrchestrationV2ThreadProjection {
33 const local = projection.visibleTurnItems
34 .filter((row) => isLocalTimelineRow(projection, row))
35 .map((row) => row.item);
36 if (local.length === 0) return projection;
37 return { ...projection, turnItems: [...local, ...projection.turnItems] };
39​
40/** Undoes both opt-in compactions; bodies carry their own markers, so they need no flag. */
41export function boundedSnapshotProjection(snapshot: {
42 readonly projection: OrchestrationV2ThreadProjection;
43 readonly turnItemsOmitLocalVisible?: true | undefined;
44}): OrchestrationV2ThreadProjection {
45 return restoreSnapshotBodies(
46 snapshot.turnItemsOmitLocalVisible === true
47 ? restoreLocalVisibleTurnItems(snapshot.projection)
48 : snapshot.projection,
49 );