dsh-keys-manager 0.2.8 → 0.2.10
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 +31 -0
- package/lib/client/format.d.ts +17 -10
- package/lib/client/store.d.ts +8 -1
- package/lib/client/styles.d.ts +1 -1
- package/lib/client/types.d.ts +2 -1
- package/lib/client.js +120 -137
- package/lib/core.d.ts +31 -0
- package/lib/core.js +77 -0
- package/lib/core.js.map +1 -1
- package/lib/domain.d.ts +6 -2
- package/lib/index.d.ts +66 -4
- package/lib/index.js +347 -27
- package/lib/index.js.map +1 -1
- package/lib/quota/cache.d.ts +14 -0
- package/lib/quota/cache.js +61 -8
- package/lib/quota/cache.js.map +1 -1
- package/lib/quota/fetch.d.ts +7 -1
- package/lib/quota/fetch.js.map +1 -1
- package/lib/quota/model.d.ts +8 -4
- package/lib/quota/model.js +0 -12
- package/lib/quota/model.js.map +1 -1
- package/lib/quota/providers.d.ts +46 -0
- package/lib/quota/providers.js +242 -3
- package/lib/quota/providers.js.map +1 -1
- package/locale/en.json +3 -5
- package/locale/zh.json +3 -5
- package/package.json +2 -2
package/lib/quota/cache.d.ts
CHANGED
|
@@ -37,6 +37,12 @@ export interface QuotaCacheOptions {
|
|
|
37
37
|
export declare class QuotaCache {
|
|
38
38
|
private readonly entries;
|
|
39
39
|
private readonly inFlight;
|
|
40
|
+
/**
|
|
41
|
+
* The surviving key ids per provider after the last `retain`. A loader that
|
|
42
|
+
* settles after its key was evicted must not write the entry back (the same
|
|
43
|
+
* bug class as an evicted row resurrecting on a later read).
|
|
44
|
+
*/
|
|
45
|
+
private readonly liveKeys;
|
|
40
46
|
private readonly ttlMs;
|
|
41
47
|
private readonly jitterRatio;
|
|
42
48
|
private readonly failureBackoffMs;
|
|
@@ -56,6 +62,14 @@ export declare class QuotaCache {
|
|
|
56
62
|
* state path uses this so a card render can never reach a vendor (D2).
|
|
57
63
|
*/
|
|
58
64
|
peek(provider: string, keyId: string): QuotaSnapshotView | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* Drop entries that no longer describe a live account (`I-000297`).
|
|
67
|
+
* `keep` returns the ids the provider record still holds; `undefined` drops
|
|
68
|
+
* the whole provider (a released route keeps nothing).
|
|
69
|
+
*/
|
|
70
|
+
retain(provider: string, keep: ReadonlySet<string> | undefined): void;
|
|
71
|
+
/** Whether a cache key still belongs to the provider's retained key set. */
|
|
72
|
+
private keyIsLive;
|
|
59
73
|
private retryDelay;
|
|
60
74
|
private settle;
|
|
61
75
|
}
|
package/lib/quota/cache.js
CHANGED
|
@@ -9,6 +9,12 @@ const DEFAULT_POLICY = {
|
|
|
9
9
|
export class QuotaCache {
|
|
10
10
|
entries = new Map();
|
|
11
11
|
inFlight = new Map();
|
|
12
|
+
/**
|
|
13
|
+
* The surviving key ids per provider after the last `retain`. A loader that
|
|
14
|
+
* settles after its key was evicted must not write the entry back (the same
|
|
15
|
+
* bug class as an evicted row resurrecting on a later read).
|
|
16
|
+
*/
|
|
17
|
+
liveKeys = new Map();
|
|
12
18
|
ttlMs;
|
|
13
19
|
jitterRatio;
|
|
14
20
|
failureBackoffMs;
|
|
@@ -47,14 +53,24 @@ export class QuotaCache {
|
|
|
47
53
|
const previous = this.entries.get(key);
|
|
48
54
|
// The cache owns the clock, so probe time and test time cannot diverge.
|
|
49
55
|
const stamped = { ...result, fetchedAt: now };
|
|
50
|
-
|
|
51
|
-
|
|
56
|
+
// Only transient outcomes keep the last-good numbers for display. A
|
|
57
|
+
// deterministic fact about the credential (`unauthorized`, or the
|
|
58
|
+
// host-synthesized `natively-managed`) must replace them: serving old
|
|
59
|
+
// numbers there would hide a refusal or an expired credential for up to
|
|
60
|
+
// `staleMs`, and no client-side retry could ever surface the truth.
|
|
61
|
+
const transient = stamped.status === 'error' || stamped.status === 'absent';
|
|
62
|
+
const lastGood = stamped.status === 'ok' ? stamped : transient ? previous?.lastGood : undefined;
|
|
63
|
+
const keepLastGood = transient && lastGood !== undefined && now - lastGood.fetchedAt <= this.staleMs;
|
|
52
64
|
const served = keepLastGood && lastGood !== undefined ? { ...lastGood, stale: true } : { ...stamped, stale: false };
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
65
|
+
// Write-back only while the key is still live: `retain` may have evicted it
|
|
66
|
+
// while the probe was in flight.
|
|
67
|
+
if (this.keyIsLive(key)) {
|
|
68
|
+
this.entries.set(key, {
|
|
69
|
+
snapshot: served,
|
|
70
|
+
nextAttemptAt: now + this.retryDelay(stamped.status),
|
|
71
|
+
lastGood,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
58
74
|
return served;
|
|
59
75
|
}
|
|
60
76
|
/**
|
|
@@ -62,7 +78,44 @@ export class QuotaCache {
|
|
|
62
78
|
* state path uses this so a card render can never reach a vendor (D2).
|
|
63
79
|
*/
|
|
64
80
|
peek(provider, keyId) {
|
|
65
|
-
|
|
81
|
+
const snapshot = this.entries.get(`${provider}\u0000${keyId}`)?.snapshot;
|
|
82
|
+
// Copy-on-read: the windows array is shared with the entry, and a caller
|
|
83
|
+
// that mutates it (sorting for display, splicing a window) must not corrupt
|
|
84
|
+
// what the next render or the ranking reads.
|
|
85
|
+
return snapshot === undefined ? undefined : { ...snapshot, windows: [...snapshot.windows] };
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Drop entries that no longer describe a live account (`I-000297`).
|
|
89
|
+
* `keep` returns the ids the provider record still holds; `undefined` drops
|
|
90
|
+
* the whole provider (a released route keeps nothing).
|
|
91
|
+
*/
|
|
92
|
+
retain(provider, keep) {
|
|
93
|
+
const prefix = `${provider}\u0000`;
|
|
94
|
+
const live = new Set(keep ?? []);
|
|
95
|
+
// Remember the surviving set first: a loader that settles from now on is
|
|
96
|
+
// fenced by it (a released provider keeps an empty set, so nothing is
|
|
97
|
+
// written back either).
|
|
98
|
+
this.liveKeys.set(provider, live);
|
|
99
|
+
for (const key of [...this.entries.keys()]) {
|
|
100
|
+
if (!key.startsWith(prefix))
|
|
101
|
+
continue;
|
|
102
|
+
if (!live.has(key.slice(prefix.length)))
|
|
103
|
+
this.entries.delete(key);
|
|
104
|
+
}
|
|
105
|
+
for (const key of [...this.inFlight.keys()]) {
|
|
106
|
+
if (!key.startsWith(prefix))
|
|
107
|
+
continue;
|
|
108
|
+
if (!live.has(key.slice(prefix.length)))
|
|
109
|
+
this.inFlight.delete(key);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/** Whether a cache key still belongs to the provider's retained key set. */
|
|
113
|
+
keyIsLive(key) {
|
|
114
|
+
const separator = key.indexOf('\u0000');
|
|
115
|
+
if (separator < 0)
|
|
116
|
+
return true;
|
|
117
|
+
const live = this.liveKeys.get(key.slice(0, separator));
|
|
118
|
+
return live === undefined || live.has(key.slice(separator + 1));
|
|
66
119
|
}
|
|
67
120
|
retryDelay(status) {
|
|
68
121
|
if (status === 'ok') {
|
package/lib/quota/cache.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache.js","sourceRoot":"","sources":["../../src/quota/cache.ts"],"names":[],"mappings":"AAsCA,MAAM,cAAc,GAAG;IACrB,KAAK,EAAE,OAAO;IACd,WAAW,EAAE,IAAI;IACjB,gBAAgB,EAAE,MAAM;IACxB,qBAAqB,EAAE,MAAM;IAC7B,OAAO,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI;CACpB,CAAA;AASV,yEAAyE;AACzE,MAAM,OAAO,UAAU;IACJ,OAAO,GAAG,IAAI,GAAG,EAAsB,CAAA;IACvC,QAAQ,GAAG,IAAI,GAAG,EAAsC,CAAA;
|
|
1
|
+
{"version":3,"file":"cache.js","sourceRoot":"","sources":["../../src/quota/cache.ts"],"names":[],"mappings":"AAsCA,MAAM,cAAc,GAAG;IACrB,KAAK,EAAE,OAAO;IACd,WAAW,EAAE,IAAI;IACjB,gBAAgB,EAAE,MAAM;IACxB,qBAAqB,EAAE,MAAM;IAC7B,OAAO,EA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IAAI,EAAE,CAAC;YACpB,MAAM,MAAM,GAAG,IAAI,CAAC,WAAW,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAA;YACzD,OAAO,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,CAAA;QAClC,CAAC;QACD,OAAO,MAAM,KAAK,cAAc,CAAC,CAAC,CAAC,IAAI,CAAC,qBAAqB,CAAC,CAAC,CAAC,IAAI,CAAC,gBAAgB,CAAA;IACvF,CAAC;IAEO,KAAK,CAAC,MAAM,CAAC,IAAkC,EAAE,GAAW;QAClE,IAAI,CAAC;YACH,OAAO,MAAM,IAAI,EAAE,CAAA;QACrB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO;gBACL,MAAM,EAAE,OAAO;gBACf,SAAS,EAAE,GAAG;gBACd,OAAO,EAAE,EAAE;gBACX,MAAM,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,oBAAoB;aACtE,CAAA;QACH,CAAC;IACH,CAAC;CACF"}
|
package/lib/quota/fetch.d.ts
CHANGED
|
@@ -12,13 +12,19 @@
|
|
|
12
12
|
import type { QuotaStatus } from './model.js';
|
|
13
13
|
/** Injected for tests; the platform `fetch` in production. */
|
|
14
14
|
export type QuotaFetch = typeof fetch;
|
|
15
|
+
/**
|
|
16
|
+
* The failure classes a probe read can produce. `natively-managed` is
|
|
17
|
+
* host-synthesized (the route is served by a projection this process cannot
|
|
18
|
+
* read), so the probe layer must not be able to name it (`I-000315`).
|
|
19
|
+
*/
|
|
20
|
+
export type QuotaReadFailure = Exclude<QuotaStatus, 'ok' | 'natively-managed'>;
|
|
15
21
|
/** A parsed 2xx body, or the classified reason the read produced nothing. */
|
|
16
22
|
export type QuotaRead = {
|
|
17
23
|
ok: true;
|
|
18
24
|
body: Record<string, unknown>;
|
|
19
25
|
} | {
|
|
20
26
|
ok: false;
|
|
21
|
-
status:
|
|
27
|
+
status: QuotaReadFailure;
|
|
22
28
|
reason: string;
|
|
23
29
|
};
|
|
24
30
|
/** Per-call overrides; all optional so probes stay one-liners. */
|
package/lib/quota/fetch.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.js","sourceRoot":"","sources":["../../src/quota/fetch.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"fetch.js","sourceRoot":"","sources":["../../src/quota/fetch.ts"],"names":[],"mappings":"AAmCA,mCAAmC;AACnC,MAAM,CAAC,MAAM,kBAAkB,GAAG,MAAM,CAAA;AAExC,6DAA6D;AAC7D,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,GAAW,EACX,OAA+B,EAC/B,UAA4B,EAAE;IAE9B,MAAM,EAAE,KAAK,EAAE,SAAS,GAAG,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,GAAG,kBAAkB,EAAE,GAAG,OAAO,CAAA;IAC/F,MAAM,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,SAAS,CAAC,CAAA;IAC9C,MAAM,QAAQ,GAAG,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAA;IACpF,IAAI,CAAC;QACH,MAAM,QAAQ,GAAG,MAAM,SAAS,CAAC,GAAG,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAA;QACpE,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG,EAAE,CAAC;YACvD,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,EAAE,QAAQ,QAAQ,CAAC,MAAM,EAAE,EAAE,CAAA;QACjF,CAAC;QACD,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG;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|
|
|
10
10
|
*
|
|
11
11
|
* @module dsh-keys-manager/quota/model
|
|
12
12
|
*/
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
import type { QuotaWindowId } from '../domain.js';
|
|
14
|
+
/**
|
|
15
|
+
* Outcome class of one quota read. Only `ok` carries usable windows; probes
|
|
16
|
+
* produce the first four, while `natively-managed` is host-only: the route is
|
|
17
|
+
* served by a DSH-managed projection whose credential this process cannot read.
|
|
18
|
+
*/
|
|
19
|
+
export type QuotaStatus = 'ok' | 'absent' | 'unauthorized' | 'error' | 'natively-managed';
|
|
20
|
+
export type { QuotaWindowId };
|
|
17
21
|
/** One usage window of a managed account's plan. */
|
|
18
22
|
export interface QuotaWindow {
|
|
19
23
|
id: QuotaWindowId;
|
package/lib/quota/model.js
CHANGED
|
@@ -1,15 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Quota-domain types and the pure unit normalizers every probe shares.
|
|
3
|
-
*
|
|
4
|
-
* Vendors disagree on nearly every unit: Claude reports *used* utilization
|
|
5
|
-
* and an ISO reset stamp, Codex reports used percent with epoch seconds or a
|
|
6
|
-
* relative delay under aliased keys. Normalizing here keeps the probes thin
|
|
7
|
-
* and keeps "used" vs "remaining" invertible in one tested place. Nothing in
|
|
8
|
-
* this module touches the network, the clock, the filesystem, or key
|
|
9
|
-
* selection state.
|
|
10
|
-
*
|
|
11
|
-
* @module dsh-keys-manager/quota/model
|
|
12
|
-
*/
|
|
13
1
|
function clampFraction(value) {
|
|
14
2
|
return Math.min(1, Math.max(0, value));
|
|
15
3
|
}
|
package/lib/quota/model.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"model.js","sourceRoot":"","sources":["../../src/quota/model.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"model.js","sourceRoot":"","sources":["../../src/quota/model.ts"],"names":[],"mappings":"AA2CA,SAAS,aAAa,CAAC,KAAa;IAClC,OAAO,IAAI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;;GAGG;AACH,MAAM,UAAU,SAAS,CAAC,KAAc;IACtC,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,0EAA0E;QAC1E,OAAO,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,KAAK,CAAC,CAAA;IAC9E,CAAC;IACD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC;QAAE,OAAO,YAAY,CAAC,KAAK,CAAC,CAAA;IAC7E,OAAO,SAAS,CAAA;AAClB,CAAC"}
|
package/lib/quota/providers.d.ts
CHANGED
|
@@ -4,6 +4,10 @@ import type { QuotaSnapshot } from './model.js';
|
|
|
4
4
|
export interface QuotaProbeInput {
|
|
5
5
|
token: string;
|
|
6
6
|
accountId?: string;
|
|
7
|
+
/** Organization scope, only consumed by the Factory Droid probe. */
|
|
8
|
+
orgId?: string;
|
|
9
|
+
/** API host residency (`'eu'` selects the EU host), only for Factory Droid. */
|
|
10
|
+
region?: string;
|
|
7
11
|
signal?: AbortSignal;
|
|
8
12
|
/** Injected for tests; the platform `fetch` in production. */
|
|
9
13
|
fetch?: QuotaFetch;
|
|
@@ -39,6 +43,48 @@ export declare const minimaxQuotaProbe: QuotaProbe;
|
|
|
39
43
|
* the subscription anniversary, so it is deliberately not mapped to `week`.
|
|
40
44
|
*/
|
|
41
45
|
export declare const opencodeGoQuotaProbe: QuotaProbe;
|
|
46
|
+
/**
|
|
47
|
+
* Command Code plan windows. The CLI's account route reports a credit balance
|
|
48
|
+
* plus `windowLimits.{fiveHour,weekly}` of `{ used, cap, resetAt }`; a
|
|
49
|
+
* pay-as-you-go account carries only the balance, which has no window at all
|
|
50
|
+
* and therefore no usage the card can render — that case is `error`, never a
|
|
51
|
+
* fabricated 0%.
|
|
52
|
+
*
|
|
53
|
+
* shortcut: the org pool needs `/alpha/whoami` first to learn `orgId`; this
|
|
54
|
+
* probe reads the personal balance only and adds the org query when a managed
|
|
55
|
+
* org account must show its own pool.
|
|
56
|
+
*/
|
|
57
|
+
export declare const commandcodeQuotaProbe: QuotaProbe;
|
|
58
|
+
/**
|
|
59
|
+
* SuperGrok (xAI OAuth) weekly credits through the Grok CLI billing proxy.
|
|
60
|
+
* The payload is `{ config: { currentPeriod: { start, end, type } ,
|
|
61
|
+
* creditUsagePercent, isUnifiedBillingUser } }`; a fresh weekly period omits
|
|
62
|
+
* the percent, which means 0 % used while that period is still open.
|
|
63
|
+
*
|
|
64
|
+
* Only an explicit WEEK period maps: this UI has `session` and `week` windows,
|
|
65
|
+
* and a monthly included quota is neither. A unified-billing account needs a
|
|
66
|
+
* second (default) billing payload to say which counter gates requests — this
|
|
67
|
+
* probe reads the credits payload alone, so unified is `error` rather than a
|
|
68
|
+
* week that may not apply.
|
|
69
|
+
*/
|
|
70
|
+
export declare const xaiQuotaProbe: QuotaProbe;
|
|
71
|
+
/**
|
|
72
|
+
* ClinePass limits, read with the stored Cline API key exactly as Cline's own
|
|
73
|
+
* client reads them (magpie `internal/provider/cline_usage.go`): the endpoint
|
|
74
|
+
* answers a key as it answers an account. `limits[]` carries `type`
|
|
75
|
+
* (`five_hour` / `weekly` / `monthly`), `percentUsed` (USED percent) and an
|
|
76
|
+
* ISO `resetsAt`.
|
|
77
|
+
*
|
|
78
|
+
* The monthly limit has no window in this UI (only `session` and `week`), so
|
|
79
|
+
* it is dropped; an account whose only limit is monthly reads `error`. A 404
|
|
80
|
+
* is Cline's "no ClinePass on this account" and stays `absent`.
|
|
81
|
+
*
|
|
82
|
+
* shortcut: the credit balance needs `/users/me` and then
|
|
83
|
+
* `/users/{id}/balance` (two more calls) and is not a window; add it only if a
|
|
84
|
+
* card must show the remaining dollars.
|
|
85
|
+
*/
|
|
86
|
+
export declare const clineQuotaProbe: QuotaProbe;
|
|
87
|
+
export declare const factoryDroidQuotaProbe: QuotaProbe;
|
|
42
88
|
/** The probe for a route id, or `undefined` when the provider exposes no usage API. */
|
|
43
89
|
export declare function quotaProbe(provider: string): QuotaProbe | undefined;
|
|
44
90
|
/**
|
package/lib/quota/providers.js
CHANGED
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Probes are deliberately lenient: private endpoints drift, fields come back
|
|
5
5
|
* missing or under aliases, and a card must degrade to `absent`/`error`
|
|
6
|
-
* instead of throwing. This release ships Claude, Codex, Z.AI, Kimi, MiniMax
|
|
7
|
-
*
|
|
8
|
-
* never to a guessed endpoint. Nothing here
|
|
6
|
+
* instead of throwing. This release ships Claude, Codex, Z.AI, Kimi, MiniMax,
|
|
7
|
+
* OpenCode Go, Command Code, xAI (SuperGrok) and Cline; any other route id has
|
|
8
|
+
* no probe and resolves to `absent`, never to a guessed endpoint. Nothing here
|
|
9
|
+
* writes cooldown or failure state.
|
|
9
10
|
*
|
|
10
11
|
* @module dsh-keys-manager/quota/providers
|
|
11
12
|
*/
|
|
@@ -18,6 +19,33 @@ const ZAI_QUOTA_URL = 'https://api.z.ai/api/monitor/usage/quota/limit';
|
|
|
18
19
|
const KIMI_USAGE_URL = 'https://api.kimi.com/coding/v1/usages';
|
|
19
20
|
const MINIMAX_QUOTA_URL = 'https://api.minimax.io/v1/api/openplatform/coding_plan/remains';
|
|
20
21
|
const OPENCODE_GO_USAGE_URL = 'https://opencode.ai/zen/go/v1/usage';
|
|
22
|
+
const COMMANDCODE_CREDITS_URL = 'https://api.commandcode.ai/alpha/billing/credits';
|
|
23
|
+
const XAI_BILLING_URL = 'https://cli-chat-proxy.grok.com/v1/billing?format=credits';
|
|
24
|
+
/**
|
|
25
|
+
* The Grok CLI billing proxy sits behind the CLI product gate: without this
|
|
26
|
+
* header the endpoint answers an auth error that would read as `unauthorized`
|
|
27
|
+
* (plan risk R2). The `User-Agent` is part of the same gate.
|
|
28
|
+
*/
|
|
29
|
+
const XAI_CLI_TOKEN_AUTH = 'xai-grok-cli';
|
|
30
|
+
const XAI_CLI_USER_AGENT = 'grok-cli/0.1 (dsh-keys-manager)';
|
|
31
|
+
const CLINE_USAGE_URL = 'https://api.cline.bot/api/v1/users/me/plan/usage-limits';
|
|
32
|
+
/**
|
|
33
|
+
* Cline's desktop client headers, as magpie sends them
|
|
34
|
+
* (`internal/provider/cline.go`): the API gates product-only surfaces on
|
|
35
|
+
* `X-CLIENT-TYPE`, and `X-CLIENT-VERSION` is the desktop release magpie
|
|
36
|
+
* claims. They are vendor constants, not credentials.
|
|
37
|
+
*/
|
|
38
|
+
const CLINE_CLIENT_VERSION = '3.5.54';
|
|
39
|
+
// shortcut: version pinned from magpie 3.5.54; bump it if the endpoint starts version-gating
|
|
40
|
+
const CLINE_CLIENT_HEADERS = Object.freeze({
|
|
41
|
+
Accept: 'application/json',
|
|
42
|
+
'HTTP-Referer': 'https://cline.bot',
|
|
43
|
+
'X-Title': 'Cline',
|
|
44
|
+
'X-IS-MULTIROOT': 'false',
|
|
45
|
+
'X-CLIENT-TYPE': 'cline-desktop',
|
|
46
|
+
'X-CLIENT-VERSION': CLINE_CLIENT_VERSION,
|
|
47
|
+
'User-Agent': `Cline/${CLINE_CLIENT_VERSION}`,
|
|
48
|
+
});
|
|
21
49
|
/** A window longer than three days is the weekly pool; the short one is the session. */
|
|
22
50
|
const WEEK_SECONDS_THRESHOLD = 3 * 24 * 60 * 60;
|
|
23
51
|
function asRecord(value) {
|
|
@@ -308,16 +336,223 @@ export const opencodeGoQuotaProbe = async ({ token, signal, fetch }) => {
|
|
|
308
336
|
return windows;
|
|
309
337
|
});
|
|
310
338
|
};
|
|
339
|
+
/**
|
|
340
|
+
* Command Code plan windows. The CLI's account route reports a credit balance
|
|
341
|
+
* plus `windowLimits.{fiveHour,weekly}` of `{ used, cap, resetAt }`; a
|
|
342
|
+
* pay-as-you-go account carries only the balance, which has no window at all
|
|
343
|
+
* and therefore no usage the card can render — that case is `error`, never a
|
|
344
|
+
* fabricated 0%.
|
|
345
|
+
*
|
|
346
|
+
* shortcut: the org pool needs `/alpha/whoami` first to learn `orgId`; this
|
|
347
|
+
* probe reads the personal balance only and adds the org query when a managed
|
|
348
|
+
* org account must show its own pool.
|
|
349
|
+
*/
|
|
350
|
+
export const commandcodeQuotaProbe = async ({ token, signal, fetch }) => {
|
|
351
|
+
const read = await readQuotaJson(COMMANDCODE_CREDITS_URL, { Authorization: `Bearer ${token}`, Accept: 'application/json' }, { fetch, signal });
|
|
352
|
+
return snapshotFromRead(read, (rawBody) => {
|
|
353
|
+
// Account routes answer `{ data: … }`; accept an unwrapped body too.
|
|
354
|
+
const body = asRecord(rawBody.data) ?? rawBody;
|
|
355
|
+
const windowLimits = asRecord(body.windowLimits);
|
|
356
|
+
if (windowLimits === undefined)
|
|
357
|
+
return 'credit balance carries no usage window';
|
|
358
|
+
const windows = [];
|
|
359
|
+
const add = (raw, id) => {
|
|
360
|
+
const row = asRecord(raw);
|
|
361
|
+
if (row === undefined)
|
|
362
|
+
return;
|
|
363
|
+
const usedFraction = fractionFromCounts(toNumber(row.used), toNumber(row.cap));
|
|
364
|
+
const resetsAt = resetOf(row, ['resetAt', 'reset_at', 'resetsAt']);
|
|
365
|
+
if (usedFraction === undefined || resetsAt === undefined)
|
|
366
|
+
return;
|
|
367
|
+
windows.push({ id, usedFraction, resetsAt });
|
|
368
|
+
};
|
|
369
|
+
add(windowLimits.fiveHour, 'session');
|
|
370
|
+
add(windowLimits.weekly, 'week');
|
|
371
|
+
return windows;
|
|
372
|
+
});
|
|
373
|
+
};
|
|
374
|
+
/**
|
|
375
|
+
* SuperGrok (xAI OAuth) weekly credits through the Grok CLI billing proxy.
|
|
376
|
+
* The payload is `{ config: { currentPeriod: { start, end, type } ,
|
|
377
|
+
* creditUsagePercent, isUnifiedBillingUser } }`; a fresh weekly period omits
|
|
378
|
+
* the percent, which means 0 % used while that period is still open.
|
|
379
|
+
*
|
|
380
|
+
* Only an explicit WEEK period maps: this UI has `session` and `week` windows,
|
|
381
|
+
* and a monthly included quota is neither. A unified-billing account needs a
|
|
382
|
+
* second (default) billing payload to say which counter gates requests — this
|
|
383
|
+
* probe reads the credits payload alone, so unified is `error` rather than a
|
|
384
|
+
* week that may not apply.
|
|
385
|
+
*/
|
|
386
|
+
export const xaiQuotaProbe = async ({ token, signal, fetch }) => {
|
|
387
|
+
const read = await readQuotaJson(XAI_BILLING_URL, {
|
|
388
|
+
Authorization: `Bearer ${token}`,
|
|
389
|
+
Accept: 'application/json',
|
|
390
|
+
'X-XAI-Token-Auth': XAI_CLI_TOKEN_AUTH,
|
|
391
|
+
'User-Agent': XAI_CLI_USER_AGENT,
|
|
392
|
+
}, { fetch, signal });
|
|
393
|
+
return snapshotFromRead(read, (body) => {
|
|
394
|
+
const config = asRecord(body.config) ?? body;
|
|
395
|
+
const period = asRecord(config.currentPeriod);
|
|
396
|
+
if (period === undefined)
|
|
397
|
+
return 'no weekly credits period';
|
|
398
|
+
const type = typeof period.type === 'string' ? period.type.toUpperCase() : '';
|
|
399
|
+
if (type.length === 0)
|
|
400
|
+
return 'missing billing period type';
|
|
401
|
+
if (!type.includes('WEEK'))
|
|
402
|
+
return `unsupported billing period (${type})`;
|
|
403
|
+
// A unified account keeps a weekly credits pool *and* a monthly included
|
|
404
|
+
// quota; an inferred weekly percent there is not usage this UI can trust
|
|
405
|
+
// (reference: xai-oauth.ts drops it unless the monthly payload proves there
|
|
406
|
+
// is no monthly quota). Never invent 0 % for it.
|
|
407
|
+
if (config.isUnifiedBillingUser === true)
|
|
408
|
+
return 'unified billing hides the weekly window';
|
|
409
|
+
const resetsAt = resetOf(period, ['end']);
|
|
410
|
+
if (resetsAt === undefined)
|
|
411
|
+
return 'no weekly credits reset time';
|
|
412
|
+
const percent = toNumber(config.creditUsagePercent);
|
|
413
|
+
const usedFraction = percent === undefined
|
|
414
|
+
? resetsAt > Date.now() ? 0 : undefined
|
|
415
|
+
: fractionFromUsed(percent);
|
|
416
|
+
if (usedFraction === undefined)
|
|
417
|
+
return 'no usable weekly credits percent';
|
|
418
|
+
return [{ id: 'week', usedFraction, resetsAt }];
|
|
419
|
+
});
|
|
420
|
+
};
|
|
421
|
+
/**
|
|
422
|
+
* ClinePass limits, read with the stored Cline API key exactly as Cline's own
|
|
423
|
+
* client reads them (magpie `internal/provider/cline_usage.go`): the endpoint
|
|
424
|
+
* answers a key as it answers an account. `limits[]` carries `type`
|
|
425
|
+
* (`five_hour` / `weekly` / `monthly`), `percentUsed` (USED percent) and an
|
|
426
|
+
* ISO `resetsAt`.
|
|
427
|
+
*
|
|
428
|
+
* The monthly limit has no window in this UI (only `session` and `week`), so
|
|
429
|
+
* it is dropped; an account whose only limit is monthly reads `error`. A 404
|
|
430
|
+
* is Cline's "no ClinePass on this account" and stays `absent`.
|
|
431
|
+
*
|
|
432
|
+
* shortcut: the credit balance needs `/users/me` and then
|
|
433
|
+
* `/users/{id}/balance` (two more calls) and is not a window; add it only if a
|
|
434
|
+
* card must show the remaining dollars.
|
|
435
|
+
*/
|
|
436
|
+
export const clineQuotaProbe = async ({ token, signal, fetch }) => {
|
|
437
|
+
const read = await readQuotaJson(CLINE_USAGE_URL, { ...CLINE_CLIENT_HEADERS, Authorization: `Bearer ${token}` }, { fetch, signal });
|
|
438
|
+
return snapshotFromRead(read, (body) => {
|
|
439
|
+
// Cline wraps every answer in `{ success, error, data }`.
|
|
440
|
+
if (body.success === false)
|
|
441
|
+
return 'usage request rejected';
|
|
442
|
+
const data = asRecord(body.data) ?? body;
|
|
443
|
+
const limits = Array.isArray(data.limits) ? data.limits : [];
|
|
444
|
+
// A plan can list more than one row per window; consumers `find()` the
|
|
445
|
+
// first match, so collapse to the tighter fraction instead of letting a
|
|
446
|
+
// loose row that arrives first win.
|
|
447
|
+
const windows = new Map();
|
|
448
|
+
for (const raw of limits) {
|
|
449
|
+
const row = asRecord(raw);
|
|
450
|
+
if (row === undefined)
|
|
451
|
+
continue;
|
|
452
|
+
const id = row.type === 'five_hour'
|
|
453
|
+
? 'session'
|
|
454
|
+
: row.type === 'weekly' ? 'week' : undefined;
|
|
455
|
+
if (id === undefined)
|
|
456
|
+
continue;
|
|
457
|
+
const usedFraction = fractionFromUsed(toNumber(row.percentUsed) ?? Number.NaN);
|
|
458
|
+
const resetsAt = resetOf(row, ['resetsAt', 'resets_at']);
|
|
459
|
+
if (usedFraction === undefined || resetsAt === undefined)
|
|
460
|
+
continue;
|
|
461
|
+
windows.set(id, tighterWindow(windows.get(id), { id, usedFraction, resetsAt }));
|
|
462
|
+
}
|
|
463
|
+
return [...windows.values()];
|
|
464
|
+
});
|
|
465
|
+
};
|
|
311
466
|
/**
|
|
312
467
|
* Route id → probe. The ids are the variants this repo and the Host directory
|
|
313
468
|
* actually use (OAuth catalog ids `anthropic`/`openai-codex`/`kimi-coding`, the
|
|
314
469
|
* seed routes in `src/providers.ts`, the sign-in routes in `src/oauth`).
|
|
315
470
|
* Unknown ids have no entry by design and resolve to `absent`.
|
|
316
471
|
*
|
|
472
|
+
* Transport routes sit beside the pool routes: the host resolves their
|
|
473
|
+
* `llm-omp-transports/<route>` grant and probes it read-only.
|
|
474
|
+
* - `factory-droid`: probed with the grant's own bearer plus the
|
|
475
|
+
* `X-Factory-Org-Id` the payload carries; no rotation or write-back.
|
|
476
|
+
*
|
|
477
|
+
* Deliberately absent, each verified against a reference implementation:
|
|
478
|
+
* - `ollama-cloud`: the pinned reference (oh-my-pi `packages/ai/src/usage/ollama.ts`)
|
|
479
|
+
* reports no standalone quota endpoint for Ollama.
|
|
480
|
+
* - `cursor`: absent because its only usable numbers are a **monthly** billing
|
|
481
|
+
* bucket (`billingCycleEnd` / `usage-summary`), and this model has only
|
|
482
|
+
* `session`/`week`; a monthly period is never relabelled as a week. Adding it
|
|
483
|
+
* later needs a monthly window in the model, not a `week` relabel.
|
|
484
|
+
* - `openrouter`: no reference in tokscale, magpie or oh-my-pi reads a usage
|
|
485
|
+
* endpoint, so `GET /api/v1/key`'s shape is unconfirmed and unshipped.
|
|
486
|
+
*
|
|
317
487
|
* shortcut: the CN-region routes (`zai-coding-cn`, `minimax-code-cn`) reuse the
|
|
318
488
|
* international endpoints, so a CN key reports `unauthorized`; add per-region
|
|
319
489
|
* base URLs when a CN-hosted account must show quota.
|
|
320
490
|
*/
|
|
491
|
+
/**
|
|
492
|
+
* Factory Droid's plan-usage read (`GET /api/billing/limits`, reference
|
|
493
|
+
* oh-my-pi `usage/factory-droid.ts`). Auth is the stored WorkOS bearer plus the
|
|
494
|
+
* `X-Factory-Org-Id` the grant itself carries, so the request needs no rotation
|
|
495
|
+
* and no other sign-in state.
|
|
496
|
+
*/
|
|
497
|
+
const FACTORY_DROID_CLIENT_VERSION = '0.237.0';
|
|
498
|
+
// shortcut: client version pinned from oh-my-pi 0.237.0; bump it if the endpoint starts version-gating
|
|
499
|
+
const FACTORY_DROID_LIMITS_PATH = '/api/billing/limits';
|
|
500
|
+
function factoryDroidHost(region) {
|
|
501
|
+
return region === 'eu' ? 'https://api.eu.factory.ai' : 'https://api.factory.ai';
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* Factory freezes a lapsed window at its last-used value instead of rolling it
|
|
505
|
+
* forward, so only an active window (`windowEnd >= now`) carries a meaning.
|
|
506
|
+
*/
|
|
507
|
+
function factoryDroidWindow(input, id, now) {
|
|
508
|
+
const bucket = asRecord(input);
|
|
509
|
+
if (bucket === undefined)
|
|
510
|
+
return undefined;
|
|
511
|
+
const usedPercent = toNumber(bucket.usedPercent);
|
|
512
|
+
const windowEnd = resetOf(bucket, ['windowEnd']);
|
|
513
|
+
const usedFraction = usedPercent === undefined ? undefined : fractionFromUsed(usedPercent);
|
|
514
|
+
if (usedFraction === undefined || windowEnd === undefined || windowEnd < now)
|
|
515
|
+
return undefined;
|
|
516
|
+
return { id, usedFraction, resetsAt: windowEnd };
|
|
517
|
+
}
|
|
518
|
+
export const factoryDroidQuotaProbe = async ({ token, orgId, region, signal, fetch }) => {
|
|
519
|
+
const read = await readQuotaJson(`${factoryDroidHost(region)}${FACTORY_DROID_LIMITS_PATH}`, {
|
|
520
|
+
Authorization: `Bearer ${token}`,
|
|
521
|
+
Accept: 'application/json',
|
|
522
|
+
'X-Client-Version': FACTORY_DROID_CLIENT_VERSION,
|
|
523
|
+
'X-Factory-Client': 'cli',
|
|
524
|
+
...(orgId === undefined || orgId.length === 0 ? {} : { 'X-Factory-Org-Id': orgId }),
|
|
525
|
+
}, { fetch, signal });
|
|
526
|
+
if (!read.ok)
|
|
527
|
+
return { status: read.status, fetchedAt: Date.now(), windows: [], reason: read.reason };
|
|
528
|
+
const body = read.body;
|
|
529
|
+
const fetchedAt = Date.now();
|
|
530
|
+
if (body.usesTokenRateLimitsBilling === false) {
|
|
531
|
+
return {
|
|
532
|
+
status: 'absent',
|
|
533
|
+
fetchedAt,
|
|
534
|
+
windows: [],
|
|
535
|
+
reason: 'this account does not use token-rate-limit billing',
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
const limits = asRecord(body.limits);
|
|
539
|
+
if (limits === undefined)
|
|
540
|
+
return { status: 'error', fetchedAt, windows: [], reason: 'no limits payload' };
|
|
541
|
+
// Two pools can be reported (Standard credits / Droid Core); one readout can
|
|
542
|
+
// only show one, so Standard wins and Core is the fallback.
|
|
543
|
+
const pool = asRecord(limits.standard) ?? asRecord(limits.core);
|
|
544
|
+
if (pool === undefined)
|
|
545
|
+
return { status: 'error', fetchedAt, windows: [], reason: 'no usable credit pool' };
|
|
546
|
+
const windows = [
|
|
547
|
+
factoryDroidWindow(pool.fiveHour, 'session', fetchedAt),
|
|
548
|
+
factoryDroidWindow(pool.weekly, 'week', fetchedAt),
|
|
549
|
+
].filter((window) => window !== undefined);
|
|
550
|
+
if (windows.length === 0) {
|
|
551
|
+
// A monthly-only payload is not a week: never relabel it.
|
|
552
|
+
return { status: 'error', fetchedAt, windows: [], reason: 'no active five-hour or weekly window' };
|
|
553
|
+
}
|
|
554
|
+
return { status: 'ok', fetchedAt, windows };
|
|
555
|
+
};
|
|
321
556
|
const QUOTA_PROBES = Object.freeze({
|
|
322
557
|
claude: claudeQuotaProbe,
|
|
323
558
|
anthropic: claudeQuotaProbe,
|
|
@@ -333,6 +568,10 @@ const QUOTA_PROBES = Object.freeze({
|
|
|
333
568
|
'minimax-code': minimaxQuotaProbe,
|
|
334
569
|
'minimax-code-cn': minimaxQuotaProbe,
|
|
335
570
|
'opencode-go': opencodeGoQuotaProbe,
|
|
571
|
+
commandcode: commandcodeQuotaProbe,
|
|
572
|
+
xai: xaiQuotaProbe,
|
|
573
|
+
'cline-pass': clineQuotaProbe,
|
|
574
|
+
'factory-droid': factoryDroidQuotaProbe,
|
|
336
575
|
});
|
|
337
576
|
/** The probe for a route id, or `undefined` when the provider exposes no usage API. */
|
|
338
577
|
export function quotaProbe(provider) {
|