tuxevil-rotator 3.7.2 → 3.9.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 +20 -1
- package/README.md +5 -1
- package/package.json +2 -1
- package/src/auto-routing.ts +582 -0
- package/src/compat.ts +292 -44
- package/src/config-defaults.ts +14 -0
- package/src/dashboard.ts +2 -2
- package/src/db-store.ts +9 -3
- package/src/key-auth.ts +15 -15
- package/src/onboarding.ts +189 -2
- package/src/providers/google-antigravity/forward.ts +8 -1
- package/src/providers/openai-codex/catalog.ts +8 -6
- package/src/providers/openai-codex/compat.ts +33 -5
- package/src/providers/registry.ts +12 -1
- package/src/proxy.ts +98 -19
- package/src/response-headers.ts +21 -0
- package/src/rotator.ts +108 -33
- package/src/static/dashboard.js +10 -1
- package/src/token-encryption.ts +91 -0
- package/src/types.ts +44 -3
- package/src/validators.ts +41 -0
package/src/proxy.ts
CHANGED
|
@@ -78,6 +78,7 @@ import {
|
|
|
78
78
|
import { requireAdmin } from "./admin-auth.js";
|
|
79
79
|
import { PayloadTooLargeError, readLimitedBody } from "./body-limit.js";
|
|
80
80
|
import { validateConfig, validateProxyRequestBody } from "./validators.js";
|
|
81
|
+
import { calculateBackoffMs } from "./fetch-with-retry.js";
|
|
81
82
|
import { logger } from "./logger.js";
|
|
82
83
|
import {
|
|
83
84
|
trackFeature,
|
|
@@ -103,8 +104,12 @@ export function providerAdapterForModel(
|
|
|
103
104
|
account: AccountRuntime,
|
|
104
105
|
model: string | undefined,
|
|
105
106
|
rotator?: { getOllamaModels?: () => string[]; getCodexModels?: () => string[] },
|
|
107
|
+
providerHint?: string,
|
|
106
108
|
): ProviderAdapter {
|
|
107
109
|
const creds = account.config.credentials ?? [];
|
|
110
|
+
if (providerHint && isKnownProvider(providerHint)) {
|
|
111
|
+
return getProviderAdapter(providerHint);
|
|
112
|
+
}
|
|
108
113
|
if (model) {
|
|
109
114
|
const context = {
|
|
110
115
|
ollamaModels: new Set(rotator?.getOllamaModels?.() ?? []),
|
|
@@ -134,11 +139,11 @@ export function providerAdapterForModel(
|
|
|
134
139
|
: fallback;
|
|
135
140
|
}
|
|
136
141
|
|
|
137
|
-
function routingModelKey(rotator: AccountRotator, model: string): string {
|
|
142
|
+
function routingModelKey(rotator: AccountRotator, model: string, providerId?: string): string {
|
|
138
143
|
const resolver = (rotator as unknown as {
|
|
139
|
-
resolveQuotaModelKeyForDisplay?: (value: string) => string | null;
|
|
144
|
+
resolveQuotaModelKeyForDisplay?: (value: string, provider?: string) => string | null;
|
|
140
145
|
}).resolveQuotaModelKeyForDisplay;
|
|
141
|
-
return resolver?.call(rotator, model) ?? resolveQuotaModelKey(model) ?? model;
|
|
146
|
+
return resolver?.call(rotator, model, providerId) ?? resolveQuotaModelKey(model) ?? model;
|
|
142
147
|
}
|
|
143
148
|
|
|
144
149
|
function observedModelKey(
|
|
@@ -235,6 +240,10 @@ export interface RequestBody {
|
|
|
235
240
|
userAgent?: string;
|
|
236
241
|
requestId?: string;
|
|
237
242
|
displayModel?: string;
|
|
243
|
+
providerId?: string;
|
|
244
|
+
selectionMode?: "typesafe" | "deterministic" | "shadow" | "disabled";
|
|
245
|
+
selectionConfidence?: number;
|
|
246
|
+
selectionReason?: string;
|
|
238
247
|
[key: string]: unknown;
|
|
239
248
|
}
|
|
240
249
|
|
|
@@ -275,6 +284,12 @@ export interface RotationAttemptContext {
|
|
|
275
284
|
requestStartMs: number;
|
|
276
285
|
endpoint: string;
|
|
277
286
|
retries: number;
|
|
287
|
+
providerId?: string;
|
|
288
|
+
requestedModel?: string;
|
|
289
|
+
selectedModel?: string;
|
|
290
|
+
selectionMode?: "typesafe" | "deterministic" | "shadow" | "disabled";
|
|
291
|
+
selectionConfidence?: number;
|
|
292
|
+
selectionReason?: string;
|
|
278
293
|
}
|
|
279
294
|
|
|
280
295
|
export type RotationOutcome<T> =
|
|
@@ -1197,7 +1212,7 @@ export async function withRotation<T>(
|
|
|
1197
1212
|
context?: RotationAttemptContext,
|
|
1198
1213
|
): RotationOutcome<T> => {
|
|
1199
1214
|
log(`[${model}] No healthy account available: ${reason}`, rotator, "warn");
|
|
1200
|
-
const retryAfterMs = rotator.getRetryAfterMs(model);
|
|
1215
|
+
const retryAfterMs = rotator.getRetryAfterMs(model, body.providerId);
|
|
1201
1216
|
const contextFields = context
|
|
1202
1217
|
? { context, totalMs: Date.now() - context.requestStartMs }
|
|
1203
1218
|
: {};
|
|
@@ -1223,7 +1238,7 @@ export async function withRotation<T>(
|
|
|
1223
1238
|
|
|
1224
1239
|
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
1225
1240
|
const admissionStartedMs = Date.now();
|
|
1226
|
-
const account = await rotator.getActiveAccount(model, signal);
|
|
1241
|
+
const account = await rotator.getActiveAccount(model, signal, body.providerId);
|
|
1227
1242
|
if (!account) {
|
|
1228
1243
|
if (signal?.aborted) {
|
|
1229
1244
|
return { ok: false, status: 499, errorText: "Client closed request" };
|
|
@@ -1232,7 +1247,7 @@ export async function withRotation<T>(
|
|
|
1232
1247
|
}
|
|
1233
1248
|
|
|
1234
1249
|
const label = account.config.label || account.config.email;
|
|
1235
|
-
const modelKey = routingModelKey(rotator, model);
|
|
1250
|
+
const modelKey = routingModelKey(rotator, model, body.providerId);
|
|
1236
1251
|
const displayModelKey = observedModelKey(
|
|
1237
1252
|
rotator,
|
|
1238
1253
|
body.displayModel || model,
|
|
@@ -1249,7 +1264,7 @@ export async function withRotation<T>(
|
|
|
1249
1264
|
};
|
|
1250
1265
|
const rotateAndRelease = async (): Promise<AccountRuntime | null> => {
|
|
1251
1266
|
releaseCurrentAccount();
|
|
1252
|
-
const nextAccount = await rotator.rotateToNext(model, account);
|
|
1267
|
+
const nextAccount = await rotator.rotateToNext(model, account, body.providerId);
|
|
1253
1268
|
return nextAccount;
|
|
1254
1269
|
};
|
|
1255
1270
|
const logRequestEnd = (status: string | number, extra = ""): void => {
|
|
@@ -1297,6 +1312,7 @@ export async function withRotation<T>(
|
|
|
1297
1312
|
account,
|
|
1298
1313
|
model,
|
|
1299
1314
|
rotator,
|
|
1315
|
+
body.providerId,
|
|
1300
1316
|
);
|
|
1301
1317
|
// A parent account may carry Google + Codex credentials; refresh the
|
|
1302
1318
|
// selected provider, never merely the account's primary provider.
|
|
@@ -1317,6 +1333,12 @@ export async function withRotation<T>(
|
|
|
1317
1333
|
requestStartMs,
|
|
1318
1334
|
endpoint,
|
|
1319
1335
|
retries: attempt,
|
|
1336
|
+
providerId: provider.id,
|
|
1337
|
+
requestedModel: body.displayModel || model,
|
|
1338
|
+
selectedModel: model,
|
|
1339
|
+
selectionMode: body.selectionMode,
|
|
1340
|
+
selectionConfidence: body.selectionConfidence,
|
|
1341
|
+
selectionReason: body.selectionReason,
|
|
1320
1342
|
};
|
|
1321
1343
|
|
|
1322
1344
|
const action = await classifyUpstreamResponse(
|
|
@@ -1349,7 +1371,7 @@ export async function withRotation<T>(
|
|
|
1349
1371
|
|
|
1350
1372
|
// success
|
|
1351
1373
|
const result = await onSuccess(response, context);
|
|
1352
|
-
const shouldRotate = rotator.recordRequest(account, model);
|
|
1374
|
+
const shouldRotate = rotator.recordRequest(account, model, body.providerId);
|
|
1353
1375
|
const inTokens =
|
|
1354
1376
|
result && typeof result === "object" && "inputTokens" in result
|
|
1355
1377
|
? (result as Record<string, unknown>).inputTokens
|
|
@@ -1367,7 +1389,7 @@ export async function withRotation<T>(
|
|
|
1367
1389
|
inTokens || outTokens ? ` inTokens=${inTokens} outTokens=${outTokens}` : "";
|
|
1368
1390
|
logRequestEnd(response.status, `endpoint=${endpoint}${ttfbInfo}${tokensInfo}`);
|
|
1369
1391
|
if (shouldRotate) {
|
|
1370
|
-
await rotator.rotateToNext(model, account);
|
|
1392
|
+
await rotator.rotateToNext(model, account, body.providerId);
|
|
1371
1393
|
}
|
|
1372
1394
|
return { ok: true, result, endpoint, context };
|
|
1373
1395
|
} catch (err) {
|
|
@@ -1423,9 +1445,18 @@ export async function withRotation<T>(
|
|
|
1423
1445
|
}
|
|
1424
1446
|
const nextAccount = await rotateAndRelease();
|
|
1425
1447
|
if (!nextAccount) {
|
|
1426
|
-
|
|
1427
|
-
|
|
1448
|
+
const waited = await sleepTransportRetry(
|
|
1449
|
+
requestId,
|
|
1450
|
+
label,
|
|
1451
|
+
attempt,
|
|
1452
|
+
maxRetries,
|
|
1453
|
+
rotator,
|
|
1454
|
+
signal,
|
|
1428
1455
|
);
|
|
1456
|
+
if (!waited) {
|
|
1457
|
+
return { ok: false, status: 499, errorText: "Client closed request" };
|
|
1458
|
+
}
|
|
1459
|
+
continue;
|
|
1429
1460
|
}
|
|
1430
1461
|
continue;
|
|
1431
1462
|
} finally {
|
|
@@ -1445,6 +1476,29 @@ function log(
|
|
|
1445
1476
|
rotator?.recordProxyEvent(msg, level);
|
|
1446
1477
|
}
|
|
1447
1478
|
|
|
1479
|
+
async function sleepTransportRetry(
|
|
1480
|
+
requestId: string,
|
|
1481
|
+
label: string,
|
|
1482
|
+
attempt: number,
|
|
1483
|
+
maxRetries: number,
|
|
1484
|
+
rotator?: AccountRotator,
|
|
1485
|
+
signal?: AbortSignal,
|
|
1486
|
+
): Promise<boolean> {
|
|
1487
|
+
if (signal?.aborted) return false;
|
|
1488
|
+
const backoffMs = calculateBackoffMs(attempt, 500, 5_000);
|
|
1489
|
+
log(
|
|
1490
|
+
`[${requestId}] Retrying after transport error on current account ${label} in ${backoffMs}ms (attempt ${attempt + 1}/${maxRetries})`,
|
|
1491
|
+
rotator,
|
|
1492
|
+
"warn",
|
|
1493
|
+
);
|
|
1494
|
+
try {
|
|
1495
|
+
await sleep(backoffMs, signal);
|
|
1496
|
+
return true;
|
|
1497
|
+
} catch {
|
|
1498
|
+
return false;
|
|
1499
|
+
}
|
|
1500
|
+
}
|
|
1501
|
+
|
|
1448
1502
|
function isGeminiGenerateContentPath(pathname: string): boolean {
|
|
1449
1503
|
const prefix = "/v1beta/models/";
|
|
1450
1504
|
if (!pathname.startsWith(prefix)) return false;
|
|
@@ -1768,6 +1822,12 @@ async function handleProxyRequest(
|
|
|
1768
1822
|
requestStartMs,
|
|
1769
1823
|
endpoint,
|
|
1770
1824
|
retries: attempt,
|
|
1825
|
+
providerId: provider.id,
|
|
1826
|
+
requestedModel: body.displayModel || body.model,
|
|
1827
|
+
selectedModel: body.model,
|
|
1828
|
+
selectionMode: body.selectionMode,
|
|
1829
|
+
selectionConfidence: body.selectionConfidence,
|
|
1830
|
+
selectionReason: body.selectionReason,
|
|
1771
1831
|
};
|
|
1772
1832
|
|
|
1773
1833
|
const action = await classifyUpstreamResponse(
|
|
@@ -1820,6 +1880,11 @@ async function handleProxyRequest(
|
|
|
1820
1880
|
healthScore: account.healthScore,
|
|
1821
1881
|
routingPolicy: rotator?.getConfig?.()?.routingPolicy || "timer-first",
|
|
1822
1882
|
retries: attempt,
|
|
1883
|
+
requestedModel: body.displayModel || body.model,
|
|
1884
|
+
selectedProvider: provider.id,
|
|
1885
|
+
modelSelection: body.selectionMode,
|
|
1886
|
+
selectionConfidence: body.selectionConfidence,
|
|
1887
|
+
selectionReason: body.selectionReason,
|
|
1823
1888
|
});
|
|
1824
1889
|
Object.assign(responseHeaders, rotatorHeaders);
|
|
1825
1890
|
|
|
@@ -1911,10 +1976,16 @@ async function handleProxyRequest(
|
|
|
1911
1976
|
}
|
|
1912
1977
|
const nextAccount = await rotateAndRelease();
|
|
1913
1978
|
if (!nextAccount) {
|
|
1914
|
-
|
|
1915
|
-
|
|
1979
|
+
const waited = await sleepTransportRetry(
|
|
1980
|
+
requestId,
|
|
1981
|
+
label,
|
|
1982
|
+
attempt,
|
|
1983
|
+
maxRetries,
|
|
1984
|
+
rotator,
|
|
1985
|
+
clientController.signal,
|
|
1916
1986
|
);
|
|
1917
|
-
return;
|
|
1987
|
+
if (!waited) return;
|
|
1988
|
+
continue;
|
|
1918
1989
|
}
|
|
1919
1990
|
continue;
|
|
1920
1991
|
} finally {
|
|
@@ -2038,12 +2109,20 @@ async function handleCodeAssistPassthrough(
|
|
|
2038
2109
|
}
|
|
2039
2110
|
releaseCurrentAccount();
|
|
2040
2111
|
const nextAccount = await rotator.rotateToNext(CODE_ASSIST_ROUTING_MODEL, account);
|
|
2041
|
-
if (nextAccount) {
|
|
2112
|
+
if (!nextAccount) {
|
|
2113
|
+
const label = account.config.label || account.config.email;
|
|
2114
|
+
const waited = await sleepTransportRetry(
|
|
2115
|
+
action,
|
|
2116
|
+
label,
|
|
2117
|
+
attempt,
|
|
2118
|
+
maxRetries,
|
|
2119
|
+
rotator,
|
|
2120
|
+
clientController.signal,
|
|
2121
|
+
);
|
|
2122
|
+
if (!waited) return;
|
|
2042
2123
|
continue;
|
|
2043
2124
|
}
|
|
2044
|
-
|
|
2045
|
-
res.end(JSON.stringify({ error: "No replacement Google account available" }));
|
|
2046
|
-
return;
|
|
2125
|
+
continue;
|
|
2047
2126
|
} finally {
|
|
2048
2127
|
releaseCurrentAccount();
|
|
2049
2128
|
}
|
|
@@ -2169,7 +2248,7 @@ export function startProxy(
|
|
|
2169
2248
|
if (method === "GET" && pathname === "/login-cli") {
|
|
2170
2249
|
if (!requireAdmin(req, res)) return;
|
|
2171
2250
|
trackFeature("cliLogin");
|
|
2172
|
-
serveCliLogin(res);
|
|
2251
|
+
serveCliLogin(res, rotator);
|
|
2173
2252
|
return;
|
|
2174
2253
|
}
|
|
2175
2254
|
|
package/src/response-headers.ts
CHANGED
|
@@ -28,6 +28,11 @@ export interface CompressionHeaderOptions {
|
|
|
28
28
|
export interface RotatorResponseHeaderOptions {
|
|
29
29
|
accountLabel?: string;
|
|
30
30
|
model?: string;
|
|
31
|
+
requestedModel?: string;
|
|
32
|
+
selectedProvider?: string;
|
|
33
|
+
modelSelection?: "typesafe" | "deterministic" | "shadow" | "disabled";
|
|
34
|
+
selectionConfidence?: number;
|
|
35
|
+
selectionReason?: string;
|
|
31
36
|
latencyMs?: number;
|
|
32
37
|
ttfbMs?: number;
|
|
33
38
|
inputTokens?: number;
|
|
@@ -61,6 +66,22 @@ export function buildRotatorResponseHeaders(
|
|
|
61
66
|
if (opts.model) {
|
|
62
67
|
headers["X-Rotator-Model"] = opts.model;
|
|
63
68
|
}
|
|
69
|
+
if (opts.requestedModel && opts.requestedModel !== opts.model) {
|
|
70
|
+
headers["X-Rotator-Requested-Model"] = opts.requestedModel;
|
|
71
|
+
}
|
|
72
|
+
if (opts.selectedProvider) {
|
|
73
|
+
headers["X-Rotator-Selected-Provider"] = opts.selectedProvider;
|
|
74
|
+
}
|
|
75
|
+
if (opts.modelSelection) {
|
|
76
|
+
headers["X-Rotator-Model-Selection"] = opts.modelSelection;
|
|
77
|
+
}
|
|
78
|
+
if (opts.selectionConfidence !== undefined && Number.isFinite(opts.selectionConfidence)) {
|
|
79
|
+
headers["X-Rotator-Selection-Confidence"] = opts.selectionConfidence.toFixed(3);
|
|
80
|
+
}
|
|
81
|
+
if (opts.selectionReason) {
|
|
82
|
+
const reason = opts.selectionReason.replace(/[^a-zA-Z0-9._-]/g, "").slice(0, 64);
|
|
83
|
+
if (reason) headers["X-Rotator-Selection-Reason"] = reason;
|
|
84
|
+
}
|
|
64
85
|
if (opts.latencyMs !== undefined && opts.latencyMs >= 0) {
|
|
65
86
|
headers["X-Rotator-Latency-Ms"] = String(Math.round(opts.latencyMs));
|
|
66
87
|
}
|
package/src/rotator.ts
CHANGED
|
@@ -84,6 +84,7 @@ import {
|
|
|
84
84
|
getCachedTokenUsage,
|
|
85
85
|
setCachedTokenUsage,
|
|
86
86
|
} from "./db-store.js";
|
|
87
|
+
import { isRedactedSecret, redactTypeSafeRoutingInConfig } from "./token-encryption.js";
|
|
87
88
|
import {
|
|
88
89
|
classifyRateLimitReason,
|
|
89
90
|
parseRetryAfterMs,
|
|
@@ -186,6 +187,7 @@ const MAX_NODE_TIMEOUT_MS = 2_147_483_647;
|
|
|
186
187
|
interface AccountRequestWaiter {
|
|
187
188
|
queueKey: string;
|
|
188
189
|
model?: string;
|
|
190
|
+
providerId?: string;
|
|
189
191
|
signal?: AbortSignal;
|
|
190
192
|
timer: ReturnType<typeof setTimeout>;
|
|
191
193
|
onAbort?: () => void;
|
|
@@ -260,6 +262,50 @@ export class AccountRotator {
|
|
|
260
262
|
);
|
|
261
263
|
}
|
|
262
264
|
|
|
265
|
+
/**
|
|
266
|
+
* Exposes only aggregate routing health to the optional Jev selector.
|
|
267
|
+
* Account identities and credentials stay inside the rotator.
|
|
268
|
+
*/
|
|
269
|
+
getAutoRoutingCandidateStatus(
|
|
270
|
+
model: string,
|
|
271
|
+
providerId: string,
|
|
272
|
+
): {
|
|
273
|
+
available: boolean;
|
|
274
|
+
operationalScore: number;
|
|
275
|
+
poolKey: string;
|
|
276
|
+
reason?: string;
|
|
277
|
+
} {
|
|
278
|
+
const poolKey = this.resolvePoolKeyForModel(model, providerId);
|
|
279
|
+
if (!poolKey) {
|
|
280
|
+
return { available: false, operationalScore: 0, poolKey: model, reason: "unknown-provider" };
|
|
281
|
+
}
|
|
282
|
+
const now = Date.now();
|
|
283
|
+
const policy = this.config.routingPolicy || "timer-first";
|
|
284
|
+
let bestScore = 0;
|
|
285
|
+
let available = false;
|
|
286
|
+
let firstReason: string | undefined;
|
|
287
|
+
for (const account of this.accounts) {
|
|
288
|
+
const rejection = this.getRoutingRejectionForModel(account, poolKey, now, policy);
|
|
289
|
+
if (rejection) {
|
|
290
|
+
firstReason ??= rejection.detail;
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
available = true;
|
|
294
|
+
const quota = this.getModelQuota(account, poolKey);
|
|
295
|
+
const quotaScore = quota < 0 ? 0.5 : Math.max(0, Math.min(1, quota / 100));
|
|
296
|
+
const capacity = Math.max(1, this.config.maxConcurrentRequestsPerAccount ?? 5);
|
|
297
|
+
const loadScore = Math.max(0, Math.min(1, 1 - account.inFlightRequests / capacity));
|
|
298
|
+
const healthScore = this.getHealthScoreBreakdown(account).score;
|
|
299
|
+
bestScore = Math.max(bestScore, 0.55 * healthScore + 0.3 * quotaScore + 0.15 * loadScore);
|
|
300
|
+
}
|
|
301
|
+
return {
|
|
302
|
+
available,
|
|
303
|
+
operationalScore: bestScore,
|
|
304
|
+
poolKey,
|
|
305
|
+
...(available ? {} : { reason: firstReason ?? "no-routable-account" }),
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
|
|
263
309
|
setCodexModels(models: string[]): void {
|
|
264
310
|
this.codexModels = new Set(
|
|
265
311
|
models
|
|
@@ -1867,25 +1913,26 @@ export class AccountRotator {
|
|
|
1867
1913
|
async getActiveAccount(
|
|
1868
1914
|
model?: string,
|
|
1869
1915
|
signal?: AbortSignal,
|
|
1916
|
+
providerId?: string,
|
|
1870
1917
|
): Promise<AccountRuntime | null> {
|
|
1871
1918
|
if (signal?.aborted) return null;
|
|
1872
|
-
const queueKey = this.getRequestQueueKey(model);
|
|
1919
|
+
const queueKey = this.getRequestQueueKey(model, providerId);
|
|
1873
1920
|
if (
|
|
1874
1921
|
this.hasQueuedRequest(queueKey) ||
|
|
1875
|
-
this.isConcurrencySaturated(model)
|
|
1922
|
+
this.isConcurrencySaturated(model, providerId)
|
|
1876
1923
|
) {
|
|
1877
|
-
return this.enqueueAccountRequest(model, signal, queueKey);
|
|
1924
|
+
return this.enqueueAccountRequest(model, signal, queueKey, providerId);
|
|
1878
1925
|
}
|
|
1879
1926
|
|
|
1880
|
-
const account = await this.tryGetActiveAccount(model);
|
|
1881
|
-
if (account || !this.isConcurrencySaturated(model)) return account;
|
|
1882
|
-
return this.enqueueAccountRequest(model, signal, queueKey);
|
|
1927
|
+
const account = await this.tryGetActiveAccount(model, providerId);
|
|
1928
|
+
if (account || !this.isConcurrencySaturated(model, providerId)) return account;
|
|
1929
|
+
return this.enqueueAccountRequest(model, signal, queueKey, providerId);
|
|
1883
1930
|
}
|
|
1884
1931
|
|
|
1885
1932
|
/** Keep fairness within a provider pool without making unrelated providers wait. */
|
|
1886
|
-
private getRequestQueueKey(model?: string): string {
|
|
1933
|
+
private getRequestQueueKey(model?: string, providerId?: string): string {
|
|
1887
1934
|
if (!model) return DEFAULT_PROVIDER;
|
|
1888
|
-
return getProviderIdForPoolKey(this.resolveRequestPoolKey(model));
|
|
1935
|
+
return providerId ?? getProviderIdForPoolKey(this.resolveRequestPoolKey(model, providerId));
|
|
1889
1936
|
}
|
|
1890
1937
|
|
|
1891
1938
|
private hasQueuedRequest(queueKey: string): boolean {
|
|
@@ -1896,6 +1943,7 @@ export class AccountRotator {
|
|
|
1896
1943
|
model?: string,
|
|
1897
1944
|
signal?: AbortSignal,
|
|
1898
1945
|
queueKey = this.getRequestQueueKey(model),
|
|
1946
|
+
providerId?: string,
|
|
1899
1947
|
): Promise<AccountRuntime | null> {
|
|
1900
1948
|
if (signal?.aborted) return Promise.resolve(null);
|
|
1901
1949
|
|
|
@@ -1903,6 +1951,7 @@ export class AccountRotator {
|
|
|
1903
1951
|
const waiter = {} as AccountRequestWaiter;
|
|
1904
1952
|
waiter.queueKey = queueKey;
|
|
1905
1953
|
waiter.model = model;
|
|
1954
|
+
waiter.providerId = providerId;
|
|
1906
1955
|
waiter.signal = signal;
|
|
1907
1956
|
waiter.resolve = resolve;
|
|
1908
1957
|
waiter.reject = reject;
|
|
@@ -2016,7 +2065,7 @@ export class AccountRotator {
|
|
|
2016
2065
|
|
|
2017
2066
|
let account: AccountRuntime | null;
|
|
2018
2067
|
try {
|
|
2019
|
-
account = await this.tryGetActiveAccount(waiter.model);
|
|
2068
|
+
account = await this.tryGetActiveAccount(waiter.model, waiter.providerId);
|
|
2020
2069
|
} catch (error) {
|
|
2021
2070
|
this.rejectAccountRequestWaiter(waiter, error);
|
|
2022
2071
|
progressed = true;
|
|
@@ -2028,7 +2077,7 @@ export class AccountRotator {
|
|
|
2028
2077
|
this.finishRequest(
|
|
2029
2078
|
account,
|
|
2030
2079
|
waiter.model
|
|
2031
|
-
? this.resolveRequestPoolKey(waiter.model)
|
|
2080
|
+
? this.resolveRequestPoolKey(waiter.model, waiter.providerId)
|
|
2032
2081
|
: undefined,
|
|
2033
2082
|
);
|
|
2034
2083
|
}
|
|
@@ -2044,9 +2093,9 @@ export class AccountRotator {
|
|
|
2044
2093
|
progressed = true;
|
|
2045
2094
|
continue;
|
|
2046
2095
|
}
|
|
2047
|
-
const wakeAt = this.getNextRequestAvailabilityAt(waiter.model);
|
|
2096
|
+
const wakeAt = this.getNextRequestAvailabilityAt(waiter.model, waiter.providerId);
|
|
2048
2097
|
if (wakeAt !== null) this.scheduleRequestWaiterWake(wakeAt);
|
|
2049
|
-
if (!this.isConcurrencySaturated(waiter.model) && wakeAt === null) {
|
|
2098
|
+
if (!this.isConcurrencySaturated(waiter.model, waiter.providerId) && wakeAt === null) {
|
|
2050
2099
|
this.settleAccountRequestWaiter(waiter, null);
|
|
2051
2100
|
progressed = true;
|
|
2052
2101
|
}
|
|
@@ -2058,10 +2107,10 @@ export class AccountRotator {
|
|
|
2058
2107
|
}
|
|
2059
2108
|
}
|
|
2060
2109
|
|
|
2061
|
-
private isConcurrencySaturated(model?: string): boolean {
|
|
2110
|
+
private isConcurrencySaturated(model?: string, providerId?: string): boolean {
|
|
2062
2111
|
const now = Date.now();
|
|
2063
2112
|
if (this.isProtectivePauseActive(now)) return false;
|
|
2064
|
-
const modelKey = model ? this.resolveRequestPoolKey(model) : null;
|
|
2113
|
+
const modelKey = model ? this.resolveRequestPoolKey(model, providerId) : null;
|
|
2065
2114
|
if (!modelKey) return false;
|
|
2066
2115
|
const policy = this.config.routingPolicy || "timer-first";
|
|
2067
2116
|
|
|
@@ -2097,12 +2146,12 @@ export class AccountRotator {
|
|
|
2097
2146
|
|
|
2098
2147
|
// Try to reserve an account immediately. The public method queues only
|
|
2099
2148
|
// when every otherwise-eligible account is blocked by concurrency.
|
|
2100
|
-
private async tryGetActiveAccount(model?: string): Promise<AccountRuntime | null> {
|
|
2149
|
+
private async tryGetActiveAccount(model?: string, providerId?: string): Promise<AccountRuntime | null> {
|
|
2101
2150
|
const now = Date.now();
|
|
2102
2151
|
if (this.accounts.length === 0) return null;
|
|
2103
2152
|
if (this.isProtectivePauseActive(now)) return null;
|
|
2104
2153
|
|
|
2105
|
-
const modelKey = model ? this.resolveRequestPoolKey(model) : null;
|
|
2154
|
+
const modelKey = model ? this.resolveRequestPoolKey(model, providerId) : null;
|
|
2106
2155
|
const state = modelKey ? this.modelState.get(modelKey) : null;
|
|
2107
2156
|
const idx = state?.activeAccountIndex ?? this.defaultIndex;
|
|
2108
2157
|
const current = this.accounts[idx];
|
|
@@ -2518,9 +2567,10 @@ export class AccountRotator {
|
|
|
2518
2567
|
async rotateToNext(
|
|
2519
2568
|
model?: string,
|
|
2520
2569
|
failedAccount?: AccountRuntime | number | string,
|
|
2570
|
+
providerId?: string,
|
|
2521
2571
|
): Promise<AccountRuntime | null> {
|
|
2522
2572
|
if (this.isProtectivePauseActive(Date.now())) return null;
|
|
2523
|
-
const modelKey = model ? this.resolveRequestPoolKey(model) : null;
|
|
2573
|
+
const modelKey = model ? this.resolveRequestPoolKey(model, providerId) : null;
|
|
2524
2574
|
let excludeIdx: number;
|
|
2525
2575
|
if (typeof failedAccount === "number") {
|
|
2526
2576
|
excludeIdx = failedAccount;
|
|
@@ -2543,14 +2593,14 @@ export class AccountRotator {
|
|
|
2543
2593
|
}
|
|
2544
2594
|
|
|
2545
2595
|
// Record a successful request. Returns true if rotation is needed.
|
|
2546
|
-
recordRequest(account: AccountRuntime, model?: string): boolean {
|
|
2596
|
+
recordRequest(account: AccountRuntime, model?: string, providerId?: string): boolean {
|
|
2547
2597
|
account.requestsSinceRotation++;
|
|
2548
2598
|
account.totalRequests++;
|
|
2549
2599
|
account.lastUsed = Date.now();
|
|
2550
2600
|
account.consecutiveErrors = 0;
|
|
2551
2601
|
account.lastError = null;
|
|
2552
2602
|
|
|
2553
|
-
const modelKey = model ? this.resolveRequestPoolKey(model) : null;
|
|
2603
|
+
const modelKey = model ? this.resolveRequestPoolKey(model, providerId) : null;
|
|
2554
2604
|
const state = modelKey ? this.modelState.get(modelKey) : null;
|
|
2555
2605
|
const shouldRotate =
|
|
2556
2606
|
!!modelKey &&
|
|
@@ -3733,8 +3783,8 @@ export class AccountRotator {
|
|
|
3733
3783
|
}
|
|
3734
3784
|
|
|
3735
3785
|
/** Public pool-key resolution for quota routing display/logging. */
|
|
3736
|
-
resolveQuotaModelKeyForDisplay(model: string): string {
|
|
3737
|
-
return this.resolveRequestPoolKey(model);
|
|
3786
|
+
resolveQuotaModelKeyForDisplay(model: string, providerId?: string): string {
|
|
3787
|
+
return this.resolveRequestPoolKey(model, providerId);
|
|
3738
3788
|
}
|
|
3739
3789
|
|
|
3740
3790
|
/** Preserve the exact identity of models learned from the runtime catalog. */
|
|
@@ -3742,11 +3792,11 @@ export class AccountRotator {
|
|
|
3742
3792
|
return dynamicCatalog.getObservedModelId(model) ?? resolveDisplayModelKey(model);
|
|
3743
3793
|
}
|
|
3744
3794
|
|
|
3745
|
-
private resolveRequestPoolKey(model: string): string {
|
|
3746
|
-
return this.resolvePoolKeyForModel(model) ?? "__default__";
|
|
3795
|
+
private resolveRequestPoolKey(model: string, providerId?: string): string {
|
|
3796
|
+
return this.resolvePoolKeyForModel(model, providerId) ?? "__default__";
|
|
3747
3797
|
}
|
|
3748
3798
|
|
|
3749
|
-
private resolvePoolKeyForModel(model: string): string | null {
|
|
3799
|
+
private resolvePoolKeyForModel(model: string, providerId?: string): string | null {
|
|
3750
3800
|
const normalizedModel = model.trim().toLowerCase();
|
|
3751
3801
|
if (
|
|
3752
3802
|
!isStaticAntigravityModel(model) &&
|
|
@@ -3758,6 +3808,7 @@ export class AccountRotator {
|
|
|
3758
3808
|
const context = {
|
|
3759
3809
|
ollamaModels: this.ollamaModels,
|
|
3760
3810
|
codexModels: this.codexModels,
|
|
3811
|
+
providerId,
|
|
3761
3812
|
};
|
|
3762
3813
|
const adapter = findProviderForModel(model, context);
|
|
3763
3814
|
if (adapter?.getPoolKey) {
|
|
@@ -3839,12 +3890,13 @@ export class AccountRotator {
|
|
|
3839
3890
|
private getRequestAvailabilityTimes(
|
|
3840
3891
|
model: string | undefined,
|
|
3841
3892
|
now: number,
|
|
3893
|
+
providerId?: string,
|
|
3842
3894
|
): number[] {
|
|
3843
3895
|
const retryTimes: number[] = [];
|
|
3844
3896
|
if (this.protectivePauseUntil > now)
|
|
3845
3897
|
retryTimes.push(this.protectivePauseUntil);
|
|
3846
3898
|
const modelKey = model
|
|
3847
|
-
? (this.resolvePoolKeyForModel(model) ?? resolveQuotaModelKey(model) ?? "__default__")
|
|
3899
|
+
? (this.resolvePoolKeyForModel(model, providerId) ?? resolveQuotaModelKey(model) ?? "__default__")
|
|
3848
3900
|
: "__default__";
|
|
3849
3901
|
const quotaStateKey = this.resolveQuotaStateKey(modelKey);
|
|
3850
3902
|
const dailyResetAt = nextUtcDayStartMs(now);
|
|
@@ -3852,14 +3904,14 @@ export class AccountRotator {
|
|
|
3852
3904
|
if (modelBreaker > now) retryTimes.push(modelBreaker);
|
|
3853
3905
|
for (const account of this.accounts) {
|
|
3854
3906
|
if (account.disabled || account.flagged) continue;
|
|
3855
|
-
const
|
|
3907
|
+
const candidateProviderId = providerId ?? getProviderIdForPoolKey(modelKey);
|
|
3856
3908
|
if (
|
|
3857
|
-
!hasCredential(account.config,
|
|
3858
|
-
account.invalidProviders?.[
|
|
3909
|
+
!hasCredential(account.config, candidateProviderId) ||
|
|
3910
|
+
account.invalidProviders?.[candidateProviderId]
|
|
3859
3911
|
) {
|
|
3860
3912
|
continue;
|
|
3861
3913
|
}
|
|
3862
|
-
const providerCooldown = account.providerCooldowns?.[
|
|
3914
|
+
const providerCooldown = account.providerCooldowns?.[candidateProviderId] ?? 0;
|
|
3863
3915
|
if (providerCooldown > now) retryTimes.push(providerCooldown);
|
|
3864
3916
|
if (this.isDailySafetyStopped(account, now))
|
|
3865
3917
|
retryTimes.push(dailyResetAt);
|
|
@@ -3888,15 +3940,15 @@ export class AccountRotator {
|
|
|
3888
3940
|
return retryTimes;
|
|
3889
3941
|
}
|
|
3890
3942
|
|
|
3891
|
-
private getNextRequestAvailabilityAt(model?: string): number | null {
|
|
3943
|
+
private getNextRequestAvailabilityAt(model?: string, providerId?: string): number | null {
|
|
3892
3944
|
const now = Date.now();
|
|
3893
|
-
const retryTimes = this.getRequestAvailabilityTimes(model, now);
|
|
3945
|
+
const retryTimes = this.getRequestAvailabilityTimes(model, now, providerId);
|
|
3894
3946
|
return retryTimes.length > 0 ? Math.min(...retryTimes) : null;
|
|
3895
3947
|
}
|
|
3896
3948
|
|
|
3897
|
-
getRetryAfterMs(model?: string): number {
|
|
3949
|
+
getRetryAfterMs(model?: string, providerId?: string): number {
|
|
3898
3950
|
const now = Date.now();
|
|
3899
|
-
const retryTimes = this.getRequestAvailabilityTimes(model, now);
|
|
3951
|
+
const retryTimes = this.getRequestAvailabilityTimes(model, now, providerId);
|
|
3900
3952
|
if (retryTimes.length === 0) return 0;
|
|
3901
3953
|
return Math.max(1000, Math.min(...retryTimes) - now);
|
|
3902
3954
|
}
|
|
@@ -4070,8 +4122,31 @@ export class AccountRotator {
|
|
|
4070
4122
|
return applyConfigDefaults(structuredClone(this.config));
|
|
4071
4123
|
}
|
|
4072
4124
|
|
|
4125
|
+
getPublicConfig(): Config {
|
|
4126
|
+
return redactTypeSafeRoutingInConfig(this.getConfig());
|
|
4127
|
+
}
|
|
4128
|
+
|
|
4073
4129
|
async replaceConfig(nextConfig: Config): Promise<void> {
|
|
4074
4130
|
const normalized = applyConfigDefaults(nextConfig);
|
|
4131
|
+
const incomingTypeSafe = normalized.typesafeRouting;
|
|
4132
|
+
const existingTypeSafe = this.config.typesafeRouting;
|
|
4133
|
+
if (existingTypeSafe && incomingTypeSafe) {
|
|
4134
|
+
const incomingKey = incomingTypeSafe.apiKey;
|
|
4135
|
+
normalized.typesafeRouting = {
|
|
4136
|
+
...existingTypeSafe,
|
|
4137
|
+
...incomingTypeSafe,
|
|
4138
|
+
apiKey:
|
|
4139
|
+
!incomingKey || isRedactedSecret(incomingKey)
|
|
4140
|
+
? existingTypeSafe.apiKey
|
|
4141
|
+
: incomingKey,
|
|
4142
|
+
};
|
|
4143
|
+
} else if (existingTypeSafe && !incomingTypeSafe) {
|
|
4144
|
+
// Public dashboard exports omit the secret; preserve it on unrelated
|
|
4145
|
+
// config edits instead of disabling Jev unexpectedly.
|
|
4146
|
+
normalized.typesafeRouting = existingTypeSafe;
|
|
4147
|
+
} else if (incomingTypeSafe && isRedactedSecret(incomingTypeSafe.apiKey)) {
|
|
4148
|
+
normalized.typesafeRouting = { ...incomingTypeSafe, apiKey: undefined };
|
|
4149
|
+
}
|
|
4075
4150
|
const unmatchedExisting = [...this.accounts];
|
|
4076
4151
|
const matchAndReuseAccount = (config: AccountConfig): AccountRuntime => {
|
|
4077
4152
|
const targetId = getAccountIdentity(config);
|
package/src/static/dashboard.js
CHANGED
|
@@ -1088,6 +1088,9 @@ var TOKEN_MODEL_COLORS = {
|
|
|
1088
1088
|
"gemini-3-flash": "#93c5fd", // Gemini 3 Flash (Azul pastel claro)
|
|
1089
1089
|
|
|
1090
1090
|
// Codex Pool (Amarillos) — de más caro a más barato
|
|
1091
|
+
"gpt-6-astra": "#713f12", // GPT-6 Astra (ámbar oscuro)
|
|
1092
|
+
"gpt-6-sol": "#a16207", // GPT-6 Sol
|
|
1093
|
+
"gpt-6-luna": "#fde047", // GPT-6 Luna
|
|
1091
1094
|
"gpt-5.6-sol": "#a16207", // GPT-5.6 Sol (Amarillo oscuro/marrón mostaza)
|
|
1092
1095
|
"gpt-5.6-terra": "#eab308", // GPT-5.6 Terra (Amarillo vivo)
|
|
1093
1096
|
"gpt-5.6-luna": "#fde047", // GPT-5.6 Luna (Amarillo pastel suave)
|
|
@@ -1133,6 +1136,9 @@ function getModelColor(model) {
|
|
|
1133
1136
|
if (lower.indexOf("gemini") !== -1 || lower.indexOf("3-flash") !== -1) return "#93c5fd";
|
|
1134
1137
|
|
|
1135
1138
|
// Codex pool (Amarillos)
|
|
1139
|
+
if (lower.indexOf("gpt-6-astra") !== -1) return "#713f12";
|
|
1140
|
+
if (lower.indexOf("gpt-6-sol") !== -1) return "#a16207";
|
|
1141
|
+
if (lower.indexOf("gpt-6-luna") !== -1) return "#fde047";
|
|
1136
1142
|
if (lower.indexOf("gpt-5.6-sol") !== -1) return "#a16207";
|
|
1137
1143
|
if (lower.indexOf("gpt-5.6-terra") !== -1) return "#eab308";
|
|
1138
1144
|
if (lower.indexOf("gpt-5.6-luna") !== -1) return "#fde047";
|
|
@@ -1182,7 +1188,10 @@ var MODEL_PRICING_CLIENT = {
|
|
|
1182
1188
|
"gemini-3.8-flash-medium": { input: 0.75, output: 3.75 },
|
|
1183
1189
|
"gemini-3.8-flash-high": { input: 0.75, output: 3.75 },
|
|
1184
1190
|
"gpt-oss-120b-medium": { input: 2.0, output: 10.0 },
|
|
1185
|
-
// OpenAI Codex
|
|
1191
|
+
// OpenAI Codex pricing — mirrors MODEL_PRICING in types.ts.
|
|
1192
|
+
"gpt-6-astra": { input: 10.0, output: 50.0 },
|
|
1193
|
+
"gpt-6-sol": { input: 2.0, output: 10.0 },
|
|
1194
|
+
"gpt-6-luna": { input: 0.1, output: 0.5 },
|
|
1186
1195
|
"gpt-5.6-sol": { input: 5.0, output: 30.0 },
|
|
1187
1196
|
"gpt-5.6-terra": { input: 2.0, output: 12.0 },
|
|
1188
1197
|
"gpt-5.6-luna": { input: 0.2, output: 1.2 },
|