The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { |
| 2 | OrchestrationV2ProjectedTurnItem, |
| 3 | OrchestrationV2ThreadProjection, |
| 4 | } from "@t3tools/contracts"; |
| 5 | |
| 6 | import { restoreSnapshotBodies } from "./orchestrationV2DedupedBodies.ts"; |
| 7 | |
| 8 | function isLocalTimelineRow( |
| 9 | projection: Pick<OrchestrationV2ThreadProjection, "thread">, |
| 10 | row: OrchestrationV2ProjectedTurnItem, |
| 11 | ): boolean { |
| 12 | return row.visibility === "local" || row.sourceThreadId === projection.thread.id; |
| 13 | } |
| 14 | |
| 15 | export 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) }; |
| 27 | } |
| 28 | |
| 29 | /** Restore marked snapshots before reducers or caches see them. */ |
| 30 | export 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] }; |
| 38 | } |
| 39 | |
| 40 | /** Undoes both opt-in compactions; bodies carry their own markers, so they need no flag. */ |
| 41 | export 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 | ); |
| 50 | } |