@@ -24,7 +24,7 @@ function convertAnthropicContentToGemini(content: string | Anthropic.ContentBloc
2424 } ,
2525 }
2626 case "tool_result" : {
27- if ( ! block . content ) {
27+ if ( ! block . content || ! Array . isArray ( block . content ) ) {
2828 return [ ]
2929 }
3030
@@ -37,36 +37,37 @@ function convertAnthropicContentToGemini(content: string | Anthropic.ContentBloc
3737 }
3838 }
3939
40- if ( Array . isArray ( block . content ) ) {
41- const textParts : string [ ] = [ ]
42- const imageParts : Part [ ] = [ ]
40+ if ( ! Array . isArray ( block . content ) ) {
41+ return [ ]
42+ }
4343
44- block . content . forEach ( ( item ) => {
45- if ( item . type === "text" ) {
46- textParts . push ( item . text )
47- } else if ( item . type === "image" ) {
48- if ( item . source . type === "base64" ) {
49- imageParts . push ( {
50- inlineData : { data : item . source . data , mimeType : item . source . media_type } ,
51- } )
52- }
53- }
54- } )
44+ const textParts : string [ ] = [ ]
45+ const imageParts : Part [ ] = [ ]
5546
56- // Create content text with a note about images if present
57- const contentText =
58- textParts . join ( "\n\n" ) + ( imageParts . length > 0 ? "\n\n(See next part for image)" : "" )
47+ block . content . forEach ( ( item ) => {
48+ if ( item . type === "text" ) {
49+ textParts . push ( item . text )
50+ } else if ( item . type === "image" ) {
51+ if ( item . source . type === "base64" ) {
52+ imageParts . push ( {
53+ inlineData : { data : item . source . data , mimeType : item . source . media_type } ,
54+ } )
55+ }
56+ }
57+ } )
5958
60- // Return function response followed by any images
61- return [
62- { functionResponse : { name : toolName , response : { name : toolName , content : contentText } } } ,
63- ...imageParts ,
64- ]
65- }
59+ // Create content text with a note about images if present
60+ const contentText =
61+ textParts . join ( "\n\n" ) + ( imageParts . length > 0 ? "\n\n(See next part for image)" : "" )
6662
67- return [ ]
63+ // Return function response followed by any images
64+ return [
65+ { functionResponse : { name : toolName , response : { name : toolName , content : contentText } } } ,
66+ ...imageParts ,
67+ ]
6868 }
6969 default :
70+ // Currently unsupported: "thinking" | "redacted_thinking" | "document"
7071 throw new Error ( `Unsupported content block type: ${ block . type } ` )
7172 }
7273 } )
0 commit comments