Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ThreadStream.ts

114 lines3.7 KB

1import type {
2 OrchestrationV2ThreadProjection,
3 OrchestrationV2ThreadStreamItem,
4} from "@t3tools/contracts";
5​
6import { omitLocalVisibleTurnItems } from "@t3tools/shared/orchestrationV2BoundedSnapshot";
7import { dedupeSnapshotBodies } from "@t3tools/shared/orchestrationV2DedupedBodies";
8​
9import {
10 buildBoundedThreadProjection,
11 type BoundedProjectionResult,
12} from "./threadHistoryPaging.ts";
13import {
14 capProjectionCheckpointFilesForWire,
15 projectThreadProjectionForWire,
16} from "./WireProjection.ts";
17​
18export const THREAD_RESUME_MAX_REPLAY_EVENTS = 128;
19​
20export const THREAD_RESUME_MAX_REPLAY_ENCODED_BYTES = 1_048_576;
21​
22/** Decoded objects and temporary strings exceed stored JSON size, even before wire slimming. */
23export const THREAD_RESUME_MAX_RAW_PAYLOAD_BYTES = 1_048_576;
24​
25export function isThreadReplayRawPayloadSafe(rawPayloadBytes: number): boolean {
26 return rawPayloadBytes <= THREAD_RESUME_MAX_RAW_PAYLOAD_BYTES;
28​
29export function threadReplayEncodedBytes(items: ReadonlyArray<unknown>): number {
30 let total = 0;
31 for (const item of items) {
32 const encoded = JSON.stringify(item);
33 total += Buffer.byteLength(encoded ?? "", "utf8");
34 }
35 return total;
37​
38type ThreadSnapshotStreamItem = Extract<
39 OrchestrationV2ThreadStreamItem,
40 { readonly kind: "snapshot" }
41>;
42​
43export function boundedSnapshotResponseFields(input: {
44 readonly bounded: BoundedProjectionResult;
45 readonly compactTurnItems: boolean;
46 readonly dedupeBodies: boolean;
47 readonly compactIds: boolean;
48}) {
49 const { bounded } = input;
50 const capped = capProjectionCheckpointFilesForWire(bounded.projection);
51 // Dedupe keeps shared items one object, which the compaction's identity check needs.
52 const projection = input.dedupeBodies ? dedupeSnapshotBodies(capped) : capped;
53 const compactProjection = input.compactTurnItems ? omitLocalVisibleTurnItems(projection) : null;
54 return {
55 projection: compactProjection ?? projection,
56 historyCursor: bounded.historyCursor,
57 hasMoreHistory: bounded.hasMoreHistory,
58 latestLocalTurnOrdinal: bounded.latestLocalTurnOrdinal,
59 ...(compactProjection === null ? {} : { turnItemsOmitLocalVisible: true as const }),
60 ...(input.compactIds ? { idTable: true as const } : {}),
61 };
63​
64export function buildBoundedThreadStreamSnapshot(input: {
65 readonly snapshotSequence: number;
66 readonly projection: OrchestrationV2ThreadProjection;
67 readonly compactTurnItems?: boolean;
68 readonly dedupeBodies?: boolean;
69 readonly compactIds?: boolean;
70}): ThreadSnapshotStreamItem {
71 const bounded = buildBoundedThreadProjection({
72 snapshotSequence: input.snapshotSequence,
73 projection: projectThreadProjectionForWire(input.projection),
74 });
75 return {
76 kind: "snapshot",
77 snapshotSequence: input.snapshotSequence,
78 ...boundedSnapshotResponseFields({
79 bounded,
80 compactTurnItems: input.compactTurnItems === true,
81 dedupeBodies: input.dedupeBodies === true,
82 compactIds: input.compactIds === true,
83 }),
84 };
86​
87export type ThreadResumePlan =
88 | {
89 readonly mode: "replay";
90 readonly afterSequence: number;
91 readonly throughSequence: number;
92 }
93 | { readonly mode: "snapshot" };
94​
95/** Bound event count and encoded bytes; replace excessive replay with one current snapshot. */
96export function decideThreadResume(input: {
97 readonly afterSequence: number;
98 readonly highWater: number;
99 readonly replayEventCount: number;
100 readonly replayEncodedBytes: number;
101}): ThreadResumePlan {
102 if (
103 input.afterSequence > input.highWater ||
104 input.replayEventCount > THREAD_RESUME_MAX_REPLAY_EVENTS ||
105 input.replayEncodedBytes > THREAD_RESUME_MAX_REPLAY_ENCODED_BYTES
106 ) {
107 return { mode: "snapshot" };
108 }
109 return {
110 mode: "replay",
111 afterSequence: input.afterSequence,
112 throughSequence: input.highWater,
113 };