fix: giảm spam 429 từ Claude OAuth usage endpoint

- claudeAutoPing: cache resetAt in-mem, bỏ qua poll usage cho tới gần reset
- ProviderLimits: throttle auto-refresh Claude 3 phút, nút bấm tay vẫn refresh ngay
- claude.js: 429 ở OAuth usage → cooldown 3 phút, fallback legacy (không ảnh hưởng chat)

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
decolua
2026-06-17 11:42:20 +07:00
co-authored by Cursor
parent da667836cc
commit 79df34cad7
4 changed files with 40 additions and 8 deletions
+16 -3
View File
@@ -14,11 +14,19 @@ const CLAUDE_CONFIG = {
apiVersion: ANTHROPIC_API_VERSION,
};
/**
* Claude Usage - Primary: OAuth endpoint, Fallback: legacy settings/org endpoint
*/
// OAuth usage endpoint rate-limits (429); cool down per-token to stop hammering it.
// Only the quota endpoint is affected — chat with the same token still works.
const OAUTH_429_COOLDOWN_MS = 180000;
const oauthCooldown = new Map();
export async function getClaudeUsage(accessToken, proxyOptions = null) {
try {
// Skip OAuth usage call while this token is cooling down from a recent 429
const cooldownUntil = oauthCooldown.get(accessToken);
if (cooldownUntil && Date.now() < cooldownUntil) {
return await getClaudeUsageLegacy(accessToken, proxyOptions);
}
// Primary: OAuth usage endpoint (Claude Code consumer OAuth tokens)
const oauthResponse = await proxyAwareFetch(CLAUDE_CONFIG.oauthUsageUrl, {
method: "GET",
@@ -73,6 +81,11 @@ export async function getClaudeUsage(accessToken, proxyOptions = null) {
};
}
// Cool down OAuth usage polling after a 429 (quota endpoint only)
if (oauthResponse.status === 429) {
oauthCooldown.set(accessToken, Date.now() + OAUTH_429_COOLDOWN_MS);
}
// Fallback: legacy settings + org usage endpoint
console.warn(`[Claude Usage] OAuth endpoint returned ${oauthResponse.status}, falling back to legacy`);
return await getClaudeUsageLegacy(accessToken, proxyOptions);