mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 13:38:31 +00:00
Refactor
This commit is contained in:
@@ -8,6 +8,7 @@ import { U, parseResetTime, toFiniteNumber } from "./shared.js";
|
||||
// Codex (OpenAI) API config
|
||||
const CODEX_CONFIG = {
|
||||
usageUrl: U("codex").url,
|
||||
resetCreditsConsumeUrl: U("codex").resetCreditsConsumeUrl,
|
||||
};
|
||||
|
||||
function getCodexRateLimitBody(snapshot) {
|
||||
@@ -82,6 +83,7 @@ export async function getCodexUsage(accessToken, proxyOptions = null) {
|
||||
const data = await response.json();
|
||||
const normalRateLimit = data.rate_limit || data.rate_limits || data.rate_limits_by_limit_id?.codex || {};
|
||||
const reviewRateLimit = getCodexReviewRateLimit(data);
|
||||
const availableResetCredits = Math.max(0, toFiniteNumber(data.rate_limit_reset_credits?.available_count, 0));
|
||||
const quotas = {};
|
||||
|
||||
appendCodexQuotaWindows(quotas, "", normalRateLimit);
|
||||
@@ -91,9 +93,53 @@ export async function getCodexUsage(accessToken, proxyOptions = null) {
|
||||
plan: data.plan_type || data.summary?.plan || "unknown",
|
||||
limitReached: getCodexRateLimitBody(normalRateLimit)?.limit_reached || false,
|
||||
reviewLimitReached: getCodexRateLimitBody(reviewRateLimit)?.limit_reached || false,
|
||||
resetCredits: { availableCount: availableResetCredits },
|
||||
quotas,
|
||||
};
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to fetch Codex usage: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Consume one Codex rate-limit reset credit (irreversible, spends 1 credit)
|
||||
export async function consumeCodexRateLimitResetCredit(accessToken, redeemRequestId, proxyOptions = null) {
|
||||
if (!accessToken) {
|
||||
throw new Error("No Codex access token available. Please re-authorize the connection.");
|
||||
}
|
||||
if (!redeemRequestId || typeof redeemRequestId !== "string") {
|
||||
throw new Error("A redeem request id is required to consume a Codex reset credit.");
|
||||
}
|
||||
|
||||
let response;
|
||||
let data = null;
|
||||
try {
|
||||
response = await proxyAwareFetch(CODEX_CONFIG.resetCreditsConsumeUrl, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Authorization": `Bearer ${accessToken}`,
|
||||
"Accept": "application/json",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({ redeem_request_id: redeemRequestId }),
|
||||
}, proxyOptions);
|
||||
|
||||
const text = await response.text();
|
||||
data = text ? JSON.parse(text) : null;
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to consume Codex reset credit: ${error.message}`);
|
||||
}
|
||||
|
||||
const code = data?.code || null;
|
||||
const windowsReset = toFiniteNumber(data?.windows_reset, 0);
|
||||
const success = response.ok && (code === "reset" || windowsReset > 0);
|
||||
|
||||
return {
|
||||
ok: success,
|
||||
noCredit: response.ok && code === "no_credit",
|
||||
status: response.status,
|
||||
code,
|
||||
windowsReset,
|
||||
message: data?.message || null,
|
||||
raw: data,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user