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

ThreadStream.ts

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