|
| 1 | +// npx vitest run core/task/__tests__/orderToolResultsFirst.spec.ts |
| 2 | + |
| 3 | +import type { Anthropic } from "@anthropic-ai/sdk" |
| 4 | + |
| 5 | +import type { ApiMessage } from "../../task-persistence" |
| 6 | +import { mergeConsecutiveApiMessages } from "../mergeConsecutiveApiMessages" |
| 7 | +import { orderToolResultsFirst } from "../orderToolResultsFirst" |
| 8 | + |
| 9 | +const toolResult = (id: string, content = `result ${id}`): Anthropic.Messages.ToolResultBlockParam => ({ |
| 10 | + type: "tool_result", |
| 11 | + tool_use_id: id, |
| 12 | + content, |
| 13 | +}) |
| 14 | +const image = (data: string): Anthropic.Messages.ImageBlockParam => ({ |
| 15 | + type: "image", |
| 16 | + source: { type: "base64", media_type: "image/jpeg", data }, |
| 17 | +}) |
| 18 | +const text = (value: string): Anthropic.Messages.TextBlockParam => ({ type: "text", text: value }) |
| 19 | + |
| 20 | +describe("orderToolResultsFirst", () => { |
| 21 | + it("repairs the persisted #1774 shape so every tool_result leads the message", () => { |
| 22 | + // [tool_result, image, tool_result, text] as saved when read_file on an image ran |
| 23 | + // alongside another tool call in the same turn. |
| 24 | + const stuck: ApiMessage = { |
| 25 | + role: "user", |
| 26 | + ts: 16, |
| 27 | + content: [toolResult("read"), image("jpeg"), toolResult("cmd"), text("<environment_details>")], |
| 28 | + } |
| 29 | + |
| 30 | + const [repaired] = orderToolResultsFirst([stuck]) |
| 31 | + |
| 32 | + expect(repaired.content).toEqual([ |
| 33 | + toolResult("read"), |
| 34 | + toolResult("cmd"), |
| 35 | + image("jpeg"), |
| 36 | + text("<environment_details>"), |
| 37 | + ]) |
| 38 | + expect(repaired.ts).toBe(16) |
| 39 | + // Stored history is untouched. |
| 40 | + expect(stuck.content).toEqual([ |
| 41 | + toolResult("read"), |
| 42 | + image("jpeg"), |
| 43 | + toolResult("cmd"), |
| 44 | + text("<environment_details>"), |
| 45 | + ]) |
| 46 | + }) |
| 47 | + |
| 48 | + it("keeps the order of results and of other blocks with several interleaved images", () => { |
| 49 | + const [repaired] = orderToolResultsFirst([ |
| 50 | + { |
| 51 | + role: "user", |
| 52 | + content: [toolResult("a"), toolResult("b"), image("1"), toolResult("c"), image("2"), text("t")], |
| 53 | + }, |
| 54 | + ]) |
| 55 | + |
| 56 | + expect(repaired.content).toEqual([ |
| 57 | + toolResult("a"), |
| 58 | + toolResult("b"), |
| 59 | + toolResult("c"), |
| 60 | + image("1"), |
| 61 | + image("2"), |
| 62 | + text("t"), |
| 63 | + ]) |
| 64 | + }) |
| 65 | + |
| 66 | + it("repairs interleaving introduced by merging consecutive user messages", () => { |
| 67 | + const merged = mergeConsecutiveApiMessages([ |
| 68 | + { role: "user", content: [toolResult("a"), text("note")] }, |
| 69 | + { role: "user", content: [toolResult("b")] }, |
| 70 | + ]) |
| 71 | + |
| 72 | + expect(orderToolResultsFirst(merged)[0].content).toEqual([toolResult("a"), toolResult("b"), text("note")]) |
| 73 | + }) |
| 74 | + |
| 75 | + it("returns already-ordered, string, and assistant messages unchanged", () => { |
| 76 | + const ordered: ApiMessage = { role: "user", content: [toolResult("a"), image("1"), text("t")] } |
| 77 | + const plain: ApiMessage = { role: "user", content: "hello" } |
| 78 | + const noResults: ApiMessage = { role: "user", content: [text("a"), image("1")] } |
| 79 | + const assistant: ApiMessage = { role: "assistant", content: [text("a")] } |
| 80 | + |
| 81 | + const out = orderToolResultsFirst([ordered, plain, noResults, assistant]) |
| 82 | + |
| 83 | + out.forEach((message, index) => expect(message).toBe([ordered, plain, noResults, assistant][index])) |
| 84 | + }) |
| 85 | +}) |
0 commit comments