tuxevil-rotator 3.0.0 → 3.2.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.
- package/CHANGELOG.md +37 -0
- package/README.md +20 -6
- package/package.json +1 -1
- package/src/account-store.ts +37 -5
- package/src/cli.ts +24 -3
- package/src/compat/model-specs.ts +1 -0
- package/src/compat.ts +540 -24
- package/src/config-normalize.ts +73 -30
- package/src/dashboard.ts +43 -42
- package/src/index.ts +1 -0
- package/src/onboarding.ts +349 -21
- package/src/providers/adapter.ts +18 -0
- package/src/providers/credential-helpers.ts +64 -0
- package/src/providers/google-antigravity/index.ts +9 -0
- package/src/providers/google-antigravity/quota.ts +2 -1
- package/src/providers/google-antigravity/translators.ts +83 -24
- package/src/providers/ollama/index.ts +8 -0
- package/src/providers/ollama/quota.ts +2 -1
- package/src/providers/openai-codex/catalog.ts +117 -0
- package/src/providers/openai-codex/compat.ts +556 -0
- package/src/providers/openai-codex/credentials.ts +82 -0
- package/src/providers/openai-codex/forward.ts +240 -0
- package/src/providers/openai-codex/index.ts +153 -0
- package/src/providers/openai-codex/login.ts +172 -0
- package/src/providers/openai-codex/oauth.ts +359 -0
- package/src/providers/openai-codex/quota.ts +231 -0
- package/src/providers/opencode-zen/catalog.ts +37 -0
- package/src/providers/opencode-zen/credentials.ts +31 -0
- package/src/providers/opencode-zen/forward.ts +214 -0
- package/src/providers/opencode-zen/index.ts +73 -0
- package/src/providers/opencode-zen/login.ts +81 -0
- package/src/providers/opencode-zen/quota.ts +67 -0
- package/src/providers/registry.ts +24 -5
- package/src/proxy.ts +75 -27
- package/src/rate-limit-parser.ts +2 -1
- package/src/rotator.ts +259 -48
- package/src/spend-logger.ts +2 -0
- package/src/static/dashboard-keys.js +152 -2
- package/src/static/dashboard.js +161 -37
- package/src/types.ts +83 -1
- package/src/validators.ts +11 -2
- package/tools/telemetry-receiver/receiver.js +14 -0
package/src/rotator.ts
CHANGED
|
@@ -23,7 +23,6 @@ import {
|
|
|
23
23
|
REQUEST_GOOG_API_CLIENT,
|
|
24
24
|
REQUEST_CLIENT_METADATA,
|
|
25
25
|
ANTIGRAVITY_ENDPOINTS,
|
|
26
|
-
KICKSTART_MODEL_FOR_QUOTA_POOL,
|
|
27
26
|
OLLAMA_TAGS_URL,
|
|
28
27
|
OLLAMA_USER_AGENT,
|
|
29
28
|
TAGS_CACHE_TTL_MS,
|
|
@@ -55,10 +54,22 @@ import {
|
|
|
55
54
|
getProviderForAccount,
|
|
56
55
|
hasCredential,
|
|
57
56
|
primaryProviderId,
|
|
57
|
+
findProviderForModel,
|
|
58
|
+
getProviderIdForPoolKey,
|
|
59
|
+
PROVIDER_ORDER,
|
|
58
60
|
} from "./providers/registry.js";
|
|
59
61
|
import type { QuotaFetchContext } from "./providers/adapter.js";
|
|
60
62
|
import { logger } from "./logger.js";
|
|
61
63
|
import { getAccountProxyDispatcher } from "./providers/proxy-dispatcher.js";
|
|
64
|
+
import {
|
|
65
|
+
fetchCodexCatalog,
|
|
66
|
+
getCodexModels,
|
|
67
|
+
isCodexRequestModel,
|
|
68
|
+
isCodexProviderModelId,
|
|
69
|
+
} from "./providers/openai-codex/catalog.js";
|
|
70
|
+
import { CodexOAuthError } from "./providers/openai-codex/oauth.js";
|
|
71
|
+
import { CODEX_QUOTA_MODEL_KEY } from "./providers/openai-codex/quota.js";
|
|
72
|
+
import { OPENCODE_ZEN_PROVIDER_ID } from "./providers/opencode-zen/credentials.js";
|
|
62
73
|
import { getUpdateInfo } from "./version-check.js";
|
|
63
74
|
import { getNotifications } from "./notification-poller.js";
|
|
64
75
|
import { getConfiguredAdminToken } from "./admin-auth.js";
|
|
@@ -119,6 +130,7 @@ export class AccountRotator {
|
|
|
119
130
|
private static readonly MAX_LATENCY_RECORDS = 200;
|
|
120
131
|
private requestLog: StatusResponse["requestLog"] = [];
|
|
121
132
|
private ollamaModels = new Set<string>();
|
|
133
|
+
private codexModels = new Set<string>(getCodexModels().map((model) => model.id));
|
|
122
134
|
private readonly usagePredictor = new UsagePredictor();
|
|
123
135
|
private lastOllamaCatalogFetch = 0;
|
|
124
136
|
|
|
@@ -130,6 +142,41 @@ export class AccountRotator {
|
|
|
130
142
|
return [...this.ollamaModels];
|
|
131
143
|
}
|
|
132
144
|
|
|
145
|
+
hasActiveProvider(providerId: string): boolean {
|
|
146
|
+
return this.accounts.some(
|
|
147
|
+
(account) =>
|
|
148
|
+
!account.disabled &&
|
|
149
|
+
!account.flagged &&
|
|
150
|
+
hasCredential(account.config, providerId),
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
setCodexModels(models: string[]): void {
|
|
155
|
+
this.codexModels = new Set(
|
|
156
|
+
models
|
|
157
|
+
.map((model) => model.trim())
|
|
158
|
+
.filter((model): model is string => isCodexProviderModelId(model)),
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
getCodexModels(): string[] {
|
|
163
|
+
return [...this.codexModels];
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async primeCodexCatalog(): Promise<void> {
|
|
167
|
+
const account = this.accounts.find(
|
|
168
|
+
(candidate) => hasCredential(candidate.config, "openai-codex") && !candidate.disabled && !candidate.flagged,
|
|
169
|
+
);
|
|
170
|
+
if (!account) return;
|
|
171
|
+
try {
|
|
172
|
+
await this.ensureValidTokenForProvider(account, "openai-codex");
|
|
173
|
+
const models = await fetchCodexCatalog(account);
|
|
174
|
+
this.setCodexModels(models.map((model) => model.id));
|
|
175
|
+
} catch {
|
|
176
|
+
// The verified base allowlist stays active when discovery is unavailable.
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
133
180
|
/** Fetch the Ollama Cloud catalog once at startup so provider-aware routing and /v1/models work before the first quota poll. */
|
|
134
181
|
primeOllamaCatalog(): Promise<void> {
|
|
135
182
|
return this.refreshOllamaCatalogOnce();
|
|
@@ -585,7 +632,7 @@ export class AccountRotator {
|
|
|
585
632
|
private logConsolidatedPoll(account: AccountRuntime): void {
|
|
586
633
|
const stash = account.lastPollByProvider;
|
|
587
634
|
if (!stash) return;
|
|
588
|
-
const ordered =
|
|
635
|
+
const ordered = PROVIDER_ORDER
|
|
589
636
|
.filter((pid) => stash[pid])
|
|
590
637
|
.map((pid) => `${pid}: ${stash[pid]}`);
|
|
591
638
|
if (ordered.length > 0) {
|
|
@@ -602,13 +649,16 @@ export class AccountRotator {
|
|
|
602
649
|
const available = this.accounts.filter((a) => !a.disabled && !a.flagged);
|
|
603
650
|
for (const account of available) {
|
|
604
651
|
try {
|
|
605
|
-
await this.ensureValidToken(account);
|
|
606
652
|
const quotaCtx: QuotaFetchContext = {
|
|
607
653
|
log: (message) => this.log(message),
|
|
608
654
|
markFlagged: (acc, reason, options) =>
|
|
609
655
|
this.markFlagged(acc, reason, options),
|
|
610
656
|
reportQuotaPollFlag: (acc, statusCode, errorText) =>
|
|
611
657
|
this.reportQuotaPollFlag(acc, statusCode, errorText),
|
|
658
|
+
markProviderInvalid: (acc, providerId, reason) =>
|
|
659
|
+
this.markProviderInvalid(acc, providerId, reason),
|
|
660
|
+
setProviderCooldown: (acc, providerId, durationMs) =>
|
|
661
|
+
this.setProviderCooldown(acc, providerId, durationMs),
|
|
612
662
|
};
|
|
613
663
|
// Parent-account model: poll quota for every provider credential
|
|
614
664
|
// the account holds (Google OAuth pools + Ollama usage pools).
|
|
@@ -617,7 +667,12 @@ export class AccountRotator {
|
|
|
617
667
|
);
|
|
618
668
|
if (providerIds.size === 0) providerIds.add(primaryProviderId(account.config));
|
|
619
669
|
for (const pid of providerIds) {
|
|
620
|
-
|
|
670
|
+
try {
|
|
671
|
+
await this.ensureValidTokenForProvider(account, pid);
|
|
672
|
+
await getProviderAdapter(pid).fetchQuota(account, quotaCtx);
|
|
673
|
+
} catch {
|
|
674
|
+
// One invalid provider credential must not suppress sibling pools.
|
|
675
|
+
}
|
|
621
676
|
}
|
|
622
677
|
} catch {
|
|
623
678
|
// Token refresh or quota fetch failed, skip this account
|
|
@@ -633,20 +688,19 @@ export class AccountRotator {
|
|
|
633
688
|
// pools (100% quota with resetTime very close to a full 5h or 7d window — timer exists but
|
|
634
689
|
// untouched). Deduplicates by upstream model within this cycle.
|
|
635
690
|
if (this.autoWarmupEnabled && account.allowFreshWindowStartsOverride) {
|
|
636
|
-
const idlePools = account.quota.filter(
|
|
691
|
+
const idlePools = account.quota.filter(
|
|
692
|
+
(q) =>
|
|
693
|
+
this.isQuotaIdleForKickstart(q) &&
|
|
694
|
+
this.getKickstartTarget(account, q.modelKey) !== null,
|
|
695
|
+
);
|
|
637
696
|
if (idlePools.length > 0) {
|
|
638
697
|
const alreadySent =
|
|
639
698
|
this.warmupSentThisCycle.get(account.config.email) ?? new Set<string>();
|
|
640
699
|
const upstreamToQuotaKey = new Map<string, string>();
|
|
641
700
|
for (const q of idlePools) {
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
const
|
|
645
|
-
q.modelKey === "session"
|
|
646
|
-
? getProviderAdapter("ollama")
|
|
647
|
-
: getProviderForAccount(account.config);
|
|
648
|
-
const upstream =
|
|
649
|
-
warmupAdapter.getKickstartModelForPool(q.modelKey) ?? q.modelKey;
|
|
701
|
+
const target = this.getKickstartTarget(account, q.modelKey);
|
|
702
|
+
if (!target) continue;
|
|
703
|
+
const upstream = target.upstreamModel;
|
|
650
704
|
if (!upstreamToQuotaKey.has(upstream)) {
|
|
651
705
|
upstreamToQuotaKey.set(upstream, q.modelKey);
|
|
652
706
|
}
|
|
@@ -739,7 +793,12 @@ export class AccountRotator {
|
|
|
739
793
|
|
|
740
794
|
// Get quota % for a specific model on an account. Returns -1 if no data.
|
|
741
795
|
private getModelQuota(account: AccountRuntime, modelKey: string): number {
|
|
742
|
-
const q = account.quota.find((q) => q.modelKey === modelKey)
|
|
796
|
+
const q = account.quota.find((q) => q.modelKey === modelKey) ??
|
|
797
|
+
(modelKey.startsWith(`${CODEX_QUOTA_MODEL_KEY}:`)
|
|
798
|
+
? account.quota.find((candidate) => candidate.modelKey === CODEX_QUOTA_MODEL_KEY)
|
|
799
|
+
: modelKey.startsWith(`${OPENCODE_ZEN_PROVIDER_ID}:`)
|
|
800
|
+
? account.quota.find((candidate) => candidate.providerId === OPENCODE_ZEN_PROVIDER_ID)
|
|
801
|
+
: undefined);
|
|
743
802
|
return q ? q.percentRemaining : -1;
|
|
744
803
|
}
|
|
745
804
|
|
|
@@ -748,7 +807,12 @@ export class AccountRotator {
|
|
|
748
807
|
account: AccountRuntime,
|
|
749
808
|
modelKey: string,
|
|
750
809
|
): "fresh" | "5h" | "7d" {
|
|
751
|
-
const q = account.quota.find((q) => q.modelKey === modelKey)
|
|
810
|
+
const q = account.quota.find((q) => q.modelKey === modelKey) ??
|
|
811
|
+
(modelKey.startsWith(`${CODEX_QUOTA_MODEL_KEY}:`)
|
|
812
|
+
? account.quota.find((candidate) => candidate.modelKey === CODEX_QUOTA_MODEL_KEY)
|
|
813
|
+
: modelKey.startsWith(`${OPENCODE_ZEN_PROVIDER_ID}:`)
|
|
814
|
+
? account.quota.find((candidate) => candidate.providerId === OPENCODE_ZEN_PROVIDER_ID)
|
|
815
|
+
: undefined);
|
|
752
816
|
return q?.timerType ?? "fresh";
|
|
753
817
|
}
|
|
754
818
|
|
|
@@ -765,6 +829,32 @@ export class AccountRotator {
|
|
|
765
829
|
return within(5 * 3600 * 1000) || within(7 * 24 * 3600 * 1000);
|
|
766
830
|
}
|
|
767
831
|
|
|
832
|
+
/**
|
|
833
|
+
* Resolve kickstart by quota-pool owner, never by the account's primary
|
|
834
|
+
* provider. Parent accounts may hold several credentials, and Codex pools
|
|
835
|
+
* deliberately have no kickstart implementation.
|
|
836
|
+
*/
|
|
837
|
+
private getKickstartTarget(
|
|
838
|
+
account: AccountRuntime,
|
|
839
|
+
quotaModelKey: string,
|
|
840
|
+
): { providerId: string; adapter: ReturnType<typeof getProviderAdapter>; upstreamModel: string } | null {
|
|
841
|
+
const quota = account.quota.find((candidate) => candidate.modelKey === quotaModelKey);
|
|
842
|
+
const providerId =
|
|
843
|
+
quota?.providerId ??
|
|
844
|
+
(quotaModelKey === "session"
|
|
845
|
+
? "ollama"
|
|
846
|
+
: quotaModelKey === CODEX_QUOTA_MODEL_KEY ||
|
|
847
|
+
quotaModelKey.startsWith(`${CODEX_QUOTA_MODEL_KEY}:`)
|
|
848
|
+
? "openai-codex"
|
|
849
|
+
: DEFAULT_PROVIDER);
|
|
850
|
+
if (!hasCredential(account.config, providerId)) return null;
|
|
851
|
+
|
|
852
|
+
const adapter = getProviderAdapter(providerId);
|
|
853
|
+
const upstreamModel = adapter.getKickstartModelForPool(quotaModelKey);
|
|
854
|
+
if (!upstreamModel) return null;
|
|
855
|
+
return { providerId, adapter, upstreamModel };
|
|
856
|
+
}
|
|
857
|
+
|
|
768
858
|
// Timer priority for a specific model:
|
|
769
859
|
// 1 (highest) = 5h timer -> only class with hard quota loss if underused before reset
|
|
770
860
|
// 2 = 7d timer -> already ticking, keep those long windows moving
|
|
@@ -1483,13 +1573,18 @@ export class AccountRotator {
|
|
|
1483
1573
|
}
|
|
1484
1574
|
|
|
1485
1575
|
if (modelKey && state) {
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1576
|
+
try {
|
|
1577
|
+
const restored = await this.restorePreferredModelAccount(
|
|
1578
|
+
modelKey,
|
|
1579
|
+
now,
|
|
1580
|
+
state,
|
|
1581
|
+
idx,
|
|
1582
|
+
);
|
|
1583
|
+
if (restored) return restored;
|
|
1584
|
+
} catch (error) {
|
|
1585
|
+
if (!this.isCodexPoolKey(modelKey)) throw error;
|
|
1586
|
+
return this.rotateModelForRequest(modelKey, now, idx);
|
|
1587
|
+
}
|
|
1493
1588
|
}
|
|
1494
1589
|
|
|
1495
1590
|
if (current && modelKey && !this.isProviderEligibleForKey(current, modelKey)) {
|
|
@@ -1551,12 +1646,15 @@ export class AccountRotator {
|
|
|
1551
1646
|
}
|
|
1552
1647
|
this.startRequest(current, modelKey ?? undefined);
|
|
1553
1648
|
try {
|
|
1554
|
-
await this.
|
|
1649
|
+
await this.ensureValidTokenForModel(current, modelKey);
|
|
1555
1650
|
if (modelKey) this.countModelAssignment(modelKey);
|
|
1556
1651
|
return current;
|
|
1557
1652
|
} catch (err) {
|
|
1558
1653
|
this.refundTokenBucket(current, Date.now());
|
|
1559
1654
|
this.finishRequest(current, modelKey ?? undefined);
|
|
1655
|
+
if (modelKey && this.isCodexPoolKey(modelKey)) {
|
|
1656
|
+
return this.rotateModelForRequest(modelKey, now, idx);
|
|
1657
|
+
}
|
|
1560
1658
|
throw err;
|
|
1561
1659
|
}
|
|
1562
1660
|
}
|
|
@@ -1642,7 +1740,15 @@ export class AccountRotator {
|
|
|
1642
1740
|
this.log(
|
|
1643
1741
|
`[${modelKey}] Rotated to ${best.config.label || best.config.email} [${timerType}] (quota: ${quota >= 0 ? quota + "%" : "unknown"})`,
|
|
1644
1742
|
);
|
|
1645
|
-
|
|
1743
|
+
try {
|
|
1744
|
+
return await this.activateModelAccount(modelKey, best, stickyAccountIndex);
|
|
1745
|
+
} catch (error) {
|
|
1746
|
+
if (!this.isCodexPoolKey(modelKey)) throw error;
|
|
1747
|
+
// A bad Codex refresh token is provider-scoped. The failed account is
|
|
1748
|
+
// marked invalid by ensureValidTokenForProvider, so retry selection
|
|
1749
|
+
// with the next Codex account without touching Google/Ollama pools.
|
|
1750
|
+
return this.rotateModel(modelKey, now, this.accounts.indexOf(best));
|
|
1751
|
+
}
|
|
1646
1752
|
}
|
|
1647
1753
|
|
|
1648
1754
|
if (
|
|
@@ -1707,7 +1813,7 @@ export class AccountRotator {
|
|
|
1707
1813
|
await this.saveState();
|
|
1708
1814
|
this.startRequest(account, modelKey);
|
|
1709
1815
|
try {
|
|
1710
|
-
await this.
|
|
1816
|
+
await this.ensureValidTokenForModel(account, modelKey);
|
|
1711
1817
|
return account;
|
|
1712
1818
|
} catch (err) {
|
|
1713
1819
|
this.refundTokenBucket(account, Date.now());
|
|
@@ -2246,6 +2352,7 @@ export class AccountRotator {
|
|
|
2246
2352
|
const lower = model.toLowerCase();
|
|
2247
2353
|
if (lower.includes("opus")) pricing = MODEL_PRICING["claude-opus-4-6-thinking"];
|
|
2248
2354
|
else if (lower.includes("sonnet")) pricing = MODEL_PRICING["claude-sonnet-4-6"];
|
|
2355
|
+
else if (lower.includes("3.7-flash")) pricing = MODEL_PRICING["gemini-3.7-flash-tiered"];
|
|
2249
2356
|
else if (lower.includes("3.6-flash")) pricing = MODEL_PRICING["gemini-3.6-flash-high"];
|
|
2250
2357
|
else if (lower.includes("3.5-flash")) pricing = MODEL_PRICING["gemini-3.5-flash-high"];
|
|
2251
2358
|
else if (lower.includes("flash")) pricing = MODEL_PRICING["gemini-3-flash"];
|
|
@@ -2303,8 +2410,11 @@ export class AccountRotator {
|
|
|
2303
2410
|
): void {
|
|
2304
2411
|
const now = Date.now();
|
|
2305
2412
|
const modelKey = model
|
|
2306
|
-
? (resolveQuotaModelKey(model) ?? "__default__")
|
|
2413
|
+
? (this.resolvePoolKeyForModel(model) ?? resolveQuotaModelKey(model) ?? "__default__")
|
|
2307
2414
|
: "__default__";
|
|
2415
|
+
if (model && (modelKey.startsWith(`${CODEX_QUOTA_MODEL_KEY}:`) || isCodexRequestModel(model))) {
|
|
2416
|
+
this.setProviderCooldown(account, "openai-codex", cooldownMs);
|
|
2417
|
+
}
|
|
2308
2418
|
account.cooldownsByModel[modelKey] = now + cooldownMs;
|
|
2309
2419
|
account.quotaExhaustedAt = now;
|
|
2310
2420
|
|
|
@@ -2323,15 +2433,24 @@ export class AccountRotator {
|
|
|
2323
2433
|
providerResourceExhausted = false,
|
|
2324
2434
|
): void {
|
|
2325
2435
|
const now = Date.now();
|
|
2326
|
-
if (
|
|
2327
|
-
|
|
2436
|
+
if (model && isCodexRequestModel(model)) {
|
|
2437
|
+
this.setProviderCooldown(account, "openai-codex", cooldownMs);
|
|
2438
|
+
return;
|
|
2439
|
+
}
|
|
2440
|
+
const poolKey = model ? this.resolvePoolKeyForModel(model) : null;
|
|
2441
|
+
const isAccountScopedProvider =
|
|
2442
|
+
providerResourceExhausted ||
|
|
2443
|
+
(poolKey && (poolKey.startsWith("opencode-zen") || poolKey.startsWith("session")));
|
|
2444
|
+
|
|
2445
|
+
if (isAccountScopedProvider) {
|
|
2446
|
+
// Account-level daily/weekly quota exhaustion or per-key rate limit is not a model outage:
|
|
2328
2447
|
// the account already gets an individual cooldown from markExhausted,
|
|
2329
2448
|
// so it must not arm the project or model circuit breaker and block
|
|
2330
2449
|
// healthy accounts that still have quota.
|
|
2331
2450
|
return;
|
|
2332
2451
|
}
|
|
2333
2452
|
const modelKey = model
|
|
2334
|
-
? (resolveQuotaModelKey(model) ?? "__default__")
|
|
2453
|
+
? (this.resolvePoolKeyForModel(model) ?? resolveQuotaModelKey(model) ?? "__default__")
|
|
2335
2454
|
: "__default__";
|
|
2336
2455
|
const windowMs =
|
|
2337
2456
|
this.config.projectCircuitBreakerWindowMs ?? 10 * 60 * 1000;
|
|
@@ -2596,7 +2715,11 @@ export class AccountRotator {
|
|
|
2596
2715
|
return true;
|
|
2597
2716
|
}
|
|
2598
2717
|
|
|
2599
|
-
async ensureValidToken(account: AccountRuntime): Promise<void> {
|
|
2718
|
+
async ensureValidToken(account: AccountRuntime, providerId?: string): Promise<void> {
|
|
2719
|
+
if (providerId) {
|
|
2720
|
+
await this.ensureValidTokenForProvider(account, providerId);
|
|
2721
|
+
return;
|
|
2722
|
+
}
|
|
2600
2723
|
const adapter = getProviderForAccount(account.config);
|
|
2601
2724
|
try {
|
|
2602
2725
|
await adapter.ensureValidToken(account);
|
|
@@ -2612,6 +2735,72 @@ export class AccountRotator {
|
|
|
2612
2735
|
}
|
|
2613
2736
|
}
|
|
2614
2737
|
|
|
2738
|
+
async ensureValidTokenForProvider(
|
|
2739
|
+
account: AccountRuntime,
|
|
2740
|
+
providerId: string,
|
|
2741
|
+
): Promise<void> {
|
|
2742
|
+
const adapter = getProviderAdapter(providerId);
|
|
2743
|
+
try {
|
|
2744
|
+
await adapter.ensureValidToken(account);
|
|
2745
|
+
} catch (error) {
|
|
2746
|
+
if (
|
|
2747
|
+
providerId === "openai-codex" &&
|
|
2748
|
+
(error instanceof CodexOAuthError ||
|
|
2749
|
+
(typeof error === "object" && error !== null &&
|
|
2750
|
+
(error as { reloginRequired?: unknown }).reloginRequired === true))
|
|
2751
|
+
) {
|
|
2752
|
+
const reason = error instanceof Error
|
|
2753
|
+
? error.message
|
|
2754
|
+
: "Codex credential requires re-authentication";
|
|
2755
|
+
this.markProviderInvalid(account, providerId, reason);
|
|
2756
|
+
}
|
|
2757
|
+
throw error;
|
|
2758
|
+
}
|
|
2759
|
+
}
|
|
2760
|
+
|
|
2761
|
+
private isCodexPoolKey(modelKey: string): boolean {
|
|
2762
|
+
return modelKey.startsWith(`${CODEX_QUOTA_MODEL_KEY}:`);
|
|
2763
|
+
}
|
|
2764
|
+
|
|
2765
|
+
private async ensureValidTokenForModel(
|
|
2766
|
+
account: AccountRuntime,
|
|
2767
|
+
modelKey: string | null,
|
|
2768
|
+
): Promise<void> {
|
|
2769
|
+
if (modelKey) {
|
|
2770
|
+
const providerId = getProviderIdForPoolKey(modelKey);
|
|
2771
|
+
await this.ensureValidToken(account, providerId);
|
|
2772
|
+
return;
|
|
2773
|
+
}
|
|
2774
|
+
await this.ensureValidToken(account);
|
|
2775
|
+
}
|
|
2776
|
+
|
|
2777
|
+
markProviderInvalid(
|
|
2778
|
+
account: AccountRuntime,
|
|
2779
|
+
providerId: string,
|
|
2780
|
+
reason: string,
|
|
2781
|
+
): void {
|
|
2782
|
+
account.invalidProviders ??= {};
|
|
2783
|
+
account.invalidProviders[providerId] = reason;
|
|
2784
|
+
account.providerTokens ??= {};
|
|
2785
|
+
account.providerTokens[providerId] = { accessToken: null, tokenExpires: 0 };
|
|
2786
|
+
account.lastError = reason;
|
|
2787
|
+
this.log(`${account.config.email}: ${providerId} credential requires re-authentication`, "warn");
|
|
2788
|
+
this.scheduleStateSave();
|
|
2789
|
+
}
|
|
2790
|
+
|
|
2791
|
+
setProviderCooldown(
|
|
2792
|
+
account: AccountRuntime,
|
|
2793
|
+
providerId: string,
|
|
2794
|
+
durationMs: number,
|
|
2795
|
+
): void {
|
|
2796
|
+
account.providerCooldowns ??= {};
|
|
2797
|
+
account.providerCooldowns[providerId] = Math.max(
|
|
2798
|
+
account.providerCooldowns[providerId] ?? 0,
|
|
2799
|
+
Date.now() + Math.max(0, durationMs),
|
|
2800
|
+
);
|
|
2801
|
+
this.scheduleStateSave();
|
|
2802
|
+
}
|
|
2803
|
+
|
|
2615
2804
|
private isAvailable(account: AccountRuntime, now: number): boolean {
|
|
2616
2805
|
if (account.disabled) return false;
|
|
2617
2806
|
if (account.flagged) return false;
|
|
@@ -2626,6 +2815,9 @@ export class AccountRotator {
|
|
|
2626
2815
|
now: number,
|
|
2627
2816
|
): boolean {
|
|
2628
2817
|
if (!this.isAvailable(account, now)) return false;
|
|
2818
|
+
const providerId = getProviderIdForPoolKey(modelKey);
|
|
2819
|
+
if (account.invalidProviders?.[providerId]) return false;
|
|
2820
|
+
if ((account.providerCooldowns?.[providerId] ?? 0) > now) return false;
|
|
2629
2821
|
const modelCooldown = account.cooldownsByModel[modelKey] ?? 0;
|
|
2630
2822
|
if (modelCooldown > now) return false;
|
|
2631
2823
|
// Ollama Cloud (pool key "session") imposes no per-account concurrency
|
|
@@ -2701,12 +2893,10 @@ export class AccountRotator {
|
|
|
2701
2893
|
account: AccountRuntime,
|
|
2702
2894
|
modelKey: string,
|
|
2703
2895
|
): boolean {
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
|
|
2707
|
-
|
|
2708
|
-
// account (google + ollama) must stay eligible here — it can serve both.
|
|
2709
|
-
return hasCredential(account.config, DEFAULT_PROVIDER);
|
|
2896
|
+
const providerId = getProviderIdForPoolKey(modelKey);
|
|
2897
|
+
return hasCredential(account.config, providerId) &&
|
|
2898
|
+
!account.invalidProviders?.[providerId] &&
|
|
2899
|
+
(account.providerCooldowns?.[providerId] ?? 0) <= Date.now();
|
|
2710
2900
|
}
|
|
2711
2901
|
|
|
2712
2902
|
/** Public pool-key resolution for quota routing display/logging. */
|
|
@@ -2715,7 +2905,14 @@ export class AccountRotator {
|
|
|
2715
2905
|
}
|
|
2716
2906
|
|
|
2717
2907
|
private resolvePoolKeyForModel(model: string): string | null {
|
|
2718
|
-
|
|
2908
|
+
const context = {
|
|
2909
|
+
ollamaModels: this.ollamaModels,
|
|
2910
|
+
codexModels: this.codexModels,
|
|
2911
|
+
};
|
|
2912
|
+
const adapter = findProviderForModel(model, context);
|
|
2913
|
+
if (adapter?.getPoolKey) {
|
|
2914
|
+
return adapter.getPoolKey(model);
|
|
2915
|
+
}
|
|
2719
2916
|
return resolveQuotaModelKey(model) ?? null;
|
|
2720
2917
|
}
|
|
2721
2918
|
|
|
@@ -2754,7 +2951,7 @@ export class AccountRotator {
|
|
|
2754
2951
|
if (this.protectivePauseUntil > now)
|
|
2755
2952
|
retryTimes.push(this.protectivePauseUntil);
|
|
2756
2953
|
const modelKey = model
|
|
2757
|
-
? (resolveQuotaModelKey(model) ?? "__default__")
|
|
2954
|
+
? (this.resolvePoolKeyForModel(model) ?? resolveQuotaModelKey(model) ?? "__default__")
|
|
2758
2955
|
: "__default__";
|
|
2759
2956
|
const dailyResetAt = nextUtcDayStartMs(now);
|
|
2760
2957
|
const modelBreaker = this.modelBreakers[modelKey] ?? 0;
|
|
@@ -2837,6 +3034,8 @@ export class AccountRotator {
|
|
|
2837
3034
|
lastError: a.lastError,
|
|
2838
3035
|
consecutiveErrors: a.consecutiveErrors,
|
|
2839
3036
|
hasValidToken: !!(a.accessToken && a.tokenExpires > now),
|
|
3037
|
+
invalidProviders: a.invalidProviders,
|
|
3038
|
+
providerCooldowns: a.providerCooldowns,
|
|
2840
3039
|
quota: a.quota,
|
|
2841
3040
|
inFlightRequests: a.inFlightRequests,
|
|
2842
3041
|
inFlightByModel: a.inFlightByModel,
|
|
@@ -2939,6 +3138,7 @@ export class AccountRotator {
|
|
|
2939
3138
|
notifications: getNotifications(),
|
|
2940
3139
|
hostedOAuthConfigured: isHostedOAuthConfigured(),
|
|
2941
3140
|
ollamaModels: this.getOllamaModels(),
|
|
3141
|
+
codexModels: this.getCodexModels(),
|
|
2942
3142
|
modelTierAccess: this.accounts.some((a) =>
|
|
2943
3143
|
hasCredential(a.config, "ollama"),
|
|
2944
3144
|
)
|
|
@@ -3425,8 +3625,18 @@ export class AccountRotator {
|
|
|
3425
3625
|
};
|
|
3426
3626
|
}
|
|
3427
3627
|
|
|
3628
|
+
const target = this.getKickstartTarget(account, quotaModelKey);
|
|
3629
|
+
if (!target) {
|
|
3630
|
+
return {
|
|
3631
|
+
ok: false,
|
|
3632
|
+
status: 409,
|
|
3633
|
+
upstreamModel: "",
|
|
3634
|
+
error: "quota pool has no configured kickstart provider",
|
|
3635
|
+
};
|
|
3636
|
+
}
|
|
3637
|
+
|
|
3428
3638
|
try {
|
|
3429
|
-
await this.
|
|
3639
|
+
await this.ensureValidTokenForProvider(account, target.providerId);
|
|
3430
3640
|
} catch (err) {
|
|
3431
3641
|
return {
|
|
3432
3642
|
ok: false,
|
|
@@ -3436,18 +3646,13 @@ export class AccountRotator {
|
|
|
3436
3646
|
};
|
|
3437
3647
|
}
|
|
3438
3648
|
|
|
3439
|
-
const kickstartAdapter =
|
|
3440
|
-
quotaModelKey === "session"
|
|
3441
|
-
? getProviderAdapter("ollama")
|
|
3442
|
-
: getProviderForAccount(account.config);
|
|
3649
|
+
const kickstartAdapter = target.adapter;
|
|
3443
3650
|
const isOllamaAccount = kickstartAdapter.id === "ollama";
|
|
3444
3651
|
if (!account.accessToken && !isOllamaAccount) {
|
|
3445
3652
|
return { ok: false, status: 401, upstreamModel: "", error: "no access token" };
|
|
3446
3653
|
}
|
|
3447
3654
|
|
|
3448
|
-
const upstreamModel =
|
|
3449
|
-
kickstartAdapter.getKickstartModelForPool(quotaModelKey) ??
|
|
3450
|
-
quotaModelKey;
|
|
3655
|
+
const upstreamModel = target.upstreamModel;
|
|
3451
3656
|
|
|
3452
3657
|
const controller = new AbortController();
|
|
3453
3658
|
const timeout = setTimeout(() => controller.abort(), 15_000);
|
|
@@ -3579,7 +3784,11 @@ export class AccountRotator {
|
|
|
3579
3784
|
return { ok: false, error: "account not found", results: [] };
|
|
3580
3785
|
}
|
|
3581
3786
|
|
|
3582
|
-
const idlePools = account.quota.filter(
|
|
3787
|
+
const idlePools = account.quota.filter(
|
|
3788
|
+
(q) =>
|
|
3789
|
+
this.isQuotaIdleForKickstart(q) &&
|
|
3790
|
+
this.getKickstartTarget(account, q.modelKey) !== null,
|
|
3791
|
+
);
|
|
3583
3792
|
if (idlePools.length === 0) {
|
|
3584
3793
|
return { ok: true, results: [] };
|
|
3585
3794
|
}
|
|
@@ -3587,7 +3796,9 @@ export class AccountRotator {
|
|
|
3587
3796
|
// Deduplicate: group quota pool keys by their upstream model
|
|
3588
3797
|
const upstreamToQuotaPools = new Map<string, string[]>();
|
|
3589
3798
|
for (const q of idlePools) {
|
|
3590
|
-
const
|
|
3799
|
+
const target = this.getKickstartTarget(account, q.modelKey);
|
|
3800
|
+
if (!target) continue;
|
|
3801
|
+
const upstream = target.upstreamModel;
|
|
3591
3802
|
const list = upstreamToQuotaPools.get(upstream) ?? [];
|
|
3592
3803
|
list.push(q.modelKey);
|
|
3593
3804
|
upstreamToQuotaPools.set(upstream, list);
|
package/src/spend-logger.ts
CHANGED
|
@@ -223,6 +223,8 @@ export function calculateCost(model: string, promptTokens: number, completionTok
|
|
|
223
223
|
pricing = MODEL_PRICING["claude-opus-4-6-thinking"];
|
|
224
224
|
} else if (lower.includes("sonnet")) {
|
|
225
225
|
pricing = MODEL_PRICING["claude-sonnet-4-6"];
|
|
226
|
+
} else if (lower.includes("3.7-flash")) {
|
|
227
|
+
pricing = MODEL_PRICING["gemini-3.7-flash-tiered"];
|
|
226
228
|
} else if (lower.includes("3.6-flash")) {
|
|
227
229
|
pricing = MODEL_PRICING["gemini-3.6-flash-high"];
|
|
228
230
|
} else if (lower.includes("3.5-flash")) {
|