@@ -19,6 +19,8 @@ import type {
1919} from '@codebuff/common/types/contracts/logger'
2020import type { NextRequest } from 'next/server'
2121
22+ import type { ChatCompletionRequestBody } from '@/llm-api/types'
23+
2224import {
2325 handleOpenAINonStream ,
2426 OPENAI_SUPPORTED_MODELS ,
@@ -109,8 +111,9 @@ export async function postChatCompletions(params: {
109111 )
110112 }
111113
112- const bodyStream = 'stream' in body && body . stream
113- const runId = ( body as any ) ?. codebuff_metadata ?. run_id
114+ const typedBody = body as unknown as ChatCompletionRequestBody
115+ const bodyStream = typedBody . stream ?? false
116+ const runId = typedBody . codebuff_metadata ?. run_id
114117
115118 // Extract and validate API key
116119 const apiKey = extractApiKeyFromHeader ( req )
@@ -204,8 +207,7 @@ export async function postChatCompletions(params: {
204207 }
205208
206209 // Extract and validate agent run ID
207- const runIdFromBody : string | undefined = ( body as any ) . codebuff_metadata
208- ?. run_id
210+ const runIdFromBody = typedBody . codebuff_metadata ?. run_id
209211 if ( ! runIdFromBody || typeof runIdFromBody !== 'string' ) {
210212 trackEvent ( {
211213 event : AnalyticsEvent . CHAT_COMPLETIONS_VALIDATION_ERROR ,
@@ -269,7 +271,7 @@ export async function postChatCompletions(params: {
269271 if ( bodyStream ) {
270272 // Streaming request
271273 const stream = await handleOpenRouterStream ( {
272- body,
274+ body : typedBody ,
273275 userId,
274276 stripeCustomerId,
275277 agentId,
@@ -299,21 +301,20 @@ export async function postChatCompletions(params: {
299301 } )
300302 } else {
301303 // Non-streaming request
302- const model = ( body as any ) ? .model
303- const shortModelName =
304- typeof model === 'string' ? model . split ( '/' ) [ 1 ] : undefined
304+ const model = typedBody . model
305+ const modelParts = model . split ( '/' )
306+ const shortModelName = modelParts . length > 1 ? modelParts [ 1 ] : model
305307 const isOpenAIDirectModel =
306- typeof model === 'string' &&
307308 model . startsWith ( 'openai/' ) &&
308- OPENAI_SUPPORTED_MODELS . includes ( shortModelName as any )
309+ ( OPENAI_SUPPORTED_MODELS as readonly string [ ] ) . includes ( shortModelName )
309310 // Only use OpenAI endpoint for OpenAI models with n parameter
310311 // All other models (including non-OpenAI with n parameter) should use OpenRouter
311312 const shouldUseOpenAIEndpoint =
312- isOpenAIDirectModel && ( body as any ) ? .codebuff_metadata ?. n
313+ isOpenAIDirectModel && typedBody . codebuff_metadata ?. n !== undefined
313314
314315 const nonStreamRequest = shouldUseOpenAIEndpoint
315316 ? handleOpenAINonStream ( {
316- body,
317+ body : typedBody ,
317318 userId,
318319 stripeCustomerId,
319320 agentId,
@@ -322,7 +323,7 @@ export async function postChatCompletions(params: {
322323 insertMessageBigquery,
323324 } )
324325 : handleOpenRouterNonStream ( {
325- body,
326+ body : typedBody ,
326327 userId,
327328 stripeCustomerId,
328329 agentId,
@@ -360,13 +361,13 @@ export async function postChatCompletions(params: {
360361 userId,
361362 agentId,
362363 runId : runIdFromBody ,
363- model : ( body as any ) ? .model ,
364+ model : typedBody . model ,
364365 streaming : ! ! bodyStream ,
365366 hasByokKey : ! ! openrouterApiKey ,
366- messageCount : Array . isArray ( ( body as any ) ? .messages )
367- ? ( body as any ) . messages . length
367+ messageCount : Array . isArray ( typedBody . messages )
368+ ? typedBody . messages . length
368369 : 0 ,
369- messages : ( body as any ) ? .messages ,
370+ messages : typedBody . messages ,
370371 openrouterStatusCode : openrouterError ?. statusCode ,
371372 openrouterStatusText : openrouterError ?. statusText ,
372373 openrouterErrorCode : errorDetails ?. error ?. code ,
0 commit comments