@@ -2,6 +2,7 @@ export * as ConvertToClaudeMessage from "./convert-to-claude-message.js"
22
33import type { LanguageModelV3FilePart , LanguageModelV3Prompt } from "@ai-sdk/provider"
44import type { SDKUserMessage } from "@anthropic-ai/claude-agent-sdk"
5+ import { Fingerprint } from "../sync/fingerprint.js"
56
67export 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+
5263function base64 ( data : string | Uint8Array ) {
5364 if ( typeof data === "string" ) return data
5465 return Buffer . from ( data ) . toString ( "base64" )
0 commit comments