fix(data): propagate errors so is_error is set correctly in run-log

Previously the data tool caught all errors and returned them as normal
tool results with error info in the JSON content. This meant pi-agent-core
never saw an exception and always set isError=false in the run-log, even
for rate limit errors (errCode 9001) and other API failures.

Now errors propagate to pi-agent-core which sets isError=true and formats
the error message for the LLM automatically.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Jiayuan Zhang 2026-02-16 03:39:11 +08:00
parent 486b090577
commit 357bf326e0

View file

@ -83,36 +83,20 @@ export function createDataTool(): AgentTool<typeof DataToolSchema, DataToolResul
const { domain, action, params } = args as DataToolArgs;
if (domain !== "finance") {
const errorPayload = {
error: true,
message: `Unknown domain: "${domain}". Currently supported: "finance".`,
};
return {
content: [{ type: "text", text: JSON.stringify(errorPayload, null, 2) }],
details: { domain, action, data: null } as unknown as DataToolResult,
};
throw new Error(`Unknown domain: "${domain}". Currently supported: "finance".`);
}
try {
const result = await executeFinanceAction(action, params ?? {}, signal);
const payload: DataToolResult = {
domain,
action,
data: result.data,
sourceUrl: result.sourceUrl,
};
return {
content: [{ type: "text", text: JSON.stringify(payload, null, 2) }],
details: payload,
};
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
const errorPayload = { error: true, domain, action, message };
return {
content: [{ type: "text", text: JSON.stringify(errorPayload, null, 2) }],
details: { domain, action, data: null } as unknown as DataToolResult,
};
}
const result = await executeFinanceAction(action, params ?? {}, signal);
const payload: DataToolResult = {
domain,
action,
data: result.data,
sourceUrl: result.sourceUrl,
};
return {
content: [{ type: "text", text: JSON.stringify(payload, null, 2) }],
details: payload,
};
},
};
}