fix(ai): honor Codex Responses maxRetries

This commit is contained in:
Armin Ronacher
2026-05-25 12:30:04 +02:00
parent 3eb002766f
commit fc8a155901

View File

@@ -50,7 +50,7 @@ import { buildBaseOptions } from "./simple-options.ts";
const DEFAULT_CODEX_BASE_URL = "https://chatgpt.com/backend-api";
const JWT_CLAIM_PATH = "https://api.openai.com/auth" as const;
const MAX_RETRIES = 3;
const DEFAULT_MAX_RETRIES = 3;
const BASE_DELAY_MS = 1000;
const CODEX_TOOL_CALL_PROVIDERS = new Set(["openai", "openai-codex", "opencode"]);
const WEBSOCKET_MESSAGE_TOO_BIG_CLOSE_CODE = 1009;
@@ -231,8 +231,9 @@ export const streamOpenAICodexResponses: StreamFunction<"openai-codex-responses"
// Fetch with retry logic for rate limits and transient errors
let response: Response | undefined;
let lastError: Error | undefined;
const maxRetries = options?.maxRetries ?? DEFAULT_MAX_RETRIES;
for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
for (let attempt = 0; attempt <= maxRetries; attempt++) {
if (options?.signal?.aborted) {
throw new Error("Request was aborted");
}
@@ -254,7 +255,7 @@ export const streamOpenAICodexResponses: StreamFunction<"openai-codex-responses"
}
const errorText = await response.text();
if (attempt < MAX_RETRIES && isRetryableError(response.status, errorText)) {
if (attempt < maxRetries && isRetryableError(response.status, errorText)) {
let delayMs = BASE_DELAY_MS * 2 ** attempt;
const retryAfterMs = response.headers.get("retry-after-ms");
@@ -297,7 +298,7 @@ export const streamOpenAICodexResponses: StreamFunction<"openai-codex-responses"
}
lastError = error instanceof Error ? error : new Error(String(error));
// Network errors are retryable
if (attempt < MAX_RETRIES && !lastError.message.includes("usage limit")) {
if (attempt < maxRetries && !lastError.message.includes("usage limit")) {
const delayMs = BASE_DELAY_MS * 2 ** attempt;
await sleep(delayMs, options?.signal);
continue;