@@ -17,9 +17,39 @@ function isVeniceGemini3ModelId(modelId: unknown): boolean {
1717 return typeof modelId === "string" && / ^ g e m i n i - 3 (?: [ . - ] | $ ) / . test ( modelId . trim ( ) . toLowerCase ( ) ) ;
1818}
1919
20- const GEMINI_THOUGHT_SIGNATURE_VALIDATOR_SKIP = "skip_thought_signature_validator" ;
20+ function stringifyHistoricalValue ( value : unknown ) : string {
21+ if ( typeof value === "string" ) {
22+ return value ;
23+ }
24+ if ( value === null || value === undefined ) {
25+ return "" ;
26+ }
27+ try {
28+ return JSON . stringify ( value ) ;
29+ } catch {
30+ return String ( value ) ;
31+ }
32+ }
33+
34+ function describeHistoricalToolCall ( toolCall : Record < string , unknown > ) : {
35+ id ?: string ;
36+ name : string ;
37+ text : string ;
38+ } {
39+ const fn =
40+ toolCall . function && typeof toolCall . function === "object"
41+ ? ( toolCall . function as Record < string , unknown > )
42+ : { } ;
43+ const name = typeof fn . name === "string" && fn . name . length > 0 ? fn . name : "unknown_tool" ;
44+ const args = stringifyHistoricalValue ( fn . arguments ) || "{}" ;
45+ return {
46+ ...( typeof toolCall . id === "string" ? { id : toolCall . id } : { } ) ,
47+ name,
48+ text : `[Historical tool call: ${ name } (${ args } )]` ,
49+ } ;
50+ }
2151
22- function applyVeniceGeminiThoughtSignatures (
52+ function applyVeniceGeminiToolHistoryCompatibility (
2353 payload : Record < string , unknown > ,
2454 context : Parameters < NonNullable < ProviderWrapStreamFnContext [ "streamFn" ] > > [ 1 ] ,
2555 model : Parameters < NonNullable < ProviderWrapStreamFnContext [ "streamFn" ] > > [ 0 ] ,
@@ -28,9 +58,7 @@ function applyVeniceGeminiThoughtSignatures(
2858 return ;
2959 }
3060 const signaturesByToolCallId = new Map < string , string > ( ) ;
31- const fallbackSignature = isVeniceGemini3ModelId ( model . id )
32- ? GEMINI_THOUGHT_SIGNATURE_VALIDATOR_SKIP
33- : undefined ;
61+ const requiresUnsignedCallFallback = isVeniceGemini3ModelId ( model . id ) ;
3462 for ( const message of context . messages ?? [ ] ) {
3563 if ( message . role !== "assistant" ) {
3664 continue ;
@@ -54,11 +82,12 @@ function applyVeniceGeminiThoughtSignatures(
5482 }
5583 }
5684 if (
57- ( signaturesByToolCallId . size === 0 && ! fallbackSignature ) ||
85+ ( signaturesByToolCallId . size === 0 && ! requiresUnsignedCallFallback ) ||
5886 ! Array . isArray ( payload . messages )
5987 ) {
6088 return ;
6189 }
90+ const downgradedToolCalls = new Map < string , string > ( ) ;
6291 for ( const message of payload . messages ) {
6392 if ( ! message || typeof message !== "object" ) {
6493 continue ;
@@ -67,6 +96,8 @@ function applyVeniceGeminiThoughtSignatures(
6796 if ( record . role !== "assistant" || ! Array . isArray ( record . tool_calls ) ) {
6897 continue ;
6998 }
99+ let shouldDowngradeBatch = false ;
100+ const describedCalls : Array < ReturnType < typeof describeHistoricalToolCall > > = [ ] ;
70101 for ( const toolCall of record . tool_calls ) {
71102 if ( ! toolCall || typeof toolCall !== "object" ) {
72103 continue ;
@@ -78,16 +109,44 @@ function applyVeniceGeminiThoughtSignatures(
78109 : undefined ;
79110 if ( signature ) {
80111 toolCallRecord . thought_signature = signature ;
81- } else if (
82- fallbackSignature &&
83- ( typeof toolCallRecord . thought_signature !== "string" ||
84- toolCallRecord . thought_signature . length === 0 )
85- ) {
86- // Gemini 3 validates every historical function call, including calls
87- // imported from another provider or recorded before signature capture.
88- toolCallRecord . thought_signature = fallbackSignature ;
112+ } else if ( requiresUnsignedCallFallback ) {
113+ shouldDowngradeBatch = true ;
114+ }
115+ describedCalls . push ( describeHistoricalToolCall ( toolCallRecord ) ) ;
116+ }
117+ if ( ! shouldDowngradeBatch ) {
118+ continue ;
119+ }
120+ for ( const call of describedCalls ) {
121+ if ( call . id ) {
122+ downgradedToolCalls . set ( call . id , call . name ) ;
89123 }
90124 }
125+ const existingContent = stringifyHistoricalValue ( record . content ) ;
126+ record . content = [ existingContent , ...describedCalls . map ( ( call ) => call . text ) ]
127+ . filter ( ( part ) => part . length > 0 )
128+ . join ( "\n" ) ;
129+ delete record . tool_calls ;
130+ }
131+ if ( downgradedToolCalls . size === 0 ) {
132+ return ;
133+ }
134+ for ( const message of payload . messages ) {
135+ if ( ! message || typeof message !== "object" ) {
136+ continue ;
137+ }
138+ const record = message as Record < string , unknown > ;
139+ const toolCallId = typeof record . tool_call_id === "string" ? record . tool_call_id : undefined ;
140+ const toolName = toolCallId ? downgradedToolCalls . get ( toolCallId ) : undefined ;
141+ if ( record . role !== "tool" || ! toolName ) {
142+ continue ;
143+ }
144+ const result = stringifyHistoricalValue ( record . content ) ;
145+ for ( const key of Object . keys ( record ) ) {
146+ delete record [ key ] ;
147+ }
148+ record . role = "user" ;
149+ record . content = `[Historical tool result for ${ toolName } :\n${ result } ]` ;
91150 }
92151}
93152
@@ -106,6 +165,6 @@ export function createVeniceStreamWrapper(
106165 replaceNullReasoningContent : true ,
107166 } ) ;
108167 }
109- applyVeniceGeminiThoughtSignatures ( payload , context , model ) ;
168+ applyVeniceGeminiToolHistoryCompatibility ( payload , context , model ) ;
110169 } ) ;
111170}
0 commit comments