tuxevil-rotator 4.2.0 → 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/CHANGELOG.md +28 -0
- package/README.md +11 -2
- package/package.json +3 -3
- package/plugins/opencode-discovery/index.d.mts +59 -0
- package/plugins/opencode-discovery/index.mjs +328 -0
- package/plugins/opencode-discovery/package.json +22 -0
- package/src/compat.ts +4 -5
- package/src/install-agent.ts +47 -20
- package/src/install-opencode.ts +138 -32
- package/src/providers/google-antigravity/catalog.ts +34 -163
- package/src/providers/google-antigravity/dynamic-catalog.ts +28 -7
- package/src/providers/google-antigravity/index.ts +12 -4
- package/src/providers/google-antigravity/quota.ts +13 -13
- package/src/providers/ollama/quota.ts +8 -6
- package/src/providers/opencode-zen/quota.ts +8 -6
- package/src/proxy.ts +35 -9
- package/src/rotator.ts +27 -28
- package/src/types.ts +1 -38
- package/plugins/opencode-discovery.js +0 -174
package/src/proxy.ts
CHANGED
|
@@ -14,6 +14,7 @@ import { Readable, type Duplex } from "node:stream";
|
|
|
14
14
|
import {
|
|
15
15
|
classifyEffortRoutingModel,
|
|
16
16
|
getEffortRoutingRule,
|
|
17
|
+
applyModelAlias,
|
|
17
18
|
resolveQuotaModelKey,
|
|
18
19
|
resolveDisplayModelKey,
|
|
19
20
|
} from "./types.js";
|
|
@@ -40,6 +41,7 @@ import {
|
|
|
40
41
|
extractUsageFromSseEvent,
|
|
41
42
|
GOOGLE_BENCHMARK_CONSTANTS,
|
|
42
43
|
} from "./providers/google-antigravity/forward.js";
|
|
44
|
+
import { isDynamicallyAvailableAntigravityModel } from "./providers/google-antigravity/catalog.js";
|
|
43
45
|
export { forwardRequest, SseEventAccumulator, extractUsageFromSseEvent };
|
|
44
46
|
import {
|
|
45
47
|
serveStatusApi,
|
|
@@ -649,9 +651,12 @@ async function handleUpstreamAccountAction(
|
|
|
649
651
|
uptimeSeconds: ctx401.uptimeSeconds,
|
|
650
652
|
timeSinceLastFlagSeconds: -1,
|
|
651
653
|
});
|
|
652
|
-
|
|
654
|
+
// Provider-scoped invalidation: only the failing provider is taken
|
|
655
|
+
// out of rotation; sibling providers on the same account remain active.
|
|
656
|
+
rotator.markProviderInvalid(
|
|
653
657
|
account,
|
|
654
|
-
|
|
658
|
+
provider.id,
|
|
659
|
+
`Provider credential rejected (401): ${action.errorText.slice(0, 300)}`,
|
|
655
660
|
);
|
|
656
661
|
logRequestEnd(401, `endpoint=${action.endpoint}`);
|
|
657
662
|
if (!options.canRetry) {
|
|
@@ -661,7 +666,7 @@ async function handleUpstreamAccountAction(
|
|
|
661
666
|
if (!nextAccount) {
|
|
662
667
|
return buildNoReplacementDecision(
|
|
663
668
|
options,
|
|
664
|
-
`no replacement account remained after ${label} was
|
|
669
|
+
`no replacement account remained after ${label} provider was invalidated with 401`,
|
|
665
670
|
);
|
|
666
671
|
}
|
|
667
672
|
return { kind: "retry" };
|
|
@@ -691,7 +696,14 @@ async function handleUpstreamAccountAction(
|
|
|
691
696
|
uptimeSeconds: ctx403.uptimeSeconds,
|
|
692
697
|
timeSinceLastFlagSeconds: -1,
|
|
693
698
|
});
|
|
694
|
-
|
|
699
|
+
// Provider-scoped invalidation: a 403 with flag patterns (suspended,
|
|
700
|
+
// banned, etc.) indicates the upstream provider rejected this credential.
|
|
701
|
+
// Only the failing provider is taken out; sibling providers remain active.
|
|
702
|
+
rotator.markProviderInvalid(
|
|
703
|
+
account,
|
|
704
|
+
provider.id,
|
|
705
|
+
`Provider flagged (403): ${action.errorText.slice(0, 300)}`,
|
|
706
|
+
);
|
|
695
707
|
if (!options.canRetry) {
|
|
696
708
|
return buildFailureDecision(options, 403, action.errorText);
|
|
697
709
|
}
|
|
@@ -699,7 +711,7 @@ async function handleUpstreamAccountAction(
|
|
|
699
711
|
if (!nextAccount) {
|
|
700
712
|
return buildNoReplacementDecision(
|
|
701
713
|
options,
|
|
702
|
-
`no replacement account remained after ${label} was
|
|
714
|
+
`no replacement account remained after ${label} provider was invalidated with 403`,
|
|
703
715
|
);
|
|
704
716
|
}
|
|
705
717
|
return { kind: "retry" };
|
|
@@ -1074,7 +1086,7 @@ export async function benchmarkAccount(
|
|
|
1074
1086
|
error: `${benchmarkProvider.displayName} is turned off on the Models page`,
|
|
1075
1087
|
};
|
|
1076
1088
|
if (turnedOff()) return turnedOffResult;
|
|
1077
|
-
rotator.startRequest(account, benchmarkSpec.body.model);
|
|
1089
|
+
rotator.startRequest(account, benchmarkSpec.body.model, benchmarkProvider.id);
|
|
1078
1090
|
const timeoutController = new AbortController();
|
|
1079
1091
|
const timeout = setTimeout(
|
|
1080
1092
|
() => timeoutController.abort(new Error("Benchmark timeout")),
|
|
@@ -1119,7 +1131,7 @@ export async function benchmarkAccount(
|
|
|
1119
1131
|
return benchmarkFailure(account, startedAt, formatError(err), ttfbMs);
|
|
1120
1132
|
} finally {
|
|
1121
1133
|
clearTimeout(timeout);
|
|
1122
|
-
rotator.finishRequest(account, benchmarkSpec.body.model);
|
|
1134
|
+
rotator.finishRequest(account, benchmarkSpec.body.model, benchmarkProvider.id);
|
|
1123
1135
|
}
|
|
1124
1136
|
}
|
|
1125
1137
|
|
|
@@ -1257,6 +1269,19 @@ export async function withRotation<T>(
|
|
|
1257
1269
|
ollamaModels: new Set(rotator.getOllamaModels?.() ?? []),
|
|
1258
1270
|
codexModels: new Set(rotator.getCodexModels?.() ?? []),
|
|
1259
1271
|
})?.id;
|
|
1272
|
+
const modelQuotaFamily = resolveQuotaModelKey(model) ??
|
|
1273
|
+
resolveQuotaModelKey(applyModelAlias(model));
|
|
1274
|
+
if (
|
|
1275
|
+
(providerId === DEFAULT_PROVIDER ||
|
|
1276
|
+
(!providerId && (modelQuotaFamily === "claude" || modelQuotaFamily === "gemini"))) &&
|
|
1277
|
+
!isDynamicallyAvailableAntigravityModel(model)
|
|
1278
|
+
) {
|
|
1279
|
+
return {
|
|
1280
|
+
ok: false,
|
|
1281
|
+
status: 404,
|
|
1282
|
+
errorText: `Model '${body.displayModel || model}' is not available in the active Antigravity model catalog`,
|
|
1283
|
+
};
|
|
1284
|
+
}
|
|
1260
1285
|
const requestIsOff = (selectedProviderId = providerId): boolean => isRequestOff(rotator, {
|
|
1261
1286
|
model,
|
|
1262
1287
|
requestedModel: body.requestedModel,
|
|
@@ -1325,7 +1350,7 @@ export async function withRotation<T>(
|
|
|
1325
1350
|
const releaseCurrentAccount = (): void => {
|
|
1326
1351
|
if (accountReleased) return;
|
|
1327
1352
|
accountReleased = true;
|
|
1328
|
-
rotator.finishRequest(account, modelKey);
|
|
1353
|
+
rotator.finishRequest(account, modelKey, providerId);
|
|
1329
1354
|
};
|
|
1330
1355
|
const rotateAndRelease = async (): Promise<AccountRuntime | null> => {
|
|
1331
1356
|
releaseCurrentAccount();
|
|
@@ -1845,7 +1870,7 @@ async function handleProxyRequest(
|
|
|
1845
1870
|
const releaseCurrentAccount = (): void => {
|
|
1846
1871
|
if (accountReleased) return;
|
|
1847
1872
|
accountReleased = true;
|
|
1848
|
-
rotator.finishRequest(account, modelKey);
|
|
1873
|
+
rotator.finishRequest(account, modelKey, providerId);
|
|
1849
1874
|
};
|
|
1850
1875
|
const rotateAndRelease = async (): Promise<AccountRuntime | null> => {
|
|
1851
1876
|
releaseCurrentAccount();
|
|
@@ -2199,6 +2224,7 @@ async function handleCodeAssistPassthrough(
|
|
|
2199
2224
|
rotator.finishRequest(
|
|
2200
2225
|
account,
|
|
2201
2226
|
resolveQuotaModelKey(CODE_ASSIST_ROUTING_MODEL) ?? undefined,
|
|
2227
|
+
DEFAULT_PROVIDER,
|
|
2202
2228
|
);
|
|
2203
2229
|
};
|
|
2204
2230
|
|
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 =
|
|
1203
|
-
|
|
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 =
|
|
1240
|
-
|
|
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
|
}
|
|
@@ -3712,13 +3710,13 @@ export class AccountRotator {
|
|
|
3712
3710
|
await this.ensureValidTokenForProvider(account, adapter.id);
|
|
3713
3711
|
} catch (err) {
|
|
3714
3712
|
const msg = err instanceof Error ? err.message : String(err);
|
|
3715
|
-
|
|
3716
|
-
|
|
3717
|
-
|
|
3718
|
-
|
|
3719
|
-
|
|
3720
|
-
this.
|
|
3721
|
-
throw new Error(
|
|
3713
|
+
// Provider-scoped invalidation: token refresh failures only affect the
|
|
3714
|
+
// provider whose token could not be refreshed, not the whole account.
|
|
3715
|
+
const reason = msg.startsWith("Token refresh failed") || msg.startsWith("Token refresh error:")
|
|
3716
|
+
? msg
|
|
3717
|
+
: `Token refresh error: ${msg}`;
|
|
3718
|
+
this.markProviderInvalid(account, adapter.id, reason);
|
|
3719
|
+
throw msg === reason ? err : new Error(reason, { cause: err });
|
|
3722
3720
|
}
|
|
3723
3721
|
}
|
|
3724
3722
|
|
|
@@ -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) ||
|
|
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
|
-
|
|
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
|
}
|
|
@@ -5142,8 +5138,11 @@ export class AccountRotator {
|
|
|
5142
5138
|
if (target.providerId === "openai-codex") {
|
|
5143
5139
|
this.markProviderInvalid(account, target.providerId, failure.description);
|
|
5144
5140
|
} else if (failure.accountEnforcement) {
|
|
5141
|
+
// Provider-scoped invalidation: even with account-enforcement language
|
|
5142
|
+
// in the response, only the specific provider credential is invalidated.
|
|
5143
|
+
// Sibling providers on the same account remain routable.
|
|
5145
5144
|
this.reportQuotaPollFlag(account, response.status, errorText, upstreamModel);
|
|
5146
|
-
this.
|
|
5145
|
+
this.markProviderInvalid(account, target.providerId, failure.description);
|
|
5147
5146
|
} else {
|
|
5148
5147
|
account.lastError = failure.description;
|
|
5149
5148
|
this.kickstartDeniedUntil.set(denialKey, Date.now() + 30 * 60 * 1000);
|
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;
|
|
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
|
-
}
|