@wengine-ai/ccr-provider-codex 1.0.2 → 1.0.5

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.
package/dist/plugin.js CHANGED
@@ -25,7 +25,7 @@ var CODEX_ORIGINATOR = "codex_cli_rs";
25
25
  var CODEX_CALLBACK_PORTS = [1455, 1457];
26
26
  var CODEX_LOOPBACK_HOSTS = ["127.0.0.1", "localhost", "::1"];
27
27
  var CODEX_CALLBACK_PATH = "/auth/callback";
28
- var CODEX_LOGIN_TTL_MS = 10 * 60 * 1e3;
28
+ var CODEX_LOGIN_TTL_MS = 30 * 60 * 1e3;
29
29
  var CODEX_REFRESH_LEAD_MS = 5 * 60 * 1e3;
30
30
  var CODEX_FETCH_TIMEOUT_MS = 3e4;
31
31
  function codexClientId() {
@@ -49,7 +49,10 @@ function codexManagementHeaders(accessToken, accountId) {
49
49
  originator: "codex_cli_rs",
50
50
  version: codexClientVersion(),
51
51
  "User-Agent": codexUserAgent(),
52
- Accept: "application/json"
52
+ Accept: "application/json",
53
+ // Same rationale as the token exchange: a compressed body without
54
+ // content-encoding hands undecodable bytes to text(); identity is plain.
55
+ "Accept-Encoding": "identity"
53
56
  };
54
57
  }
55
58
  function randomToken(n) {
@@ -185,7 +188,13 @@ function buildCodexAuthorizeUrl(params) {
185
188
  async function exchangeCodexCode(params) {
186
189
  const res = await params.fetchImpl(`${params.issuer ?? codexIssuer()}${CODEX_TOKEN_PATH}`, boundedFetchInit({
187
190
  method: "POST",
188
- headers: { "Content-Type": "application/x-www-form-urlencoded" },
191
+ headers: {
192
+ "Content-Type": "application/x-www-form-urlencoded",
193
+ // Some paths (middleboxes on the TUN/direct route) answer with a binary
194
+ // compressed body missing content-type/content-encoding, which fetch then
195
+ // hands through undecoded. identity = plain JSON, always parseable.
196
+ "Accept-Encoding": "identity"
197
+ },
189
198
  body: new URLSearchParams({
190
199
  grant_type: "authorization_code",
191
200
  code: params.code,
@@ -228,7 +237,7 @@ async function readTokenResponse(res, stage) {
228
237
  const reauth = !res.ok && (res.status === 400 || res.status === 401 || /invalid_grant|refresh_token_(reused|invalidated|expired)|token_revoked|invalidated/i.test(String(errType)));
229
238
  throw codexError({
230
239
  code: reauth ? "codex_reauth_required" : "codex_upstream_error",
231
- message: reauth ? CODEX_SAFE_MESSAGES.codex_reauth_required : `Codex OAuth ${stage} failed.`,
240
+ message: reauth ? CODEX_SAFE_MESSAGES.codex_reauth_required : `Codex OAuth ${stage} failed (HTTP ${res.status}, ct=${res.headers.get("content-type") ?? "?"}, body=${text.slice(0, 160)}).`,
232
241
  httpStatus: reauth ? 401 : 502
233
242
  });
234
243
  }
@@ -306,7 +315,7 @@ async function fetchCodexModels(params) {
306
315
  const classified = classifyCodexError(res.status, json || text);
307
316
  throw codexError({
308
317
  code: classified?.code === "codex_reauth_required" ? "codex_reauth_required" : "codex_upstream_error",
309
- message: classified?.message ?? "Codex model list could not be loaded.",
318
+ message: classified?.message ?? `Codex model list could not be loaded (HTTP ${res.status}, ct=${res.headers.get("content-type") ?? "?"}, body=${text.slice(0, 120)}).`,
310
319
  httpStatus: classified?.httpStatus ?? 502
311
320
  });
312
321
  }
@@ -397,6 +406,20 @@ function isCodexRuntime(p) {
397
406
  }
398
407
  return p?.name === CODEX_PROVIDER_NAME && (p?.apiKey === CODEX_API_KEY || p?.api_key === CODEX_API_KEY);
399
408
  }
409
+ function responseHasOutput(response) {
410
+ const output = response?.output;
411
+ if (Array.isArray(output) && output.length > 0) return true;
412
+ const usage = response?.usage;
413
+ return typeof usage?.output_tokens === "number" && usage.output_tokens > 0;
414
+ }
415
+ function codexEmptyAnswerError() {
416
+ return codexError({
417
+ code: "codex_upstream_error",
418
+ message: "Codex returned an empty answer (completed with no output).",
419
+ httpStatus: 502,
420
+ retryable: true
421
+ });
422
+ }
400
423
 
401
424
  // src/auth-service.ts
402
425
  var CODEX_CREDENTIAL_FILE = "codex-credentials.json";
@@ -1041,7 +1064,8 @@ var CodexAuthService = class {
1041
1064
  fetchImpl: (url, init) => this.directFetch(url, init)
1042
1065
  });
1043
1066
  } catch (error) {
1044
- this.log.warn(`[codex] token exchange failed: ${error?.code === "codex_reauth_required" ? "rejected" : "upstream"}`);
1067
+ const detail = error?.statusCode !== void 0 ? `status=${error.statusCode} ${String(error?.message ?? "").slice(0, 120)}` : `transport=${error?.cause?.code ?? error?.code ?? "?"} ${String(error?.message ?? "").slice(0, 120)}`;
1068
+ this.log.warn(`[codex] token exchange failed: ${error?.code === "codex_reauth_required" ? "rejected" : "upstream"} (${detail})`);
1045
1069
  return fail("codex_login_exchange_failed");
1046
1070
  }
1047
1071
  if (this.epoch !== epoch || job.result && job.result.status !== "connected") {
@@ -1057,7 +1081,8 @@ var CodexAuthService = class {
1057
1081
  fetchImpl: (url, init) => this.directFetch(url, init)
1058
1082
  });
1059
1083
  } catch (error) {
1060
- this.log.warn(`[codex] model discovery after login failed: ${error?.code ?? "upstream"}`);
1084
+ const cause = error?.cause?.code ?? error?.cause?.message ?? "";
1085
+ this.log.warn(`[codex] model discovery after login failed: ${error?.code ?? error?.name ?? "upstream"} ${cause} ${String(error?.message ?? "").slice(0, 120)}`);
1061
1086
  }
1062
1087
  const previous = await this.store.load().catch(() => null);
1063
1088
  const data = {
@@ -1736,6 +1761,7 @@ var CodexTransformer = class {
1736
1761
  let committed = false;
1737
1762
  let finished = false;
1738
1763
  let rateLimitsSeen = false;
1764
+ let sawOutput = false;
1739
1765
  const handleEvent = async (data) => {
1740
1766
  if (data === "[DONE]") return true;
1741
1767
  let event;
@@ -1774,7 +1800,19 @@ var CodexTransformer = class {
1774
1800
  }
1775
1801
  throw codexClassifiedStreamError(event, response.status);
1776
1802
  }
1777
- return typeof event?.type === "string" && (event.type.startsWith("response.output_text") || event.type.startsWith("response.function_call_arguments") || event.type.startsWith("response.reasoning") || event.type === "response.output_item.done" || event.type === "response.completed" || event.type === "response.incomplete");
1803
+ if (typeof event?.type === "string" && (event.type.startsWith("response.output_text") || event.type.startsWith("response.function_call_arguments") || event.type.startsWith("response.reasoning") || event.type === "response.output_item.done")) {
1804
+ sawOutput = true;
1805
+ return true;
1806
+ }
1807
+ if (event.type === "response.completed") {
1808
+ if (sawOutput || responseHasOutput(event.response)) {
1809
+ sawOutput = true;
1810
+ return true;
1811
+ }
1812
+ throw codexEmptyAnswerError();
1813
+ }
1814
+ if (event.type === "response.incomplete") return true;
1815
+ return false;
1778
1816
  };
1779
1817
  const stream = new ReadableStream({
1780
1818
  async pull(controller) {
@@ -1897,6 +1935,35 @@ function createCodexManagedRuntime(options) {
1897
1935
  });
1898
1936
  }
1899
1937
  },
1938
+ /**
1939
+ * Level 1 of the background recovery probe: report the parked window so the
1940
+ * core skips the network call for an account that cannot answer yet.
1941
+ */
1942
+ probeAccountState: async () => {
1943
+ if (!await service.isAccountCooling()) return { available: true };
1944
+ let parkedUntil;
1945
+ try {
1946
+ const status = await service.getStatus?.();
1947
+ const candidate = status?.cooldownUntil ?? status?.rateLimitCooldownUntil ?? status?.resetsAt;
1948
+ if (typeof candidate === "number" && candidate > Date.now()) parkedUntil = candidate;
1949
+ } catch {
1950
+ }
1951
+ return { available: false, reason: "account_cooling", parkedUntil };
1952
+ },
1953
+ /**
1954
+ * Level 2: prove the account is alive AND reachable. `getUsage()` is the
1955
+ * cheapest authenticated call that exercises the same token path as a chat, so
1956
+ * a restored model is one the next real turn can actually use. A null answer
1957
+ * means "could not confirm", which must keep the circuit open rather than
1958
+ * restore a model on a guess.
1959
+ */
1960
+ probeLiveness: async () => {
1961
+ const usage = await service.getUsage?.();
1962
+ return new Response(JSON.stringify(usage ?? null), {
1963
+ status: usage ? 200 : 503,
1964
+ headers: { "content-type": "application/json" }
1965
+ });
1966
+ },
1900
1967
  // Pin the upstream: a config-supplied base_url can never win, a redirect
1901
1968
  // away from the fixed origin is an error, and the fixed origin ignores
1902
1969
  // CCR/proxy environment routing.
@@ -2309,7 +2376,7 @@ var codexLocales = {
2309
2376
  // src/plugin.ts
2310
2377
  var manifest = {
2311
2378
  name: "codex",
2312
- version: "1.0.2",
2379
+ version: "1.0.5",
2313
2380
  coreApi: "1",
2314
2381
  description: "Codex subscription managed provider: chatgpt.com backend-api/codex data plane, browser OAuth login (PKCE, loopback 1455/1457 + paste), model sync, usage and admin routes."
2315
2382
  };
package/dist/plugin.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codex",
3
- "version": "1.0.2",
3
+ "version": "1.0.5",
4
4
  "coreApi": "1",
5
5
  "description": "Codex subscription managed provider: chatgpt.com backend-api/codex data plane, browser OAuth login (PKCE, loopback 1455/1457 + paste), model sync, usage and admin routes."
6
6
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wengine-ai/ccr-provider-codex",
3
- "version": "1.0.2",
3
+ "version": "1.0.5",
4
4
  "description": "Codex subscription provider plugin for claude-code-router: browser OAuth login, fixed-origin responses data plane, transformer, managed runtime, admin routes, config protector and UI card.",
5
5
  "type": "module",
6
6
  "main": "dist/plugin.js",