tuxevil-rotator 3.0.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 +773 -0
- package/LICENSE +21 -0
- package/README.md +341 -0
- package/bin/pi-antigravity-rotator.js +27 -0
- package/bin/tuxevil-rotator.js +5 -0
- package/package.json +63 -0
- package/src/account-store.ts +542 -0
- package/src/admin-auth.ts +126 -0
- package/src/body-limit.ts +52 -0
- package/src/cli.ts +293 -0
- package/src/compat/cache.ts +38 -0
- package/src/compat/model-specs.ts +82 -0
- package/src/compat/schema-sanitizer.ts +284 -0
- package/src/compat.ts +2357 -0
- package/src/compression/index.ts +92 -0
- package/src/compression/lite.ts +210 -0
- package/src/compression/preservation.ts +183 -0
- package/src/compression/rtk/command-detector.ts +170 -0
- package/src/compression/rtk/deduplicator.ts +39 -0
- package/src/compression/rtk/filter-loader.ts +84 -0
- package/src/compression/rtk/filter-types.ts +25 -0
- package/src/compression/rtk/filters/aws.json +34 -0
- package/src/compression/rtk/filters/biome.json +35 -0
- package/src/compression/rtk/filters/build-eslint.json +37 -0
- package/src/compression/rtk/filters/build-typescript.json +33 -0
- package/src/compression/rtk/filters/build-vite.json +41 -0
- package/src/compression/rtk/filters/build-webpack.json +34 -0
- package/src/compression/rtk/filters/bundle-install.json +35 -0
- package/src/compression/rtk/filters/composer.json +81 -0
- package/src/compression/rtk/filters/curl.json +34 -0
- package/src/compression/rtk/filters/df.json +34 -0
- package/src/compression/rtk/filters/docker-build.json +78 -0
- package/src/compression/rtk/filters/docker-logs.json +42 -0
- package/src/compression/rtk/filters/docker-ps.json +33 -0
- package/src/compression/rtk/filters/dotnet.json +47 -0
- package/src/compression/rtk/filters/du.json +33 -0
- package/src/compression/rtk/filters/error-stacktrace.json +41 -0
- package/src/compression/rtk/filters/gcloud.json +34 -0
- package/src/compression/rtk/filters/generic-output.json +32 -0
- package/src/compression/rtk/filters/gh.json +98 -0
- package/src/compression/rtk/filters/git-branch.json +45 -0
- package/src/compression/rtk/filters/git-diff.json +33 -0
- package/src/compression/rtk/filters/git-log.json +33 -0
- package/src/compression/rtk/filters/git-status.json +40 -0
- package/src/compression/rtk/filters/golangci-lint.json +34 -0
- package/src/compression/rtk/filters/gradle.json +47 -0
- package/src/compression/rtk/filters/json-output.json +40 -0
- package/src/compression/rtk/filters/kubectl.json +99 -0
- package/src/compression/rtk/filters/make.json +38 -0
- package/src/compression/rtk/filters/mypy.json +34 -0
- package/src/compression/rtk/filters/npm-audit.json +43 -0
- package/src/compression/rtk/filters/npm-install.json +41 -0
- package/src/compression/rtk/filters/nx.json +34 -0
- package/src/compression/rtk/filters/pip.json +41 -0
- package/src/compression/rtk/filters/playwright.json +42 -0
- package/src/compression/rtk/filters/poetry-install.json +35 -0
- package/src/compression/rtk/filters/prettier.json +40 -0
- package/src/compression/rtk/filters/ps.json +34 -0
- package/src/compression/rtk/filters/rsync.json +33 -0
- package/src/compression/rtk/filters/rubocop.json +34 -0
- package/src/compression/rtk/filters/ruff.json +40 -0
- package/src/compression/rtk/filters/shell-find.json +33 -0
- package/src/compression/rtk/filters/shell-grep.json +37 -0
- package/src/compression/rtk/filters/shell-ls.json +33 -0
- package/src/compression/rtk/filters/ssh.json +43 -0
- package/src/compression/rtk/filters/systemctl-status.json +43 -0
- package/src/compression/rtk/filters/terraform-plan.json +34 -0
- package/src/compression/rtk/filters/test-cargo.json +42 -0
- package/src/compression/rtk/filters/test-go.json +41 -0
- package/src/compression/rtk/filters/test-jest.json +43 -0
- package/src/compression/rtk/filters/test-pytest.json +41 -0
- package/src/compression/rtk/filters/test-vitest.json +43 -0
- package/src/compression/rtk/filters/tofu-plan.json +34 -0
- package/src/compression/rtk/filters/turbo.json +34 -0
- package/src/compression/rtk/filters/uv-sync.json +35 -0
- package/src/compression/rtk/filters/wget.json +34 -0
- package/src/compression/rtk/index.ts +103 -0
- package/src/compression/rtk/line-filter.ts +124 -0
- package/src/compression/rtk/smart-truncate.ts +71 -0
- package/src/config-defaults.ts +93 -0
- package/src/config-normalize.ts +48 -0
- package/src/config-storage.ts +21 -0
- package/src/dashboard.ts +1169 -0
- package/src/db-store.ts +188 -0
- package/src/doctor.ts +115 -0
- package/src/env.ts +15 -0
- package/src/exposure.ts +17 -0
- package/src/fetch-with-retry.ts +93 -0
- package/src/idempotency.ts +137 -0
- package/src/index.ts +267 -0
- package/src/key-auth.ts +159 -0
- package/src/key-migrations.ts +84 -0
- package/src/logger.ts +92 -0
- package/src/login.ts +48 -0
- package/src/notification-poller.ts +155 -0
- package/src/onboarding.ts +754 -0
- package/src/paths.ts +153 -0
- package/src/providers/adapter.ts +131 -0
- package/src/providers/credential-helpers.ts +72 -0
- package/src/providers/google-antigravity/code-assist.ts +161 -0
- package/src/providers/google-antigravity/credentials.ts +24 -0
- package/src/providers/google-antigravity/forward.ts +424 -0
- package/src/providers/google-antigravity/index.ts +168 -0
- package/src/providers/google-antigravity/login.ts +285 -0
- package/src/providers/google-antigravity/oauth.ts +314 -0
- package/src/providers/google-antigravity/quota.ts +152 -0
- package/src/providers/google-antigravity/translators.ts +1663 -0
- package/src/providers/ollama/api-key-validation.ts +59 -0
- package/src/providers/ollama/credentials.ts +28 -0
- package/src/providers/ollama/forward.ts +306 -0
- package/src/providers/ollama/index.ts +85 -0
- package/src/providers/ollama/login.ts +55 -0
- package/src/providers/ollama/prediction.ts +188 -0
- package/src/providers/ollama/quota-poll-store.ts +160 -0
- package/src/providers/ollama/quota.ts +189 -0
- package/src/providers/ollama/translators.ts +256 -0
- package/src/providers/ollama/usage-windows.ts +48 -0
- package/src/providers/proxy-dispatcher.ts +269 -0
- package/src/providers/registry.ts +70 -0
- package/src/proxy.ts +2446 -0
- package/src/rate-limit-parser.ts +126 -0
- package/src/response-headers.ts +98 -0
- package/src/responses-store.ts +159 -0
- package/src/rotator.ts +3614 -0
- package/src/settings-repository.ts +327 -0
- package/src/spend-logger.ts +696 -0
- package/src/static/dashboard-keys.js +384 -0
- package/src/static/dashboard-logs.js +797 -0
- package/src/static/dashboard.css +2740 -0
- package/src/static/dashboard.js +3177 -0
- package/src/storage.ts +105 -0
- package/src/systemd-env.ts +214 -0
- package/src/telemetry.ts +384 -0
- package/src/token-encryption.ts +199 -0
- package/src/types.ts +897 -0
- package/src/validators.ts +218 -0
- package/src/version-check.ts +225 -0
- package/src/virtual-keys.ts +312 -0
- package/tools/telemetry-receiver/README.md +147 -0
- package/tools/telemetry-receiver/package.json +13 -0
- package/tools/telemetry-receiver/receiver.js +2334 -0
- package/tuxevil-rotator_logo.png +0 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,897 @@
|
|
|
1
|
+
// Account types and configuration
|
|
2
|
+
|
|
3
|
+
import { rotatorEnv } from "./env.js";
|
|
4
|
+
import type { ExhaustionPrediction } from "./providers/ollama/prediction.js";
|
|
5
|
+
|
|
6
|
+
export type AccountType = "pro" | "free";
|
|
7
|
+
export type AccountTier =
|
|
8
|
+
| "ultra"
|
|
9
|
+
| "pro"
|
|
10
|
+
| "plus"
|
|
11
|
+
| "free"
|
|
12
|
+
| "unknown"
|
|
13
|
+
| "max"
|
|
14
|
+
| "team";
|
|
15
|
+
export type RoutingPolicy =
|
|
16
|
+
| "timer-first"
|
|
17
|
+
| "tier-first"
|
|
18
|
+
| "quota-first"
|
|
19
|
+
| "hybrid"
|
|
20
|
+
| "sequential-quota"
|
|
21
|
+
| "sticky-quota";
|
|
22
|
+
|
|
23
|
+
export type RoutingRejectionReason =
|
|
24
|
+
| "disabled"
|
|
25
|
+
| "flagged"
|
|
26
|
+
| "provider-ineligible"
|
|
27
|
+
| "account-concurrency"
|
|
28
|
+
| "project-concurrency"
|
|
29
|
+
| "cooldown"
|
|
30
|
+
| "fresh-window-blocked"
|
|
31
|
+
| "quota-zero"
|
|
32
|
+
| "project-breaker"
|
|
33
|
+
| "model-breaker"
|
|
34
|
+
| "daily-account-stop"
|
|
35
|
+
| "daily-project-stop"
|
|
36
|
+
| "token-bucket-empty";
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* One set of credentials for a single provider, owned by an account.
|
|
40
|
+
* The account (email) is the parent entity; it may hold credentials for
|
|
41
|
+
* multiple providers (e.g. the same human with a Google Antigravity token
|
|
42
|
+
* and an Ollama Cloud API key).
|
|
43
|
+
*/
|
|
44
|
+
export interface ProviderCredential {
|
|
45
|
+
/** Provider id: "google-antigravity" or "ollama". */
|
|
46
|
+
provider: string;
|
|
47
|
+
/** Ollama Cloud: static API key (never expires). */
|
|
48
|
+
apiKey?: string;
|
|
49
|
+
/** Google Antigravity: OAuth refresh token. */
|
|
50
|
+
refreshToken?: string;
|
|
51
|
+
/** Google Antigravity: Cloud project id. */
|
|
52
|
+
projectId?: string;
|
|
53
|
+
// How the projectId was obtained.
|
|
54
|
+
projectSource?: "google" | "manual";
|
|
55
|
+
/** Optional HTTP(S) or SOCKS5 egress proxy for this provider credential. */
|
|
56
|
+
proxyUrl?: string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface AccountConfig {
|
|
60
|
+
email: string;
|
|
61
|
+
/**
|
|
62
|
+
* Per-provider credentials. The account (email) is the parent entity and
|
|
63
|
+
* may hold credentials for several providers.
|
|
64
|
+
*
|
|
65
|
+
* Legacy configs (pre-2.8) used flat fields instead: `provider`,
|
|
66
|
+
* `apiKey` (Ollama) and `refreshToken`/`projectId` (Google). Those shapes
|
|
67
|
+
* are still accepted on read and normalized into `credentials` by
|
|
68
|
+
* normalizeAccountConfig().
|
|
69
|
+
*/
|
|
70
|
+
credentials?: ProviderCredential[];
|
|
71
|
+
/**
|
|
72
|
+
* @deprecated legacy flat provider id ("google-antigravity" default).
|
|
73
|
+
* Migrated into `credentials` on read; kept for back-compat.
|
|
74
|
+
*/
|
|
75
|
+
provider?: string;
|
|
76
|
+
/** @deprecated legacy Ollama Cloud API key, migrated into credentials. */
|
|
77
|
+
apiKey?: string;
|
|
78
|
+
/** @deprecated legacy Google Antigravity OAuth refresh token. */
|
|
79
|
+
refreshToken?: string;
|
|
80
|
+
/** @deprecated legacy Google Antigravity Cloud project id. */
|
|
81
|
+
projectId?: string;
|
|
82
|
+
/** @deprecated migrated into credentials. */
|
|
83
|
+
projectSource?: "google" | "manual";
|
|
84
|
+
/** @deprecated use credentials[].proxyUrl for provider-scoped routing. */
|
|
85
|
+
proxyUrl?: string;
|
|
86
|
+
label?: string;
|
|
87
|
+
// Optional - pro/free is detected dynamically from quota API reset times
|
|
88
|
+
type?: AccountType;
|
|
89
|
+
tier?: AccountTier;
|
|
90
|
+
familyManager?: boolean;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface Config {
|
|
94
|
+
accounts: AccountConfig[];
|
|
95
|
+
requestsPerRotation: number;
|
|
96
|
+
proxyPort: number;
|
|
97
|
+
bindHost?: string;
|
|
98
|
+
routingPolicy?: RoutingPolicy;
|
|
99
|
+
// Rotate when a model's quota drops by this many percentage points (0 = disabled, use request count)
|
|
100
|
+
rotateOnQuotaDrop: number;
|
|
101
|
+
// How often to poll quota (ms). Default: 5min
|
|
102
|
+
quotaPollIntervalMs: number;
|
|
103
|
+
// Hard cap on parallel requests per account. Conservative default is 1.
|
|
104
|
+
maxConcurrentRequestsPerAccount?: number;
|
|
105
|
+
// Hard cap on parallel requests per projectId/model. Conservative default is 1.
|
|
106
|
+
maxConcurrentRequestsPerProjectModel?: number;
|
|
107
|
+
// Global delay in ms added to every request to slow down traffic and avoid rate limits.
|
|
108
|
+
globalRequestDelayMs?: number;
|
|
109
|
+
// Pause projectId/model when several accounts hit provider 429 in a short window. Defaults: 3 hits / 10min / 60min pause.
|
|
110
|
+
projectCircuitBreaker429Threshold?: number;
|
|
111
|
+
projectCircuitBreakerWindowMs?: number;
|
|
112
|
+
projectCircuitBreakerCooldownMs?: number;
|
|
113
|
+
// Pause a model globally when several unique accounts hit provider 429 in a short window. Defaults: 3 hits / same window / 6h pause.
|
|
114
|
+
modelCircuitBreaker429Threshold?: number;
|
|
115
|
+
modelCircuitBreakerCooldownMs?: number;
|
|
116
|
+
// Daily safety budgets. Defaults: account slow 250, account stop 350, project slow 900, project stop 1200.
|
|
117
|
+
dailyAccountSlowRequests?: number;
|
|
118
|
+
dailyAccountStopRequests?: number;
|
|
119
|
+
dailyProjectSlowRequests?: number;
|
|
120
|
+
dailyProjectStopRequests?: number;
|
|
121
|
+
// Add small delay before upstream call when an account/project is in slow mode. Default: 8-25s.
|
|
122
|
+
slowModeJitterMinMs?: number;
|
|
123
|
+
slowModeJitterMaxMs?: number;
|
|
124
|
+
// Pause all routing after a serious provider flag. Default: 6h.
|
|
125
|
+
protectivePauseMs?: number;
|
|
126
|
+
// Use request-count rotation only before quota data is available. Default: true.
|
|
127
|
+
useRequestCountRotationWhenQuotaUnknownOnly?: boolean;
|
|
128
|
+
tokenBucketEnabled?: boolean;
|
|
129
|
+
tokenBucketMaxTokens?: number;
|
|
130
|
+
tokenBucketRefillPerMinute?: number;
|
|
131
|
+
tokenBucketInitialTokens?: number;
|
|
132
|
+
// Deduplicate in-flight / short-window identical requests to prevent duplicate upstream calls.
|
|
133
|
+
idempotencyEnabled?: boolean;
|
|
134
|
+
idempotencyWindowMs?: number;
|
|
135
|
+
// Retry upstream failures before any response bytes reach the client. Default: 2.
|
|
136
|
+
streamRecoveryMaxRetries?: number;
|
|
137
|
+
// Prompt compression mode ("off" | "lite" | "rtk" | "rtk+lite").
|
|
138
|
+
compressionMode?: "off" | "lite" | "rtk" | "rtk+lite";
|
|
139
|
+
// Override per-model specs used by the compat layer. Keys are model id substrings
|
|
140
|
+
// matched case-insensitively. When set, replaces the bundled defaults entirely.
|
|
141
|
+
modelSpecs?: Record<string, ModelSpecConfig>;
|
|
142
|
+
// Override model-id aliases used to translate the operator-facing name
|
|
143
|
+
// (e.g. "gemini-3.5-flash-high") to the upstream Antigravity name
|
|
144
|
+
// (e.g. "gemini-3-flash-agent"). When set, replaces the bundled defaults.
|
|
145
|
+
modelAliases?: Record<string, string>;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export const DEFAULT_QUOTA_POLL_INTERVAL_MS = 300_000;
|
|
149
|
+
export const MIN_QUOTA_POLL_INTERVAL_MS = 60_000;
|
|
150
|
+
export const MAX_QUOTA_POLL_INTERVAL_MS = 24 * 60 * 60 * 1000;
|
|
151
|
+
|
|
152
|
+
// ── Default model-id aliases ─────────────────────────────────────────
|
|
153
|
+
// Translates operator-facing model names to Antigravity upstream names.
|
|
154
|
+
// When the provider adds a new model, this is the only place that needs
|
|
155
|
+
// updating. Operators can override via Config.modelAliases in accounts.json.
|
|
156
|
+
const DEFAULT_MODEL_ALIASES: Record<string, string> = {
|
|
157
|
+
"gemini-3.1-pro-high": "gemini-pro-agent",
|
|
158
|
+
"gemini-3.5-flash": "gemini-3-flash-agent",
|
|
159
|
+
"gemini-3.5-flash-high": "gemini-3-flash-agent",
|
|
160
|
+
"gemini-3.5-flash-medium": "gemini-3-flash-agent",
|
|
161
|
+
"gpt-oss-120b": "gpt-oss-120b-medium",
|
|
162
|
+
};
|
|
163
|
+
let modelAliasesOverride: Record<string, string> | null = null;
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Replace the bundled model-alias table with operator-provided overrides.
|
|
167
|
+
* Pass `null` to restore the defaults. Called once at startup from
|
|
168
|
+
* index.ts via `setModelAliasesOverride(config.modelAliases ?? null)`.
|
|
169
|
+
*
|
|
170
|
+
* @param aliases Map of operator-facing model name to upstream model name,
|
|
171
|
+
* or null to restore defaults.
|
|
172
|
+
*/
|
|
173
|
+
export function setModelAliasesOverride(
|
|
174
|
+
aliases: Record<string, string> | null,
|
|
175
|
+
): void {
|
|
176
|
+
modelAliasesOverride =
|
|
177
|
+
aliases && Object.keys(aliases).length > 0 ? aliases : null;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function getActiveModelAliases(): Record<string, string> {
|
|
181
|
+
return modelAliasesOverride ?? DEFAULT_MODEL_ALIASES;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Translate a model name to its upstream equivalent. Exact-match lookup
|
|
186
|
+
* (case-insensitive). Returns the original model if no alias is configured.
|
|
187
|
+
*/
|
|
188
|
+
export function applyModelAlias(model: string): string {
|
|
189
|
+
const aliases = getActiveModelAliases();
|
|
190
|
+
if (model in aliases) return aliases[model];
|
|
191
|
+
const lower = model.toLowerCase();
|
|
192
|
+
for (const [from, to] of Object.entries(aliases)) {
|
|
193
|
+
if (from.toLowerCase() === lower) return to;
|
|
194
|
+
}
|
|
195
|
+
return model;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Quota API response from Google
|
|
199
|
+
export interface GoogleQuotaResponse {
|
|
200
|
+
models: Record<
|
|
201
|
+
string,
|
|
202
|
+
{
|
|
203
|
+
quotaInfo?: {
|
|
204
|
+
remainingFraction?: number;
|
|
205
|
+
resetTime?: string;
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
>;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Per-model thinking/output spec used by the compat layer.
|
|
212
|
+
// Operators can override defaults via the `modelSpecs` field in accounts.json.
|
|
213
|
+
export interface ModelSpecConfig {
|
|
214
|
+
maxOutputTokens: number;
|
|
215
|
+
thinkingBudget: number; // -1 = adaptive (model decides), >=0 = fixed
|
|
216
|
+
isThinking: boolean;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Per-model quota info for an account
|
|
220
|
+
export interface ModelQuota {
|
|
221
|
+
modelKey: string;
|
|
222
|
+
displayName: string;
|
|
223
|
+
percentRemaining: number;
|
|
224
|
+
/** Raw usage fraction (0..1) when the provider reports one (Ollama). */
|
|
225
|
+
usageRaw?: number;
|
|
226
|
+
resetTime: string | null;
|
|
227
|
+
// Timer classification based on resetTime duration
|
|
228
|
+
// "fresh" = no active timer, "5h" = short timer, "7d" = long timer
|
|
229
|
+
timerType: "fresh" | "5h" | "7d";
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Model key mapping for the quota API. One entry per family: all Claude
|
|
233
|
+
// variants (and gpt-oss) share one bucket, all Gemini variants share one.
|
|
234
|
+
export const QUOTA_MODEL_KEYS: Record<
|
|
235
|
+
string,
|
|
236
|
+
{ key: string; altKeys: string[]; display: string }
|
|
237
|
+
> = {
|
|
238
|
+
claude: {
|
|
239
|
+
key: "claude",
|
|
240
|
+
altKeys: [
|
|
241
|
+
"claude-opus-4-6-thinking",
|
|
242
|
+
"claude-opus-4-5-thinking",
|
|
243
|
+
"claude-opus-4-5",
|
|
244
|
+
"claude-sonnet-4-6-thinking",
|
|
245
|
+
"claude-sonnet-4-6",
|
|
246
|
+
"claude-sonnet-4-5-thinking",
|
|
247
|
+
"claude-sonnet-4-5",
|
|
248
|
+
"gpt-oss-120b-medium",
|
|
249
|
+
"gpt-oss-120b",
|
|
250
|
+
],
|
|
251
|
+
display: "Claude",
|
|
252
|
+
},
|
|
253
|
+
gemini: {
|
|
254
|
+
key: "gemini",
|
|
255
|
+
altKeys: [
|
|
256
|
+
"gemini-3.1-pro",
|
|
257
|
+
"gemini-3.1-pro-low",
|
|
258
|
+
"gemini-3.1-pro-high",
|
|
259
|
+
"gemini-3-pro-high",
|
|
260
|
+
"gemini-3-pro-low",
|
|
261
|
+
"gemini-3.5-flash",
|
|
262
|
+
"gemini-3.5-flash-low",
|
|
263
|
+
"gemini-3.5-flash-medium",
|
|
264
|
+
"gemini-3.5-flash-high",
|
|
265
|
+
"gemini-3-flash-agent",
|
|
266
|
+
"gemini-3-flash",
|
|
267
|
+
"gemini-3.6-flash",
|
|
268
|
+
"gemini-3.6-flash-high",
|
|
269
|
+
"gemini-3.6-flash-medium",
|
|
270
|
+
"gemini-3.6-flash-low",
|
|
271
|
+
"gemini-3.6-flash-tiered",
|
|
272
|
+
],
|
|
273
|
+
display: "Gemini",
|
|
274
|
+
},
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
// Map request model names to quota model keys (family buckets).
|
|
278
|
+
export function resolveQuotaModelKey(requestModel: string): string | null {
|
|
279
|
+
const lower = requestModel.toLowerCase();
|
|
280
|
+
// Claude family: every Claude variant and gpt-oss share one bucket.
|
|
281
|
+
if (
|
|
282
|
+
lower.includes("claude") ||
|
|
283
|
+
lower.includes("gpt-oss")
|
|
284
|
+
) {
|
|
285
|
+
return "claude";
|
|
286
|
+
}
|
|
287
|
+
// Gemini family: every Gemini variant shares one bucket.
|
|
288
|
+
if (lower.includes("gemini")) {
|
|
289
|
+
return "gemini";
|
|
290
|
+
}
|
|
291
|
+
return null;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Resolves the precise model name for metrics/savings/latency/logs.
|
|
296
|
+
* Unlike resolveQuotaModelKey, this preserves the distinction between:
|
|
297
|
+
* - gemini-3.1-pro-low vs gemini-3.1-pro-high (same quota pool, different display)
|
|
298
|
+
* - claude-sonnet-4-6 vs claude-opus-4-6-thinking (different pricing)
|
|
299
|
+
*/
|
|
300
|
+
export function resolveDisplayModelKey(requestModel: string): string {
|
|
301
|
+
const lower = requestModel.toLowerCase();
|
|
302
|
+
// Explicit agent and gpt-oss overrides
|
|
303
|
+
if (lower.includes("gemini-3-flash-agent")) return "gemini-3.5-flash-high";
|
|
304
|
+
if (lower.includes("gpt-oss-120b")) return "gpt-oss-120b-medium";
|
|
305
|
+
|
|
306
|
+
// Claude — distinguish sonnet vs opus
|
|
307
|
+
if (lower.includes("claude")) {
|
|
308
|
+
if (lower.includes("sonnet")) return "claude-sonnet-4-6";
|
|
309
|
+
if (lower.includes("opus")) return "claude-opus-4-6-thinking";
|
|
310
|
+
return "claude-opus-4-6-thinking"; // fallback
|
|
311
|
+
}
|
|
312
|
+
// Gemini Pro — distinguish low vs high
|
|
313
|
+
if (lower.includes("gemini") && lower.includes("pro")) {
|
|
314
|
+
if (lower.includes("-low")) return "gemini-3.1-pro-low";
|
|
315
|
+
if (lower.includes("-high")) return "gemini-3.1-pro-high";
|
|
316
|
+
return "gemini-3.1-pro"; // unspecified variant
|
|
317
|
+
}
|
|
318
|
+
// Gemini 3.6 Flash — distinguish variants
|
|
319
|
+
if (
|
|
320
|
+
lower.includes("gemini") &&
|
|
321
|
+
lower.includes("3.6") &&
|
|
322
|
+
lower.includes("flash")
|
|
323
|
+
) {
|
|
324
|
+
if (lower.includes("-low")) return "gemini-3.6-flash-low";
|
|
325
|
+
if (lower.includes("-medium")) return "gemini-3.6-flash-medium";
|
|
326
|
+
if (lower.includes("-tiered")) return "gemini-3.6-flash-tiered";
|
|
327
|
+
if (lower.includes("-high")) return "gemini-3.6-flash-high";
|
|
328
|
+
return "gemini-3.6-flash-high"; // unspecified variant
|
|
329
|
+
}
|
|
330
|
+
// Gemini 3.5 Flash — distinguish medium vs high
|
|
331
|
+
if (
|
|
332
|
+
lower.includes("gemini") &&
|
|
333
|
+
lower.includes("3.5") &&
|
|
334
|
+
lower.includes("flash")
|
|
335
|
+
) {
|
|
336
|
+
if (lower.includes("-low") || lower.includes("-medium"))
|
|
337
|
+
return "gemini-3.5-flash-medium";
|
|
338
|
+
if (lower.includes("-high")) return "gemini-3.5-flash-high";
|
|
339
|
+
return "gemini-3.5-flash"; // unspecified variant
|
|
340
|
+
}
|
|
341
|
+
// Flash
|
|
342
|
+
if (lower.includes("gemini") && lower.includes("flash"))
|
|
343
|
+
return "gemini-3-flash";
|
|
344
|
+
// Fallback: return as-is cleaned up
|
|
345
|
+
return requestModel;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// Runtime state for a single account
|
|
349
|
+
export interface AccountRuntime {
|
|
350
|
+
config: AccountConfig;
|
|
351
|
+
accessToken: string | null;
|
|
352
|
+
tokenExpires: number;
|
|
353
|
+
// Rotation tracking (per-model via rotator)
|
|
354
|
+
requestsSinceRotation: number;
|
|
355
|
+
totalRequests: number;
|
|
356
|
+
// Cooldown / exhaustion per-model
|
|
357
|
+
cooldownsByModel: Record<string, number>;
|
|
358
|
+
quotaExhaustedAt: number;
|
|
359
|
+
// Quota tracking (from API) - per-model data
|
|
360
|
+
quota: ModelQuota[];
|
|
361
|
+
lastQuotaPoll: number;
|
|
362
|
+
// Per-provider RAW POLL strings, accumulated by each adapter during a
|
|
363
|
+
// quota cycle. The rotator emits one consolidated log per account/cycle
|
|
364
|
+
// and resets the map.
|
|
365
|
+
lastPollByProvider?: Record<string, string>;
|
|
366
|
+
// Status
|
|
367
|
+
lastUsed: number;
|
|
368
|
+
lastError: string | null;
|
|
369
|
+
consecutiveErrors: number;
|
|
370
|
+
disabled: boolean; // permanently disabled (revoked token, etc.)
|
|
371
|
+
flagged: boolean; // flagged for infringement/abuse by Google
|
|
372
|
+
inFlightRequests: number;
|
|
373
|
+
inFlightByModel: Record<string, number>;
|
|
374
|
+
allowFreshWindowStartsOverride: boolean;
|
|
375
|
+
dailyRequestCount: number;
|
|
376
|
+
dailyRequestDay: string;
|
|
377
|
+
healthScore: number;
|
|
378
|
+
tokenBucket: {
|
|
379
|
+
tokens: number;
|
|
380
|
+
lastRefillAt: number;
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// Per-model rotation state tracked by the rotator
|
|
385
|
+
export interface ModelRotationState {
|
|
386
|
+
activeAccountIndex: number;
|
|
387
|
+
/** Preferred account for quota-aware policies while a temporary fallback is active. */
|
|
388
|
+
stickyAccountIndex?: number;
|
|
389
|
+
quotaAtRotationStart: number; // quota % when this account became active for this model
|
|
390
|
+
requestsOnActiveAccount: number;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
export interface PersistedSafetyState {
|
|
394
|
+
day: string;
|
|
395
|
+
projectRequests: Record<string, number>;
|
|
396
|
+
projectModelBreakers: Record<string, number>;
|
|
397
|
+
modelBreakers?: Record<string, number>;
|
|
398
|
+
provider429Events: Array<{
|
|
399
|
+
ts: number;
|
|
400
|
+
projectId: string;
|
|
401
|
+
modelKey: string;
|
|
402
|
+
account: string;
|
|
403
|
+
}>;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
export interface PersistedState {
|
|
407
|
+
// Per-model active account index
|
|
408
|
+
modelAccounts: Record<string, number>;
|
|
409
|
+
// Per-model request count on the active account
|
|
410
|
+
modelRequestCounts?: Record<string, number>;
|
|
411
|
+
// Per-model preferred account for quota-aware sticky/sequential fallback
|
|
412
|
+
modelStickyAccounts?: Record<string, number>;
|
|
413
|
+
// Legacy fallback
|
|
414
|
+
currentIndex?: number;
|
|
415
|
+
protectivePauseUntil?: number;
|
|
416
|
+
protectivePauseReason?: string | null;
|
|
417
|
+
allowFreshWindowStarts?: boolean;
|
|
418
|
+
autoWarmupEnabled?: boolean;
|
|
419
|
+
safety?: PersistedSafetyState;
|
|
420
|
+
accounts: Record<
|
|
421
|
+
string,
|
|
422
|
+
{
|
|
423
|
+
totalRequests: number;
|
|
424
|
+
dailyRequestCount?: number;
|
|
425
|
+
dailyRequestDay?: string;
|
|
426
|
+
cooldownUntil?: number; // legacy fallback
|
|
427
|
+
cooldownsByModel?: Record<string, number>;
|
|
428
|
+
quotaExhaustedAt: number;
|
|
429
|
+
disabled: boolean;
|
|
430
|
+
flagged: boolean;
|
|
431
|
+
allowFreshWindowStartsOverride?: boolean;
|
|
432
|
+
}
|
|
433
|
+
>;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// Version check info for dashboard
|
|
437
|
+
export interface UpdateInfo {
|
|
438
|
+
currentVersion: string;
|
|
439
|
+
latestVersion: string | null;
|
|
440
|
+
updateAvailable: boolean;
|
|
441
|
+
checkedAt: number;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
// Admin broadcast notification
|
|
445
|
+
export interface AdminNotification {
|
|
446
|
+
id: string;
|
|
447
|
+
type: "info" | "warning" | "critical";
|
|
448
|
+
title: string;
|
|
449
|
+
message: string;
|
|
450
|
+
createdAt: string;
|
|
451
|
+
actionUrl?: string | null;
|
|
452
|
+
actionLabel?: string | null;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// Dashboard API response
|
|
456
|
+
export interface StatusResponse {
|
|
457
|
+
version: string;
|
|
458
|
+
proxyPort: number;
|
|
459
|
+
requestsPerRotation: number;
|
|
460
|
+
totalRequestsAllAccounts: number;
|
|
461
|
+
uptime: number;
|
|
462
|
+
// Per-model active account
|
|
463
|
+
activeAccounts: Record<string, string>;
|
|
464
|
+
accounts: AccountStatus[];
|
|
465
|
+
protectivePauseUntil: number;
|
|
466
|
+
protectivePauseRemaining: number;
|
|
467
|
+
protectivePauseReason: string | null;
|
|
468
|
+
operatorControls: {
|
|
469
|
+
allowFreshWindowStarts: boolean;
|
|
470
|
+
autoWarmupEnabled: boolean;
|
|
471
|
+
};
|
|
472
|
+
security: {
|
|
473
|
+
adminTokenConfigured: boolean;
|
|
474
|
+
warning: string | null;
|
|
475
|
+
bindHost: string;
|
|
476
|
+
};
|
|
477
|
+
routingDiagnostics: Record<string, RoutingModelDiagnostics>;
|
|
478
|
+
ollamaModels: string[];
|
|
479
|
+
// Present only when at least one account carries an ollama credential.
|
|
480
|
+
modelTierAccess?: Record<string, ModelTierAccess>;
|
|
481
|
+
predictions: Record<string, ExhaustionPrediction>;
|
|
482
|
+
circuitBreakers: {
|
|
483
|
+
model: Record<string, { until: number; remainingMs: number }>;
|
|
484
|
+
project: Record<string, { until: number; remainingMs: number }>;
|
|
485
|
+
};
|
|
486
|
+
routingHealth: {
|
|
487
|
+
state: "healthy" | "paused" | "cooldown_wait" | "busy" | "stopped";
|
|
488
|
+
reason: string;
|
|
489
|
+
nextRetryIn: number;
|
|
490
|
+
availableCount: number;
|
|
491
|
+
readyCount: number;
|
|
492
|
+
activeCount: number;
|
|
493
|
+
cooldownCount: number;
|
|
494
|
+
busyCount: number;
|
|
495
|
+
flaggedCount: number;
|
|
496
|
+
disabledCount: number;
|
|
497
|
+
errorCount: number;
|
|
498
|
+
};
|
|
499
|
+
recentEvents: RecentEvent[];
|
|
500
|
+
requestLog: RequestLogEntry[];
|
|
501
|
+
tokenUsage: TokenUsageData;
|
|
502
|
+
latencyStats: Record<
|
|
503
|
+
string,
|
|
504
|
+
{
|
|
505
|
+
ttfb: { p50: number; p95: number };
|
|
506
|
+
total: { p50: number; p95: number };
|
|
507
|
+
count: number;
|
|
508
|
+
}
|
|
509
|
+
>;
|
|
510
|
+
updateInfo?: UpdateInfo;
|
|
511
|
+
notifications?: AdminNotification[];
|
|
512
|
+
hostedOAuthConfigured?: boolean;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
export interface AccountStatus {
|
|
516
|
+
email: string;
|
|
517
|
+
label: string;
|
|
518
|
+
/** Provider id, e.g. "google-antigravity" | "ollama". */
|
|
519
|
+
provider: string;
|
|
520
|
+
status:
|
|
521
|
+
| "active"
|
|
522
|
+
| "ready"
|
|
523
|
+
| "cooldown"
|
|
524
|
+
| "exhausted"
|
|
525
|
+
| "disabled"
|
|
526
|
+
| "flagged"
|
|
527
|
+
| "error";
|
|
528
|
+
// Which models this account is currently active for
|
|
529
|
+
activeForModels: string[];
|
|
530
|
+
requestsSinceRotation: number;
|
|
531
|
+
totalRequests: number;
|
|
532
|
+
dailyRequestCount: number;
|
|
533
|
+
dailyAccountStopRequests: number;
|
|
534
|
+
dailyProjectRequestCount: number;
|
|
535
|
+
dailyProjectStopRequests: number;
|
|
536
|
+
cooldownsByModel: Record<string, number>;
|
|
537
|
+
lastUsed: number;
|
|
538
|
+
lastError: string | null;
|
|
539
|
+
consecutiveErrors: number;
|
|
540
|
+
hasValidToken: boolean;
|
|
541
|
+
quota: ModelQuota[];
|
|
542
|
+
inFlightRequests: number;
|
|
543
|
+
inFlightByModel: Record<string, number>;
|
|
544
|
+
// Pro family sharing
|
|
545
|
+
proDetected: boolean;
|
|
546
|
+
tier: AccountTier;
|
|
547
|
+
healthScore: number;
|
|
548
|
+
tokenBucket: {
|
|
549
|
+
enabled: boolean;
|
|
550
|
+
tokens: number;
|
|
551
|
+
capacity: number;
|
|
552
|
+
nextRefillInMs: number;
|
|
553
|
+
};
|
|
554
|
+
allowFreshWindowStartsOverride: boolean;
|
|
555
|
+
effectiveFreshWindowStartsAllowed: boolean;
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
export interface HealthScoreBreakdown {
|
|
559
|
+
quotaComponent: number;
|
|
560
|
+
errorPenalty: number;
|
|
561
|
+
cooldownPenalty: number;
|
|
562
|
+
availabilityPenalty: number;
|
|
563
|
+
score: number;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
export interface RoutingAccountDiagnostic {
|
|
567
|
+
email: string;
|
|
568
|
+
label: string;
|
|
569
|
+
status: AccountStatus["status"];
|
|
570
|
+
score: number | null;
|
|
571
|
+
timerPriority: number | null;
|
|
572
|
+
quota: number | null;
|
|
573
|
+
tier: AccountTier;
|
|
574
|
+
healthScore: number;
|
|
575
|
+
healthBreakdown: HealthScoreBreakdown;
|
|
576
|
+
distance: number | null;
|
|
577
|
+
tokenBucket: {
|
|
578
|
+
enabled: boolean;
|
|
579
|
+
tokens: number;
|
|
580
|
+
capacity: number;
|
|
581
|
+
nextRefillInMs: number;
|
|
582
|
+
};
|
|
583
|
+
rejectedReason: RoutingRejectionReason | null;
|
|
584
|
+
rejectedDetail: string | null;
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
export interface RoutingModelDiagnostics {
|
|
588
|
+
modelKey: string;
|
|
589
|
+
policy: RoutingPolicy;
|
|
590
|
+
selectedEmail: string | null;
|
|
591
|
+
reason: string;
|
|
592
|
+
availableCandidates: number;
|
|
593
|
+
rejectedCandidates: number;
|
|
594
|
+
accounts: RoutingAccountDiagnostic[];
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
export interface RecentEvent {
|
|
598
|
+
timestamp: number;
|
|
599
|
+
source: "rotator" | "proxy";
|
|
600
|
+
level: "info" | "warn" | "error";
|
|
601
|
+
message: string;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
export interface RequestLogEntry {
|
|
605
|
+
timestamp: number;
|
|
606
|
+
model: string;
|
|
607
|
+
account: string;
|
|
608
|
+
statusCode: number;
|
|
609
|
+
ttfbMs: number;
|
|
610
|
+
totalMs: number;
|
|
611
|
+
inputTokens: number;
|
|
612
|
+
outputTokens: number;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
// Token usage tracking — tiered time-series
|
|
616
|
+
export interface TokenBucket {
|
|
617
|
+
period: string; // key: "2026-04-28T12:05" (min), "2026-04-28T12" (hour), "2026-04-28" (day), "2026-04" (month)
|
|
618
|
+
inputTokens: number;
|
|
619
|
+
outputTokens: number;
|
|
620
|
+
requests: number;
|
|
621
|
+
byModel: Record<
|
|
622
|
+
string,
|
|
623
|
+
{ inputTokens: number; outputTokens: number; requests: number }
|
|
624
|
+
>;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
export interface TokenUsageTiered {
|
|
628
|
+
minutes: TokenBucket[]; // current hour, max 60
|
|
629
|
+
hours: TokenBucket[]; // last 7 days, max 168
|
|
630
|
+
days: TokenBucket[]; // last year, max 365
|
|
631
|
+
months: TokenBucket[]; // historical, unlimited
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
export interface TokenUsageData {
|
|
635
|
+
minutes: TokenBucket[];
|
|
636
|
+
hours: TokenBucket[];
|
|
637
|
+
days: TokenBucket[];
|
|
638
|
+
months: TokenBucket[];
|
|
639
|
+
totalInputTokens: number;
|
|
640
|
+
totalOutputTokens: number;
|
|
641
|
+
totalRequests: number;
|
|
642
|
+
tokensByModel: Record<
|
|
643
|
+
string,
|
|
644
|
+
{ input: number; output: number; requests: number }
|
|
645
|
+
>;
|
|
646
|
+
savings: {
|
|
647
|
+
totalUsd: number;
|
|
648
|
+
byModel: Record<
|
|
649
|
+
string,
|
|
650
|
+
{ inputUsd: number; outputUsd: number; totalUsd: number }
|
|
651
|
+
>;
|
|
652
|
+
};
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
// Pricing per 1M tokens (USD) — what these would cost on paid APIs
|
|
656
|
+
export const MODEL_PRICING: Record<
|
|
657
|
+
string,
|
|
658
|
+
{
|
|
659
|
+
inputPer1M: number;
|
|
660
|
+
outputPer1M: number;
|
|
661
|
+
cachingPer1M?: number;
|
|
662
|
+
cachingStoragePer1MPerHour?: number;
|
|
663
|
+
}
|
|
664
|
+
> = {
|
|
665
|
+
"claude-opus-4-6-thinking": { inputPer1M: 5.0, outputPer1M: 25.0 },
|
|
666
|
+
"claude-sonnet-4-6": { inputPer1M: 3.0, outputPer1M: 15.0 },
|
|
667
|
+
"gemini-3.1-pro": { inputPer1M: 2.0, outputPer1M: 12.0 },
|
|
668
|
+
"gemini-3.1-pro-low": { inputPer1M: 2.0, outputPer1M: 12.0 },
|
|
669
|
+
"gemini-3.1-pro-high": { inputPer1M: 2.0, outputPer1M: 12.0 },
|
|
670
|
+
"gemini-3-flash": { inputPer1M: 0.5, outputPer1M: 3.0 },
|
|
671
|
+
"gemini-3.5-flash": {
|
|
672
|
+
inputPer1M: 1.5,
|
|
673
|
+
outputPer1M: 9.0,
|
|
674
|
+
cachingPer1M: 0.15,
|
|
675
|
+
cachingStoragePer1MPerHour: 1.0,
|
|
676
|
+
},
|
|
677
|
+
"gemini-3.5-flash-low": {
|
|
678
|
+
inputPer1M: 1.5,
|
|
679
|
+
outputPer1M: 9.0,
|
|
680
|
+
cachingPer1M: 0.15,
|
|
681
|
+
cachingStoragePer1MPerHour: 1.0,
|
|
682
|
+
},
|
|
683
|
+
"gemini-3.5-flash-medium": {
|
|
684
|
+
inputPer1M: 1.5,
|
|
685
|
+
outputPer1M: 9.0,
|
|
686
|
+
cachingPer1M: 0.15,
|
|
687
|
+
cachingStoragePer1MPerHour: 1.0,
|
|
688
|
+
},
|
|
689
|
+
"gemini-3.5-flash-high": {
|
|
690
|
+
inputPer1M: 1.5,
|
|
691
|
+
outputPer1M: 9.0,
|
|
692
|
+
cachingPer1M: 0.15,
|
|
693
|
+
cachingStoragePer1MPerHour: 1.0,
|
|
694
|
+
},
|
|
695
|
+
"gemini-3.6-flash": {
|
|
696
|
+
inputPer1M: 1.5,
|
|
697
|
+
outputPer1M: 7.5,
|
|
698
|
+
cachingPer1M: 0.15,
|
|
699
|
+
cachingStoragePer1MPerHour: 1.0,
|
|
700
|
+
},
|
|
701
|
+
"gemini-3.6-flash-high": {
|
|
702
|
+
inputPer1M: 1.5,
|
|
703
|
+
outputPer1M: 7.5,
|
|
704
|
+
cachingPer1M: 0.15,
|
|
705
|
+
cachingStoragePer1MPerHour: 1.0,
|
|
706
|
+
},
|
|
707
|
+
"gemini-3.6-flash-medium": {
|
|
708
|
+
inputPer1M: 1.5,
|
|
709
|
+
outputPer1M: 7.5,
|
|
710
|
+
cachingPer1M: 0.15,
|
|
711
|
+
cachingStoragePer1MPerHour: 1.0,
|
|
712
|
+
},
|
|
713
|
+
"gemini-3.6-flash-low": {
|
|
714
|
+
inputPer1M: 1.5,
|
|
715
|
+
outputPer1M: 7.5,
|
|
716
|
+
cachingPer1M: 0.15,
|
|
717
|
+
cachingStoragePer1MPerHour: 1.0,
|
|
718
|
+
},
|
|
719
|
+
"gemini-3.6-flash-tiered": {
|
|
720
|
+
inputPer1M: 1.5,
|
|
721
|
+
outputPer1M: 7.5,
|
|
722
|
+
cachingPer1M: 0.15,
|
|
723
|
+
cachingStoragePer1MPerHour: 1.0,
|
|
724
|
+
},
|
|
725
|
+
"gpt-oss-120b-medium": { inputPer1M: 2.0, outputPer1M: 10.0 },
|
|
726
|
+
|
|
727
|
+
// Ollama Cloud model pricing (USD per 1M tokens). Sourced from the
|
|
728
|
+
// ~/ollama-rotator project (verified 2026-08-09).
|
|
729
|
+
"gpt-oss:20b": { inputPer1M: 0.075, outputPer1M: 0.30 },
|
|
730
|
+
"gpt-oss:120b": { inputPer1M: 0.15, outputPer1M: 0.60 },
|
|
731
|
+
"deepseek-v4-flash:preview": { inputPer1M: 0.14, outputPer1M: 0.28, cachingPer1M: 0.0028 },
|
|
732
|
+
"deepseek-v4-flash:0731": { inputPer1M: 0.14, outputPer1M: 0.28, cachingPer1M: 0.0028 },
|
|
733
|
+
"deepseek-v4-pro": { inputPer1M: 0.435, outputPer1M: 0.87, cachingPer1M: 0.0036 },
|
|
734
|
+
"qwen3.5:397b": { inputPer1M: 0.60, outputPer1M: 3.60 },
|
|
735
|
+
"glm-5.1": { inputPer1M: 0.80, outputPer1M: 2.56 },
|
|
736
|
+
"glm-5.2": { inputPer1M: 0.80, outputPer1M: 2.56 },
|
|
737
|
+
"gemma4:31b": { inputPer1M: 0.38, outputPer1M: 1.15 },
|
|
738
|
+
"kimi-k2.6": { inputPer1M: 0.95, outputPer1M: 4.00 },
|
|
739
|
+
"kimi-k2.7-code": { inputPer1M: 0.95, outputPer1M: 4.00 },
|
|
740
|
+
"kimi-k3": { inputPer1M: 0.95, outputPer1M: 4.00 },
|
|
741
|
+
"minimax-m2.7": { inputPer1M: 0.30, outputPer1M: 1.20 },
|
|
742
|
+
"minimax-m3": { inputPer1M: 0.30, outputPer1M: 1.20 },
|
|
743
|
+
"mistral-large-3:675b": { inputPer1M: 0.50, outputPer1M: 1.50 },
|
|
744
|
+
"nemotron-3-nano:30b": { inputPer1M: 0.50, outputPer1M: 1.50 },
|
|
745
|
+
"nemotron-3-super": { inputPer1M: 0.60, outputPer1M: 1.80 },
|
|
746
|
+
"nemotron-3-ultra": { inputPer1M: 0.60, outputPer1M: 1.80 },
|
|
747
|
+
};
|
|
748
|
+
|
|
749
|
+
// Which Ollama Cloud models respond on which subscription tiers, verified
|
|
750
|
+
// 2026-08-09 with minimal /api/chat probes against free-tier accounts
|
|
751
|
+
// (HTTP 200 vs 403). "free" = served on the free tier; "subscription" =
|
|
752
|
+
// the API returns "this model requires a subscription" until the account
|
|
753
|
+
// is upgraded. Sourced from ~/ollama-rotator.
|
|
754
|
+
export type ModelTierAccess = "free" | "subscription";
|
|
755
|
+
export const MODEL_TIER_ACCESS: Record<string, ModelTierAccess> = {
|
|
756
|
+
"gpt-oss:20b": "free",
|
|
757
|
+
"gpt-oss:120b": "free",
|
|
758
|
+
"gemma4:31b": "free",
|
|
759
|
+
"minimax-m3": "free",
|
|
760
|
+
"nemotron-3-nano:30b": "free",
|
|
761
|
+
"nemotron-3-super": "free",
|
|
762
|
+
"nemotron-3-ultra": "free",
|
|
763
|
+
"deepseek-v4-flash:0731": "subscription",
|
|
764
|
+
"deepseek-v4-flash:preview": "subscription",
|
|
765
|
+
"deepseek-v4-pro": "subscription",
|
|
766
|
+
"glm-5.1": "subscription",
|
|
767
|
+
"glm-5.2": "subscription",
|
|
768
|
+
"kimi-k2.6": "subscription",
|
|
769
|
+
"kimi-k2.7-code": "subscription",
|
|
770
|
+
"kimi-k3": "subscription",
|
|
771
|
+
"minimax-m2.7": "subscription",
|
|
772
|
+
"mistral-large-3:675b": "subscription",
|
|
773
|
+
"qwen3.5:397b": "subscription",
|
|
774
|
+
};
|
|
775
|
+
|
|
776
|
+
export const TOKEN_URL = "https://oauth2.googleapis.com/token";
|
|
777
|
+
|
|
778
|
+
/**
|
|
779
|
+
* Legacy compatibility client used by existing installations.
|
|
780
|
+
*
|
|
781
|
+
* Operator-provided ANTIGRAVITY_CLIENT_ID and ANTIGRAVITY_CLIENT_SECRET
|
|
782
|
+
* take precedence in src/oauth.ts. Keep this fallback only so upgrades do
|
|
783
|
+
* not break existing account refresh and login flows.
|
|
784
|
+
*/
|
|
785
|
+
export const CLIENT_ID = atob(
|
|
786
|
+
"MTA3MTAwNjA2MDU5MS10bWhzc2luMmgyMWxjcmUyMzV2dG9sb2poNGc0MDNlcC5hcHBzLmdvb2dsZXVzZXJjb250ZW50LmNvbQ==",
|
|
787
|
+
);
|
|
788
|
+
export const CLIENT_SECRET = atob(
|
|
789
|
+
"R09DU1BYLUs1OEZXUjQ4NkxkTEoxbUxCOHNYQzR6NnFEQWY=",
|
|
790
|
+
);
|
|
791
|
+
|
|
792
|
+
// Production default: use the daily Cloud Code Assist endpoint. The proxy
|
|
793
|
+
// forwarder supports cascading if additional verified endpoints are added here.
|
|
794
|
+
export const ANTIGRAVITY_ENDPOINTS = [
|
|
795
|
+
"https://daily-cloudcode-pa.googleapis.com",
|
|
796
|
+
] as const;
|
|
797
|
+
|
|
798
|
+
// Maps each quota pool key (Google quota API) to the cheapest upstream model
|
|
799
|
+
// used for kickstart warmup requests. Gemini 3.6/3.5 Flash and Gemini 3.1 Pro
|
|
800
|
+
// share the same upstream pool, so all map to gemini-3-flash.
|
|
801
|
+
export const KICKSTART_MODEL_FOR_QUOTA_POOL: Record<string, string> = {
|
|
802
|
+
"claude-opus-4-6-thinking": "gpt-oss-120b-medium",
|
|
803
|
+
"gemini-3.5-flash": "gemini-3-flash",
|
|
804
|
+
"gemini-3.6-flash": "gemini-3-flash",
|
|
805
|
+
"gemini-3.1-pro": "gemini-3-flash",
|
|
806
|
+
};
|
|
807
|
+
|
|
808
|
+
export const QUOTA_API_URL =
|
|
809
|
+
"https://daily-cloudcode-pa.googleapis.com/v1internal:fetchAvailableModels";
|
|
810
|
+
|
|
811
|
+
// ── Ollama Cloud endpoints ──────────────────────────────────────────
|
|
812
|
+
export const OLLAMA_API_BASE = "https://ollama.com/api";
|
|
813
|
+
export const OLLAMA_OPENAI_BASE = "https://ollama.com/v1";
|
|
814
|
+
export const OLLAMA_USAGE_URL = "https://ollama.com/api/usage";
|
|
815
|
+
export const OLLAMA_TAGS_URL = "https://ollama.com/api/tags";
|
|
816
|
+
// Mutable so tests can redirect to a local stub (same pattern as ANTIGRAVITY_ENDPOINTS).
|
|
817
|
+
export const OLLAMA_CHAT_ENDPOINTS = ["https://ollama.com/api/chat"];
|
|
818
|
+
export const OLLAMA_CHAT_URL = OLLAMA_CHAT_ENDPOINTS[0];
|
|
819
|
+
|
|
820
|
+
// User-Agent sent to ollama.com (defaults are spoofed per docs examples).
|
|
821
|
+
export const OLLAMA_USER_AGENT =
|
|
822
|
+
process.env.TUXEVIL_ROTATOR_OLLAMA_USER_AGENT ||
|
|
823
|
+
process.env.OLLAMA_ROTATOR_USER_AGENT ||
|
|
824
|
+
"ollama-rotator/1.0";
|
|
825
|
+
|
|
826
|
+
// TTL for the cached /api/tags listing (model catalog refresh).
|
|
827
|
+
export const TAGS_CACHE_TTL_MS = 5 * 60 * 1000;
|
|
828
|
+
export const ANTIGRAVITY_VERSION =
|
|
829
|
+
rotatorEnv("ANTIGRAVITY_VERSION") ||
|
|
830
|
+
process.env.PI_AI_ANTIGRAVITY_VERSION ||
|
|
831
|
+
"1.107.0";
|
|
832
|
+
export const QUOTA_USER_AGENT =
|
|
833
|
+
rotatorEnv("QUOTA_USER_AGENT") ||
|
|
834
|
+
`antigravity/${ANTIGRAVITY_VERSION} darwin/arm64`;
|
|
835
|
+
export const REQUEST_USER_AGENT =
|
|
836
|
+
rotatorEnv("REQUEST_USER_AGENT") || QUOTA_USER_AGENT;
|
|
837
|
+
export const REQUEST_GOOG_API_CLIENT =
|
|
838
|
+
rotatorEnv("X_GOOG_API_CLIENT") ||
|
|
839
|
+
"google-cloud-sdk vscode_cloudshelleditor/0.1";
|
|
840
|
+
export const REQUEST_CLIENT_METADATA =
|
|
841
|
+
rotatorEnv("CLIENT_METADATA") ||
|
|
842
|
+
JSON.stringify({
|
|
843
|
+
ideType: "ANTIGRAVITY",
|
|
844
|
+
platform: "MACOS",
|
|
845
|
+
pluginType: "GEMINI",
|
|
846
|
+
});
|
|
847
|
+
|
|
848
|
+
// ── Virtual Keys & Spend Logging ─────────────────────────────────────
|
|
849
|
+
|
|
850
|
+
export interface VirtualKey {
|
|
851
|
+
tokenHash: string;
|
|
852
|
+
keyName: string;
|
|
853
|
+
keyAlias: string;
|
|
854
|
+
userId?: string | null;
|
|
855
|
+
models?: string[];
|
|
856
|
+
metadata?: Record<string, unknown>;
|
|
857
|
+
blocked: boolean;
|
|
858
|
+
lastActive?: string | null;
|
|
859
|
+
createdAt: string;
|
|
860
|
+
createdBy?: string | null;
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
export interface SpendLog {
|
|
864
|
+
requestId: string;
|
|
865
|
+
apiKeyHash?: string | null;
|
|
866
|
+
keyAlias?: string | null;
|
|
867
|
+
keyName?: string | null;
|
|
868
|
+
model: string;
|
|
869
|
+
accountEmail?: string | null;
|
|
870
|
+
callType: string;
|
|
871
|
+
status: "success" | "failure";
|
|
872
|
+
promptTokens: number;
|
|
873
|
+
completionTokens: number;
|
|
874
|
+
totalTokens: number;
|
|
875
|
+
cost?: number;
|
|
876
|
+
startTime: string;
|
|
877
|
+
endTime: string;
|
|
878
|
+
ttfbMs?: number | null;
|
|
879
|
+
durationMs: number;
|
|
880
|
+
requestMessages?: unknown;
|
|
881
|
+
responseContent?: unknown;
|
|
882
|
+
metadata?: Record<string, unknown>;
|
|
883
|
+
requesterIp?: string | null;
|
|
884
|
+
createdAt?: string;
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
export interface DailySpend {
|
|
888
|
+
apiKeyHash?: string | null;
|
|
889
|
+
model: string;
|
|
890
|
+
date: string;
|
|
891
|
+
promptTokens: number;
|
|
892
|
+
completionTokens: number;
|
|
893
|
+
totalRequests: number;
|
|
894
|
+
successfulRequests: number;
|
|
895
|
+
failedRequests: number;
|
|
896
|
+
totalDurationMs: number;
|
|
897
|
+
}
|