@@ -44,6 +44,24 @@ type UsageInfo = Pick<
4444 "total_tokens" | "completion_tokens" | "prompt_tokens"
4545> ;
4646
47+ interface GeminiToolCall
48+ extends OpenAI . Chat . Completions . ChatCompletionMessageFunctionToolCall {
49+ extra_content ?: {
50+ google ?: {
51+ thought_signature ?: string ;
52+ } ;
53+ } ;
54+ }
55+
56+ interface GeminiToolDelta
57+ extends OpenAI . Chat . Completions . ChatCompletionChunk . Choice . Delta {
58+ extra_content ?: {
59+ google ?: {
60+ thought_signature ?: string ;
61+ } ;
62+ } ;
63+ }
64+
4765export class GeminiApi implements BaseLlmApi {
4866 apiBase : string = "https://generativelanguage.googleapis.com/v1beta/" ;
4967
@@ -143,41 +161,43 @@ export class GeminiApi implements BaseLlmApi {
143161
144162 return {
145163 role : "model" as const ,
146- parts : msg . tool_calls . map ( ( toolCall , index ) => {
147- if ( toolCall . type === "function" && "function" in toolCall ) {
148- let thoughtSignature : string | undefined ;
149- if ( index === 0 ) {
150- const rawSignature = ( toolCall as any ) ?. extra_content ?. google
151- ?. thought_signature ;
152-
153- if (
154- typeof rawSignature === "string" &&
155- rawSignature . length > 0
156- ) {
157- thoughtSignature = rawSignature ;
158- } else {
159- // Fallback per https://ai.google.dev/gemini-api/docs/thought-signatures
160- // for histories that were not generated by Gemini or are missing signatures.
161- thoughtSignature = "skip_thought_signature_validator" ;
164+ parts : ( msg . tool_calls as GeminiToolCall [ ] ) . map (
165+ ( toolCall , index ) => {
166+ if ( toolCall . type === "function" && "function" in toolCall ) {
167+ let thoughtSignature : string | undefined ;
168+ if ( index === 0 ) {
169+ const rawSignature =
170+ toolCall ?. extra_content ?. google ?. thought_signature ;
171+
172+ if (
173+ typeof rawSignature === "string" &&
174+ rawSignature . length > 0
175+ ) {
176+ thoughtSignature = rawSignature ;
177+ } else {
178+ // Fallback per https://ai.google.dev/gemini-api/docs/thought-signatures
179+ // for histories that were not generated by Gemini or are missing signatures.
180+ thoughtSignature = "skip_thought_signature_validator" ;
181+ }
162182 }
163- }
164183
165- return {
166- functionCall : {
167- id : includeToolCallIds ? toolCall . id : undefined ,
168- name : toolCall . function . name ,
169- args : safeParseArgs (
170- toolCall . function . arguments ,
171- `Call: ${ toolCall . function . name } ${ toolCall . id } ` ,
172- ) ,
173- } ,
174- ...( thoughtSignature && { thoughtSignature } ) ,
175- } ;
176- }
177- throw new Error (
178- `Unsupported tool call type in Gemini: ${ toolCall . type } ` ,
179- ) ;
180- } ) ,
184+ return {
185+ functionCall : {
186+ id : includeToolCallIds ? toolCall . id : undefined ,
187+ name : toolCall . function . name ,
188+ args : safeParseArgs (
189+ toolCall . function . arguments ,
190+ `Call: ${ toolCall . function . name } ${ toolCall . id } ` ,
191+ ) ,
192+ } ,
193+ ...( thoughtSignature && { thoughtSignature } ) ,
194+ } ;
195+ }
196+ throw new Error (
197+ `Unsupported tool call type in Gemini: ${ toolCall . type } ` ,
198+ ) ;
199+ } ,
200+ ) ,
181201 } ;
182202 }
183203
@@ -344,7 +364,7 @@ export class GeminiApi implements BaseLlmApi {
344364 if ( contentParts ) {
345365 for ( const part of contentParts ) {
346366 if ( "text" in part ) {
347- const thoughtSignature = ( part as any ) ?. thoughtSignature ;
367+ const thoughtSignature = part ?. thoughtSignature ;
348368 if ( thoughtSignature ) {
349369 yield chatChunkFromDelta ( {
350370 model,
@@ -355,7 +375,7 @@ export class GeminiApi implements BaseLlmApi {
355375 thought_signature : thoughtSignature ,
356376 } ,
357377 } ,
358- } as any ,
378+ } as GeminiToolDelta ,
359379 } ) ;
360380 }
361381
@@ -364,7 +384,7 @@ export class GeminiApi implements BaseLlmApi {
364384 model,
365385 } ) ;
366386 } else if ( "functionCall" in part ) {
367- const thoughtSignature = ( part as any ) ?. thoughtSignature ;
387+ const thoughtSignature = part ?. thoughtSignature ;
368388 yield chatChunkFromDelta ( {
369389 model,
370390 delta : {
0 commit comments