@hashimkarim/usagestat-darwin-arm64 2.0.0-alpha.3 → 2.3.2
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/native-manifest.json +1532 -134
- package/package.json +235 -2
- package/plugins/UPSTREAM-LICENSES.md +6 -2
- package/plugins/_provider-icons/LICENSE +21 -0
- package/plugins/_provider-icons/NOTICE +12 -0
- package/plugins/_provider-icons/licenses/CodexBar-MIT.txt +21 -0
- package/plugins/_provider-icons/licenses/LobeHub-MIT.txt +21 -0
- package/plugins/_provider-icons/licenses/UsageStat-Bar-MIT.txt +21 -0
- package/plugins/_provider-icons/manifest.json +1219 -0
- package/plugins/_provider-icons/provenance.json +269 -0
- package/plugins/_provider-icons/source.json +4 -0
- package/plugins/abacus-ai/icon-color.svg +18 -0
- package/plugins/abliteration/LICENSE +21 -0
- package/plugins/abliteration/icon.svg +5 -0
- package/plugins/abliteration/plugin.js +89 -0
- package/plugins/abliteration/plugin.json +24 -0
- package/plugins/aerostack/LICENSE +21 -0
- package/plugins/aerostack/icon.svg +1 -0
- package/plugins/aerostack/plugin.js +94 -0
- package/plugins/aerostack/plugin.json +24 -0
- package/plugins/aixy/LICENSE +21 -0
- package/plugins/aixy/icon.svg +1 -0
- package/plugins/aixy/plugin.js +266 -0
- package/plugins/aixy/plugin.json +32 -0
- package/plugins/antigravity/plugin.js +189 -65
- package/plugins/antigravity-cli/icon-color.svg +1 -0
- package/plugins/antigravity-cli/icon.svg +1 -3
- package/plugins/antigravity-cli/plugin.js +32 -20
- package/plugins/antigravity-ide/icon-color.svg +1 -0
- package/plugins/antigravity-ide/icon.svg +1 -3
- package/plugins/antigravity-ide/plugin.js +19 -7
- package/plugins/atlascloud/LICENSE +21 -0
- package/plugins/atlascloud/icon-color.svg +1 -0
- package/plugins/atlascloud/icon.svg +1 -0
- package/plugins/atlascloud/plugin.js +54 -0
- package/plugins/atlascloud/plugin.json +24 -0
- package/plugins/augment/icon-color.svg +14 -0
- package/plugins/bifrost/LICENSE +21 -0
- package/plugins/bifrost/icon.svg +3 -0
- package/plugins/bifrost/plugin.js +366 -0
- package/plugins/bifrost/plugin.json +31 -0
- package/plugins/chatbox/LICENSE +21 -0
- package/plugins/chatbox/icon.svg +4 -0
- package/plugins/chatbox/plugin.js +208 -0
- package/plugins/chatbox/plugin.json +24 -0
- package/plugins/claude/plugin.js +340 -153
- package/plugins/claude/plugin.json +1 -1
- package/plugins/claudeplatform/LICENSE +21 -0
- package/plugins/claudeplatform/icon.svg +3 -0
- package/plugins/claudeplatform/plugin.js +244 -0
- package/plugins/claudeplatform/plugin.json +25 -0
- package/plugins/clinepass/plugin.js +19 -3
- package/plugins/codebuff/icon-color.svg +92 -0
- package/plugins/coderabbit/plugin.js +25 -0
- package/plugins/coderabbit/plugin.json +12 -0
- package/plugins/codex/plugin.js +15 -8
- package/plugins/codex/plugin.json +11 -2
- package/plugins/command-code/icon-color.svg +3 -0
- package/plugins/command-code/plugin.js +7 -3
- package/plugins/copilot/icon.svg +1 -1
- package/plugins/copilot/plugin.json +1 -0
- package/plugins/cosmic/LICENSE +21 -0
- package/plugins/cosmic/icon.svg +1 -0
- package/plugins/cosmic/plugin.js +134 -0
- package/plugins/cosmic/plugin.json +32 -0
- package/plugins/crof/icon-color.svg +3 -0
- package/plugins/crofai/icon-color.svg +3 -0
- package/plugins/crofai/icon.svg +2 -15
- package/plugins/cursor/icon-color.svg +1 -0
- package/plugins/cursor/plugin.js +51 -14
- package/plugins/cursor-nightly/plugin.json +1 -1
- package/plugins/deepinfra/icon-color.svg +1 -0
- package/plugins/deepinfra/icon.svg +1 -4
- package/plugins/deepseek/plugin.js +150 -59
- package/plugins/deepseek/plugin.json +3 -1
- package/plugins/devin/plugin.js +3 -0
- package/plugins/devpass/LICENSE +21 -0
- package/plugins/devpass/icon.svg +3 -0
- package/plugins/devpass/plugin.js +120 -0
- package/plugins/devpass/plugin.json +24 -0
- package/plugins/elevenlabs/icon-color.svg +4 -0
- package/plugins/exa/LICENSE +21 -0
- package/plugins/exa/icon-color.svg +1 -0
- package/plugins/exa/icon.svg +1 -0
- package/plugins/exa/plugin.js +75 -0
- package/plugins/exa/plugin.json +32 -0
- package/plugins/factory/icon-color.svg +1 -0
- package/plugins/fal/LICENSE +21 -0
- package/plugins/fal/icon-color.svg +1 -0
- package/plugins/fal/icon.svg +1 -0
- package/plugins/fal/plugin.js +74 -0
- package/plugins/fal/plugin.json +24 -0
- package/plugins/fireworks-ai/icon-color.svg +1 -0
- package/plugins/fireworks-ai/icon.svg +1 -6
- package/plugins/flora/LICENSE +21 -0
- package/plugins/flora/icon.svg +4 -0
- package/plugins/flora/plugin.js +93 -0
- package/plugins/flora/plugin.json +32 -0
- package/plugins/futureagi/LICENSE +21 -0
- package/plugins/futureagi/icon.svg +24 -0
- package/plugins/futureagi/plugin.js +139 -0
- package/plugins/futureagi/plugin.json +32 -0
- package/plugins/gemini-apps/LICENSE +21 -0
- package/plugins/gemini-apps/icon-color.svg +1 -0
- package/plugins/gemini-apps/icon.svg +1 -0
- package/plugins/gemini-apps/plugin.js +141 -0
- package/plugins/gemini-apps/plugin.json +25 -0
- package/plugins/gitkraken/LICENSE +21 -0
- package/plugins/gitkraken/icon.svg +2 -0
- package/plugins/gitkraken/plugin.js +158 -0
- package/plugins/gitkraken/plugin.json +32 -0
- package/plugins/googledeveloper/LICENSE +21 -0
- package/plugins/googledeveloper/icon.svg +3 -0
- package/plugins/googledeveloper/plugin.js +204 -0
- package/plugins/googledeveloper/plugin.json +25 -0
- package/plugins/grok/icon-color.svg +1 -0
- package/plugins/groqcloud/icon-color.svg +1 -0
- package/plugins/helmcode/LICENSE +21 -0
- package/plugins/helmcode/icon.svg +5 -0
- package/plugins/helmcode/plugin.js +253 -0
- package/plugins/helmcode/plugin.json +32 -0
- package/plugins/huggingface/LICENSE +21 -0
- package/plugins/huggingface/icon-color.svg +1 -0
- package/plugins/huggingface/icon.svg +1 -0
- package/plugins/huggingface/plugin.js +298 -0
- package/plugins/huggingface/plugin.json +26 -0
- package/plugins/hyper/LICENSE +21 -0
- package/plugins/hyper/plugin.js +137 -0
- package/plugins/hyper/plugin.json +27 -0
- package/plugins/jetbrains-ai-assistant/LICENSE +22 -0
- package/plugins/jetbrains-ai-assistant/plugin.js +502 -16
- package/plugins/jetbrains-ai-assistant/plugin.json +27 -4
- package/plugins/kilo/icon-color.svg +1 -0
- package/plugins/kimi/plugin.js +54 -14
- package/plugins/kimi/plugin.json +46 -6
- package/plugins/langdock/LICENSE +21 -0
- package/plugins/langdock/icon.svg +4 -0
- package/plugins/langdock/plugin.js +106 -0
- package/plugins/langdock/plugin.json +25 -0
- package/plugins/linkup/LICENSE +21 -0
- package/plugins/linkup/icon.svg +10 -0
- package/plugins/linkup/plugin.js +46 -0
- package/plugins/linkup/plugin.json +24 -0
- package/plugins/litellm/plugin.js +54 -3
- package/plugins/lithosai/LICENSE +21 -0
- package/plugins/lithosai/icon.svg +1 -0
- package/plugins/lithosai/plugin.js +130 -0
- package/plugins/lithosai/plugin.json +26 -0
- package/plugins/llmman/LICENSE +21 -0
- package/plugins/llmman/icon.svg +4 -0
- package/plugins/llmman/plugin.js +138 -0
- package/plugins/llmman/plugin.json +31 -0
- package/plugins/longcat/icon-color.svg +1 -0
- package/plugins/longcat/icon.svg +1 -4
- package/plugins/manus/icon-color.svg +6 -0
- package/plugins/mimo/icon-color.svg +4 -0
- package/plugins/mimo/icon.svg +4 -1
- package/plugins/mistral/plugin.js +164 -27
- package/plugins/moonshot/icon-color.svg +1 -0
- package/plugins/munito/LICENSE +21 -0
- package/plugins/munito/icon.svg +7 -0
- package/plugins/munito/plugin.js +171 -0
- package/plugins/munito/plugin.json +24 -0
- package/plugins/muse/LICENSE +21 -0
- package/plugins/muse/icon.svg +3 -0
- package/plugins/muse/plugin.js +288 -0
- package/plugins/muse/plugin.json +34 -0
- package/plugins/museai/LICENSE +21 -0
- package/plugins/museai/icon.svg +3 -0
- package/plugins/museai/plugin.js +161 -0
- package/plugins/museai/plugin.json +26 -0
- package/plugins/nous/LICENSE +21 -0
- package/plugins/nous/icon-color.svg +1 -0
- package/plugins/nous/icon.svg +1 -0
- package/plugins/nous/plugin.js +129 -0
- package/plugins/nous/plugin.json +32 -0
- package/plugins/ollama/icon-color.svg +1 -0
- package/plugins/ollama/plugin.js +58 -0
- package/plugins/openai-api/icon-color.svg +1 -0
- package/plugins/opencode/icon-color.svg +1 -0
- package/plugins/opencode-go/icon-color.svg +1 -0
- package/plugins/opencode-go/plugin.js +104 -37
- package/plugins/opendesign/LICENSE +21 -0
- package/plugins/opendesign/icon.svg +4 -0
- package/plugins/opendesign/plugin.js +108 -0
- package/plugins/opendesign/plugin.json +25 -0
- package/plugins/openrouter/icon-color.svg +1 -0
- package/plugins/parallel/LICENSE +21 -0
- package/plugins/parallel/icon.svg +11 -0
- package/plugins/parallel/plugin.js +97 -0
- package/plugins/parallel/plugin.json +25 -0
- package/plugins/perplexity/LICENSE +22 -0
- package/plugins/perplexity/plugin.js +122 -0
- package/plugins/perplexity/plugin.json +36 -7
- package/plugins/pi/plugin.js +113 -0
- package/plugins/pi/plugin.json +11 -0
- package/plugins/qwencloud/LICENSE +21 -0
- package/plugins/qwencloud/icon-color.svg +1 -0
- package/plugins/qwencloud/icon.svg +1 -4
- package/plugins/qwencloud/plugin.js +197 -0
- package/plugins/qwencloud/plugin.json +17 -0
- package/plugins/raycast/LICENSE +21 -0
- package/plugins/raycast/icon.svg +4 -0
- package/plugins/raycast/plugin.js +179 -0
- package/plugins/raycast/plugin.json +32 -0
- package/plugins/replicate/LICENSE +21 -0
- package/plugins/replicate/icon-color.svg +1 -0
- package/plugins/replicate/icon.svg +1 -0
- package/plugins/replicate/plugin.js +170 -0
- package/plugins/replicate/plugin.json +25 -0
- package/plugins/sailresearch/LICENSE +21 -0
- package/plugins/sailresearch/icon.svg +4 -0
- package/plugins/sailresearch/plugin.js +104 -0
- package/plugins/sailresearch/plugin.json +24 -0
- package/plugins/siliconflow/LICENSE +21 -0
- package/plugins/siliconflow/icon.svg +3 -0
- package/plugins/siliconflow/plugin.js +87 -0
- package/plugins/siliconflow/plugin.json +32 -0
- package/plugins/sofya/LICENSE +21 -0
- package/plugins/sofya/icon.svg +3 -0
- package/plugins/sofya/plugin.js +86 -0
- package/plugins/sofya/plugin.json +24 -0
- package/plugins/soniox/LICENSE +21 -0
- package/plugins/soniox/icon.svg +3 -0
- package/plugins/soniox/plugin.js +103 -0
- package/plugins/soniox/plugin.json +24 -0
- package/plugins/synthetic/icon-color.svg +14 -0
- package/plugins/synthetic/plugin.js +12 -0
- package/plugins/synthetic/plugin.json +51 -9
- package/plugins/tavily/LICENSE +21 -0
- package/plugins/tavily/icon.svg +3 -0
- package/plugins/tavily/plugin.js +61 -0
- package/plugins/tavily/plugin.json +24 -0
- package/plugins/tinyapi/LICENSE +21 -0
- package/plugins/tinyapi/icon.svg +4 -0
- package/plugins/tinyapi/plugin.js +73 -0
- package/plugins/tinyapi/plugin.json +25 -0
- package/plugins/traecn/LICENSE +21 -0
- package/plugins/traecn/icon.svg +3 -0
- package/plugins/traecn/plugin.js +103 -0
- package/plugins/traecn/plugin.json +25 -0
- package/plugins/tripo/LICENSE +21 -0
- package/plugins/tripo/icon.svg +4 -0
- package/plugins/tripo/plugin.js +60 -0
- package/plugins/tripo/plugin.json +24 -0
- package/plugins/typesafe/LICENSE +21 -0
- package/plugins/typesafe/icon.svg +4 -0
- package/plugins/typesafe/plugin.js +181 -0
- package/plugins/typesafe/plugin.json +25 -0
- package/plugins/v0/LICENSE +21 -0
- package/plugins/v0/icon-color.svg +1 -0
- package/plugins/v0/icon.svg +1 -0
- package/plugins/v0/plugin.js +219 -0
- package/plugins/v0/plugin.json +32 -0
- package/plugins/venice/plugin.js +50 -1
- package/plugins/venice/plugin.json +2 -1
- package/plugins/vercel/LICENSE +21 -0
- package/plugins/vercel/icon-color.svg +1 -0
- package/plugins/vercel/icon.svg +1 -0
- package/plugins/vercel/plugin.js +202 -0
- package/plugins/vercel/plugin.json +54 -0
- package/plugins/windsurf/icon-color.svg +1 -0
- package/plugins/workbuddy/LICENSE +21 -0
- package/plugins/workbuddy/icon.svg +1 -0
- package/plugins/workbuddy/plugin.js +318 -0
- package/plugins/workbuddy/plugin.json +63 -0
- package/plugins/xai/icon-color.svg +1 -0
- package/plugins/xai/icon.svg +1 -7
- package/plugins/xai/plugin.js +9 -0
- package/plugins/xapi/LICENSE +21 -0
- package/plugins/xapi/icon.svg +1 -0
- package/plugins/xapi/plugin.js +90 -0
- package/plugins/xapi/plugin.json +25 -0
- package/plugins/xkiro/LICENSE +21 -0
- package/plugins/xkiro/icon.svg +1 -0
- package/plugins/xkiro/plugin.js +104 -0
- package/plugins/xkiro/plugin.json +24 -0
- package/plugins/zai/icon-color.svg +1 -0
- package/plugins/zai/plugin.js +5 -1
- package/plugins/zenmux/icon-color.svg +1 -0
- package/plugins/zenmux/icon.svg +1 -3
- package/usagestat +0 -0
- package/usagestatd +0 -0
- package/plugins/cursor-nightly/plugin.js +0 -1416
package/plugins/claude/plugin.js
CHANGED
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
// Rate-limit state persisted across probe() calls (module scope survives re-invocations).
|
|
20
20
|
const MIN_USAGE_FETCH_INTERVAL_MS = 5 * 60 * 1000 // never poll more than once per 5 min
|
|
21
21
|
const DEFAULT_RATE_LIMIT_BACKOFF_MS = 5 * 60 * 1000 // fallback when no Retry-After header
|
|
22
|
+
const RESET_GRANTS_CHECK_INTERVAL_MS = 60 * 60 * 1000
|
|
22
23
|
const LIVE_USAGE_CACHE_FILE = "live-usage-cache.json"
|
|
23
24
|
const LIVE_USAGE_CACHE_VERSION = 2
|
|
24
25
|
const LIVE_USAGE_CACHE_MAX_AGE_MS = 24 * 60 * 60 * 1000
|
|
@@ -411,6 +412,8 @@
|
|
|
411
412
|
|
|
412
413
|
const envAccessToken = readEnvText(ctx, "CLAUDE_CODE_OAUTH_TOKEN")
|
|
413
414
|
const stored = loadStoredCredentials(ctx, !!envAccessToken)
|
|
415
|
+
// setup-token credentials cannot read quotas; prefer the signed-in profile.
|
|
416
|
+
if (stored && stored.oauth && stored.oauth.accessToken && hasProfileScope(stored)) return stored
|
|
414
417
|
if (!envAccessToken) {
|
|
415
418
|
return stored
|
|
416
419
|
}
|
|
@@ -594,18 +597,43 @@
|
|
|
594
597
|
|
|
595
598
|
function fetchUsage(ctx, accessToken) {
|
|
596
599
|
const oauthConfig = getOauthConfig(ctx)
|
|
597
|
-
|
|
600
|
+
const cache = readLiveUsageCache(ctx)
|
|
601
|
+
const now = Date.parse(ctx.nowIso)
|
|
602
|
+
const checkedAt = Number(cache && cache.resetGrantsCheckedAtMs)
|
|
603
|
+
const checkGrants = !Number.isFinite(checkedAt) || checkedAt <= 0 || now < checkedAt || now - checkedAt >= RESET_GRANTS_CHECK_INTERVAL_MS
|
|
604
|
+
let version = "2.1.0"
|
|
605
|
+
try {
|
|
606
|
+
const result = ctx.host.command.run({program: "claude", args: ["--version"], timeoutMs: 3000})
|
|
607
|
+
const match = result.status === 0 && /^\s*(\d+\.\d+\.\d+)(?:\s|$)/.exec(result.stdout || "")
|
|
608
|
+
if (match) version = match[1]
|
|
609
|
+
} catch (_) { /* The OAuth source also works without a local CLI. */ }
|
|
610
|
+
const response = ctx.util.request({
|
|
598
611
|
method: "GET",
|
|
599
|
-
url: oauthConfig.usageUrl,
|
|
612
|
+
url: oauthConfig.usageUrl + (checkGrants ? "?cedar_ember=1&skip_spend=1" : ""),
|
|
600
613
|
headers: {
|
|
601
614
|
Authorization: "Bearer " + accessToken.trim(),
|
|
602
615
|
Accept: "application/json",
|
|
603
616
|
"Content-Type": "application/json",
|
|
604
617
|
"anthropic-beta": "oauth-2025-04-20",
|
|
605
|
-
"User-Agent": "claude-code/
|
|
618
|
+
"User-Agent": "claude-code/" + version,
|
|
606
619
|
},
|
|
607
620
|
timeoutMs: 10000,
|
|
608
621
|
})
|
|
622
|
+
if (response.status >= 200 && response.status < 300) {
|
|
623
|
+
const data = ctx.util.tryParseJson(response.bodyText)
|
|
624
|
+
if (data && typeof data === "object" && !Array.isArray(data) && !data.error) {
|
|
625
|
+
const validLines=[]
|
|
626
|
+
addUsageWindowLines(ctx,data,validLines)
|
|
627
|
+
if (!validLines.length) return response
|
|
628
|
+
if (checkGrants || data.cedar_ember) {
|
|
629
|
+
writeLiveUsageCache(ctx, {resetGrantsCheckedAtMs:now,resetGrantsBlock: data.cedar_ember || null})
|
|
630
|
+
} else if (cache && cache.resetGrantsBlock) {
|
|
631
|
+
data.cedar_ember = cache.resetGrantsBlock
|
|
632
|
+
response.bodyText = JSON.stringify(data)
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
return response
|
|
609
637
|
}
|
|
610
638
|
|
|
611
639
|
function parseRetryAfterSeconds(headers) {
|
|
@@ -626,6 +654,29 @@
|
|
|
626
654
|
return null
|
|
627
655
|
}
|
|
628
656
|
|
|
657
|
+
function livePlanLabel(ctx, creds) {
|
|
658
|
+
const token = creds.oauth.accessToken
|
|
659
|
+
const tokenHash = ctx.host.crypto.sha256(token)
|
|
660
|
+
const cached = readLiveUsageCache(ctx)
|
|
661
|
+
if (cached && cached.profileTokenHash === tokenHash) return cached.livePlan || null
|
|
662
|
+
let plan = null
|
|
663
|
+
try {
|
|
664
|
+
const result = ctx.util.requestJson({method: "GET", url: getOauthConfig(ctx).baseApiUrl + "/api/oauth/profile",
|
|
665
|
+
headers: {Authorization: "Bearer " + token, Accept: "application/json", "anthropic-beta": "oauth-2025-04-20"}, timeoutMs: 3000})
|
|
666
|
+
const org = result.resp.status === 200 && result.json && result.json.organization
|
|
667
|
+
if (org && typeof org === "object") {
|
|
668
|
+
const type = cleanText(org.organization_type || org.organizationType)
|
|
669
|
+
const tier = cleanText(org.rate_limit_tier || org.rateLimitTier) || creds.oauth.rateLimitTier
|
|
670
|
+
if (type) plan = oauthPlanLabel(ctx, {oauth: {subscriptionType: type.replace(/^claude_/, ""), rateLimitTier: tier}})
|
|
671
|
+
}
|
|
672
|
+
} catch (_) {
|
|
673
|
+
ctx.host.log.warn("Claude profile unavailable; retaining credential plan.")
|
|
674
|
+
}
|
|
675
|
+
// Include failed lookups, so a profile outage cannot add a request on every poll.
|
|
676
|
+
writeLiveUsageCache(ctx, {profileTokenHash: tokenHash, livePlan: plan})
|
|
677
|
+
return plan
|
|
678
|
+
}
|
|
679
|
+
|
|
629
680
|
function fmtRateLimitMinutes(seconds) {
|
|
630
681
|
if (seconds <= 0) return "now"
|
|
631
682
|
const mins = Math.ceil(seconds / 60)
|
|
@@ -1149,9 +1200,9 @@
|
|
|
1149
1200
|
|
|
1150
1201
|
function getSessionKey(ctx) {
|
|
1151
1202
|
const candidates = [
|
|
1203
|
+
providerCookieHeader(ctx),
|
|
1152
1204
|
readEnvText(ctx, "CLAUDE_AI_SESSION_KEY"),
|
|
1153
1205
|
readEnvText(ctx, "CLAUDE_WEB_SESSION_KEY"),
|
|
1154
|
-
providerCookieHeader(ctx),
|
|
1155
1206
|
]
|
|
1156
1207
|
for (let i = 0; i < candidates.length; i++) {
|
|
1157
1208
|
const key = extractSessionKey(candidates[i])
|
|
@@ -1160,9 +1211,12 @@
|
|
|
1160
1211
|
return null
|
|
1161
1212
|
}
|
|
1162
1213
|
|
|
1163
|
-
function buildWebHeaders(sessionKey) {
|
|
1214
|
+
function buildWebHeaders(ctx, sessionKey) {
|
|
1215
|
+
const configured = providerCookieHeader(ctx)
|
|
1216
|
+
const cookie = configured && extractSessionKey(configured) === sessionKey && configured.includes("=")
|
|
1217
|
+
? configured : "sessionKey=" + sessionKey
|
|
1164
1218
|
return {
|
|
1165
|
-
Cookie:
|
|
1219
|
+
Cookie: cookie,
|
|
1166
1220
|
Accept: "application/json",
|
|
1167
1221
|
Origin: "https://claude.ai",
|
|
1168
1222
|
Referer: "https://claude.ai/settings/usage",
|
|
@@ -1171,8 +1225,22 @@
|
|
|
1171
1225
|
}
|
|
1172
1226
|
}
|
|
1173
1227
|
|
|
1228
|
+
function checkWebResponse(resp) {
|
|
1229
|
+
const headers = resp.headers || {}
|
|
1230
|
+
const challenge = String(headers["cf-mitigated"] || headers["Cf-Mitigated"] || "").toLowerCase() === "challenge"
|
|
1231
|
+
|| /<title>\s*just a moment|\/cdn-cgi\/challenge-platform\//i.test(String(resp.bodyText || "").slice(0,65536))
|
|
1232
|
+
if (resp.status === 403 && challenge) {
|
|
1233
|
+
throw "Claude web usage is blocked by a Cloudflare browser challenge. Your Claude login has not been rejected."
|
|
1234
|
+
}
|
|
1235
|
+
if (resp.status === 429) {
|
|
1236
|
+
const error = new Error("Claude web usage updates are temporarily rate-limited, not your message allowance.")
|
|
1237
|
+
error.retryAfterSeconds = parseRetryAfterSeconds(headers)
|
|
1238
|
+
throw error
|
|
1239
|
+
}
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1174
1242
|
function fetchWebUsage(ctx, sessionKey) {
|
|
1175
|
-
const headers = buildWebHeaders(sessionKey)
|
|
1243
|
+
const headers = buildWebHeaders(ctx, sessionKey)
|
|
1176
1244
|
|
|
1177
1245
|
let orgResp
|
|
1178
1246
|
try {
|
|
@@ -1187,6 +1255,7 @@
|
|
|
1187
1255
|
throw "Web usage request failed. Check your connection."
|
|
1188
1256
|
}
|
|
1189
1257
|
|
|
1258
|
+
checkWebResponse(orgResp)
|
|
1190
1259
|
if (orgResp.status === 401 || orgResp.status === 403) {
|
|
1191
1260
|
throw "Web session invalid or expired. Set CLAUDE_AI_SESSION_KEY to your claude.ai sessionKey cookie."
|
|
1192
1261
|
}
|
|
@@ -1219,6 +1288,7 @@
|
|
|
1219
1288
|
}
|
|
1220
1289
|
|
|
1221
1290
|
lastUsageStatus = usageResp.status
|
|
1291
|
+
checkWebResponse(usageResp)
|
|
1222
1292
|
if (usageResp.status === 401 || usageResp.status === 403) {
|
|
1223
1293
|
ctx.host.log.info("web mode: usage unavailable for org " + candidateOrgId + " (HTTP " + usageResp.status + ")")
|
|
1224
1294
|
continue
|
|
@@ -1265,33 +1335,46 @@
|
|
|
1265
1335
|
|
|
1266
1336
|
function loadWebUsageData(ctx, sessionKey, nowMs) {
|
|
1267
1337
|
const liveCache = readLiveUsageCache(ctx)
|
|
1338
|
+
if (liveCache && Number(liveCache.webRetryAtMs) > nowMs) {
|
|
1339
|
+
throw liveCache.webError || "Claude web usage updates are temporarily unavailable."
|
|
1340
|
+
}
|
|
1268
1341
|
const cachedLastWebUsageFetchMs = Number(liveCache && liveCache.lastWebUsageFetchMs) || 0
|
|
1269
1342
|
const effectiveLastWebUsageFetchMs = Math.max(lastWebUsageFetchMs, cachedLastWebUsageFetchMs)
|
|
1270
|
-
const cached = cachedWebUsageData || cachedLiveUsageData(liveCache, nowMs)
|
|
1343
|
+
const cached = cachedWebUsageData || (liveCache && liveCache.usageSource === "web" && cachedLiveUsageData(liveCache, nowMs))
|
|
1271
1344
|
if (cached && nowMs - effectiveLastWebUsageFetchMs < MIN_USAGE_FETCH_INTERVAL_MS) {
|
|
1272
1345
|
lastWebUsageFetchMs = effectiveLastWebUsageFetchMs
|
|
1273
1346
|
ctx.host.log.info(
|
|
1274
1347
|
"web usage fetch skipped: last fetch was " +
|
|
1275
1348
|
Math.round((nowMs - effectiveLastWebUsageFetchMs) / 1000) + "s ago"
|
|
1276
1349
|
)
|
|
1277
|
-
return { data: cached, plan: null }
|
|
1350
|
+
return { data: cached, plan: liveCache && liveCache.webPlan || null }
|
|
1278
1351
|
}
|
|
1279
1352
|
|
|
1280
1353
|
lastWebUsageFetchMs = nowMs
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1354
|
+
let result
|
|
1355
|
+
try {
|
|
1356
|
+
result = fetchWebUsage(ctx, sessionKey)
|
|
1357
|
+
const validLines = []
|
|
1358
|
+
addUsageWindowLines(ctx, result.usageData, validLines)
|
|
1359
|
+
if (!validLines.length) throw "Web usage response invalid. Try again later."
|
|
1360
|
+
} catch (error) {
|
|
1361
|
+
const delay = error && Number.isFinite(error.retryAfterSeconds) ? error.retryAfterSeconds * 1000 : MIN_USAGE_FETCH_INTERVAL_MS
|
|
1362
|
+
writeLiveUsageCache(ctx, {lastWebUsageFetchMs: nowMs, webRetryAtMs: nowMs + Math.max(MIN_USAGE_FETCH_INTERVAL_MS, delay),
|
|
1363
|
+
webError: error && error.message || String(error)})
|
|
1364
|
+
throw error
|
|
1365
|
+
}
|
|
1285
1366
|
cachedWebUsageData = result.usageData
|
|
1367
|
+
let plan = null
|
|
1368
|
+
if (result.accountInfo && result.accountInfo.rate_limit_tier) {
|
|
1369
|
+
plan = webTierToPlanLabel(result.accountInfo.rate_limit_tier)
|
|
1370
|
+
}
|
|
1286
1371
|
writeLiveUsageCache(ctx, {
|
|
1287
1372
|
usageData: result.usageData,
|
|
1373
|
+
usageSource: "web",
|
|
1288
1374
|
usageFetchedAtMs: nowMs,
|
|
1289
1375
|
lastWebUsageFetchMs: nowMs,
|
|
1376
|
+
webRetryAtMs: 0, webError: null, webPlan: plan,
|
|
1290
1377
|
})
|
|
1291
|
-
let plan = null
|
|
1292
|
-
if (result.accountInfo && result.accountInfo.rate_limit_tier) {
|
|
1293
|
-
plan = webTierToPlanLabel(result.accountInfo.rate_limit_tier)
|
|
1294
|
-
}
|
|
1295
1378
|
return { data: result.usageData, plan: plan }
|
|
1296
1379
|
}
|
|
1297
1380
|
|
|
@@ -1439,6 +1522,41 @@
|
|
|
1439
1522
|
}))
|
|
1440
1523
|
}
|
|
1441
1524
|
|
|
1525
|
+
const grants = data.cedar_ember
|
|
1526
|
+
if (grants && typeof grants === "object" && !Array.isArray(grants)) {
|
|
1527
|
+
let count = 0
|
|
1528
|
+
const expiries = []
|
|
1529
|
+
if (grants.eligible === true && Array.isArray(grants.grants)) {
|
|
1530
|
+
for (const grant of grants.grants) {
|
|
1531
|
+
if (!grant || !Number.isSafeInteger(grant.resets_left) || grant.resets_left <= 0) continue
|
|
1532
|
+
const end = ctx.util.toIso(grant.ends_at)
|
|
1533
|
+
if (end && Date.parse(end) <= Date.parse(ctx.nowIso)) continue
|
|
1534
|
+
if (!Number.isSafeInteger(count + grant.resets_left)) continue
|
|
1535
|
+
count += grant.resets_left
|
|
1536
|
+
if (end) expiries.push(end + " (" + grant.resets_left + ")")
|
|
1537
|
+
}
|
|
1538
|
+
}
|
|
1539
|
+
lines.push(ctx.line.text({label: "Rate Limit Resets", value: count + " available",
|
|
1540
|
+
subtitle: expiries.length ? "Expires " + expiries.sort().join(", ") : undefined}))
|
|
1541
|
+
}
|
|
1542
|
+
|
|
1543
|
+
const cloud = data.iguana_necktie
|
|
1544
|
+
if (cloud && typeof cloud === 'object' && !Array.isArray(cloud)) {
|
|
1545
|
+
const limit = cloud.limit_dollars, used = cloud.used_dollars
|
|
1546
|
+
const remaining = cloud.remaining_dollars != null ? cloud.remaining_dollars
|
|
1547
|
+
: typeof used === 'number' ? limit - used : null
|
|
1548
|
+
const validMoney = value => typeof value === 'number' && Number.isFinite(value) && value >= 0 && value <= limit
|
|
1549
|
+
const expires = cloud.resets_at == null ? null : ctx.util.toIso(cloud.resets_at)
|
|
1550
|
+
if (typeof limit === 'number' && Number.isFinite(limit) && limit > 0 && validMoney(remaining) &&
|
|
1551
|
+
(used == null || validMoney(used)) && (cloud.resets_at == null || expires) &&
|
|
1552
|
+
(cloud.locked_reason == null || typeof cloud.locked_reason === 'string')) {
|
|
1553
|
+
const expired = expires && Date.parse(expires) <= Date.parse(ctx.nowIso)
|
|
1554
|
+
const locked = typeof cloud.locked_reason === 'string' && cloud.locked_reason.trim()
|
|
1555
|
+
lines.push(ctx.line.text({label:'Cloud credits',value:expired ? 'Expired' : locked ? 'Unavailable' : '$'+remaining.toFixed(2)+' remaining',
|
|
1556
|
+
subtitle:expires ? (expired ? 'Expired ' : 'Expires ')+expires : undefined}))
|
|
1557
|
+
}
|
|
1558
|
+
}
|
|
1559
|
+
|
|
1442
1560
|
if (data.extra_usage && data.extra_usage.is_enabled) {
|
|
1443
1561
|
const used = moneyMajorUnits(data.extra_usage.used_credits)
|
|
1444
1562
|
const limit = moneyMajorUnits(data.extra_usage.monthly_limit)
|
|
@@ -1488,6 +1606,29 @@
|
|
|
1488
1606
|
|
|
1489
1607
|
// ── probe() ────────────────────────────────────────────────────────────────
|
|
1490
1608
|
|
|
1609
|
+
function readNativeQuota(ctx, creds, probe) {
|
|
1610
|
+
// Never borrow the machine's ambient CLI account for a named account or an
|
|
1611
|
+
// inference-only token. The native host also verifies the profile token.
|
|
1612
|
+
if (!creds || creds.source !== "file" || !hasProfileScope(creds)) return null
|
|
1613
|
+
const host = ctx.host.claude
|
|
1614
|
+
const read = host && host[probe ? "probeQuota" : "readQuota"]
|
|
1615
|
+
if (typeof read !== "function") return null
|
|
1616
|
+
try {
|
|
1617
|
+
const raw = read(ctx.host.crypto.sha256(creds.oauth.accessToken), ctx.provider && ctx.provider.workspaceId || null)
|
|
1618
|
+
const result = typeof raw === "string" ? ctx.util.tryParseJson(raw) : raw
|
|
1619
|
+
if (!result || !result.data || !Number.isFinite(result.fetchedAtMs)) return null
|
|
1620
|
+
const age = Date.now() - result.fetchedAtMs
|
|
1621
|
+
if (age < 0 || age > MIN_USAGE_FETCH_INTERVAL_MS) return null
|
|
1622
|
+
if (result.source !== "cli" && result.source !== "cli-cache") return null
|
|
1623
|
+
const valid = []
|
|
1624
|
+
addUsageWindowLines(ctx, result.data, valid)
|
|
1625
|
+
return valid.length ? result : null
|
|
1626
|
+
} catch (e) {
|
|
1627
|
+
ctx.host.log.info("Claude CLI quota unavailable: " + String(e))
|
|
1628
|
+
return null
|
|
1629
|
+
}
|
|
1630
|
+
}
|
|
1631
|
+
|
|
1491
1632
|
function probe(ctx) {
|
|
1492
1633
|
const creds = loadCredentials(ctx)
|
|
1493
1634
|
const sessionKey = getSessionKey(ctx)
|
|
@@ -1497,6 +1638,7 @@
|
|
|
1497
1638
|
const wantsAuto = sourceMode === "auto"
|
|
1498
1639
|
const wantsWeb = sourceMode === "web"
|
|
1499
1640
|
const wantsOAuth = sourceMode === "oauth"
|
|
1641
|
+
const wantsCli = sourceMode === "cli"
|
|
1500
1642
|
const wantsLocal = sourceMode === "local"
|
|
1501
1643
|
const wantsApi = sourceMode === "api"
|
|
1502
1644
|
|
|
@@ -1519,6 +1661,7 @@
|
|
|
1519
1661
|
const homePath = getClaudeHomeOverride(ctx)
|
|
1520
1662
|
let data = null
|
|
1521
1663
|
let quotaSource = null
|
|
1664
|
+
let quotaFetchedAtMs = null
|
|
1522
1665
|
let lines = []
|
|
1523
1666
|
let plan = null
|
|
1524
1667
|
let rateLimited = false
|
|
@@ -1526,149 +1669,194 @@
|
|
|
1526
1669
|
|
|
1527
1670
|
if (hasOAuth) {
|
|
1528
1671
|
plan = oauthPlanLabel(ctx, creds)
|
|
1672
|
+
if (liveCache && liveCache.profileTokenHash === ctx.host.crypto.sha256(creds.oauth.accessToken) && liveCache.livePlan) plan = liveCache.livePlan
|
|
1673
|
+
}
|
|
1674
|
+
|
|
1675
|
+
let nativeQuota = null
|
|
1676
|
+
if (wantsAuto || wantsCli) {
|
|
1677
|
+
nativeQuota = readNativeQuota(ctx, creds, false)
|
|
1678
|
+
const cooldownUntil = Math.max(rateLimitedUntilMs, Number(liveCache && liveCache.rateLimitedUntilMs) || 0)
|
|
1679
|
+
const apiFetchedAt = Number(liveCache && liveCache.usageFetchedAtMs) || 0
|
|
1680
|
+
const freshApi = cachedLiveUsageData(liveCache, nowMs) && apiFetchedAt <= nowMs && nowMs - apiFetchedAt < MIN_USAGE_FETCH_INTERVAL_MS
|
|
1681
|
+
if (!nativeQuota && wantsCli && nowMs >= cooldownUntil) {
|
|
1682
|
+
nativeQuota = readNativeQuota(ctx, creds, true)
|
|
1683
|
+
}
|
|
1684
|
+
// Prefer the newer complete observation; passive reads never clear an
|
|
1685
|
+
// HTTP Retry-After deadline or rewrite the REST cache's fetch timestamp.
|
|
1686
|
+
if (nativeQuota && !wantsCli && freshApi && apiFetchedAt > nativeQuota.fetchedAtMs) nativeQuota = null
|
|
1529
1687
|
}
|
|
1530
1688
|
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
const cachedRateLimitedUntilMs = Number(liveCache && liveCache.rateLimitedUntilMs) || 0
|
|
1544
|
-
const effectiveRateLimitedUntilMs = Math.max(rateLimitedUntilMs, cachedRateLimitedUntilMs)
|
|
1545
|
-
|
|
1546
|
-
if (nowMs < effectiveRateLimitedUntilMs) {
|
|
1547
|
-
// Still within a rate-limit window from a previous probe call — skip the
|
|
1548
|
-
// API request entirely and surface the remaining wait time to the user.
|
|
1689
|
+
try {
|
|
1690
|
+
if (wantsLocal) {
|
|
1691
|
+
ctx.host.log.info("local mode requested; skipping live usage fetch")
|
|
1692
|
+
} else if (wantsApi) {
|
|
1693
|
+
ctx.host.log.info("api mode requested; skipping live quota fetch")
|
|
1694
|
+
} else if (nativeQuota) {
|
|
1695
|
+
data = nativeQuota.data
|
|
1696
|
+
quotaSource = nativeQuota.source
|
|
1697
|
+
quotaFetchedAtMs = nativeQuota.fetchedAtMs
|
|
1698
|
+
} else if (wantsCli) {
|
|
1699
|
+
const cooldownUntil = Math.max(rateLimitedUntilMs, Number(liveCache && liveCache.rateLimitedUntilMs) || 0)
|
|
1700
|
+
if (nowMs < cooldownUntil) {
|
|
1549
1701
|
rateLimited = true
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
data = cachedData
|
|
1553
|
-
ctx.host.log.info("usage fetch skipped: rate-limited for " + retryAfterSeconds + "s more")
|
|
1702
|
+
retryAfterSeconds = Math.ceil((cooldownUntil - nowMs) / 1000)
|
|
1703
|
+
data = cachedLiveUsageData(liveCache, nowMs)
|
|
1554
1704
|
} else {
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1705
|
+
throw "Claude CLI returned no fresh quota data. Usage checks may be temporarily unavailable."
|
|
1706
|
+
}
|
|
1707
|
+
} else if (hasOAuth && !wantsWeb) {
|
|
1708
|
+
quotaSource = "oauth"
|
|
1709
|
+
// ── OAuth mode ───────────────────────────────────────────────────────
|
|
1710
|
+
let accessToken = creds.oauth.accessToken
|
|
1711
|
+
const canFetchLiveUsage = hasProfileScope(creds)
|
|
1712
|
+
|
|
1713
|
+
if (canFetchLiveUsage) {
|
|
1714
|
+
const cachedData = cachedUsageData || cachedLiveUsageData(liveCache, nowMs)
|
|
1715
|
+
const cachedRateLimitedUntilMs = Number(liveCache && liveCache.rateLimitedUntilMs) || 0
|
|
1716
|
+
const effectiveRateLimitedUntilMs = Math.max(rateLimitedUntilMs, cachedRateLimitedUntilMs)
|
|
1717
|
+
|
|
1718
|
+
if (nowMs < effectiveRateLimitedUntilMs) {
|
|
1719
|
+
// Still within a rate-limit window from a previous probe call — skip the
|
|
1720
|
+
// API request entirely and surface the remaining wait time to the user.
|
|
1721
|
+
rateLimited = true
|
|
1722
|
+
rateLimitedUntilMs = effectiveRateLimitedUntilMs
|
|
1723
|
+
retryAfterSeconds = Math.ceil((effectiveRateLimitedUntilMs - nowMs) / 1000)
|
|
1566
1724
|
data = cachedData
|
|
1567
|
-
ctx.host.log.info(
|
|
1568
|
-
"usage fetch skipped: last fetch was " +
|
|
1569
|
-
Math.round((nowMs - effectiveLastUsageFetchMs) / 1000) + "s ago (min interval " +
|
|
1570
|
-
MIN_USAGE_FETCH_INTERVAL_MS / 1000 + "s)"
|
|
1571
|
-
)
|
|
1725
|
+
ctx.host.log.info("usage fetch skipped: rate-limited for " + retryAfterSeconds + "s more")
|
|
1572
1726
|
} else {
|
|
1573
|
-
//
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
lastUsageFetchMs = nowMs
|
|
1585
|
-
let resp
|
|
1586
|
-
let didRefresh = false
|
|
1587
|
-
try {
|
|
1588
|
-
resp = ctx.util.retryOnceOnAuth({
|
|
1589
|
-
request: (token) => {
|
|
1590
|
-
try {
|
|
1591
|
-
return fetchUsage(ctx, token || accessToken)
|
|
1592
|
-
} catch (e) {
|
|
1593
|
-
ctx.host.log.error("usage request exception: " + String(e))
|
|
1594
|
-
if (didRefresh) {
|
|
1595
|
-
throw "Usage request failed after refresh. Try again."
|
|
1596
|
-
}
|
|
1597
|
-
throw "Usage request failed. Check your connection."
|
|
1598
|
-
}
|
|
1599
|
-
},
|
|
1600
|
-
refresh: () => {
|
|
1601
|
-
ctx.host.log.info("usage returned 401, attempting refresh")
|
|
1602
|
-
didRefresh = true
|
|
1603
|
-
return refreshToken(ctx, creds)
|
|
1604
|
-
},
|
|
1605
|
-
})
|
|
1606
|
-
} catch (e) {
|
|
1607
|
-
if (typeof e === "string") throw e
|
|
1608
|
-
ctx.host.log.error("usage request failed: " + String(e))
|
|
1609
|
-
throw "Usage request failed. Check your connection."
|
|
1610
|
-
}
|
|
1611
|
-
|
|
1612
|
-
if (ctx.util.isAuthStatus(resp.status)) {
|
|
1613
|
-
ctx.host.log.error("usage returned auth error after all retries: status=" + resp.status)
|
|
1614
|
-
throw "Token expired. Run `claude` to log in again."
|
|
1615
|
-
}
|
|
1616
|
-
|
|
1617
|
-
if (resp.status === 429) {
|
|
1618
|
-
rateLimited = true
|
|
1619
|
-
retryAfterSeconds = parseRetryAfterSeconds(resp.headers)
|
|
1620
|
-
const backoffMs = retryAfterSeconds !== null
|
|
1621
|
-
? retryAfterSeconds * 1000
|
|
1622
|
-
: DEFAULT_RATE_LIMIT_BACKOFF_MS
|
|
1623
|
-
rateLimitedUntilMs = nowMs + backoffMs
|
|
1727
|
+
// Rate-limit window has expired (or was never set). Check whether we were
|
|
1728
|
+
// previously rate-limited so we can bypass the min-interval guard: a short
|
|
1729
|
+
// Retry-After (< 5 min) must not be swallowed by the normal poll throttle.
|
|
1730
|
+
const wasRateLimited = effectiveRateLimitedUntilMs > 0
|
|
1731
|
+
rateLimitedUntilMs = 0
|
|
1732
|
+
|
|
1733
|
+
const cachedLastUsageFetchMs = Number(liveCache && liveCache.lastUsageFetchMs) || 0
|
|
1734
|
+
const effectiveLastUsageFetchMs = Math.max(lastUsageFetchMs, cachedLastUsageFetchMs)
|
|
1735
|
+
if (cachedData && !wasRateLimited && nowMs - effectiveLastUsageFetchMs < MIN_USAGE_FETCH_INTERVAL_MS) {
|
|
1736
|
+
// Polled too recently in normal operation — reuse last cached response.
|
|
1737
|
+
lastUsageFetchMs = effectiveLastUsageFetchMs
|
|
1624
1738
|
data = cachedData
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
usageFetchedAtMs: Number(liveCache && liveCache.usageFetchedAtMs) || null,
|
|
1630
|
-
lastRateLimitedAtMs: nowMs,
|
|
1631
|
-
})
|
|
1632
|
-
ctx.host.log.warn(
|
|
1633
|
-
"usage rate limited (429), backing off for " +
|
|
1634
|
-
Math.round(backoffMs / 1000) + "s"
|
|
1739
|
+
ctx.host.log.info(
|
|
1740
|
+
"usage fetch skipped: last fetch was " +
|
|
1741
|
+
Math.round((nowMs - effectiveLastUsageFetchMs) / 1000) + "s ago (min interval " +
|
|
1742
|
+
MIN_USAGE_FETCH_INTERVAL_MS / 1000 + "s)"
|
|
1635
1743
|
)
|
|
1636
|
-
} else if (resp.status < 200 || resp.status >= 300) {
|
|
1637
|
-
ctx.host.log.error("usage returned error: status=" + resp.status)
|
|
1638
|
-
throw "Usage request failed (HTTP " + String(resp.status) + "). Try again later."
|
|
1639
1744
|
} else {
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1745
|
+
// Proactively refresh if token is expired or about to expire
|
|
1746
|
+
if (needsRefresh(ctx, creds.oauth, nowMs)) {
|
|
1747
|
+
ctx.host.log.info("token needs refresh (expired or expiring soon)")
|
|
1748
|
+
const refreshed = refreshToken(ctx, creds)
|
|
1749
|
+
if (refreshed) {
|
|
1750
|
+
accessToken = refreshed
|
|
1751
|
+
} else {
|
|
1752
|
+
ctx.host.log.warn("proactive refresh failed, trying with existing token")
|
|
1753
|
+
}
|
|
1754
|
+
}
|
|
1755
|
+
|
|
1756
|
+
lastUsageFetchMs = nowMs
|
|
1757
|
+
let resp
|
|
1758
|
+
let didRefresh = false
|
|
1759
|
+
try {
|
|
1760
|
+
resp = ctx.util.retryOnceOnAuth({
|
|
1761
|
+
request: (token) => {
|
|
1762
|
+
try {
|
|
1763
|
+
return fetchUsage(ctx, token || accessToken)
|
|
1764
|
+
} catch (e) {
|
|
1765
|
+
ctx.host.log.error("usage request exception: " + String(e))
|
|
1766
|
+
if (didRefresh) {
|
|
1767
|
+
throw "Usage request failed after refresh. Try again."
|
|
1768
|
+
}
|
|
1769
|
+
throw "Usage request failed. Check your connection."
|
|
1770
|
+
}
|
|
1771
|
+
},
|
|
1772
|
+
refresh: () => {
|
|
1773
|
+
ctx.host.log.info("usage returned 401, attempting refresh")
|
|
1774
|
+
didRefresh = true
|
|
1775
|
+
return refreshToken(ctx, creds)
|
|
1776
|
+
},
|
|
1777
|
+
})
|
|
1778
|
+
} catch (e) {
|
|
1779
|
+
if (typeof e === "string") throw e
|
|
1780
|
+
ctx.host.log.error("usage request failed: " + String(e))
|
|
1781
|
+
throw "Usage request failed. Check your connection."
|
|
1782
|
+
}
|
|
1783
|
+
|
|
1784
|
+
if (ctx.util.isAuthStatus(resp.status)) {
|
|
1785
|
+
ctx.host.log.error("usage returned auth error after all retries: status=" + resp.status)
|
|
1786
|
+
throw "Token expired. Run `claude` to log in again."
|
|
1646
1787
|
}
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1788
|
+
|
|
1789
|
+
const responseData = ctx.util.tryParseJson(resp.bodyText)
|
|
1790
|
+
const inBandRateLimit = resp.status >= 200 && resp.status < 300
|
|
1791
|
+
&& responseData && responseData.error && responseData.error.type === "rate_limit_error"
|
|
1792
|
+
if (resp.status === 429 || inBandRateLimit) {
|
|
1793
|
+
rateLimited = true
|
|
1794
|
+
retryAfterSeconds = parseRetryAfterSeconds(resp.headers)
|
|
1795
|
+
const backoffMs = retryAfterSeconds !== null
|
|
1796
|
+
? retryAfterSeconds * 1000
|
|
1797
|
+
: DEFAULT_RATE_LIMIT_BACKOFF_MS
|
|
1798
|
+
rateLimitedUntilMs = nowMs + backoffMs
|
|
1799
|
+
data = cachedData
|
|
1800
|
+
writeLiveUsageCache(ctx, {
|
|
1801
|
+
rateLimitedUntilMs: rateLimitedUntilMs,
|
|
1802
|
+
lastUsageFetchMs: nowMs,
|
|
1803
|
+
usageData: data,
|
|
1804
|
+
usageFetchedAtMs: Number(liveCache && liveCache.usageFetchedAtMs) || null,
|
|
1805
|
+
lastRateLimitedAtMs: nowMs,
|
|
1806
|
+
})
|
|
1807
|
+
ctx.host.log.warn(
|
|
1808
|
+
"usage rate limited (429), backing off for " +
|
|
1809
|
+
Math.round(backoffMs / 1000) + "s"
|
|
1810
|
+
)
|
|
1811
|
+
} else if (resp.status < 200 || resp.status >= 300) {
|
|
1812
|
+
ctx.host.log.error("usage returned error: status=" + resp.status)
|
|
1813
|
+
throw "Usage request failed (HTTP " + String(resp.status) + "). Try again later."
|
|
1814
|
+
} else {
|
|
1815
|
+
ctx.host.log.info("usage fetch succeeded")
|
|
1816
|
+
data = responseData
|
|
1817
|
+
const validLines = []
|
|
1818
|
+
if (data && typeof data === "object") addUsageWindowLines(ctx, data, validLines)
|
|
1819
|
+
if (!validLines.length) {
|
|
1820
|
+
throw "Usage response invalid. Try again later."
|
|
1821
|
+
}
|
|
1822
|
+
cachedUsageData = data
|
|
1823
|
+
rateLimitedUntilMs = 0
|
|
1824
|
+
writeLiveUsageCache(ctx, {
|
|
1825
|
+
rateLimitedUntilMs: 0,
|
|
1826
|
+
lastUsageFetchMs: nowMs,
|
|
1827
|
+
usageData: data,
|
|
1828
|
+
usageSource: "oauth",
|
|
1829
|
+
usageFetchedAtMs: nowMs,
|
|
1830
|
+
lastRateLimitedAtMs: null,
|
|
1831
|
+
})
|
|
1832
|
+
plan = livePlanLabel(ctx, creds) || plan
|
|
1833
|
+
}
|
|
1834
|
+
} // end fetch else-branch
|
|
1835
|
+
}
|
|
1836
|
+
} else {
|
|
1837
|
+
ctx.host.log.info("skipping live usage fetch for inference-only token")
|
|
1838
|
+
}
|
|
1839
|
+
} else if (sessionKey) {
|
|
1840
|
+
// ── Web mode ─────────────────────────────────────────────────────────
|
|
1841
|
+
const webResult = loadWebUsageData(ctx, sessionKey, nowMs)
|
|
1842
|
+
data = webResult.data
|
|
1843
|
+
quotaSource = "web"
|
|
1844
|
+
if (webResult.plan) {
|
|
1845
|
+
plan = webResult.plan
|
|
1658
1846
|
}
|
|
1659
1847
|
} else {
|
|
1660
|
-
ctx.host.log.info("
|
|
1661
|
-
}
|
|
1662
|
-
} else if (sessionKey) {
|
|
1663
|
-
// ── Web mode ─────────────────────────────────────────────────────────
|
|
1664
|
-
const webResult = loadWebUsageData(ctx, sessionKey, nowMs)
|
|
1665
|
-
data = webResult.data
|
|
1666
|
-
quotaSource = "web"
|
|
1667
|
-
if (webResult.plan) {
|
|
1668
|
-
plan = webResult.plan
|
|
1848
|
+
ctx.host.log.info("auto mode using available Claude usage history")
|
|
1669
1849
|
}
|
|
1670
|
-
}
|
|
1671
|
-
|
|
1850
|
+
} catch (primaryError) {
|
|
1851
|
+
const cooldownUntil = Math.max(rateLimitedUntilMs, Number(liveCache && liveCache.rateLimitedUntilMs) || 0)
|
|
1852
|
+
const fallback = wantsAuto && nowMs >= cooldownUntil &&
|
|
1853
|
+
(readNativeQuota(ctx, creds, false) || readNativeQuota(ctx, creds, true))
|
|
1854
|
+
if (!fallback) throw primaryError
|
|
1855
|
+
data = fallback.data
|
|
1856
|
+
quotaSource = fallback.source
|
|
1857
|
+
quotaFetchedAtMs = fallback.fetchedAtMs
|
|
1858
|
+
rateLimited = false
|
|
1859
|
+
retryAfterSeconds = null
|
|
1672
1860
|
}
|
|
1673
1861
|
|
|
1674
1862
|
if (rateLimited && wantsAuto && sessionKey) {
|
|
@@ -1738,12 +1926,10 @@
|
|
|
1738
1926
|
? fmtRateLimitMinutes(retryAfterSeconds)
|
|
1739
1927
|
: null
|
|
1740
1928
|
const waitText = retryText
|
|
1741
|
-
? "
|
|
1742
|
-
: "
|
|
1929
|
+
? "Usage updates paused; retry in ~" + retryText
|
|
1930
|
+
: "Usage updates temporarily unavailable"
|
|
1743
1931
|
lines.unshift(ctx.line.badge({ label: "Status", text: waitText, color: "#f59e0b" }))
|
|
1744
|
-
const noteText =
|
|
1745
|
-
? "Live usage rate limited — retry in ~" + retryText
|
|
1746
|
-
: "Live usage rate limited — data may be stale"
|
|
1932
|
+
const noteText = "Anthropic limited usage checks, not your message allowance." + (data ? " Showing the last successful reading." : "")
|
|
1747
1933
|
lines.push(ctx.line.text({ label: "Note", value: noteText }))
|
|
1748
1934
|
} else if (lines.length === 0) {
|
|
1749
1935
|
if (noLiveCredentials && wantsAuto) throw "Not logged in and no local Claude usage found. Run `claude` to authenticate."
|
|
@@ -1751,8 +1937,9 @@
|
|
|
1751
1937
|
}
|
|
1752
1938
|
|
|
1753
1939
|
const finalCache = readLiveUsageCache(ctx)
|
|
1754
|
-
const fetchedAt = data &&
|
|
1940
|
+
const fetchedAt = data && ctx.util.toIso(quotaFetchedAtMs || finalCache && finalCache.usageFetchedAtMs)
|
|
1755
1941
|
return { plan: plan, lines: lines, fetchedAt: fetchedAt || ctx.nowIso,
|
|
1942
|
+
state: rateLimited ? "failed" : lines.length ? "ready" : "no-data",
|
|
1756
1943
|
source: data ? (rateLimited ? "cached" : quotaSource) : wantsApi ? "api" : "local" }
|
|
1757
1944
|
}
|
|
1758
1945
|
|