@@ -25,6 +25,16 @@ import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata } from ".
2525import { getApiRequestTimeout } from "./utils/timeout-config"
2626import { handleOpenAIError } from "./utils/openai-error-handler"
2727
28+ // Custom interface for OpenAI params to support DeepSeek's thinking mode and other non-standard features
29+ type OpenAiChatCompletionParams = (
30+ | OpenAI . Chat . ChatCompletionCreateParamsStreaming
31+ | OpenAI . Chat . Completions . ChatCompletionCreateParamsNonStreaming
32+ ) & {
33+ extra_body ?: {
34+ thinking ?: { type : "enabled" | "disabled" }
35+ }
36+ }
37+
2838// TODO: Rename this to OpenAICompatibleHandler. Also, I think the
2939// `OpenAINativeHandler` can subclass from this, since it's obviously
3040// compatible with the OpenAI API. We can also rename it to `OpenAIHandler`.
@@ -157,7 +167,7 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
157167 const isDeepSeekV4 =
158168 deepseekReasoner && ( this . _isDeepSeek ( this . options . openAiBaseUrl ) || modelId . includes ( "deepseek" ) )
159169
160- const requestOptions : any = {
170+ const requestOptions : OpenAiChatCompletionParams = {
161171 model : modelId ,
162172 temperature : deepseekReasoner
163173 ? undefined
@@ -167,11 +177,11 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
167177 ...( isGrokXAI ? { } : { stream_options : { include_usage : true } } ) ,
168178 ...( reasoning && reasoning ) ,
169179 ...( deepseekReasoner && {
170- reasoning_effort : isDeepSeekV4
180+ reasoning_effort : ( isDeepSeekV4
171181 ? modelInfo . reasoningEffort === "xhigh"
172182 ? "max"
173183 : "high"
174- : ( modelInfo . reasoningEffort as any ) ,
184+ : modelInfo . reasoningEffort ) as OpenAI . Chat . Completions . ChatCompletionCreateParams [ "reasoning_effort" ] ,
175185 extra_body : {
176186 thinking : { type : "enabled" } ,
177187 } ,
@@ -187,12 +197,12 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
187197 // Add max_tokens if needed
188198 this . addMaxTokensIfNeeded ( requestOptions , modelInfo )
189199
190- let stream
200+ let stream : AsyncIterable < OpenAI . Chat . Completions . ChatCompletionChunk >
191201 try {
192202 stream = ( await this . client . chat . completions . create (
193- requestOptions ,
203+ requestOptions as OpenAI . Chat . Completions . ChatCompletionCreateParamsStreaming ,
194204 isAzureAiInference ? { path : OPENAI_AZURE_AI_INFERENCE_PATH } : { } ,
195- ) ) as any
205+ ) ) as AsyncIterable < OpenAI . Chat . Completions . ChatCompletionChunk >
196206 } catch ( error ) {
197207 throw handleOpenAIError ( error , this . providerName )
198208 }
@@ -244,7 +254,7 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
244254 const isDeepSeekV4 =
245255 deepseekReasoner && ( this . _isDeepSeek ( this . options . openAiBaseUrl ) || modelId . includes ( "deepseek" ) )
246256
247- const requestOptions : any = {
257+ const requestOptions : OpenAiChatCompletionParams = {
248258 model : modelId ,
249259 messages : deepseekReasoner
250260 ? convertToR1Format ( [ { role : "user" , content : systemPrompt } , ...messages ] , {
@@ -256,11 +266,11 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
256266 tool_choice : metadata ?. tool_choice ,
257267 parallel_tool_calls : metadata ?. parallelToolCalls ?? true ,
258268 ...( deepseekReasoner && {
259- reasoning_effort : isDeepSeekV4
269+ reasoning_effort : ( isDeepSeekV4
260270 ? modelInfo . reasoningEffort === "xhigh"
261271 ? "max"
262272 : "high"
263- : ( modelInfo . reasoningEffort as any ) ,
273+ : modelInfo . reasoningEffort ) as OpenAI . Chat . Completions . ChatCompletionCreateParams [ "reasoning_effort" ] ,
264274 extra_body : {
265275 thinking : { type : "enabled" } ,
266276 } ,
@@ -274,12 +284,12 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
274284 // Add max_tokens if needed
275285 this . addMaxTokensIfNeeded ( requestOptions , modelInfo )
276286
277- let response
287+ let response : OpenAI . Chat . Completions . ChatCompletion
278288 try {
279289 response = ( await this . client . chat . completions . create (
280- requestOptions ,
290+ requestOptions as OpenAI . Chat . Completions . ChatCompletionCreateParamsNonStreaming ,
281291 this . _isAzureAiInference ( modelUrl ) ? { path : OPENAI_AZURE_AI_INFERENCE_PATH } : { } ,
282- ) ) as any
292+ ) ) as OpenAI . Chat . Completions . ChatCompletion
283293 } catch ( error ) {
284294 throw handleOpenAIError ( error , this . providerName )
285295 }
0 commit comments