Skip to content

Commit 2cfa4b3

Browse files
PierrunoYTclaude
andcommitted
fix(task): nest an image that follows a tool_result into that result's content
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent e5de8f1 commit 2cfa4b3

3 files changed

Lines changed: 75 additions & 16 deletions

File tree

‎src/core/task/__tests__/Task.spec.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3024,11 +3024,17 @@ describe("Cline", () => {
30243024

30253025
const [, sentHistory] = requireDefined(createMessageSpy.mock.calls[0])
30263026
const lastUserContent = requireDefined(sentHistory.at(-1)).content as Array<{ type: string }>
3027-
// Both results lead, followed by the image and the environment details, none lost.
3027+
// Both results lead; the image sits inside the read_file result; the environment details follow.
30283028
expect(lastUserContent).toEqual([
3029-
{ type: "tool_result", tool_use_id: "read", content: "File: logo.jpg" },
3029+
{
3030+
type: "tool_result",
3031+
tool_use_id: "read",
3032+
content: [
3033+
{ type: "text", text: "File: logo.jpg" },
3034+
{ type: "image", source: { type: "base64", media_type: "image/jpeg", data: "abc" } },
3035+
],
3036+
},
30303037
{ type: "tool_result", tool_use_id: "cmd", content: "Exit code: 0" },
3031-
{ type: "image", source: { type: "base64", media_type: "image/jpeg", data: "abc" } },
30323038
{ type: "text", text: "<environment_details>" },
30333039
])
30343040
// The stored history keeps its original shape.

‎src/core/task/__tests__/orderToolResultsFirst.spec.ts‎

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ const image = (data: string): Anthropic.Messages.ImageBlockParam => ({
1616
source: { type: "base64", media_type: "image/jpeg", data },
1717
})
1818
const text = (value: string): Anthropic.Messages.TextBlockParam => ({ type: "text", text: value })
19+
const nested = (id: string, content: Anthropic.Messages.ToolResultBlockParam["content"]) => ({
20+
type: "tool_result" as const,
21+
tool_use_id: id,
22+
content,
23+
})
1924

2025
describe("orderToolResultsFirst", () => {
2126
it("repairs the persisted #1774 shape so every tool_result leads the message", () => {
@@ -29,10 +34,10 @@ describe("orderToolResultsFirst", () => {
2934

3035
const [repaired] = orderToolResultsFirst([stuck])
3136

37+
// The image moves into the read_file result, the shape the Anthropic API expects.
3238
expect(repaired.content).toEqual([
33-
toolResult("read"),
39+
nested("read", [text("result read"), image("jpeg")]),
3440
toolResult("cmd"),
35-
image("jpeg"),
3641
text("<environment_details>"),
3742
])
3843
expect(repaired.ts).toBe(16)
@@ -55,14 +60,39 @@ describe("orderToolResultsFirst", () => {
5560

5661
expect(repaired.content).toEqual([
5762
toolResult("a"),
58-
toolResult("b"),
59-
toolResult("c"),
60-
image("1"),
61-
image("2"),
63+
nested("b", [text("result b"), image("1")]),
64+
nested("c", [text("result c"), image("2")]),
6265
text("t"),
6366
])
6467
})
6568

69+
it("appends an image to a result whose content is already an array", () => {
70+
const withArray = nested("a", [text("first"), text("second")])
71+
72+
const [repaired] = orderToolResultsFirst([{ role: "user", content: [withArray, image("1"), toolResult("b")] }])
73+
74+
expect(repaired.content).toEqual([nested("a", [text("first"), text("second"), image("1")]), toolResult("b")])
75+
// The original result block is not mutated.
76+
expect(withArray.content).toEqual([text("first"), text("second")])
77+
})
78+
79+
it("keeps an image standalone when no result directly precedes it", () => {
80+
const [repaired] = orderToolResultsFirst([
81+
{
82+
role: "user",
83+
content: [image("lead"), toolResult("a"), text("note"), image("after-text"), toolResult("b")],
84+
},
85+
])
86+
87+
expect(repaired.content).toEqual([
88+
toolResult("a"),
89+
toolResult("b"),
90+
image("lead"),
91+
text("note"),
92+
image("after-text"),
93+
])
94+
})
95+
6696
it("repairs interleaving introduced by merging consecutive user messages", () => {
6797
const merged = mergeConsecutiveApiMessages([
6898
{ role: "user", content: [toolResult("a"), text("note")] },

‎src/core/task/orderToolResultsFirst.ts‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1+
import type { Anthropic } from "@anthropic-ai/sdk"
2+
13
import type { ApiMessage } from "../task-persistence"
24

5+
type Block = Anthropic.Messages.ContentBlockParam
6+
type ToolResultBlock = Anthropic.Messages.ToolResultBlockParam
7+
8+
function toContentArray(content: ToolResultBlock["content"]): Exclude<ToolResultBlock["content"], string | undefined> {
9+
if (typeof content === "string") return [{ type: "text", text: content }]
10+
return content ?? []
11+
}
12+
313
/**
414
* Moves `tool_result` blocks to the front of each user message, keeping the relative order of
5-
* the results and of the remaining blocks.
15+
* the results and of the remaining blocks. An image that directly follows a result (as when
16+
* `read_file` returns an image) is nested into that result's `content`, where Anthropic expects it.
617
*
718
* Anthropic requires the results for the previous turn's `tool_use` blocks to lead the next user
819
* message. History saved with image blocks between results (#1307, #1774), or produced by merging
@@ -22,12 +33,24 @@ export function orderToolResultsFirst(messages: ApiMessage[]): ApiMessage[] {
2233
if (!hasLaterResult) {
2334
return message
2435
}
25-
return {
26-
...message,
27-
content: [
28-
...message.content.filter((block) => block.type === "tool_result"),
29-
...message.content.filter((block) => block.type !== "tool_result"),
30-
],
36+
37+
const results: ToolResultBlock[] = []
38+
const rest: Block[] = []
39+
// Index of the result an immediately following image belongs to; reset by any other block.
40+
let owner = -1
41+
for (const block of message.content) {
42+
if (block.type === "tool_result") {
43+
results.push(block)
44+
owner = results.length - 1
45+
} else if (block.type === "image" && owner !== -1) {
46+
const current = results[owner]
47+
results[owner] = { ...current, content: [...toContentArray(current.content), block] }
48+
} else {
49+
owner = -1
50+
rest.push(block)
51+
}
3152
}
53+
54+
return { ...message, content: [...results, ...rest] }
3255
})
3356
}

0 commit comments

Comments
 (0)