Skip to content

Commit e29159e

Browse files
authored
fix(core): stop Claude Code treating an image from earlier turns as newly attached (#66755)
1 parent 2be552c commit e29159e

3 files changed

Lines changed: 38 additions & 8 deletions

File tree

‎packages/core/src/plugin/provider/claude-agent/language-model/convert-to-claude-message.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export * as ConvertToClaudeMessage from "./convert-to-claude-message.js"
22

33
import type { LanguageModelV3FilePart, LanguageModelV3Prompt } from "@ai-sdk/provider"
44
import type { SDKUserMessage } from "@anthropic-ai/claude-agent-sdk"
5+
import { Fingerprint } from "../sync/fingerprint.js"
56

67
export type ContentBlock = Exclude<SDKUserMessage["message"]["content"], string>[number]
78

@@ -11,14 +12,16 @@ export function content(prompt: LanguageModelV3Prompt): ContentBlock[] {
1112
const history = prompt.some((message) => message.role === "assistant" || message.role === "tool")
1213
for (const message of prompt) {
1314
if (message.role === "system") continue
14-
if (history) blocks.push({ type: "text", text: `<${message.role}>` })
15+
// OpenCode sends the files a tool returned as a user message after the tool result.
16+
const role = Fingerprint.isToolMedia(message) ? "tool" : message.role
17+
if (history) blocks.push({ type: "text", text: `<${role}>` })
1518
for (const part of message.content) {
1619
if (part.type === "text") {
1720
blocks.push({ type: "text", text: part.text })
1821
continue
1922
}
2023
if (part.type === "file") {
21-
blocks.push(file(part))
24+
blocks.push(history ? past(file(part)) : file(part))
2225
continue
2326
}
2427
if (part.type === "tool-call") {
@@ -31,7 +34,7 @@ export function content(prompt: LanguageModelV3Prompt): ContentBlock[] {
3134
blocks.push({ type: "text", text: `<tool-result>\n${result}\n</tool-result>` })
3235
}
3336
}
34-
if (history) blocks.push({ type: "text", text: `</${message.role}>` })
37+
if (history) blocks.push({ type: "text", text: `</${role}>` })
3538
}
3639
return blocks
3740
}
@@ -49,6 +52,14 @@ function file(part: LanguageModelV3FilePart): ContentBlock {
4952
throw new Error(`Claude Code does not support ${type} attachments`)
5053
}
5154

55+
// Claude Code treats an image block in a user message as one the user just pasted: it saves a copy and adds an
56+
// "[Image: source: ...]" line after the new prompt, so Claude takes an old image for a new attachment. An image inside a
57+
// document block reaches Claude as it is.
58+
function past(block: ContentBlock): ContentBlock {
59+
if (block.type !== "image") return block
60+
return { type: "document", source: { type: "content", content: [block] } }
61+
}
62+
5263
function base64(data: string | Uint8Array) {
5364
if (typeof data === "string") return data
5465
return Buffer.from(data).toString("base64")

‎packages/core/src/plugin/provider/claude-agent/sync/fingerprint.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function visible(message: Exclude<LanguageModelV3Message, { role: "system" }>) {
7171
}
7272

7373
// OpenCode lowers tool attachments into this separate user message.
74-
function isToolMedia(message: LanguageModelV3Message) {
74+
export function isToolMedia(message: LanguageModelV3Message) {
7575
return (
7676
message.role === "user" &&
7777
message.content.length > 1 &&

‎packages/core/test/plugin/provider/claude-agent/sync/sync.test.ts‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,32 @@ describe("history reconciliation", () => {
201201
{ type: "file", mediaType: "application/pdf", data: "AwQ=" },
202202
],
203203
},
204-
user("Continue"),
204+
{
205+
role: "user",
206+
content: [
207+
{ type: "text", text: "Continue" },
208+
{ type: "file", mediaType: "image/png", data: "BQY=" },
209+
],
210+
},
205211
])
206212
expect(JSON.stringify(blocks)).toContain("marker.txt")
207213
expect(JSON.stringify(blocks)).toContain("LEMON-IBIS")
208214
expect(JSON.stringify(blocks)).not.toContain("PRIVATE")
209-
expect(blocks.some((block) => block.type === "image")).toBe(true)
210-
expect(blocks.some((block) => block.type === "document")).toBe(true)
211-
expect(blocks.at(-1)).toEqual({ type: "text", text: "Continue" })
215+
expect(blocks.slice(-8)).toEqual([
216+
{ type: "text", text: "<tool>" },
217+
{ type: "text", text: "Attached media from tool result:" },
218+
{
219+
type: "document",
220+
source: {
221+
type: "content",
222+
content: [{ type: "image", source: { type: "base64", media_type: "image/png", data: "AQI=" } }],
223+
},
224+
},
225+
{ type: "document", source: { type: "base64", media_type: "application/pdf", data: "AwQ=" } },
226+
{ type: "text", text: "</tool>" },
227+
{ type: "text", text: "</conversation-checkpoint>" },
228+
{ type: "text", text: "Continue" },
229+
{ type: "image", source: { type: "base64", media_type: "image/png", data: "BQY=" } },
230+
])
212231
})
213232
})

0 commit comments

Comments
 (0)