tuxevil-rotator 4.2.1 → 4.2.2

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/src/rotator.ts CHANGED
@@ -27,7 +27,6 @@ import {
27
27
  DEFAULT_QUOTA_POLL_INTERVAL_MS,
28
28
  MAX_QUOTA_POLL_INTERVAL_MS,
29
29
  MIN_QUOTA_POLL_INTERVAL_MS,
30
- isStaticAntigravityModel,
31
30
  resolveQuotaModelKey,
32
31
  resolveDisplayModelKey,
33
32
  } from "./types.js";
@@ -1199,9 +1198,8 @@ export class AccountRotator {
1199
1198
 
1200
1199
  // Get quota % for a specific model on an account. Returns -1 if no data.
1201
1200
  private getModelQuota(account: AccountRuntime, modelKey: string): number {
1202
- const quotaKey = isStaticAntigravityModel(modelKey)
1203
- ? modelKey
1204
- : dynamicCatalog.resolveQuotaPool(modelKey) ?? modelKey;
1201
+ const quotaKey = dynamicCatalog.resolveQuotaPool(modelKey) ??
1202
+ resolveQuotaModelKey(modelKey) ?? modelKey;
1205
1203
  const q = account.quota.find((q) => q.modelKey === quotaKey) ??
1206
1204
  (quotaKey.startsWith(`${CODEX_QUOTA_MODEL_KEY}:`)
1207
1205
  ? account.quota.find((candidate) => candidate.modelKey === CODEX_QUOTA_MODEL_KEY)
@@ -1236,9 +1234,8 @@ export class AccountRotator {
1236
1234
  account: AccountRuntime,
1237
1235
  modelKey: string,
1238
1236
  ): ModelQuota["timerType"] {
1239
- const quotaKey = isStaticAntigravityModel(modelKey)
1240
- ? modelKey
1241
- : dynamicCatalog.resolveQuotaPool(modelKey) ?? modelKey;
1237
+ const quotaKey = dynamicCatalog.resolveQuotaPool(modelKey) ??
1238
+ resolveQuotaModelKey(modelKey) ?? modelKey;
1242
1239
  const q = account.quota.find((q) => q.modelKey === quotaKey) ??
1243
1240
  (quotaKey.startsWith(`${CODEX_QUOTA_MODEL_KEY}:`)
1244
1241
  ? account.quota.find((candidate) => candidate.modelKey === CODEX_QUOTA_MODEL_KEY)
@@ -2204,6 +2201,7 @@ export class AccountRotator {
2204
2201
  waiter.model
2205
2202
  ? this.resolveRequestPoolKey(waiter.model, waiter.providerId)
2206
2203
  : undefined,
2204
+ waiter.providerId,
2207
2205
  );
2208
2206
  }
2209
2207
  progressed = true;
@@ -2412,7 +2410,7 @@ export class AccountRotator {
2412
2410
  }
2413
2411
  }
2414
2412
  }
2415
- this.startRequest(current, modelKey ?? undefined);
2413
+ this.startRequest(current, modelKey ?? undefined, providerId);
2416
2414
  if (modelKey && state) {
2417
2415
  state.requestsOnActiveAccount++;
2418
2416
  this.scheduleStateSave();
@@ -2426,7 +2424,7 @@ export class AccountRotator {
2426
2424
  this.scheduleStateSave();
2427
2425
  }
2428
2426
  this.refundTokenBucket(current, Date.now());
2429
- this.finishRequest(current, modelKey ?? undefined);
2427
+ this.finishRequest(current, modelKey ?? undefined, providerId);
2430
2428
  if (modelKey && this.isCodexPoolKey(modelKey)) {
2431
2429
  return this.rotateModelForRequest(modelKey, now, idx);
2432
2430
  }
@@ -2598,7 +2596,7 @@ export class AccountRotator {
2598
2596
  forRequest = false,
2599
2597
  ): Promise<AccountRuntime> {
2600
2598
  if (forRequest) {
2601
- this.startRequest(account, modelKey);
2599
+ this.startRequest(account, modelKey, getProviderIdForPoolKey(modelKey));
2602
2600
  }
2603
2601
  const newIdx = this.accounts.indexOf(account);
2604
2602
  const quota = this.getModelQuota(account, modelKey);
@@ -2621,7 +2619,7 @@ export class AccountRotator {
2621
2619
  state.requestsOnActiveAccount = Math.max(0, state.requestsOnActiveAccount - 1);
2622
2620
  }
2623
2621
  this.refundTokenBucket(account, Date.now());
2624
- this.finishRequest(account, modelKey);
2622
+ this.finishRequest(account, modelKey, getProviderIdForPoolKey(modelKey));
2625
2623
  }
2626
2624
  throw err;
2627
2625
  }
@@ -3939,12 +3937,13 @@ export class AccountRotator {
3939
3937
  this.scheduleStateSave();
3940
3938
  }
3941
3939
 
3942
- startRequest(account: AccountRuntime, modelKey?: string): void {
3940
+ startRequest(account: AccountRuntime, modelKey?: string, providerId?: string): void {
3943
3941
  const key = modelKey ?? "__default__";
3942
+ const requestProviderId = providerId ?? getProviderIdForPoolKey(key);
3944
3943
  // Ollama Cloud imposes no per-account concurrency limit, so its
3945
3944
  // requests must not activate in-flight tracking at all (the monthly
3946
3945
  // pool and raw ollama model names from benchmark probes).
3947
- if (isOllamaQuotaPoolKey(key) || this.ollamaModels.has(key)) {
3946
+ if (isOllamaQuotaPoolKey(key) || requestProviderId === "ollama") {
3948
3947
  this.consumeTokenBucket(account, Date.now());
3949
3948
  return;
3950
3949
  }
@@ -3954,9 +3953,10 @@ export class AccountRotator {
3954
3953
  this.consumeTokenBucket(account, Date.now());
3955
3954
  }
3956
3955
 
3957
- finishRequest(account: AccountRuntime, modelKey?: string): void {
3956
+ finishRequest(account: AccountRuntime, modelKey?: string, providerId?: string): void {
3958
3957
  const key = modelKey ?? "__default__";
3959
- if (isOllamaQuotaPoolKey(key) || this.ollamaModels.has(key)) return;
3958
+ const requestProviderId = providerId ?? getProviderIdForPoolKey(key);
3959
+ if (isOllamaQuotaPoolKey(key) || requestProviderId === "ollama") return;
3960
3960
  account.inFlightByModel[key] = Math.max(
3961
3961
  0,
3962
3962
  (account.inFlightByModel[key] ?? 0) - 1,
@@ -3987,7 +3987,6 @@ export class AccountRotator {
3987
3987
  ): { reason: RoutingRejectionReason; detail: string } | null {
3988
3988
  const accountId = getAccountIdentity(account);
3989
3989
  if (
3990
- !isStaticAntigravityModel(modelKey) &&
3991
3990
  dynamicCatalog.hasOwnershipForModel(modelKey) &&
3992
3991
  !dynamicCatalog.hasModelForAccount(accountId, modelKey)
3993
3992
  ) {
@@ -4059,10 +4058,7 @@ export class AccountRotator {
4059
4058
  // capture a request that is pinned or routed to another provider.
4060
4059
  const provider = providerId ?? routedProviderId(this, model);
4061
4060
  if (!provider || provider === DEFAULT_PROVIDER) {
4062
- if (
4063
- !isStaticAntigravityModel(model) &&
4064
- dynamicCatalog.wasDiscovered(model)
4065
- ) return normalizedModel;
4061
+ if (dynamicCatalog.wasDiscovered(model)) return normalizedModel;
4066
4062
  if (this.hasRelevantQuotaStateForModel(normalizedModel, Date.now())) {
4067
4063
  return normalizedModel;
4068
4064
  }
package/src/types.ts CHANGED
@@ -363,55 +363,18 @@ export interface ModelQuota {
363
363
  // variants (and gpt-oss) share one bucket, all Gemini variants share one.
364
364
  export const QUOTA_MODEL_KEYS: Record<
365
365
  string,
366
- { key: string; altKeys: string[]; display: string }
366
+ { key: string; display: string }
367
367
  > = {
368
368
  claude: {
369
369
  key: "claude",
370
- altKeys: [
371
- "claude-opus-4-6-thinking",
372
- "claude-opus-4-5-thinking",
373
- "claude-opus-4-5",
374
- "claude-sonnet-4-6-thinking",
375
- "claude-sonnet-4-6",
376
- "claude-sonnet-4-5-thinking",
377
- "claude-sonnet-4-5",
378
- "gpt-oss-120b-medium",
379
- "gpt-oss-120b",
380
- ],
381
370
  display: "Claude",
382
371
  },
383
372
  gemini: {
384
373
  key: "gemini",
385
- altKeys: [
386
- "gemini-3.1-pro",
387
- "gemini-3.1-pro-low",
388
- "gemini-3.1-pro-high",
389
- "gemini-3-pro-high",
390
- "gemini-3-pro-low",
391
- "gemini-3-flash-agent",
392
- "gemini-3-flash",
393
- "gemini-3.6-flash",
394
- "gemini-3.6-flash-high",
395
- "gemini-3.6-flash-medium",
396
- "gemini-3.6-flash-low",
397
- "gemini-3.6-flash-tiered",
398
- "gemini-3.7-flash-tiered",
399
- "gemini-3.8-flash-low",
400
- "gemini-3.8-flash-medium",
401
- "gemini-3.8-flash-high",
402
- ],
403
374
  display: "Gemini",
404
375
  },
405
376
  };
406
377
 
407
- const STATIC_ANTIGRAVITY_MODEL_IDS = new Set(
408
- Object.values(QUOTA_MODEL_KEYS).flatMap(({ altKeys }) => altKeys),
409
- );
410
-
411
- export function isStaticAntigravityModel(model: string): boolean {
412
- return STATIC_ANTIGRAVITY_MODEL_IDS.has(model.toLowerCase());
413
- }
414
-
415
378
  // Map request model names to quota model keys (family buckets).
416
379
  export function resolveQuotaModelKey(requestModel: string): string | null {
417
380
  const lower = requestModel.toLowerCase();
@@ -1,174 +0,0 @@
1
- /**
2
- * OpenCode Model Discovery Plugin for tuxevil-rotator
3
- *
4
- * Automatically fetches the active model list from a tuxevil-rotator instance
5
- * (or any compatible endpoint) and injects the models with their limits and capabilities
6
- * into the OpenCode configuration at startup.
7
- */
8
-
9
- /**
10
- * Resolves the configuration options for the discovery plugin.
11
- * Precedence:
12
- * 1. Explicit plugin options passed in opencode.json
13
- * 2. Environment variables (TUXEVIL_ROTATOR_BASE_URL, ROTATOR_URL)
14
- * 3. Existing provider with baseURL matching rotator port or localhost in config
15
- * 4. Default: http://127.0.0.1:51200/v1
16
- */
17
- export function resolvePluginOptions(config = {}, options = {}) {
18
- const envBaseURL = process.env.TUXEVIL_ROTATOR_BASE_URL || process.env.ROTATOR_URL;
19
-
20
- let discoveredBaseURL = options.baseURL || envBaseURL;
21
- let providerID = options.providerID;
22
- let providerName = options.providerName;
23
-
24
- if (!discoveredBaseURL && config.provider) {
25
- for (const [id, prov] of Object.entries(config.provider)) {
26
- const url = prov?.options?.baseURL;
27
- if (typeof url === "string" && (url.includes("51200") || url.includes("rotator") || url.includes("antigravity"))) {
28
- discoveredBaseURL = url;
29
- if (!providerID) providerID = id;
30
- if (!providerName && prov.name) providerName = prov.name;
31
- break;
32
- }
33
- }
34
- }
35
-
36
- if (!discoveredBaseURL) {
37
- discoveredBaseURL = "http://127.0.0.1:51200/v1";
38
- }
39
-
40
- // Normalize baseURL (remove trailing slashes)
41
- discoveredBaseURL = discoveredBaseURL.replace(/\/+$/, "");
42
-
43
- if (!providerID) {
44
- providerID = options.providerID || "antigravity";
45
- }
46
-
47
- if (!providerName) {
48
- providerName = options.providerName || "Antigravity (rotator)";
49
- }
50
-
51
- const timeoutMs = Number(options.timeoutMs) || 3500;
52
-
53
- return {
54
- baseURL: discoveredBaseURL,
55
- providerID,
56
- providerName,
57
- timeoutMs,
58
- };
59
- }
60
-
61
- /**
62
- * Maps raw model object from rotator /v1/models to OpenCode model definition.
63
- */
64
- export function mapModelToOpenCode(rawModel) {
65
- const id = rawModel.id;
66
- const meta = rawModel.meta || {};
67
-
68
- const contextLimit = Number(meta.context_length) || Number(rawModel.context_window) || Number(rawModel.max_model_len) || 128000;
69
- const outputLimit = Number(meta.max_output_tokens) || 8192;
70
-
71
- const isMultimodal = Boolean(meta.multimodal);
72
- const toolCalling = meta.tool_calling !== false;
73
-
74
- const modelDef = {
75
- name: id,
76
- limit: {
77
- context: contextLimit,
78
- output: outputLimit,
79
- },
80
- capabilities: {
81
- tools: toolCalling,
82
- input: isMultimodal ? ["text", "image"] : ["text"],
83
- output: ["text"],
84
- },
85
- };
86
-
87
- return { id, modelDef };
88
- }
89
-
90
- /**
91
- * Fetches available models from the target rotator endpoint.
92
- */
93
- export async function fetchRotatorModels(baseURL, timeoutMs = 3500) {
94
- const modelsUrl = `${baseURL}/models`;
95
- const controller = new AbortController();
96
- const timer = setTimeout(() => controller.abort(), timeoutMs);
97
-
98
- try {
99
- const res = await fetch(modelsUrl, {
100
- signal: controller.signal,
101
- headers: {
102
- Accept: "application/json",
103
- },
104
- });
105
-
106
- if (!res.ok) {
107
- throw new Error(`Endpoint returned status ${res.status}: ${res.statusText}`);
108
- }
109
-
110
- const json = await res.json();
111
- if (!json || !Array.isArray(json.data)) {
112
- throw new Error("Invalid response format: 'data' array not found");
113
- }
114
-
115
- return json.data;
116
- } finally {
117
- clearTimeout(timer);
118
- }
119
- }
120
-
121
- /**
122
- * OpenCode Plugin Entrypoint
123
- */
124
- export default async function RotatorDiscoveryPlugin(_input, options = {}) {
125
- return {
126
- config: async (config) => {
127
- try {
128
- const resolved = resolvePluginOptions(config, options);
129
- const rawModels = await fetchRotatorModels(resolved.baseURL, resolved.timeoutMs);
130
-
131
- if (!config.provider) {
132
- config.provider = {};
133
- }
134
-
135
- const existingProvider = config.provider[resolved.providerID] || {};
136
- const models = { ...(existingProvider.models || {}) };
137
-
138
- for (const raw of rawModels) {
139
- if (!raw || !raw.id) continue;
140
- const { id, modelDef } = mapModelToOpenCode(raw);
141
- // Preserve any user-customized options or limits while merging capabilities
142
- models[id] = {
143
- ...modelDef,
144
- ...(models[id] || {}),
145
- capabilities: {
146
- ...modelDef.capabilities,
147
- ...(models[id]?.capabilities || {}),
148
- },
149
- limit: {
150
- ...modelDef.limit,
151
- ...(models[id]?.limit || {}),
152
- },
153
- };
154
- }
155
-
156
- config.provider[resolved.providerID] = {
157
- npm: "@ai-sdk/openai-compatible",
158
- name: resolved.providerName,
159
- ...existingProvider,
160
- options: {
161
- baseURL: resolved.baseURL,
162
- ...(existingProvider.options || {}),
163
- },
164
- models,
165
- };
166
- } catch (err) {
167
- // Safe degrade: do not fail OpenCode startup if rotator is unreachable
168
- if (process.env.DEBUG || process.env.NODE_ENV === "development") {
169
- console.warn("[rotator-discovery-plugin] Could not auto-discover models:", err?.message || err);
170
- }
171
- }
172
- },
173
- };
174
- }