From cf5c738eb92d7782c01bca932693636d0e1594ed Mon Sep 17 00:00:00 2001 From: Kevin Lin Date: Fri, 5 Dec 2025 22:19:31 -0800 Subject: [PATCH] fix: Handle response with only tool call or reasoning (#160) --- src/headless.ts | 44 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/src/headless.ts b/src/headless.ts index 2b1dcaa..59026a2 100644 --- a/src/headless.ts +++ b/src/headless.ts @@ -15,6 +15,7 @@ import { getModelUpdateArgs } from "./agent/model"; import { SessionStats } from "./agent/stats"; import { createBuffers, + type Line, markIncompleteToolsAsCancelled, toLines, } from "./cli/helpers/accumulator"; @@ -867,16 +868,41 @@ export async function handleHeadlessCommand( // Update stats with final usage data from buffers sessionStats.updateUsageFromBuffers(buffers); - // Extract final assistant message + // Extract final result from transcript, with sensible fallbacks const lines = toLines(buffers); - const lastAssistant = [...lines] - .reverse() - .find((line) => line.kind === "assistant"); + const reversed = [...lines].reverse(); + + const lastAssistant = reversed.find( + (line) => + line.kind === "assistant" && + "text" in line && + typeof line.text === "string" && + line.text.trim().length > 0, + ) as Extract | undefined; + + const lastReasoning = reversed.find( + (line) => + line.kind === "reasoning" && + "text" in line && + typeof line.text === "string" && + line.text.trim().length > 0, + ) as Extract | undefined; + + const lastToolResult = reversed.find( + (line) => + line.kind === "tool_call" && + "resultText" in line && + typeof (line as Extract).resultText === + "string" && + ((line as Extract).resultText ?? "").trim() + .length > 0, + ) as Extract | undefined; const resultText = - lastAssistant && "text" in lastAssistant - ? lastAssistant.text - : "No assistant response found"; + (lastAssistant && lastAssistant.text) || + (lastReasoning && lastReasoning.text) || + (lastToolResult && lastToolResult.resultText) || + "No assistant response found"; // Output based on format if (outputFormat === "json") { @@ -930,10 +956,10 @@ export async function handleHeadlessCommand( console.log(JSON.stringify(resultEvent)); } else { // text format (default) - if (!lastAssistant || !("text" in lastAssistant)) { + if (!resultText || resultText === "No assistant response found") { console.error("No assistant response found"); process.exit(1); } - console.log(lastAssistant.text); + console.log(resultText); } }