@@ -79,7 +79,8 @@ export function convertAnthropicContentToGemini(
7979 }
8080
8181 // Extract tool name from tool_use_id (e.g., "calculator-123" -> "calculator")
82- const toolName = block . tool_use_id . split ( "-" ) [ 0 ]
82+ const lastHyphenIndex = block . tool_use_id . lastIndexOf ( "-" )
83+ const toolName = lastHyphenIndex >= 0 ? block . tool_use_id . slice ( 0 , lastHyphenIndex ) : block . tool_use_id
8384
8485 if ( typeof block . content === "string" ) {
8586 return {
@@ -124,59 +125,16 @@ export function convertAnthropicMessageToGemini(
124125 message : Anthropic . Messages . MessageParam ,
125126 options ?: { includeThoughtSignatures ?: boolean } ,
126127) : Content [ ] {
127- const content : ExtendedContentBlockParam [ ] = Array . isArray ( message . content )
128- ? ( message . content as ExtendedContentBlockParam [ ] )
129- : [ { type : "text" , text : message . content ?? "" } ]
130-
131- const toolUseParts = content . filter ( ( block ) => block . type === "tool_use" ) as Anthropic . ToolUseBlock [ ]
132- const toolResultParts = content . filter ( ( block ) => block . type === "tool_result" ) as Anthropic . ToolResultBlockParam [ ]
133- const otherParts = content . filter ( ( block ) => block . type !== "tool_use" && block . type !== "tool_result" )
134- const thoughtSignatureBlocks = content . filter ( ( block ) => isThoughtSignatureContentBlock ( block ) )
135- const role = message . role === "assistant" ? "model" : "user"
136- const assistantThoughtOptions = {
137- ...options ,
138- includeThoughtSignatures : message . role === "assistant" ? options ?. includeThoughtSignatures ?? true : false ,
139- }
140-
141- // Gemini expects a flat list of Content objects. We group regular message parts,
142- // tool uses, and tool results into separate Content entries while preserving their
143- // relative order within each category.
144- const contents : Content [ ] = [ ]
145-
146- if ( otherParts . length > 0 ) {
147- contents . push ( {
148- role,
149- parts : convertAnthropicContentToGemini ( otherParts , assistantThoughtOptions ) ,
150- } )
151- }
152-
153- if ( toolUseParts . length > 0 ) {
154- const toolUseWithSignatures =
155- thoughtSignatureBlocks . length > 0 ? [ ...thoughtSignatureBlocks , ...toolUseParts ] : toolUseParts
156- contents . push ( {
157- role : "model" ,
158- parts : convertAnthropicContentToGemini ( toolUseWithSignatures , {
159- ...options ,
160- includeThoughtSignatures : options ?. includeThoughtSignatures ?? true ,
161- } ) ,
162- } )
163- }
164-
165- if ( toolResultParts . length > 0 ) {
166- contents . push ( {
167- role : "user" ,
168- parts : convertAnthropicContentToGemini ( toolResultParts , { ...options , includeThoughtSignatures : false } ) ,
169- } )
170- }
128+ const parts = convertAnthropicContentToGemini ( message . content , options )
171129
172- if ( contents . length === 0 ) {
173- return [
174- {
175- role,
176- parts : convertAnthropicContentToGemini ( message . content ?? "" , assistantThoughtOptions ) ,
177- } ,
178- ]
130+ if ( parts . length === 0 ) {
131+ return [ ]
179132 }
180133
181- return contents
134+ return [
135+ {
136+ role : message . role === "assistant" ? "model" : "user" ,
137+ parts,
138+ } ,
139+ ]
182140}
0 commit comments