codex-multi-auth 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (327) hide show
  1. package/LICENSE +1 -0
  2. package/README.md +162 -0
  3. package/assets/opencode-logo-ornate-dark.svg +18 -0
  4. package/assets/readme-hero.svg +31 -0
  5. package/config/README.md +87 -0
  6. package/config/minimal-opencode.json +13 -0
  7. package/config/opencode-legacy.json +571 -0
  8. package/config/opencode-modern.json +239 -0
  9. package/dist/index.d.ts +45 -0
  10. package/dist/index.d.ts.map +1 -0
  11. package/dist/index.js +3160 -0
  12. package/dist/index.js.map +1 -0
  13. package/dist/lib/accounts/rate-limits.d.ts +22 -0
  14. package/dist/lib/accounts/rate-limits.d.ts.map +1 -0
  15. package/dist/lib/accounts/rate-limits.js +63 -0
  16. package/dist/lib/accounts/rate-limits.js.map +1 -0
  17. package/dist/lib/accounts.d.ts +95 -0
  18. package/dist/lib/accounts.d.ts.map +1 -0
  19. package/dist/lib/accounts.js +668 -0
  20. package/dist/lib/accounts.js.map +1 -0
  21. package/dist/lib/audit.d.ts +45 -0
  22. package/dist/lib/audit.d.ts.map +1 -0
  23. package/dist/lib/audit.js +131 -0
  24. package/dist/lib/audit.js.map +1 -0
  25. package/dist/lib/auth/auth.d.ts +56 -0
  26. package/dist/lib/auth/auth.d.ts.map +1 -0
  27. package/dist/lib/auth/auth.js +214 -0
  28. package/dist/lib/auth/auth.js.map +1 -0
  29. package/dist/lib/auth/browser.d.ts +34 -0
  30. package/dist/lib/auth/browser.d.ts.map +1 -0
  31. package/dist/lib/auth/browser.js +185 -0
  32. package/dist/lib/auth/browser.js.map +1 -0
  33. package/dist/lib/auth/server.d.ts +24 -0
  34. package/dist/lib/auth/server.d.ts.map +1 -0
  35. package/dist/lib/auth/server.js +116 -0
  36. package/dist/lib/auth/server.js.map +1 -0
  37. package/dist/lib/auth/token-utils.d.ts +59 -0
  38. package/dist/lib/auth/token-utils.d.ts.map +1 -0
  39. package/dist/lib/auth/token-utils.js +331 -0
  40. package/dist/lib/auth/token-utils.js.map +1 -0
  41. package/dist/lib/auth-rate-limit.d.ts +20 -0
  42. package/dist/lib/auth-rate-limit.d.ts.map +1 -0
  43. package/dist/lib/auth-rate-limit.js +91 -0
  44. package/dist/lib/auth-rate-limit.js.map +1 -0
  45. package/dist/lib/auto-update-checker.d.ts +10 -0
  46. package/dist/lib/auto-update-checker.d.ts.map +1 -0
  47. package/dist/lib/auto-update-checker.js +216 -0
  48. package/dist/lib/auto-update-checker.js.map +1 -0
  49. package/dist/lib/capability-policy.d.ts +18 -0
  50. package/dist/lib/capability-policy.d.ts.map +1 -0
  51. package/dist/lib/capability-policy.js +150 -0
  52. package/dist/lib/capability-policy.js.map +1 -0
  53. package/dist/lib/circuit-breaker.d.ts +34 -0
  54. package/dist/lib/circuit-breaker.d.ts.map +1 -0
  55. package/dist/lib/circuit-breaker.js +124 -0
  56. package/dist/lib/circuit-breaker.js.map +1 -0
  57. package/dist/lib/cli.d.ts +64 -0
  58. package/dist/lib/cli.d.ts.map +1 -0
  59. package/dist/lib/cli.js +274 -0
  60. package/dist/lib/cli.js.map +1 -0
  61. package/dist/lib/codex-cli/observability.d.ts +22 -0
  62. package/dist/lib/codex-cli/observability.d.ts.map +1 -0
  63. package/dist/lib/codex-cli/observability.js +36 -0
  64. package/dist/lib/codex-cli/observability.js.map +1 -0
  65. package/dist/lib/codex-cli/state.d.ts +86 -0
  66. package/dist/lib/codex-cli/state.d.ts.map +1 -0
  67. package/dist/lib/codex-cli/state.js +470 -0
  68. package/dist/lib/codex-cli/state.js.map +1 -0
  69. package/dist/lib/codex-cli/sync.d.ts +27 -0
  70. package/dist/lib/codex-cli/sync.d.ts.map +1 -0
  71. package/dist/lib/codex-cli/sync.js +325 -0
  72. package/dist/lib/codex-cli/sync.js.map +1 -0
  73. package/dist/lib/codex-cli/writer.d.ts +12 -0
  74. package/dist/lib/codex-cli/writer.d.ts.map +1 -0
  75. package/dist/lib/codex-cli/writer.js +388 -0
  76. package/dist/lib/codex-cli/writer.js.map +1 -0
  77. package/dist/lib/codex-manager.d.ts +2 -0
  78. package/dist/lib/codex-manager.d.ts.map +1 -0
  79. package/dist/lib/codex-manager.js +4841 -0
  80. package/dist/lib/codex-manager.js.map +1 -0
  81. package/dist/lib/config.d.ts +269 -0
  82. package/dist/lib/config.d.ts.map +1 -0
  83. package/dist/lib/config.js +789 -0
  84. package/dist/lib/config.js.map +1 -0
  85. package/dist/lib/constants.d.ts +78 -0
  86. package/dist/lib/constants.d.ts.map +1 -0
  87. package/dist/lib/constants.js +78 -0
  88. package/dist/lib/constants.js.map +1 -0
  89. package/dist/lib/context-overflow.d.ts +27 -0
  90. package/dist/lib/context-overflow.d.ts.map +1 -0
  91. package/dist/lib/context-overflow.js +124 -0
  92. package/dist/lib/context-overflow.js.map +1 -0
  93. package/dist/lib/dashboard-settings.d.ts +90 -0
  94. package/dist/lib/dashboard-settings.d.ts.map +1 -0
  95. package/dist/lib/dashboard-settings.js +327 -0
  96. package/dist/lib/dashboard-settings.js.map +1 -0
  97. package/dist/lib/entitlement-cache.d.ts +41 -0
  98. package/dist/lib/entitlement-cache.d.ts.map +1 -0
  99. package/dist/lib/entitlement-cache.js +137 -0
  100. package/dist/lib/entitlement-cache.js.map +1 -0
  101. package/dist/lib/errors.d.ts +113 -0
  102. package/dist/lib/errors.d.ts.map +1 -0
  103. package/dist/lib/errors.js +103 -0
  104. package/dist/lib/errors.js.map +1 -0
  105. package/dist/lib/forecast.d.ts +42 -0
  106. package/dist/lib/forecast.d.ts.map +1 -0
  107. package/dist/lib/forecast.js +256 -0
  108. package/dist/lib/forecast.js.map +1 -0
  109. package/dist/lib/health.d.ts +33 -0
  110. package/dist/lib/health.d.ts.map +1 -0
  111. package/dist/lib/health.js +70 -0
  112. package/dist/lib/health.js.map +1 -0
  113. package/dist/lib/index.d.ts +32 -0
  114. package/dist/lib/index.d.ts.map +1 -0
  115. package/dist/lib/index.js +32 -0
  116. package/dist/lib/index.js.map +1 -0
  117. package/dist/lib/live-account-sync.d.ts +39 -0
  118. package/dist/lib/live-account-sync.d.ts.map +1 -0
  119. package/dist/lib/live-account-sync.js +196 -0
  120. package/dist/lib/live-account-sync.js.map +1 -0
  121. package/dist/lib/logger.d.ts +40 -0
  122. package/dist/lib/logger.d.ts.map +1 -0
  123. package/dist/lib/logger.js +364 -0
  124. package/dist/lib/logger.js.map +1 -0
  125. package/dist/lib/oauth-success.html +338 -0
  126. package/dist/lib/parallel-probe.d.ts +28 -0
  127. package/dist/lib/parallel-probe.d.ts.map +1 -0
  128. package/dist/lib/parallel-probe.js +97 -0
  129. package/dist/lib/parallel-probe.js.map +1 -0
  130. package/dist/lib/preemptive-quota-scheduler.d.ts +53 -0
  131. package/dist/lib/preemptive-quota-scheduler.d.ts.map +1 -0
  132. package/dist/lib/preemptive-quota-scheduler.js +220 -0
  133. package/dist/lib/preemptive-quota-scheduler.js.map +1 -0
  134. package/dist/lib/proactive-refresh.d.ts +66 -0
  135. package/dist/lib/proactive-refresh.d.ts.map +1 -0
  136. package/dist/lib/proactive-refresh.js +143 -0
  137. package/dist/lib/proactive-refresh.js.map +1 -0
  138. package/dist/lib/prompts/codex-opencode-bridge.d.ts +19 -0
  139. package/dist/lib/prompts/codex-opencode-bridge.d.ts.map +1 -0
  140. package/dist/lib/prompts/codex-opencode-bridge.js +169 -0
  141. package/dist/lib/prompts/codex-opencode-bridge.js.map +1 -0
  142. package/dist/lib/prompts/codex.d.ts +41 -0
  143. package/dist/lib/prompts/codex.d.ts.map +1 -0
  144. package/dist/lib/prompts/codex.js +383 -0
  145. package/dist/lib/prompts/codex.js.map +1 -0
  146. package/dist/lib/prompts/opencode-codex.d.ts +25 -0
  147. package/dist/lib/prompts/opencode-codex.d.ts.map +1 -0
  148. package/dist/lib/prompts/opencode-codex.js +270 -0
  149. package/dist/lib/prompts/opencode-codex.js.map +1 -0
  150. package/dist/lib/quota-cache.d.ts +68 -0
  151. package/dist/lib/quota-cache.d.ts.map +1 -0
  152. package/dist/lib/quota-cache.js +224 -0
  153. package/dist/lib/quota-cache.js.map +1 -0
  154. package/dist/lib/quota-probe.d.ts +49 -0
  155. package/dist/lib/quota-probe.d.ts.map +1 -0
  156. package/dist/lib/quota-probe.js +368 -0
  157. package/dist/lib/quota-probe.js.map +1 -0
  158. package/dist/lib/recovery/constants.d.ts +12 -0
  159. package/dist/lib/recovery/constants.d.ts.map +1 -0
  160. package/dist/lib/recovery/constants.js +31 -0
  161. package/dist/lib/recovery/constants.js.map +1 -0
  162. package/dist/lib/recovery/index.d.ts +12 -0
  163. package/dist/lib/recovery/index.d.ts.map +1 -0
  164. package/dist/lib/recovery/index.js +12 -0
  165. package/dist/lib/recovery/index.js.map +1 -0
  166. package/dist/lib/recovery/storage.d.ts +24 -0
  167. package/dist/lib/recovery/storage.d.ts.map +1 -0
  168. package/dist/lib/recovery/storage.js +362 -0
  169. package/dist/lib/recovery/storage.js.map +1 -0
  170. package/dist/lib/recovery/types.d.ts +116 -0
  171. package/dist/lib/recovery/types.d.ts.map +1 -0
  172. package/dist/lib/recovery/types.js +7 -0
  173. package/dist/lib/recovery/types.js.map +1 -0
  174. package/dist/lib/recovery.d.ts +31 -0
  175. package/dist/lib/recovery.d.ts.map +1 -0
  176. package/dist/lib/recovery.js +313 -0
  177. package/dist/lib/recovery.js.map +1 -0
  178. package/dist/lib/refresh-guardian.d.ts +31 -0
  179. package/dist/lib/refresh-guardian.d.ts.map +1 -0
  180. package/dist/lib/refresh-guardian.js +151 -0
  181. package/dist/lib/refresh-guardian.js.map +1 -0
  182. package/dist/lib/refresh-lease.d.ts +37 -0
  183. package/dist/lib/refresh-lease.d.ts.map +1 -0
  184. package/dist/lib/refresh-lease.js +335 -0
  185. package/dist/lib/refresh-lease.js.map +1 -0
  186. package/dist/lib/refresh-queue.d.ts +117 -0
  187. package/dist/lib/refresh-queue.d.ts.map +1 -0
  188. package/dist/lib/refresh-queue.js +297 -0
  189. package/dist/lib/refresh-queue.js.map +1 -0
  190. package/dist/lib/request/failure-policy.d.ts +42 -0
  191. package/dist/lib/request/failure-policy.d.ts.map +1 -0
  192. package/dist/lib/request/failure-policy.js +133 -0
  193. package/dist/lib/request/failure-policy.js.map +1 -0
  194. package/dist/lib/request/fetch-helpers.d.ts +152 -0
  195. package/dist/lib/request/fetch-helpers.d.ts.map +1 -0
  196. package/dist/lib/request/fetch-helpers.js +704 -0
  197. package/dist/lib/request/fetch-helpers.js.map +1 -0
  198. package/dist/lib/request/helpers/input-utils.d.ts +7 -0
  199. package/dist/lib/request/helpers/input-utils.d.ts.map +1 -0
  200. package/dist/lib/request/helpers/input-utils.js +214 -0
  201. package/dist/lib/request/helpers/input-utils.js.map +1 -0
  202. package/dist/lib/request/helpers/model-map.d.ts +28 -0
  203. package/dist/lib/request/helpers/model-map.d.ts.map +1 -0
  204. package/dist/lib/request/helpers/model-map.js +133 -0
  205. package/dist/lib/request/helpers/model-map.js.map +1 -0
  206. package/dist/lib/request/helpers/tool-utils.d.ts +29 -0
  207. package/dist/lib/request/helpers/tool-utils.d.ts.map +1 -0
  208. package/dist/lib/request/helpers/tool-utils.js +117 -0
  209. package/dist/lib/request/helpers/tool-utils.js.map +1 -0
  210. package/dist/lib/request/rate-limit-backoff.d.ts +17 -0
  211. package/dist/lib/request/rate-limit-backoff.d.ts.map +1 -0
  212. package/dist/lib/request/rate-limit-backoff.js +83 -0
  213. package/dist/lib/request/rate-limit-backoff.js.map +1 -0
  214. package/dist/lib/request/request-transformer.d.ts +107 -0
  215. package/dist/lib/request/request-transformer.d.ts.map +1 -0
  216. package/dist/lib/request/request-transformer.js +814 -0
  217. package/dist/lib/request/request-transformer.js.map +1 -0
  218. package/dist/lib/request/response-handler.d.ts +23 -0
  219. package/dist/lib/request/response-handler.d.ts.map +1 -0
  220. package/dist/lib/request/response-handler.js +155 -0
  221. package/dist/lib/request/response-handler.js.map +1 -0
  222. package/dist/lib/request/stream-failover.d.ts +21 -0
  223. package/dist/lib/request/stream-failover.d.ts.map +1 -0
  224. package/dist/lib/request/stream-failover.js +204 -0
  225. package/dist/lib/request/stream-failover.js.map +1 -0
  226. package/dist/lib/rotation.d.ts +146 -0
  227. package/dist/lib/rotation.d.ts.map +1 -0
  228. package/dist/lib/rotation.js +321 -0
  229. package/dist/lib/rotation.js.map +1 -0
  230. package/dist/lib/runtime-paths.d.ts +58 -0
  231. package/dist/lib/runtime-paths.d.ts.map +1 -0
  232. package/dist/lib/runtime-paths.js +164 -0
  233. package/dist/lib/runtime-paths.js.map +1 -0
  234. package/dist/lib/schemas.d.ts +435 -0
  235. package/dist/lib/schemas.d.ts.map +1 -0
  236. package/dist/lib/schemas.js +268 -0
  237. package/dist/lib/schemas.js.map +1 -0
  238. package/dist/lib/session-affinity.d.ts +23 -0
  239. package/dist/lib/session-affinity.d.ts.map +1 -0
  240. package/dist/lib/session-affinity.js +127 -0
  241. package/dist/lib/session-affinity.js.map +1 -0
  242. package/dist/lib/shutdown.d.ts +7 -0
  243. package/dist/lib/shutdown.d.ts.map +1 -0
  244. package/dist/lib/shutdown.js +43 -0
  245. package/dist/lib/shutdown.js.map +1 -0
  246. package/dist/lib/storage/migrations.d.ts +59 -0
  247. package/dist/lib/storage/migrations.d.ts.map +1 -0
  248. package/dist/lib/storage/migrations.js +41 -0
  249. package/dist/lib/storage/migrations.js.map +1 -0
  250. package/dist/lib/storage/paths.d.ts +51 -0
  251. package/dist/lib/storage/paths.d.ts.map +1 -0
  252. package/dist/lib/storage/paths.js +152 -0
  253. package/dist/lib/storage/paths.js.map +1 -0
  254. package/dist/lib/storage.d.ts +106 -0
  255. package/dist/lib/storage.d.ts.map +1 -0
  256. package/dist/lib/storage.js +896 -0
  257. package/dist/lib/storage.js.map +1 -0
  258. package/dist/lib/table-formatter.d.ts +32 -0
  259. package/dist/lib/table-formatter.d.ts.map +1 -0
  260. package/dist/lib/table-formatter.js +44 -0
  261. package/dist/lib/table-formatter.js.map +1 -0
  262. package/dist/lib/tools/hashline-tools.d.ts +51 -0
  263. package/dist/lib/tools/hashline-tools.d.ts.map +1 -0
  264. package/dist/lib/tools/hashline-tools.js +456 -0
  265. package/dist/lib/tools/hashline-tools.js.map +1 -0
  266. package/dist/lib/types.d.ts +130 -0
  267. package/dist/lib/types.d.ts.map +1 -0
  268. package/dist/lib/types.js +2 -0
  269. package/dist/lib/types.js.map +1 -0
  270. package/dist/lib/ui/ansi.d.ts +40 -0
  271. package/dist/lib/ui/ansi.d.ts.map +1 -0
  272. package/dist/lib/ui/ansi.js +68 -0
  273. package/dist/lib/ui/ansi.js.map +1 -0
  274. package/dist/lib/ui/auth-menu.d.ts +76 -0
  275. package/dist/lib/ui/auth-menu.d.ts.map +1 -0
  276. package/dist/lib/ui/auth-menu.js +590 -0
  277. package/dist/lib/ui/auth-menu.js.map +1 -0
  278. package/dist/lib/ui/confirm.d.ts +11 -0
  279. package/dist/lib/ui/confirm.d.ts.map +1 -0
  280. package/dist/lib/ui/confirm.js +29 -0
  281. package/dist/lib/ui/confirm.js.map +1 -0
  282. package/dist/lib/ui/copy.d.ts +123 -0
  283. package/dist/lib/ui/copy.d.ts.map +1 -0
  284. package/dist/lib/ui/copy.js +127 -0
  285. package/dist/lib/ui/copy.js.map +1 -0
  286. package/dist/lib/ui/format.d.ts +62 -0
  287. package/dist/lib/ui/format.d.ts.map +1 -0
  288. package/dist/lib/ui/format.js +205 -0
  289. package/dist/lib/ui/format.js.map +1 -0
  290. package/dist/lib/ui/runtime.d.ts +43 -0
  291. package/dist/lib/ui/runtime.d.ts.map +1 -0
  292. package/dist/lib/ui/runtime.js +69 -0
  293. package/dist/lib/ui/runtime.js.map +1 -0
  294. package/dist/lib/ui/select.d.ts +60 -0
  295. package/dist/lib/ui/select.d.ts.map +1 -0
  296. package/dist/lib/ui/select.js +467 -0
  297. package/dist/lib/ui/select.js.map +1 -0
  298. package/dist/lib/ui/theme.d.ts +56 -0
  299. package/dist/lib/ui/theme.d.ts.map +1 -0
  300. package/dist/lib/ui/theme.js +186 -0
  301. package/dist/lib/ui/theme.js.map +1 -0
  302. package/dist/lib/unified-settings.d.ts +71 -0
  303. package/dist/lib/unified-settings.d.ts.map +1 -0
  304. package/dist/lib/unified-settings.js +299 -0
  305. package/dist/lib/unified-settings.js.map +1 -0
  306. package/dist/lib/utils.d.ts +29 -0
  307. package/dist/lib/utils.d.ts.map +1 -0
  308. package/dist/lib/utils.js +54 -0
  309. package/dist/lib/utils.js.map +1 -0
  310. package/package.json +115 -0
  311. package/scripts/audit-dev-allowlist.js +128 -0
  312. package/scripts/bench-format/hashline-v2.mjs +642 -0
  313. package/scripts/bench-format/models.mjs +105 -0
  314. package/scripts/bench-format/opencode.mjs +205 -0
  315. package/scripts/bench-format/render.mjs +496 -0
  316. package/scripts/bench-format/stats.mjs +54 -0
  317. package/scripts/bench-format/tasks.mjs +151 -0
  318. package/scripts/benchmark-edit-formats.mjs +1161 -0
  319. package/scripts/benchmark-render-dashboard.mjs +49 -0
  320. package/scripts/codex-multi-auth.js +6 -0
  321. package/scripts/codex-routing.js +34 -0
  322. package/scripts/codex.js +122 -0
  323. package/scripts/copy-oauth-success.js +37 -0
  324. package/scripts/install-opencode-codex-auth.js +193 -0
  325. package/scripts/test-all-models.sh +7 -0
  326. package/scripts/test-model-matrix.js +424 -0
  327. package/scripts/validate-model-map.sh +7 -0
@@ -0,0 +1,704 @@
1
+ /**
2
+ * Helper functions for the custom fetch implementation
3
+ * These functions break down the complex fetch logic into manageable, testable units
4
+ */
5
+ import { queuedRefresh } from "../refresh-queue.js";
6
+ import { logRequest, logError, logWarn } from "../logger.js";
7
+ import { getCodexInstructions, getModelFamily } from "../prompts/codex.js";
8
+ import { transformRequestBody, normalizeModel } from "./request-transformer.js";
9
+ import { convertSseToJson, ensureContentType } from "./response-handler.js";
10
+ import { CodexAuthError } from "../errors.js";
11
+ import { isRecord } from "../utils.js";
12
+ import { CODEX_BASE_URL, HTTP_STATUS, OPENAI_HEADERS, OPENAI_HEADER_VALUES, URL_PATHS, ERROR_MESSAGES, LOG_STAGES, } from "../constants.js";
13
+ const CODEX_BASE_URL_OBJECT = new URL(CODEX_BASE_URL);
14
+ const CODEX_BASE_PATH_PREFIX = CODEX_BASE_URL_OBJECT.pathname.endsWith("/")
15
+ ? CODEX_BASE_URL_OBJECT.pathname.slice(0, -1)
16
+ : CODEX_BASE_URL_OBJECT.pathname;
17
+ const CHATGPT_CODEX_UNSUPPORTED_MODEL_CODE = "model_not_supported_with_chatgpt_account";
18
+ const CHATGPT_CODEX_UNSUPPORTED_MODEL_PATTERN = /model is not supported when using codex with a chatgpt account/i;
19
+ const NORMALIZED_UNSUPPORTED_MODEL_PATTERN = /the model ['"]([^'"]+)['"] is not currently available for this chatgpt account/i;
20
+ export const DEFAULT_UNSUPPORTED_CODEX_FALLBACK_CHAIN = {
21
+ "gpt-5.3-codex-spark": ["gpt-5-codex", "gpt-5.3-codex", "gpt-5.2-codex"],
22
+ "gpt-5.3-codex": ["gpt-5-codex", "gpt-5.2-codex"],
23
+ "gpt-5.2-codex": ["gpt-5-codex"],
24
+ "gpt-5.1-codex": ["gpt-5-codex"],
25
+ };
26
+ function canonicalizeModelName(model) {
27
+ if (!model)
28
+ return undefined;
29
+ const trimmed = model.trim().toLowerCase();
30
+ if (!trimmed)
31
+ return undefined;
32
+ const stripped = trimmed.includes("/")
33
+ ? (trimmed.split("/").pop() ?? trimmed)
34
+ : trimmed;
35
+ return stripped.replace(/-(none|minimal|low|medium|high|xhigh)$/i, "");
36
+ }
37
+ function normalizeFallbackChain(customChain) {
38
+ const normalized = {};
39
+ for (const [key, values] of Object.entries(DEFAULT_UNSUPPORTED_CODEX_FALLBACK_CHAIN)) {
40
+ const normalizedKey = canonicalizeModelName(key);
41
+ if (!normalizedKey)
42
+ continue;
43
+ normalized[normalizedKey] = values
44
+ .map((value) => canonicalizeModelName(value))
45
+ .filter((value) => !!value);
46
+ }
47
+ if (!customChain) {
48
+ return normalized;
49
+ }
50
+ for (const [key, values] of Object.entries(customChain)) {
51
+ const normalizedKey = canonicalizeModelName(key);
52
+ if (!normalizedKey || !Array.isArray(values))
53
+ continue;
54
+ const normalizedValues = values
55
+ .map((value) => canonicalizeModelName(value))
56
+ .filter((value) => !!value);
57
+ if (normalizedValues.length > 0) {
58
+ normalized[normalizedKey] = normalizedValues;
59
+ }
60
+ }
61
+ return normalized;
62
+ }
63
+ export function extractUnsupportedCodexModelFromText(bodyText) {
64
+ const directMatch = bodyText.match(/['"]([^'"]+)['"]\s+model is not supported when using codex with a chatgpt account/i);
65
+ if (directMatch?.[1]) {
66
+ return canonicalizeModelName(directMatch[1]);
67
+ }
68
+ const normalizedMatch = bodyText.match(NORMALIZED_UNSUPPORTED_MODEL_PATTERN);
69
+ if (normalizedMatch?.[1]) {
70
+ return canonicalizeModelName(normalizedMatch[1]);
71
+ }
72
+ return undefined;
73
+ }
74
+ function isUnsupportedCodexModelForChatGpt(status, bodyText) {
75
+ if (status !== HTTP_STATUS.BAD_REQUEST)
76
+ return false;
77
+ if (!bodyText)
78
+ return false;
79
+ return CHATGPT_CODEX_UNSUPPORTED_MODEL_PATTERN.test(bodyText);
80
+ }
81
+ export function getUnsupportedCodexModelInfo(errorBody) {
82
+ if (!isRecord(errorBody)) {
83
+ return { isUnsupported: false };
84
+ }
85
+ const maybeError = errorBody.error;
86
+ if (!isRecord(maybeError)) {
87
+ return { isUnsupported: false };
88
+ }
89
+ const code = typeof maybeError.code === "string" ? maybeError.code : undefined;
90
+ const message = typeof maybeError.message === "string" ? maybeError.message : undefined;
91
+ const unsupportedModelFromPayload = typeof maybeError.unsupported_model === "string"
92
+ ? maybeError.unsupported_model
93
+ : undefined;
94
+ const unsupportedModel = unsupportedModelFromPayload
95
+ ? canonicalizeModelName(unsupportedModelFromPayload)
96
+ : extractUnsupportedCodexModelFromText(message ?? "");
97
+ const isUnsupported = code === CHATGPT_CODEX_UNSUPPORTED_MODEL_CODE ||
98
+ (message ? CHATGPT_CODEX_UNSUPPORTED_MODEL_PATTERN.test(message) : false);
99
+ return {
100
+ isUnsupported,
101
+ code,
102
+ message,
103
+ unsupportedModel: unsupportedModel ?? undefined,
104
+ };
105
+ }
106
+ export function resolveUnsupportedCodexFallbackModel(options) {
107
+ if (!options.fallbackOnUnsupportedCodexModel)
108
+ return undefined;
109
+ const unsupported = getUnsupportedCodexModelInfo(options.errorBody);
110
+ if (!unsupported.isUnsupported)
111
+ return undefined;
112
+ const requestedModel = canonicalizeModelName(options.requestedModel);
113
+ const currentModel = requestedModel ?? unsupported.unsupportedModel;
114
+ if (!currentModel)
115
+ return undefined;
116
+ const attempted = new Set();
117
+ for (const model of options.attemptedModels ?? []) {
118
+ const normalized = canonicalizeModelName(model);
119
+ if (normalized)
120
+ attempted.add(normalized);
121
+ }
122
+ const chain = normalizeFallbackChain(options.customChain);
123
+ const targets = chain[currentModel] ?? [];
124
+ if (targets.length === 0)
125
+ return undefined;
126
+ for (const target of targets) {
127
+ if (!options.fallbackToGpt52OnUnsupportedGpt53 &&
128
+ currentModel === "gpt-5.3-codex" &&
129
+ target === "gpt-5.2-codex") {
130
+ continue;
131
+ }
132
+ if (target === currentModel)
133
+ continue;
134
+ if (attempted.has(target))
135
+ continue;
136
+ return target;
137
+ }
138
+ return undefined;
139
+ }
140
+ /**
141
+ * Returns true when the legacy `gpt-5.3-codex -> gpt-5.2-codex` edge is available.
142
+ */
143
+ export function shouldFallbackToGpt52OnUnsupportedGpt53(requestedModel, errorBody) {
144
+ if (canonicalizeModelName(requestedModel) !== "gpt-5.3-codex") {
145
+ return false;
146
+ }
147
+ return (resolveUnsupportedCodexFallbackModel({
148
+ requestedModel,
149
+ errorBody,
150
+ // Skip the canonical `gpt-5-codex` step and probe whether the legacy
151
+ // gpt-5.2 edge is still active under current policy/toggles.
152
+ attemptedModels: ["gpt-5-codex"],
153
+ fallbackOnUnsupportedCodexModel: true,
154
+ fallbackToGpt52OnUnsupportedGpt53: true,
155
+ }) === "gpt-5.2-codex");
156
+ }
157
+ /**
158
+ * Detects whether an error code or response body indicates an entitlement/subscription issue for Codex models.
159
+ *
160
+ * Entitlement errors signal that the requested feature is not included in the user's plan and should not be treated as rate limits.
161
+ * This function is pure and safe to call concurrently; it performs no filesystem access (including on Windows) and does not read or redact tokens — callers must avoid passing sensitive credentials in `code` or `bodyText`.
162
+ *
163
+ * @param code - The error code string returned by the service
164
+ * @param bodyText - The response body text to inspect for entitlement-related phrases
165
+ * @returns `true` if the combined `code` or `bodyText` indicates an entitlement/subscription issue, `false` otherwise
166
+ */
167
+ export function isEntitlementError(code, bodyText) {
168
+ const haystack = `${code} ${bodyText}`.toLowerCase();
169
+ // "usage_not_included" means the subscription doesn't include this feature
170
+ // This is different from "usage_limit_reached" which is a temporary quota limit
171
+ return /usage_not_included|not.included.in.your.plan|subscription.does.not.include/i.test(haystack);
172
+ }
173
+ /**
174
+ * Constructs a standardized 403 entitlement error Response indicating the user lacks access to Codex models.
175
+ *
176
+ * This function returns a JSON Response with an `error` payload containing a user-facing message, a
177
+ * `type` of `"entitlement_error"`, and a `code` of `"usage_not_included"`. The message suggests checking
178
+ * account/workspace access and re-authenticating with `codex login`.
179
+ *
180
+ * Concurrency: stateless and safe to call concurrently from multiple threads or requests.
181
+ * Windows filesystem behavior: none (function does not access the filesystem).
182
+ * Token redaction: any tokens are not included in the generated payload; do not pass sensitive tokens in `_bodyText`.
183
+ *
184
+ * @param _bodyText - Original response body text (accepted for compatibility; ignored when building the response)
185
+ * @returns A 403 Response with a JSON body describing the entitlement error and guidance for resolving it
186
+ */
187
+ export function createEntitlementErrorResponse(_bodyText) {
188
+ const message = "This model is not included in your ChatGPT subscription. " +
189
+ "Please check that your account or workspace has access to Codex models (Plus/Pro/Business/Enterprise). " +
190
+ "If you recently subscribed or switched workspaces, try logging out and back in with `codex login`.";
191
+ const payload = {
192
+ error: {
193
+ message,
194
+ type: "entitlement_error",
195
+ code: "usage_not_included",
196
+ },
197
+ };
198
+ return new Response(JSON.stringify(payload), {
199
+ status: 403, // Forbidden - not a rate limit
200
+ statusText: "Forbidden",
201
+ headers: { "content-type": "application/json; charset=utf-8" },
202
+ });
203
+ }
204
+ /**
205
+ * Determines if the current auth token needs to be refreshed
206
+ * @param auth - Current authentication state
207
+ * @returns True if token is expired or invalid
208
+ */
209
+ export function shouldRefreshToken(auth, skewMs = 0) {
210
+ if (auth.type !== "oauth")
211
+ return true;
212
+ if (!auth.access)
213
+ return true;
214
+ const safeSkewMs = Math.max(0, Math.floor(skewMs));
215
+ return auth.expires <= Date.now() + safeSkewMs;
216
+ }
217
+ /**
218
+ * Refreshes the OAuth token and updates stored credentials
219
+ * @param currentAuth - Current auth state
220
+ * @param client - Opencode client for updating stored credentials
221
+ * @returns Updated auth (throws on failure)
222
+ */
223
+ export async function refreshAndUpdateToken(currentAuth, client) {
224
+ const refreshToken = currentAuth.type === "oauth" ? currentAuth.refresh : "";
225
+ const refreshResult = await queuedRefresh(refreshToken);
226
+ if (refreshResult.type === "failed") {
227
+ throw new CodexAuthError(ERROR_MESSAGES.TOKEN_REFRESH_FAILED, { retryable: false });
228
+ }
229
+ await client.auth.set({
230
+ path: { id: "openai" },
231
+ body: {
232
+ type: "oauth",
233
+ access: refreshResult.access,
234
+ refresh: refreshResult.refresh,
235
+ expires: refreshResult.expires,
236
+ multiAccount: true,
237
+ },
238
+ });
239
+ // Update current auth reference if it's OAuth type
240
+ if (currentAuth.type === "oauth") {
241
+ currentAuth.access = refreshResult.access;
242
+ currentAuth.refresh = refreshResult.refresh;
243
+ currentAuth.expires = refreshResult.expires;
244
+ }
245
+ return currentAuth;
246
+ }
247
+ /**
248
+ * Extracts URL string from various request input types
249
+ * @param input - Request input (string, URL, or Request object)
250
+ * @returns URL string
251
+ */
252
+ export function extractRequestUrl(input) {
253
+ if (typeof input === "string")
254
+ return input;
255
+ if (input instanceof URL)
256
+ return input.toString();
257
+ return input.url;
258
+ }
259
+ /**
260
+ * Rewrites OpenAI API URLs to Codex backend URLs
261
+ * @param url - Original URL
262
+ * @returns Rewritten URL for Codex backend
263
+ */
264
+ export function rewriteUrlForCodex(url) {
265
+ const parsedUrl = new URL(url);
266
+ const rewrittenPath = parsedUrl.pathname.includes(URL_PATHS.RESPONSES)
267
+ ? parsedUrl.pathname.replace(URL_PATHS.RESPONSES, URL_PATHS.CODEX_RESPONSES)
268
+ : parsedUrl.pathname;
269
+ const normalizedPath = rewrittenPath === CODEX_BASE_PATH_PREFIX ||
270
+ rewrittenPath.startsWith(`${CODEX_BASE_PATH_PREFIX}/`)
271
+ ? rewrittenPath
272
+ : `${CODEX_BASE_PATH_PREFIX}${rewrittenPath.startsWith("/") ? rewrittenPath : `/${rewrittenPath}`}`;
273
+ parsedUrl.protocol = CODEX_BASE_URL_OBJECT.protocol;
274
+ parsedUrl.username = "";
275
+ parsedUrl.password = "";
276
+ parsedUrl.host = CODEX_BASE_URL_OBJECT.host;
277
+ parsedUrl.pathname = normalizedPath;
278
+ return parsedUrl.toString();
279
+ }
280
+ /**
281
+ * Transforms request body and logs the transformation
282
+ * Fetches model-specific Codex instructions based on the request model
283
+ *
284
+ * @param init - Request init options
285
+ * @param url - Request URL
286
+ * @param userConfig - User configuration
287
+ * @param codexMode - Enable CODEX_MODE (bridge prompt instead of tool remap)
288
+ * @param parsedBody - Pre-parsed body to avoid double JSON.parse (optional)
289
+ * @returns Transformed body and updated init, or undefined if no body
290
+ */
291
+ export async function transformRequestForCodex(init, url, userConfig, codexMode = true, parsedBody, options) {
292
+ const hasParsedBody = parsedBody !== undefined &&
293
+ parsedBody !== null &&
294
+ typeof parsedBody === "object" &&
295
+ Object.keys(parsedBody).length > 0;
296
+ if (!init?.body && !hasParsedBody)
297
+ return undefined;
298
+ try {
299
+ // Use pre-parsed body if provided, otherwise parse from init.body
300
+ let body;
301
+ if (hasParsedBody) {
302
+ body = parsedBody;
303
+ }
304
+ else {
305
+ if (typeof init?.body !== "string")
306
+ return undefined;
307
+ body = JSON.parse(init.body);
308
+ }
309
+ const originalModel = body.model;
310
+ // Normalize model first to determine which instructions to fetch
311
+ // This ensures we get the correct model-specific prompt
312
+ const normalizedModel = normalizeModel(originalModel);
313
+ const modelFamily = getModelFamily(normalizedModel);
314
+ // Log original request
315
+ logRequest(LOG_STAGES.BEFORE_TRANSFORM, {
316
+ url,
317
+ originalModel,
318
+ model: body.model,
319
+ hasTools: !!body.tools,
320
+ hasInput: !!body.input,
321
+ inputLength: body.input?.length,
322
+ codexMode,
323
+ body: body,
324
+ });
325
+ // Fetch model-specific Codex instructions (cached per model family)
326
+ const codexInstructions = await getCodexInstructions(normalizedModel);
327
+ // Transform request body
328
+ const transformedBody = await transformRequestBody(body, codexInstructions, userConfig, codexMode, options?.fastSession ?? false, options?.fastSessionStrategy ?? "hybrid", options?.fastSessionMaxInputItems ?? 30);
329
+ // Log transformed request
330
+ logRequest(LOG_STAGES.AFTER_TRANSFORM, {
331
+ url,
332
+ originalModel,
333
+ normalizedModel: transformedBody.model,
334
+ modelFamily,
335
+ hasTools: !!transformedBody.tools,
336
+ hasInput: !!transformedBody.input,
337
+ inputLength: transformedBody.input?.length,
338
+ reasoning: transformedBody.reasoning,
339
+ textVerbosity: transformedBody.text?.verbosity,
340
+ include: transformedBody.include,
341
+ body: transformedBody,
342
+ });
343
+ return {
344
+ body: transformedBody,
345
+ updatedInit: { ...(init ?? {}), body: JSON.stringify(transformedBody) },
346
+ };
347
+ }
348
+ catch (e) {
349
+ logError(`${ERROR_MESSAGES.REQUEST_PARSE_ERROR}`, e);
350
+ return undefined;
351
+ }
352
+ }
353
+ /**
354
+ * Creates headers for Codex API requests
355
+ * @param init - Request init options
356
+ * @param accountId - ChatGPT account ID
357
+ * @param accessToken - OAuth access token
358
+ * @returns Headers object with all required Codex headers
359
+ */
360
+ export function createCodexHeaders(init, accountId, accessToken, opts) {
361
+ const headers = new Headers(init?.headers ?? {});
362
+ headers.delete("x-api-key"); // Remove any existing API key
363
+ headers.set("Authorization", `Bearer ${accessToken}`);
364
+ headers.set(OPENAI_HEADERS.ACCOUNT_ID, accountId);
365
+ headers.set(OPENAI_HEADERS.BETA, OPENAI_HEADER_VALUES.BETA_RESPONSES);
366
+ headers.set(OPENAI_HEADERS.ORIGINATOR, OPENAI_HEADER_VALUES.ORIGINATOR_CODEX);
367
+ const cacheKey = opts?.promptCacheKey;
368
+ if (cacheKey) {
369
+ headers.set(OPENAI_HEADERS.CONVERSATION_ID, cacheKey);
370
+ headers.set(OPENAI_HEADERS.SESSION_ID, cacheKey);
371
+ }
372
+ else {
373
+ headers.delete(OPENAI_HEADERS.CONVERSATION_ID);
374
+ headers.delete(OPENAI_HEADERS.SESSION_ID);
375
+ }
376
+ headers.set("accept", "text/event-stream");
377
+ return headers;
378
+ }
379
+ /**
380
+ * Handles error responses from the Codex API
381
+ * @param response - Error response from API
382
+ * @returns Original response or mapped retryable response
383
+ */
384
+ export async function handleErrorResponse(response, options) {
385
+ const bodyText = await safeReadBody(response);
386
+ const mapped = mapUsageLimit404WithBody(response, bodyText);
387
+ // Entitlement errors return a ready-to-use Response with 403 status
388
+ if (mapped && mapped.status === HTTP_STATUS.FORBIDDEN) {
389
+ return { response: mapped, rateLimit: undefined, errorBody: undefined };
390
+ }
391
+ const finalResponse = mapped ?? response;
392
+ const rateLimit = extractRateLimitInfoFromBody(finalResponse, bodyText);
393
+ let errorBody;
394
+ try {
395
+ errorBody = bodyText ? JSON.parse(bodyText) : undefined;
396
+ }
397
+ catch {
398
+ errorBody = { message: bodyText };
399
+ }
400
+ const diagnostics = extractErrorDiagnostics(finalResponse, options);
401
+ const normalizedError = normalizeErrorPayload(errorBody, bodyText, finalResponse.statusText, finalResponse.status, diagnostics);
402
+ const errorResponse = ensureJsonErrorResponse(finalResponse, normalizedError);
403
+ if (finalResponse.status === HTTP_STATUS.UNAUTHORIZED) {
404
+ logWarn("Codex upstream returned 401 Unauthorized", diagnostics);
405
+ }
406
+ logRequest(LOG_STAGES.ERROR_RESPONSE, {
407
+ status: finalResponse.status,
408
+ statusText: finalResponse.statusText,
409
+ diagnostics,
410
+ });
411
+ return { response: errorResponse, rateLimit, errorBody: normalizedError };
412
+ }
413
+ /**
414
+ * Handles successful responses from the Codex API
415
+ * Converts SSE to JSON for non-streaming requests (generateText)
416
+ * Passes through SSE for streaming requests (streamText)
417
+ * @param response - Success response from API
418
+ * @param isStreaming - Whether this is a streaming request (stream=true in body)
419
+ * @returns Processed response (SSE→JSON for non-streaming, stream for streaming)
420
+ */
421
+ export async function handleSuccessResponse(response, isStreaming, options) {
422
+ // Check for deprecation headers (RFC 8594)
423
+ const deprecation = response.headers.get("Deprecation");
424
+ const sunset = response.headers.get("Sunset");
425
+ if (deprecation || sunset) {
426
+ logWarn(`API deprecation notice`, { deprecation, sunset });
427
+ }
428
+ const responseHeaders = ensureContentType(response.headers);
429
+ // For non-streaming requests (generateText), convert SSE to JSON
430
+ if (!isStreaming) {
431
+ return await convertSseToJson(response, responseHeaders, options);
432
+ }
433
+ // For streaming requests (streamText), return stream as-is
434
+ return new Response(response.body, {
435
+ status: response.status,
436
+ statusText: response.statusText,
437
+ headers: responseHeaders,
438
+ });
439
+ }
440
+ async function safeReadBody(response) {
441
+ try {
442
+ return await response.clone().text();
443
+ }
444
+ catch {
445
+ return "";
446
+ }
447
+ }
448
+ function mapUsageLimit404WithBody(response, bodyText) {
449
+ if (response.status !== HTTP_STATUS.NOT_FOUND)
450
+ return null;
451
+ if (!bodyText)
452
+ return null;
453
+ let code = "";
454
+ try {
455
+ const parsed = JSON.parse(bodyText);
456
+ code = (parsed?.error?.code ?? parsed?.error?.type ?? "").toString();
457
+ }
458
+ catch {
459
+ code = "";
460
+ }
461
+ // Check for entitlement errors first - these should NOT be treated as rate limits
462
+ if (isEntitlementError(code, bodyText)) {
463
+ return createEntitlementErrorResponse(bodyText);
464
+ }
465
+ const haystack = `${code} ${bodyText}`.toLowerCase();
466
+ if (!/usage_limit_reached|rate_limit_exceeded|usage limit/i.test(haystack)) {
467
+ return null;
468
+ }
469
+ const headers = new Headers(response.headers);
470
+ return new Response(bodyText, {
471
+ status: HTTP_STATUS.TOO_MANY_REQUESTS,
472
+ statusText: "Too Many Requests",
473
+ headers,
474
+ });
475
+ }
476
+ function extractRateLimitInfoFromBody(response, bodyText) {
477
+ const isStatusRateLimit = response.status === HTTP_STATUS.TOO_MANY_REQUESTS;
478
+ const parsed = parseRateLimitBody(bodyText);
479
+ const haystack = `${parsed?.code ?? ""} ${bodyText}`.toLowerCase();
480
+ // Entitlement errors should not be treated as rate limits
481
+ if (isEntitlementError(parsed?.code ?? "", bodyText)) {
482
+ return undefined;
483
+ }
484
+ const isRateLimit = isStatusRateLimit ||
485
+ /usage_limit_reached|rate_limit_exceeded|rate_limit|usage limit/i.test(haystack);
486
+ if (!isRateLimit)
487
+ return undefined;
488
+ const retryAfterMs = parseRetryAfterMs(response, parsed) ?? 60000;
489
+ return { retryAfterMs, code: parsed?.code };
490
+ }
491
+ function parseRateLimitBody(body) {
492
+ if (!body)
493
+ return undefined;
494
+ try {
495
+ const parsed = JSON.parse(body);
496
+ const error = parsed?.error ?? {};
497
+ const code = (error.code ?? error.type ?? "").toString();
498
+ const resetsAt = toNumber(error.resets_at ?? error.reset_at);
499
+ const retryAfterMs = toNumber(error.retry_after_ms ?? error.retry_after);
500
+ return { code, resetsAt, retryAfterMs };
501
+ }
502
+ catch {
503
+ return undefined;
504
+ }
505
+ }
506
+ /**
507
+ * Build a normalized ErrorPayload from a raw response body, status, and diagnostics.
508
+ *
509
+ * Produces a structured error object by preferring explicit error fields in `errorBody`, falling back to `bodyText`, `statusText`, or a generic message; special-cases Codex ChatGPT unsupported-model entitlement errors and appends diagnostic info when provided.
510
+ *
511
+ * @param errorBody - Parsed response body, if available; may be any JSON-derived value.
512
+ * @param bodyText - Raw response text used as a fallback message when structured fields are absent.
513
+ * @param statusText - HTTP status text used as a final fallback for the error message.
514
+ * @param status - HTTP status code; when 401 adds a short hint to run `codex login`.
515
+ * @param diagnostics - Optional diagnostic metadata (request IDs, correlation/thread IDs); fields may be redacted for tokens and sensitive values.
516
+ * @returns The normalized ErrorPayload with an `error.message` and optional `type`, `code`, `unsupported_model`, and `diagnostics` fields.
517
+ *
518
+ * Concurrency: pure and safe to call concurrently from multiple threads/tasks.
519
+ * Filesystem: performs no filesystem I/O and has no Windows-specific behavior.
520
+ * Token redaction: callers should assume diagnostic fields may be redacted to avoid leaking credentials.
521
+ */
522
+ function normalizeErrorPayload(errorBody, bodyText, statusText, status, diagnostics) {
523
+ if (isUnsupportedCodexModelForChatGpt(status, bodyText)) {
524
+ const unsupportedModel = extractUnsupportedCodexModelFromText(bodyText) ?? "requested model";
525
+ const payload = {
526
+ error: {
527
+ message: `The model '${unsupportedModel}' is not currently available for this ChatGPT account when using Codex OAuth. ` +
528
+ "This is an account/workspace entitlement gate, not a temporary rate limit. " +
529
+ "Try 'gpt-5-codex' (canonical), or legacy aliases like 'gpt-5.3-codex'/'gpt-5.2-codex', or enable automatic fallback via " +
530
+ 'unsupportedCodexPolicy: "fallback" (or CODEX_AUTH_UNSUPPORTED_MODEL_POLICY=fallback). ' +
531
+ "(Legacy: CODEX_AUTH_FALLBACK_UNSUPPORTED_MODEL=1 or fallbackOnUnsupportedCodexModel).",
532
+ type: "entitlement_error",
533
+ code: CHATGPT_CODEX_UNSUPPORTED_MODEL_CODE,
534
+ unsupported_model: unsupportedModel,
535
+ },
536
+ };
537
+ if (diagnostics && Object.keys(diagnostics).length > 0) {
538
+ payload.error.diagnostics = diagnostics;
539
+ }
540
+ return payload;
541
+ }
542
+ if (isRecord(errorBody)) {
543
+ const maybeError = errorBody.error;
544
+ if (isRecord(maybeError) && typeof maybeError.message === "string") {
545
+ const payload = {
546
+ error: {
547
+ message: maybeError.message,
548
+ },
549
+ };
550
+ if (typeof maybeError.type === "string") {
551
+ payload.error.type = maybeError.type;
552
+ }
553
+ if (typeof maybeError.code === "string" || typeof maybeError.code === "number") {
554
+ payload.error.code = maybeError.code;
555
+ }
556
+ if (diagnostics && Object.keys(diagnostics).length > 0) {
557
+ payload.error.diagnostics = diagnostics;
558
+ }
559
+ if (status === HTTP_STATUS.UNAUTHORIZED) {
560
+ payload.error.message = `${payload.error.message} (run \`codex login\` if this persists)`;
561
+ }
562
+ return payload;
563
+ }
564
+ if (typeof errorBody.message === "string") {
565
+ const payload = { error: { message: errorBody.message } };
566
+ if (diagnostics && Object.keys(diagnostics).length > 0) {
567
+ payload.error.diagnostics = diagnostics;
568
+ }
569
+ if (status === HTTP_STATUS.UNAUTHORIZED) {
570
+ payload.error.message = `${payload.error.message} (run \`codex login\` if this persists)`;
571
+ }
572
+ return payload;
573
+ }
574
+ }
575
+ const trimmed = bodyText.trim();
576
+ if (trimmed) {
577
+ const payload = { error: { message: trimmed } };
578
+ if (diagnostics && Object.keys(diagnostics).length > 0) {
579
+ payload.error.diagnostics = diagnostics;
580
+ }
581
+ if (status === HTTP_STATUS.UNAUTHORIZED) {
582
+ payload.error.message = `${payload.error.message} (run \`codex login\` if this persists)`;
583
+ }
584
+ return payload;
585
+ }
586
+ if (statusText) {
587
+ const payload = { error: { message: statusText } };
588
+ if (diagnostics && Object.keys(diagnostics).length > 0) {
589
+ payload.error.diagnostics = diagnostics;
590
+ }
591
+ if (status === HTTP_STATUS.UNAUTHORIZED) {
592
+ payload.error.message = `${payload.error.message} (run \`codex login\` if this persists)`;
593
+ }
594
+ return payload;
595
+ }
596
+ const payload = { error: { message: "Request failed" } };
597
+ if (diagnostics && Object.keys(diagnostics).length > 0) {
598
+ payload.error.diagnostics = diagnostics;
599
+ }
600
+ if (status === HTTP_STATUS.UNAUTHORIZED) {
601
+ payload.error.message = `${payload.error.message} (run \`codex login\` if this persists)`;
602
+ }
603
+ return payload;
604
+ }
605
+ function ensureJsonErrorResponse(response, payload) {
606
+ const headers = new Headers(response.headers);
607
+ headers.set("content-type", "application/json; charset=utf-8");
608
+ return new Response(JSON.stringify(payload), {
609
+ status: response.status,
610
+ statusText: response.statusText,
611
+ headers,
612
+ });
613
+ }
614
+ function parseRetryAfterMs(response, parsedBody) {
615
+ if (parsedBody?.retryAfterMs !== undefined) {
616
+ return normalizeRetryAfter(parsedBody.retryAfterMs);
617
+ }
618
+ const retryAfterMsHeader = response.headers.get("retry-after-ms");
619
+ if (retryAfterMsHeader) {
620
+ const parsed = Number.parseInt(retryAfterMsHeader, 10);
621
+ if (!Number.isNaN(parsed) && parsed > 0) {
622
+ return parsed;
623
+ }
624
+ }
625
+ const retryAfterHeader = response.headers.get("retry-after");
626
+ if (retryAfterHeader) {
627
+ const parsed = Number.parseInt(retryAfterHeader, 10);
628
+ if (!Number.isNaN(parsed) && parsed > 0) {
629
+ return parsed * 1000;
630
+ }
631
+ }
632
+ const resetAtHeaders = [
633
+ "x-codex-primary-reset-at",
634
+ "x-codex-secondary-reset-at",
635
+ "x-ratelimit-reset",
636
+ ];
637
+ const now = Date.now();
638
+ const resetCandidates = [];
639
+ for (const header of resetAtHeaders) {
640
+ const value = response.headers.get(header);
641
+ if (!value)
642
+ continue;
643
+ const parsed = Number.parseInt(value, 10);
644
+ if (!Number.isNaN(parsed) && parsed > 0) {
645
+ const timestamp = parsed < 10_000_000_000 ? parsed * 1000 : parsed;
646
+ const delta = timestamp - now;
647
+ if (delta > 0)
648
+ resetCandidates.push(delta);
649
+ }
650
+ }
651
+ if (parsedBody?.resetsAt) {
652
+ const timestamp = parsedBody.resetsAt < 10_000_000_000
653
+ ? parsedBody.resetsAt * 1000
654
+ : parsedBody.resetsAt;
655
+ const delta = timestamp - now;
656
+ if (delta > 0)
657
+ resetCandidates.push(delta);
658
+ }
659
+ if (resetCandidates.length > 0) {
660
+ return Math.min(...resetCandidates);
661
+ }
662
+ return null;
663
+ }
664
+ function normalizeRetryAfter(value) {
665
+ if (!Number.isFinite(value))
666
+ return 60000;
667
+ let ms;
668
+ if (value > 0 && value < 1000) {
669
+ ms = Math.floor(value * 1000);
670
+ }
671
+ else {
672
+ ms = Math.floor(value);
673
+ }
674
+ const MAX_RETRY_DELAY_MS = 5 * 60 * 1000;
675
+ return Math.min(ms, MAX_RETRY_DELAY_MS);
676
+ }
677
+ function toNumber(value) {
678
+ if (value === null || value === undefined)
679
+ return undefined;
680
+ const parsed = Number(value);
681
+ return Number.isFinite(parsed) ? parsed : undefined;
682
+ }
683
+ function extractErrorDiagnostics(response, options) {
684
+ const requestId = response.headers.get("x-request-id") ??
685
+ response.headers.get("request-id") ??
686
+ response.headers.get("openai-request-id") ??
687
+ response.headers.get("x-openai-request-id") ??
688
+ undefined;
689
+ const cfRay = response.headers.get("cf-ray") ?? undefined;
690
+ const diagnostics = {
691
+ httpStatus: response.status,
692
+ requestId,
693
+ cfRay,
694
+ correlationId: options?.requestCorrelationId,
695
+ threadId: options?.threadId,
696
+ };
697
+ for (const [key, value] of Object.entries(diagnostics)) {
698
+ if (value === undefined || value === "") {
699
+ delete diagnostics[key];
700
+ }
701
+ }
702
+ return Object.keys(diagnostics).length > 0 ? diagnostics : undefined;
703
+ }
704
+ //# sourceMappingURL=fetch-helpers.js.map