tuxevil-rotator 3.3.2 → 3.5.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 +19 -0
- package/README.md +7 -2
- package/package.json +1 -1
- package/src/account-identity.ts +70 -0
- package/src/compat/model-specs.ts +28 -13
- package/src/compat.ts +60 -26
- package/src/providers/google-antigravity/catalog.ts +12 -9
- package/src/providers/google-antigravity/dynamic-catalog.ts +252 -0
- package/src/providers/google-antigravity/quota.ts +21 -0
- package/src/providers/google-antigravity/translators.ts +21 -7
- package/src/providers/ollama/prediction.ts +1 -1
- package/src/proxy.ts +17 -2
- package/src/rotator.ts +41 -73
- package/src/spend-logger.ts +3 -21
- package/src/static/dashboard-keys.js +4 -3
- package/src/static/dashboard-logs.js +5 -2
- package/src/static/dashboard.js +14 -13
- package/src/types.ts +100 -67
- package/tools/telemetry-receiver/receiver.js +5 -5
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
// Dynamic Google Antigravity model availability discovered during quota polling.
|
|
2
|
+
|
|
3
|
+
import type { ModelSpec } from "../../compat/model-specs.js";
|
|
4
|
+
import type { GoogleQuotaResponse } from "../../types.js";
|
|
5
|
+
|
|
6
|
+
export interface DynamicModelEntry {
|
|
7
|
+
id: string;
|
|
8
|
+
family: string;
|
|
9
|
+
ctx: number;
|
|
10
|
+
quotaPool: string;
|
|
11
|
+
multimodal: boolean;
|
|
12
|
+
tools: boolean;
|
|
13
|
+
maxOutputTokens: number;
|
|
14
|
+
thinkingBudget: number;
|
|
15
|
+
isThinking: boolean;
|
|
16
|
+
isTiered: boolean;
|
|
17
|
+
displayName?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const DEFAULT_ACCOUNT_KEY = "__default__";
|
|
21
|
+
|
|
22
|
+
function accountKey(value: string): string {
|
|
23
|
+
return value.trim();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function familyDefaults(
|
|
27
|
+
family: string,
|
|
28
|
+
): Pick<
|
|
29
|
+
DynamicModelEntry,
|
|
30
|
+
"ctx" | "multimodal" | "tools" | "maxOutputTokens" | "thinkingBudget" | "isThinking"
|
|
31
|
+
> {
|
|
32
|
+
if (family === "gpt-oss") {
|
|
33
|
+
return {
|
|
34
|
+
ctx: 131_072,
|
|
35
|
+
multimodal: false,
|
|
36
|
+
tools: true,
|
|
37
|
+
maxOutputTokens: 32_768,
|
|
38
|
+
thinkingBudget: 8_192,
|
|
39
|
+
isThinking: true,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
if (family.includes("claude")) {
|
|
43
|
+
return {
|
|
44
|
+
ctx: 1_000_000,
|
|
45
|
+
multimodal: true,
|
|
46
|
+
tools: true,
|
|
47
|
+
maxOutputTokens: 64_000,
|
|
48
|
+
thinkingBudget: 32_768,
|
|
49
|
+
isThinking: true,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
if (family.includes("gemini")) {
|
|
53
|
+
return {
|
|
54
|
+
ctx: 1_000_000,
|
|
55
|
+
multimodal: true,
|
|
56
|
+
tools: true,
|
|
57
|
+
maxOutputTokens: 65_536,
|
|
58
|
+
thinkingBudget: 24_576,
|
|
59
|
+
isThinking: true,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
ctx: 128_000,
|
|
64
|
+
multimodal: false,
|
|
65
|
+
tools: true,
|
|
66
|
+
maxOutputTokens: 65_536,
|
|
67
|
+
thinkingBudget: 24_576,
|
|
68
|
+
isThinking: false,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export class DynamicModelRegistry {
|
|
73
|
+
private static instance: DynamicModelRegistry | null = null;
|
|
74
|
+
private accountModels = new Map<string, Map<string, DynamicModelEntry>>();
|
|
75
|
+
private defaultAgentModelIds = new Map<string, string>();
|
|
76
|
+
private discoveredModelIds = new Set<string>();
|
|
77
|
+
|
|
78
|
+
static getInstance(): DynamicModelRegistry {
|
|
79
|
+
DynamicModelRegistry.instance ??= new DynamicModelRegistry();
|
|
80
|
+
return DynamicModelRegistry.instance;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Reset dynamic account state (primarily for tests). */
|
|
84
|
+
reset(): void {
|
|
85
|
+
this.accountModels.clear();
|
|
86
|
+
this.defaultAgentModelIds.clear();
|
|
87
|
+
this.discoveredModelIds.clear();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Derive the model family and quota pool from a model ID string. */
|
|
91
|
+
static inferFamilyAndPool(modelId: string): { family: string; quotaPool: string } {
|
|
92
|
+
const lower = modelId.toLowerCase();
|
|
93
|
+
if (lower.includes("claude") || lower.includes("sonnet") || lower.includes("opus")) {
|
|
94
|
+
const match = lower.match(/(claude-[a-z0-9.-]+)/);
|
|
95
|
+
return { family: match?.[1] ?? "claude", quotaPool: "claude" };
|
|
96
|
+
}
|
|
97
|
+
if (lower.includes("gpt-oss")) {
|
|
98
|
+
return { family: "gpt-oss", quotaPool: "claude" };
|
|
99
|
+
}
|
|
100
|
+
if (lower.includes("gemini")) {
|
|
101
|
+
const match = lower.match(/(gemini-[0-9.]+-?[a-z]*)/);
|
|
102
|
+
return { family: match?.[1] ?? "gemini", quotaPool: "gemini" };
|
|
103
|
+
}
|
|
104
|
+
return { family: lower, quotaPool: "gemini" };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Replace one account's advertised models with its latest successful response. */
|
|
108
|
+
updateFromEndpointResponse(
|
|
109
|
+
data: GoogleQuotaResponse,
|
|
110
|
+
accountId = DEFAULT_ACCOUNT_KEY,
|
|
111
|
+
): number {
|
|
112
|
+
if (!data || typeof data !== "object" || !data.models) return 0;
|
|
113
|
+
|
|
114
|
+
const key = accountKey(accountId);
|
|
115
|
+
const previous = this.accountModels.get(key);
|
|
116
|
+
const knownBefore = new Set(this.getAllModels().map((model) => model.id.toLowerCase()));
|
|
117
|
+
const next = new Map<string, DynamicModelEntry>();
|
|
118
|
+
const tieredIds = new Set(
|
|
119
|
+
Object.values(data.tieredModelIds ?? {}).flat().map((id) => id.trim().toLowerCase()),
|
|
120
|
+
);
|
|
121
|
+
let newModelsCount = 0;
|
|
122
|
+
|
|
123
|
+
if (data.defaultAgentModelId) {
|
|
124
|
+
this.defaultAgentModelIds.delete(key);
|
|
125
|
+
this.defaultAgentModelIds.set(key, data.defaultAgentModelId);
|
|
126
|
+
} else {
|
|
127
|
+
this.defaultAgentModelIds.delete(key);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
for (const [rawId, info] of Object.entries(data.models)) {
|
|
131
|
+
const normalizedId = rawId.trim();
|
|
132
|
+
const lowerId = normalizedId.toLowerCase();
|
|
133
|
+
if (
|
|
134
|
+
!lowerId ||
|
|
135
|
+
lowerId.startsWith("chat_") ||
|
|
136
|
+
lowerId.startsWith("tab_") ||
|
|
137
|
+
lowerId.startsWith("gemini-3.5-")
|
|
138
|
+
) continue;
|
|
139
|
+
|
|
140
|
+
this.discoveredModelIds.add(lowerId);
|
|
141
|
+
const { family, quotaPool } = DynamicModelRegistry.inferFamilyAndPool(normalizedId);
|
|
142
|
+
const defaults = previous?.get(lowerId) ?? familyDefaults(family);
|
|
143
|
+
const isTiered = lowerId.includes("-tiered") || tieredIds.has(lowerId);
|
|
144
|
+
const hasThinkingBudget = typeof info.thinkingBudget === "number";
|
|
145
|
+
const thinkingBudget = hasThinkingBudget
|
|
146
|
+
? info.thinkingBudget!
|
|
147
|
+
: isTiered
|
|
148
|
+
? -1
|
|
149
|
+
: defaults.thinkingBudget;
|
|
150
|
+
const isThinking = typeof info.supportsThinking === "boolean"
|
|
151
|
+
? info.supportsThinking
|
|
152
|
+
: hasThinkingBudget
|
|
153
|
+
? thinkingBudget !== 0
|
|
154
|
+
: defaults.isThinking;
|
|
155
|
+
const hasMultimodalMetadata =
|
|
156
|
+
typeof info.supportsImages === "boolean" || typeof info.supportsVideo === "boolean";
|
|
157
|
+
|
|
158
|
+
next.set(lowerId, {
|
|
159
|
+
id: normalizedId,
|
|
160
|
+
family,
|
|
161
|
+
ctx: typeof info.maxTokens === "number" && info.maxTokens > 0
|
|
162
|
+
? info.maxTokens
|
|
163
|
+
: defaults.ctx,
|
|
164
|
+
quotaPool,
|
|
165
|
+
multimodal: hasMultimodalMetadata
|
|
166
|
+
? Boolean(info.supportsImages || info.supportsVideo)
|
|
167
|
+
: defaults.multimodal,
|
|
168
|
+
tools: defaults.tools,
|
|
169
|
+
maxOutputTokens:
|
|
170
|
+
typeof info.maxOutputTokens === "number" && info.maxOutputTokens > 0
|
|
171
|
+
? info.maxOutputTokens
|
|
172
|
+
: defaults.maxOutputTokens,
|
|
173
|
+
thinkingBudget,
|
|
174
|
+
isThinking,
|
|
175
|
+
isTiered,
|
|
176
|
+
displayName: info.displayName ?? previous?.get(lowerId)?.displayName ?? normalizedId,
|
|
177
|
+
});
|
|
178
|
+
if (!knownBefore.has(lowerId)) newModelsCount++;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
this.accountModels.set(key, next);
|
|
182
|
+
return newModelsCount;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Expire catalogs belonging to accounts that are no longer active. */
|
|
186
|
+
retainAccounts(accountIds: Iterable<string>): void {
|
|
187
|
+
const active = new Set(Array.from(accountIds, accountKey));
|
|
188
|
+
for (const key of this.accountModels.keys()) {
|
|
189
|
+
if (!active.has(key)) this.accountModels.delete(key);
|
|
190
|
+
}
|
|
191
|
+
for (const key of this.defaultAgentModelIds.keys()) {
|
|
192
|
+
if (!active.has(key)) this.defaultAgentModelIds.delete(key);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
getAllModels(): DynamicModelEntry[] {
|
|
197
|
+
const models = new Map<string, DynamicModelEntry>();
|
|
198
|
+
for (const accountCatalog of this.accountModels.values()) {
|
|
199
|
+
for (const [id, model] of accountCatalog) models.set(id, model);
|
|
200
|
+
}
|
|
201
|
+
return Array.from(models.values());
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
getModel(id: string): DynamicModelEntry | undefined {
|
|
205
|
+
let match: DynamicModelEntry | undefined;
|
|
206
|
+
const lowerId = id.toLowerCase();
|
|
207
|
+
for (const accountCatalog of this.accountModels.values()) {
|
|
208
|
+
match = accountCatalog.get(lowerId) ?? match;
|
|
209
|
+
}
|
|
210
|
+
return match;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
hasModelForAccount(accountId: string, id: string): boolean {
|
|
214
|
+
return this.accountModels
|
|
215
|
+
.get(accountKey(accountId))
|
|
216
|
+
?.has(id.trim().toLowerCase()) ?? false;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
wasDiscovered(id: string): boolean {
|
|
220
|
+
return this.discoveredModelIds.has(id.trim().toLowerCase());
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
getModelSpec(id: string): ModelSpec | undefined {
|
|
224
|
+
const entry = this.getModel(id);
|
|
225
|
+
if (!entry) return undefined;
|
|
226
|
+
return {
|
|
227
|
+
maxOutputTokens: entry.maxOutputTokens,
|
|
228
|
+
thinkingBudget: entry.thinkingBudget,
|
|
229
|
+
isThinking: entry.isThinking,
|
|
230
|
+
contextWindow: entry.ctx,
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
getContextWindow(id: string): number | undefined {
|
|
235
|
+
return this.getModel(id)?.ctx;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
isTiered(id: string): boolean {
|
|
239
|
+
const lowerId = id.toLowerCase();
|
|
240
|
+
return lowerId.includes("-tiered") || (this.getModel(lowerId)?.isTiered ?? false);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
resolveQuotaPool(id: string): string | undefined {
|
|
244
|
+
return this.getModel(id)?.quotaPool;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
getDefaultAgentModelId(): string | null {
|
|
248
|
+
return Array.from(this.defaultAgentModelIds.values()).at(-1) ?? null;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export const dynamicCatalog = DynamicModelRegistry.getInstance();
|
|
@@ -12,11 +12,14 @@ import {
|
|
|
12
12
|
QUOTA_MODEL_KEYS,
|
|
13
13
|
} from "../../types.js";
|
|
14
14
|
import { fetchWithRetry } from "../../fetch-with-retry.js";
|
|
15
|
+
import { getAccountIdentity } from "../../account-identity.js";
|
|
15
16
|
import type { QuotaFetchContext } from "../adapter.js";
|
|
16
17
|
import { DEFAULT_PROVIDER, getProviderProjectId } from "../credential-helpers.js";
|
|
17
18
|
import { getAccountProxyDispatcher } from "../proxy-dispatcher.js";
|
|
18
19
|
import { sortQuotaPools } from "../registry.js";
|
|
19
20
|
|
|
21
|
+
import { dynamicCatalog, DynamicModelRegistry } from "./dynamic-catalog.js";
|
|
22
|
+
|
|
20
23
|
/**
|
|
21
24
|
* Extract per-model quotas from a Google quota response, preserving the
|
|
22
25
|
* previously classified timer type when the reset time is unchanged.
|
|
@@ -38,6 +41,15 @@ export function extractQuotas(
|
|
|
38
41
|
}
|
|
39
42
|
}
|
|
40
43
|
|
|
44
|
+
if (!modelInfo) {
|
|
45
|
+
for (const [modelKey, info] of Object.entries(data.models)) {
|
|
46
|
+
if (DynamicModelRegistry.inferFamilyAndPool(modelKey).quotaPool === config.key) {
|
|
47
|
+
modelInfo = info;
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
41
53
|
if (modelInfo?.quotaInfo) {
|
|
42
54
|
const remainingFraction = modelInfo.quotaInfo.remainingFraction ?? 0;
|
|
43
55
|
// Google can publish a nominal reset for an untouched pool; no usage means
|
|
@@ -119,6 +131,15 @@ export async function fetchProviderQuota(
|
|
|
119
131
|
}
|
|
120
132
|
|
|
121
133
|
const data = (await response.json()) as GoogleQuotaResponse;
|
|
134
|
+
const newModels = dynamicCatalog.updateFromEndpointResponse(
|
|
135
|
+
data,
|
|
136
|
+
getAccountIdentity(account),
|
|
137
|
+
);
|
|
138
|
+
if (newModels > 0) {
|
|
139
|
+
ctx.log(
|
|
140
|
+
`${account.config.email}: discovered ${newModels} Antigravity model(s) from quota response`,
|
|
141
|
+
);
|
|
142
|
+
}
|
|
122
143
|
const oldQuota = account.quota || [];
|
|
123
144
|
const fresh = extractQuotas(data, oldQuota);
|
|
124
145
|
// Drop the previous Antigravity entries so the new ones fully replace
|
|
@@ -841,20 +841,35 @@ export function convertResponsesToChatRequest(
|
|
|
841
841
|
* reasoning_effort. Matched case-insensitively against the exact ID only;
|
|
842
842
|
* virtual siblings (e.g. `gemini-3.7-flash-tiered-high`) are excluded.
|
|
843
843
|
*/
|
|
844
|
+
import { dynamicCatalog } from "./dynamic-catalog.js";
|
|
845
|
+
|
|
844
846
|
export const TIERED_EFFORT_MODEL_ID = "gemini-3.7-flash-tiered";
|
|
847
|
+
export const TIERED_EFFORT_MODEL_IDS = new Set([
|
|
848
|
+
"gemini-3.7-flash-tiered",
|
|
849
|
+
]);
|
|
850
|
+
|
|
851
|
+
export function isTieredEffortModel(modelId: string): boolean {
|
|
852
|
+
const l = modelId.toLowerCase().trim();
|
|
853
|
+
if (l.includes("3.6")) return false;
|
|
854
|
+
if (TIERED_EFFORT_MODEL_IDS.has(l)) return true;
|
|
855
|
+
// Exclude virtual siblings like gemini-X-tiered-high
|
|
856
|
+
if (l.includes("-tiered-")) return false;
|
|
857
|
+
if (l.endsWith("-tiered")) return true;
|
|
858
|
+
return dynamicCatalog.isTiered(l);
|
|
859
|
+
}
|
|
845
860
|
|
|
846
861
|
/**
|
|
847
862
|
* Map an OpenAI Chat `reasoning_effort` to a Gemini `thinkingLevel` for
|
|
848
|
-
* `gemini-3.7-flash-tiered` only.
|
|
849
|
-
* (case-insensitive); anything else returns
|
|
850
|
-
* back to the existing adaptive/fixed-budget
|
|
851
|
-
* invalid upstream enum.
|
|
863
|
+
* tiered models (`gemini-3.7-flash-tiered`) only.
|
|
864
|
+
* Accepts exactly low/medium/high (case-insensitive); anything else returns
|
|
865
|
+
* undefined so the caller falls back to the existing adaptive/fixed-budget
|
|
866
|
+
* semantics and never emits an invalid upstream enum.
|
|
852
867
|
*/
|
|
853
868
|
export function mapTieredReasoningEffortToThinkingLevel(
|
|
854
869
|
effort: string | undefined,
|
|
855
870
|
modelId: string,
|
|
856
871
|
): "LOW" | "MEDIUM" | "HIGH" | undefined {
|
|
857
|
-
if (modelId
|
|
872
|
+
if (!isTieredEffortModel(modelId)) return undefined;
|
|
858
873
|
if (!isNonEmptyString(effort)) return undefined;
|
|
859
874
|
switch (effort.toLowerCase()) {
|
|
860
875
|
case "low":
|
|
@@ -1418,8 +1433,7 @@ export function mapReasoningEffortToThinkingLevel(
|
|
|
1418
1433
|
const lowerModel = modelId.toLowerCase();
|
|
1419
1434
|
const isGemini31Pro = /gemini-3\.1-pro/i.test(modelId);
|
|
1420
1435
|
const isGemini3Flash =
|
|
1421
|
-
lowerModel.includes("gemini-3-flash")
|
|
1422
|
-
lowerModel.includes("gemini-3.5-flash");
|
|
1436
|
+
lowerModel.includes("gemini-3-flash");
|
|
1423
1437
|
|
|
1424
1438
|
let effectiveEffort = effort;
|
|
1425
1439
|
if (!effectiveEffort) {
|
|
@@ -21,13 +21,13 @@ export interface PoolBudgetEntry {
|
|
|
21
21
|
// Measured on 2026-08-09 with controlled burns (see docs/usage-calibration.md).
|
|
22
22
|
// Two gpt-oss:20b runs on different accounts agreed within ~1%, so these are
|
|
23
23
|
// per-account, per-model budgets, not per-IP.
|
|
24
|
+
// minimax-m3 removed 2026-08-30: moved to subscription tier (HTTP 402 on free).
|
|
24
25
|
export const CALIBRATED_MODEL_BUDGETS: Readonly<Record<string, PoolBudgetEntry>> = {
|
|
25
26
|
"gpt-oss:20b": { sessionTokens: 1_560_000, weeklyTokens: 4_210_000 },
|
|
26
27
|
"gemma4:31b": { sessionTokens: 1_150_000, weeklyTokens: 3_110_000 },
|
|
27
28
|
"nemotron-3-nano:30b": { sessionTokens: 1_930_000, weeklyTokens: 5_190_000 },
|
|
28
29
|
"gpt-oss:120b": { sessionTokens: 760_000, weeklyTokens: 2_050_000 },
|
|
29
30
|
"nemotron-3-super": { sessionTokens: 740_000, weeklyTokens: 2_010_000 },
|
|
30
|
-
"minimax-m3": { sessionTokens: 190_000, weeklyTokens: 520_000 },
|
|
31
31
|
"nemotron-3-ultra": { sessionTokens: 120_000, weeklyTokens: 330_000 },
|
|
32
32
|
};
|
|
33
33
|
|
package/src/proxy.ts
CHANGED
|
@@ -1280,7 +1280,22 @@ export async function withRotation<T>(
|
|
|
1280
1280
|
// success
|
|
1281
1281
|
const result = await onSuccess(response, context);
|
|
1282
1282
|
const shouldRotate = rotator.recordRequest(account, model);
|
|
1283
|
-
|
|
1283
|
+
const inTokens =
|
|
1284
|
+
result && typeof result === "object" && "inputTokens" in result
|
|
1285
|
+
? (result as Record<string, unknown>).inputTokens
|
|
1286
|
+
: 0;
|
|
1287
|
+
const outTokens =
|
|
1288
|
+
result && typeof result === "object" && "outputTokens" in result
|
|
1289
|
+
? (result as Record<string, unknown>).outputTokens
|
|
1290
|
+
: 0;
|
|
1291
|
+
const ttfbMs =
|
|
1292
|
+
result && typeof result === "object" && "firstByteMs" in result
|
|
1293
|
+
? (result as Record<string, unknown>).firstByteMs
|
|
1294
|
+
: undefined;
|
|
1295
|
+
const ttfbInfo = ttfbMs !== undefined ? ` ttfbMs=${ttfbMs}` : "";
|
|
1296
|
+
const tokensInfo =
|
|
1297
|
+
inTokens || outTokens ? ` inTokens=${inTokens} outTokens=${outTokens}` : "";
|
|
1298
|
+
logRequestEnd(response.status, `endpoint=${endpoint}${ttfbInfo}${tokensInfo}`);
|
|
1284
1299
|
if (shouldRotate) {
|
|
1285
1300
|
await rotator.rotateToNext(model, account);
|
|
1286
1301
|
}
|
|
@@ -1750,7 +1765,7 @@ async function handleProxyRequest(
|
|
|
1750
1765
|
const ttfbMs = usage?.firstByteMs ?? totalMs;
|
|
1751
1766
|
const outcomeStatus = usage?.streamError ? 502 : response.status;
|
|
1752
1767
|
rotator.recordLatency(body.displayModel || body.model, ttfbMs, totalMs);
|
|
1753
|
-
logRequestEnd(outcomeStatus, `ttfbMs=${ttfbMs} endpoint=${endpoint}`);
|
|
1768
|
+
logRequestEnd(outcomeStatus, `ttfbMs=${ttfbMs} inTokens=${usage?.inputTokens ?? 0} outTokens=${usage?.outputTokens ?? 0} endpoint=${endpoint}`);
|
|
1754
1769
|
rotator.recordRequestLog({
|
|
1755
1770
|
model: displayModelKey,
|
|
1756
1771
|
account: label,
|
package/src/rotator.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { createHash } from "node:crypto";
|
|
2
1
|
import {
|
|
3
2
|
type AccountConfig,
|
|
4
3
|
type AccountRuntime,
|
|
@@ -9,7 +8,6 @@ import {
|
|
|
9
8
|
type ModelQuota,
|
|
10
9
|
type ModelRotationState,
|
|
11
10
|
type PersistedState,
|
|
12
|
-
type ProviderCredential,
|
|
13
11
|
type RoutingAccountDiagnostic,
|
|
14
12
|
type RoutingModelDiagnostics,
|
|
15
13
|
type RoutingRejectionReason,
|
|
@@ -17,8 +15,8 @@ import {
|
|
|
17
15
|
type TokenBucket,
|
|
18
16
|
type TokenUsageData,
|
|
19
17
|
type TokenUsageTiered,
|
|
20
|
-
MODEL_PRICING,
|
|
21
18
|
MODEL_TIER_ACCESS,
|
|
19
|
+
getModelPricing,
|
|
22
20
|
QUOTA_USER_AGENT,
|
|
23
21
|
REQUEST_GOOG_API_CLIENT,
|
|
24
22
|
REQUEST_CLIENT_METADATA,
|
|
@@ -29,9 +27,11 @@ import {
|
|
|
29
27
|
DEFAULT_QUOTA_POLL_INTERVAL_MS,
|
|
30
28
|
MAX_QUOTA_POLL_INTERVAL_MS,
|
|
31
29
|
MIN_QUOTA_POLL_INTERVAL_MS,
|
|
30
|
+
isStaticAntigravityModel,
|
|
32
31
|
resolveQuotaModelKey,
|
|
33
32
|
resolveDisplayModelKey,
|
|
34
33
|
} from "./types.js";
|
|
34
|
+
import { dynamicCatalog } from "./providers/google-antigravity/dynamic-catalog.js";
|
|
35
35
|
import {
|
|
36
36
|
reportFlagEvent,
|
|
37
37
|
FLAG_PATTERNS,
|
|
@@ -88,59 +88,15 @@ import {
|
|
|
88
88
|
parseRetryAfterMs,
|
|
89
89
|
RESOURCE_EXHAUSTED_FALLBACK_MS,
|
|
90
90
|
} from "./rate-limit-parser.js";
|
|
91
|
+
import {
|
|
92
|
+
getAccountIdentity,
|
|
93
|
+
getProviderCredentialDetails,
|
|
94
|
+
} from "./account-identity.js";
|
|
91
95
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
// codeql[js/insufficient-password-hash]
|
|
97
|
-
return createHash("sha256").update(secret).digest("hex").slice(0, 12);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export function getProviderCredentialDetails(
|
|
101
|
-
norm: AccountConfig,
|
|
102
|
-
cred?: ProviderCredential,
|
|
103
|
-
): {
|
|
104
|
-
provider: string;
|
|
105
|
-
projectId: string;
|
|
106
|
-
providerAccountId: string;
|
|
107
|
-
secret: string;
|
|
108
|
-
fingerprint: string;
|
|
109
|
-
} {
|
|
110
|
-
const provider = cred?.provider ?? DEFAULT_PROVIDER;
|
|
111
|
-
const projectId = getProviderProjectId(norm, provider);
|
|
112
|
-
let providerAccountId = cred?.providerAccountId || "";
|
|
113
|
-
let secret = cred?.refreshToken || cred?.apiKey || "";
|
|
114
|
-
|
|
115
|
-
if (provider === "google-antigravity") {
|
|
116
|
-
secret ||= norm.refreshToken || norm.apiKey || "";
|
|
117
|
-
} else if (provider === "openai-codex") {
|
|
118
|
-
providerAccountId ||= norm.codexAccountId || "";
|
|
119
|
-
secret ||= norm.codexRefreshToken || "";
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const fingerprint = !projectId && !providerAccountId && secret ? credentialFingerprint(secret) : "";
|
|
123
|
-
return { provider, projectId, providerAccountId, secret, fingerprint };
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
export function getAccountIdentity(account: AccountConfig | AccountRuntime): string {
|
|
127
|
-
const config = "config" in account ? account.config : account;
|
|
128
|
-
const norm = normalizeAccountConfig(config);
|
|
129
|
-
const email = norm.email.toLowerCase().trim();
|
|
130
|
-
const creds = (norm.credentials ?? [])
|
|
131
|
-
.slice()
|
|
132
|
-
.sort((a, b) => a.provider.localeCompare(b.provider))
|
|
133
|
-
.map((c) => {
|
|
134
|
-
const details = getProviderCredentialDetails(norm, c);
|
|
135
|
-
return `${details.provider}:${details.projectId}:${details.providerAccountId}:${details.fingerprint}`;
|
|
136
|
-
})
|
|
137
|
-
.join("|");
|
|
138
|
-
if (!creds) {
|
|
139
|
-
const details = getProviderCredentialDetails(norm, { provider: DEFAULT_PROVIDER });
|
|
140
|
-
return `${email}#${details.provider}:${details.projectId}:${details.providerAccountId}:${details.fingerprint}`;
|
|
141
|
-
}
|
|
142
|
-
return `${email}#${creds}`;
|
|
143
|
-
}
|
|
96
|
+
export {
|
|
97
|
+
getAccountIdentity,
|
|
98
|
+
getProviderCredentialDetails,
|
|
99
|
+
} from "./account-identity.js";
|
|
144
100
|
|
|
145
101
|
export function areAccountIdentitiesCompatible(
|
|
146
102
|
incomingConfig: AccountConfig,
|
|
@@ -904,6 +860,11 @@ export class AccountRotator {
|
|
|
904
860
|
void this.refreshOllamaCatalogOnce().catch(() => {});
|
|
905
861
|
|
|
906
862
|
const available = this.accounts.filter((a) => !a.disabled && !a.flagged);
|
|
863
|
+
dynamicCatalog.retainAccounts(
|
|
864
|
+
available
|
|
865
|
+
.filter((account) => hasCredential(account.config, DEFAULT_PROVIDER))
|
|
866
|
+
.map(getAccountIdentity),
|
|
867
|
+
);
|
|
907
868
|
let quotaPublished = false;
|
|
908
869
|
for (const account of available) {
|
|
909
870
|
if (await this.pollAccountQuota(account)) {
|
|
@@ -1000,10 +961,13 @@ export class AccountRotator {
|
|
|
1000
961
|
|
|
1001
962
|
// Get quota % for a specific model on an account. Returns -1 if no data.
|
|
1002
963
|
private getModelQuota(account: AccountRuntime, modelKey: string): number {
|
|
1003
|
-
const
|
|
1004
|
-
|
|
964
|
+
const quotaKey = isStaticAntigravityModel(modelKey)
|
|
965
|
+
? modelKey
|
|
966
|
+
: dynamicCatalog.resolveQuotaPool(modelKey) ?? modelKey;
|
|
967
|
+
const q = account.quota.find((q) => q.modelKey === quotaKey) ??
|
|
968
|
+
(quotaKey.startsWith(`${CODEX_QUOTA_MODEL_KEY}:`)
|
|
1005
969
|
? account.quota.find((candidate) => candidate.modelKey === CODEX_QUOTA_MODEL_KEY)
|
|
1006
|
-
:
|
|
970
|
+
: quotaKey.startsWith(`${OPENCODE_ZEN_PROVIDER_ID}:`)
|
|
1007
971
|
? account.quota.find((candidate) => candidate.providerId === OPENCODE_ZEN_PROVIDER_ID)
|
|
1008
972
|
: undefined);
|
|
1009
973
|
if (!q) return -1;
|
|
@@ -1011,7 +975,7 @@ export class AccountRotator {
|
|
|
1011
975
|
// quota is fully exhausted (0%), the account cannot accept any requests
|
|
1012
976
|
// regardless of how much session quota remains. Treat it as 0 so the
|
|
1013
977
|
// account is skipped at all call sites that check `quota === 0`.
|
|
1014
|
-
if (
|
|
978
|
+
if (quotaKey === "session") {
|
|
1015
979
|
const weekly = account.quota.find((w) => w.modelKey === "weekly");
|
|
1016
980
|
if (weekly && weekly.percentRemaining === 0) return 0;
|
|
1017
981
|
}
|
|
@@ -1023,10 +987,13 @@ export class AccountRotator {
|
|
|
1023
987
|
account: AccountRuntime,
|
|
1024
988
|
modelKey: string,
|
|
1025
989
|
): "fresh" | "5h" | "7d" {
|
|
1026
|
-
const
|
|
1027
|
-
|
|
990
|
+
const quotaKey = isStaticAntigravityModel(modelKey)
|
|
991
|
+
? modelKey
|
|
992
|
+
: dynamicCatalog.resolveQuotaPool(modelKey) ?? modelKey;
|
|
993
|
+
const q = account.quota.find((q) => q.modelKey === quotaKey) ??
|
|
994
|
+
(quotaKey.startsWith(`${CODEX_QUOTA_MODEL_KEY}:`)
|
|
1028
995
|
? account.quota.find((candidate) => candidate.modelKey === CODEX_QUOTA_MODEL_KEY)
|
|
1029
|
-
:
|
|
996
|
+
: quotaKey.startsWith(`${OPENCODE_ZEN_PROVIDER_ID}:`)
|
|
1030
997
|
? account.quota.find((candidate) => candidate.providerId === OPENCODE_ZEN_PROVIDER_ID)
|
|
1031
998
|
: undefined);
|
|
1032
999
|
return q?.timerType ?? "fresh";
|
|
@@ -2888,17 +2855,7 @@ export class AccountRotator {
|
|
|
2888
2855
|
let totalUsd = 0;
|
|
2889
2856
|
const byModel: TokenUsageData["savings"]["byModel"] = {};
|
|
2890
2857
|
for (const [model, totals] of Object.entries(modelTotals)) {
|
|
2891
|
-
|
|
2892
|
-
if (!pricing) {
|
|
2893
|
-
const lower = model.toLowerCase();
|
|
2894
|
-
if (lower.includes("opus")) pricing = MODEL_PRICING["claude-opus-4-6-thinking"];
|
|
2895
|
-
else if (lower.includes("sonnet")) pricing = MODEL_PRICING["claude-sonnet-4-6"];
|
|
2896
|
-
else if (lower.includes("3.7-flash")) pricing = MODEL_PRICING["gemini-3.7-flash-tiered"];
|
|
2897
|
-
else if (lower.includes("3.6-flash")) pricing = MODEL_PRICING["gemini-3.6-flash-high"];
|
|
2898
|
-
else if (lower.includes("3.5-flash")) pricing = MODEL_PRICING["gemini-3.5-flash-high"];
|
|
2899
|
-
else if (lower.includes("flash")) pricing = MODEL_PRICING["gemini-3-flash"];
|
|
2900
|
-
else if (lower.includes("pro")) pricing = MODEL_PRICING["gemini-3.1-pro"];
|
|
2901
|
-
}
|
|
2858
|
+
const pricing = getModelPricing(model);
|
|
2902
2859
|
if (!pricing) continue;
|
|
2903
2860
|
const inputUsd = (totals.input / 1_000_000) * pricing.inputPer1M;
|
|
2904
2861
|
const outputUsd = (totals.output / 1_000_000) * pricing.outputPer1M;
|
|
@@ -3538,6 +3495,13 @@ export class AccountRotator {
|
|
|
3538
3495
|
account: AccountRuntime,
|
|
3539
3496
|
modelKey: string,
|
|
3540
3497
|
): boolean {
|
|
3498
|
+
if (
|
|
3499
|
+
!isStaticAntigravityModel(modelKey) &&
|
|
3500
|
+
dynamicCatalog.wasDiscovered(modelKey) &&
|
|
3501
|
+
!dynamicCatalog.hasModelForAccount(getAccountIdentity(account), modelKey)
|
|
3502
|
+
) {
|
|
3503
|
+
return false;
|
|
3504
|
+
}
|
|
3541
3505
|
const providerId = getProviderIdForPoolKey(modelKey);
|
|
3542
3506
|
return hasCredential(account.config, providerId) &&
|
|
3543
3507
|
!account.invalidProviders?.[providerId] &&
|
|
@@ -3554,6 +3518,10 @@ export class AccountRotator {
|
|
|
3554
3518
|
}
|
|
3555
3519
|
|
|
3556
3520
|
private resolvePoolKeyForModel(model: string): string | null {
|
|
3521
|
+
if (
|
|
3522
|
+
!isStaticAntigravityModel(model) &&
|
|
3523
|
+
dynamicCatalog.wasDiscovered(model)
|
|
3524
|
+
) return model.toLowerCase();
|
|
3557
3525
|
const context = {
|
|
3558
3526
|
ollamaModels: this.ollamaModels,
|
|
3559
3527
|
codexModels: this.codexModels,
|
package/src/spend-logger.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import randomBytes from "node:crypto";
|
|
2
|
-
import {
|
|
2
|
+
import { getModelPricing, type SpendLog, type DailySpend } from "./types.js";
|
|
3
3
|
import { isDbConfigured, queryDb } from "./db-store.js";
|
|
4
4
|
import { rotatorEnv } from "./env.js";
|
|
5
5
|
|
|
@@ -59,7 +59,7 @@ export function logSpend(
|
|
|
59
59
|
return;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
const pricing =
|
|
62
|
+
const pricing = getModelPricing(log.model) ?? { inputPer1M: 0, outputPer1M: 0 };
|
|
63
63
|
const promptTokens = Math.max(0, log.promptTokens || 0);
|
|
64
64
|
const completionTokens = Math.max(0, log.completionTokens || 0);
|
|
65
65
|
const promptCostUsd = (promptTokens / 1_000_000) * (pricing.inputPer1M || 0);
|
|
@@ -253,25 +253,7 @@ export async function flushSpendLogs(): Promise<void> {
|
|
|
253
253
|
}
|
|
254
254
|
|
|
255
255
|
export function calculateCost(model: string, promptTokens: number, completionTokens: number): number {
|
|
256
|
-
|
|
257
|
-
if (!pricing) {
|
|
258
|
-
const lower = model.toLowerCase();
|
|
259
|
-
if (lower.includes("opus")) {
|
|
260
|
-
pricing = MODEL_PRICING["claude-opus-4-6-thinking"];
|
|
261
|
-
} else if (lower.includes("sonnet")) {
|
|
262
|
-
pricing = MODEL_PRICING["claude-sonnet-4-6"];
|
|
263
|
-
} else if (lower.includes("3.7-flash")) {
|
|
264
|
-
pricing = MODEL_PRICING["gemini-3.7-flash-tiered"];
|
|
265
|
-
} else if (lower.includes("3.6-flash")) {
|
|
266
|
-
pricing = MODEL_PRICING["gemini-3.6-flash-high"];
|
|
267
|
-
} else if (lower.includes("3.5-flash")) {
|
|
268
|
-
pricing = MODEL_PRICING["gemini-3.5-flash-high"];
|
|
269
|
-
} else if (lower.includes("flash")) {
|
|
270
|
-
pricing = MODEL_PRICING["gemini-3-flash"];
|
|
271
|
-
} else if (lower.includes("pro")) {
|
|
272
|
-
pricing = MODEL_PRICING["gemini-3.1-pro"];
|
|
273
|
-
}
|
|
274
|
-
}
|
|
256
|
+
const pricing = getModelPricing(model);
|
|
275
257
|
if (!pricing) return 0;
|
|
276
258
|
const inputCost = (promptTokens / 1_000_000) * pricing.inputPer1M;
|
|
277
259
|
const outputCost = (completionTokens / 1_000_000) * pricing.outputPer1M;
|
|
@@ -251,14 +251,15 @@ function fetchModelCatalog() {
|
|
|
251
251
|
var FALLBACK_MODEL_CATALOG = [
|
|
252
252
|
{ id: "gemini-3.1-pro-low", owned_by: "tuxevil-rotator" },
|
|
253
253
|
{ id: "gemini-3.1-pro-high", owned_by: "tuxevil-rotator" },
|
|
254
|
-
{ id: "gemini-3.5-flash-medium", owned_by: "tuxevil-rotator" },
|
|
255
|
-
{ id: "gemini-3.5-flash-high", owned_by: "tuxevil-rotator" },
|
|
256
254
|
{ id: "gemini-3-flash", owned_by: "tuxevil-rotator" },
|
|
257
255
|
{ id: "gemini-3.6-flash-low", owned_by: "tuxevil-rotator" },
|
|
258
256
|
{ id: "gemini-3.6-flash-medium", owned_by: "tuxevil-rotator" },
|
|
259
257
|
{ id: "gemini-3.6-flash-high", owned_by: "tuxevil-rotator" },
|
|
260
258
|
{ id: "gemini-3.6-flash-tiered", owned_by: "tuxevil-rotator" },
|
|
261
259
|
{ id: "gemini-3.7-flash-tiered", owned_by: "tuxevil-rotator" },
|
|
260
|
+
{ id: "gemini-3.8-flash-low", owned_by: "tuxevil-rotator" },
|
|
261
|
+
{ id: "gemini-3.8-flash-medium", owned_by: "tuxevil-rotator" },
|
|
262
|
+
{ id: "gemini-3.8-flash-high", owned_by: "tuxevil-rotator" },
|
|
262
263
|
{ id: "claude-sonnet-4-6", owned_by: "tuxevil-rotator" },
|
|
263
264
|
{ id: "claude-opus-4-6-thinking", owned_by: "tuxevil-rotator" },
|
|
264
265
|
{ id: "gpt-oss-120b-medium", owned_by: "tuxevil-rotator" },
|
|
@@ -531,4 +532,4 @@ if (document.readyState === "loading") {
|
|
|
531
532
|
document.addEventListener("DOMContentLoaded", initKeysPage);
|
|
532
533
|
} else {
|
|
533
534
|
initKeysPage();
|
|
534
|
-
}
|
|
535
|
+
}
|
|
@@ -271,8 +271,11 @@ document.addEventListener("click", function(e) {
|
|
|
271
271
|
var availableKeys = [];
|
|
272
272
|
var selectedKeyValues = [];
|
|
273
273
|
var availableModels = [
|
|
274
|
+
{ value: "gemini-3.8-flash-high", label: "gemini-3.8-flash-high" },
|
|
275
|
+
{ value: "gemini-3.8-flash-medium", label: "gemini-3.8-flash-medium" },
|
|
276
|
+
{ value: "gemini-3.8-flash-low", label: "gemini-3.8-flash-low" },
|
|
277
|
+
{ value: "gemini-3.7-flash-tiered", label: "gemini-3.7-flash-tiered" },
|
|
274
278
|
{ value: "gemini-3.6-flash", label: "gemini-3.6-flash" },
|
|
275
|
-
{ value: "gemini-3.5-flash", label: "gemini-3.5-flash" },
|
|
276
279
|
{ value: "gemini-3.1-pro", label: "gemini-3.1-pro" },
|
|
277
280
|
{ value: "claude-sonnet-4-6", label: "claude-sonnet-4-6" },
|
|
278
281
|
{ value: "claude-opus-4-6-thinking", label: "claude-opus-4-6-thinking" },
|
|
@@ -794,4 +797,4 @@ if (document.readyState === "loading") {
|
|
|
794
797
|
document.addEventListener("DOMContentLoaded", initLogsPage);
|
|
795
798
|
} else {
|
|
796
799
|
initLogsPage();
|
|
797
|
-
}
|
|
800
|
+
}
|