@@ -3027,80 +3027,86 @@ describe("Cline", () => {
30273027 expect ( Object . keys ( cleanConversationHistory [ 0 ] ! ) ) . toEqual ( [ "role" , "content" ] )
30283028 } )
30293029
3030- it ( "sends tool_result blocks first when stored history has images between them (#1774)" , async ( ) => {
3031- const cline = new Task ( {
3032- provider : mockProvider ,
3033- apiConfiguration : mockApiConfig ,
3034- task : "test task" ,
3035- startTask : false ,
3036- } )
3037- vi . spyOn ( getTaskTestAccess ( cline ) , "getSystemPrompt" ) . mockResolvedValue ( "mock system prompt" )
3038- // Image-capable model so the image block is kept in the outgoing request.
3039- vi . spyOn ( cline . api , "getModel" ) . mockReturnValue ( {
3040- id : "claude-3-sonnet" ,
3041- info : {
3042- supportsImages : true ,
3043- supportsPromptCache : false ,
3044- contextWindow : 200000 ,
3045- maxTokens : 4096 ,
3046- } as ModelInfo ,
3047- } )
3048- const mockStream = {
3049- async * [ Symbol . asyncIterator ] ( ) {
3050- yield { type : "text" , text : "response" }
3051- } ,
3052- } as AsyncGenerator < ApiStreamChunk >
3053- const createMessageSpy = vi . spyOn ( cline . api , "createMessage" ) . mockReturnValue ( mockStream )
3030+ it . each ( [
3031+ {
3032+ supportsImages : true ,
3033+ imageBlock : { type : "image" , source : { type : "base64" , media_type : "image/jpeg" , data : "abc" } } ,
3034+ } ,
3035+ {
3036+ // A text-only model must not receive the image, including after the repair.
3037+ supportsImages : false ,
3038+ imageBlock : { type : "text" , text : "[Referenced image in conversation]" } ,
3039+ } ,
3040+ ] ) (
3041+ "sends tool_result blocks first when stored history has images between them (#1774, supportsImages: $supportsImages)" ,
3042+ async ( { supportsImages, imageBlock } ) => {
3043+ const cline = new Task ( {
3044+ provider : mockProvider ,
3045+ apiConfiguration : mockApiConfig ,
3046+ task : "test task" ,
3047+ startTask : false ,
3048+ } )
3049+ vi . spyOn ( getTaskTestAccess ( cline ) , "getSystemPrompt" ) . mockResolvedValue ( "mock system prompt" )
3050+ vi . spyOn ( cline . api , "getModel" ) . mockReturnValue ( {
3051+ id : "claude-3-sonnet" ,
3052+ info : {
3053+ supportsImages,
3054+ supportsPromptCache : false ,
3055+ contextWindow : 200000 ,
3056+ maxTokens : 4096 ,
3057+ } as ModelInfo ,
3058+ } )
3059+ const mockStream = {
3060+ async * [ Symbol . asyncIterator ] ( ) {
3061+ yield { type : "text" , text : "response" }
3062+ } ,
3063+ } as AsyncGenerator < ApiStreamChunk >
3064+ const createMessageSpy = vi . spyOn ( cline . api , "createMessage" ) . mockReturnValue ( mockStream )
30543065
3055- // History as persisted by a turn that read an image alongside another tool call.
3056- cline . apiConversationHistory = [
3057- { role : "user" , content : [ { type : "text" , text : "read the logo and list files" } ] , ts : 1 } ,
3058- {
3059- role : "assistant" ,
3060- content : [
3061- { type : "tool_use" , id : "read" , name : "read_file" , input : { path : "logo.jpg" } } ,
3062- { type : "tool_use" , id : "cmd" , name : "execute_command" , input : { command : "ls" } } ,
3063- ] ,
3064- ts : 2 ,
3065- } ,
3066- {
3067- role : "user" ,
3068- content : [
3069- { type : "tool_result" , tool_use_id : "read" , content : "File: logo.jpg" } ,
3070- { type : "image" , source : { type : "base64" , media_type : "image/jpeg" , data : "abc" } } ,
3071- { type : "tool_result" , tool_use_id : "cmd" , content : "Exit code: 0" } ,
3072- { type : "text" , text : "<environment_details>" } ,
3073- ] ,
3074- ts : 3 ,
3075- } ,
3076- ]
3066+ // History as persisted by a turn that read an image alongside another tool call.
3067+ cline . apiConversationHistory = [
3068+ { role : "user" , content : [ { type : "text" , text : "read the logo and list files" } ] , ts : 1 } ,
3069+ {
3070+ role : "assistant" ,
3071+ content : [
3072+ { type : "tool_use" , id : "read" , name : "read_file" , input : { path : "logo.jpg" } } ,
3073+ { type : "tool_use" , id : "cmd" , name : "execute_command" , input : { command : "ls" } } ,
3074+ ] ,
3075+ ts : 2 ,
3076+ } ,
3077+ {
3078+ role : "user" ,
3079+ content : [
3080+ { type : "tool_result" , tool_use_id : "read" , content : "File: logo.jpg" } ,
3081+ { type : "image" , source : { type : "base64" , media_type : "image/jpeg" , data : "abc" } } ,
3082+ { type : "tool_result" , tool_use_id : "cmd" , content : "Exit code: 0" } ,
3083+ { type : "text" , text : "<environment_details>" } ,
3084+ ] ,
3085+ ts : 3 ,
3086+ } ,
3087+ ]
30773088
3078- const iterator = cline . attemptApiRequest ( 0 )
3079- await iterator . next ( )
3089+ const iterator = cline . attemptApiRequest ( 0 )
3090+ await iterator . next ( )
30803091
3081- const [ , sentHistory ] = requireDefined ( createMessageSpy . mock . calls [ 0 ] )
3082- const lastUserContent = requireDefined ( sentHistory . at ( - 1 ) ) . content as Array < { type : string } >
3083- // Both results lead; the image sits inside the read_file result; the environment details follow.
3084- expect ( lastUserContent ) . toEqual ( [
3085- {
3086- type : "tool_result" ,
3087- tool_use_id : "read" ,
3088- content : [
3089- { type : "text" , text : "File: logo.jpg" } ,
3090- { type : "image" , source : { type : "base64" , media_type : "image/jpeg" , data : "abc" } } ,
3091- ] ,
3092- } ,
3093- { type : "tool_result" , tool_use_id : "cmd" , content : "Exit code: 0" } ,
3094- { type : "text" , text : "<environment_details>" } ,
3095- ] )
3096- // The stored history keeps its original shape.
3097- expect ( requireDefined ( cline . apiConversationHistory . at ( - 1 ) ) . content ) . toMatchObject ( [
3098- { type : "tool_result" } ,
3099- { type : "image" } ,
3100- { type : "tool_result" } ,
3101- { type : "text" } ,
3102- ] )
3103- } )
3092+ const [ , sentHistory ] = requireDefined ( createMessageSpy . mock . calls [ 0 ] )
3093+ const lastUserContent = requireDefined ( sentHistory . at ( - 1 ) ) . content as Array < { type : string } >
3094+ // Both results lead; the image (or its text stand-in) and the environment details follow.
3095+ expect ( lastUserContent ) . toEqual ( [
3096+ { type : "tool_result" , tool_use_id : "read" , content : "File: logo.jpg" } ,
3097+ { type : "tool_result" , tool_use_id : "cmd" , content : "Exit code: 0" } ,
3098+ imageBlock ,
3099+ { type : "text" , text : "<environment_details>" } ,
3100+ ] )
3101+ // The stored history keeps its original shape.
3102+ expect ( requireDefined ( cline . apiConversationHistory . at ( - 1 ) ) . content ) . toMatchObject ( [
3103+ { type : "tool_result" } ,
3104+ { type : "image" } ,
3105+ { type : "tool_result" } ,
3106+ { type : "text" } ,
3107+ ] )
3108+ } ,
3109+ )
31043110
31053111 it ( "should shape image blocks for API compatibility before request construction" , async ( ) => {
31063112 const conversationHistory = [
0 commit comments