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 payloadBudgetExceeded: bounded.payloadBudgetExceeded,
56 ...(compactProjection === null ? {} : { turnItemsOmitLocalVisible: true as const }),
57 };
58}
59
60export function buildBoundedThreadStreamSnapshot(input: {
61 readonly snapshotSequence: number;
62 readonly projection: OrchestrationV2ThreadProjection;
63 readonly compactTurnItems?: boolean;
64}): ThreadSnapshotStreamItem {
65 const bounded = buildBoundedThreadProjection({
66 snapshotSequence: input.snapshotSequence,
67 projection: projectThreadProjectionForWire(input.projection),
68 });
69 return {
70 kind: "snapshot",
71 snapshotSequence: input.snapshotSequence,
72 ...boundedSnapshotResponseFields({
73 bounded,
74 compactTurnItems: input.compactTurnItems === true,
75 }),
76 };
77}
78
79export type ThreadResumePlan =
80 | {
81 readonly mode: "replay";
82 readonly afterSequence: number;
83 readonly throughSequence: number;
84 }
85 | { readonly mode: "snapshot" };
86
87/**
88 * Event count limits reducer churn while encoded bytes limit a small number of
89 * large updates. Either excess is cheaper to replace with one current snapshot.
90 */
91export function decideThreadResume(input: {
92 readonly afterSequence: number;
93 readonly highWater: number;
94 readonly replayEventCount: number;
95 readonly replayEncodedBytes: number;
96}): ThreadResumePlan {
97 if (
98 input.afterSequence > input.highWater ||
99 input.replayEventCount > THREAD_RESUME_MAX_REPLAY_EVENTS ||
100 input.replayEncodedBytes > THREAD_RESUME_MAX_REPLAY_ENCODED_BYTES
101 ) {
102 return { mode: "snapshot" };
103 }
104 return {
105 mode: "replay",
106 afterSequence: input.afterSequence,
107 throughSequence: input.highWater,
108 };
109}