Skip to content

Commit acd4812

Browse files
[codex] prefer terminal reply fields in CLI JSONL parser
1 parent 60cfd3f commit acd4812

2 files changed

Lines changed: 106 additions & 8 deletions

File tree

‎src/agents/cli-output.test.ts‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,51 @@ describe("parseCliJsonl", () => {
422422
});
423423
});
424424

425+
it("prefers final-answer phase content over Gemini stream-json message text", () => {
426+
const result = parseCliJsonl(
427+
[
428+
JSON.stringify({
429+
type: "message",
430+
role: "assistant",
431+
content: "progress text",
432+
delta: true,
433+
}),
434+
JSON.stringify({
435+
type: "result",
436+
status: "success",
437+
stats: { total_tokens: 9, input_tokens: 4, output_tokens: 5 },
438+
}),
439+
JSON.stringify({
440+
type: "response_item",
441+
payload: {
442+
type: "message",
443+
role: "assistant",
444+
phase: "final_answer",
445+
content: [{ type: "output_text", text: "Final answer text" }],
446+
},
447+
}),
448+
].join("\n"),
449+
{
450+
command: "gemini",
451+
output: "jsonl",
452+
jsonlDialect: "gemini-stream-json",
453+
},
454+
"google-gemini-cli",
455+
);
456+
457+
expect(result).toEqual({
458+
text: "Final answer text",
459+
sessionId: undefined,
460+
usage: {
461+
input: 4,
462+
output: 5,
463+
cacheRead: undefined,
464+
cacheWrite: undefined,
465+
total: 9,
466+
},
467+
});
468+
});
469+
425470
it("parses Gemini stream-json result errors as provider errors", () => {
426471
const result = parseCliJsonl(
427472
[
@@ -1240,6 +1285,56 @@ describe("createCliJsonlStreamingParser", () => {
12401285
});
12411286
});
12421287

1288+
it("prefers streamed final-answer phase content over Gemini result output", () => {
1289+
const parser = createCliJsonlStreamingParser({
1290+
backend: {
1291+
command: "gemini",
1292+
output: "jsonl",
1293+
jsonlDialect: "gemini-stream-json",
1294+
},
1295+
providerId: "google-gemini-cli",
1296+
onAssistantDelta: () => {},
1297+
});
1298+
1299+
parser.push(
1300+
[
1301+
JSON.stringify({
1302+
type: "message",
1303+
role: "assistant",
1304+
content: "progress text",
1305+
delta: true,
1306+
}),
1307+
JSON.stringify({
1308+
type: "result",
1309+
status: "success",
1310+
stats: { total_tokens: 9, input_tokens: 4, output_tokens: 5 },
1311+
}),
1312+
JSON.stringify({
1313+
type: "response_item",
1314+
payload: {
1315+
type: "message",
1316+
role: "assistant",
1317+
phase: "final_answer",
1318+
content: [{ type: "output_text", text: "Final answer text" }],
1319+
},
1320+
}),
1321+
].join("\n"),
1322+
);
1323+
parser.finish();
1324+
1325+
expect(parser.getOutput()).toEqual({
1326+
text: "Final answer text",
1327+
sessionId: undefined,
1328+
usage: {
1329+
input: 4,
1330+
output: 5,
1331+
cacheRead: undefined,
1332+
cacheWrite: undefined,
1333+
total: 9,
1334+
},
1335+
});
1336+
});
1337+
12431338
it("keeps streamed commentary-only structured output empty instead of null", () => {
12441339
const parser = createCliJsonlStreamingParser({
12451340
backend: {

‎src/agents/cli-output.ts‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -970,16 +970,19 @@ export function createCliJsonlStreamingParser(params: {
970970
}
971971
},
972972
getOutput() {
973-
if (output) {
973+
if (output?.errorText) {
974974
return output;
975975
}
976-
if (isGeminiStreamJsonDialect(params) && (assistantText.trim() || sessionId || usage)) {
977-
return { text: assistantText.trim(), sessionId, usage };
978-
}
979976
const terminalText = taskCompleteText ?? finalAnswerText;
980977
if (terminalText) {
981978
return { text: terminalText, sessionId, usage };
982979
}
980+
if (output) {
981+
return output;
982+
}
983+
if (isGeminiStreamJsonDialect(params) && (assistantText.trim() || sessionId || usage)) {
984+
return { text: assistantText.trim(), sessionId, usage };
985+
}
983986
const text = texts.join("\n").trim();
984987
if (!text) {
985988
return sawStructuredOutput ? { text: "", sessionId, usage } : null;
@@ -1129,16 +1132,16 @@ export function parseCliJsonl(
11291132
if (isGeminiStreamJsonDialect({ backend, providerId }) && geminiErrorText) {
11301133
return { text: "", sessionId, usage, errorText: geminiErrorText };
11311134
}
1135+
const terminalText = taskCompleteText ?? finalAnswerText;
1136+
if (terminalText) {
1137+
return { text: terminalText, sessionId, usage };
1138+
}
11321139
if (
11331140
isGeminiStreamJsonDialect({ backend, providerId }) &&
11341141
(sawGeminiStructuredOutput || sessionId || usage)
11351142
) {
11361143
return { text: geminiText.trim(), sessionId, usage };
11371144
}
1138-
const terminalText = taskCompleteText ?? finalAnswerText;
1139-
if (terminalText) {
1140-
return { text: terminalText, sessionId, usage };
1141-
}
11421145
const text = texts.join("\n").trim();
11431146
if (!text) {
11441147
return sawStructuredOutput ? { text: "", sessionId, usage } : null;

0 commit comments

Comments
 (0)