Open History to see the last commit
1import type {
2 OrchestrationV2ThreadProjection,
3 OrchestrationV2ThreadStreamItem,
4} from "@t3tools/contracts";
5
6import { omitLocalVisibleTurnItems } from "@t3tools/shared/orchestrationV2BoundedSnapshot";
7
8import {
9 buildBoundedThreadProjection,
10 type BoundedProjectionResult,
11} from "./threadHistoryPaging.ts";
12import { projectThreadProjectionForWire } from "./WireProjection.ts";
13
14export const THREAD_RESUME_MAX_REPLAY_EVENTS = 128;
15
16export const THREAD_RESUME_MAX_REPLAY_ENCODED_BYTES = 1_048_576;
17
18/**
19 * Keep the original preflight budget: decoded objects and temporary strings
20 * cost more than their stored JSON, even if wire projection shrinks them later.
21 */
22export const THREAD_RESUME_MAX_RAW_PAYLOAD_BYTES = 1_048_576;
23
24export function isThreadReplayRawPayloadSafe(rawPayloadBytes: number): boolean {
25 return rawPayloadBytes <= THREAD_RESUME_MAX_RAW_PAYLOAD_BYTES;
26}
27
28export function threadReplayEncodedBytes(items: ReadonlyArray<unknown>): number {
29 let total = 0;
30 for (const item of items) {
31 const encoded = JSON.stringify(item);
32 total += Buffer.byteLength(encoded ?? "", "utf8");
33 }
34 return total;
35}
36
37type ThreadSnapshotStreamItem = Extract<
38 OrchestrationV2ThreadStreamItem,
39 { readonly kind: "snapshot" }
40>;
41
42export function boundedSnapshotResponseFields(input: {
43 readonly bounded: BoundedProjectionResult;
44 readonly compactTurnItems: boolean;
45}) {
46 const { bounded } = input;
47 const compactProjection = input.compactTurnItems
48 ? omitLocalVisibleTurnItems(bounded.projection)
49 : null;
50 return {
51 projection: compactProjection ?? bounded.projection,
52 historyCursor: bounded.historyCursor,
53 hasMoreHistory: bounded.hasMoreHistory,
54 latestLocalTurnOrdinal: bounded.latestLocalTurnOrdinal,
55 ...(compactProjection === null ? {} : { turnItemsOmitLocalVisible: true as const }),
56 };
57}
58
59export function buildBoundedThreadStreamSnapshot(input: {
60 readonly snapshotSequence: number;
61 readonly projection: OrchestrationV2ThreadProjection;
62 readonly compactTurnItems?: boolean;
63}): ThreadSnapshotStreamItem {
64 const bounded = buildBoundedThreadProjection({
65 snapshotSequence: input.snapshotSequence,
66 projection: projectThreadProjectionForWire(input.projection),
67 });
68 return {
69 kind: "snapshot",
70 snapshotSequence: input.snapshotSequence,
71 ...boundedSnapshotResponseFields({
72 bounded,
73 compactTurnItems: input.compactTurnItems === true,
74 }),
75 };
76}
77
78export type ThreadResumePlan =
79 | {
80 readonly mode: "replay";
81 readonly afterSequence: number;
82 readonly throughSequence: number;
83 }
84 | { readonly mode: "snapshot" };
85
86/**
87 * Event count limits reducer churn while encoded bytes limit a small number of
88 * large updates. Either excess is cheaper to replace with one current snapshot.
89 */
90export function decideThreadResume(input: {
91 readonly afterSequence: number;
92 readonly highWater: number;
93 readonly replayEventCount: number;
94 readonly replayEncodedBytes: number;
95}): ThreadResumePlan {
96 if (
97 input.afterSequence > input.highWater ||
98 input.replayEventCount > THREAD_RESUME_MAX_REPLAY_EVENTS ||
99 input.replayEncodedBytes > THREAD_RESUME_MAX_REPLAY_ENCODED_BYTES
100 ) {
101 return { mode: "snapshot" };
102 }
103 return {
104 mode: "replay",
105 afterSequence: input.afterSequence,
106 throughSequence: input.highWater,
107 };
108}