fix: skip synthetic toolResult for aborted/errored assistant messages
This commit is contained in:
parent
34653e4baf
commit
747b84972e
@ -109,4 +109,49 @@ describe("sanitizeToolUseResultPairing", () => {
|
|||||||
expect(out.some((m) => m.role === "toolResult")).toBe(false);
|
expect(out.some((m) => m.role === "toolResult")).toBe(false);
|
||||||
expect(out.map((m) => m.role)).toEqual(["user", "assistant"]);
|
expect(out.map((m) => m.role)).toEqual(["user", "assistant"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("does not insert synthetic toolResult for aborted assistant message", () => {
|
||||||
|
const input = [
|
||||||
|
{
|
||||||
|
role: "assistant",
|
||||||
|
content: [{ type: "toolCall", id: "call_aborted", name: "edit", arguments: {} }],
|
||||||
|
stopReason: "aborted",
|
||||||
|
},
|
||||||
|
{ role: "user", content: "next message" },
|
||||||
|
] as AgentMessage[];
|
||||||
|
|
||||||
|
const out = sanitizeToolUseResultPairing(input);
|
||||||
|
expect(out.filter((m) => m.role === "toolResult")).toHaveLength(0);
|
||||||
|
expect(out.map((m) => m.role)).toEqual(["assistant", "user"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not insert synthetic toolResult for errored assistant message", () => {
|
||||||
|
const input = [
|
||||||
|
{
|
||||||
|
role: "assistant",
|
||||||
|
content: [{ type: "toolCall", id: "call_errored", name: "cron", arguments: {} }],
|
||||||
|
stopReason: "error",
|
||||||
|
},
|
||||||
|
{ role: "user", content: "next message" },
|
||||||
|
] as AgentMessage[];
|
||||||
|
|
||||||
|
const out = sanitizeToolUseResultPairing(input);
|
||||||
|
expect(out.filter((m) => m.role === "toolResult")).toHaveLength(0);
|
||||||
|
expect(out.map((m) => m.role)).toEqual(["assistant", "user"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("still inserts synthetic toolResult for normal assistant message with missing result", () => {
|
||||||
|
const input = [
|
||||||
|
{
|
||||||
|
role: "assistant",
|
||||||
|
content: [{ type: "toolCall", id: "call_normal", name: "read", arguments: {} }],
|
||||||
|
stopReason: "toolUse",
|
||||||
|
},
|
||||||
|
{ role: "user", content: "next message" },
|
||||||
|
] as AgentMessage[];
|
||||||
|
|
||||||
|
const out = sanitizeToolUseResultPairing(input);
|
||||||
|
expect(out.filter((m) => m.role === "toolResult")).toHaveLength(1);
|
||||||
|
expect((out[1] as { toolCallId?: string }).toolCallId).toBe("call_normal");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -8,6 +8,9 @@ type ToolCallLike = {
|
|||||||
function extractToolCallsFromAssistant(
|
function extractToolCallsFromAssistant(
|
||||||
msg: Extract<AgentMessage, { role: "assistant" }>,
|
msg: Extract<AgentMessage, { role: "assistant" }>,
|
||||||
): ToolCallLike[] {
|
): ToolCallLike[] {
|
||||||
|
const stopReason = (msg as { stopReason?: unknown }).stopReason;
|
||||||
|
if (stopReason === "error" || stopReason === "aborted") return [];
|
||||||
|
|
||||||
const content = msg.content;
|
const content = msg.content;
|
||||||
if (!Array.isArray(content)) return [];
|
if (!Array.isArray(content)) return [];
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user