dsh-plugin-subscriptions 0.5.2 → 0.6.0

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.
Files changed (60) hide show
  1. package/README.md +79 -5
  2. package/README.zh.md +78 -4
  3. package/lib/auth/rpc.d.ts +64 -13
  4. package/lib/auth/rpc.js +75 -10
  5. package/lib/auth/store.d.ts +75 -17
  6. package/lib/auth/store.js +148 -27
  7. package/lib/client/ImageGenerateToolview.d.ts +1 -1
  8. package/lib/client/SpeedSelect.d.ts +25 -2
  9. package/lib/client/SpeedSelect.js +10 -6
  10. package/lib/client/SubscriptionsSection.d.ts +83 -3
  11. package/lib/client/SubscriptionsSection.js +411 -62
  12. package/lib/client/VideoGenerateToolview.d.ts +1 -1
  13. package/lib/client/index.d.ts +1 -9
  14. package/lib/client/index.js +7 -4
  15. package/lib/client/locales.d.ts +46 -10
  16. package/lib/client/locales.js +46 -10
  17. package/lib/client.js +703 -132
  18. package/lib/client.js.map +1 -1
  19. package/lib/compat.d.ts +36 -0
  20. package/lib/compat.js +20 -0
  21. package/lib/index.d.ts +26 -1
  22. package/lib/index.js +2377 -309
  23. package/lib/model-defaults.d.ts +23 -0
  24. package/lib/model-defaults.js +237 -0
  25. package/lib/providers/accounts.d.ts +102 -0
  26. package/lib/providers/accounts.js +123 -0
  27. package/lib/providers/claude.d.ts +46 -7
  28. package/lib/providers/claude.js +125 -34
  29. package/lib/providers/codex.d.ts +45 -3
  30. package/lib/providers/codex.js +152 -26
  31. package/lib/providers/common.d.ts +87 -6
  32. package/lib/providers/common.js +185 -22
  33. package/lib/providers/copilot.d.ts +32 -3
  34. package/lib/providers/copilot.js +111 -19
  35. package/lib/providers/grok.d.ts +45 -4
  36. package/lib/providers/grok.js +136 -20
  37. package/lib/providers/pool-family.d.ts +56 -0
  38. package/lib/providers/pool-family.js +45 -0
  39. package/lib/providers/pool-health.d.ts +74 -0
  40. package/lib/providers/pool-health.js +148 -0
  41. package/lib/providers/pool-usage.d.ts +78 -0
  42. package/lib/providers/pool-usage.js +185 -0
  43. package/lib/providers/pool.d.ts +107 -0
  44. package/lib/providers/pool.js +371 -0
  45. package/lib/providers/rate-limit.d.ts +192 -0
  46. package/lib/providers/rate-limit.js +338 -0
  47. package/lib/tools/image-generate.d.ts +3 -3
  48. package/lib/tools/image-generate.js +2 -1
  49. package/lib/tools/video-generate.d.ts +2 -2
  50. package/lib/tools/video-generate.js +2 -1
  51. package/lib/tools/x-search.d.ts +2 -2
  52. package/lib/tools/x-search.js +2 -1
  53. package/lib/translate/anthropic.js +5 -4
  54. package/lib/translate/chat-completions.js +5 -4
  55. package/lib/translate/responses.js +5 -4
  56. package/package.json +21 -21
  57. package/lib/providers/antigravity.d.ts +0 -90
  58. package/lib/providers/antigravity.js +0 -392
  59. package/lib/translate/antigravity.d.ts +0 -110
  60. package/lib/translate/antigravity.js +0 -303
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Same-subscription account routing: the picker is the union of every
3
+ * account's catalog (deduped by wire id). A model listed by two or more
4
+ * accounts failovers between them; a model listed by only one account is
5
+ * sent to that account. No extra pool identity, no cross-provider
6
+ * aggregation.
7
+ */
8
+ import type { LlmModelInfo } from '@deepseek-ai/dsh-llm';
9
+ import type { ProviderId } from '../auth/store.js';
10
+ /** One pool member: an exact provider/account/model route. */
11
+ export interface PoolMemberRef {
12
+ provider: ProviderId;
13
+ /** Account key; omitted in configured members to mean "the default account". */
14
+ account?: string;
15
+ model: string;
16
+ }
17
+ /** A member with its account resolved (no config indirection left). */
18
+ export type ConcretePoolMember = PoolMemberRef & {
19
+ account: string;
20
+ };
21
+ /** One pool: its members plus display metadata for the picker. */
22
+ export interface PoolDefinition {
23
+ members: PoolMemberRef[];
24
+ /** Display name of the catalog entry (account pools) or the pool id (extras). */
25
+ name?: string;
26
+ /** Description of the catalog entry, when the pool borrowed one. */
27
+ description?: string;
28
+ /**
29
+ * When true, the pool is an extra picker entry (a configured tier). Account
30
+ * pools leave this unset so they reuse the provider's existing catalog row.
31
+ */
32
+ extra?: boolean;
33
+ }
34
+ /** One account's catalog as seen through that account's credentials. */
35
+ export interface AccountCatalog {
36
+ account: string;
37
+ models: readonly LlmModelInfo[];
38
+ }
39
+ /** One provider's contribution to account-pool aggregation. */
40
+ export interface ProviderPoolSource {
41
+ /** Per-account catalogs, default first. A model pools only the accounts that list it. */
42
+ catalogs: readonly AccountCatalog[];
43
+ }
44
+ /** Map key for one provider's pool of one model (ids collide across providers). */
45
+ export declare function poolKey(provider: string, model: string): string;
46
+ /**
47
+ * Build per-provider account routes. Each model id becomes a definition of
48
+ * the accounts that list it: two or more fail over; one is pinned to that
49
+ * account (so a Max-only model is never sent to a Plus login). The picker
50
+ * unions these catalogs; a logout that drops a model to one account keeps
51
+ * the same id and pins it to whoever remains.
52
+ * @param sources - per-account catalogs (providers with no accounts list
53
+ * nothing and simply never join a pool).
54
+ * @returns `provider/model` → pool definition (not listed as an extra entry).
55
+ */
56
+ export declare function buildAccountPools(sources: Partial<Record<ProviderId, ProviderPoolSource>>): Map<string, PoolDefinition>;
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Same-subscription account routing: the picker is the union of every
3
+ * account's catalog (deduped by wire id). A model listed by two or more
4
+ * accounts failovers between them; a model listed by only one account is
5
+ * sent to that account. No extra pool identity, no cross-provider
6
+ * aggregation.
7
+ */
8
+ /** Map key for one provider's pool of one model (ids collide across providers). */
9
+ export function poolKey(provider, model) {
10
+ return `${provider}/${model}`;
11
+ }
12
+ /**
13
+ * Build per-provider account routes. Each model id becomes a definition of
14
+ * the accounts that list it: two or more fail over; one is pinned to that
15
+ * account (so a Max-only model is never sent to a Plus login). The picker
16
+ * unions these catalogs; a logout that drops a model to one account keeps
17
+ * the same id and pins it to whoever remains.
18
+ * @param sources - per-account catalogs (providers with no accounts list
19
+ * nothing and simply never join a pool).
20
+ * @returns `provider/model` → pool definition (not listed as an extra entry).
21
+ */
22
+ export function buildAccountPools(sources) {
23
+ const pools = new Map();
24
+ for (const [provider, source] of Object.entries(sources)) {
25
+ const byModel = new Map();
26
+ for (const catalog of source.catalogs) {
27
+ for (const model of catalog.models) {
28
+ let entry = byModel.get(model.id);
29
+ if (entry === undefined) {
30
+ entry = { members: [], info: model };
31
+ byModel.set(model.id, entry);
32
+ }
33
+ entry.members.push({ provider, account: catalog.account, model: model.id });
34
+ }
35
+ }
36
+ for (const [id, { members, info }] of byModel) {
37
+ pools.set(poolKey(provider, id), {
38
+ members,
39
+ ...info.name === undefined || info.name === id ? {} : { name: info.name },
40
+ ...info.description === undefined ? {} : { description: info.description },
41
+ });
42
+ }
43
+ }
44
+ return pools;
45
+ }
@@ -0,0 +1,74 @@
1
+ /**
2
+ * Health bookkeeping for pool members: which `(provider, account, model)`
3
+ * member is cooling down after a failure, and for how long. Purely in-memory
4
+ * — a restart re-probes members naturally, so nothing here is persisted.
5
+ *
6
+ * The failure classifier maps the adapters' stable `LlmError` codes (see
7
+ * `httpLlmError`/`mapFetchFailure` in `common.ts`) to one of three actions:
8
+ * switch to another member with a cooldown, switch without recording a
9
+ * cooldown (transport blips say nothing about the account), or rethrow
10
+ * (the request itself is at fault and another account would fail alike).
11
+ */
12
+ import type { ProviderId } from '../auth/store.js';
13
+ /** Registry key for one pool member. */
14
+ export declare function memberKey(provider: ProviderId, account: string, model: string): string;
15
+ /** Registry key parking EVERY member of one account (account-level failures). */
16
+ export declare function accountKey(provider: ProviderId, account: string): string;
17
+ /** Default cooldown when a quota/rate failure carries no `retry-after`. */
18
+ export declare const DEFAULT_QUOTA_COOLDOWN_MS: number;
19
+ /** Auth failures recheck after a day; a re-login clears the record immediately. */
20
+ export declare const AUTH_COOLDOWN_MS: number;
21
+ /** Transient server-side failures cool down briefly. */
22
+ export declare const TRANSIENT_COOLDOWN_MS = 60000;
23
+ /** Whether a failure parks one member or the account's whole quota. */
24
+ export type PoolFailureScope = 'member' | 'account';
25
+ /** What the pool should do with a member that just failed. */
26
+ export type PoolFailureAction = {
27
+ action: 'switch';
28
+ cooldownMs: number;
29
+ reason: string;
30
+ scope: PoolFailureScope;
31
+ } | {
32
+ action: 'switch';
33
+ } | {
34
+ action: 'throw';
35
+ };
36
+ /**
37
+ * Classify a member failure. Quota and rate-limit failures cool down (using
38
+ * the provider's own `retry-after` when sent, which is more accurate than
39
+ * any fixed guess) — account-wide for account-metered providers, per-member
40
+ * for model-scoped ones; auth failures park the account until re-login
41
+ * (credentials are account-level); server/timeout failures get a short
42
+ * per-member cooldown; transport failures switch without a record;
43
+ * everything else — most importantly CONTEXT_WINDOW_EXCEEDED and ABORTED —
44
+ * is the request's own fault and is rethrown untouched.
45
+ * @param error - the failure thrown by a member adapter's stream.
46
+ * @param provider - the failing member's provider (decides the quota scope).
47
+ * @returns the action the pool should take.
48
+ */
49
+ export declare function classifyPoolFailure(error: unknown, provider: ProviderId): PoolFailureAction;
50
+ /**
51
+ * Cooldown registry keyed by {@link memberKey}. A member whose cooldown has
52
+ * expired is simply available again — recovery is proven by the next real
53
+ * request, not by a background probe.
54
+ */
55
+ export declare class PoolHealthRegistry {
56
+ private readonly records;
57
+ /** Whether a member may serve: neither it nor its whole account is cooling. */
58
+ isMemberAvailable(provider: ProviderId, account: string, model: string, now?: number): boolean;
59
+ /** Whether one registry key is clear right now. */
60
+ isAvailable(key: string, now?: number): boolean;
61
+ /** Park a member for `cooldownMs`; a longer existing cooldown wins. */
62
+ markUnavailable(key: string, cooldownMs: number, reason: string, now?: number): void;
63
+ /**
64
+ * Epoch ms at which the earliest cooling record among `keys` recovers;
65
+ * `undefined` when none of them is cooling. The registry is shared by
66
+ * every pool, so the caller passes the keys of ITS members (member and
67
+ * account keys alike) — an unrelated pool's cooldown must not shape this
68
+ * pool's retry hint. Feeds the pool-exhausted error's
69
+ * `providerRetryAfterMs`.
70
+ */
71
+ earliestRecovery(keys: ReadonlySet<string>, now?: number): number | undefined;
72
+ /** Drop records of one provider, or of a single account when given (auth changes). */
73
+ clear(provider: ProviderId, account?: string): void;
74
+ }
@@ -0,0 +1,148 @@
1
+ /**
2
+ * Health bookkeeping for pool members: which `(provider, account, model)`
3
+ * member is cooling down after a failure, and for how long. Purely in-memory
4
+ * — a restart re-probes members naturally, so nothing here is persisted.
5
+ *
6
+ * The failure classifier maps the adapters' stable `LlmError` codes (see
7
+ * `httpLlmError`/`mapFetchFailure` in `common.ts`) to one of three actions:
8
+ * switch to another member with a cooldown, switch without recording a
9
+ * cooldown (transport blips say nothing about the account), or rethrow
10
+ * (the request itself is at fault and another account would fail alike).
11
+ */
12
+ import { CONTEXT_WINDOW_EXCEEDED_CODE, LlmError, QUOTA_EXCEEDED_CODE } from '@deepseek-ai/dsh-llm';
13
+ /** Registry key for one pool member. */
14
+ export function memberKey(provider, account, model) {
15
+ return `${provider}/${account}/${model}`;
16
+ }
17
+ /** Registry key parking EVERY member of one account (account-level failures). */
18
+ export function accountKey(provider, account) {
19
+ return `${provider}/${account}/*`;
20
+ }
21
+ /** Default cooldown when a quota/rate failure carries no `retry-after`. */
22
+ export const DEFAULT_QUOTA_COOLDOWN_MS = 5 * 60_000;
23
+ /** Auth failures recheck after a day; a re-login clears the record immediately. */
24
+ export const AUTH_COOLDOWN_MS = 24 * 60 * 60_000;
25
+ /** Transient server-side failures cool down briefly. */
26
+ export const TRANSIENT_COOLDOWN_MS = 60_000;
27
+ /**
28
+ * Providers whose quota windows are model-scoped, so a quota failure on one
29
+ * model says nothing about its siblings (Claude's Opus/Sonnet lanes). Every
30
+ * other provider meters the account as a whole: one member hitting the wall
31
+ * means its siblings on the SAME account would too, so the cooldown parks
32
+ * the account (other accounts of the provider are unaffected).
33
+ */
34
+ const MODEL_SCOPED_QUOTA_PROVIDERS = new Set(['claude']);
35
+ /** The `retry-after` an adapter propagated through `httpLlmError`, when any. */
36
+ function retryAfterMs(error) {
37
+ return error.failure.providerRetryAfterMs;
38
+ }
39
+ /**
40
+ * Classify a member failure. Quota and rate-limit failures cool down (using
41
+ * the provider's own `retry-after` when sent, which is more accurate than
42
+ * any fixed guess) — account-wide for account-metered providers, per-member
43
+ * for model-scoped ones; auth failures park the account until re-login
44
+ * (credentials are account-level); server/timeout failures get a short
45
+ * per-member cooldown; transport failures switch without a record;
46
+ * everything else — most importantly CONTEXT_WINDOW_EXCEEDED and ABORTED —
47
+ * is the request's own fault and is rethrown untouched.
48
+ * @param error - the failure thrown by a member adapter's stream.
49
+ * @param provider - the failing member's provider (decides the quota scope).
50
+ * @returns the action the pool should take.
51
+ */
52
+ export function classifyPoolFailure(error, provider) {
53
+ if (!(error instanceof LlmError))
54
+ return { action: 'throw' };
55
+ switch (error.code) {
56
+ case QUOTA_EXCEEDED_CODE:
57
+ case 'RATE_LIMIT':
58
+ return {
59
+ action: 'switch',
60
+ cooldownMs: retryAfterMs(error) ?? DEFAULT_QUOTA_COOLDOWN_MS,
61
+ reason: error.code,
62
+ scope: MODEL_SCOPED_QUOTA_PROVIDERS.has(provider) ? 'member' : 'account',
63
+ };
64
+ case 'AUTH':
65
+ case 'INVALID_CREDENTIAL':
66
+ case 'MISSING_CREDENTIAL':
67
+ return { action: 'switch', cooldownMs: AUTH_COOLDOWN_MS, reason: error.code, scope: 'account' };
68
+ case 'SERVER':
69
+ case 'TIMEOUT':
70
+ case 'EMPTY_RESPONSE':
71
+ return { action: 'switch', cooldownMs: TRANSIENT_COOLDOWN_MS, reason: error.code, scope: 'member' };
72
+ case 'TRANSPORT':
73
+ return { action: 'switch' };
74
+ case 'HTTP_402':
75
+ case 'HTTP_404':
76
+ // Plan/model availability is account-shaped: another account of the
77
+ // same subscription may still serve. A 400 that is not a context-window
78
+ // error stays HTTP_400 and throws (the request itself is at fault).
79
+ return { action: 'switch', cooldownMs: TRANSIENT_COOLDOWN_MS, reason: error.code, scope: 'member' };
80
+ case CONTEXT_WINDOW_EXCEEDED_CODE:
81
+ case 'ABORTED':
82
+ default:
83
+ return { action: 'throw' };
84
+ }
85
+ }
86
+ /**
87
+ * Cooldown registry keyed by {@link memberKey}. A member whose cooldown has
88
+ * expired is simply available again — recovery is proven by the next real
89
+ * request, not by a background probe.
90
+ */
91
+ export class PoolHealthRegistry {
92
+ records = new Map();
93
+ /** Whether a member may serve: neither it nor its whole account is cooling. */
94
+ isMemberAvailable(provider, account, model, now = Date.now()) {
95
+ return this.isAvailable(accountKey(provider, account), now)
96
+ && this.isAvailable(memberKey(provider, account, model), now);
97
+ }
98
+ /** Whether one registry key is clear right now. */
99
+ isAvailable(key, now = Date.now()) {
100
+ const record = this.records.get(key);
101
+ if (record === undefined)
102
+ return true;
103
+ if (record.unavailableUntil <= now) {
104
+ this.records.delete(key);
105
+ return true;
106
+ }
107
+ return false;
108
+ }
109
+ /** Park a member for `cooldownMs`; a longer existing cooldown wins. */
110
+ markUnavailable(key, cooldownMs, reason, now = Date.now()) {
111
+ const until = now + cooldownMs;
112
+ const existing = this.records.get(key);
113
+ if (existing !== undefined && existing.unavailableUntil > until)
114
+ return;
115
+ this.records.set(key, { unavailableUntil: until, reason });
116
+ }
117
+ /**
118
+ * Epoch ms at which the earliest cooling record among `keys` recovers;
119
+ * `undefined` when none of them is cooling. The registry is shared by
120
+ * every pool, so the caller passes the keys of ITS members (member and
121
+ * account keys alike) — an unrelated pool's cooldown must not shape this
122
+ * pool's retry hint. Feeds the pool-exhausted error's
123
+ * `providerRetryAfterMs`.
124
+ */
125
+ earliestRecovery(keys, now = Date.now()) {
126
+ let earliest;
127
+ for (const [key, record] of this.records) {
128
+ if (record.unavailableUntil <= now) {
129
+ this.records.delete(key);
130
+ continue;
131
+ }
132
+ if (!keys.has(key))
133
+ continue;
134
+ if (earliest === undefined || record.unavailableUntil < earliest) {
135
+ earliest = record.unavailableUntil;
136
+ }
137
+ }
138
+ return earliest;
139
+ }
140
+ /** Drop records of one provider, or of a single account when given (auth changes). */
141
+ clear(provider, account) {
142
+ const prefix = account === undefined ? `${provider}/` : `${provider}/${account}/`;
143
+ for (const key of [...this.records.keys()]) {
144
+ if (key.startsWith(prefix))
145
+ this.records.delete(key);
146
+ }
147
+ }
148
+ }
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Quota tracking for pool members: polls the providers' usage endpoints
3
+ * (the same normalized `ProviderUsage` shape the Settings page consumes) and
4
+ * turns the windows into a scheduling score.
5
+ *
6
+ * The score is a REQUIRED BURN RATE: the fraction of the window that must be
7
+ * consumed per millisecond for the quota to be exactly used up at reset time
8
+ * (`remaining / timeUntilReset`). Subscription quota does not roll over, so a
9
+ * window about to reset with plenty left is the most urgent to spend — the
10
+ * `quota_aware` strategy therefore prefers the highest-urgency member, which
11
+ * over time converges on every window hitting zero right at its reset.
12
+ */
13
+ import type { ProviderUsage } from './common.js';
14
+ import type { ProviderId } from '../auth/store.js';
15
+ import type { ConcretePoolMember } from './pool-family.js';
16
+ /** A member is taken out of rotation once any window crosses this fill level. */
17
+ export declare const QUOTA_FULL_PERCENT = 95;
18
+ /** How long a usage snapshot is trusted before a background refresh. */
19
+ export declare const USAGE_TTL_MS: number;
20
+ /** The scheduling view of one member's quota. */
21
+ export interface MemberQuota {
22
+ /** False when a window is effectively full or the login is gone. */
23
+ available: boolean;
24
+ /** Required burn rate (fraction of window per ms); 0 when unknown. */
25
+ urgency: number;
26
+ /** Epoch ms of the snapshot this was computed from; 0 when none. */
27
+ fetchedAt: number;
28
+ }
29
+ /**
30
+ * Per-ACCOUNT usage snapshots with in-flight dedupe and
31
+ * stale-while-revalidate refresh. Providers without a usage endpoint
32
+ * (copilot) resolve no fetcher and score a constant zero urgency — which
33
+ * naturally ranks them behind every measured member. Fetchers are resolved
34
+ * lazily per (provider, account) so accounts added after startup join
35
+ * tracking on their first score.
36
+ */
37
+ export declare class PoolUsageTracker {
38
+ private readonly fetcherFor;
39
+ private readonly ttlMs;
40
+ private readonly entries;
41
+ private readonly inflight;
42
+ constructor(fetcherFor: (provider: ProviderId, account: string) => (() => Promise<ProviderUsage>) | undefined, ttlMs?: number);
43
+ /**
44
+ * The quota view of one member. A cold cache awaits the first fetch; a
45
+ * stale one answers immediately while the refresh serves the NEXT call
46
+ * (member selection must never block on the network mid-conversation). A
47
+ * failure still cooling down degrades immediately with no network call.
48
+ * @param member - the pool member to score (account resolved).
49
+ * @returns availability plus the urgency score.
50
+ */
51
+ quotaFor(member: ConcretePoolMember): Promise<MemberQuota>;
52
+ /**
53
+ * Same cache as {@link quotaFor}, for direct display (the Settings page):
54
+ * the raw snapshot, or the original fetch error, instead of a routing
55
+ * score.
56
+ * @param provider - the account's provider.
57
+ * @param account - the account key.
58
+ * @param force - bypass a fresh cached SNAPSHOT for an honest re-check (the
59
+ * manual Refresh button). A live failure cooldown is never bypassed —
60
+ * retrying through it is exactly what turns a 429 into a permanent
61
+ * lockout, so even a forced call still answers from the negative cache.
62
+ * @returns `{ supported: false }` when the provider has no usage fetcher.
63
+ */
64
+ snapshotFor(provider: ProviderId, account: string, force?: boolean): Promise<ProviderUsage>;
65
+ /** Drop cached snapshots: one account, or a whole provider when `account` is omitted. */
66
+ invalidate(provider: ProviderId, account?: string): void;
67
+ /**
68
+ * Run (or join) the single in-flight fetch for one account key, caching
69
+ * either outcome. A missing/invalid credential is deliberately NOT
70
+ * negative-cached: it costs no network round trip (the session lookup
71
+ * fails before the request goes out) and re-checking live means the
72
+ * member rejoins routing the instant its login is fixed, rather than
73
+ * waiting out a stale cooldown.
74
+ */
75
+ private refresh;
76
+ /** Score one member against a snapshot's windows. */
77
+ private score;
78
+ }
@@ -0,0 +1,185 @@
1
+ /**
2
+ * Quota tracking for pool members: polls the providers' usage endpoints
3
+ * (the same normalized `ProviderUsage` shape the Settings page consumes) and
4
+ * turns the windows into a scheduling score.
5
+ *
6
+ * The score is a REQUIRED BURN RATE: the fraction of the window that must be
7
+ * consumed per millisecond for the quota to be exactly used up at reset time
8
+ * (`remaining / timeUntilReset`). Subscription quota does not roll over, so a
9
+ * window about to reset with plenty left is the most urgent to spend — the
10
+ * `quota_aware` strategy therefore prefers the highest-urgency member, which
11
+ * over time converges on every window hitting zero right at its reset.
12
+ */
13
+ import { isMissingOrInvalidCredential, OAuthEndpointError } from './common.js';
14
+ /** A member is taken out of rotation once any window crosses this fill level. */
15
+ export const QUOTA_FULL_PERCENT = 95;
16
+ /** How long a usage snapshot is trusted before a background refresh. */
17
+ export const USAGE_TTL_MS = 5 * 60_000;
18
+ /** Assumed window length when the provider discloses no `resetsAt`. */
19
+ const FALLBACK_HORIZON_MS = {
20
+ session: 5 * 60 * 60_000,
21
+ weekly: 7 * 24 * 60 * 60_000,
22
+ other: 30 * 24 * 60 * 60_000,
23
+ };
24
+ /**
25
+ * Per-ACCOUNT usage snapshots with in-flight dedupe and
26
+ * stale-while-revalidate refresh. Providers without a usage endpoint
27
+ * (copilot) resolve no fetcher and score a constant zero urgency — which
28
+ * naturally ranks them behind every measured member. Fetchers are resolved
29
+ * lazily per (provider, account) so accounts added after startup join
30
+ * tracking on their first score.
31
+ */
32
+ export class PoolUsageTracker {
33
+ fetcherFor;
34
+ ttlMs;
35
+ entries = new Map();
36
+ inflight = new Map();
37
+ constructor(fetcherFor, ttlMs = USAGE_TTL_MS) {
38
+ this.fetcherFor = fetcherFor;
39
+ this.ttlMs = ttlMs;
40
+ }
41
+ /**
42
+ * The quota view of one member. A cold cache awaits the first fetch; a
43
+ * stale one answers immediately while the refresh serves the NEXT call
44
+ * (member selection must never block on the network mid-conversation). A
45
+ * failure still cooling down degrades immediately with no network call.
46
+ * @param member - the pool member to score (account resolved).
47
+ * @returns availability plus the urgency score.
48
+ */
49
+ async quotaFor(member) {
50
+ const key = `${member.provider}/${member.account}`;
51
+ const fetcher = this.fetcherFor(member.provider, member.account);
52
+ if (fetcher === undefined)
53
+ return { available: true, urgency: 0, fetchedAt: 0 };
54
+ const entry = this.entries.get(key);
55
+ if (entry !== undefined) {
56
+ const fresh = Date.now() - entry.at < (entry.cooldownMs ?? this.ttlMs);
57
+ if (entry.snapshot !== undefined) {
58
+ if (!fresh)
59
+ void this.refresh(key, fetcher).catch(() => undefined);
60
+ return this.score(member, entry);
61
+ }
62
+ if (fresh)
63
+ return degradedQuota(entry.error);
64
+ // The cooldown expired: fall through to a fresh, blocking attempt.
65
+ }
66
+ try {
67
+ const snapshot = await this.refresh(key, fetcher);
68
+ return this.score(member, { snapshot, at: Date.now() });
69
+ }
70
+ catch (error) {
71
+ return degradedQuota(error);
72
+ }
73
+ }
74
+ /**
75
+ * Same cache as {@link quotaFor}, for direct display (the Settings page):
76
+ * the raw snapshot, or the original fetch error, instead of a routing
77
+ * score.
78
+ * @param provider - the account's provider.
79
+ * @param account - the account key.
80
+ * @param force - bypass a fresh cached SNAPSHOT for an honest re-check (the
81
+ * manual Refresh button). A live failure cooldown is never bypassed —
82
+ * retrying through it is exactly what turns a 429 into a permanent
83
+ * lockout, so even a forced call still answers from the negative cache.
84
+ * @returns `{ supported: false }` when the provider has no usage fetcher.
85
+ */
86
+ async snapshotFor(provider, account, force = false) {
87
+ const fetcher = this.fetcherFor(provider, account);
88
+ if (fetcher === undefined)
89
+ return { supported: false };
90
+ const key = `${provider}/${account}`;
91
+ const entry = this.entries.get(key);
92
+ if (entry !== undefined && Date.now() - entry.at < (entry.cooldownMs ?? this.ttlMs)) {
93
+ if (entry.snapshot !== undefined) {
94
+ if (!force)
95
+ return entry.snapshot;
96
+ }
97
+ else {
98
+ throw entry.error;
99
+ }
100
+ }
101
+ return this.refresh(key, fetcher);
102
+ }
103
+ /** Drop cached snapshots: one account, or a whole provider when `account` is omitted. */
104
+ invalidate(provider, account) {
105
+ if (account !== undefined) {
106
+ this.entries.delete(`${provider}/${account}`);
107
+ return;
108
+ }
109
+ for (const key of [...this.entries.keys()]) {
110
+ if (key.startsWith(`${provider}/`))
111
+ this.entries.delete(key);
112
+ }
113
+ }
114
+ /**
115
+ * Run (or join) the single in-flight fetch for one account key, caching
116
+ * either outcome. A missing/invalid credential is deliberately NOT
117
+ * negative-cached: it costs no network round trip (the session lookup
118
+ * fails before the request goes out) and re-checking live means the
119
+ * member rejoins routing the instant its login is fixed, rather than
120
+ * waiting out a stale cooldown.
121
+ */
122
+ refresh(key, fetcher) {
123
+ let pending = this.inflight.get(key);
124
+ if (pending === undefined) {
125
+ pending = fetcher().then((snapshot) => {
126
+ this.entries.set(key, { snapshot, at: Date.now() });
127
+ return snapshot;
128
+ }, (error) => {
129
+ if (!isMissingOrInvalidCredential(error)) {
130
+ this.entries.set(key, { error, at: Date.now(), cooldownMs: cooldownFor(error, this.ttlMs) });
131
+ }
132
+ throw error;
133
+ }).finally(() => {
134
+ this.inflight.delete(key);
135
+ });
136
+ this.inflight.set(key, pending);
137
+ }
138
+ return pending;
139
+ }
140
+ /** Score one member against a snapshot's windows. */
141
+ score(member, entry) {
142
+ const windows = (entry.snapshot.windows ?? []).filter(window => windowApplies(window, member.model));
143
+ let available = true;
144
+ let urgency = 0;
145
+ for (const window of windows) {
146
+ if (window.usedPercent >= QUOTA_FULL_PERCENT)
147
+ available = false;
148
+ urgency = Math.max(urgency, windowUrgency(window));
149
+ }
150
+ return { available, urgency, fetchedAt: entry.at };
151
+ }
152
+ }
153
+ /**
154
+ * The routing view of a fetch failure. Logged out: the member cannot serve
155
+ * at all. Any other failure (network, endpoint rate limit) must not block
156
+ * routing — the member stays available with a zero score, degrading the
157
+ * strategy to plain priority order for it.
158
+ */
159
+ function degradedQuota(error) {
160
+ return isMissingOrInvalidCredential(error)
161
+ ? { available: false, urgency: 0, fetchedAt: 0 }
162
+ : { available: true, urgency: 0, fetchedAt: 0 };
163
+ }
164
+ /** How long to hold a failure in the negative cache: the endpoint's own `retry-after`, or the default TTL. */
165
+ function cooldownFor(error, defaultTtlMs) {
166
+ return error instanceof OAuthEndpointError && error.retryAfterMs !== undefined ? error.retryAfterMs : defaultTtlMs;
167
+ }
168
+ /**
169
+ * Whether a window constrains this model: unscoped windows always do; a
170
+ * model-scoped window (Claude's Opus/Sonnet lanes) applies when its scope
171
+ * names the model family.
172
+ */
173
+ function windowApplies(window, model) {
174
+ if (window.scope === undefined)
175
+ return true;
176
+ return model.toLowerCase().includes(window.scope.toLowerCase());
177
+ }
178
+ /** The required burn rate of one window (fraction per ms). */
179
+ function windowUrgency(window, now = Date.now()) {
180
+ const remaining = Math.max(0, 1 - window.usedPercent / 100);
181
+ const horizon = window.resetsAt !== undefined
182
+ ? Math.max(window.resetsAt - now, 1)
183
+ : FALLBACK_HORIZON_MS[window.kind];
184
+ return remaining / horizon;
185
+ }