feat(quota): sync upstream quota queries and improve reset handling

- Update Claude, Codex, Kimi, Antigravity, and xAI compatibility
 - Display plan details, subscription expiry, and dynamic reset times
 - Deduplicate quota requests and serialize Codex resets
 - Require confirmation for potentially billable xAI health probes
 - Add quota parsing, request, and rendering regression tests
This commit is contained in:
lzt404 committed 2026-09-06 19:02:49 +08:00
1 parent 34cab99a0a
commit e4d0059efa
13 files changed
+911 -141

No files matched your search

+10
View File
@@ -133,6 +133,16 @@ export const jaOverrides = {
'quota.resetCredits': '手動リセット回数',
'quota.earliestExpiry': '最も早い有効期限',
'quota.remaining': '残り {percent}%',
'quota.resetPassed': 'リセット時刻到達。更新して確認してください',
'quota.subscriptionExpiry': 'サブスクリプション期限:{time}',
'quota.resetApplicable': '現在適用可能:{count} 回',
'quota.resetCreditsWarning': 'リセット回数の詳細取得に失敗:{error}',
'quota.xaiProbeConfirm': 'xAI 有料アカウントはこの請求クォータ API に対応していません。短い grok-4.5 リクエストで利用可能か確認しますか?少額の料金が発生する可能性があり、残りクォータは取得できません。',
'quota.service.window.sevenDayFable': '7 日間 Fable 枠',
'quota.service.xaiPaidHealth': '有料 API 利用可能',
'quota.service.xaiPaidHealthDetail': '短い会話テストに成功。残りクォータは提供されていません',
'quota.service.error.xaiProbeRequired': '有料 API テストには確認が必要です。課金リクエストは送信していません',
'quota.service.error.resetCreditsInvalid': 'リセット回数 API のデータ形式を認識できません',
'quota.confirm.title': '「{name}」の Codex クォータをリセットしますか?',
'quota.confirm.cost': 'この操作では手動リセットを 1 回消費します。',
'quota.confirm.available': '現在利用可能:{count} 回',
+10
View File
@@ -170,6 +170,16 @@ export const en: Record<MessageKey, string> = {
'quota.resetCredits': 'Manual resets',
'quota.earliestExpiry': 'Earliest expiry',
'quota.remaining': '{percent}% remaining',
'quota.resetPassed': 'Reset time reached; refresh to verify',
'quota.subscriptionExpiry': 'Subscription expires: {time}',
'quota.resetApplicable': 'Currently applicable: {count}',
'quota.resetCreditsWarning': 'Reset credit details unavailable: {error}',
'quota.xaiProbeConfirm': 'Paid xAI accounts do not expose this billing quota API. Send a tiny grok-4.5 request to check availability? This may incur a small charge and does not report remaining quota.',
'quota.service.window.sevenDayFable': '7-day Fable window',
'quota.service.xaiPaidHealth': 'Paid API available',
'quota.service.xaiPaidHealthDetail': 'Tiny chat probe succeeded; remaining quota is not provided',
'quota.service.error.xaiProbeRequired': 'Paid API health probing requires confirmation; no billable request was sent',
'quota.service.error.resetCreditsInvalid': 'The reset credits endpoint returned unrecognized data',
'quota.confirm.title': 'Reset the Codex quota for “{name}”?',
'quota.confirm.cost': 'This will consume one manual reset credit.',
'quota.confirm.available': 'Currently available: {count}',
+10
View File
@@ -168,6 +168,16 @@ export const zhCN = {
'quota.resetCredits': '主动重置次数',
'quota.earliestExpiry': '最早过期',
'quota.remaining': '剩余 {percent}%',
'quota.resetPassed': '重置时间已到,请刷新确认',
'quota.subscriptionExpiry': '订阅到期:{time}',
'quota.resetApplicable': '当前适用:{count} 次',
'quota.resetCreditsWarning': '重置次数详情查询失败:{error}',
'quota.xaiProbeConfirm': 'xAI 付费账户不提供此账单额度接口。是否发送极短的 grok-4.5 请求检测可用性?这可能产生少量费用,且不会返回剩余额度。',
'quota.service.window.sevenDayFable': '7 天 Fable 窗口',
'quota.service.xaiPaidHealth': '付费 API 可用',
'quota.service.xaiPaidHealthDetail': '极短对话探测成功;上游未提供剩余额度',
'quota.service.error.xaiProbeRequired': '付费 API 可用性探测需要确认,尚未发送计费请求',
'quota.service.error.resetCreditsInvalid': '重置次数接口返回了无法识别的数据',
'quota.confirm.title': '确认重置“{name}”的 Codex 额度吗?',
'quota.confirm.cost': '本操作将消耗 1 次主动重置机会。',
'quota.confirm.available': '当前可用:{count} 次',
+12 -3
View File
@@ -38,6 +38,7 @@ import {
import {
captureQuotaCacheGeneration,
commitQuotaCacheIfCurrent,
getQuotaCacheSnapshot,
pruneQuotaCache,
updateQuotaCache,
useQuotaCache,
@@ -57,6 +58,7 @@ import {
type OAuthModelDefinition,
} from '../services/oauthModels';
import { getCurrentLocale, translate, useI18n } from '../i18n';
import { formatQuotaReset, useQuotaClock } from '../services/quotaTime';
type AuthFile = Record<string, unknown>;
@@ -102,6 +104,7 @@ const statusText = (file: AuthFile) => {
function AuthFileQuotaSummary({ quota }: { quota: QuotaState }) {
const { locale, t } = useI18n();
const now = useQuotaClock() + (quota.serverTimeOffsetMs ?? 0);
if (quota.status === 'loading') {
return (
<div className="auth-file-quota loading">
@@ -124,18 +127,23 @@ function AuthFileQuotaSummary({ quota }: { quota: QuotaState }) {
<div className="auth-file-quota" aria-label={t('authFiles.quota.aria')}>
{quota.plan ? <span className="auth-file-quota-plan">{quota.plan}</span> : null}
{quota.rows.length > 0 ? quota.rows.map((row, index) => {
const detail = [row.detail, row.reset].filter(Boolean).join(' · ');
const reset = formatQuotaReset(row.resetAtMs, row.reset, locale, now);
const detail = [row.detail, reset].filter(Boolean).join(' · ');
return (
<span className="auth-file-quota-item" key={`${row.label}-${index}`} title={detail || undefined}>
<span>{row.label}</span>
<strong>{row.remainingPercent === null ? '—' : `${Math.round(row.remainingPercent)}%`}</strong>
{row.reset ? <small>{row.reset}</small> : null}
{reset ? <small>{reset}</small> : null}
{row.remainingPercent === null && row.detail ? <small>{row.detail}</small> : null}
</span>
);
}) : <span className="auth-file-quota-empty">{t('authFiles.quota.empty')}</span>}
{quota.resetCredits !== undefined ? (
<span className="auth-file-quota-credit">{t('authFiles.quota.resets', { count: quota.resetCredits })}</span>
) : null}
{quota.resetCreditsApplicable !== undefined ? <span className="auth-file-quota-credit">{t('quota.resetApplicable', { count: quota.resetCreditsApplicable })}</span> : null}
{quota.subscriptionActiveUntil ? <span className="auth-file-quota-credit">{t('quota.subscriptionExpiry', { time: formatQuotaTimestamp(quota.subscriptionActiveUntil, locale) })}</span> : null}
{quota.resetCreditsError ? <small>{t('quota.resetCreditsWarning', { error: quota.resetCreditsError })}</small> : null}
{quota.resetCreditsEarliestExpiry ? (
<span className="auth-file-quota-credit">{t('authFiles.quota.expiry', { time: formatQuotaTimestamp(quota.resetCreditsEarliestExpiry, locale) })}</span>
) : null}
@@ -205,9 +213,10 @@ export function AuthFileManagementPage() {
const refreshQuota = async (file: AuthFile) => {
if (readBoolean(file, 'disabled')) return;
const key = quotaKey(file);
if (getQuotaCacheSnapshot()[key]?.status === 'loading') return;
const cacheGeneration = captureQuotaCacheGeneration();
updateQuotaCache((current) => ({ ...current, [key]: { status: 'loading', rows: [] } }));
const result = await loadQuota(file);
const result = await loadQuota(file, { confirmXaiPaidProbe: () => window.confirm(t('quota.xaiProbeConfirm')) });
commitQuotaCacheIfCurrent(cacheGeneration, () => {
updateQuotaCache((current) => ({ ...current, [key]: result }));
});
+39 -13
View File
@@ -6,6 +6,7 @@ import codexIcon from '../assets/icons/codex.svg';
import grokIcon from '../assets/icons/grok.svg';
import kimiIcon from '../assets/icons/kimi-light.svg';
import { managementApi, readBoolean, responseList } from '../services/managementApi';
import { formatQuotaReset, useQuotaClock } from '../services/quotaTime';
import {
consumeCodexResetCredit,
fileName,
@@ -21,6 +22,7 @@ import {
import {
captureQuotaCacheGeneration,
commitQuotaCacheIfCurrent,
getQuotaCacheSnapshot,
pruneQuotaCache,
updateQuotaCache,
useQuotaCache,
@@ -46,17 +48,17 @@ export function QuotaPage() {
const [loading, setLoading] = useState(true);
const [refreshing, setRefreshing] = useState(false);
const [error, setError] = useState('');
const querying = Object.values(quotas).some((quota) => quota.status === 'loading');
const loadFiles = useCallback(async () => {
setLoading(true);
setError('');
try {
const payload = await managementApi.get('/auth-files');
const nextFiles = dedupeAuthFiles(responseList(payload, 'files')).filter(
(file) => !readBoolean(file, 'disabled') && providerForFile(file),
);
const allFiles = dedupeAuthFiles(responseList(payload, 'files'));
const nextFiles = allFiles.filter((file) => !readBoolean(file, 'disabled') && providerForFile(file));
setFiles(nextFiles);
const validQuotaKeys = new Set(nextFiles.map(quotaKey));
const validQuotaKeys = new Set(allFiles.map(quotaKey));
pruneQuotaCache(validQuotaKeys);
updateQuotaCache((current) => {
const next = { ...current };
@@ -79,15 +81,17 @@ export function QuotaPage() {
const refreshOne = useCallback(async (file: AuthFile) => {
const key = quotaKey(file);
if (getQuotaCacheSnapshot()[key]?.status === 'loading') return;
const cacheGeneration = captureQuotaCacheGeneration();
updateQuotaCache((current) => ({ ...current, [key]: { status: 'loading', rows: [] } }));
const result = await loadQuota(file);
const result = await loadQuota(file, { confirmXaiPaidProbe: () => window.confirm(t('quota.xaiProbeConfirm')) });
commitQuotaCacheIfCurrent(cacheGeneration, () => {
updateQuotaCache((current) => ({ ...current, [key]: result }));
});
}, []);
}, [t]);
const resetCodexQuota = useCallback(async (file: AuthFile, quota: QuotaState) => {
if (getQuotaCacheSnapshot()[quotaKey(file)]?.status === 'loading') return;
const confirmed = window.confirm([
t('quota.confirm.title', { name: fileName(file) }),
'',
@@ -121,15 +125,23 @@ export function QuotaPage() {
}, [locale, t]);
const refreshAll = useCallback(async () => {
if (Object.values(getQuotaCacheSnapshot()).some((quota) => quota.status === 'loading')) return;
setRefreshing(true);
setError('');
const cacheGeneration = captureQuotaCacheGeneration();
updateQuotaCache((current) => Object.fromEntries(files.map((file) => [quotaKey(file), { ...current[quotaKey(file)], status: 'loading', rows: [] }])));
updateQuotaCache((current) => ({
...current,
...Object.fromEntries(files.map((file) => [quotaKey(file), {
...current[quotaKey(file)], status: 'loading', rows: [],
}])),
}));
try {
for (let index = 0; index < files.length; index += REFRESH_CONCURRENCY) {
const batch = files.slice(index, index + REFRESH_CONCURRENCY);
await Promise.all(batch.map(async (file) => {
const result = await loadQuota(file);
const result = await loadQuota(file, {
confirmXaiPaidProbe: () => window.confirm(`${fileName(file)}\n\n${t('quota.xaiProbeConfirm')}`),
});
commitQuotaCacheIfCurrent(cacheGeneration, () => {
updateQuotaCache((current) => ({ ...current, [quotaKey(file)]: result }));
});
@@ -138,7 +150,7 @@ export function QuotaPage() {
} finally {
setRefreshing(false);
}
}, [files]);
}, [files, t]);
const grouped = useMemo(() => {
const groups = new Map<QuotaProvider, { file: AuthFile; quota: QuotaState }[]>();
@@ -161,10 +173,10 @@ export function QuotaPage() {
<div><span>Quota</span><h1>{t('quota.title')}</h1></div>
<div className="management-heading-actions">
<span className="muted-summary">{t(files.length === 1 ? 'quota.queryableCredentials.one' : 'quota.queryableCredentials.other', { count: files.length })}</span>
<button type="button" className="secondary-button compact-button" onClick={() => void loadFiles()} disabled={loading || refreshing}>
<button type="button" className="secondary-button compact-button" onClick={() => void loadFiles()} disabled={loading || refreshing || querying}>
<RefreshCw size={16} />{t('quota.readList')}
</button>
<button type="button" className="secondary-button compact-button" onClick={() => void refreshAll()} disabled={refreshing || loading || files.length === 0}>
<button type="button" className="secondary-button compact-button" onClick={() => void refreshAll()} disabled={refreshing || loading || querying || files.length === 0}>
<RefreshCw size={16} className={refreshing ? 'spin' : ''} />{t('quota.refreshAll')}
</button>
</div>
@@ -190,6 +202,7 @@ export function QuotaPage() {
export function QuotaCard({ file, quota, onRefresh, onReset }: { file: AuthFile; quota: QuotaState; onRefresh: () => void; onReset?: () => void }) {
const { locale, t } = useI18n();
const now = useQuotaClock() + (quota.serverTimeOffsetMs ?? 0);
const provider = providerForFile(file);
const name = fileName(file);
const disabled = readBoolean(file, 'disabled');
@@ -199,8 +212,21 @@ export function QuotaCard({ file, quota, onRefresh, onReset }: { file: AuthFile;
{quota.status === 'idle' ? <div className="quota-card-message"><span>{disabled ? t('quota.fileDisabled') : t('quota.notFetched')}</span><button type="button" className="secondary-button compact-button" onClick={onRefresh} disabled={disabled}>{disabled ? t('quota.disabled') : t('quota.fetch')}</button></div> : null}
{quota.status === 'loading' ? <div className="quota-card-message"><LoaderCircle size={18} className="spin" />{t('quota.querying')}</div> : null}
{quota.status === 'error' ? <div className="quota-card-error"><AlertCircle size={18} />{quota.error}</div> : null}
{quota.status === 'success' && provider === 'codex' ? <div className="quota-reset-credit-summary"><span>{t('quota.resetCredits')} <strong>{quota.resetCredits ?? '—'}</strong></span><span>{t('quota.earliestExpiry')} <strong>{formatQuotaTimestamp(quota.resetCreditsEarliestExpiry, locale)}</strong></span></div> : null}
{quota.status === 'success' ? <div className="quota-row-list">{quota.rows.map((row, index) => <div className="real-quota-row" key={`${row.label}-${index}`}><div><span>{row.label}</span><strong>{row.remainingPercent === null ? '—' : t('quota.remaining', { percent: Math.round(row.remainingPercent) })}</strong></div><div className="real-quota-track"><span style={{ width: `${Math.max(0, Math.min(100, row.remainingPercent ?? 0))}%` }} /></div><small>{row.detail ?? ''}{row.reset ? `${row.detail ? ' · ' : ''}${row.reset}` : ''}</small></div>)}</div> : null}
{quota.status === 'success' && provider === 'codex' ? <div className="quota-reset-credit-summary">
<span>{t('quota.resetCredits')} <strong>{quota.resetCredits ?? '—'}</strong></span>
{quota.resetCreditsApplicable !== undefined ? <span>{t('quota.resetApplicable', { count: quota.resetCreditsApplicable })}</span> : null}
<span>{t('quota.earliestExpiry')} <strong>{formatQuotaTimestamp(quota.resetCreditsEarliestExpiry, locale)}</strong></span>
{quota.subscriptionActiveUntil ? <span>{t('quota.subscriptionExpiry', { time: formatQuotaTimestamp(quota.subscriptionActiveUntil, locale) })}</span> : null}
{quota.resetCreditsError ? <small>{t('quota.resetCreditsWarning', { error: quota.resetCreditsError })}</small> : null}
</div> : null}
{quota.status === 'success' ? <div className="quota-row-list">{quota.rows.map((row, index) => {
const reset = formatQuotaReset(row.resetAtMs, row.reset, locale, now);
return <div className="real-quota-row" key={`${row.label}-${index}`}>
<div><span>{row.label}</span><strong>{row.remainingPercent === null ? '—' : t('quota.remaining', { percent: Math.round(row.remainingPercent) })}</strong></div>
{row.remainingPercent !== null ? <div className="real-quota-track"><span style={{ width: `${Math.max(0, Math.min(100, row.remainingPercent))}%` }} /></div> : null}
<small>{[row.detail, reset].filter(Boolean).join(' · ')}</small>
</div>;
})}</div> : null}
</article>
);
}
+74
View File
@@ -0,0 +1,74 @@
import { isRecord, readString } from './managementApi';
export const decodeQuotaToken = (value: unknown): Record<string, unknown> | null => {
if (isRecord(value)) return value;
if (typeof value !== 'string' || !value.trim()) return null;
try {
const parsed = JSON.parse(value);
if (isRecord(parsed)) return parsed;
} catch { /* Try the JWT payload next. */ }
const segment = value.trim().split('.')[1];
if (!segment) return null;
try {
const normalized = segment.replace(/-/g, '+').replace(/_/g, '/')
.padEnd(Math.ceil(segment.length / 4) * 4, '=');
const bytes = Uint8Array.from(atob(normalized), (character) => character.charCodeAt(0));
const parsed = JSON.parse(new TextDecoder().decode(bytes));
return isRecord(parsed) ? parsed : null;
} catch {
return null;
}
};
export const quotaAuthRecords = (file: Record<string, unknown>) =>
[file, file.metadata, file.attributes].filter(isRecord);
export const codexMetadataFor = (file: Record<string, unknown>) => {
const records = quotaAuthRecords(file).flatMap((record) => {
const tokens = [record.id_token, record.idToken].flatMap((token) => {
const payload = decodeQuotaToken(token);
if (!payload) return [];
return [payload, payload['https://api.openai.com/auth']].filter(isRecord);
});
return [record, ...tokens];
});
const first = (...keys: string[]) => records.map((record) => readString(record, ...keys)).find(Boolean);
const expiry = first(
'chatgpt_subscription_active_until', 'chatgptSubscriptionActiveUntil',
'subscription_active_until', 'subscriptionActiveUntil',
) || records.map((record) => readString(record.subscription, 'active_until', 'activeUntil')).find(Boolean);
return {
accountId: first('chatgpt_account_id', 'chatgptAccountId', 'account_id', 'accountId') || '',
plan: first('plan_type', 'planType', 'chatgpt_plan_type', 'chatgptPlanType')?.toLowerCase(),
subscriptionActiveUntil: expiry && expiry !== '0' ? expiry : undefined,
};
};
export const antigravityProjectFor = (file: Record<string, unknown>): string => {
const records = [...quotaAuthRecords(file), file.installed, file.web].filter(isRecord);
return records.map((record) => readString(record, 'project_id', 'projectId', 'gemini_virtual_project'))
.find(Boolean) || '';
};
export const isPaidXaiFile = (file: Record<string, unknown>): boolean => {
const records: Record<string, unknown>[] = [];
const visited = new Set<Record<string, unknown>>();
const visit = (value: unknown, depth: number) => {
if (!isRecord(value) || visited.has(value) || depth > 2) return;
visited.add(value);
records.push(value);
for (const key of ['metadata', 'attributes', 'oauth', 'raw', 'credential', 'auth']) {
visit(value[key], depth + 1);
}
};
visit(file, 0);
const usingApi = records.some((record) =>
['true', '1', 'yes', 'y', 'on'].includes(String(record.using_api ?? record.usingApi).trim().toLowerCase()));
if (usingApi && records.some((record) => readString(record, 'prefix').toLowerCase() === 'paid')) return true;
return records.some((record) => ['access_token', 'accessToken', 'id_token', 'idToken', 'token'].some((key) => {
const payload = decodeQuotaToken(record[key]);
if (!payload) return false;
return Object.entries(payload).some(([claim, tier]) =>
/(?:^|[/:])tier$/i.test(claim) && (typeof tier === 'number' || typeof tier === 'string') && Number(tier) >= 1);
}));
};
+220 -123
View File
@@ -6,6 +6,8 @@ import {
readString,
} from './managementApi';
import { authFileName } from './authFiles';
import { antigravityProjectFor, codexMetadataFor, isPaidXaiFile } from './quotaMetadata';
import { quotaResetFor, quotaResetInstant } from './quotaTime';
import { getCurrentLocale, translate, type AppLocale } from '../i18n';
const quotaText = (
@@ -20,6 +22,7 @@ export type QuotaRow = {
label: string;
remainingPercent: number | null;
reset?: string;
resetAtMs?: number;
detail?: string;
};
export type QuotaState = {
@@ -28,7 +31,11 @@ export type QuotaState = {
error?: string;
plan?: string;
resetCredits?: number;
resetCreditsApplicable?: number;
resetCreditsError?: string;
resetCreditsEarliestExpiry?: string;
subscriptionActiveUntil?: string;
serverTimeOffsetMs?: number;
fetchedAt?: number;
};
@@ -60,7 +67,7 @@ const headersByProvider: Record<QuotaProvider, Record<string, string>> = {
codex: {
Authorization: 'Bearer $TOKEN$',
'Content-Type': 'application/json',
'User-Agent': 'codex_cli_rs/0.76.0 (Debian 13.0.0; x86_64) WindowsTerminal',
'User-Agent': 'codex-tui/0.149.1 (Mac OS 26.5.2; arm64) iTerm.app/3.6.11 (codex-tui; 0.149.1)',
},
kimi: { Authorization: 'Bearer $TOKEN$' },
xai: {
@@ -78,7 +85,8 @@ const headersByProvider: Record<QuotaProvider, Record<string, string>> = {
};
export const providerForFile = (file: AuthFile): QuotaProvider | null => {
const value = readString(file, 'provider', 'type', 'account_type').toLowerCase();
const value = readString(file, 'provider', 'type', 'account_type').toLowerCase().replace(/_/g, '-');
if (value === 'x-ai' || value === 'grok') return 'xai';
if (value === 'anthropic') return 'claude';
if (value === 'anti-gravity') return 'antigravity';
return ['claude', 'codex', 'kimi', 'xai', 'antigravity'].includes(value)
@@ -102,7 +110,7 @@ const parseBody = (value: unknown): unknown => {
const numberValue = (value: unknown): number | null => {
if (isRecord(value) && 'val' in value) return numberValue(value.val);
if (value === null || value === undefined || typeof value === 'boolean') return null;
if (typeof value !== 'number' && typeof value !== 'string') return null;
if (typeof value === 'string' && !value.trim()) return null;
const parsed = typeof value === 'number' ? value : Number(value);
return Number.isFinite(parsed) ? parsed : null;
@@ -141,8 +149,9 @@ const formatDateTime = (date: Date): string | undefined => {
export const formatQuotaTimestamp = (value: string | undefined, locale: AppLocale = 'zh-CN'): string => {
if (!value) return '—';
const date = new Date(value);
if (Number.isNaN(date.getTime())) return '—';
const ms = quotaResetInstant(value);
if (ms === undefined) return '—';
const date = new Date(ms);
return new Intl.DateTimeFormat(locale, {
year: 'numeric',
month: '2-digit',
@@ -154,13 +163,8 @@ export const formatQuotaTimestamp = (value: string | undefined, locale: AppLocal
};
const absoluteResetLabel = (value: unknown): string | undefined => {
if (value === null || value === undefined || value === '') return undefined;
const numeric = numberValue(value);
const numericText = typeof value === 'string' && /^\d+(?:\.\d+)?$/.test(value.trim());
const date = typeof value === 'number' || numericText
? new Date((numeric ?? 0) < 1e12 ? (numeric ?? 0) * 1000 : (numeric ?? 0))
: new Date(String(value));
return formatDateTime(date);
const ms = quotaResetInstant(value);
return ms === undefined ? undefined : formatDateTime(new Date(ms));
};
const relativeResetLabel = (value: unknown): string | undefined => {
@@ -213,7 +217,6 @@ const codexWindowLabel = (
duration: number | null,
prefix: string,
kind: 'primary' | 'secondary',
teamPlan: boolean,
) => {
if (duration === FIVE_HOUR_SECONDS) return `${prefix}${quotaText('quota.service.limit.fiveHours')}`;
if (duration === WEEK_SECONDS) return `${prefix}${quotaText('quota.service.limit.week')}`;
@@ -224,7 +227,7 @@ const codexWindowLabel = (
return `${prefix}${quotaText('quota.service.limit.duration', { duration: formatWindowDuration(duration) })}`;
}
if (kind === 'primary') return `${prefix}${quotaText('quota.service.limit.fiveHours')}`;
return `${prefix}${quotaText(teamPlan ? 'quota.service.limit.month' : 'quota.service.limit.week')}`;
return `${prefix}${quotaText('quota.service.limit.week')}`;
};
const codexWindowRows = (value: Record<string, unknown>): QuotaRow[] => {
@@ -236,10 +239,17 @@ const codexWindowRows = (value: Record<string, unknown>): QuotaRow[] => {
}> = [];
const addRateLimit = (rawRateLimit: unknown, prefix: string) => {
if (!isRecord(rawRateLimit)) return;
windows.push(
const entries: typeof windows = [
{ raw: rawRateLimit.primary_window ?? rawRateLimit.primaryWindow, kind: 'primary', prefix, source: rawRateLimit },
{ raw: rawRateLimit.secondary_window ?? rawRateLimit.secondaryWindow, kind: 'secondary', prefix, source: rawRateLimit },
);
];
const order = ({ raw, kind }: typeof entries[number]) => {
const duration = isRecord(raw) ? numberValue(raw.limit_window_seconds ?? raw.limitWindowSeconds) : null;
if (duration === FIVE_HOUR_SECONDS) return 0;
if (duration === WEEK_SECONDS || (duration !== null && duration >= MIN_MONTH_SECONDS && duration <= MAX_MONTH_SECONDS)) return 1;
return kind === 'primary' ? 0 : 1;
};
windows.push(...entries.sort((a, b) => order(a) - order(b)));
};
addRateLimit(value.rate_limit ?? value.rateLimit, '');
@@ -257,15 +267,17 @@ const codexWindowRows = (value: Record<string, unknown>): QuotaRow[] => {
});
}
const teamPlan = readString(value, 'plan_type', 'planType').toLowerCase() === 'team';
return windows.map(({ raw, kind, prefix, source }): QuotaRow | null => {
if (!isRecord(raw)) return null;
const duration = numberValue(raw.limit_window_seconds ?? raw.limitWindowSeconds);
const reached = source.limit_reached === true || source.limitReached === true || source.allowed === false;
const resetAtMs = quotaResetFor(raw, ['reset_at', 'resetAt'], ['reset_after_seconds', 'resetAfterSeconds']);
return {
label: codexWindowLabel(duration, prefix, kind, teamPlan),
remainingPercent: remainingFromUsedPercent(raw.used_percent ?? raw.usedPercent) ?? (reached ? 0 : null),
label: codexWindowLabel(duration, prefix, kind),
remainingPercent: remainingFromUsedPercent(raw.used_percent ?? raw.usedPercent)
?? (reached && resetAtMs !== undefined ? 0 : null),
reset: codexResetLabel(raw),
resetAtMs,
};
}).filter((row): row is QuotaRow => row !== null);
};
@@ -285,7 +297,7 @@ export const codexResetCreditsFor = (payload: unknown): number | undefined => {
export const codexResetCreditDetailsFor = (
payload: unknown,
nowMs = Date.now(),
): { availableCount?: number; earliestExpiry?: string } => {
): { availableCount?: number; applicableAvailableCount?: number; earliestExpiry?: string } => {
const value = parseBody(payload);
if (!isRecord(value)) return {};
const credits = Array.isArray(value.credits)
@@ -297,9 +309,14 @@ export const codexResetCreditDetailsFor = (
)
: [];
const availableCount = numberValue(value.available_count ?? value.availableCount);
const earliestExpiry = credits
const applicableCount = numberValue(value.applicable_available_count ?? value.applicableAvailableCount);
const validCredits = credits.filter((credit) => {
const expiry = quotaResetInstant(credit.expires_at ?? credit.expiresAt);
return expiry !== undefined && expiry > nowMs;
});
const earliestExpiry = validCredits
.map((credit) => readString(credit, 'expires_at', 'expiresAt'))
.map((expiresAt) => ({ expiresAt, expiresAtMs: new Date(expiresAt).getTime() }))
.map((expiresAt) => ({ expiresAt, expiresAtMs: quotaResetInstant(expiresAt) ?? NaN }))
.filter((credit) =>
credit.expiresAt
&& Number.isFinite(credit.expiresAtMs)
@@ -308,9 +325,11 @@ export const codexResetCreditDetailsFor = (
.sort((left, right) => left.expiresAtMs - right.expiresAtMs)[0]?.expiresAt;
return {
// An empty detail list is not an explicit zero; keep the usage-summary fallback.
availableCount: availableCount === null
? credits.length || undefined
? validCredits.length || undefined
: Math.max(0, Math.floor(availableCount)),
...(applicableCount === null ? {} : { applicableAvailableCount: Math.max(0, Math.floor(applicableCount)) }),
earliestExpiry,
};
};
@@ -329,19 +348,35 @@ export const quotaRowsFor = (provider: QuotaProvider, payload: unknown): QuotaRo
seven_day_opus: quotaText('quota.service.window.sevenDayOpus'),
seven_day_sonnet: quotaText('quota.service.window.sevenDaySonnet'),
seven_day_cowork: quotaText('quota.service.window.sevenDayCowork'),
iguana_necktie: 'Iguana Necktie',
iguana_necktie: quotaText('quota.service.window.sevenDayFable'),
};
const rows = Object.entries(value)
.filter(([key]) => key in labels)
.map(([key, raw]): QuotaRow | null => {
if (!isRecord(raw)) return null;
const fableCandidates = (Array.isArray(value.limits) ? value.limits : []).filter(isRecord)
.filter((limit) => {
const scope = isRecord(limit.scope) ? limit.scope : null;
const name = readString(scope?.model, 'display_name', 'displayName').toLowerCase();
return readString(limit, 'kind').toLowerCase() === 'weekly_scoped'
&& ['fable', 'fable 5'].includes(name) && numberValue(limit.percent) !== null;
});
const fable = fableCandidates.find((limit) => limit.is_active === true) ?? fableCandidates[0];
const rows = Object.entries(labels)
.filter(([key]) => key !== 'iguana_necktie' || !fable)
.map(([key]): QuotaRow | null => {
const raw = value[key];
if (!isRecord(raw) || !('utilization' in raw)) return null;
return {
label: labels[key],
remainingPercent: remainingFromUsedPercent(raw.utilization),
reset: absoluteResetLabel(raw.resets_at ?? raw.resetsAt),
resetAtMs: quotaResetFor(raw, ['resets_at', 'resetsAt']),
};
})
.filter((row): row is QuotaRow => row !== null);
if (fable) rows.push({
label: quotaText('quota.service.window.sevenDayFable'),
remainingPercent: remainingFromUsedPercent(fable.percent),
reset: absoluteResetLabel(fable.resets_at ?? fable.resetsAt),
resetAtMs: quotaResetFor(fable, ['resets_at', 'resetsAt']),
});
const extraUsage = isRecord(value.extra_usage)
? value.extra_usage
: isRecord(value.extraUsage)
@@ -369,11 +404,10 @@ export const quotaRowsFor = (provider: QuotaProvider, payload: unknown): QuotaRo
}
if (provider === 'kimi') {
const items: unknown[] = [];
const items: unknown[] = Array.isArray(value.limits) ? [...value.limits] : [];
if (isRecord(value.usage)) {
items.push({ ...value.usage, label: quotaText('quota.service.weekly') });
items.push({ ...value.usage, label: readString(value.usage, 'name', 'title') || quotaText('quota.service.weekly') });
}
if (Array.isArray(value.limits)) items.push(...value.limits);
return items
.map((raw, index): QuotaRow | null => {
if (!isRecord(raw)) return null;
@@ -385,19 +419,21 @@ export const quotaRowsFor = (provider: QuotaProvider, payload: unknown): QuotaRo
if (usedValue === null && limit === null) return null;
const window = isRecord(raw.window) ? raw.window : null;
const duration = numberValue(window?.duration ?? raw.duration ?? detail.duration);
const unit = readString(window, 'timeUnit', 'time_unit')
const unit = (readString(window, 'timeUnit', 'time_unit')
|| readString(raw, 'timeUnit', 'time_unit')
|| readString(detail, 'timeUnit', 'time_unit');
|| readString(detail, 'timeUnit', 'time_unit')).toLowerCase().replace(/^time_unit_/, '');
const durationText = duration !== null && duration > 0
? unit.toLowerCase().startsWith('day')
? quotaText('quota.service.duration.days', { count: duration })
: unit.toLowerCase().startsWith('hour')
? quotaText('quota.service.duration.hours', { count: duration })
: unit.toLowerCase().startsWith('second')
? quotaText('quota.service.duration.seconds', { count: duration })
: duration % 60 === 0
? quotaText('quota.service.duration.hours', { count: duration / 60 })
: quotaText('quota.service.duration.minutes', { count: duration })
? unit.startsWith('week')
? quotaText('quota.service.duration.days', { count: duration * 7 })
: unit.startsWith('day')
? quotaText('quota.service.duration.days', { count: duration })
: unit.startsWith('hour')
? quotaText('quota.service.duration.hours', { count: duration })
: unit.startsWith('second')
? quotaText('quota.service.duration.seconds', { count: duration })
: duration % 60 === 0
? quotaText('quota.service.duration.hours', { count: duration / 60 })
: quotaText('quota.service.duration.minutes', { count: duration })
: '';
const durationLabel = durationText
? quotaText('quota.service.window.duration', { duration: durationText })
@@ -420,6 +456,7 @@ export const quotaRowsFor = (provider: QuotaProvider, payload: unknown): QuotaRo
detail.reset_at ?? detail.resetAt ?? detail.reset_time ?? detail.resetTime,
)
?? relativeResetLabel(detail.reset_in ?? detail.resetIn ?? detail.ttl),
resetAtMs: quotaResetFor(detail, ['reset_at', 'resetAt', 'reset_time', 'resetTime'], ['reset_in', 'resetIn', 'ttl']),
detail: limit === null ? undefined : `${usedValue ?? 0} / ${limit}`,
};
})
@@ -427,6 +464,11 @@ export const quotaRowsFor = (provider: QuotaProvider, payload: unknown): QuotaRo
}
if (provider === 'xai') {
if (value.mode === 'paid-health') return [{
label: quotaText('quota.service.xaiPaidHealth'),
remainingPercent: null,
detail: quotaText('quota.service.xaiPaidHealthDetail'),
}];
const payloads = isRecord(value.weekly) || isRecord(value.monthly)
? [value.weekly, value.monthly]
: [value];
@@ -443,11 +485,13 @@ export const quotaRowsFor = (provider: QuotaProvider, payload: unknown): QuotaRo
const periodType = readString(currentPeriod, 'type').toLowerCase();
const weeklyUsed = numberValue(config.credit_usage_percent ?? config.creditUsagePercent);
if (weeklyUsed !== null || periodType.includes('week')) {
const productItems = config.product_usage ?? config.productUsage;
if (weeklyUsed !== null || periodType.includes('week') || (Array.isArray(productItems) && productItems.length > 0)) {
rows.push({
label: quotaText('quota.service.weekly'),
remainingPercent: remainingFromUsedPercent(weeklyUsed),
reset: absoluteResetLabel(currentPeriod?.end),
resetAtMs: currentPeriod ? quotaResetFor(currentPeriod, ['end']) : undefined,
});
const productUsage = Array.isArray(config.product_usage)
? config.product_usage
@@ -463,6 +507,7 @@ export const quotaRowsFor = (provider: QuotaProvider, payload: unknown): QuotaRo
item.usage_percent ?? item.usagePercent,
),
reset: absoluteResetLabel(currentPeriod?.end),
resetAtMs: currentPeriod ? quotaResetFor(currentPeriod, ['end']) : undefined,
});
});
}
@@ -482,6 +527,7 @@ export const quotaRowsFor = (provider: QuotaProvider, payload: unknown): QuotaRo
label: quotaText('quota.service.monthlyIncluded'),
remainingPercent: clampPercent(remaining),
reset: absoluteResetLabel(config.billing_period_end ?? config.billingPeriodEnd),
resetAtMs: quotaResetFor(config, ['billing_period_end', 'billingPeriodEnd']),
detail: limit !== null
? `${formatUsdFromCents(Math.max(0, limit - (includedUsed ?? 0)))} / ${formatUsdFromCents(limit)}`
: undefined,
@@ -499,6 +545,7 @@ export const quotaRowsFor = (provider: QuotaProvider, payload: unknown): QuotaRo
onDemandUsed === null ? null : ((onDemandCap - onDemandUsed) / onDemandCap) * 100,
),
reset: absoluteResetLabel(config.billing_period_end ?? config.billingPeriodEnd),
resetAtMs: quotaResetFor(config, ['billing_period_end', 'billingPeriodEnd']),
detail: `${formatUsdFromCents(Math.max(0, onDemandCap - (onDemandUsed ?? 0)))} / ${formatUsdFromCents(onDemandCap)}`,
});
}
@@ -513,10 +560,16 @@ export const quotaRowsFor = (provider: QuotaProvider, payload: unknown): QuotaRo
});
}
const groups = Array.isArray(value.groups) ? value.groups : [];
const nested = parseBody(value.body);
const summary = !Array.isArray(value.groups) && isRecord(nested) ? nested : value;
const groups = Array.isArray(summary.groups) ? summary.groups : [];
return groups.flatMap((group) => {
if (!isRecord(group) || !Array.isArray(group.buckets)) return [];
const buckets = group.buckets;
const order = (bucket: unknown) => {
const window = readString(bucket, 'window').toLowerCase();
return ['5h', 'five-hour', 'five_hour'].includes(window) ? 0 : ['weekly', 'week'].includes(window) ? 1 : 2;
};
const buckets = [...group.buckets].sort((a, b) => order(a) - order(b));
const groupLabel = readString(group, 'display_name', 'displayName')
|| quotaText('quota.service.quota');
const groupDescription = readString(group, 'description');
@@ -533,6 +586,7 @@ export const quotaRowsFor = (provider: QuotaProvider, payload: unknown): QuotaRo
label: label || quotaText('quota.service.quota.numbered', { index: index + 1 }),
remainingPercent: remaining * 100,
reset: absoluteResetLabel(bucket.reset_time ?? bucket.resetTime),
resetAtMs: quotaResetFor(bucket, ['reset_time', 'resetTime']),
detail: readString(bucket, 'description') || groupDescription || undefined,
};
})
@@ -540,66 +594,18 @@ export const quotaRowsFor = (provider: QuotaProvider, payload: unknown): QuotaRo
});
};
const resolveProjectId = (file: AuthFile): string =>
readString(file, 'project_id', 'projectId');
const decodeJwtPayload = (value: unknown): Record<string, unknown> | null => {
if (isRecord(value)) return value;
if (typeof value !== 'string') return null;
const trimmed = value.trim();
if (!trimmed) return null;
try {
const parsed = JSON.parse(trimmed);
if (isRecord(parsed)) return parsed;
} catch {
// Continue with JWT payload decoding.
}
const segment = trimmed.split('.')[1];
if (!segment) return null;
try {
const normalized = segment.replace(/-/g, '+').replace(/_/g, '/').padEnd(Math.ceil(segment.length / 4) * 4, '=');
const parsed = JSON.parse(atob(normalized));
return isRecord(parsed) ? parsed : null;
} catch {
return null;
}
};
const codexAccountIdFromRecord = (record: Record<string, unknown>): string => {
const direct = readString(
record,
'chatgpt_account_id',
'chatgptAccountId',
'account_id',
'accountId',
);
const resolveProjectId = async (file: AuthFile): Promise<string> => {
const direct = antigravityProjectFor(file);
if (direct) return direct;
for (const container of [record.metadata, record.attributes]) {
if (!isRecord(container)) continue;
const nestedDirect = readString(
container,
'chatgpt_account_id',
'chatgptAccountId',
'account_id',
'accountId',
);
if (nestedDirect) return nestedDirect;
try {
const payload = parseBody(await managementApi.get('/auth-files/download', { name: fileName(file) }));
return isRecord(payload) ? antigravityProjectFor(payload) : '';
} catch {
return '';
}
for (const candidate of [record.id_token, record.idToken]) {
const payload = decodeJwtPayload(candidate);
const nested = payload && isRecord(payload['https://api.openai.com/auth']) ? payload['https://api.openai.com/auth'] : payload;
const accountId = nested
? readString(nested, 'chatgpt_account_id', 'chatgptAccountId', 'account_id', 'accountId')
: '';
if (accountId) return accountId;
}
return '';
};
const resolveCodexAccountId = (file: AuthFile): string =>
codexAccountIdFromRecord(file);
const resolveCodexAccountId = (file: AuthFile): string => codexMetadataFor(file).accountId;
const xaiUserIdFromRecord = (record: Record<string, unknown>): string => {
const nestedRecords = [record, record.metadata, record.attributes]
@@ -625,6 +631,8 @@ const requestQuotaPayload = async (
header: Record<string, string>,
method: 'GET' | 'POST' = 'GET',
data?: string,
timeoutMs?: number,
responseClock?: { serverTimeOffsetMs?: number },
) => {
const response = await managementApi.post<Record<string, unknown>>('/api-call', {
authIndex,
@@ -632,17 +640,44 @@ const requestQuotaPayload = async (
url,
header,
data,
});
}, { timeoutMs });
const status = Number(response.status_code ?? response.statusCode ?? 0);
if (status < 200 || status >= 300) {
throw new Error(apiCallErrorMessage(response));
}
if (responseClock) {
const header = isRecord(response.header) ? response.header : {};
const raw = Object.entries(header).find(([key]) => key.toLowerCase() === 'date')?.[1];
const serverTime = quotaResetInstant(Array.isArray(raw) ? raw[0] : raw);
responseClock.serverTimeOffsetMs = serverTime === undefined ? undefined : serverTime - Date.now();
}
return parseBody(response.body ?? response.bodyText);
};
const callXaiQuota = async (file: AuthFile): Promise<unknown> => {
export type QuotaLoadOptions = {
// Paid health checks send a real (potentially billable) completion. Never do this silently.
confirmXaiPaidProbe?: () => boolean | Promise<boolean>;
};
const callXaiPaidHealth = async (authIndex: string, options: QuotaLoadOptions): Promise<unknown> => {
if (!await options.confirmXaiPaidProbe?.()) throw new Error(quotaText('quota.service.error.xaiProbeRequired'));
const header = { Authorization: 'Bearer $TOKEN$', accept: 'application/json' };
const [profile, chat] = await Promise.allSettled([
requestQuotaPayload(authIndex, 'https://api.x.ai/v1/me', header, 'GET', undefined, 15_000),
requestQuotaPayload(authIndex, 'https://api.x.ai/v1/chat/completions', {
...header, 'Content-Type': 'application/json',
}, 'POST', JSON.stringify({
model: 'grok-4.5', messages: [{ role: 'user', content: 'ping' }], max_tokens: 1, stream: false,
}), 15_000),
]);
if (chat.status === 'rejected') throw chat.reason;
return { mode: 'paid-health', plan_type: 'Paid', profile: profile.status === 'fulfilled' ? profile.value : null };
};
const callXaiQuota = async (file: AuthFile, options: QuotaLoadOptions): Promise<unknown> => {
const authIndex = normalizeAuthIndex(file.auth_index ?? file.authIndex);
if (!authIndex) throw new Error(quotaText('quota.service.error.missingAuthIndex'));
if (isPaidXaiFile(file)) return callXaiPaidHealth(authIndex, options);
const header = { ...headersByProvider.xai };
const userId = await resolveXaiUserId(file);
if (userId) header['x-userid'] = userId;
@@ -650,22 +685,28 @@ const callXaiQuota = async (file: AuthFile): Promise<unknown> => {
requestQuotaPayload(authIndex, XAI_WEEKLY_URL, header),
requestQuotaPayload(authIndex, endpointByProvider.xai, header),
]);
if (weekly.status === 'rejected' && monthly.status === 'rejected') {
throw weekly.reason;
}
return {
const payload = {
weekly: weekly.status === 'fulfilled' ? weekly.value : null,
monthly: monthly.status === 'fulfilled' ? monthly.value : null,
};
if (quotaRowsFor('xai', payload).length > 0) return payload;
const billingError = weekly.status === 'rejected' && monthly.status === 'rejected'
? weekly.reason : new Error(quotaText('quota.service.error.unrecognized'));
try {
return await callXaiPaidHealth(authIndex, options);
} catch {
// Keep the useful billing error if the alternative account mode also fails or is declined.
throw billingError;
}
};
const booleanValue = (value: unknown): boolean | null => {
if (typeof value === 'boolean') return value;
if (typeof value === 'number') return value !== 0;
if (typeof value === 'number') return Number.isFinite(value) ? value !== 0 : null;
if (typeof value === 'string') {
const normalized = value.trim().toLowerCase();
if (['true', '1', 'yes', 'on'].includes(normalized)) return true;
if (['false', '0', 'no', 'off'].includes(normalized)) return false;
if (['true', '1', 'yes', 'y', 'on'].includes(normalized)) return true;
if (['false', '0', 'no', 'n', 'off'].includes(normalized)) return false;
}
return null;
};
@@ -740,6 +781,7 @@ async function callUpstreamQuota(
file: AuthFile,
provider: QuotaProvider,
resolvedCodexAccountId?: string,
responseClock?: { serverTimeOffsetMs?: number },
): Promise<unknown> {
const authIndex = normalizeAuthIndex(file.auth_index ?? file.authIndex);
if (!authIndex) throw new Error(quotaText('quota.service.error.missingAuthIndex'));
@@ -765,6 +807,8 @@ async function callUpstreamQuota(
header,
provider === 'antigravity' ? 'POST' : 'GET',
project ? JSON.stringify({ project }) : undefined,
undefined,
provider === 'antigravity' ? responseClock : undefined,
);
if (provider === 'antigravity') {
hadSuccessfulResponse = true;
@@ -790,7 +834,7 @@ async function callUpstreamQuota(
const callCodexResetCredits = async (
file: AuthFile,
accountId: string,
): Promise<{ availableCount?: number; earliestExpiry?: string }> => {
): Promise<ReturnType<typeof codexResetCreditDetailsFor>> => {
const authIndex = normalizeAuthIndex(file.auth_index ?? file.authIndex);
if (!authIndex) throw new Error(quotaText('quota.service.error.missingResetAuthIndex'));
const header: Record<string, string> = {
@@ -800,11 +844,14 @@ const callCodexResetCredits = async (
Originator: 'Codex Desktop',
};
if (accountId) header['Chatgpt-Account-Id'] = accountId;
const payload = await requestQuotaPayload(authIndex, CODEX_RESET_CREDITS_URL, header);
const payload = await requestQuotaPayload(authIndex, CODEX_RESET_CREDITS_URL, header, 'GET', undefined, 8_000);
if (!isRecord(payload) || !['credits', 'available_count', 'availableCount', 'applicable_available_count', 'applicableAvailableCount'].some((key) => key in payload)) {
throw new Error(quotaText('quota.service.error.resetCreditsInvalid'));
}
return codexResetCreditDetailsFor(payload);
};
export async function loadQuota(file: AuthFile): Promise<QuotaState> {
async function loadQuotaSnapshot(file: AuthFile, options: QuotaLoadOptions = {}): Promise<QuotaState> {
const provider = providerForFile(file);
if (!provider) {
return {
@@ -814,17 +861,24 @@ export async function loadQuota(file: AuthFile): Promise<QuotaState> {
};
}
try {
const codexAccountId = provider === 'codex' ? await resolveCodexAccountId(file) : '';
if (booleanValue(file.disabled) === true) throw new Error(quotaText('quota.fileDisabled'));
const codexMetadata = provider === 'codex' ? codexMetadataFor(file) : undefined;
const codexAccountId = codexMetadata?.accountId || '';
const responseClock: { serverTimeOffsetMs?: number } = {};
const payloadPromise = provider === 'xai'
? callXaiQuota(file)
: callUpstreamQuota(file, provider, codexAccountId);
? callXaiQuota(file, options)
: callUpstreamQuota(file, provider, codexAccountId, responseClock);
const planPromise = provider === 'claude'
? loadClaudePlan(file)
: provider === 'antigravity'
? loadAntigravityPlan(file)
: Promise.resolve(undefined);
let resetCreditsError: string | undefined;
const resetCreditsPromise = provider === 'codex'
? callCodexResetCredits(file, codexAccountId).catch(() => null)
? callCodexResetCredits(file, codexAccountId).catch((error) => {
resetCreditsError = error instanceof Error ? error.message : String(error);
return null;
})
: Promise.resolve(null);
const [payload, detectedPlan, resetCreditDetails] = await Promise.all([
payloadPromise,
@@ -839,15 +893,24 @@ export async function loadQuota(file: AuthFile): Promise<QuotaState> {
error: quotaText('quota.service.error.unrecognized'),
};
}
const resetCredits = provider === 'codex'
? resetCreditDetails?.availableCount ?? codexResetCreditsFor(payload)
: undefined;
const usageCreditDetails = provider === 'codex' && isRecord(payload)
? codexResetCreditDetailsFor(payload.rate_limit_reset_credits ?? payload.rateLimitResetCredits)
: {};
return {
status: 'success',
rows,
plan: detectedPlan
?? readString(isRecord(payload) ? payload : {}, 'plan_type', 'planType'),
resetCredits: provider === 'codex'
? resetCreditDetails?.availableCount ?? codexResetCreditsFor(payload)
: undefined,
?? (readString(isRecord(payload) ? payload : {}, 'plan_type', 'planType') || codexMetadata?.plan),
subscriptionActiveUntil: codexMetadata?.subscriptionActiveUntil,
resetCreditsError,
resetCreditsApplicable: usageCreditDetails.applicableAvailableCount
?? resetCreditDetails?.applicableAvailableCount ?? resetCredits,
resetCredits,
resetCreditsEarliestExpiry: resetCreditDetails?.earliestExpiry,
serverTimeOffsetMs: responseClock.serverTimeOffsetMs,
fetchedAt: Date.now(),
};
} catch (error) {
@@ -868,10 +931,11 @@ const createRedeemRequestId = () => {
});
};
export async function consumeCodexResetCredit(file: AuthFile): Promise<QuotaState> {
async function consumeCodexResetCreditSnapshot(file: AuthFile): Promise<QuotaState> {
if (providerForFile(file) !== 'codex') {
throw new Error(quotaText('quota.service.error.codexResetOnly'));
}
if (booleanValue(file.disabled) === true) throw new Error(quotaText('quota.fileDisabled'));
const authIndex = normalizeAuthIndex(file.auth_index ?? file.authIndex);
if (!authIndex) throw new Error(quotaText('quota.service.error.missingConsumeAuthIndex'));
const header = {
@@ -886,5 +950,38 @@ export async function consumeCodexResetCredit(file: AuthFile): Promise<QuotaStat
'POST',
JSON.stringify({ redeem_request_id: createRedeemRequestId() }),
);
return loadQuota(file);
return loadQuotaSnapshot(file);
}
// Shared by both pages: deduplicate refreshes and serialize reset mutations per credential.
const quotaRequests = new Map<string, Promise<QuotaState>>();
const resetRequests = new Map<string, Promise<QuotaState>>();
export function loadQuota(file: AuthFile, options: QuotaLoadOptions = {}): Promise<QuotaState> {
const key = quotaKey(file);
const reset = resetRequests.get(key);
if (reset) return reset.catch((error): QuotaState => ({
status: 'error', rows: [], error: error instanceof Error ? error.message : String(error),
}));
const existing = quotaRequests.get(key);
if (existing) return existing;
const request = loadQuotaSnapshot(file, options).finally(() => {
if (quotaRequests.get(key) === request) quotaRequests.delete(key);
});
quotaRequests.set(key, request);
return request;
}
export function consumeCodexResetCredit(file: AuthFile): Promise<QuotaState> {
const key = quotaKey(file);
const existing = resetRequests.get(key);
if (existing) return existing;
const request = (async () => {
await quotaRequests.get(key);
return consumeCodexResetCreditSnapshot(file);
})().finally(() => {
if (resetRequests.get(key) === request) resetRequests.delete(key);
});
resetRequests.set(key, request);
return request;
}
+76
View File
@@ -0,0 +1,76 @@
import { useSyncExternalStore } from 'react';
import { getCurrentLocale, translate, type AppLocale } from '../i18n';
export const quotaResetInstant = (value: unknown): number | undefined => {
if (typeof value !== 'number' && typeof value !== 'string') return undefined;
if (typeof value === 'string' && !value.trim()) return undefined;
const numeric = Number(value);
const ms = Number.isFinite(numeric)
? numeric > 0 ? numeric < 1e12 ? numeric * 1000 : numeric : NaN
: Date.parse(String(value));
return Number.isFinite(ms) && Number.isFinite(new Date(ms).getTime()) ? ms : undefined;
};
export const quotaResetFor = (
record: Record<string, unknown>,
absoluteKeys: string[],
relativeKeys: string[] = [],
nowMs = Date.now(),
): number | undefined => {
for (const key of absoluteKeys) {
const ms = quotaResetInstant(record[key]);
if (ms !== undefined) return ms;
}
for (const key of relativeKeys) {
const raw = record[key];
if (typeof raw !== 'number' && typeof raw !== 'string') continue;
if (typeof raw === 'string' && !raw.trim()) continue;
const seconds = Number(raw);
const ms = nowMs + seconds * 1000;
if (Number.isFinite(seconds) && seconds >= 0 && Number.isFinite(new Date(ms).getTime())) return ms;
}
return undefined;
};
export const formatQuotaReset = (
resetAtMs: number | undefined,
fallback?: string,
locale: AppLocale = getCurrentLocale(),
nowMs = Date.now(),
): string => {
if (resetAtMs === undefined || !Number.isFinite(resetAtMs)) return fallback || '';
const absolute = new Intl.DateTimeFormat(locale, {
month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', hour12: false,
}).format(resetAtMs);
const delta = resetAtMs - nowMs;
if (delta <= 0) return `${absolute} · ${translate(locale, 'quota.resetPassed')}`;
const minutes = Math.max(1, Math.ceil(delta / 60000));
const relative = new Intl.RelativeTimeFormat(locale, { numeric: 'always' });
const label = minutes >= 1440 ? relative.format(Math.ceil(minutes / 1440), 'day')
: minutes >= 60 ? relative.format(Math.ceil(minutes / 60), 'hour')
: relative.format(minutes, 'minute');
return `${absolute} · ${label}`;
};
// One clock shared by quota cards on both pages; no polling of upstream APIs.
let now = Date.now();
let timer: ReturnType<typeof setInterval> | undefined;
const listeners = new Set<() => void>();
const subscribe = (listener: () => void) => {
listeners.add(listener);
if (!timer) {
now = Date.now();
timer = setInterval(() => {
now = Date.now();
listeners.forEach((notify) => notify());
}, 60_000);
}
return () => {
listeners.delete(listener);
if (!listeners.size) {
clearInterval(timer);
timer = undefined;
}
};
};
export const useQuotaClock = () => useSyncExternalStore(subscribe, () => now, () => now);