mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-11 05:24:32 +00:00
fix(agents): normalize malformed tool results in adapter (#27007)
This commit is contained in:
@@ -25,6 +25,15 @@ async function executeThrowingTool(name: string, callId: string) {
|
||||
return await def.execute(callId, {}, undefined, undefined, extensionContext);
|
||||
}
|
||||
|
||||
async function executeTool(tool: AgentTool, callId: string) {
|
||||
const defs = toToolDefinitions([tool]);
|
||||
const def = defs[0];
|
||||
if (!def) {
|
||||
throw new Error("missing tool definition");
|
||||
}
|
||||
return await def.execute(callId, {}, undefined, undefined, extensionContext);
|
||||
}
|
||||
|
||||
describe("pi tool definition adapter", () => {
|
||||
it("wraps tool errors into a tool result", async () => {
|
||||
const result = await executeThrowingTool("boom", "call1");
|
||||
@@ -46,4 +55,46 @@ describe("pi tool definition adapter", () => {
|
||||
error: "nope",
|
||||
});
|
||||
});
|
||||
|
||||
it("coerces details-only tool results to include content", async () => {
|
||||
const tool = {
|
||||
name: "memory_query",
|
||||
label: "Memory Query",
|
||||
description: "returns details only",
|
||||
parameters: Type.Object({}),
|
||||
execute: (async () => ({
|
||||
details: {
|
||||
hits: [{ id: "a1", score: 0.9 }],
|
||||
},
|
||||
})) as unknown as AgentTool["execute"],
|
||||
} satisfies AgentTool;
|
||||
|
||||
const result = await executeTool(tool, "call3");
|
||||
expect(result.details).toEqual({
|
||||
hits: [{ id: "a1", score: 0.9 }],
|
||||
});
|
||||
expect(result.content[0]).toMatchObject({ type: "text" });
|
||||
expect((result.content[0] as { text?: string }).text).toContain('"hits"');
|
||||
});
|
||||
|
||||
it("coerces non-standard object results to include content", async () => {
|
||||
const tool = {
|
||||
name: "memory_query_raw",
|
||||
label: "Memory Query Raw",
|
||||
description: "returns plain object",
|
||||
parameters: Type.Object({}),
|
||||
execute: (async () => ({
|
||||
count: 2,
|
||||
ids: ["m1", "m2"],
|
||||
})) as unknown as AgentTool["execute"],
|
||||
} satisfies AgentTool;
|
||||
|
||||
const result = await executeTool(tool, "call4");
|
||||
expect(result.details).toEqual({
|
||||
count: 2,
|
||||
ids: ["m1", "m2"],
|
||||
});
|
||||
expect(result.content[0]).toMatchObject({ type: "text" });
|
||||
expect((result.content[0] as { text?: string }).text).toContain('"count"');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user