The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { |
| 2 | OrchestrationV2ThreadHistoryPage, |
| 3 | OrchestrationV2ThreadProjection, |
| 4 | OrchestrationV2ThreadStreamItem, |
| 5 | } from "@t3tools/contracts"; |
| 6 | import { boundedSnapshotProjection } from "@t3tools/shared/orchestrationV2BoundedSnapshot"; |
| 7 | |
| 8 | import { applyOrchestrationV2ProjectionEvent } from "./orchestrationV2Projection.ts"; |
| 9 | import { |
| 10 | mergeOlderHistoryIntoProjection, |
| 11 | mergeOlderSubagentsIntoProjection, |
| 12 | } from "./threadHistoryMerge.ts"; |
| 13 | |
| 14 | /** |
| 15 | * A thread built from its stream alone, for a view with no environment connection of its own to |
| 16 | * the thread: a guest page, or a linked environment's thread read through the link. |
| 17 | */ |
| 18 | export interface ThreadStreamState { |
| 19 | /** The thread as the owner's client holds it, built with the same reducer. */ |
| 20 | readonly projection: OrchestrationV2ThreadProjection | null; |
| 21 | readonly sequence: number; |
| 22 | /** Set while the snapshot is a window of a longer history, as the owner's client tracks it. */ |
| 23 | readonly partial: { readonly latestLocalTurnOrdinal: number | null } | null; |
| 24 | /** Where "Load earlier turns" continues, or null once the whole thread is loaded. */ |
| 25 | readonly historyCursor: string | null; |
| 26 | } |
| 27 | |
| 28 | export const initialThreadStreamState: ThreadStreamState = { |
| 29 | projection: null, |
| 30 | sequence: 0, |
| 31 | partial: null, |
| 32 | historyCursor: null, |
| 33 | }; |
| 34 | |
| 35 | /** |
| 36 | * Applies the owner's thread stream the way the owner's client does (see EnvironmentThreadState): |
| 37 | * a snapshot replaces the thread, and later events pass through the same projection reducer, once |
| 38 | * each. |
| 39 | */ |
| 40 | export function applyThreadStreamItem( |
| 41 | state: ThreadStreamState, |
| 42 | item: OrchestrationV2ThreadStreamItem, |
| 43 | ): ThreadStreamState { |
| 44 | if (item.kind === "snapshot") { |
| 45 | return { |
| 46 | projection: boundedSnapshotProjection(item), |
| 47 | sequence: item.snapshotSequence, |
| 48 | partial: |
| 49 | item.hasMoreHistory === true || (item.historyCursor ?? null) !== null |
| 50 | ? { latestLocalTurnOrdinal: item.latestLocalTurnOrdinal ?? null } |
| 51 | : null, |
| 52 | historyCursor: item.hasMoreHistory === true ? (item.historyCursor ?? null) : null, |
| 53 | }; |
| 54 | } |
| 55 | if (item.kind === "synchronized" || item.sequence <= state.sequence) return state; |
| 56 | if (item.kind === "unknown-event") return { ...state, sequence: item.sequence }; |
| 57 | const projection = |
| 58 | applyOrchestrationV2ProjectionEvent( |
| 59 | state.projection, |
| 60 | item.event, |
| 61 | state.partial === null ? undefined : { partialTimeline: true, ...state.partial }, |
| 62 | ) ?? state.projection; |
| 63 | const turnItem = item.event.type === "turn-item.updated" ? item.event.payload : null; |
| 64 | const watermark = state.partial?.latestLocalTurnOrdinal ?? null; |
| 65 | return { |
| 66 | ...state, |
| 67 | projection, |
| 68 | sequence: item.sequence, |
| 69 | partial: |
| 70 | state.partial !== null && |
| 71 | turnItem !== null && |
| 72 | (watermark === null || turnItem.ordinal > watermark) |
| 73 | ? { latestLocalTurnOrdinal: turnItem.ordinal } |
| 74 | : state.partial, |
| 75 | }; |
| 76 | } |
| 77 | |
| 78 | /** Merges a page of older turns, fetched from `cursor`, in front, as in the owner's view. */ |
| 79 | export function applyThreadHistoryPage( |
| 80 | state: ThreadStreamState, |
| 81 | cursor: string, |
| 82 | page: OrchestrationV2ThreadHistoryPage, |
| 83 | ): ThreadStreamState { |
| 84 | // A snapshot since the request replaced the thread and its cursor. |
| 85 | if (state.projection === null || state.historyCursor !== cursor) return state; |
| 86 | return { |
| 87 | ...state, |
| 88 | projection: mergeOlderSubagentsIntoProjection( |
| 89 | mergeOlderHistoryIntoProjection(state.projection, page.items), |
| 90 | page.subagents, |
| 91 | ), |
| 92 | historyCursor: page.hasMoreHistory ? page.nextCursor : null, |
| 93 | }; |
| 94 | } |