@wengine-ai/ccr-provider-codex 1.0.15 → 1.0.17
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/card.js +2 -0
- package/dist/plugin.js +282 -106
- package/dist/plugin.json +1 -1
- package/package.json +1 -1
package/dist/card.js
CHANGED
|
@@ -367,6 +367,7 @@ var codexLocales = {
|
|
|
367
367
|
codex_request_rejected: "Codex rejected the request.",
|
|
368
368
|
codex_upstream_error: "Codex upstream request failed.",
|
|
369
369
|
reset_failed: "The usage reset failed. Please try again.",
|
|
370
|
+
codex_reset_account_changed: "The connected account changed. Check the available resets again.",
|
|
370
371
|
codex_reset_busy: "A usage reset is already running \u2014 wait for it to finish.",
|
|
371
372
|
codex_reset_no_credit: "No usage resets are available.",
|
|
372
373
|
codex_reset_unavailable: "The reset service is unavailable right now. Please try again.",
|
|
@@ -474,6 +475,7 @@ var codexLocales = {
|
|
|
474
475
|
codex_request_rejected: "Codex \u62D2\u7EDD\u4E86\u8BE5\u8BF7\u6C42\u3002",
|
|
475
476
|
codex_upstream_error: "Codex \u4E0A\u6E38\u8BF7\u6C42\u5931\u8D25\u3002",
|
|
476
477
|
reset_failed: "\u7528\u91CF\u91CD\u7F6E\u5931\u8D25\uFF0C\u8BF7\u91CD\u8BD5\u3002",
|
|
478
|
+
codex_reset_account_changed: "\u5DF2\u5207\u6362\u8FDE\u63A5\u8D26\u6237\uFF0C\u8BF7\u91CD\u65B0\u67E5\u8BE2\u53EF\u7528\u91CD\u7F6E\u5361\u3002",
|
|
477
479
|
codex_reset_busy: "\u5DF2\u6709\u7528\u91CF\u91CD\u7F6E\u6B63\u5728\u8FDB\u884C\u2014\u2014\u8BF7\u7B49\u5F85\u5B8C\u6210\u3002",
|
|
478
480
|
codex_reset_no_credit: "\u6CA1\u6709\u53EF\u7528\u7684\u7528\u91CF\u91CD\u7F6E\u673A\u4F1A\u3002",
|
|
479
481
|
codex_reset_unavailable: "\u91CD\u7F6E\u670D\u52A1\u6682\u65F6\u4E0D\u53EF\u7528\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5\u3002",
|
package/dist/plugin.js
CHANGED
|
@@ -405,25 +405,32 @@ function rateLimitsSaturated(limits, now) {
|
|
|
405
405
|
}
|
|
406
406
|
return false;
|
|
407
407
|
}
|
|
408
|
+
function isCodexResetCreditId(value) {
|
|
409
|
+
return typeof value === "string" && value.length <= 256 && !!value.trim() && !/[\x00-\x1f\x7f-\x9f]/.test(value);
|
|
410
|
+
}
|
|
411
|
+
function isCodexAccountRevision(value) {
|
|
412
|
+
return typeof value === "string" && /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(value);
|
|
413
|
+
}
|
|
408
414
|
var RESET_CREDIT_TITLES = {
|
|
409
415
|
codex_rate_limits: "Full reset"
|
|
410
416
|
};
|
|
411
|
-
function parseResetCredit(raw) {
|
|
417
|
+
function parseResetCredit(raw, confirmedCreditId) {
|
|
412
418
|
if (!raw || typeof raw !== "object") return null;
|
|
413
|
-
if (
|
|
419
|
+
if (!isCodexResetCreditId(raw.id)) return null;
|
|
414
420
|
if (typeof raw.status !== "string" || !raw.status || raw.status.length > 32) return null;
|
|
415
|
-
const expires = typeof raw.expires_at === "string"
|
|
421
|
+
const expires = typeof raw.expires_at === "string" ? Date.parse(raw.expires_at) : NaN;
|
|
422
|
+
if (raw.id === confirmedCreditId && raw.expires_at != null && !Number.isFinite(expires)) return null;
|
|
416
423
|
const title = typeof raw.title === "string" && raw.title.trim() ? raw.title.trim().slice(0, 200) : RESET_CREDIT_TITLES[typeof raw.reset_type === "string" ? raw.reset_type : ""];
|
|
417
424
|
const description = typeof raw.description === "string" && raw.description.trim() ? raw.description.trim().slice(0, 300) : void 0;
|
|
418
425
|
return {
|
|
419
426
|
id: raw.id,
|
|
420
427
|
status: raw.status,
|
|
421
|
-
...Number.isFinite(expires)
|
|
428
|
+
...Number.isFinite(expires) ? { expiresAt: expires } : {},
|
|
422
429
|
...title ? { title } : {},
|
|
423
430
|
...description ? { description } : {}
|
|
424
431
|
};
|
|
425
432
|
}
|
|
426
|
-
function parseCodexResetCredits(raw) {
|
|
433
|
+
function parseCodexResetCredits(raw, confirmedCreditId) {
|
|
427
434
|
if (!raw || typeof raw !== "object") return null;
|
|
428
435
|
const r = raw;
|
|
429
436
|
const availableCount = typeof r.available_count === "number" && Number.isFinite(r.available_count) ? Math.max(0, Math.floor(r.available_count)) : null;
|
|
@@ -432,7 +439,7 @@ function parseCodexResetCredits(raw) {
|
|
|
432
439
|
if (Array.isArray(r.credits)) {
|
|
433
440
|
if (r.credits.length > 256) return null;
|
|
434
441
|
for (const item of r.credits) {
|
|
435
|
-
const credit = parseResetCredit(item);
|
|
442
|
+
const credit = parseResetCredit(item, confirmedCreditId);
|
|
436
443
|
if (!credit) return null;
|
|
437
444
|
credits.push(credit);
|
|
438
445
|
}
|
|
@@ -481,7 +488,7 @@ async function fetchCodexResetCredits(params) {
|
|
|
481
488
|
fetchImpl: params.fetchImpl
|
|
482
489
|
});
|
|
483
490
|
if (!res.ok) return null;
|
|
484
|
-
return parseCodexResetCredits(res.json);
|
|
491
|
+
return parseCodexResetCredits(res.json, params.confirmedCreditId);
|
|
485
492
|
}
|
|
486
493
|
async function consumeCodexResetCredit(params) {
|
|
487
494
|
const origin = params.origin ?? CODEX_CHATGPT_ORIGIN;
|
|
@@ -584,6 +591,9 @@ var LAST_ERROR_CODES = /* @__PURE__ */ new Set([
|
|
|
584
591
|
function boundedString(value, max, min = 1) {
|
|
585
592
|
return typeof value === "string" && value.length >= min && value.length <= max && !/[\x00-\x1f\x7f]/.test(value);
|
|
586
593
|
}
|
|
594
|
+
function sameCodexAccount(a, b) {
|
|
595
|
+
return a?.accountId === b?.accountId && a?.connectedAt === b?.connectedAt;
|
|
596
|
+
}
|
|
587
597
|
function normalizeRateLimits(raw) {
|
|
588
598
|
if (!raw || typeof raw !== "object") return null;
|
|
589
599
|
const r = raw;
|
|
@@ -750,20 +760,16 @@ var CodexAuthService = class {
|
|
|
750
760
|
refreshFlight = null;
|
|
751
761
|
forceRefreshRequested = false;
|
|
752
762
|
usageCache = null;
|
|
753
|
-
/** Reject quota reads overtaken by a
|
|
763
|
+
/** Reject quota reads overtaken by a reset or a data-plane observation. */
|
|
754
764
|
quotaRevision = 0;
|
|
755
765
|
usageFlight = null;
|
|
756
|
-
/** Reset-credit listing cache (same short-TTL shape as usage). */
|
|
757
766
|
resetCache = null;
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
resetFlight =
|
|
762
|
-
/**
|
|
763
|
-
|
|
764
|
-
* terminal outcome (upstream dedupes per key — a lost response therefore
|
|
765
|
-
* cannot double-spend). Cleared on terminal outcomes and disconnect. */
|
|
766
|
-
pendingReset = null;
|
|
767
|
+
resetRevision = 0;
|
|
768
|
+
accountRevision = randomUUID();
|
|
769
|
+
/** Reject concurrent reset confirmations instead of queueing a second spend. */
|
|
770
|
+
resetFlight = false;
|
|
771
|
+
/** Unsettled redeem keys for the current account, indexed by selected credit. */
|
|
772
|
+
pendingReset = /* @__PURE__ */ new Map();
|
|
767
773
|
/** Locally marked spent credits: written only on a CONFIRMED terminal
|
|
768
774
|
* outcome; prevents re-consumption of a known-spent credit within this
|
|
769
775
|
* service lifetime. Upstream stays authoritative. */
|
|
@@ -811,12 +817,31 @@ var CodexAuthService = class {
|
|
|
811
817
|
// ---------------------------------------------------------------- loading --
|
|
812
818
|
async ensureLoaded(force = false) {
|
|
813
819
|
if (this.loaded && !force) return this.data;
|
|
814
|
-
const
|
|
815
|
-
this.
|
|
816
|
-
|
|
820
|
+
const previous = this.data;
|
|
821
|
+
const epoch = this.epoch;
|
|
822
|
+
let data;
|
|
823
|
+
try {
|
|
824
|
+
data = await this.store.load();
|
|
825
|
+
} catch {
|
|
826
|
+
if (this.data === previous && this.epoch === epoch) this.loaded = false;
|
|
827
|
+
return null;
|
|
828
|
+
}
|
|
829
|
+
if (this.data === previous && this.epoch === epoch) {
|
|
830
|
+
this.applyLoaded(data);
|
|
831
|
+
this.loaded = true;
|
|
832
|
+
} else if (this.epoch === epoch && !sameCodexAccount(data?.account, this.data?.account)) {
|
|
833
|
+
return this.ensureLoaded(true);
|
|
834
|
+
}
|
|
817
835
|
return this.data;
|
|
818
836
|
}
|
|
819
837
|
applyLoaded(data) {
|
|
838
|
+
if (!sameCodexAccount(this.data?.account, data?.account)) {
|
|
839
|
+
this.accountRevision = randomUUID();
|
|
840
|
+
if (this.data?.account) this.invalidateUsage();
|
|
841
|
+
this.invalidateResetCredits();
|
|
842
|
+
this.pendingReset.clear();
|
|
843
|
+
this.spentResetCredits.clear();
|
|
844
|
+
}
|
|
820
845
|
this.data = data;
|
|
821
846
|
this.instructions.clear();
|
|
822
847
|
for (const m of data?.models ?? []) {
|
|
@@ -825,9 +850,7 @@ var CodexAuthService = class {
|
|
|
825
850
|
}
|
|
826
851
|
async saveData(data) {
|
|
827
852
|
await this.store.save(data);
|
|
828
|
-
this.data
|
|
829
|
-
this.instructions.clear();
|
|
830
|
-
for (const m of data.models) if (m.instructions) this.instructions.set(m.slug, m.instructions);
|
|
853
|
+
this.applyLoaded(data);
|
|
831
854
|
}
|
|
832
855
|
// ------------------------------------------------------------------ status -
|
|
833
856
|
async getStatus() {
|
|
@@ -1399,10 +1422,6 @@ var CodexAuthService = class {
|
|
|
1399
1422
|
}
|
|
1400
1423
|
this.refreshFlight = null;
|
|
1401
1424
|
this.forceRefreshRequested = false;
|
|
1402
|
-
this.usageCache = null;
|
|
1403
|
-
this.resetCache = null;
|
|
1404
|
-
this.spentResetCredits.clear();
|
|
1405
|
-
this.pendingReset = null;
|
|
1406
1425
|
const result = { status: "connected", models: registered, modelCount: registered.length };
|
|
1407
1426
|
job.result = result;
|
|
1408
1427
|
this.lastResult.set(job.loginId, result);
|
|
@@ -1576,11 +1595,16 @@ var CodexAuthService = class {
|
|
|
1576
1595
|
if (this.usageFlight === flight) this.usageFlight = null;
|
|
1577
1596
|
}
|
|
1578
1597
|
}
|
|
1598
|
+
invalidateUsage() {
|
|
1599
|
+
this.quotaRevision++;
|
|
1600
|
+
this.usageCache = null;
|
|
1601
|
+
this.usageFlight = null;
|
|
1602
|
+
}
|
|
1579
1603
|
async readUsageAndReconcile() {
|
|
1580
|
-
const revision = this.quotaRevision;
|
|
1581
1604
|
const epoch = this.epoch;
|
|
1582
1605
|
const data = await this.ensureLoaded();
|
|
1583
|
-
if (!data?.account) return null;
|
|
1606
|
+
if (!data?.account || this.epoch !== epoch || !sameCodexAccount(data.account, this.data?.account)) return null;
|
|
1607
|
+
const revision = this.quotaRevision;
|
|
1584
1608
|
if (this.usageCache && this.now - this.usageCache.at < USAGE_CACHE_MS) {
|
|
1585
1609
|
return this.usageCache.value;
|
|
1586
1610
|
}
|
|
@@ -1625,85 +1649,136 @@ var CodexAuthService = class {
|
|
|
1625
1649
|
return usage;
|
|
1626
1650
|
}
|
|
1627
1651
|
// ------------------------------------------------------ reset credits -
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1652
|
+
invalidateResetCredits() {
|
|
1653
|
+
this.resetRevision++;
|
|
1654
|
+
this.resetCache = null;
|
|
1655
|
+
}
|
|
1656
|
+
fetchResetCredits(account, confirmedCreditId) {
|
|
1657
|
+
return fetchCodexResetCredits({
|
|
1658
|
+
confirmedCreditId,
|
|
1659
|
+
accessToken: account.accessToken,
|
|
1660
|
+
accountId: account.accountId || void 0,
|
|
1661
|
+
fetchImpl: (url, init) => this.directFetch(url, init)
|
|
1662
|
+
}).catch(() => null);
|
|
1663
|
+
}
|
|
1664
|
+
cacheResetListing(listing) {
|
|
1665
|
+
const value = listing && {
|
|
1666
|
+
...listing,
|
|
1667
|
+
// Desktop displays positive epoch milliseconds. Historical expiries cannot be redeemed.
|
|
1668
|
+
credits: listing.credits.filter((c) => !this.spentResetCredits.has(c.id) && (c.expiresAt === void 0 || c.expiresAt > 0)),
|
|
1669
|
+
accountRevision: this.accountRevision
|
|
1670
|
+
};
|
|
1671
|
+
this.resetCache = { at: this.now, value };
|
|
1672
|
+
return value;
|
|
1673
|
+
}
|
|
1674
|
+
/** Available gifted resets, cached briefly for display. */
|
|
1633
1675
|
async getResetCredits() {
|
|
1634
|
-
const
|
|
1635
|
-
|
|
1676
|
+
const epoch = this.epoch;
|
|
1677
|
+
const data = await this.ensureLoaded(true);
|
|
1678
|
+
if (!data?.account || this.epoch !== epoch) return null;
|
|
1679
|
+
const revision = this.resetRevision;
|
|
1680
|
+
const isCurrent = () => this.epoch === epoch && this.resetRevision === revision && sameCodexAccount(data.account, this.data?.account);
|
|
1636
1681
|
if (this.resetCache && this.now - this.resetCache.at < USAGE_CACHE_MS) {
|
|
1637
1682
|
return this.resetCache.value;
|
|
1638
1683
|
}
|
|
1639
1684
|
const accessToken = await this.getAccessToken().catch(() => null);
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
const listing = await
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
fetchImpl: (url, init) => this.directFetch(url, init)
|
|
1646
|
-
}).catch(() => null);
|
|
1647
|
-
const value = listing && listing.credits.length ? { ...listing, credits: listing.credits.filter((c) => !this.spentResetCredits.has(c.id)) } : listing;
|
|
1648
|
-
this.resetCache = { at: this.now, value };
|
|
1649
|
-
return value;
|
|
1685
|
+
const account = this.data?.account;
|
|
1686
|
+
if (!accessToken || !account || account.accessToken !== accessToken || !isCurrent()) return null;
|
|
1687
|
+
const listing = await this.fetchResetCredits(account);
|
|
1688
|
+
if (!isCurrent()) return null;
|
|
1689
|
+
return this.cacheResetListing(listing);
|
|
1650
1690
|
}
|
|
1651
|
-
/**
|
|
1652
|
-
* Consume ONE gifted reset credit. Explicit user action only — never called
|
|
1653
|
-
* by mount/poll/refresh. Single-flight: a second concurrent consume is
|
|
1654
|
-
* rejected (two queued consumes could redeem two credits). The idempotency
|
|
1655
|
-
* key is minted here at confirmation time and retained across user-initiated
|
|
1656
|
-
* retries of the same confirmation (upstream dedupes on it), so a lost
|
|
1657
|
-
* response cannot double-spend. Outcomes map to the upstream taxonomy;
|
|
1658
|
-
* `reset`/`already_redeemed` clear the usage cache and locally mark the
|
|
1659
|
-
* credit spent, then the caller re-lists for the authoritative count.
|
|
1660
|
-
*/
|
|
1691
|
+
/** Legacy route preserves optional credit selection and the authoritative recount. */
|
|
1661
1692
|
async consumeResetCredit(creditId) {
|
|
1693
|
+
return this.redeemResetCredit(creditId);
|
|
1694
|
+
}
|
|
1695
|
+
async consumeConfirmedResetCredit(creditId, accountRevision) {
|
|
1696
|
+
if (!isCodexResetCreditId(creditId) || !isCodexAccountRevision(accountRevision)) {
|
|
1697
|
+
throw codexAdminError("codex_reset_invalid", 422);
|
|
1698
|
+
}
|
|
1699
|
+
return this.redeemResetCredit(creditId, accountRevision);
|
|
1700
|
+
}
|
|
1701
|
+
async redeemResetCredit(creditId, accountRevision) {
|
|
1662
1702
|
if (this.resetFlight) throw codexAdminError("codex_reset_busy", 429);
|
|
1663
|
-
|
|
1664
|
-
const
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1703
|
+
this.resetFlight = true;
|
|
1704
|
+
const epoch = this.epoch;
|
|
1705
|
+
const confirmed = accountRevision !== void 0;
|
|
1706
|
+
const assertConfirmedRevision = () => {
|
|
1707
|
+
if (confirmed && (accountRevision !== this.accountRevision || this.epoch !== epoch)) {
|
|
1708
|
+
throw codexAdminError("codex_reset_account_changed", 409);
|
|
1709
|
+
}
|
|
1710
|
+
};
|
|
1711
|
+
try {
|
|
1712
|
+
await this.mutationTail;
|
|
1713
|
+
const data = await this.ensureLoaded(true);
|
|
1714
|
+
assertConfirmedRevision();
|
|
1715
|
+
if (!data?.account || this.epoch !== epoch) throw codexAdminError("codex_not_connected", 409);
|
|
1716
|
+
const revision = this.resetRevision;
|
|
1717
|
+
const assertCurrentAccount = () => {
|
|
1718
|
+
assertConfirmedRevision();
|
|
1719
|
+
if (this.epoch !== epoch || this.resetRevision !== revision || !sameCodexAccount(data.account, this.data?.account)) {
|
|
1720
|
+
throw codexAdminError("codex_not_connected", 409);
|
|
1670
1721
|
}
|
|
1671
|
-
|
|
1722
|
+
};
|
|
1723
|
+
if (creditId && this.spentResetCredits.has(creditId)) throw codexAdminError("codex_reset_no_credit", 409);
|
|
1724
|
+
const accessToken = await this.getAccessToken();
|
|
1725
|
+
const account = this.data?.account;
|
|
1726
|
+
assertCurrentAccount();
|
|
1727
|
+
if (!account || account.accessToken !== accessToken || confirmed && !account.accountId) {
|
|
1728
|
+
throw codexAdminError("codex_not_connected", 409);
|
|
1729
|
+
}
|
|
1730
|
+
const pendingKey = this.pendingReset.get(creditId);
|
|
1731
|
+
if (confirmed) {
|
|
1732
|
+
const listing = await this.fetchResetCredits(account, creditId);
|
|
1733
|
+
await this.mutationTail;
|
|
1672
1734
|
const fresh = await this.ensureLoaded(true);
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
if (error?.code === "codex_upstream_error") throw codexAdminError("codex_reset_unavailable", 502);
|
|
1684
|
-
throw error;
|
|
1735
|
+
assertCurrentAccount();
|
|
1736
|
+
if (!fresh?.account) throw codexAdminError("codex_not_connected", 409);
|
|
1737
|
+
if (!listing) throw codexAdminError("codex_reset_unavailable", 502);
|
|
1738
|
+
const selected = listing.credits.filter((c) => c.id === creditId);
|
|
1739
|
+
if (!listing.availableCount || selected.length !== 1 || selected[0].status !== "available" || selected[0].expiresAt !== void 0 && selected[0].expiresAt <= this.now) {
|
|
1740
|
+
this.invalidateResetCredits();
|
|
1741
|
+
this.cacheResetListing({ ...listing, credits: listing.credits.filter((c) => c.id !== creditId || c.status !== "available") });
|
|
1742
|
+
if (pendingKey) throw codexAdminError("codex_reset_unavailable", 502);
|
|
1743
|
+
throw codexAdminError("codex_reset_no_credit", 409);
|
|
1744
|
+
}
|
|
1685
1745
|
}
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1746
|
+
const redeemRequest = pendingKey ?? randomUUID();
|
|
1747
|
+
this.pendingReset.set(creditId, redeemRequest);
|
|
1748
|
+
const result = await consumeCodexResetCredit({
|
|
1749
|
+
redeemRequest,
|
|
1750
|
+
creditId,
|
|
1751
|
+
accessToken: account.accessToken,
|
|
1752
|
+
accountId: account.accountId || void 0,
|
|
1753
|
+
fetchImpl: (url, init) => this.directFetch(url, init)
|
|
1754
|
+
}).catch((error) => {
|
|
1755
|
+
if (!confirmed || error?.code === "codex_reauth_required") throw error;
|
|
1756
|
+
throw codexAdminError("codex_reset_unavailable", 502);
|
|
1757
|
+
});
|
|
1758
|
+
assertCurrentAccount();
|
|
1759
|
+
this.pendingReset.delete(creditId);
|
|
1691
1760
|
if (result.outcome === "reset" || result.outcome === "already_redeemed") {
|
|
1692
1761
|
if (creditId) this.spentResetCredits.add(creditId);
|
|
1693
|
-
this.
|
|
1694
|
-
this.
|
|
1762
|
+
this.invalidateUsage();
|
|
1763
|
+
this.invalidateResetCredits();
|
|
1764
|
+
if (!confirmed) {
|
|
1765
|
+
const listing = await this.getResetCredits().catch(() => null);
|
|
1766
|
+
if (this.epoch === epoch && sameCodexAccount(data.account, this.data?.account) && listing) {
|
|
1767
|
+
return { ...result, availableCount: listing.availableCount };
|
|
1768
|
+
}
|
|
1769
|
+
}
|
|
1695
1770
|
} else if (result.outcome === "no_credit") {
|
|
1696
|
-
this.
|
|
1771
|
+
this.invalidateResetCredits();
|
|
1772
|
+
if (!confirmed) this.cacheResetListing({ credits: [], availableCount: 0 });
|
|
1697
1773
|
}
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
return { ...result, ...availableCount !== void 0 ? { availableCount } : {} };
|
|
1774
|
+
return result;
|
|
1775
|
+
} catch (error) {
|
|
1776
|
+
assertConfirmedRevision();
|
|
1777
|
+
if (error?.code === "codex_reauth_required") throw codexAdminError("codex_reauth_required", 422);
|
|
1778
|
+
if (error?.code === "codex_upstream_error") throw codexAdminError("codex_reset_unavailable", 502);
|
|
1779
|
+
throw error;
|
|
1705
1780
|
} finally {
|
|
1706
|
-
|
|
1781
|
+
this.resetFlight = false;
|
|
1707
1782
|
}
|
|
1708
1783
|
}
|
|
1709
1784
|
// -------------------------------------------------------------- cooling/... -
|
|
@@ -1841,10 +1916,6 @@ var CodexAuthService = class {
|
|
|
1841
1916
|
this.loaded = true;
|
|
1842
1917
|
this.refreshFlight = null;
|
|
1843
1918
|
this.forceRefreshRequested = false;
|
|
1844
|
-
this.usageCache = null;
|
|
1845
|
-
this.resetCache = null;
|
|
1846
|
-
this.spentResetCredits.clear();
|
|
1847
|
-
this.pendingReset = null;
|
|
1848
1919
|
return { success: true };
|
|
1849
1920
|
});
|
|
1850
1921
|
}
|
|
@@ -1857,6 +1928,57 @@ var CodexAuthService = class {
|
|
|
1857
1928
|
}
|
|
1858
1929
|
};
|
|
1859
1930
|
|
|
1931
|
+
// src/service-tier.ts
|
|
1932
|
+
import { randomUUID as randomUUID2 } from "node:crypto";
|
|
1933
|
+
import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
1934
|
+
import { dirname as dirname2, join as join2 } from "node:path";
|
|
1935
|
+
function isCodexServiceTierSetting(value) {
|
|
1936
|
+
if (!value || typeof value !== "object" || Array.isArray(value) || !Object.hasOwn(value, "tier") || Object.keys(value).length !== 1) return false;
|
|
1937
|
+
const { tier } = value;
|
|
1938
|
+
return tier === "follow" || tier === "standard" || tier === "priority";
|
|
1939
|
+
}
|
|
1940
|
+
var CodexServiceTierStore = class _CodexServiceTierStore {
|
|
1941
|
+
constructor(path, current) {
|
|
1942
|
+
this.path = path;
|
|
1943
|
+
this.current = current;
|
|
1944
|
+
}
|
|
1945
|
+
writeTail = Promise.resolve();
|
|
1946
|
+
static async load(configDir, log) {
|
|
1947
|
+
const path = join2(configDir, "codex-service-tier.json");
|
|
1948
|
+
let tier = "follow";
|
|
1949
|
+
try {
|
|
1950
|
+
const setting = JSON.parse(await readFile(path, "utf8"));
|
|
1951
|
+
if (!isCodexServiceTierSetting(setting)) throw new Error("invalid service tier");
|
|
1952
|
+
tier = setting.tier;
|
|
1953
|
+
} catch (error) {
|
|
1954
|
+
if (error.code !== "ENOENT") {
|
|
1955
|
+
log.warn("Could not read Codex service tier. Following the request tier.");
|
|
1956
|
+
}
|
|
1957
|
+
}
|
|
1958
|
+
return new _CodexServiceTierStore(path, tier);
|
|
1959
|
+
}
|
|
1960
|
+
get tier() {
|
|
1961
|
+
return this.current;
|
|
1962
|
+
}
|
|
1963
|
+
set(tier) {
|
|
1964
|
+
const write = this.writeTail.then(async () => {
|
|
1965
|
+
await mkdir(dirname2(this.path), { recursive: true, mode: 448 });
|
|
1966
|
+
const temp = `${this.path}.${randomUUID2()}.tmp`;
|
|
1967
|
+
try {
|
|
1968
|
+
await writeFile(temp, JSON.stringify({ tier }), { flag: "wx", mode: 384 });
|
|
1969
|
+
await rename(temp, this.path);
|
|
1970
|
+
} catch (error) {
|
|
1971
|
+
await rm(temp, { force: true });
|
|
1972
|
+
throw error;
|
|
1973
|
+
}
|
|
1974
|
+
this.current = tier;
|
|
1975
|
+
});
|
|
1976
|
+
this.writeTail = write.catch(() => {
|
|
1977
|
+
});
|
|
1978
|
+
return write;
|
|
1979
|
+
}
|
|
1980
|
+
};
|
|
1981
|
+
|
|
1860
1982
|
// src/finalize-body.ts
|
|
1861
1983
|
import { createHash as createHash2 } from "node:crypto";
|
|
1862
1984
|
var CODEX_PARALLEL_TOOL_CALLS_DEFAULT = true;
|
|
@@ -2014,7 +2136,13 @@ function codexFinalizeBody(body, options = {}) {
|
|
|
2014
2136
|
const source = isPlainObject(body) ? clone(body) : {};
|
|
2015
2137
|
const out = { ...source };
|
|
2016
2138
|
for (const key of DROP_FIELDS) delete out[key];
|
|
2017
|
-
if (
|
|
2139
|
+
if (options.serviceTier === "standard") {
|
|
2140
|
+
delete out.service_tier;
|
|
2141
|
+
} else if (options.serviceTier === "priority" || options.serviceTier === "follow" && out.service_tier === "fast") {
|
|
2142
|
+
out.service_tier = "priority";
|
|
2143
|
+
} else if (typeof out.service_tier === "string" && !ALLOWED_SERVICE_TIERS.has(out.service_tier)) {
|
|
2144
|
+
delete out.service_tier;
|
|
2145
|
+
}
|
|
2018
2146
|
out.store = false;
|
|
2019
2147
|
out.stream = true;
|
|
2020
2148
|
out.tool_choice = "auto";
|
|
@@ -2141,9 +2269,11 @@ var CodexTransformer = class {
|
|
|
2141
2269
|
name = "codex";
|
|
2142
2270
|
logger;
|
|
2143
2271
|
service;
|
|
2272
|
+
getServiceTier;
|
|
2144
2273
|
constructor(options) {
|
|
2145
2274
|
if (!options?.service) throw new Error("CodexTransformer requires { service }");
|
|
2146
2275
|
this.service = options.service;
|
|
2276
|
+
this.getServiceTier = options.getServiceTier;
|
|
2147
2277
|
this.logger = options.logger;
|
|
2148
2278
|
}
|
|
2149
2279
|
/**
|
|
@@ -2151,7 +2281,7 @@ var CodexTransformer = class {
|
|
|
2151
2281
|
* `openai-responses` transformer. The runtime performs auth injection later
|
|
2152
2282
|
* (after the body is final), so no token is touched here.
|
|
2153
2283
|
*/
|
|
2154
|
-
async transformRequestIn(request,
|
|
2284
|
+
async transformRequestIn(request, provider, context) {
|
|
2155
2285
|
const model = typeof request?.model === "string" ? request.model : "";
|
|
2156
2286
|
let modelInstructions;
|
|
2157
2287
|
try {
|
|
@@ -2160,7 +2290,10 @@ var CodexTransformer = class {
|
|
|
2160
2290
|
modelInstructions = void 0;
|
|
2161
2291
|
}
|
|
2162
2292
|
const sessionId = this.sessionIdFor(request, context);
|
|
2163
|
-
const
|
|
2293
|
+
const serviceTier = isCodexRuntime(provider) ? this.getServiceTier?.() ?? "follow" : void 0;
|
|
2294
|
+
const originalTier = context?.req?.body?.service_tier;
|
|
2295
|
+
const source = serviceTier === "follow" && request.service_tier === void 0 && typeof originalTier === "string" ? { ...request, service_tier: originalTier } : request;
|
|
2296
|
+
const body = codexFinalizeBody(source, { modelInstructions, sessionId, serviceTier });
|
|
2164
2297
|
if (context) context.codexSessionKey = codexSessionKey(body);
|
|
2165
2298
|
return { body };
|
|
2166
2299
|
}
|
|
@@ -2534,9 +2667,8 @@ function registerCodexAdminRoutes(app, deps) {
|
|
|
2534
2667
|
err.statusCode = 403;
|
|
2535
2668
|
throw err;
|
|
2536
2669
|
};
|
|
2537
|
-
const
|
|
2538
|
-
|
|
2539
|
-
if (!isLoopback) deny();
|
|
2670
|
+
const isLoopback = (ip) => ip === "127.0.0.1" || ip === "::1" || ip === "::ffff:127.0.0.1";
|
|
2671
|
+
if (!isLoopback(req.raw?.socket?.remoteAddress) || !isLoopback(req.ip)) deny();
|
|
2540
2672
|
const origin = req.headers?.origin;
|
|
2541
2673
|
let host = "";
|
|
2542
2674
|
try {
|
|
@@ -2575,6 +2707,7 @@ function registerCodexAdminRoutes(app, deps) {
|
|
|
2575
2707
|
codex_models_fetch_failed: 422,
|
|
2576
2708
|
codex_reauth_required: 422,
|
|
2577
2709
|
codex_model_invalid: 422,
|
|
2710
|
+
codex_reset_account_changed: 409,
|
|
2578
2711
|
codex_reset_busy: 429,
|
|
2579
2712
|
codex_reset_no_credit: 409,
|
|
2580
2713
|
codex_reset_unavailable: 502,
|
|
@@ -2606,6 +2739,28 @@ function registerCodexAdminRoutes(app, deps) {
|
|
|
2606
2739
|
reply.status(422);
|
|
2607
2740
|
return { ok: false, success: false, error: "codex_login_state_rejected" };
|
|
2608
2741
|
}
|
|
2742
|
+
app.get("/api/codex/service-tier", async (req, reply) => {
|
|
2743
|
+
try {
|
|
2744
|
+
assertLocalCodexRequest(req);
|
|
2745
|
+
return { tier: deps.serviceTier.tier };
|
|
2746
|
+
} catch (error) {
|
|
2747
|
+
return codexRouteError(error, reply, "service-tier", "codex_request_failed");
|
|
2748
|
+
}
|
|
2749
|
+
});
|
|
2750
|
+
app.put("/api/codex/service-tier", async (req, reply) => {
|
|
2751
|
+
try {
|
|
2752
|
+
assertLocalCodexRequest(req);
|
|
2753
|
+
if (req.headers["content-type"]?.split(";", 1)[0].trim().toLowerCase() !== "application/json" || !isCodexServiceTierSetting(req.body)) {
|
|
2754
|
+
reply.status(422);
|
|
2755
|
+
return { ok: false, success: false, error: "codex_service_tier_invalid" };
|
|
2756
|
+
}
|
|
2757
|
+
const { tier } = req.body;
|
|
2758
|
+
await deps.serviceTier.set(tier);
|
|
2759
|
+
return { tier };
|
|
2760
|
+
} catch (error) {
|
|
2761
|
+
return codexRouteError(error, reply, "service-tier", "codex_config_write_failed");
|
|
2762
|
+
}
|
|
2763
|
+
});
|
|
2609
2764
|
app.get("/api/codex/status", async (req, reply) => {
|
|
2610
2765
|
try {
|
|
2611
2766
|
assertLocalCodexRequest(req);
|
|
@@ -2781,6 +2936,24 @@ function registerCodexAdminRoutes(app, deps) {
|
|
|
2781
2936
|
return codexRouteError(error, reply, "reset-credits/consume", "codex_reset_failed");
|
|
2782
2937
|
}
|
|
2783
2938
|
});
|
|
2939
|
+
app.post("/api/codex/reset-credits/consume-confirmed-v2", async (req, reply) => {
|
|
2940
|
+
try {
|
|
2941
|
+
assertLocalCodexRequest(req);
|
|
2942
|
+
const service = deps.getService();
|
|
2943
|
+
if (!service) {
|
|
2944
|
+
reply.status(503);
|
|
2945
|
+
return { ok: false, success: false, error: "codex_request_failed" };
|
|
2946
|
+
}
|
|
2947
|
+
const { creditId, accountRevision } = req.body ?? {};
|
|
2948
|
+
if (!isCodexResetCreditId(creditId) || !isCodexAccountRevision(accountRevision)) {
|
|
2949
|
+
throw codexAdminError("codex_reset_invalid", 422);
|
|
2950
|
+
}
|
|
2951
|
+
const result = await service.consumeConfirmedResetCredit(creditId, accountRevision);
|
|
2952
|
+
return { ok: true, success: true, ...result };
|
|
2953
|
+
} catch (error) {
|
|
2954
|
+
return codexRouteError(error, reply, "reset-credits/consume-confirmed-v2", "codex_reset_failed");
|
|
2955
|
+
}
|
|
2956
|
+
});
|
|
2784
2957
|
app.post("/api/codex/disconnect", async (req, reply) => {
|
|
2785
2958
|
try {
|
|
2786
2959
|
assertLocalCodexRequest(req);
|
|
@@ -2897,6 +3070,7 @@ var codexLocales = {
|
|
|
2897
3070
|
codex_request_rejected: "Codex rejected the request.",
|
|
2898
3071
|
codex_upstream_error: "Codex upstream request failed.",
|
|
2899
3072
|
reset_failed: "The usage reset failed. Please try again.",
|
|
3073
|
+
codex_reset_account_changed: "The connected account changed. Check the available resets again.",
|
|
2900
3074
|
codex_reset_busy: "A usage reset is already running \u2014 wait for it to finish.",
|
|
2901
3075
|
codex_reset_no_credit: "No usage resets are available.",
|
|
2902
3076
|
codex_reset_unavailable: "The reset service is unavailable right now. Please try again.",
|
|
@@ -3004,6 +3178,7 @@ var codexLocales = {
|
|
|
3004
3178
|
codex_request_rejected: "Codex \u62D2\u7EDD\u4E86\u8BE5\u8BF7\u6C42\u3002",
|
|
3005
3179
|
codex_upstream_error: "Codex \u4E0A\u6E38\u8BF7\u6C42\u5931\u8D25\u3002",
|
|
3006
3180
|
reset_failed: "\u7528\u91CF\u91CD\u7F6E\u5931\u8D25\uFF0C\u8BF7\u91CD\u8BD5\u3002",
|
|
3181
|
+
codex_reset_account_changed: "\u5DF2\u5207\u6362\u8FDE\u63A5\u8D26\u6237\uFF0C\u8BF7\u91CD\u65B0\u67E5\u8BE2\u53EF\u7528\u91CD\u7F6E\u5361\u3002",
|
|
3007
3182
|
codex_reset_busy: "\u5DF2\u6709\u7528\u91CF\u91CD\u7F6E\u6B63\u5728\u8FDB\u884C\u2014\u2014\u8BF7\u7B49\u5F85\u5B8C\u6210\u3002",
|
|
3008
3183
|
codex_reset_no_credit: "\u6CA1\u6709\u53EF\u7528\u7684\u7528\u91CF\u91CD\u7F6E\u673A\u4F1A\u3002",
|
|
3009
3184
|
codex_reset_unavailable: "\u91CD\u7F6E\u670D\u52A1\u6682\u65F6\u4E0D\u53EF\u7528\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5\u3002",
|
|
@@ -3085,7 +3260,7 @@ function replaceCodexTokenMaintenance(next) {
|
|
|
3085
3260
|
// src/plugin.ts
|
|
3086
3261
|
var manifest = {
|
|
3087
3262
|
name: "codex",
|
|
3088
|
-
version: "1.0.
|
|
3263
|
+
version: "1.0.17",
|
|
3089
3264
|
coreApi: "1",
|
|
3090
3265
|
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."
|
|
3091
3266
|
};
|
|
@@ -3099,9 +3274,10 @@ async function activate(ctx) {
|
|
|
3099
3274
|
host: upstreamHost,
|
|
3100
3275
|
logger: ctx.log
|
|
3101
3276
|
});
|
|
3277
|
+
const serviceTier = await CodexServiceTierStore.load(ctx.host.configDir, ctx.log);
|
|
3102
3278
|
class ActivatedCodexTransformer extends CodexTransformer {
|
|
3103
3279
|
constructor() {
|
|
3104
|
-
super({ service, logger: ctx.log });
|
|
3280
|
+
super({ service, logger: ctx.log, getServiceTier: () => serviceTier.tier });
|
|
3105
3281
|
}
|
|
3106
3282
|
}
|
|
3107
3283
|
const runtime = createCodexManagedRuntime({ service, host: upstreamHost, log: ctx.log });
|
|
@@ -3113,7 +3289,7 @@ async function activate(ctx) {
|
|
|
3113
3289
|
replaceCodexTokenMaintenance(maintenance);
|
|
3114
3290
|
ctx.registerTransformer(ActivatedCodexTransformer);
|
|
3115
3291
|
ctx.registerManagedRuntime(runtime);
|
|
3116
|
-
ctx.registerAdminRoutes((app) => registerCodexAdminRoutes(app, { getService: () => service }));
|
|
3292
|
+
ctx.registerAdminRoutes((app) => registerCodexAdminRoutes(app, { getService: () => service, serviceTier }));
|
|
3117
3293
|
ctx.registerConfigProtector((next, prev) => protectCodexConfig(next, prev));
|
|
3118
3294
|
ctx.registerUiCard({ cardPath: "card.js" });
|
|
3119
3295
|
ctx.registerLocales(codexLocales);
|
package/dist/plugin.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "codex",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.17",
|
|
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.
|
|
3
|
+
"version": "1.0.17",
|
|
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",
|