forked from nexu-io/open-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-event-stream.ts
More file actions
974 lines (898 loc) · 33.9 KB
/
Copy pathjson-event-stream.ts
File metadata and controls
974 lines (898 loc) · 33.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
type JsonObject = Record<string, unknown>;
type StreamEvent = Record<string, unknown>;
type StreamEventHandler = (event: StreamEvent) => void;
type ParserKind = string;
type ParserState = {
cursorTextSoFar: string;
cursorTurnStart: number;
openCodeToolUses: Set<string>;
openCodeToolResults: Set<string>;
codexToolUses: Set<string>;
codexErrorEmitted: boolean;
codexPreviousEventWasAgentMessage: boolean;
codexLastAgentMessageEndedWithNewline: boolean;
// Per reasoning-item chars already emitted as thinking deltas, keyed by
// item id. Codex replays the accumulated summary text on every lifecycle
// event of the same item (started → updated → completed), so only the
// unseen suffix may be re-emitted.
codexReasoningEmittedByItem: Map<string, number>;
codexReasoningEmittedAny: boolean;
suppressNextArtifactText: boolean;
suppressDuplicateArtifactText: boolean;
artifactOpenCandidate: string;
pendingArtifactText: string;
};
type Usage = {
input_tokens?: number;
output_tokens?: number;
thought_tokens?: number;
cached_read_tokens?: number;
cached_write_tokens?: number;
};
function isRecord(value: unknown): value is JsonObject {
return value != null && typeof value === 'object' && !Array.isArray(value);
}
function safeParseJson(value: unknown): unknown {
if (value == null) return null;
if (typeof value === 'object') return value;
if (typeof value !== 'string') return null;
try {
return JSON.parse(value);
} catch {
return null;
}
}
function stringifyContent(value: unknown): string {
if (typeof value === 'string') return value;
if (value == null) return '';
try {
return JSON.stringify(value);
} catch {
return String(value);
}
}
function parseJsonObjectsFromContent(value: string): JsonObject[] {
const trimmed = value.trim();
if (!trimmed) return [];
const direct = safeParseJson(trimmed);
if (isRecord(direct)) return [direct];
const objects: JsonObject[] = [];
for (const line of trimmed.split(/\r?\n/u)) {
const parsedLine = safeParseJson(line.trim());
if (isRecord(parsedLine)) objects.push(parsedLine);
}
return objects;
}
function extractConnectorApiError(value: JsonObject): JsonObject | null {
if (isRecord(value.error)) {
if (typeof value.error.code === 'string') return value.error;
if (isRecord(value.error.data) && isRecord(value.error.data.error)) {
const wrappedError = value.error.data.error;
if (typeof wrappedError.code === 'string') return wrappedError;
}
}
return null;
}
function connectorToolSelectionErrorMessage(content: string): string | null {
if (!content.includes('CONNECTOR_TOOL_NOT_FOUND')) return null;
let error: JsonObject | null = null;
for (const parsed of parseJsonObjectsFromContent(content)) {
const parsedError = extractConnectorApiError(parsed);
if (parsedError?.code === 'CONNECTOR_TOOL_NOT_FOUND') {
error = parsedError;
break;
}
}
if (!error) return null;
const details = isRecord(error.details) ? error.details : {};
const connectorId = typeof details.connectorId === 'string' && details.connectorId
? details.connectorId
: undefined;
const toolName = typeof details.toolName === 'string' && details.toolName
? details.toolName
: 'the requested connector tool';
const target = connectorId
? `Connector tool ${toolName} is not allowed for connector ${connectorId}.`
: `Connector tool ${toolName} is not allowed.`;
return `${target} Re-list the connector catalog and choose one of the currently allowed read-only tools.`;
}
function extractErrorMessage(value: unknown, fallback: string): string {
if (typeof value === 'string') {
const parsed = safeParseJson(value);
if (parsed && typeof parsed === 'object') {
return extractErrorMessage(parsed, value);
}
return value;
}
if (isRecord(value)) {
if (typeof value.detail === 'string' && value.detail) return value.detail;
if (typeof value.message === 'string' && value.message) {
return extractErrorMessage(value.message, value.message);
}
if (typeof value.error === 'string' && value.error) return value.error;
if (value.error && typeof value.error === 'object') {
return extractErrorMessage(value.error, fallback);
}
if (value.data && typeof value.data === 'object') {
const dataMessage = extractErrorMessage(value.data, '');
if (dataMessage) return dataMessage;
}
if (typeof value.name === 'string' && value.name) return value.name;
}
return fallback;
}
function isRecoverableCodexReconnect(message: string): boolean {
return (
message.startsWith('Reconnecting...') &&
(
message.includes('timeout waiting for child process to exit') ||
message.includes('stream disconnected before completion')
)
);
}
function formatOpenCodeUsage(tokens: unknown): Usage | null {
if (!isRecord(tokens)) return null;
const usage: Usage = {};
if (typeof tokens.input === 'number') usage.input_tokens = tokens.input;
if (typeof tokens.output === 'number') usage.output_tokens = tokens.output;
if (typeof tokens.reasoning === 'number') usage.thought_tokens = tokens.reasoning;
if (isRecord(tokens.cache)) {
if (typeof tokens.cache.read === 'number') usage.cached_read_tokens = tokens.cache.read;
if (typeof tokens.cache.write === 'number') usage.cached_write_tokens = tokens.cache.write;
}
return Object.keys(usage).length > 0 ? usage : null;
}
function isPowerShellErrorRecord(toolName: string, output: unknown): boolean {
const normalizedTool = toolName.toLowerCase();
if (normalizedTool !== 'bash' && normalizedTool !== 'shell') return false;
if (typeof output !== 'string') return false;
// A PowerShell non-terminating error can leave the shell process at exit 0.
// Require both canonical ErrorRecord fields so ordinary output containing a
// word such as "failed" does not become an error result.
return (
/(?:^|\r?\n)\s*\+\s*CategoryInfo\s*:/u.test(output) &&
/(?:^|\r?\n)\s*\+\s*FullyQualifiedErrorId\s*:/u.test(output)
);
}
function openCodeToolResult(
toolName: string,
statePart: JsonObject,
): { content: string; isError: boolean } | null {
const status = typeof statePart.status === 'string' ? statePart.status.toLowerCase() : '';
if (status !== 'completed' && status !== 'error' && status !== 'failed') return null;
const metadata = isRecord(statePart.metadata) ? statePart.metadata : {};
const exitCodes = [statePart.exit, statePart.exitCode, metadata.exit];
const hasNonZeroExit = exitCodes.some(
(exitCode) => typeof exitCode === 'number' && Number.isFinite(exitCode) && exitCode !== 0,
);
const explicitError =
(typeof statePart.error === 'string' && statePart.error.trim().length > 0) ||
(isRecord(statePart.error) && Object.keys(statePart.error).length > 0)
? statePart.error
: null;
const isError =
status === 'error' ||
status === 'failed' ||
explicitError !== null ||
hasNonZeroExit ||
isPowerShellErrorRecord(toolName, statePart.output);
const contentSource = explicitError ?? statePart.output;
return { content: stringifyContent(contentSource), isError };
}
function handleOpenCodeEvent(obj: unknown, onEvent: StreamEventHandler, state: ParserState): boolean {
if (!isRecord(obj)) return false;
const part = isRecord(obj.part) ? obj.part : {};
if (obj.type === 'step_start') {
// `sessionID` is OpenCode's own session handle (capture-style resume).
// Surface it on the step-start status so the daemon can persist it to
// `agent_sessions` and replay it as `run -s <id>` next turn. OpenCode
// stamps it on every stream event; step_start is the turn opener, so a
// create turn always exposes it here.
const sessionId =
typeof obj.sessionID === 'string' && obj.sessionID.length > 0
? obj.sessionID
: null;
onEvent({ type: 'status', label: 'running', sessionId });
return true;
}
if (obj.type === 'text' && typeof part.text === 'string' && part.text.length > 0) {
onEvent({ type: 'text_delta', delta: part.text });
return true;
}
if (obj.type === 'tool_use' && typeof part.tool === 'string' && typeof part.callID === 'string') {
const statePart = isRecord(part.state) ? part.state : null;
const key = `${obj.sessionID || 'session'}:${part.callID}`;
if (!state.openCodeToolUses.has(key)) {
state.openCodeToolUses.add(key);
onEvent({
type: 'tool_use',
id: part.callID,
name: part.tool,
input: safeParseJson(statePart?.input) ?? statePart?.input ?? null,
});
}
const result = statePart ? openCodeToolResult(part.tool, statePart) : null;
if (result && !state.openCodeToolResults.has(key)) {
state.openCodeToolResults.add(key);
onEvent({
type: 'tool_result',
toolUseId: part.callID,
content: result.content,
isError: result.isError,
});
}
return true;
}
if (obj.type === 'step_finish') {
const usage = formatOpenCodeUsage(part.tokens);
if (usage) {
onEvent({
type: 'usage',
usage,
costUsd: typeof part.cost === 'number' ? part.cost : undefined,
});
}
return true;
}
if (obj.type === 'error') {
// OpenCode emits structured error frames on stdout (e.g. provider auth
// failures, network errors, schema mismatches) and still exits 0. Surface
// them as proper `error` events so server.ts's `sendAgentEvent` wrapper
// can flip the run to `failed` and forward a visible SSE error to the
// chat UI. Previously we downgraded these to `type:'raw'`, which is not
// rendered as an assistant message — the run looked like a fast clean
// success while the user actually got nothing back. See issue #691.
//
// Shape mirrors the qoder-stream contract (`{type, message, raw}`) so
// the daemon's existing error-handling path recognises it without
// further wiring.
const message = extractErrorMessage(
obj.error ?? obj.message,
'OpenCode error',
);
onEvent({ type: 'error', message, raw: stringifyContent(obj) });
return true;
}
return false;
}
function handleGeminiEvent(obj: unknown, onEvent: StreamEventHandler, state: ParserState): boolean {
if (!isRecord(obj)) return false;
const isAssistantTextMessage =
obj.type === 'message' &&
obj.role === 'assistant' &&
typeof obj.content === 'string' &&
obj.content.length > 0;
if (!isAssistantTextMessage) {
flushPendingArtifactText(state, onEvent);
}
if (obj.type === 'init') {
onEvent({
type: 'status',
label: 'initializing',
model: typeof obj.model === 'string' ? obj.model : undefined,
});
return true;
}
if (obj.type === 'message' && obj.role === 'user') {
return true;
}
if (
obj.type === 'message' &&
obj.role === 'assistant' &&
typeof obj.content === 'string' &&
obj.content.length > 0
) {
const delta = stripDuplicateArtifactText(obj.content, state);
if (delta) onEvent({ type: 'text_delta', delta });
return true;
}
if (
obj.type === 'tool_use' &&
typeof obj.tool_id === 'string' &&
typeof obj.tool_name === 'string'
) {
const input = safeParseJson(obj.parameters) ?? obj.parameters ?? null;
if (obj.tool_name === 'write_todos') {
const todoInput = todoWriteInputFromParsedValue(input);
if (todoInput) {
onEvent({
type: 'tool_use',
id: `${obj.tool_id}:todo-native`,
name: 'TodoWrite',
input: todoInput,
});
return true;
}
}
if (isFileWriteToolUse(obj.tool_name, input)) {
state.suppressNextArtifactText = true;
}
onEvent({
type: 'tool_use',
id: obj.tool_id,
name: obj.tool_name,
input,
});
return true;
}
if (obj.type === 'tool_result' && typeof obj.tool_id === 'string') {
const error = isRecord(obj.error) ? obj.error : null;
const errorMessage = error ? extractErrorMessage(error, '') : '';
const output = typeof obj.output === 'string'
? obj.output
: errorMessage || stringifyContent(obj.output);
onEvent({
type: 'tool_result',
toolUseId: obj.tool_id,
content: output,
isError: obj.status === 'error' || Boolean(error),
});
return true;
}
if (obj.type === 'error') {
const severity = typeof obj.severity === 'string' ? obj.severity.toLowerCase() : '';
const message = extractErrorMessage(
obj.message ?? obj.error,
severity === 'warning' ? 'Gemini CLI warning' : 'Gemini CLI error',
);
if (severity === 'warning') {
onEvent({ type: 'status', label: 'warning', detail: message });
} else {
onEvent({ type: 'error', message, raw: stringifyContent(obj) });
}
return true;
}
if (obj.type === 'result') {
if (obj.status === 'error' || isRecord(obj.error)) {
onEvent({
type: 'error',
message: extractErrorMessage(obj.error, 'Gemini CLI error'),
raw: stringifyContent(obj),
});
return true;
}
if (!isRecord(obj.stats)) return true;
const usage: Usage = {};
if (typeof obj.stats.input_tokens === 'number') usage.input_tokens = obj.stats.input_tokens;
if (typeof obj.stats.output_tokens === 'number') usage.output_tokens = obj.stats.output_tokens;
if (typeof obj.stats.cached === 'number') usage.cached_read_tokens = obj.stats.cached;
onEvent({
type: 'usage',
usage,
durationMs: typeof obj.stats.duration_ms === 'number' ? obj.stats.duration_ms : undefined,
});
return true;
}
return false;
}
function handleKimiEvent(obj: unknown, onEvent: StreamEventHandler): boolean {
if (!isRecord(obj)) return false;
if (obj.role === 'assistant' && Array.isArray(obj.tool_calls)) {
for (const rawCall of obj.tool_calls) {
const call = isRecord(rawCall) ? rawCall : null;
const fn = isRecord(call?.function) ? call.function : null;
const id = typeof call?.id === 'string' && call.id.trim()
? call.id.trim()
: null;
const name = typeof fn?.name === 'string' && fn.name.trim()
? fn.name.trim()
: null;
if (!id || !name) continue;
const input = safeParseJson(fn?.arguments) ?? fn?.arguments ?? null;
onEvent({ type: 'tool_use', id, name, input });
}
return true;
}
if (
obj.role === 'tool' &&
typeof obj.tool_call_id === 'string' &&
obj.tool_call_id.trim()
) {
onEvent({
type: 'tool_result',
toolUseId: obj.tool_call_id.trim(),
content: stringifyContent(obj.content),
isError: false,
});
return true;
}
if (
obj.role === 'assistant' &&
typeof obj.content === 'string' &&
obj.content.length > 0
) {
onEvent({ type: 'text_delta', delta: obj.content });
return true;
}
if (obj.role === 'meta' && obj.type === 'session.resume_hint') {
return true;
}
return false;
}
function extractCursorText(message: unknown): string {
const content = isRecord(message) ? message.content : undefined;
const blocks = Array.isArray(content) ? content : [];
return blocks
.filter((block): block is { type: 'text'; text: string } => isRecord(block) && block.type === 'text' && typeof block.text === 'string')
.map((block) => block.text)
.join('');
}
function normalizeTodoStatus(value: unknown): string {
const status = typeof value === 'string'
? value.trim().toLowerCase().replace(/[-\s]+/g, '_')
: '';
if (status === 'completed' || status === 'complete' || status === 'done' || status.startsWith('completed')) {
return 'completed';
}
if (status === 'in_progress' || status === 'doing' || status === 'active' || status.startsWith('in_progress')) {
return 'in_progress';
}
if (
status === 'stopped' ||
status === 'failed' ||
status === 'blocked' ||
status === 'canceled' ||
status === 'cancelled' ||
status.startsWith('stopped') ||
status.startsWith('failed') ||
status.startsWith('blocked') ||
status.startsWith('canceled') ||
status.startsWith('cancelled')
) {
return 'stopped';
}
return 'pending';
}
function todoWriteInputFromItems(items: unknown): JsonObject | null {
if (!Array.isArray(items)) return null;
const todos = items
.map((raw): JsonObject | null => {
if (!isRecord(raw)) return null;
const content = typeof raw.content === 'string'
? raw.content
: typeof raw.label === 'string'
? raw.label
: typeof raw.description === 'string'
? raw.description
: typeof raw.text === 'string'
? raw.text
: '';
if (!content) return null;
return {
content,
status: raw.completed === true
? 'completed'
: normalizeTodoStatus(raw.status),
};
})
.filter((todo): todo is JsonObject => todo !== null);
return todos.length > 0 ? { todos } : null;
}
function todoWriteInputFromParsedValue(value: unknown): JsonObject | null {
if (Array.isArray(value)) return todoWriteInputFromItems(value);
if (!isRecord(value)) return null;
if (Array.isArray(value.todos)) return todoWriteInputFromItems(value.todos);
if (Array.isArray(value.todo)) return todoWriteInputFromItems(value.todo);
return null;
}
function stripDuplicateArtifactText(text: string, state: ParserState): string {
if (
!state.suppressNextArtifactText &&
!state.suppressDuplicateArtifactText &&
state.artifactOpenCandidate.length === 0
) {
return text;
}
const openTag = '<artifact';
const current = `${state.artifactOpenCandidate}${text}`;
state.artifactOpenCandidate = '';
if (state.suppressDuplicateArtifactText) {
const closeIndex = current.indexOf('</artifact>');
if (closeIndex === -1) return '';
state.suppressDuplicateArtifactText = false;
state.suppressNextArtifactText = false;
return stripDuplicateArtifactText(current.slice(closeIndex + '</artifact>'.length), state);
}
const openIndex = current.indexOf(openTag);
if (openIndex === -1) {
const candidateLength = artifactOpenCandidateLength(current, openTag);
if (state.suppressNextArtifactText && candidateLength > 0) {
state.artifactOpenCandidate = current.slice(-candidateLength);
return current.slice(0, -candidateLength);
}
if (state.suppressNextArtifactText) {
state.suppressNextArtifactText = false;
return current;
}
return current;
}
state.suppressDuplicateArtifactText = true;
state.suppressNextArtifactText = false;
const prefix = `${state.pendingArtifactText}${current.slice(0, openIndex)}`;
state.pendingArtifactText = '';
return `${prefix}${stripDuplicateArtifactText(current.slice(openIndex), state)}`;
}
function artifactOpenCandidateLength(text: string, openTag: string): number {
const max = Math.min(openTag.length - 1, text.length);
for (let len = max; len > 0; len -= 1) {
if (openTag.startsWith(text.slice(-len))) return len;
}
return 0;
}
function flushPendingArtifactText(state: ParserState, onEvent: StreamEventHandler): void {
const delta = `${state.pendingArtifactText}${state.artifactOpenCandidate}`;
if (!delta) return;
state.pendingArtifactText = '';
state.artifactOpenCandidate = '';
state.suppressNextArtifactText = false;
onEvent({ type: 'text_delta', delta });
}
function isFileWriteToolUse(toolName: string, input: unknown): boolean {
if (!isRecord(input)) return false;
const path = typeof input.file_path === 'string'
? input.file_path
: typeof input.path === 'string'
? input.path
: '';
const writesFile = toolName === 'write_file' ||
toolName === 'write' ||
toolName === 'replace' ||
toolName === 'edit';
if (!writesFile) return false;
if (/\.(html|htm|css|js|jsx|ts|tsx|md)$/iu.test(path)) return true;
return typeof input.content === 'string' || typeof input.new_string === 'string';
}
function codexTodoListInput(item: JsonObject): JsonObject | null {
if (item.type !== 'todo_list' || !Array.isArray(item.items)) return null;
return todoWriteInputFromItems(item.items);
}
function emitCodexTodoList(item: JsonObject, onEvent: StreamEventHandler): boolean {
if (typeof item.id !== 'string') return false;
const input = codexTodoListInput(item);
if (!input) return false;
onEvent({
type: 'tool_use',
id: item.id,
name: 'TodoWrite',
input,
});
return true;
}
function emitCursorTextDelta(text: string, onEvent: StreamEventHandler, state: ParserState): void {
// Timestamped assistant events WITHOUT `model_call_id` are cursor-agent's
// real-time incremental deltas (`--stream-partial-output`): the final turn
// text is the in-order concatenation of every such delta. Emit each one
// verbatim — do NOT dedupe by content. Legitimately repeated deltas
// (`"ha"`, `"ha"` -> `"haha"`) or a delta that happens to be a prefix of
// earlier text are real content, not duplicates; content-based prefix or
// equality checks would silently drop them. Duplicate suppression and
// dropped-chunk recovery belong to the buffered terminal replay paths
// (`model_call_id` and no-timestamp events) via reconcileCursorTurnReplay.
if (!text) return;
state.cursorTextSoFar += text;
onEvent({ type: 'text_delta', delta: text });
}
/**
* Reconcile a Cursor terminal replay against the text already emitted for the
* CURRENT turn. A terminal replay (either a `model_call_id` message or a
* non-timestamped final assistant message) carries the full text for the
* current turn only, so it must be compared against
* `cursorTextSoFar.slice(cursorTurnStart)` — NOT the whole cross-turn buffer,
* which would miss the current-turn prefix on later turns and re-append the
* whole replay (duplicate output like "secondsecond turn").
*
* Only a verified prefix permits suffix recovery: if the emitted turn text is
* a prefix of the replay (including the empty case where no chunk arrived),
* emit the missing suffix. On divergence (a non-final chunk was dropped, so
* the emitted text is not a prefix) leave the append-only stream untouched
* rather than duplicate already-shown text. Always advances the turn boundary.
*/
function reconcileCursorTurnReplay(text: string, onEvent: StreamEventHandler, state: ParserState): void {
const emittedTurn = state.cursorTextSoFar.slice(state.cursorTurnStart);
if (text && text !== emittedTurn && text.startsWith(emittedTurn)) {
const suffix = text.slice(emittedTurn.length);
if (suffix) onEvent({ type: 'text_delta', delta: suffix });
state.cursorTextSoFar += suffix;
}
state.cursorTurnStart = state.cursorTextSoFar.length;
}
function handleCursorEvent(obj: unknown, onEvent: StreamEventHandler, state: ParserState): boolean {
if (!isRecord(obj)) return false;
if (obj.type === 'system' && obj.subtype === 'init') {
onEvent({
type: 'status',
label: 'initializing',
model: typeof obj.model === 'string' ? obj.model : undefined,
});
return true;
}
if (obj.type === 'assistant' && obj.message) {
// Cursor sends a final assistant message that replays the full text for
// the current turn — either tagged with `model_call_id`, or (fallback)
// as a non-timestamped terminal assistant message. Both are reconciled
// against the current turn's emitted text via reconcileCursorTurnReplay.
if (typeof obj.model_call_id === 'string') {
const text = extractCursorText(obj.message);
reconcileCursorTurnReplay(text, onEvent, state);
return true;
}
const text = extractCursorText(obj.message);
if (!text) return false;
if (typeof obj.timestamp_ms === 'number') {
// Incremental streaming chunk within a turn — accumulate as usual.
emitCursorTextDelta(text, onEvent, state);
return true;
}
// Non-timestamped final assistant message: a terminal replay that marks a
// turn boundary. Reconcile against the current turn (not the whole
// cross-turn buffer) so later fallback-terminated turns do not duplicate
// output, then advance the turn boundary.
reconcileCursorTurnReplay(text, onEvent, state);
return true;
}
if (obj.type === 'result' && isRecord(obj.usage)) {
const usage: Usage = {};
if (typeof obj.usage.inputTokens === 'number') usage.input_tokens = obj.usage.inputTokens;
if (typeof obj.usage.outputTokens === 'number') usage.output_tokens = obj.usage.outputTokens;
if (typeof obj.usage.cacheReadTokens === 'number') {
usage.cached_read_tokens = obj.usage.cacheReadTokens;
}
if (typeof obj.usage.cacheWriteTokens === 'number') {
usage.cached_write_tokens = obj.usage.cacheWriteTokens;
}
onEvent({
type: 'usage',
usage,
durationMs: typeof obj.duration_ms === 'number' ? obj.duration_ms : undefined,
});
return true;
}
return false;
}
/**
* Codex streams model reasoning as summary items (`item.started` /
* `item.updated` / `item.completed` with `item.type === 'reasoning'`, the
* summary text accumulated on `item.text`). Emit the unseen suffix of each
* item as `thinking_delta` so the web's collapsible thinking block has real
* content behind the "Thinking" label; distinct reasoning items are joined
* with a blank line because the web folds consecutive thinking deltas into
* one block. Idempotent across repeated lifecycle events of the same item.
*/
function emitCodexReasoningItem(
obj: JsonObject,
onEvent: StreamEventHandler,
state: ParserState,
): boolean {
if (
obj.type !== 'item.started' &&
obj.type !== 'item.updated' &&
obj.type !== 'item.completed'
) {
return false;
}
if (!isRecord(obj.item) || obj.item.type !== 'reasoning') return false;
const key = typeof obj.item.id === 'string' ? obj.item.id : '';
const text = typeof obj.item.text === 'string' ? obj.item.text : '';
const emitted = state.codexReasoningEmittedByItem.get(key) ?? 0;
if (text.length > emitted) {
const suffix = text.slice(emitted);
const delta =
emitted === 0 && state.codexReasoningEmittedAny ? `\n\n${suffix}` : suffix;
onEvent({ type: 'thinking_delta', delta });
state.codexReasoningEmittedByItem.set(key, text.length);
state.codexReasoningEmittedAny = true;
}
return true;
}
function handleCodexEvent(obj: unknown, onEvent: StreamEventHandler, state: ParserState): boolean {
if (!isRecord(obj)) return false;
if (obj.type === 'error') {
const message = extractErrorMessage(obj.message ?? obj.error, 'Codex error');
// Reconnecting events are recoverable — treat as status warning, not fatal
if (isRecoverableCodexReconnect(message)) {
onEvent({ type: 'status', label: message });
return true;
}
if (!state.codexErrorEmitted) {
state.codexErrorEmitted = true;
onEvent({ type: 'error', message });
}
return true;
}
if (obj.type === 'turn.failed') {
if (!state.codexErrorEmitted) {
state.codexErrorEmitted = true;
onEvent({
type: 'error',
message: extractErrorMessage(obj.error ?? obj.message, 'Codex turn failed'),
});
}
return true;
}
if (obj.type === 'thread.started') {
// `thread_id` is Codex's own session handle, surfaced on the same
// `sessionId` status channel claude uses (claude-stream.ts). It serves two
// consumers: (1) the daemon persists it to `agent_sessions` and replays it
// as `exec resume <thread_id>` on the next turn (capture-style resume), and
// (2) it identifies this run's rollout file
// (`$CODEX_HOME/sessions/**/rollout-*-<thread_id>.jsonl`), the only place
// codex records per-call usage, which run_finished reads to recover the
// turn's first-call cache hit (codex's stream usage is cumulative only).
// Codex emits this both for a fresh `exec` and for `exec resume` (echoing
// the resumed id), so it is a stable capture point either way.
const threadId =
typeof obj.thread_id === 'string' && obj.thread_id.length > 0
? obj.thread_id
: null;
onEvent({ type: 'status', label: 'initializing', sessionId: threadId });
return true;
}
if (obj.type === 'turn.started') {
state.codexPreviousEventWasAgentMessage = false;
state.codexLastAgentMessageEndedWithNewline = false;
onEvent({ type: 'status', label: 'thinking' });
return true;
}
if (emitCodexReasoningItem(obj, onEvent, state)) return true;
if (obj.type === 'item.started' && isRecord(obj.item)) {
const item = obj.item;
if (emitCodexTodoList(item, onEvent)) {
state.codexPreviousEventWasAgentMessage = false;
state.codexLastAgentMessageEndedWithNewline = false;
return true;
}
if (item.type === 'command_execution' && typeof item.id === 'string') {
state.codexPreviousEventWasAgentMessage = false;
state.codexLastAgentMessageEndedWithNewline = false;
if (!state.codexToolUses.has(item.id)) {
state.codexToolUses.add(item.id);
onEvent({
type: 'tool_use',
id: item.id,
name: 'Bash',
input: {
command: typeof item.command === 'string' ? item.command : '',
},
});
}
return true;
}
}
if (obj.type === 'item.updated' && isRecord(obj.item)) {
const item = obj.item;
if (emitCodexTodoList(item, onEvent)) {
state.codexPreviousEventWasAgentMessage = false;
state.codexLastAgentMessageEndedWithNewline = false;
return true;
}
}
if (obj.type === 'item.completed' && isRecord(obj.item)) {
const item = obj.item;
if (emitCodexTodoList(item, onEvent)) {
state.codexPreviousEventWasAgentMessage = false;
state.codexLastAgentMessageEndedWithNewline = false;
return true;
}
// Codex reports non-fatal in-stream notices (e.g. the skills
// context-budget warning) as `error` ITEMS while the turn keeps running;
// fatal failures arrive separately as top-level `error` / `turn.failed`
// events. Surface these as a visible warning pill instead of dropping
// them as raw noise — during a silent provider hang such an item can be
// the only signal the user ever gets (incident recvqgLmAkUM6G).
if (item.type === 'error' && typeof item.message === 'string' && item.message.length > 0) {
onEvent({ type: 'status', label: 'warning', detail: item.message });
return true;
}
if (item.type === 'command_execution' && typeof item.id === 'string') {
state.codexPreviousEventWasAgentMessage = false;
state.codexLastAgentMessageEndedWithNewline = false;
if (!state.codexToolUses.has(item.id)) {
state.codexToolUses.add(item.id);
onEvent({
type: 'tool_use',
id: item.id,
name: 'Bash',
input: {
command: typeof item.command === 'string' ? item.command : '',
},
});
}
const content = stringifyContent(item.aggregated_output ?? '');
onEvent({
type: 'tool_result',
toolUseId: item.id,
content,
isError: typeof item.exit_code === 'number' ? item.exit_code !== 0 : item.status === 'failed',
});
const connectorToolError = connectorToolSelectionErrorMessage(content);
if (connectorToolError && !state.codexErrorEmitted) {
state.codexErrorEmitted = true;
onEvent({ type: 'error', message: connectorToolError });
}
return true;
}
}
if (
obj.type === 'item.completed' &&
isRecord(obj.item) &&
obj.item.type === 'agent_message' &&
typeof obj.item.text === 'string' &&
obj.item.text.length > 0
) {
const text = obj.item.text;
const needsBoundary =
state.codexPreviousEventWasAgentMessage &&
!state.codexLastAgentMessageEndedWithNewline &&
!text.startsWith('\n');
const delta = needsBoundary ? `\n${text}` : text;
onEvent({ type: 'text_delta', delta });
state.codexPreviousEventWasAgentMessage = true;
state.codexLastAgentMessageEndedWithNewline = text.endsWith('\n');
return true;
}
if (obj.type === 'turn.completed' && isRecord(obj.usage)) {
const usage: Usage = {};
if (typeof obj.usage.input_tokens === 'number') usage.input_tokens = obj.usage.input_tokens;
if (typeof obj.usage.output_tokens === 'number') usage.output_tokens = obj.usage.output_tokens;
if (typeof obj.usage.reasoning_output_tokens === 'number') {
usage.thought_tokens = obj.usage.reasoning_output_tokens;
}
if (typeof obj.usage.cached_input_tokens === 'number') {
usage.cached_read_tokens = obj.usage.cached_input_tokens;
}
onEvent({ type: 'usage', usage });
return true;
}
return false;
}
export function createJsonEventStreamHandler(kind: ParserKind, onEvent: StreamEventHandler) {
let buffer = '';
const state: ParserState = {
cursorTextSoFar: '',
cursorTurnStart: 0,
openCodeToolUses: new Set<string>(),
openCodeToolResults: new Set<string>(),
codexToolUses: new Set<string>(),
codexErrorEmitted: false,
codexPreviousEventWasAgentMessage: false,
codexLastAgentMessageEndedWithNewline: false,
codexReasoningEmittedByItem: new Map<string, number>(),
codexReasoningEmittedAny: false,
suppressNextArtifactText: false,
suppressDuplicateArtifactText: false,
artifactOpenCandidate: '',
pendingArtifactText: '',
};
function handleLine(line: string): void {
let obj: unknown;
try {
obj = JSON.parse(line);
} catch {
onEvent({ type: 'raw', line });
return;
}
if (kind === 'opencode' && handleOpenCodeEvent(obj, onEvent, state)) return;
if (kind === 'gemini' && handleGeminiEvent(obj, onEvent, state)) return;
if (kind === 'kimi' && handleKimiEvent(obj, onEvent)) return;
if (kind === 'cursor-agent' && handleCursorEvent(obj, onEvent, state)) return;
if (kind === 'codex' && handleCodexEvent(obj, onEvent, state)) return;
onEvent({ type: 'raw', line });
}
function feed(chunk: string): void {
buffer += chunk;
let nl;
while ((nl = buffer.indexOf('\n')) !== -1) {
const line = buffer.slice(0, nl).trim();
buffer = buffer.slice(nl + 1);
if (!line) continue;
handleLine(line);
}
}
function flush(): void {
const rem = buffer.trim();
buffer = '';
if (rem) handleLine(rem);
flushPendingArtifactText(state, onEvent);
}
return { feed, flush };
}