fix(agent): soften tool errors

This commit is contained in:
Jiayuan 2026-02-02 15:17:17 +08:00
parent 64878d9fe1
commit 32dd62747d
3 changed files with 88 additions and 4 deletions

View file

@ -112,6 +112,14 @@ describe("output", () => {
expect(extractResultDetails(result)).toEqual(result);
});
it("should prefer details when present", () => {
const result = {
content: [{ type: "text", text: "not json" }],
details: { count: 3, truncated: false },
};
expect(extractResultDetails(result)).toEqual({ count: 3, truncated: false });
});
it("should return direct object if no content array", () => {
const result = { count: 10, truncated: true };
expect(extractResultDetails(result)).toEqual({ count: 10, truncated: true });

View file

@ -118,6 +118,11 @@ export function extractResultDetails(result: unknown): Record<string, unknown> |
}
}
const withDetails = result as { details?: unknown };
if (withDetails.details && typeof withDetails.details === "object") {
return withDetails.details as Record<string, unknown>;
}
// Try direct object access
return result as Record<string, unknown>;
}
@ -252,8 +257,18 @@ export function createAgentOutput(params: {
}
case "tool_execution_end": {
// Stop spinner and show final result with summary
if (event.isError) {
const errorText = extractText(event.result) || "Tool failed";
const details = extractResultDetails(event.result);
const errorField = details?.error;
const hasError =
event.isError ||
Boolean(errorField) ||
details?.success === false;
if (hasError) {
const errorText =
(typeof details?.message === "string" && details.message) ||
(typeof errorField === "string" && errorField) ||
extractText(event.result) ||
"Tool failed";
const bullet = colors.toolError("✗");
const title = colors.toolName(toolDisplayName(event.toolName));
spinner.stop(`${bullet} ${title}: ${colors.toolError(errorText)}`);