tokenusage-cli 0.4.3 → 0.4.5
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/CHANGELOG.md +66 -0
- package/THIRD_PARTY_NOTICES.md +17 -2
- package/package.json +4 -4
- package/src/commands/init.js +8 -1
- package/src/commands/reconcile.js +17 -5
- package/src/commands/status.js +4 -0
- package/src/commands/sync.js +36 -1
- package/src/lib/cursor-config.js +5 -1
- package/src/lib/deepseek-limits.js +280 -0
- package/src/lib/deepseek-status.js +157 -0
- package/src/lib/doctor.js +50 -0
- package/src/lib/grok-limits.js +40 -1
- package/src/lib/ingest-sanitize.js +9 -0
- package/src/lib/pricing/curated-overrides.json +627 -0
- package/src/lib/pricing/index.js +16 -0
- package/src/lib/pricing-coverage.js +205 -0
- package/src/lib/project-key.js +23 -0
- package/src/lib/provider-api-key-store.js +111 -0
- package/src/lib/provider-status.js +14 -2
- package/src/lib/qoder-limits.js +4 -1
- package/src/lib/rollout.js +308 -58
- package/src/lib/scheduled-task.js +7 -1
- package/src/lib/session-analytics.js +52 -2
- package/src/lib/session-upload.js +25 -12
- package/src/lib/upload-queue.js +4 -1
- package/src/lib/usage-limits.js +536 -22
package/src/lib/usage-limits.js
CHANGED
|
@@ -27,11 +27,12 @@ const {
|
|
|
27
27
|
fetchCursorSandAccessStatus,
|
|
28
28
|
fetchCursorSandUsageStatus,
|
|
29
29
|
} = require("./cursor-config");
|
|
30
|
-
const { fetchGrokLimits } = require("./grok-limits");
|
|
30
|
+
const { fetchGrokLimits, grokTokenSubject, loadGrokAuthEntry } = require("./grok-limits");
|
|
31
31
|
const { fetchZcodeLimits } = require("./zcode-limits");
|
|
32
32
|
const { fetchOpencodeGoLimits } = require("./opencode-go-limits");
|
|
33
33
|
const { fetchCommandcodeLimits } = require("./commandcode-limits");
|
|
34
34
|
const { fetchDevinLimits } = require("./devin-limits");
|
|
35
|
+
const { fetchDeepseekLimits } = require("./deepseek-limits");
|
|
35
36
|
const { fetchQoderLimits, fetchQoderCnLimits } = require("./qoder-limits");
|
|
36
37
|
const { fetchArkCodingPlanLimits } = require("./ark-coding-plan-limits");
|
|
37
38
|
const { fetchArkAgentPlanLimits } = require("./ark-agent-plan-limits");
|
|
@@ -68,7 +69,9 @@ const CACHE_TTL_MS = 2 * 60 * 1000;
|
|
|
68
69
|
const CACHE_MIN_TTL_MS = 5 * 1000;
|
|
69
70
|
const DEFAULT_PROVIDER_TIMEOUT_MS = 15_000;
|
|
70
71
|
const ANTIGRAVITY_LIMITS_CACHE_FILE = "usage-limits-cache.json";
|
|
71
|
-
|
|
72
|
+
// Stale-serve TTL unified to 12h across every limits cache (2026-11-03, rationale
|
|
73
|
+
// in the cursor note below).
|
|
74
|
+
const ANTIGRAVITY_LIMITS_CACHE_MAX_AGE_MS = 12 * 60 * 60 * 1000;
|
|
72
75
|
const ANTIGRAVITY_LIMITS_CACHE_UNKNOWN_RESET_TTL_MS = 12 * 60 * 60 * 1000;
|
|
73
76
|
// Well-known public OAuth client for Google Antigravity, embedded in the agy
|
|
74
77
|
// binary. Installed-app OAuth clients cannot keep a confidential secret, so
|
|
@@ -114,9 +117,10 @@ const ANTIGRAVITY_NOT_RUNNING_MESSAGE = "Antigravity IDE is not running. Launch
|
|
|
114
117
|
// Claude shares its OAuth usage endpoint budget with Claude Code itself, so a transient
|
|
115
118
|
// 429 is common. Persist the last successful read so the panel can keep showing it instead
|
|
116
119
|
// of flashing a red error. Separate file from Antigravity's (whose writer rewrites the whole
|
|
117
|
-
// file with only its own key, so a shared file would clobber).
|
|
120
|
+
// file with only its own key, so a shared file would clobber). Stale-serve TTL unified to
|
|
121
|
+
// 12h across every limits cache (2026-11-03, see the cursor note below).
|
|
118
122
|
const CLAUDE_LIMITS_CACHE_FILE = "claude-usage-limits-cache.json";
|
|
119
|
-
const CLAUDE_LIMITS_CACHE_MAX_AGE_MS =
|
|
123
|
+
const CLAUDE_LIMITS_CACHE_MAX_AGE_MS = 12 * 60 * 60 * 1000;
|
|
120
124
|
const CLAUDE_LIMITS_CACHE_FRESH_TTL_MS = 10 * 60 * 1000;
|
|
121
125
|
// Longer than the fresh TTL, so a prompt made after a snapshot was written always
|
|
122
126
|
// falls inside it (see claudeCacheAwaitsNewWindow).
|
|
@@ -124,16 +128,38 @@ const CLAUDE_UNSTARTED_WINDOW_RETRY_MS = 15 * 60 * 1000;
|
|
|
124
128
|
// Codex has no rate-limit cooldown like Claude, but its two sequential chatgpt.com requests
|
|
125
129
|
// (/wham/usage + /wham/rate-limit-reset-credits) make it the provider most exposed to slow
|
|
126
130
|
// networks. Persist the last successful read so a timeout serves stale bars instead of a red
|
|
127
|
-
// error, mirroring the Claude stale-fallback path.
|
|
131
|
+
// error, mirroring the Claude stale-fallback path. Stale-serve TTL unified to 12h
|
|
132
|
+
// across every limits cache (2026-11-03, see the cursor note below).
|
|
128
133
|
const CODEX_LIMITS_CACHE_FILE = "codex-usage-limits-cache.json";
|
|
129
|
-
const CODEX_LIMITS_CACHE_MAX_AGE_MS =
|
|
134
|
+
const CODEX_LIMITS_CACHE_MAX_AGE_MS = 12 * 60 * 60 * 1000;
|
|
130
135
|
// OpenCode Go hits opencode.ai over the public internet (15s timeout) with no local CLI to
|
|
131
136
|
// fall back on. A transient 5xx / timeout previously left the panel blank until the next
|
|
132
137
|
// 2-minute poll — persist the last successful windows so a blip serves stale bars instead
|
|
133
|
-
// of a flash of "fetch failed" (mirrors Claude / Codex / Antigravity).
|
|
138
|
+
// of a flash of "fetch failed" (mirrors Claude / Codex / Antigravity). Stale-serve TTL
|
|
139
|
+
// unified to 12h across every limits cache (2026-11-03, see the cursor note below).
|
|
134
140
|
const OPENCODE_GO_LIMITS_CACHE_FILE = "opencode-go-usage-limits-cache.json";
|
|
135
|
-
const OPENCODE_GO_LIMITS_CACHE_MAX_AGE_MS =
|
|
141
|
+
const OPENCODE_GO_LIMITS_CACHE_MAX_AGE_MS = 12 * 60 * 60 * 1000;
|
|
136
142
|
const OPENCODE_GO_LOCAL_ESTIMATE_CACHE_MAX_AGE_MS = 60 * 60 * 1000;
|
|
143
|
+
// Cursor's web summary (cursor.com) and Grok Bot RPCs (api2.cursor.sh) both traverse
|
|
144
|
+
// proxied routes where a timeout or 5xx previously blanked the card until the next
|
|
145
|
+
// poll. Persist the last successful windows so a blip serves stale bars instead
|
|
146
|
+
// (mirrors Claude / Codex / OpenCode Go). TTL is 12h, aligned with the DeepSeek
|
|
147
|
+
// balance cache: quota/balance readings age meaningfully within a day, so a
|
|
148
|
+
// week-old fallback would mislead more than it helps (aligned 2026-11-03).
|
|
149
|
+
const CURSOR_LIMITS_CACHE_FILE = "cursor-usage-limits-cache.json";
|
|
150
|
+
const CURSOR_LIMITS_CACHE_MAX_AGE_MS = 12 * 60 * 60 * 1000;
|
|
151
|
+
// The Kimi coding API (api.kimi.com / api.kimi.ai) is another proxied route where
|
|
152
|
+
// a timeout or 5xx previously blanked the card until the next poll. Persist the
|
|
153
|
+
// last successful windows so a blip serves stale bars instead
|
|
154
|
+
// (mirrors Cursor / Codex / OpenCode Go). TTL 12h — see the cursor note above.
|
|
155
|
+
const KIMI_LIMITS_CACHE_FILE = "kimi-usage-limits-cache.json";
|
|
156
|
+
const KIMI_LIMITS_CACHE_MAX_AGE_MS = 12 * 60 * 60 * 1000;
|
|
157
|
+
// Grok Build's billing proxy (cli-chat-proxy.grok.com) sits behind the same kind of
|
|
158
|
+
// proxied routes where a timeout or 5xx previously blanked the card until the next
|
|
159
|
+
// poll. Persist the last successful windows so a blip serves stale bars instead
|
|
160
|
+
// (mirrors Claude / Codex / Cursor). TTL 12h — see the cursor note above.
|
|
161
|
+
const GROK_LIMITS_CACHE_FILE = "grok-usage-limits-cache.json";
|
|
162
|
+
const GROK_LIMITS_CACHE_MAX_AGE_MS = 12 * 60 * 60 * 1000;
|
|
137
163
|
// A 429 from the usage endpoint carries a long `retry-after` (often 20+ minutes). Persist
|
|
138
164
|
// the cooldown so every surface — this process, the menu bar app's embedded server, a later
|
|
139
165
|
// restart — stops calling until it expires. Hammering during the cooldown just renews the
|
|
@@ -786,11 +812,11 @@ function normalizeCursorSandUsageStatus(body, { eligible = true } = {}) {
|
|
|
786
812
|
return buildWindow({ usedPercent, resetAt, windowSeconds });
|
|
787
813
|
}
|
|
788
814
|
|
|
789
|
-
async function fetchCursorLimits({ home, fetchImpl = fetch } = {}) {
|
|
790
|
-
if (!isCursorInstalled({ home })) {
|
|
815
|
+
async function fetchCursorLimits({ home, env, fetchImpl = fetch } = {}) {
|
|
816
|
+
if (!isCursorInstalled({ home, env })) {
|
|
791
817
|
return { configured: false };
|
|
792
818
|
}
|
|
793
|
-
const auth = extractCursorAuth({ home });
|
|
819
|
+
const auth = extractCursorAuth({ home, env });
|
|
794
820
|
if (!auth?.cookie || !auth?.accessToken) {
|
|
795
821
|
return { configured: false };
|
|
796
822
|
}
|
|
@@ -813,11 +839,18 @@ async function fetchCursorLimits({ home, fetchImpl = fetch } = {}) {
|
|
|
813
839
|
error: null,
|
|
814
840
|
...normalizeCursorUsageSummary(body),
|
|
815
841
|
quaternary_window: grokBotWindow,
|
|
842
|
+
// Internal cache-partition key (stripped before the payload is served).
|
|
843
|
+
cache_identity: auth.userId,
|
|
816
844
|
};
|
|
817
845
|
} catch (error) {
|
|
818
846
|
return {
|
|
819
847
|
configured: true,
|
|
820
848
|
error: error?.message || "Unknown error",
|
|
849
|
+
// A session-expiry error is actionable (re-login) and must not be hidden
|
|
850
|
+
// behind the stale disk-cache fallback (opencode-go auth_error precedent).
|
|
851
|
+
// Internal marker — stripped before the payload is served.
|
|
852
|
+
...(error?.code === "AUTH_EXPIRED" ? { auth_error: true } : {}),
|
|
853
|
+
cache_identity: auth.userId,
|
|
821
854
|
};
|
|
822
855
|
}
|
|
823
856
|
}
|
|
@@ -890,7 +923,9 @@ function kimiCredentialsExpired(creds, nowMs = Date.now()) {
|
|
|
890
923
|
|
|
891
924
|
async function refreshKimiAccessToken({ refreshToken, profile, fetchImpl = fetch } = {}) {
|
|
892
925
|
if (typeof refreshToken !== "string" || !refreshToken.trim()) {
|
|
893
|
-
|
|
926
|
+
const error = new Error("Not logged in to Kimi. Run 'kimi' in Terminal to authenticate.");
|
|
927
|
+
error.code = "AUTH_EXPIRED";
|
|
928
|
+
throw error;
|
|
894
929
|
}
|
|
895
930
|
|
|
896
931
|
const body = new URLSearchParams({
|
|
@@ -909,7 +944,9 @@ async function refreshKimiAccessToken({ refreshToken, profile, fetchImpl = fetch
|
|
|
909
944
|
body,
|
|
910
945
|
});
|
|
911
946
|
if (res.status === 401 || res.status === 403) {
|
|
912
|
-
|
|
947
|
+
const error = new Error("Not logged in to Kimi. Run 'kimi' in Terminal to authenticate.");
|
|
948
|
+
error.code = "AUTH_EXPIRED";
|
|
949
|
+
throw error;
|
|
913
950
|
}
|
|
914
951
|
if (!res.ok) {
|
|
915
952
|
throw new Error(`Kimi token refresh failed (HTTP ${res.status})`);
|
|
@@ -943,7 +980,11 @@ async function fetchKimiUsage(accessToken, { profile, fetchImpl = fetch } = {})
|
|
|
943
980
|
},
|
|
944
981
|
});
|
|
945
982
|
if (res.status === 401) {
|
|
946
|
-
|
|
983
|
+
// The retry-with-refresh path keys off this message; the code marks the
|
|
984
|
+
// error actionable (re-login) so the cache fallback never hides it.
|
|
985
|
+
const error = new Error("token_expired");
|
|
986
|
+
error.code = "AUTH_EXPIRED";
|
|
987
|
+
throw error;
|
|
947
988
|
}
|
|
948
989
|
if (!res.ok) {
|
|
949
990
|
throw new Error(`Kimi API returned ${res.status}`);
|
|
@@ -956,12 +997,17 @@ async function fetchKimiLimits({ home, env, fetchImpl = fetch } = {}) {
|
|
|
956
997
|
if (!profile.configured) {
|
|
957
998
|
return { configured: false };
|
|
958
999
|
}
|
|
959
|
-
|
|
1000
|
+
// A profile misconfiguration (unsupported endpoints, non-file credential
|
|
1001
|
+
// storage) is actionable — the user must fix their config — so it must not
|
|
1002
|
+
// be hidden behind the stale disk-cache fallback. Internal marker —
|
|
1003
|
+
// stripped before the payload is served.
|
|
1004
|
+
if (profile.error) return { configured: true, error: profile.error, config_error: true };
|
|
960
1005
|
const creds = loadKimiCredentials({ profile });
|
|
961
1006
|
let accessToken = typeof creds?.access_token === "string" ? creds.access_token.trim() : "";
|
|
962
1007
|
if (!accessToken) {
|
|
963
1008
|
return { configured: false };
|
|
964
1009
|
}
|
|
1010
|
+
const cacheIdentity = kimiProfileIdentity(profile);
|
|
965
1011
|
try {
|
|
966
1012
|
if (kimiCredentialsExpired(creds) && creds?.refresh_token) {
|
|
967
1013
|
accessToken = await refreshKimiAccessToken({
|
|
@@ -989,11 +1035,18 @@ async function fetchKimiLimits({ home, env, fetchImpl = fetch } = {}) {
|
|
|
989
1035
|
configured: true,
|
|
990
1036
|
error: null,
|
|
991
1037
|
...normalizeKimiUsageResponse(body),
|
|
1038
|
+
// Internal cache-partition key (stripped before the payload is served).
|
|
1039
|
+
cache_identity: cacheIdentity,
|
|
992
1040
|
};
|
|
993
1041
|
} catch (error) {
|
|
994
1042
|
return {
|
|
995
1043
|
configured: true,
|
|
996
1044
|
error: error?.message || "Unknown error",
|
|
1045
|
+
// A session-expiry error is actionable (re-login) and must not be hidden
|
|
1046
|
+
// behind the stale disk-cache fallback (opencode-go auth_error precedent).
|
|
1047
|
+
// Internal marker — stripped before the payload is served.
|
|
1048
|
+
...(error?.code === "AUTH_EXPIRED" ? { auth_error: true } : {}),
|
|
1049
|
+
cache_identity: cacheIdentity,
|
|
997
1050
|
};
|
|
998
1051
|
}
|
|
999
1052
|
}
|
|
@@ -2679,6 +2732,323 @@ function writeOpencodeGoLimitsCache(limits, { home, nowMs = Date.now() } = {}) {
|
|
|
2679
2732
|
} catch (_error) {}
|
|
2680
2733
|
}
|
|
2681
2734
|
|
|
2735
|
+
function resolveCursorLimitsCachePath({ home } = {}) {
|
|
2736
|
+
return path.join(home || os.homedir(), ".tokenusage", "tracker", CURSOR_LIMITS_CACHE_FILE);
|
|
2737
|
+
}
|
|
2738
|
+
|
|
2739
|
+
function hasCursorWindow(limits) {
|
|
2740
|
+
return Boolean(
|
|
2741
|
+
limits?.primary_window
|
|
2742
|
+
|| limits?.secondary_window
|
|
2743
|
+
|| limits?.tertiary_window
|
|
2744
|
+
|| limits?.quaternary_window,
|
|
2745
|
+
);
|
|
2746
|
+
}
|
|
2747
|
+
|
|
2748
|
+
// Cursor windows carry `reset_at` as an ISO string (billing-cycle end for the three
|
|
2749
|
+
// summary windows, the RPC's own expiry for the Grok Bot window). A window whose reset
|
|
2750
|
+
// has already passed is stale data, not a usable fallback, so drop it. Windows without
|
|
2751
|
+
// a reset stamp are kept — the overall cached_at max-age gate already bounds them.
|
|
2752
|
+
function isCursorCacheWindowUsable(window, { nowMs } = {}) {
|
|
2753
|
+
if (!window || typeof window !== "object") return false;
|
|
2754
|
+
const resetAtMs = parseTimeMs(window.reset_at);
|
|
2755
|
+
if (resetAtMs === null) return true;
|
|
2756
|
+
return resetAtMs > nowMs;
|
|
2757
|
+
}
|
|
2758
|
+
|
|
2759
|
+
function normalizeCursorCachedLimits(
|
|
2760
|
+
raw,
|
|
2761
|
+
{ nowMs = Date.now(), maxAgeMs = CURSOR_LIMITS_CACHE_MAX_AGE_MS } = {},
|
|
2762
|
+
) {
|
|
2763
|
+
const cachedAtMs = parseTimeMs(raw?.cached_at);
|
|
2764
|
+
if (!Number.isFinite(cachedAtMs)) return null;
|
|
2765
|
+
if (cachedAtMs > nowMs + 60_000) return null;
|
|
2766
|
+
if (nowMs - cachedAtMs > maxAgeMs) return null;
|
|
2767
|
+
|
|
2768
|
+
const cached = {
|
|
2769
|
+
configured: true,
|
|
2770
|
+
error: null,
|
|
2771
|
+
membership_type: typeof raw?.membership_type === "string" ? raw.membership_type : null,
|
|
2772
|
+
primary_window: isCursorCacheWindowUsable(raw?.primary_window, { nowMs }) ? raw.primary_window : null,
|
|
2773
|
+
secondary_window: isCursorCacheWindowUsable(raw?.secondary_window, { nowMs }) ? raw.secondary_window : null,
|
|
2774
|
+
tertiary_window: isCursorCacheWindowUsable(raw?.tertiary_window, { nowMs }) ? raw.tertiary_window : null,
|
|
2775
|
+
quaternary_window: isCursorCacheWindowUsable(raw?.quaternary_window, { nowMs }) ? raw.quaternary_window : null,
|
|
2776
|
+
stale: true,
|
|
2777
|
+
cached_at: raw.cached_at,
|
|
2778
|
+
};
|
|
2779
|
+
return hasCursorWindow(cached) ? cached : null;
|
|
2780
|
+
}
|
|
2781
|
+
|
|
2782
|
+
// The signed-in Cursor account partitions the cache (zcode login-identity precedent):
|
|
2783
|
+
// the id is the non-secret normalized subject Cursor itself puts in the session cookie.
|
|
2784
|
+
// A single home holds one active login, so one stamped entry suffices; a read requires
|
|
2785
|
+
// the stored identity to match the current one, and a write without an identity is
|
|
2786
|
+
// refused — an unreadable login must never pin another account's bars in place.
|
|
2787
|
+
function readCursorLimitsCache({ home, nowMs = Date.now(), identity } = {}) {
|
|
2788
|
+
if (!identity) return null;
|
|
2789
|
+
try {
|
|
2790
|
+
const parsed = JSON.parse(fs.readFileSync(resolveCursorLimitsCachePath({ home }), "utf8"));
|
|
2791
|
+
const entry = parsed?.cursor;
|
|
2792
|
+
if (entry?.identity !== identity) return null;
|
|
2793
|
+
return normalizeCursorCachedLimits(entry, { nowMs });
|
|
2794
|
+
} catch (_error) {
|
|
2795
|
+
return null;
|
|
2796
|
+
}
|
|
2797
|
+
}
|
|
2798
|
+
|
|
2799
|
+
function writeCursorLimitsCache(limits, { home, nowMs = Date.now(), identity } = {}) {
|
|
2800
|
+
if (!identity) return;
|
|
2801
|
+
if (!limits?.configured || limits.error || !hasCursorWindow(limits)) return;
|
|
2802
|
+
const cachePath = resolveCursorLimitsCachePath({ home });
|
|
2803
|
+
const payload = {
|
|
2804
|
+
cursor: {
|
|
2805
|
+
identity,
|
|
2806
|
+
membership_type: limits.membership_type || null,
|
|
2807
|
+
primary_window: limits.primary_window || null,
|
|
2808
|
+
secondary_window: limits.secondary_window || null,
|
|
2809
|
+
tertiary_window: limits.tertiary_window || null,
|
|
2810
|
+
quaternary_window: limits.quaternary_window || null,
|
|
2811
|
+
cached_at: new Date(nowMs).toISOString(),
|
|
2812
|
+
},
|
|
2813
|
+
};
|
|
2814
|
+
try {
|
|
2815
|
+
fs.mkdirSync(path.dirname(cachePath), { recursive: true });
|
|
2816
|
+
const tmpPath = `${cachePath}.${process.pid}.tmp`;
|
|
2817
|
+
fs.writeFileSync(tmpPath, JSON.stringify(payload, null, 2), { encoding: "utf8", mode: 0o600 });
|
|
2818
|
+
fs.renameSync(tmpPath, cachePath);
|
|
2819
|
+
} catch (_error) {}
|
|
2820
|
+
}
|
|
2821
|
+
|
|
2822
|
+
// Failure-only identity re-derivation for the cache fallback: when the provider slot's
|
|
2823
|
+
// result was produced by the outer timeout race, the fetcher's own identity field is
|
|
2824
|
+
// lost, so read the (still-local) state DB once to learn who we are serving for.
|
|
2825
|
+
function resolveCursorCacheIdentity({ home, env } = {}) {
|
|
2826
|
+
try {
|
|
2827
|
+
return extractCursorAuth({ home, env })?.userId || null;
|
|
2828
|
+
} catch (_error) {
|
|
2829
|
+
return null;
|
|
2830
|
+
}
|
|
2831
|
+
}
|
|
2832
|
+
|
|
2833
|
+
// The active Kimi profile partitions the cache (ark profile_identity precedent):
|
|
2834
|
+
// mainland and global logins — and any KIMI_HOME override — resolve to different
|
|
2835
|
+
// endpoint-scoped credential slots, and one slot must never serve another's
|
|
2836
|
+
// windows. The profile home plus OAuth host is non-secret and derivable from
|
|
2837
|
+
// local files alone (the credential file holds no account id).
|
|
2838
|
+
function kimiProfileIdentity(profile) {
|
|
2839
|
+
if (!profile?.home || !profile?.oauthHost) return null;
|
|
2840
|
+
return `${profile.home}::${profile.oauthHost}`;
|
|
2841
|
+
}
|
|
2842
|
+
|
|
2843
|
+
// Failure-only identity re-derivation for the cache fallback: when the provider
|
|
2844
|
+
// slot's result was produced by the outer timeout race, the fetcher's own
|
|
2845
|
+
// identity field is lost, so re-resolve the (still-local) Kimi profile.
|
|
2846
|
+
function resolveKimiCacheIdentity({ home, env } = {}) {
|
|
2847
|
+
try {
|
|
2848
|
+
return kimiProfileIdentity(resolveKimiProfile({ home, env }));
|
|
2849
|
+
} catch (_error) {
|
|
2850
|
+
return null;
|
|
2851
|
+
}
|
|
2852
|
+
}
|
|
2853
|
+
|
|
2854
|
+
function resolveKimiLimitsCachePath({ home } = {}) {
|
|
2855
|
+
return path.join(home || os.homedir(), ".tokenusage", "tracker", KIMI_LIMITS_CACHE_FILE);
|
|
2856
|
+
}
|
|
2857
|
+
|
|
2858
|
+
function hasKimiWindow(limits) {
|
|
2859
|
+
return Boolean(
|
|
2860
|
+
limits?.primary_window
|
|
2861
|
+
|| limits?.secondary_window
|
|
2862
|
+
|| limits?.tertiary_window,
|
|
2863
|
+
);
|
|
2864
|
+
}
|
|
2865
|
+
|
|
2866
|
+
// Kimi windows carry `reset_at` (ISO) when the API reports a cycle end; a window
|
|
2867
|
+
// whose reset has already passed is stale data, not a usable fallback, so drop
|
|
2868
|
+
// it. Windows without a reset stamp are kept — the overall cached_at max-age
|
|
2869
|
+
// gate already bounds them.
|
|
2870
|
+
function isKimiCacheWindowUsable(window, { nowMs } = {}) {
|
|
2871
|
+
if (!window || typeof window !== "object") return false;
|
|
2872
|
+
const resetAtMs = parseTimeMs(window.reset_at);
|
|
2873
|
+
if (resetAtMs === null) return true;
|
|
2874
|
+
return resetAtMs > nowMs;
|
|
2875
|
+
}
|
|
2876
|
+
|
|
2877
|
+
function normalizeKimiCachedLimits(
|
|
2878
|
+
raw,
|
|
2879
|
+
{ nowMs = Date.now(), maxAgeMs = KIMI_LIMITS_CACHE_MAX_AGE_MS } = {},
|
|
2880
|
+
) {
|
|
2881
|
+
const cachedAtMs = parseTimeMs(raw?.cached_at);
|
|
2882
|
+
if (!Number.isFinite(cachedAtMs)) return null;
|
|
2883
|
+
if (cachedAtMs > nowMs + 60_000) return null;
|
|
2884
|
+
if (nowMs - cachedAtMs > maxAgeMs) return null;
|
|
2885
|
+
|
|
2886
|
+
const cached = {
|
|
2887
|
+
configured: true,
|
|
2888
|
+
error: null,
|
|
2889
|
+
membership_level: typeof raw?.membership_level === "string" ? raw.membership_level : null,
|
|
2890
|
+
subscription_type: typeof raw?.subscription_type === "string" ? raw.subscription_type : null,
|
|
2891
|
+
parallel_limit: Number.isFinite(raw?.parallel_limit) ? raw.parallel_limit : null,
|
|
2892
|
+
primary_window: isKimiCacheWindowUsable(raw?.primary_window, { nowMs }) ? raw.primary_window : null,
|
|
2893
|
+
secondary_window: isKimiCacheWindowUsable(raw?.secondary_window, { nowMs }) ? raw.secondary_window : null,
|
|
2894
|
+
tertiary_window: isKimiCacheWindowUsable(raw?.tertiary_window, { nowMs }) ? raw.tertiary_window : null,
|
|
2895
|
+
stale: true,
|
|
2896
|
+
cached_at: raw.cached_at,
|
|
2897
|
+
};
|
|
2898
|
+
return hasKimiWindow(cached) ? cached : null;
|
|
2899
|
+
}
|
|
2900
|
+
|
|
2901
|
+
function readKimiLimitsCache({ home, nowMs = Date.now(), identity } = {}) {
|
|
2902
|
+
if (!identity) return null;
|
|
2903
|
+
try {
|
|
2904
|
+
const parsed = JSON.parse(fs.readFileSync(resolveKimiLimitsCachePath({ home }), "utf8"));
|
|
2905
|
+
const entry = parsed?.kimi;
|
|
2906
|
+
if (entry?.identity !== identity) return null;
|
|
2907
|
+
return normalizeKimiCachedLimits(entry, { nowMs });
|
|
2908
|
+
} catch (_error) {
|
|
2909
|
+
return null;
|
|
2910
|
+
}
|
|
2911
|
+
}
|
|
2912
|
+
|
|
2913
|
+
function writeKimiLimitsCache(limits, { home, nowMs = Date.now(), identity } = {}) {
|
|
2914
|
+
if (!identity) return;
|
|
2915
|
+
if (!limits?.configured || limits.error || !hasKimiWindow(limits)) return;
|
|
2916
|
+
const cachePath = resolveKimiLimitsCachePath({ home });
|
|
2917
|
+
const payload = {
|
|
2918
|
+
kimi: {
|
|
2919
|
+
identity,
|
|
2920
|
+
membership_level: limits.membership_level || null,
|
|
2921
|
+
subscription_type: limits.subscription_type || null,
|
|
2922
|
+
parallel_limit: Number.isFinite(limits.parallel_limit) ? limits.parallel_limit : null,
|
|
2923
|
+
primary_window: limits.primary_window || null,
|
|
2924
|
+
secondary_window: limits.secondary_window || null,
|
|
2925
|
+
tertiary_window: limits.tertiary_window || null,
|
|
2926
|
+
cached_at: new Date(nowMs).toISOString(),
|
|
2927
|
+
},
|
|
2928
|
+
};
|
|
2929
|
+
try {
|
|
2930
|
+
fs.mkdirSync(path.dirname(cachePath), { recursive: true });
|
|
2931
|
+
const tmpPath = `${cachePath}.${process.pid}.tmp`;
|
|
2932
|
+
fs.writeFileSync(tmpPath, JSON.stringify(payload, null, 2), { encoding: "utf8", mode: 0o600 });
|
|
2933
|
+
fs.renameSync(tmpPath, cachePath);
|
|
2934
|
+
} catch (_error) {}
|
|
2935
|
+
}
|
|
2936
|
+
|
|
2937
|
+
function resolveGrokLimitsCachePath({ home } = {}) {
|
|
2938
|
+
return path.join(home || os.homedir(), ".tokenusage", "tracker", GROK_LIMITS_CACHE_FILE);
|
|
2939
|
+
}
|
|
2940
|
+
|
|
2941
|
+
function hasGrokWindow(limits) {
|
|
2942
|
+
return Boolean(limits?.primary_window || limits?.secondary_window);
|
|
2943
|
+
}
|
|
2944
|
+
|
|
2945
|
+
// Grok windows carry `reset_at` as an ISO string — the billing-period end shared
|
|
2946
|
+
// by the pool, on-demand, and per-product windows. A window whose reset has
|
|
2947
|
+
// already passed is stale data, not a usable fallback, so drop it. Windows
|
|
2948
|
+
// without a reset stamp are kept — the overall cached_at max-age gate already
|
|
2949
|
+
// bounds them (mirrors Cursor).
|
|
2950
|
+
function isGrokCacheWindowUsable(window, { nowMs } = {}) {
|
|
2951
|
+
if (!window || typeof window !== "object") return false;
|
|
2952
|
+
const resetAtMs = parseTimeMs(window.reset_at);
|
|
2953
|
+
if (resetAtMs === null) return true;
|
|
2954
|
+
return resetAtMs > nowMs;
|
|
2955
|
+
}
|
|
2956
|
+
|
|
2957
|
+
function normalizeGrokCachedLimits(
|
|
2958
|
+
raw,
|
|
2959
|
+
{ nowMs = Date.now(), maxAgeMs = GROK_LIMITS_CACHE_MAX_AGE_MS } = {},
|
|
2960
|
+
) {
|
|
2961
|
+
const cachedAtMs = parseTimeMs(raw?.cached_at);
|
|
2962
|
+
if (!Number.isFinite(cachedAtMs)) return null;
|
|
2963
|
+
if (cachedAtMs > nowMs + 60_000) return null;
|
|
2964
|
+
if (nowMs - cachedAtMs > maxAgeMs) return null;
|
|
2965
|
+
|
|
2966
|
+
const cached = {
|
|
2967
|
+
configured: true,
|
|
2968
|
+
error: null,
|
|
2969
|
+
plan_label: typeof raw?.plan_label === "string" ? raw.plan_label : null,
|
|
2970
|
+
period_type: typeof raw?.period_type === "string" ? raw.period_type : null,
|
|
2971
|
+
monthly_credits_limit: raw?.monthly_credits_limit ?? null,
|
|
2972
|
+
monthly_credits_used: raw?.monthly_credits_used ?? null,
|
|
2973
|
+
credit_usage_percent: raw?.credit_usage_percent ?? null,
|
|
2974
|
+
on_demand_cap: raw?.on_demand_cap ?? null,
|
|
2975
|
+
on_demand_used: raw?.on_demand_used ?? null,
|
|
2976
|
+
billing_period_start: typeof raw?.billing_period_start === "string" ? raw.billing_period_start : null,
|
|
2977
|
+
primary_window: isGrokCacheWindowUsable(raw?.primary_window, { nowMs }) ? raw.primary_window : null,
|
|
2978
|
+
secondary_window: isGrokCacheWindowUsable(raw?.secondary_window, { nowMs }) ? raw.secondary_window : null,
|
|
2979
|
+
// Per-product shares of the pool, not independent windows — each is gated
|
|
2980
|
+
// by the same reset stamp as the pool window it attributes.
|
|
2981
|
+
products: Array.isArray(raw?.products)
|
|
2982
|
+
? raw.products.filter((entry) => isGrokCacheWindowUsable(entry?.window, { nowMs }))
|
|
2983
|
+
: [],
|
|
2984
|
+
stale: true,
|
|
2985
|
+
cached_at: raw.cached_at,
|
|
2986
|
+
};
|
|
2987
|
+
return hasGrokWindow(cached) ? cached : null;
|
|
2988
|
+
}
|
|
2989
|
+
|
|
2990
|
+
// The signed-in Grok account partitions the cache (zcode login-identity precedent):
|
|
2991
|
+
// the id is the non-secret OIDC `sub` claim inside the access token — the
|
|
2992
|
+
// credential itself never enters the id (no token fingerprint fallback: CodeQL
|
|
2993
|
+
// js/insufficient-password-hash precedent). auth.x.ai issues JWT access tokens
|
|
2994
|
+
// (RFC 9068), so the subject is present in practice; an opaque token yields no
|
|
2995
|
+
// identity and the cache is simply refused on both the write and the read path
|
|
2996
|
+
// (fail-closed) — an unattributable login must never pin another account's bars,
|
|
2997
|
+
// which a scope-key fallback cannot guarantee because `issuer::client_id` stays
|
|
2998
|
+
// constant across account switches.
|
|
2999
|
+
function readGrokLimitsCache({ home, nowMs = Date.now(), identity } = {}) {
|
|
3000
|
+
if (!identity) return null;
|
|
3001
|
+
try {
|
|
3002
|
+
const parsed = JSON.parse(fs.readFileSync(resolveGrokLimitsCachePath({ home }), "utf8"));
|
|
3003
|
+
const entry = parsed?.grok;
|
|
3004
|
+
if (entry?.identity !== identity) return null;
|
|
3005
|
+
return normalizeGrokCachedLimits(entry, { nowMs });
|
|
3006
|
+
} catch (_error) {
|
|
3007
|
+
return null;
|
|
3008
|
+
}
|
|
3009
|
+
}
|
|
3010
|
+
|
|
3011
|
+
function writeGrokLimitsCache(limits, { home, nowMs = Date.now(), identity } = {}) {
|
|
3012
|
+
if (!identity) return;
|
|
3013
|
+
if (!limits?.configured || limits.error || !hasGrokWindow(limits)) return;
|
|
3014
|
+
const cachePath = resolveGrokLimitsCachePath({ home });
|
|
3015
|
+
const payload = {
|
|
3016
|
+
grok: {
|
|
3017
|
+
identity,
|
|
3018
|
+
plan_label: limits.plan_label || null,
|
|
3019
|
+
period_type: limits.period_type || null,
|
|
3020
|
+
monthly_credits_limit: limits.monthly_credits_limit ?? null,
|
|
3021
|
+
monthly_credits_used: limits.monthly_credits_used ?? null,
|
|
3022
|
+
credit_usage_percent: limits.credit_usage_percent ?? null,
|
|
3023
|
+
on_demand_cap: limits.on_demand_cap ?? null,
|
|
3024
|
+
on_demand_used: limits.on_demand_used ?? null,
|
|
3025
|
+
billing_period_start: limits.billing_period_start || null,
|
|
3026
|
+
primary_window: limits.primary_window || null,
|
|
3027
|
+
secondary_window: limits.secondary_window || null,
|
|
3028
|
+
products: Array.isArray(limits.products) ? limits.products : [],
|
|
3029
|
+
cached_at: new Date(nowMs).toISOString(),
|
|
3030
|
+
},
|
|
3031
|
+
};
|
|
3032
|
+
try {
|
|
3033
|
+
fs.mkdirSync(path.dirname(cachePath), { recursive: true });
|
|
3034
|
+
const tmpPath = `${cachePath}.${process.pid}.tmp`;
|
|
3035
|
+
fs.writeFileSync(tmpPath, JSON.stringify(payload, null, 2), { encoding: "utf8", mode: 0o600 });
|
|
3036
|
+
fs.renameSync(tmpPath, cachePath);
|
|
3037
|
+
} catch (_error) {}
|
|
3038
|
+
}
|
|
3039
|
+
|
|
3040
|
+
// Failure-only identity re-derivation for the cache fallback: when the provider
|
|
3041
|
+
// slot's result was produced by the outer timeout race, the fetcher's own
|
|
3042
|
+
// identity field is lost, so read auth.json once to learn who we are serving for.
|
|
3043
|
+
// Opaque tokens yield null → the read is refused (fail-closed, see above).
|
|
3044
|
+
function resolveGrokCacheIdentity({ home, env } = {}) {
|
|
3045
|
+
try {
|
|
3046
|
+
return grokTokenSubject(loadGrokAuthEntry({ home, env })?.entry?.key);
|
|
3047
|
+
} catch (_error) {
|
|
3048
|
+
return null;
|
|
3049
|
+
}
|
|
3050
|
+
}
|
|
3051
|
+
|
|
2682
3052
|
function resolveClaudeRateLimitPath({ home } = {}) {
|
|
2683
3053
|
return path.join(home || os.homedir(), ".tokenusage", "tracker", CLAUDE_RATE_LIMIT_FILE);
|
|
2684
3054
|
}
|
|
@@ -4079,7 +4449,7 @@ async function fetchUsageLimitsUncached({
|
|
|
4079
4449
|
: null;
|
|
4080
4450
|
|
|
4081
4451
|
const providerFetch = withFetchTimeout(fetchImpl, providerTimeoutMs);
|
|
4082
|
-
const [claudeResult, codexResult, cursor, kimi, gemini, kiro, antigravity, copilot, grok, zcode, opencodeGoRaw, qoder, qoderCn, codingPlan, agentPlan, commandCodeRaw, devinRaw, claudeServiceStatus] = await Promise.all([
|
|
4452
|
+
const [claudeResult, codexResult, cursor, kimi, gemini, kiro, antigravity, copilot, grok, zcode, opencodeGoRaw, qoder, qoderCn, codingPlan, agentPlan, commandCodeRaw, devinRaw, deepseek, claudeServiceStatus] = await Promise.all([
|
|
4083
4453
|
claudeToken && !freshClaudeCache && !claudeRetryAtMs
|
|
4084
4454
|
? withProviderTimeout(fetchClaudeUsageLimits(claudeToken, { fetchImpl: providerFetch, maxAttempts: 1 }), "Claude", providerTimeoutMs).then(
|
|
4085
4455
|
(value) => ({ status: "fulfilled", value }),
|
|
@@ -4096,7 +4466,7 @@ async function fetchUsageLimitsUncached({
|
|
|
4096
4466
|
(reason) => ({ status: "rejected", reason }),
|
|
4097
4467
|
)
|
|
4098
4468
|
: Promise.resolve(null),
|
|
4099
|
-
withProviderTimeout(fetchCursorLimits({ home, fetchImpl: providerFetch }), "Cursor", providerTimeoutMs)
|
|
4469
|
+
withProviderTimeout(fetchCursorLimits({ home, env, fetchImpl: providerFetch }), "Cursor", providerTimeoutMs)
|
|
4100
4470
|
.catch((reason) => ({ configured: true, error: reason?.message || "Unknown error" })),
|
|
4101
4471
|
withProviderTimeout(fetchKimiLimits({ home, env, fetchImpl: providerFetch }), "Kimi", providerTimeoutMs)
|
|
4102
4472
|
.catch((reason) => ({ configured: true, error: reason?.message || "Unknown error" })),
|
|
@@ -4204,6 +4574,16 @@ async function fetchUsageLimitsUncached({
|
|
|
4204
4574
|
(value) => ({ status: "fulfilled", value }),
|
|
4205
4575
|
(reason) => ({ status: "rejected", reason }),
|
|
4206
4576
|
),
|
|
4577
|
+
// DeepSeek (platform.deepseek.com, pay-as-you-go): the balance endpoint is
|
|
4578
|
+
// the only documented programmatic source, so this slot yields a balance
|
|
4579
|
+
// card, not quota windows (#960). The last-good disk-cache fallback with a
|
|
4580
|
+
// pure TTL and a key fingerprint binding lives inside deepseek-limits.js;
|
|
4581
|
+
// auth errors bypass it so a rejected key stays actionable.
|
|
4582
|
+
withProviderTimeout(
|
|
4583
|
+
fetchDeepseekLimits({ home, env, fetchImpl: providerFetch }),
|
|
4584
|
+
"DeepSeek",
|
|
4585
|
+
providerTimeoutMs,
|
|
4586
|
+
).catch((reason) => ({ configured: true, error: reason?.message || "Unknown error" })),
|
|
4207
4587
|
// Public status-page probe (fail-soft, own 5-min cache in provider-status.js).
|
|
4208
4588
|
// Only probed for configured accounts — without a token the Claude section
|
|
4209
4589
|
// never renders, so the reading would have nowhere to go.
|
|
@@ -4430,9 +4810,138 @@ async function fetchUsageLimitsUncached({
|
|
|
4430
4810
|
: { configured: true, error: reason?.message || "Unknown error" };
|
|
4431
4811
|
}
|
|
4432
4812
|
|
|
4433
|
-
|
|
4434
|
-
|
|
4435
|
-
|
|
4813
|
+
// Cursor: persist last successful windows so a transient timeout/5xx on the
|
|
4814
|
+
// cursor.com / api2.cursor.sh routes serves stale bars instead of a flashing
|
|
4815
|
+
// "fetch failed" (mirrors Codex / Claude / OpenCode Go). A session-expiry error
|
|
4816
|
+
// (401/403 — "re-login in Cursor") is actionable and must not be hidden behind
|
|
4817
|
+
// stale cache, so it passes through untouched.
|
|
4818
|
+
let cursorWithCache;
|
|
4819
|
+
if (!cursor || cursor.configured === false) {
|
|
4820
|
+
cursorWithCache = cursor;
|
|
4821
|
+
} else if (cursor.auth_error === true) {
|
|
4822
|
+
cursorWithCache = cursor;
|
|
4823
|
+
} else if (!cursor.error && hasCursorWindow(cursor)) {
|
|
4824
|
+
cursorWithCache = {
|
|
4825
|
+
...cursor,
|
|
4826
|
+
stale: false,
|
|
4827
|
+
cached_at: new Date(nowMs).toISOString(),
|
|
4828
|
+
};
|
|
4829
|
+
writeCursorLimitsCache(cursorWithCache, {
|
|
4830
|
+
home,
|
|
4831
|
+
nowMs,
|
|
4832
|
+
identity: typeof cursor.cache_identity === "string"
|
|
4833
|
+
? cursor.cache_identity
|
|
4834
|
+
: resolveCursorCacheIdentity({ home, env }),
|
|
4835
|
+
});
|
|
4836
|
+
} else {
|
|
4837
|
+
// The slot's error object loses the fetcher's identity field when the outer
|
|
4838
|
+
// timeout race wins (the common failure mode this cache exists for), so
|
|
4839
|
+
// re-derive it from the local state DB on this failure-only path.
|
|
4840
|
+
const identity = typeof cursor.cache_identity === "string"
|
|
4841
|
+
? cursor.cache_identity
|
|
4842
|
+
: resolveCursorCacheIdentity({ home, env });
|
|
4843
|
+
const cached = readCursorLimitsCache({ home, nowMs, identity });
|
|
4844
|
+
cursorWithCache = cached || cursor;
|
|
4845
|
+
}
|
|
4846
|
+
// cache_identity / auth_error are internal cache-plumbing markers, not payload.
|
|
4847
|
+
if (cursorWithCache && ("cache_identity" in cursorWithCache || "auth_error" in cursorWithCache)) {
|
|
4848
|
+
const { cache_identity: _identity, auth_error: _authError, ...cursorPayload } = cursorWithCache;
|
|
4849
|
+
cursorWithCache = cursorPayload;
|
|
4850
|
+
}
|
|
4851
|
+
|
|
4852
|
+
// Grok: persist last successful windows so a transient timeout/5xx on the
|
|
4853
|
+
// cli-chat-proxy.grok.com route serves stale bars instead of a flashing
|
|
4854
|
+
// "fetch failed" (mirrors Codex / Claude / Cursor). A re-login error
|
|
4855
|
+
// (GROK_AUTH_REQUIRED / GROK_REAUTH_REQUIRED — "run `grok login`") is
|
|
4856
|
+
// actionable and must not be hidden behind stale cache, so it passes
|
|
4857
|
+
// through untouched.
|
|
4858
|
+
let grokWithCache;
|
|
4859
|
+
if (!grok || grok.configured === false) {
|
|
4860
|
+
grokWithCache = grok;
|
|
4861
|
+
} else if (grok.auth_error === true) {
|
|
4862
|
+
grokWithCache = grok;
|
|
4863
|
+
} else if (!grok.error && hasGrokWindow(grok)) {
|
|
4864
|
+
grokWithCache = {
|
|
4865
|
+
...grok,
|
|
4866
|
+
stale: false,
|
|
4867
|
+
cached_at: new Date(nowMs).toISOString(),
|
|
4868
|
+
};
|
|
4869
|
+
// Write only under the identity the fetch itself carried: re-reading
|
|
4870
|
+
// auth.json here could attribute a finished fetch's windows to an account
|
|
4871
|
+
// that logged in while the request was in flight (opaque → JWT switch).
|
|
4872
|
+
// No fetch-side identity (opaque token) → refuse the write (fail-closed).
|
|
4873
|
+
if (typeof grok.cache_identity === "string") {
|
|
4874
|
+
writeGrokLimitsCache(grokWithCache, {
|
|
4875
|
+
home,
|
|
4876
|
+
nowMs,
|
|
4877
|
+
identity: grok.cache_identity,
|
|
4878
|
+
});
|
|
4879
|
+
}
|
|
4880
|
+
} else {
|
|
4881
|
+
// The slot's error object loses the fetcher's identity field when the outer
|
|
4882
|
+
// timeout race wins (the common failure mode this cache exists for), so
|
|
4883
|
+
// re-derive it from auth.json on this failure-only path.
|
|
4884
|
+
const identity = typeof grok.cache_identity === "string"
|
|
4885
|
+
? grok.cache_identity
|
|
4886
|
+
: resolveGrokCacheIdentity({ home, env });
|
|
4887
|
+
const cached = readGrokLimitsCache({ home, nowMs, identity });
|
|
4888
|
+
grokWithCache = cached || grok;
|
|
4889
|
+
}
|
|
4890
|
+
// cache_identity / auth_error are internal cache-plumbing markers, not payload.
|
|
4891
|
+
if (grokWithCache && ("cache_identity" in grokWithCache || "auth_error" in grokWithCache)) {
|
|
4892
|
+
const { cache_identity: _identity, auth_error: _authError, ...grokPayload } = grokWithCache;
|
|
4893
|
+
grokWithCache = grokPayload;
|
|
4894
|
+
}
|
|
4895
|
+
|
|
4896
|
+
const cursorLimits = cursorSandDuplicatesGrokPool(cursorWithCache, grokWithCache)
|
|
4897
|
+
? { ...cursorWithCache, quaternary_window: null }
|
|
4898
|
+
: cursorWithCache;
|
|
4899
|
+
|
|
4900
|
+
// Kimi: persist last successful windows so a transient timeout/5xx on the
|
|
4901
|
+
// api.kimi.com / api.kimi.ai coding routes serves stale bars instead of a
|
|
4902
|
+
// flashing "fetch failed" (mirrors Cursor / Codex / OpenCode Go). Re-login
|
|
4903
|
+
// errors ("Not logged in to Kimi" / "token_expired") and actionable profile
|
|
4904
|
+
// misconfigurations must not be hidden behind stale cache, so they pass
|
|
4905
|
+
// through untouched.
|
|
4906
|
+
let kimiWithCache;
|
|
4907
|
+
if (!kimi || kimi.configured === false) {
|
|
4908
|
+
kimiWithCache = kimi;
|
|
4909
|
+
} else if (kimi.auth_error === true || kimi.config_error === true) {
|
|
4910
|
+
kimiWithCache = kimi;
|
|
4911
|
+
} else if (!kimi.error && hasKimiWindow(kimi)) {
|
|
4912
|
+
kimiWithCache = {
|
|
4913
|
+
...kimi,
|
|
4914
|
+
stale: false,
|
|
4915
|
+
cached_at: new Date(nowMs).toISOString(),
|
|
4916
|
+
};
|
|
4917
|
+
writeKimiLimitsCache(kimiWithCache, {
|
|
4918
|
+
home,
|
|
4919
|
+
nowMs,
|
|
4920
|
+
identity: typeof kimi.cache_identity === "string"
|
|
4921
|
+
? kimi.cache_identity
|
|
4922
|
+
: resolveKimiCacheIdentity({ home, env }),
|
|
4923
|
+
});
|
|
4924
|
+
} else {
|
|
4925
|
+
// The slot's error object loses the fetcher's identity field when the outer
|
|
4926
|
+
// timeout race wins (the common failure mode this cache exists for), so
|
|
4927
|
+
// re-derive it from the local Kimi profile on this failure-only path.
|
|
4928
|
+
const identity = typeof kimi.cache_identity === "string"
|
|
4929
|
+
? kimi.cache_identity
|
|
4930
|
+
: resolveKimiCacheIdentity({ home, env });
|
|
4931
|
+
const cached = readKimiLimitsCache({ home, nowMs, identity });
|
|
4932
|
+
kimiWithCache = cached || kimi;
|
|
4933
|
+
}
|
|
4934
|
+
// cache_identity / auth_error / config_error are internal cache-plumbing
|
|
4935
|
+
// markers, not payload.
|
|
4936
|
+
if (kimiWithCache && ("cache_identity" in kimiWithCache || "auth_error" in kimiWithCache || "config_error" in kimiWithCache)) {
|
|
4937
|
+
const {
|
|
4938
|
+
cache_identity: _kimiIdentity,
|
|
4939
|
+
auth_error: _kimiAuthError,
|
|
4940
|
+
config_error: _kimiConfigError,
|
|
4941
|
+
...kimiPayload
|
|
4942
|
+
} = kimiWithCache;
|
|
4943
|
+
kimiWithCache = kimiPayload;
|
|
4944
|
+
}
|
|
4436
4945
|
|
|
4437
4946
|
const data = {
|
|
4438
4947
|
fetched_at: new Date(nowMs).toISOString(),
|
|
@@ -4443,7 +4952,7 @@ async function fetchUsageLimitsUncached({
|
|
|
4443
4952
|
// tier, and membership.level is an opaque enum (LEVEL_INTERMEDIATE) with no
|
|
4444
4953
|
// authoritative human-readable plan mapping. Both rendered as garbage like
|
|
4445
4954
|
// "Kimi Type_event" (issue #130), so show the bare brand instead.
|
|
4446
|
-
kimi: withPlanLabel(
|
|
4955
|
+
kimi: withPlanLabel(kimiWithCache, null, "Kimi"),
|
|
4447
4956
|
gemini: withPlanLabel(gemini, gemini.account_plan, "Gemini"),
|
|
4448
4957
|
kiro: withPlanLabel(kiro, kiro.plan_name, "Kiro"),
|
|
4449
4958
|
antigravity: withPlanLabel(antigravity, antigravity.account_plan, "Antigravity"),
|
|
@@ -4454,7 +4963,7 @@ async function fetchUsageLimitsUncached({
|
|
|
4454
4963
|
// outright, so surface the label as-is like CommandCode below. Error and
|
|
4455
4964
|
// unconfigured branches never carry plan_label, so the panel still falls
|
|
4456
4965
|
// back to the bare brand name.
|
|
4457
|
-
grok,
|
|
4966
|
+
grok: grokWithCache,
|
|
4458
4967
|
zcode: withPlanLabel(zcode, zcode.plan_label, "ZCode"),
|
|
4459
4968
|
opencodeGo: withPlanLabel(opencodeGo, opencodeGo?.plan_label, "OpenCode Go"),
|
|
4460
4969
|
// CommandCode tiers are acronym brands (GOAT/Pro/Max) — the fetcher already
|
|
@@ -4468,6 +4977,10 @@ async function fetchUsageLimitsUncached({
|
|
|
4468
4977
|
qoderCn: withPlanLabel(qoderCn, qoderCn?.plan_label, "Qoder CN"),
|
|
4469
4978
|
codingPlan: withPlanLabel(codingPlan, codingPlan?.plan_label, "Ark Coding Plan"),
|
|
4470
4979
|
agentPlan: withPlanLabel(agentPlan, agentPlan?.plan_label, "Ark Agent Plan"),
|
|
4980
|
+
// DeepSeek carries a balance card (no windows, no plan label): the fetcher
|
|
4981
|
+
// stamps cached_at/stale on both its live and last-good paths, so pass it
|
|
4982
|
+
// through verbatim like grok.
|
|
4983
|
+
deepseek,
|
|
4471
4984
|
};
|
|
4472
4985
|
|
|
4473
4986
|
for (const [providerName, provider] of Object.entries(data)) {
|
|
@@ -4547,6 +5060,7 @@ module.exports = {
|
|
|
4547
5060
|
fetchZcodeLimits,
|
|
4548
5061
|
fetchOpencodeGoLimits,
|
|
4549
5062
|
fetchCommandcodeLimits,
|
|
5063
|
+
fetchDeepseekLimits,
|
|
4550
5064
|
fetchQoderLimits,
|
|
4551
5065
|
fetchQoderCnLimits,
|
|
4552
5066
|
};
|