@@ -75,7 +75,7 @@ import { buildBaseOptions } from "./simple-options.js";
7575// ============================================================================
7676
7777const DEFAULT_CODEX_BASE_URL = "https://chatgpt.com/backend-api" ;
78- const MAX_RETRIES = 3 ;
78+ const DEFAULT_MAX_RETRIES = 3 ;
7979const BASE_DELAY_MS = 1000 ;
8080const REQUEST_COMPRESSION_ZSTD_LEVEL = 3 ;
8181const CODEX_TOOL_CALL_PROVIDERS = new Set ( [ "openai" , "opencode" ] ) ;
@@ -271,14 +271,9 @@ export const streamOpenAICodexResponses: StreamFunction<
271271 // per request, which forfeits session-affinity routing on the WS transport (the
272272 // backend routes by session_id/x-client-request-id). Left as-is for this fix;
273273 // see the SSE-path session_id addition in buildOpenAIClientHeaders (agents/openai-transport-stream.ts).
274- const websocketRequestId = options ?. sessionId || createCodexRequestId ( ) ;
275- const sseHeaders = buildSSEHeaders (
276- modelHeaders ,
277- optionHeaders ,
278- accountId ,
279- apiKey ,
280- options ?. sessionId ,
281- ) ;
274+ const sessionId = clampOpenAIPromptCacheKey ( options ?. sessionId ) ;
275+ const websocketRequestId = sessionId || createCodexRequestId ( ) ;
276+ const sseHeaders = buildSSEHeaders ( modelHeaders , optionHeaders , accountId , apiKey , sessionId ) ;
282277 const websocketHeaders = buildWebSocketHeaders (
283278 modelHeaders ,
284279 optionHeaders ,
@@ -371,8 +366,9 @@ export const streamOpenAICodexResponses: StreamFunction<
371366 // Fetch with retry logic for rate limits and transient errors
372367 let response : Response | undefined ;
373368 let lastError : Error | undefined ;
369+ const maxRetries = options ?. maxRetries ?? DEFAULT_MAX_RETRIES ;
374370
375- for ( let attempt = 0 ; attempt <= MAX_RETRIES ; attempt ++ ) {
371+ for ( let attempt = 0 ; attempt <= maxRetries ; attempt ++ ) {
376372 if ( activeSignal ?. aborted ) {
377373 throw new Error ( "Request was aborted" ) ;
378374 }
@@ -394,7 +390,7 @@ export const streamOpenAICodexResponses: StreamFunction<
394390 }
395391
396392 const errorText = await readChatGptResponsesErrorTextLimited ( response ) ;
397- if ( attempt < MAX_RETRIES && isRetryableError ( response . status , errorText ) ) {
393+ if ( attempt < maxRetries && isRetryableError ( response . status , errorText ) ) {
398394 let delayMs = BASE_DELAY_MS * 2 ** attempt ;
399395
400396 const retryAfterMs = response . headers . get ( "retry-after-ms" ) ;
@@ -453,7 +449,7 @@ export const streamOpenAICodexResponses: StreamFunction<
453449 }
454450 lastError = error instanceof Error ? error : new Error ( String ( error ) ) ;
455451 // Network errors are retryable
456- if ( attempt < MAX_RETRIES && ! lastError . message . includes ( "usage limit" ) ) {
452+ if ( attempt < maxRetries && ! lastError . message . includes ( "usage limit" ) ) {
457453 const delayMs = BASE_DELAY_MS * 2 ** attempt ;
458454 await sleepWithAbort ( delayMs , activeSignal ) ;
459455 continue ;
0 commit comments