@@ -57,7 +57,11 @@ export class OpenAiNativeHandler extends BaseProvider implements SingleCompletio
5757 this . options . enableGpt5ReasoningSummary = true
5858 }
5959 const apiKey = this . options . openAiNativeApiKey ?? "not-provided"
60- this . client = new OpenAI ( { baseURL : this . options . openAiNativeBaseUrl , apiKey } )
60+ this . client = new OpenAI ( {
61+ baseURL : this . options . openAiNativeBaseUrl ,
62+ apiKey,
63+ timeout : 15 * 1000 * 60 , // 15 minutes default timeout
64+ } )
6165 }
6266
6367 private normalizeGpt5Usage ( usage : any , model : OpenAiNativeModel ) : ApiStreamUsageChunk | undefined {
@@ -136,7 +140,7 @@ export class OpenAiNativeHandler extends BaseProvider implements SingleCompletio
136140 const isOriginalO1 = model . id === "o1"
137141 const { reasoning } = this . getModel ( )
138142
139- const response = await this . client . chat . completions . create ( {
143+ const params : any = {
140144 model : model . id ,
141145 messages : [
142146 {
@@ -148,9 +152,30 @@ export class OpenAiNativeHandler extends BaseProvider implements SingleCompletio
148152 stream : true ,
149153 stream_options : { include_usage : true } ,
150154 ...( reasoning && reasoning ) ,
151- } )
155+ }
156+
157+ // Add service_tier parameter if configured and not "auto"
158+ if ( this . options . serviceTier && this . options . serviceTier !== "auto" ) {
159+ params . service_tier = this . options . serviceTier
160+ console . log ( "[DEBUG] Setting service_tier parameter:" , this . options . serviceTier )
161+ console . log ( "[DEBUG] Full request params:" , JSON . stringify ( params , null , 2 ) )
162+ } else {
163+ console . log ( "[DEBUG] Service tier not set or is 'auto'. Current value:" , this . options . serviceTier )
164+ }
165+
166+ const response = await this . client . chat . completions . create ( params , { timeout : 15 * 1000 * 60 } )
167+ console . log ( "[DEBUG] OpenAI Chat Completions Response (O1Family):" , response )
152168
153- yield * this . handleStreamResponse ( response , model )
169+ if ( typeof ( response as any ) [ Symbol . asyncIterator ] !== "function" ) {
170+ throw new Error (
171+ "OpenAI SDK did not return an AsyncIterable for streaming response. Please check SDK version and usage." ,
172+ )
173+ }
174+
175+ yield * this . handleStreamResponse (
176+ response as unknown as AsyncIterable < OpenAI . Chat . Completions . ChatCompletionChunk > ,
177+ model ,
178+ )
154179 }
155180
156181 private async * handleReasonerMessage (
@@ -161,7 +186,7 @@ export class OpenAiNativeHandler extends BaseProvider implements SingleCompletio
161186 ) : ApiStream {
162187 const { reasoning } = this . getModel ( )
163188
164- const stream = await this . client . chat . completions . create ( {
189+ const params : any = {
165190 model : family ,
166191 messages : [
167192 {
@@ -173,9 +198,29 @@ export class OpenAiNativeHandler extends BaseProvider implements SingleCompletio
173198 stream : true ,
174199 stream_options : { include_usage : true } ,
175200 ...( reasoning && reasoning ) ,
176- } )
201+ }
202+
203+ // Add service_tier parameter if configured and not "auto"
204+ if ( this . options . serviceTier && this . options . serviceTier !== "auto" ) {
205+ params . service_tier = this . options . serviceTier
206+ console . log ( "[DEBUG] Setting service_tier parameter:" , this . options . serviceTier )
207+ console . log ( "[DEBUG] Full request params:" , JSON . stringify ( params , null , 2 ) )
208+ } else {
209+ console . log ( "[DEBUG] Service tier not set or is 'auto'. Current value:" , this . options . serviceTier )
210+ }
211+
212+ const stream = await this . client . chat . completions . create ( params , { timeout : 15 * 1000 * 60 } )
177213
178- yield * this . handleStreamResponse ( stream , model )
214+ if ( typeof ( stream as any ) [ Symbol . asyncIterator ] !== "function" ) {
215+ throw new Error (
216+ "OpenAI SDK did not return an AsyncIterable for streaming response. Please check SDK version and usage." ,
217+ )
218+ }
219+
220+ yield * this . handleStreamResponse (
221+ stream as unknown as AsyncIterable < OpenAI . Chat . Completions . ChatCompletionChunk > ,
222+ model ,
223+ )
179224 }
180225
181226 private async * handleDefaultModelMessage (
@@ -200,7 +245,16 @@ export class OpenAiNativeHandler extends BaseProvider implements SingleCompletio
200245 params . verbosity = verbosity
201246 }
202247
203- const stream = await this . client . chat . completions . create ( params )
248+ // Add service_tier parameter if configured and not "auto"
249+ if ( this . options . serviceTier && this . options . serviceTier !== "auto" ) {
250+ params . service_tier = this . options . serviceTier
251+ console . log ( "[DEBUG] Setting service_tier parameter:" , this . options . serviceTier )
252+ console . log ( "[DEBUG] Full request params:" , JSON . stringify ( params , null , 2 ) )
253+ } else {
254+ console . log ( "[DEBUG] Service tier not set or is 'auto'. Current value:" , this . options . serviceTier )
255+ }
256+
257+ const stream = await this . client . chat . completions . create ( params , { timeout : 15 * 1000 * 60 } )
204258
205259 if ( typeof ( stream as any ) [ Symbol . asyncIterator ] !== "function" ) {
206260 throw new Error (
@@ -277,6 +331,7 @@ export class OpenAiNativeHandler extends BaseProvider implements SingleCompletio
277331 temperature ?: number
278332 max_output_tokens ?: number
279333 previous_response_id ?: string
334+ service_tier ?: string
280335 }
281336
282337 const requestBody : Gpt5RequestBody = {
@@ -297,9 +352,20 @@ export class OpenAiNativeHandler extends BaseProvider implements SingleCompletio
297352 ...( requestPreviousResponseId && { previous_response_id : requestPreviousResponseId } ) ,
298353 }
299354
355+ // Add service_tier parameter if configured and not "auto"
356+ if ( this . options . serviceTier && this . options . serviceTier !== "auto" ) {
357+ requestBody . service_tier = this . options . serviceTier
358+ console . log ( "[DEBUG] Setting service_tier parameter:" , this . options . serviceTier )
359+ console . log ( "[DEBUG] Full request body:" , JSON . stringify ( requestBody , null , 2 ) )
360+ } else {
361+ console . log ( "[DEBUG] Service tier not set or is 'auto'. Current value:" , this . options . serviceTier )
362+ }
363+
300364 try {
301365 // Use the official SDK
302- const stream = ( await ( this . client as any ) . responses . create ( requestBody ) ) as AsyncIterable < any >
366+ const stream = ( await ( this . client as any ) . responses . create ( requestBody , {
367+ timeout : 15 * 1000 * 60 ,
368+ } ) ) as AsyncIterable < any >
303369
304370 if ( typeof ( stream as any ) [ Symbol . asyncIterator ] !== "function" ) {
305371 throw new Error (
@@ -308,11 +374,13 @@ export class OpenAiNativeHandler extends BaseProvider implements SingleCompletio
308374 }
309375
310376 for await ( const event of stream ) {
377+ console . log ( "[DEBUG] GPT-5 Responses API Stream Event:" , event ) // Log each event
311378 for await ( const outChunk of this . processGpt5Event ( event , model ) ) {
312379 yield outChunk
313380 }
314381 }
315382 } catch ( sdkErr : any ) {
383+ console . error ( "[DEBUG] OpenAI Responses API SDK Error:" , sdkErr ) // Log SDK errors
316384 // Check if this is a 400 error about previous_response_id not found
317385 const errorMessage = sdkErr ?. message || sdkErr ?. error ?. message || ""
318386 const is400Error = sdkErr ?. status === 400 || sdkErr ?. response ?. status === 400
@@ -419,6 +487,15 @@ export class OpenAiNativeHandler extends BaseProvider implements SingleCompletio
419487 const baseUrl = this . options . openAiNativeBaseUrl || "https://api.openai.com"
420488 const url = `${ baseUrl } /v1/responses`
421489
490+ // Log the exact request being sent
491+ console . log ( "[DEBUG] GPT-5 Responses API Request URL:" , url )
492+ console . log ( "[DEBUG] GPT-5 Responses API Request Headers:" , {
493+ "Content-Type" : "application/json" ,
494+ Authorization : `Bearer ${ apiKey . substring ( 0 , 8 ) } ...` , // Log only first 8 chars of API key for security
495+ Accept : "text/event-stream" ,
496+ } )
497+ console . log ( "[DEBUG] GPT-5 Responses API Request Body:" , JSON . stringify ( requestBody , null , 2 ) )
498+
422499 try {
423500 const response = await fetch ( url , {
424501 method : "POST" ,
@@ -430,8 +507,18 @@ export class OpenAiNativeHandler extends BaseProvider implements SingleCompletio
430507 body : JSON . stringify ( requestBody ) ,
431508 } )
432509
510+ // Log the response status
511+ console . log ( "[DEBUG] GPT-5 Responses API Response Status:" , response . status )
512+ // Convert headers to a plain object for logging
513+ const headersObj : Record < string , string > = { }
514+ response . headers . forEach ( ( value , key ) => {
515+ headersObj [ key ] = value
516+ } )
517+ console . log ( "[DEBUG] GPT-5 Responses API Response Headers:" , headersObj )
518+
433519 if ( ! response . ok ) {
434520 const errorText = await response . text ( )
521+ console . log ( "[DEBUG] GPT-5 Responses API Error Response Body:" , errorText )
435522
436523 let errorMessage = `GPT-5 API request failed (${ response . status } )`
437524 let errorDetails = ""
@@ -471,6 +558,11 @@ export class OpenAiNativeHandler extends BaseProvider implements SingleCompletio
471558 this . resolveResponseId ( undefined )
472559
473560 // Retry the request without the previous_response_id
561+ console . log (
562+ "[DEBUG] GPT-5 Responses API Retry Request Body:" ,
563+ JSON . stringify ( retryRequestBody , null , 2 ) ,
564+ )
565+
474566 const retryResponse = await fetch ( url , {
475567 method : "POST" ,
476568 headers : {
@@ -481,7 +573,11 @@ export class OpenAiNativeHandler extends BaseProvider implements SingleCompletio
481573 body : JSON . stringify ( retryRequestBody ) ,
482574 } )
483575
576+ console . log ( "[DEBUG] GPT-5 Responses API Retry Response Status:" , retryResponse . status )
577+
484578 if ( ! retryResponse . ok ) {
579+ const retryErrorText = await retryResponse . text ( )
580+ console . log ( "[DEBUG] GPT-5 Responses API Retry Error Response Body:" , retryErrorText )
485581 // If retry also fails, throw the original error
486582 throw new Error ( `GPT-5 API retry failed (${ retryResponse . status } )` )
487583 }
@@ -537,6 +633,7 @@ export class OpenAiNativeHandler extends BaseProvider implements SingleCompletio
537633 // Handle streaming response
538634 yield * this . handleGpt5StreamResponse ( response . body , model )
539635 } catch ( error ) {
636+ console . error ( "[DEBUG] GPT-5 Responses API Fetch Error:" , error ) // Log fetch errors
540637 if ( error instanceof Error ) {
541638 // Re-throw with the original error message if it's already formatted
542639 if ( error . message . includes ( "GPT-5" ) ) {
@@ -1040,6 +1137,7 @@ export class OpenAiNativeHandler extends BaseProvider implements SingleCompletio
10401137 * Used by both the official SDK streaming path and (optionally) by the SSE fallback.
10411138 */
10421139 private async * processGpt5Event ( event : any , model : OpenAiNativeModel ) : ApiStream {
1140+ console . log ( "[DEBUG] processGpt5Event: Processing event type:" , event ?. type )
10431141 // Persist response id for conversation continuity when available
10441142 if ( event ?. response ?. id ) {
10451143 this . resolveResponseId ( event . response . id )
@@ -1148,6 +1246,7 @@ export class OpenAiNativeHandler extends BaseProvider implements SingleCompletio
11481246 model : OpenAiNativeModel ,
11491247 ) : ApiStream {
11501248 for await ( const chunk of stream ) {
1249+ console . log ( "[DEBUG] handleStreamResponse: OpenAI Chat Completions Stream Chunk:" , chunk ) // Log each chunk here
11511250 const delta = chunk . choices [ 0 ] ?. delta
11521251
11531252 if ( delta ?. content ) {
0 commit comments