Open History to see the last commit
1import type {
2 OrchestrationV2ProjectedTurnItem,
3 OrchestrationV2ThreadProjection,
4} from "@t3tools/contracts";
5
6function isLocalTimelineRow(
7 projection: Pick<OrchestrationV2ThreadProjection, "thread">,
8 row: OrchestrationV2ProjectedTurnItem,
9): boolean {
10 return row.visibility === "local" || row.sourceThreadId === projection.thread.id;
11}
12
13export function omitLocalVisibleTurnItems(
14 projection: OrchestrationV2ThreadProjection,
15): OrchestrationV2ThreadProjection | null {
16 let omitted = 0;
17 for (const row of projection.visibleTurnItems) {
18 if (!isLocalTimelineRow(projection, row)) continue;
19 // Same object, not merely the same id: restoring from the row must
20 // reproduce the original entry exactly.
21 if (projection.turnItems[omitted] !== row.item) return null;
22 omitted += 1;
23 }
24 if (omitted === 0) return null;
25 return { ...projection, turnItems: projection.turnItems.slice(omitted) };
26}
27
28/**
29 * Client half: call on a decoded snapshot that carries the
30 * `turnItemsOmitLocalVisible` marker, before reducers or caches see it.
31 */
32export function restoreLocalVisibleTurnItems(
33 projection: OrchestrationV2ThreadProjection,
34): OrchestrationV2ThreadProjection {
35 const local = projection.visibleTurnItems
36 .filter((row) => isLocalTimelineRow(projection, row))
37 .map((row) => row.item);
38 if (local.length === 0) return projection;
39 return { ...projection, turnItems: [...local, ...projection.turnItems] };
40}
41
42export function boundedSnapshotProjection(snapshot: {
43 readonly projection: OrchestrationV2ThreadProjection;
44 readonly turnItemsOmitLocalVisible?: true | undefined;
45}): OrchestrationV2ThreadProjection {
46 return snapshot.turnItemsOmitLocalVisible === true
47 ? restoreLocalVisibleTurnItems(snapshot.projection)
48 : snapshot.projection;
49}