@@ -7,6 +7,12 @@ type Block = Anthropic.Messages.ContentBlockParam
77
88const use = ( id : string ) : Block => ( { type : "tool_use" , id, name : "read_file" , input : { } } )
99const result = ( id : string , content = `result ${ id } ` ) : Block => ( { type : "tool_result" , tool_use_id : id , content } )
10+ const interrupted = ( id : string ) : Block => ( {
11+ type : "tool_result" ,
12+ tool_use_id : id ,
13+ content : "Tool execution was interrupted before completion." ,
14+ is_error : true ,
15+ } )
1016const text = ( value : string ) : Block => ( { type : "text" , text : value } )
1117const image : Block = { type : "image" , source : { type : "base64" , media_type : "image/png" , data : "AAAA" } }
1218const assistant = ( ...content : Block [ ] ) : ApiMessage => ( { role : "assistant" , content } )
@@ -32,11 +38,13 @@ describe("normalizeToolResultPairing", () => {
3238 it ( "adds a synthetic error result for an interrupted tool_use" , ( ) => {
3339 const input = [ assistant ( use ( "A" ) , use ( "B" ) ) , user ( result ( "A" ) , text ( "next" ) ) ]
3440
35- expect ( normalizeToolResultPairing ( input ) [ 1 ] . content ) . toEqual ( [
36- result ( "A" ) ,
37- { type : "tool_result" , tool_use_id : "B" , content : "Tool execution was interrupted before completion." } ,
38- text ( "next" ) ,
39- ] )
41+ expect ( normalizeToolResultPairing ( input ) [ 1 ] . content ) . toEqual ( [ result ( "A" ) , interrupted ( "B" ) , text ( "next" ) ] )
42+ } )
43+
44+ it ( "preserves string content in the user turn after a tool_use" , ( ) => {
45+ const out = normalizeToolResultPairing ( [ assistant ( use ( "A" ) ) , { role : "user" , content : "hi" } ] )
46+
47+ expect ( out [ 1 ] . content ) . toEqual ( [ interrupted ( "A" ) , text ( "hi" ) ] )
4048 } )
4149
4250 it ( "inserts a user turn when an assistant turn is followed by another assistant turn" , ( ) => {
0 commit comments