mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 10:42:43 +00:00
refactor(agents): dedupe anthropic turn validation fixtures
This commit is contained in:
@@ -10,6 +10,28 @@ function asMessages(messages: unknown[]): AgentMessage[] {
|
|||||||
return messages as AgentMessage[];
|
return messages as AgentMessage[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function makeDualToolUseAssistantContent() {
|
||||||
|
return [
|
||||||
|
{ type: "toolUse", id: "tool-1", name: "test1", input: {} },
|
||||||
|
{ type: "toolUse", id: "tool-2", name: "test2", input: {} },
|
||||||
|
{ type: "text", text: "Done" },
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
function makeDualToolAnthropicTurns(nextUserContent: unknown[]) {
|
||||||
|
return asMessages([
|
||||||
|
{ role: "user", content: [{ type: "text", text: "Use tools" }] },
|
||||||
|
{
|
||||||
|
role: "assistant",
|
||||||
|
content: makeDualToolUseAssistantContent(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
role: "user",
|
||||||
|
content: nextUserContent,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
describe("validate turn edge cases", () => {
|
describe("validate turn edge cases", () => {
|
||||||
it("returns empty array unchanged", () => {
|
it("returns empty array unchanged", () => {
|
||||||
expect(validateGeminiTurns([])).toEqual([]);
|
expect(validateGeminiTurns([])).toEqual([]);
|
||||||
@@ -410,18 +432,7 @@ describe("validateAnthropicTurns strips dangling tool_use blocks", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("should handle multiple dangling tool_use blocks", () => {
|
it("should handle multiple dangling tool_use blocks", () => {
|
||||||
const msgs = asMessages([
|
const msgs = makeDualToolAnthropicTurns([{ type: "text", text: "OK" }]);
|
||||||
{ role: "user", content: [{ type: "text", text: "Use tools" }] },
|
|
||||||
{
|
|
||||||
role: "assistant",
|
|
||||||
content: [
|
|
||||||
{ type: "toolUse", id: "tool-1", name: "test1", input: {} },
|
|
||||||
{ type: "toolUse", id: "tool-2", name: "test2", input: {} },
|
|
||||||
{ type: "text", text: "Done" },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{ role: "user", content: [{ type: "text", text: "OK" }] },
|
|
||||||
]);
|
|
||||||
|
|
||||||
const result = validateAnthropicTurns(msgs);
|
const result = validateAnthropicTurns(msgs);
|
||||||
|
|
||||||
@@ -432,27 +443,13 @@ describe("validateAnthropicTurns strips dangling tool_use blocks", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("should handle mixed tool_use with some having matching tool_result", () => {
|
it("should handle mixed tool_use with some having matching tool_result", () => {
|
||||||
const msgs = asMessages([
|
const msgs = makeDualToolAnthropicTurns([
|
||||||
{ role: "user", content: [{ type: "text", text: "Use tools" }] },
|
|
||||||
{
|
{
|
||||||
role: "assistant",
|
type: "toolResult",
|
||||||
content: [
|
toolUseId: "tool-1",
|
||||||
{ type: "toolUse", id: "tool-1", name: "test1", input: {} },
|
content: [{ type: "text", text: "Result 1" }],
|
||||||
{ type: "toolUse", id: "tool-2", name: "test2", input: {} },
|
|
||||||
{ type: "text", text: "Done" },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
role: "user",
|
|
||||||
content: [
|
|
||||||
{
|
|
||||||
type: "toolResult",
|
|
||||||
toolUseId: "tool-1",
|
|
||||||
content: [{ type: "text", text: "Result 1" }],
|
|
||||||
},
|
|
||||||
{ type: "text", text: "Thanks" },
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
|
{ type: "text", text: "Thanks" },
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const result = validateAnthropicTurns(msgs);
|
const result = validateAnthropicTurns(msgs);
|
||||||
@@ -486,25 +483,11 @@ describe("validateAnthropicTurns strips dangling tool_use blocks", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("is replay-safe across repeated validation passes", () => {
|
it("is replay-safe across repeated validation passes", () => {
|
||||||
const msgs = asMessages([
|
const msgs = makeDualToolAnthropicTurns([
|
||||||
{ role: "user", content: [{ type: "text", text: "Use tools" }] },
|
|
||||||
{
|
{
|
||||||
role: "assistant",
|
type: "toolResult",
|
||||||
content: [
|
toolUseId: "tool-1",
|
||||||
{ type: "toolUse", id: "tool-1", name: "test1", input: {} },
|
content: [{ type: "text", text: "Result 1" }],
|
||||||
{ type: "toolUse", id: "tool-2", name: "test2", input: {} },
|
|
||||||
{ type: "text", text: "Done" },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
role: "user",
|
|
||||||
content: [
|
|
||||||
{
|
|
||||||
type: "toolResult",
|
|
||||||
toolUseId: "tool-1",
|
|
||||||
content: [{ type: "text", text: "Result 1" }],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user