@wjarka/cezarion 0.17.0-pr2883.4838 → 0.17.0-pr2885.4841
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.
- package/dist/core/opencode-server-runner.js +225 -215
- package/dist/core/opencode-server-runner.js.map +1 -1
- package/dist/server/server.d.ts +60 -60
- package/package.json +3 -3
- package/scripts/benchmark-run-store.ts +61 -41
- package/scripts/mock-claude-model-refusal.mjs +53 -19
- package/scripts/mock-codex-reverse-steer.mjs +45 -32
|
@@ -236,96 +236,94 @@ class OpencodeSession {
|
|
|
236
236
|
this.baseUrl = await urlReady;
|
|
237
237
|
await this.bootstrap();
|
|
238
238
|
})();
|
|
239
|
-
this.result = (
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
finally {
|
|
252
|
-
if (deadline)
|
|
253
|
-
clearTimeout(deadline);
|
|
254
|
-
if (this.autoEndTimer)
|
|
255
|
-
clearTimeout(this.autoEndTimer);
|
|
256
|
-
this.sse.abort();
|
|
257
|
-
this.serverOpen = false;
|
|
258
|
-
this.clearLostWakeTimer();
|
|
259
|
-
this.agentRequest?.abort();
|
|
260
|
-
this.expirePermissions('session ended');
|
|
261
|
-
this.terminate();
|
|
262
|
-
}
|
|
263
|
-
const serve = this.serve;
|
|
264
|
-
await serve.exited;
|
|
265
|
-
await serve.outputDrained;
|
|
266
|
-
if (serve.spawnFailed) {
|
|
267
|
-
this.emit({ type: 'error', message: serve.spawnFailed.message });
|
|
268
|
-
throw serve.spawnFailed;
|
|
269
|
-
}
|
|
270
|
-
// SSE closure can precede the process exit event. Choose one authoritative
|
|
271
|
-
// error after settlement, before the synthetic turn-end, retaining the code.
|
|
272
|
-
const code = serve.child.exitCode;
|
|
273
|
-
const signal = serve.child.signalCode;
|
|
274
|
-
const crashed = (signal !== null && !serve.signalled)
|
|
275
|
-
|| (code !== null && code !== 0 && !(serve.signalled && isSignalTerminationExit(code)));
|
|
276
|
-
if (!this.timedOut && (crashed || this.exitFailure)) {
|
|
277
|
-
const stderr = serve.stderrChunks.join('');
|
|
278
|
-
const detail = summarizeRunnerStderr(stderr);
|
|
279
|
-
if (this.retrySpawnError) {
|
|
280
|
-
if (stderr.trim())
|
|
281
|
-
this.emit({ type: 'note', message: `opencode serve stderr:\n${stderr}` });
|
|
282
|
-
this.emit({
|
|
283
|
-
type: 'error',
|
|
284
|
-
message: `opencode serve exited before listening (${describeEarlyExit(serve)}) and its retry could not start: ${this.retrySpawnError.message}`,
|
|
285
|
-
});
|
|
286
|
-
}
|
|
287
|
-
else if (this.failedStart) {
|
|
288
|
-
// #872: the retry failed too — one error naming both attempts.
|
|
289
|
-
const attempts = [this.failedStart, serve];
|
|
290
|
-
const notes = attempts.map((attempt, i) => {
|
|
291
|
-
const text = attempt.stderrChunks.join('');
|
|
292
|
-
return text.trim() ? `opencode serve stderr (attempt ${i + 1}):\n${text}` : '';
|
|
293
|
-
}).filter(Boolean);
|
|
294
|
-
if (notes.length)
|
|
295
|
-
this.emit({ type: 'note', message: notes.join('\n') });
|
|
296
|
-
const described = attempts.map((attempt, i) => `attempt ${i + 1}: ${describeEarlyExit(attempt)}`);
|
|
297
|
-
this.emit({ type: 'error', message: `opencode serve exited before listening on both attempts — ${described.join('; ')}` });
|
|
298
|
-
}
|
|
299
|
-
else {
|
|
300
|
-
if (stderr.trim())
|
|
301
|
-
this.emit({ type: 'note', message: `opencode serve stderr:\n${stderr}` });
|
|
302
|
-
const message = crashed
|
|
303
|
-
? `opencode serve exited with ${describeExit(serve)}${detail ? ` — ${detail}` : ''}`
|
|
304
|
-
: this.exitFailure;
|
|
305
|
-
this.emit({ type: 'error', message });
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
// Timeout/interrupt can cut the SSE feed before its `session.idle` —
|
|
309
|
-
// close the turn (idempotent) so consumers still see the v1 boundary,
|
|
310
|
-
// and recover prose buffered mid-part.
|
|
311
|
-
this.finishTurn();
|
|
312
|
-
this.textCoalescer.flush();
|
|
313
|
-
// Chunks are whole blocks now (one per finished part), so newline-join
|
|
314
|
-
// like the other runners, not the old delta concatenation.
|
|
315
|
-
const text = this.textChunks.join('\n').trim();
|
|
316
|
-
const base = {
|
|
317
|
-
text,
|
|
318
|
-
toolCalls: this.toolCalls,
|
|
319
|
-
tokensUsed: this.tokensUsed,
|
|
320
|
-
sessionId: this.sessionId ?? spec.sessionId,
|
|
321
|
-
};
|
|
322
|
-
if (this.timedOut) {
|
|
323
|
-
const mins = Math.round((limitMs / 60_000) * 10) / 10;
|
|
324
|
-
this.emit({ type: 'error', message: `opencode timed out after ${mins}m and was killed` });
|
|
239
|
+
this.result = this.finishSession(deadline, limitMs);
|
|
240
|
+
}
|
|
241
|
+
/** Wait out the child, then map its exit onto the v1 result. */
|
|
242
|
+
async finishSession(deadline, limitMs) {
|
|
243
|
+
try {
|
|
244
|
+
await this.ready;
|
|
245
|
+
// Live for the whole session; the SSE loop runs until end()/interrupt.
|
|
246
|
+
await this.serve.exited;
|
|
247
|
+
}
|
|
248
|
+
catch (err) {
|
|
249
|
+
if (!this.timedOut && !this.openingPromptFailed) {
|
|
250
|
+
this.exitFailure = `opencode: ${err instanceof Error ? err.message : String(err)}`;
|
|
325
251
|
}
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
252
|
+
}
|
|
253
|
+
finally {
|
|
254
|
+
if (deadline)
|
|
255
|
+
clearTimeout(deadline);
|
|
256
|
+
if (this.autoEndTimer)
|
|
257
|
+
clearTimeout(this.autoEndTimer);
|
|
258
|
+
this.sse.abort();
|
|
259
|
+
this.serverOpen = false;
|
|
260
|
+
this.clearLostWakeTimer();
|
|
261
|
+
this.agentRequest?.abort();
|
|
262
|
+
this.expirePermissions('session ended');
|
|
263
|
+
this.terminate();
|
|
264
|
+
}
|
|
265
|
+
const serve = this.serve;
|
|
266
|
+
await serve.exited;
|
|
267
|
+
await serve.outputDrained;
|
|
268
|
+
if (serve.spawnFailed) {
|
|
269
|
+
this.emit({ type: 'error', message: serve.spawnFailed.message });
|
|
270
|
+
throw serve.spawnFailed;
|
|
271
|
+
}
|
|
272
|
+
this.reportServeFailure(serve);
|
|
273
|
+
return this.resultFromCleanExit(limitMs);
|
|
274
|
+
}
|
|
275
|
+
reportServeFailure(serve) {
|
|
276
|
+
// SSE closure can precede the process exit event. Choose one authoritative
|
|
277
|
+
// error after settlement, before the synthetic turn-end, retaining the code.
|
|
278
|
+
const code = serve.child.exitCode;
|
|
279
|
+
const signal = serve.child.signalCode;
|
|
280
|
+
const crashed = (signal !== null && !serve.signalled)
|
|
281
|
+
|| (code !== null && code !== 0 && !(serve.signalled && isSignalTerminationExit(code)));
|
|
282
|
+
if (this.timedOut || (!crashed && !this.exitFailure))
|
|
283
|
+
return;
|
|
284
|
+
const stderr = serve.stderrChunks.join('');
|
|
285
|
+
const detail = summarizeRunnerStderr(stderr);
|
|
286
|
+
if (this.retrySpawnError) {
|
|
287
|
+
if (stderr.trim())
|
|
288
|
+
this.emit({ type: 'note', message: `opencode serve stderr:\n${stderr}` });
|
|
289
|
+
this.emit({ type: 'error', message: `opencode serve exited before listening (${describeEarlyExit(serve)}) and its retry could not start: ${this.retrySpawnError.message}` });
|
|
290
|
+
}
|
|
291
|
+
else if (this.failedStart) {
|
|
292
|
+
// #872: the retry failed too — one error naming both attempts.
|
|
293
|
+
const attempts = [this.failedStart, serve];
|
|
294
|
+
const notes = attempts.map((attempt, i) => {
|
|
295
|
+
const text = attempt.stderrChunks.join('');
|
|
296
|
+
return text.trim() ? `opencode serve stderr (attempt ${i + 1}):\n${text}` : '';
|
|
297
|
+
}).filter(Boolean);
|
|
298
|
+
if (notes.length)
|
|
299
|
+
this.emit({ type: 'note', message: notes.join('\n') });
|
|
300
|
+
const described = attempts.map((attempt, i) => `attempt ${i + 1}: ${describeEarlyExit(attempt)}`);
|
|
301
|
+
this.emit({ type: 'error', message: `opencode serve exited before listening on both attempts — ${described.join('; ')}` });
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
if (stderr.trim())
|
|
305
|
+
this.emit({ type: 'note', message: `opencode serve stderr:\n${stderr}` });
|
|
306
|
+
this.emit({ type: 'error', message: crashed ? `opencode serve exited with ${describeExit(serve)}${detail ? ` — ${detail}` : ''}` : this.exitFailure });
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
resultFromCleanExit(limitMs) {
|
|
310
|
+
// Timeout/interrupt can cut the SSE feed before its `session.idle` —
|
|
311
|
+
// close the turn (idempotent) so consumers still see the v1 boundary,
|
|
312
|
+
// and recover prose buffered mid-part.
|
|
313
|
+
this.finishTurn();
|
|
314
|
+
this.textCoalescer.flush();
|
|
315
|
+
// Chunks are whole blocks now (one per finished part), so newline-join
|
|
316
|
+
// like the other runners, not the old delta concatenation.
|
|
317
|
+
const base = {
|
|
318
|
+
text: this.textChunks.join('\n').trim(), toolCalls: this.toolCalls,
|
|
319
|
+
tokensUsed: this.tokensUsed, sessionId: this.sessionId ?? this.spec.sessionId,
|
|
320
|
+
};
|
|
321
|
+
if (this.timedOut) {
|
|
322
|
+
const mins = Math.round((limitMs / 60_000) * 10) / 10;
|
|
323
|
+
this.emit({ type: 'error', message: `opencode timed out after ${mins}m and was killed` });
|
|
324
|
+
}
|
|
325
|
+
this.emit({ type: 'done' });
|
|
326
|
+
return base;
|
|
329
327
|
}
|
|
330
328
|
get open() {
|
|
331
329
|
return this.serverOpen;
|
|
@@ -755,41 +753,44 @@ class OpencodeSession {
|
|
|
755
753
|
await this.http('POST', `/session/${this.sessionId}/prompt_async`, body, signal);
|
|
756
754
|
}
|
|
757
755
|
catch (err) {
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
this.emit({ type: '
|
|
778
|
-
// A dropped ACK can be the first sign of a process crash. Retain HTTP
|
|
779
|
-
// rejection details, but let settlement choose one error before turn-end.
|
|
780
|
-
this.openingPromptFailed = true;
|
|
781
|
-
this.exitFailure = `opencode: ${message}`;
|
|
756
|
+
this.reportPromptFailure(origin, err);
|
|
757
|
+
throw err;
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
reportPromptFailure(origin, err) {
|
|
761
|
+
// No turn started server-side, so no `session.idle` will ever close it —
|
|
762
|
+
// surface the failure and end the turn here instead of parking the run.
|
|
763
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
764
|
+
if (origin === 'agent' && this.providerErrorPending) {
|
|
765
|
+
// Latch the initiating provider failure before rejection reaches the
|
|
766
|
+
// manager's ACK handler (including grace expiry and explicit abort).
|
|
767
|
+
this.providerErrorPending.reported = true;
|
|
768
|
+
if (this.serverOpen)
|
|
769
|
+
this.emit({ type: 'error', message: this.providerErrorPending.message });
|
|
770
|
+
}
|
|
771
|
+
else if (origin === 'agent') {
|
|
772
|
+
// Fatal BEFORE the synthetic boundary: the rejected submission stays
|
|
773
|
+
// queued and must not drain more input or settle DONE.
|
|
774
|
+
if (this.serverOpen) {
|
|
775
|
+
this.emit({ type: 'error', message: `opencode: agent input failed: ${message}` });
|
|
782
776
|
this.interrupt();
|
|
783
777
|
}
|
|
784
|
-
else {
|
|
785
|
-
this.emit({ type: 'note', message: `opencode: prompt failed: ${message}` });
|
|
786
|
-
}
|
|
787
|
-
if (!this.providerErrorPending && origin !== 'opening')
|
|
788
|
-
this.finishTurn();
|
|
789
|
-
// Bootstrap still rejects; its result path reports the latched opening
|
|
790
|
-
// failure. Human follow-ups keep their nonfatal note and turn boundary.
|
|
791
|
-
throw err;
|
|
792
778
|
}
|
|
779
|
+
else if (origin === 'opening') {
|
|
780
|
+
this.emit({ type: 'note', message: `opencode: prompt failed: ${message}` });
|
|
781
|
+
// A dropped ACK can be the first sign of a process crash. Retain HTTP
|
|
782
|
+
// rejection details, but let settlement choose one error before turn-end.
|
|
783
|
+
this.openingPromptFailed = true;
|
|
784
|
+
this.exitFailure = `opencode: ${message}`;
|
|
785
|
+
this.interrupt();
|
|
786
|
+
}
|
|
787
|
+
else {
|
|
788
|
+
this.emit({ type: 'note', message: `opencode: prompt failed: ${message}` });
|
|
789
|
+
}
|
|
790
|
+
if (!this.providerErrorPending && origin !== 'opening')
|
|
791
|
+
this.finishTurn();
|
|
792
|
+
// Bootstrap still rejects; its result path reports the latched opening
|
|
793
|
+
// failure. Human follow-ups keep their nonfatal note and turn boundary.
|
|
793
794
|
}
|
|
794
795
|
promptBody(text) {
|
|
795
796
|
const body = { parts: [{ type: 'text', text }] };
|
|
@@ -1034,26 +1035,7 @@ class OpencodeSession {
|
|
|
1034
1035
|
const type = evt.type ?? '';
|
|
1035
1036
|
const props = evt.properties ?? {};
|
|
1036
1037
|
if (type === 'message.updated' || type === 'message.created' || type === 'message.completed') {
|
|
1037
|
-
|
|
1038
|
-
const mid = stringField(info, 'id');
|
|
1039
|
-
const role = stringField(info, 'role');
|
|
1040
|
-
if (mid && role)
|
|
1041
|
-
this.msgRole.set(mid, role);
|
|
1042
|
-
// The model answers a steered user message: it has read it (#505).
|
|
1043
|
-
const parent = role === 'assistant' ? stringField(info, 'parentID') : undefined;
|
|
1044
|
-
const submission = parent ? this.userMessageSubmission.get(parent) : undefined;
|
|
1045
|
-
if (parent && submission) {
|
|
1046
|
-
this.userMessageSubmission.delete(parent);
|
|
1047
|
-
// After an idle, the server started a new run for steered input: a new turn.
|
|
1048
|
-
if (!this.turnActive && this.serverOpen) {
|
|
1049
|
-
this.openTurn();
|
|
1050
|
-
this.emitUi(opencodeTurnStarted);
|
|
1051
|
-
}
|
|
1052
|
-
const ids = this.submissions.consume(submission);
|
|
1053
|
-
if (ids.length)
|
|
1054
|
-
this.opts.onAgentInputConsumed?.(ids);
|
|
1055
|
-
}
|
|
1056
|
-
this.absorbUsage(info);
|
|
1038
|
+
this.handleMessageEvent(props);
|
|
1057
1039
|
}
|
|
1058
1040
|
else if (type === 'message.part.updated' || type === 'message.part.created') {
|
|
1059
1041
|
this.handlePart(props.part ?? props);
|
|
@@ -1062,15 +1044,7 @@ class OpencodeSession {
|
|
|
1062
1044
|
this.handlePermissionAsked(props);
|
|
1063
1045
|
}
|
|
1064
1046
|
else if (type === 'permission.replied') {
|
|
1065
|
-
|
|
1066
|
-
const nativeId = stringField(props, 'requestID');
|
|
1067
|
-
const pending = nativeId === undefined ? undefined : this.pendingPermissions.get(nativeId);
|
|
1068
|
-
// OpenCode publishes this before it answers the reply route, so a human reply in flight
|
|
1069
|
-
// that it echoes is that human's answer.
|
|
1070
|
-
if (pending) {
|
|
1071
|
-
const answered = pending.phase === 'answering' && pending.answer?.reply === stringField(props, 'reply');
|
|
1072
|
-
this.settlePermission(pending, answered ? { optionId: pending.answer.optionId } : { reason: 'resolved by OpenCode' });
|
|
1073
|
-
}
|
|
1047
|
+
this.handlePermissionReplied(props);
|
|
1074
1048
|
}
|
|
1075
1049
|
else if (type === 'session.idle') {
|
|
1076
1050
|
// THE turn-end signal. The SSE bus is server-wide, so a child session's
|
|
@@ -1081,42 +1055,63 @@ class OpencodeSession {
|
|
|
1081
1055
|
this.finishTurn();
|
|
1082
1056
|
}
|
|
1083
1057
|
else if (type === 'session.error') {
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1058
|
+
this.handleSessionError(props);
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
handleMessageEvent(props) {
|
|
1062
|
+
const info = props.info ?? props;
|
|
1063
|
+
const mid = stringField(info, 'id');
|
|
1064
|
+
const role = stringField(info, 'role');
|
|
1065
|
+
if (mid && role)
|
|
1066
|
+
this.msgRole.set(mid, role);
|
|
1067
|
+
// The model answers a steered user message: it has read it (#505).
|
|
1068
|
+
const parent = role === 'assistant' ? stringField(info, 'parentID') : undefined;
|
|
1069
|
+
const submission = parent ? this.userMessageSubmission.get(parent) : undefined;
|
|
1070
|
+
if (parent && submission) {
|
|
1071
|
+
this.userMessageSubmission.delete(parent);
|
|
1072
|
+
// After an idle, the server started a new run for steered input: a new turn.
|
|
1073
|
+
if (!this.turnActive && this.serverOpen) {
|
|
1074
|
+
this.openTurn();
|
|
1075
|
+
this.emitUi(opencodeTurnStarted);
|
|
1098
1076
|
}
|
|
1077
|
+
const ids = this.submissions.consume(submission);
|
|
1078
|
+
if (ids.length)
|
|
1079
|
+
this.opts.onAgentInputConsumed?.(ids);
|
|
1080
|
+
}
|
|
1081
|
+
this.absorbUsage(info);
|
|
1082
|
+
}
|
|
1083
|
+
handlePermissionReplied(props) {
|
|
1084
|
+
// OpenCode settled the request, in any phase: our own reply, another client's, or its own.
|
|
1085
|
+
const nativeId = stringField(props, 'requestID');
|
|
1086
|
+
const pending = nativeId === undefined ? undefined : this.pendingPermissions.get(nativeId);
|
|
1087
|
+
// OpenCode publishes this before it answers the reply route, so a human reply in flight
|
|
1088
|
+
// that it echoes is that human's answer.
|
|
1089
|
+
if (!pending)
|
|
1090
|
+
return;
|
|
1091
|
+
const answered = pending.phase === 'answering' && pending.answer?.reply === stringField(props, 'reply');
|
|
1092
|
+
this.settlePermission(pending, answered ? { optionId: pending.answer.optionId } : { reason: 'resolved by OpenCode' });
|
|
1093
|
+
}
|
|
1094
|
+
handleSessionError(props) {
|
|
1095
|
+
const warning = opencodeSkillWarning(props);
|
|
1096
|
+
if (warning) {
|
|
1097
|
+
this.emit({ type: 'note', message: warning });
|
|
1098
|
+
return;
|
|
1099
|
+
}
|
|
1100
|
+
// The wire's failure signal, now that the prompt POST returns
|
|
1101
|
+
// before the turn runs: forward it to v1, whose `error` events are what
|
|
1102
|
+
// run.ts records failed steps from — dropped, the terminal idle would
|
|
1103
|
+
// file a provider/auth failure as a successful step. The idle that
|
|
1104
|
+
// follows still closes the turn.
|
|
1105
|
+
const sid = stringField(props, 'sessionID');
|
|
1106
|
+
if (sid === undefined || sid === this.sessionId) {
|
|
1107
|
+
this.turnErrored = true;
|
|
1108
|
+
this.emit({ type: 'error', message: this.sessionErrorMessage(props.error) });
|
|
1099
1109
|
}
|
|
1100
1110
|
}
|
|
1101
1111
|
handlePart(part) {
|
|
1102
|
-
|
|
1103
|
-
// belong to another one (a sub-agent's stream is surfaced by v2 only).
|
|
1104
|
-
const partSession = stringField(part, 'sessionID');
|
|
1105
|
-
if (partSession !== undefined && this.sessionId !== undefined && partSession !== this.sessionId)
|
|
1112
|
+
if (!this.partBelongsToUs(part))
|
|
1106
1113
|
return;
|
|
1107
|
-
// Only surface parts of assistant messages — the user's own message streams
|
|
1108
|
-
// over the same feed. Role is known early (the message.updated event
|
|
1109
|
-
// precedes its parts); an unknown role means "not assistant yet" → skip.
|
|
1110
1114
|
const messageID = stringField(part, 'messageID');
|
|
1111
|
-
if (messageID && this.msgRole.get(messageID) !== 'assistant') {
|
|
1112
|
-
// A user message carrying submitted agent text: remember which submission it is.
|
|
1113
|
-
if (this.msgRole.get(messageID) === 'user' && stringField(part, 'type') === 'text' && !this.userMessageSubmission.has(messageID)) {
|
|
1114
|
-
const submission = this.submissions.findByText(stringField(part, 'text') ?? '');
|
|
1115
|
-
if (submission && ![...this.userMessageSubmission.values()].includes(submission))
|
|
1116
|
-
this.userMessageSubmission.set(messageID, submission);
|
|
1117
|
-
}
|
|
1118
|
-
return;
|
|
1119
|
-
}
|
|
1120
1115
|
const kind = stringField(part, 'type');
|
|
1121
1116
|
const id = stringField(part, 'id') ?? messageID ?? '';
|
|
1122
1117
|
if (kind === 'text') {
|
|
@@ -1134,43 +1129,58 @@ class OpencodeSession {
|
|
|
1134
1129
|
}
|
|
1135
1130
|
}
|
|
1136
1131
|
else if (kind === 'tool') {
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
const
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
};
|
|
1157
|
-
void capture.then(clearCapture, clearCapture);
|
|
1158
|
-
}
|
|
1159
|
-
const callId = id || `${name}-${this.toolsSeen.size}`;
|
|
1160
|
-
if (!this.toolsSeen.has(callId)) {
|
|
1161
|
-
this.toolsSeen.add(callId);
|
|
1162
|
-
this.toolCalls.push({ id: callId, name, input: state.input ?? state });
|
|
1163
|
-
this.emit({ type: 'tool-call', id: callId, tool: name, input: state.input ?? state });
|
|
1164
|
-
}
|
|
1165
|
-
if (status === 'completed' || status === 'error') {
|
|
1166
|
-
this.emit({
|
|
1167
|
-
type: 'tool-result',
|
|
1168
|
-
toolCallId: callId,
|
|
1169
|
-
result: safeStringify(state.output ?? state.result ?? state),
|
|
1170
|
-
isError: status === 'error',
|
|
1171
|
-
});
|
|
1132
|
+
this.handleToolPart(part, id, messageID);
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
partBelongsToUs(part) {
|
|
1136
|
+
// The SSE bus carries every session on the server — drop parts that
|
|
1137
|
+
// belong to another one (a sub-agent's stream is surfaced by v2 only).
|
|
1138
|
+
const partSession = stringField(part, 'sessionID');
|
|
1139
|
+
if (partSession !== undefined && this.sessionId !== undefined && partSession !== this.sessionId)
|
|
1140
|
+
return false;
|
|
1141
|
+
// Only surface parts of assistant messages — the user's own message streams
|
|
1142
|
+
// over the same feed. Role is known early (the message.updated event
|
|
1143
|
+
// precedes its parts); an unknown role means "not assistant yet" → skip.
|
|
1144
|
+
const messageID = stringField(part, 'messageID');
|
|
1145
|
+
if (messageID && this.msgRole.get(messageID) !== 'assistant') {
|
|
1146
|
+
// A user message carrying submitted agent text: remember which submission it is.
|
|
1147
|
+
if (this.msgRole.get(messageID) === 'user' && stringField(part, 'type') === 'text' && !this.userMessageSubmission.has(messageID)) {
|
|
1148
|
+
const submission = this.submissions.findByText(stringField(part, 'text') ?? '');
|
|
1149
|
+
if (submission && ![...this.userMessageSubmission.values()].includes(submission))
|
|
1150
|
+
this.userMessageSubmission.set(messageID, submission);
|
|
1172
1151
|
}
|
|
1152
|
+
return false;
|
|
1173
1153
|
}
|
|
1154
|
+
return true;
|
|
1155
|
+
}
|
|
1156
|
+
handleToolPart(part, id, messageID) {
|
|
1157
|
+
const state = part.state ?? {};
|
|
1158
|
+
const status = stringField(state, 'status');
|
|
1159
|
+
const name = stringField(part, 'tool') ?? stringField(part, 'name') ?? 'tool';
|
|
1160
|
+
this.maybeCaptureQuestion(part, state, status, name, messageID);
|
|
1161
|
+
const callId = id || `${name}-${this.toolsSeen.size}`;
|
|
1162
|
+
if (!this.toolsSeen.has(callId)) {
|
|
1163
|
+
this.toolsSeen.add(callId);
|
|
1164
|
+
this.toolCalls.push({ id: callId, name, input: state.input ?? state });
|
|
1165
|
+
this.emit({ type: 'tool-call', id: callId, tool: name, input: state.input ?? state });
|
|
1166
|
+
}
|
|
1167
|
+
if (status === 'completed' || status === 'error') {
|
|
1168
|
+
this.emit({ type: 'tool-result', toolCallId: callId, result: safeStringify(state.output ?? state.result ?? state), isError: status === 'error' });
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
maybeCaptureQuestion(part, state, status, name, messageID) {
|
|
1172
|
+
const questionPartId = stringField(part, 'id') ?? stringField(part, 'callID') ?? messageID;
|
|
1173
|
+
const questionInput = state.input ?? state;
|
|
1174
|
+
const questionReady = name === 'question' && (toCezarQuestions(questionInput) !== null || status !== 'pending');
|
|
1175
|
+
if (!questionReady || !this.turnActive || this.pendingQuestion !== undefined || this.questionCapture !== undefined || this.questionReply !== undefined || (questionPartId !== undefined && this.handledQuestionParts.has(questionPartId)))
|
|
1176
|
+
return;
|
|
1177
|
+
if (questionPartId)
|
|
1178
|
+
this.handledQuestionParts.add(questionPartId);
|
|
1179
|
+
const capture = this.captureQuestion(questionInput);
|
|
1180
|
+
this.questionCapture = capture;
|
|
1181
|
+
const clearCapture = () => { if (this.questionCapture === capture)
|
|
1182
|
+
this.questionCapture = undefined; };
|
|
1183
|
+
void capture.then(clearCapture, clearCapture);
|
|
1174
1184
|
}
|
|
1175
1185
|
/** Pull cumulative tokens/cost out of an assistant message info object. */
|
|
1176
1186
|
absorbUsage(info) {
|