@linzumi/cli 0.0.5-beta → 0.0.6-beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,102 @@
1
+ /*
2
+ - Date: 2026-04-26
3
+ Spec: plans/2026-04-26-local-codex-driver-worldclass-spec.md
4
+ Relationship: Owns the shared timer/chain lifecycle for runner-batched Codex
5
+ stream updates so assistant, reasoning, command-output, and file-change
6
+ deltas all follow one flush state machine.
7
+ */
8
+
9
+ export type StreamDeltaQueue<TDelta extends { readonly turnId: string | undefined }> = {
10
+ pending: TDelta[];
11
+ timer: ReturnType<typeof setTimeout> | undefined;
12
+ chain: Promise<void>;
13
+ };
14
+
15
+ export type StreamDeltaQueueRuntime<TDelta extends { readonly turnId: string | undefined }> = {
16
+ readonly flushIntervalMs: number;
17
+ readonly coalesce: (deltas: readonly TDelta[]) => TDelta[];
18
+ readonly forward: (delta: TDelta) => Promise<void>;
19
+ readonly firstTurnId: (deltas: readonly TDelta[]) => string | undefined;
20
+ readonly logFailure: (turnId: string | undefined, error: unknown) => void;
21
+ };
22
+
23
+ export function createStreamDeltaQueue<
24
+ TDelta extends { readonly turnId: string | undefined },
25
+ >(): StreamDeltaQueue<TDelta> {
26
+ return {
27
+ pending: [],
28
+ timer: undefined,
29
+ chain: Promise.resolve(),
30
+ };
31
+ }
32
+
33
+ export function enqueueStreamDelta<
34
+ TDelta extends { readonly turnId: string | undefined },
35
+ >(
36
+ queue: StreamDeltaQueue<TDelta>,
37
+ delta: TDelta,
38
+ runtime: StreamDeltaQueueRuntime<TDelta>,
39
+ ): void {
40
+ queue.pending.push(delta);
41
+ scheduleStreamDeltaFlush(queue, runtime);
42
+ }
43
+
44
+ export function flushStreamDeltaQueue<
45
+ TDelta extends { readonly turnId: string | undefined },
46
+ >(
47
+ queue: StreamDeltaQueue<TDelta>,
48
+ runtime: StreamDeltaQueueRuntime<TDelta>,
49
+ ): void {
50
+ clearStreamDeltaFlushTimer(queue);
51
+
52
+ const pending = queue.pending;
53
+
54
+ if (pending.length === 0) {
55
+ return;
56
+ }
57
+
58
+ queue.pending = [];
59
+ const deltas = runtime.coalesce(pending);
60
+ const previous = queue.chain;
61
+ const next = previous
62
+ .catch(() => undefined)
63
+ .then(async () => {
64
+ for (const delta of deltas) {
65
+ await runtime.forward(delta);
66
+ }
67
+ });
68
+
69
+ queue.chain = next.catch(error => {
70
+ runtime.logFailure(runtime.firstTurnId(deltas), error);
71
+ });
72
+ }
73
+
74
+ export function clearStreamDeltaFlushTimer<
75
+ TDelta extends { readonly turnId: string | undefined },
76
+ >(queue: StreamDeltaQueue<TDelta>): void {
77
+ if (queue.timer !== undefined) {
78
+ clearTimeout(queue.timer);
79
+ queue.timer = undefined;
80
+ }
81
+ }
82
+
83
+ function scheduleStreamDeltaFlush<
84
+ TDelta extends { readonly turnId: string | undefined },
85
+ >(
86
+ queue: StreamDeltaQueue<TDelta>,
87
+ runtime: StreamDeltaQueueRuntime<TDelta>,
88
+ ): void {
89
+ if (queue.timer !== undefined) {
90
+ return;
91
+ }
92
+
93
+ if (runtime.flushIntervalMs <= 0) {
94
+ flushStreamDeltaQueue(queue, runtime);
95
+ return;
96
+ }
97
+
98
+ queue.timer = setTimeout(() => {
99
+ queue.timer = undefined;
100
+ flushStreamDeltaQueue(queue, runtime);
101
+ }, runtime.flushIntervalMs);
102
+ }