fix: Handle response with only tool call or reasoning (#160)
This commit is contained in:
@@ -15,6 +15,7 @@ import { getModelUpdateArgs } from "./agent/model";
|
|||||||
import { SessionStats } from "./agent/stats";
|
import { SessionStats } from "./agent/stats";
|
||||||
import {
|
import {
|
||||||
createBuffers,
|
createBuffers,
|
||||||
|
type Line,
|
||||||
markIncompleteToolsAsCancelled,
|
markIncompleteToolsAsCancelled,
|
||||||
toLines,
|
toLines,
|
||||||
} from "./cli/helpers/accumulator";
|
} from "./cli/helpers/accumulator";
|
||||||
@@ -867,16 +868,41 @@ export async function handleHeadlessCommand(
|
|||||||
// Update stats with final usage data from buffers
|
// Update stats with final usage data from buffers
|
||||||
sessionStats.updateUsageFromBuffers(buffers);
|
sessionStats.updateUsageFromBuffers(buffers);
|
||||||
|
|
||||||
// Extract final assistant message
|
// Extract final result from transcript, with sensible fallbacks
|
||||||
const lines = toLines(buffers);
|
const lines = toLines(buffers);
|
||||||
const lastAssistant = [...lines]
|
const reversed = [...lines].reverse();
|
||||||
.reverse()
|
|
||||||
.find((line) => line.kind === "assistant");
|
const lastAssistant = reversed.find(
|
||||||
|
(line) =>
|
||||||
|
line.kind === "assistant" &&
|
||||||
|
"text" in line &&
|
||||||
|
typeof line.text === "string" &&
|
||||||
|
line.text.trim().length > 0,
|
||||||
|
) as Extract<Line, { kind: "assistant" }> | undefined;
|
||||||
|
|
||||||
|
const lastReasoning = reversed.find(
|
||||||
|
(line) =>
|
||||||
|
line.kind === "reasoning" &&
|
||||||
|
"text" in line &&
|
||||||
|
typeof line.text === "string" &&
|
||||||
|
line.text.trim().length > 0,
|
||||||
|
) as Extract<Line, { kind: "reasoning" }> | undefined;
|
||||||
|
|
||||||
|
const lastToolResult = reversed.find(
|
||||||
|
(line) =>
|
||||||
|
line.kind === "tool_call" &&
|
||||||
|
"resultText" in line &&
|
||||||
|
typeof (line as Extract<Line, { kind: "tool_call" }>).resultText ===
|
||||||
|
"string" &&
|
||||||
|
((line as Extract<Line, { kind: "tool_call" }>).resultText ?? "").trim()
|
||||||
|
.length > 0,
|
||||||
|
) as Extract<Line, { kind: "tool_call" }> | undefined;
|
||||||
|
|
||||||
const resultText =
|
const resultText =
|
||||||
lastAssistant && "text" in lastAssistant
|
(lastAssistant && lastAssistant.text) ||
|
||||||
? lastAssistant.text
|
(lastReasoning && lastReasoning.text) ||
|
||||||
: "No assistant response found";
|
(lastToolResult && lastToolResult.resultText) ||
|
||||||
|
"No assistant response found";
|
||||||
|
|
||||||
// Output based on format
|
// Output based on format
|
||||||
if (outputFormat === "json") {
|
if (outputFormat === "json") {
|
||||||
@@ -930,10 +956,10 @@ export async function handleHeadlessCommand(
|
|||||||
console.log(JSON.stringify(resultEvent));
|
console.log(JSON.stringify(resultEvent));
|
||||||
} else {
|
} else {
|
||||||
// text format (default)
|
// text format (default)
|
||||||
if (!lastAssistant || !("text" in lastAssistant)) {
|
if (!resultText || resultText === "No assistant response found") {
|
||||||
console.error("No assistant response found");
|
console.error("No assistant response found");
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
console.log(lastAssistant.text);
|
console.log(resultText);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user