|
| 1 | +/** |
| 2 | + * Parse and format Codex Responses `usage_limit_reached` bodies. |
| 3 | + * |
| 4 | + * Live shape (HTTP 429): |
| 5 | + * { detail: { error: { code, message, plan_type, resets_in_seconds } } } |
| 6 | + * |
| 7 | + * Harness extractErrorMessage only unwraps top-level `{ error: { message } }`, |
| 8 | + * so the nested detail is left on `InferenceError.raw` while the classified |
| 9 | + * message falls back to statusText. Retry and transcript paths re-read raw here. |
| 10 | + * |
| 11 | + * Matchers stay Codex-narrow: exact `usage_limit_*` codes only. Generic OpenAI |
| 12 | + * codes (`insufficient_quota`, `rate_limit_exceeded`) and loose "limit reached" |
| 13 | + * copy must not rebrand other providers as Codex. |
| 14 | + */ |
| 15 | + |
| 16 | +export type CodexUsageLimitError = { |
| 17 | + readonly code: string; |
| 18 | + readonly message: string; |
| 19 | + readonly planType?: string; |
| 20 | + readonly resetsInSeconds?: number; |
| 21 | +}; |
| 22 | + |
| 23 | +/** Exact codes observed / expected from the Codex ChatGPT backend. */ |
| 24 | +const USAGE_LIMIT_CODES = new Set([ |
| 25 | + "usage_limit_reached", |
| 26 | + "usage_limit_exceeded", |
| 27 | +]); |
| 28 | + |
| 29 | +function asRecord(value: unknown): Record<string, unknown> | undefined { |
| 30 | + if (typeof value !== "object" || value === null || Array.isArray(value)) return undefined; |
| 31 | + return value as Record<string, unknown>; |
| 32 | +} |
| 33 | + |
| 34 | +function tryParseJSON(text: string): unknown { |
| 35 | + try { |
| 36 | + return JSON.parse(text) as unknown; |
| 37 | + } catch { |
| 38 | + return undefined; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +function coerceBody(raw: unknown): unknown { |
| 43 | + if (typeof raw === "string") { |
| 44 | + const trimmed = raw.trim(); |
| 45 | + if (trimmed.length === 0) return undefined; |
| 46 | + return tryParseJSON(trimmed) ?? raw; |
| 47 | + } |
| 48 | + return raw; |
| 49 | +} |
| 50 | + |
| 51 | +function readErrorNode(body: unknown): Record<string, unknown> | undefined { |
| 52 | + const root = asRecord(body); |
| 53 | + if (root === undefined) return undefined; |
| 54 | + |
| 55 | + const detail = asRecord(root["detail"]); |
| 56 | + if (detail !== undefined) { |
| 57 | + const nested = asRecord(detail["error"]); |
| 58 | + if (nested !== undefined) return nested; |
| 59 | + // Some gateways put the fields directly under detail. |
| 60 | + if (typeof detail["code"] === "string") return detail; |
| 61 | + } |
| 62 | + |
| 63 | + const top = asRecord(root["error"]); |
| 64 | + if (top !== undefined) return top; |
| 65 | + |
| 66 | + if (typeof root["code"] === "string") return root; |
| 67 | + return undefined; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Returns a structured usage-limit error when `raw` matches the Codex body. |
| 72 | + * Undefined for unrelated payloads (including other providers' quota 429s). |
| 73 | + */ |
| 74 | +export function parseCodexUsageLimitError(raw: unknown): CodexUsageLimitError | undefined { |
| 75 | + const body = coerceBody(raw); |
| 76 | + const node = readErrorNode(body); |
| 77 | + if (node === undefined) return undefined; |
| 78 | + |
| 79 | + const code = typeof node["code"] === "string" ? node["code"] : undefined; |
| 80 | + // Exact code only — no regex, no message-only fallback. OpenAI uses |
| 81 | + // insufficient_quota / rate_limit_exceeded; those must stay non-Codex. |
| 82 | + if (code === undefined || !USAGE_LIMIT_CODES.has(code)) return undefined; |
| 83 | + |
| 84 | + const message = typeof node["message"] === "string" ? node["message"] : ""; |
| 85 | + |
| 86 | + const planType = |
| 87 | + typeof node["plan_type"] === "string" |
| 88 | + ? node["plan_type"] |
| 89 | + : typeof node["planType"] === "string" |
| 90 | + ? node["planType"] |
| 91 | + : undefined; |
| 92 | + |
| 93 | + const resetsRaw = node["resets_in_seconds"] ?? node["resetsInSeconds"] ?? node["reset_after_seconds"]; |
| 94 | + const resetsInSeconds = |
| 95 | + typeof resetsRaw === "number" && Number.isFinite(resetsRaw) && resetsRaw >= 0 |
| 96 | + ? Math.floor(resetsRaw) |
| 97 | + : undefined; |
| 98 | + |
| 99 | + return { |
| 100 | + code, |
| 101 | + message, |
| 102 | + ...(planType !== undefined ? { planType } : {}), |
| 103 | + ...(resetsInSeconds !== undefined ? { resetsInSeconds } : {}), |
| 104 | + }; |
| 105 | +} |
| 106 | + |
| 107 | +/** `retryAfterMs` for the default retry policy; undefined when the body omits reset. */ |
| 108 | +export function codexUsageLimitRetryAfterMs(parsed: CodexUsageLimitError): number | undefined { |
| 109 | + if (parsed.resetsInSeconds === undefined) return undefined; |
| 110 | + if (parsed.resetsInSeconds <= 0) return 0; |
| 111 | + return parsed.resetsInSeconds * 1000; |
| 112 | +} |
| 113 | + |
| 114 | +export function formatResetETA(seconds: number): string { |
| 115 | + if (seconds <= 0) return "now"; |
| 116 | + if (seconds < 60) return `${String(Math.ceil(seconds))}s`; |
| 117 | + if (seconds < 3600) return `~${String(Math.ceil(seconds / 60))}m`; |
| 118 | + if (seconds < 86_400) { |
| 119 | + const h = Math.floor(seconds / 3600); |
| 120 | + const m = Math.ceil((seconds % 3600) / 60); |
| 121 | + return m > 0 ? `~${String(h)}h ${String(m)}m` : `~${String(h)}h`; |
| 122 | + } |
| 123 | + const d = Math.floor(seconds / 86_400); |
| 124 | + const h = Math.ceil((seconds % 86_400) / 3600); |
| 125 | + return h > 0 ? `~${String(d)}d ${String(h)}h` : `~${String(d)}d`; |
| 126 | +} |
| 127 | + |
| 128 | +export type FormatCodexUsageLimitOpts = { |
| 129 | + /** Active Codex profile name (from `codex/<profile>` provider id) when known. */ |
| 130 | + readonly profile?: string; |
| 131 | +}; |
| 132 | + |
| 133 | +/** |
| 134 | + * Operator-facing one-liner: which plan/profile hit the wall, when it resets, |
| 135 | + * and how to try another subscription. |
| 136 | + */ |
| 137 | +export function formatCodexUsageLimitMessage( |
| 138 | + parsed: CodexUsageLimitError, |
| 139 | + opts?: FormatCodexUsageLimitOpts, |
| 140 | +): string { |
| 141 | + const who = |
| 142 | + opts?.profile !== undefined && opts.profile.length > 0 |
| 143 | + ? `Codex profile "${opts.profile}"` |
| 144 | + : "Codex"; |
| 145 | + const plan = |
| 146 | + parsed.planType !== undefined && parsed.planType.length > 0 |
| 147 | + ? ` (${parsed.planType.replace(/_/g, " ")})` |
| 148 | + : ""; |
| 149 | + const reset = |
| 150 | + parsed.resetsInSeconds !== undefined |
| 151 | + ? ` Resets in ${formatResetETA(parsed.resetsInSeconds)}.` |
| 152 | + : ""; |
| 153 | + return `${who} usage limit reached${plan}.${reset} Switch profile with /model if another Codex subscription has quota.`; |
| 154 | +} |
0 commit comments