@@ -12,6 +12,11 @@ const MODEL_FALLBACKS: Record<string, string> = {
1212 "gemini-2.5-flash-image" : "gemini-2.5-flash" ,
1313} ;
1414
15+ export interface ThinkingConfigDefaults {
16+ provider ?: unknown ;
17+ models ?: Record < string , unknown > ;
18+ }
19+
1520/**
1621 * Rewrites OpenAI-style requests into Gemini Code Assist request shape.
1722 */
@@ -20,6 +25,7 @@ export function prepareGeminiRequest(
2025 init : RequestInit | undefined ,
2126 accessToken : string ,
2227 projectId : string ,
28+ thinkingConfigDefaults ?: ThinkingConfigDefaults ,
2329) : {
2430 request : RequestInfo ;
2531 init : RequestInit ;
@@ -61,7 +67,13 @@ export function prepareGeminiRequest(
6167 let requestIdentifier : string = randomUUID ( ) ;
6268
6369 if ( typeof baseInit . body === "string" && baseInit . body ) {
64- const transformed = transformRequestBody ( baseInit . body , projectId , effectiveModel ) ;
70+ const transformed = transformRequestBody (
71+ baseInit . body ,
72+ projectId ,
73+ effectiveModel ,
74+ rawModel ,
75+ thinkingConfigDefaults ,
76+ ) ;
6577 if ( transformed . body ) {
6678 body = transformed . body ;
6779 requestIdentifier = transformed . userPromptId ;
@@ -97,6 +109,8 @@ function transformRequestBody(
97109 body : string ,
98110 projectId : string ,
99111 effectiveModel : string ,
112+ requestedModel : string ,
113+ thinkingConfigDefaults ?: ThinkingConfigDefaults ,
100114) : { body ?: string ; userPromptId : string } {
101115 const fallbackId = randomUUID ( ) ;
102116 try {
@@ -115,7 +129,11 @@ function transformRequestBody(
115129 const requestPayload = { ...parsedBody } ;
116130 transformOpenAIToolCalls ( requestPayload ) ;
117131 addThoughtSignaturesToFunctionCalls ( requestPayload ) ;
118- normalizeThinking ( requestPayload ) ;
132+ normalizeThinking (
133+ requestPayload ,
134+ resolveDefaultThinkingConfig ( thinkingConfigDefaults , requestedModel , effectiveModel ) ,
135+ thinkingConfigDefaults ?. provider ,
136+ ) ;
119137 normalizeSystemInstruction ( requestPayload ) ;
120138 normalizeCachedContent ( requestPayload ) ;
121139 stripThoughtPartsFromHistory ( requestPayload ) ;
@@ -139,9 +157,30 @@ function transformRequestBody(
139157 }
140158}
141159
142- function normalizeThinking ( requestPayload : Record < string , unknown > ) : void {
160+ function resolveDefaultThinkingConfig (
161+ thinkingConfigDefaults : ThinkingConfigDefaults | undefined ,
162+ requestedModel : string ,
163+ effectiveModel : string ,
164+ ) : unknown {
165+ if ( ! thinkingConfigDefaults ?. models ) {
166+ return undefined ;
167+ }
168+
169+ return thinkingConfigDefaults . models [ requestedModel ] ?? thinkingConfigDefaults . models [ effectiveModel ] ;
170+ }
171+
172+ function normalizeThinking (
173+ requestPayload : Record < string , unknown > ,
174+ modelThinkingConfig : unknown ,
175+ providerThinkingConfig : unknown ,
176+ ) : void {
143177 const rawGenerationConfig = requestPayload . generationConfig as Record < string , unknown > | undefined ;
144- const normalizedThinking = normalizeThinkingConfig ( rawGenerationConfig ?. thinkingConfig ) ;
178+ const hasRequestThinkingConfig =
179+ ! ! rawGenerationConfig && Object . prototype . hasOwnProperty . call ( rawGenerationConfig , "thinkingConfig" ) ;
180+ const sourceThinkingConfig = hasRequestThinkingConfig
181+ ? rawGenerationConfig ?. thinkingConfig
182+ : modelThinkingConfig ?? providerThinkingConfig ;
183+ const normalizedThinking = normalizeThinkingConfig ( sourceThinkingConfig ) ;
145184 if ( normalizedThinking ) {
146185 if ( rawGenerationConfig ) {
147186 rawGenerationConfig . thinkingConfig = normalizedThinking ;
@@ -152,7 +191,7 @@ function normalizeThinking(requestPayload: Record<string, unknown>): void {
152191 return ;
153192 }
154193
155- if ( rawGenerationConfig ?. thinkingConfig ) {
194+ if ( hasRequestThinkingConfig && rawGenerationConfig ) {
156195 delete rawGenerationConfig . thinkingConfig ;
157196 requestPayload . generationConfig = rawGenerationConfig ;
158197 }
0 commit comments