tuxevil-rotator 3.7.2 → 3.8.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 +10 -0
- package/README.md +3 -1
- package/package.json +2 -1
- package/src/auto-routing.ts +370 -0
- package/src/compat.ts +292 -44
- package/src/config-defaults.ts +13 -0
- package/src/dashboard.ts +2 -2
- package/src/db-store.ts +9 -3
- package/src/key-auth.ts +15 -15
- package/src/onboarding.ts +174 -2
- package/src/providers/google-antigravity/forward.ts +8 -1
- package/src/providers/openai-codex/compat.ts +33 -5
- package/src/providers/registry.ts +12 -1
- package/src/proxy.ts +98 -19
- package/src/response-headers.ts +21 -0
- package/src/rotator.ts +108 -33
- package/src/token-encryption.ts +91 -0
- package/src/types.ts +23 -0
- package/src/validators.ts +38 -0
package/src/compat.ts
CHANGED
|
@@ -10,7 +10,11 @@ import {
|
|
|
10
10
|
type CompressionStats,
|
|
11
11
|
} from "./compression/index.js";
|
|
12
12
|
import { idempotencyManager } from "./idempotency.js";
|
|
13
|
-
import {
|
|
13
|
+
import {
|
|
14
|
+
authenticateVirtualKey,
|
|
15
|
+
isVirtualKeyModelAllowed,
|
|
16
|
+
sendAuthErrorResponse,
|
|
17
|
+
} from "./key-auth.js";
|
|
14
18
|
import { logSpend } from "./spend-logger.js";
|
|
15
19
|
import { hashKey } from "./virtual-keys.js";
|
|
16
20
|
import { PayloadTooLargeError, readLimitedBody } from "./body-limit.js";
|
|
@@ -100,6 +104,11 @@ import {
|
|
|
100
104
|
getCodexModels,
|
|
101
105
|
} from "./providers/openai-codex/catalog.js";
|
|
102
106
|
import { serveCodexChat, serveCodexResponses } from "./providers/openai-codex/compat.js";
|
|
107
|
+
import {
|
|
108
|
+
selectAutoRoutingTarget,
|
|
109
|
+
type AutoRoutingCatalogEntry,
|
|
110
|
+
type AutoRoutingRoute,
|
|
111
|
+
} from "./auto-routing.js";
|
|
103
112
|
|
|
104
113
|
function isCodexModelForRotator(rotator: AccountRotator, model: string): boolean {
|
|
105
114
|
if (isCodexRequestModel(model)) return true;
|
|
@@ -111,6 +120,109 @@ function isCodexModelForRotator(rotator: AccountRotator, model: string): boolean
|
|
|
111
120
|
}
|
|
112
121
|
}
|
|
113
122
|
|
|
123
|
+
function isAutoModel(model: string): boolean {
|
|
124
|
+
return model.trim().toLowerCase() === "auto";
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function authorizeResolvedAutoModel(
|
|
128
|
+
res: ServerResponse,
|
|
129
|
+
auth: Awaited<ReturnType<typeof authenticateVirtualKey>>,
|
|
130
|
+
model: string,
|
|
131
|
+
): boolean {
|
|
132
|
+
if (isVirtualKeyModelAllowed(auth.key, model)) return true;
|
|
133
|
+
sendAuthErrorResponse(res, {
|
|
134
|
+
authenticated: false,
|
|
135
|
+
key: auth.key,
|
|
136
|
+
rawKey: auth.rawKey,
|
|
137
|
+
error: `Model '${model}' is not allowed for this Virtual Key`,
|
|
138
|
+
statusCode: 403,
|
|
139
|
+
});
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function buildAutoRoutingCatalog(
|
|
144
|
+
rotator: AccountRotator,
|
|
145
|
+
route: AutoRoutingRoute,
|
|
146
|
+
): AutoRoutingCatalogEntry[] {
|
|
147
|
+
const catalog: AutoRoutingCatalogEntry[] = getEffectiveAntigravityModels()
|
|
148
|
+
.filter((model) => !["whisper-1", "proactive-observer", "proactive-observer-v10", "models/proactive-observer-v10"].includes(model.id.toLowerCase()))
|
|
149
|
+
.map((model) => ({
|
|
150
|
+
providerId: "google-antigravity",
|
|
151
|
+
modelId: model.id,
|
|
152
|
+
contextWindow: model.ctx,
|
|
153
|
+
multimodal: model.multimodal,
|
|
154
|
+
tools: model.tools,
|
|
155
|
+
reasoning: model.isThinking,
|
|
156
|
+
family: model.family,
|
|
157
|
+
}));
|
|
158
|
+
if (route === "gemini") return catalog;
|
|
159
|
+
|
|
160
|
+
const codexCatalog = new Map(getCodexModels().map((model) => [model.id, model]));
|
|
161
|
+
for (const modelId of route === "anthropic" ? [] : rotator.getCodexModels?.() ?? []) {
|
|
162
|
+
const model = codexCatalog.get(modelId);
|
|
163
|
+
catalog.push({
|
|
164
|
+
providerId: "openai-codex",
|
|
165
|
+
modelId,
|
|
166
|
+
contextWindow: model?.contextWindow ?? 1_050_000,
|
|
167
|
+
multimodal: model?.multimodal ?? true,
|
|
168
|
+
tools: model?.tools ?? true,
|
|
169
|
+
reasoning: model?.reasoning ?? true,
|
|
170
|
+
family: "openai-codex",
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
for (const modelId of rotator.getOllamaModels?.() ?? []) {
|
|
174
|
+
catalog.push({
|
|
175
|
+
providerId: "ollama",
|
|
176
|
+
modelId,
|
|
177
|
+
contextWindow: 128_000,
|
|
178
|
+
multimodal: false,
|
|
179
|
+
tools: true,
|
|
180
|
+
reasoning: false,
|
|
181
|
+
responses: true,
|
|
182
|
+
family: "ollama-cloud",
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
for (const model of OPENCODE_ZEN_CATALOG) {
|
|
186
|
+
catalog.push({
|
|
187
|
+
providerId: OPENCODE_ZEN_PROVIDER_ID,
|
|
188
|
+
modelId: model.id,
|
|
189
|
+
contextWindow: model.contextWindow,
|
|
190
|
+
multimodal: false,
|
|
191
|
+
tools: true,
|
|
192
|
+
reasoning: false,
|
|
193
|
+
responses: isOpenCodeZenResponsesModel(model.id),
|
|
194
|
+
family: "opencode-zen",
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
return catalog;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
async function resolveAutoModel(
|
|
201
|
+
rotator: AccountRotator,
|
|
202
|
+
route: AutoRoutingRoute,
|
|
203
|
+
input: unknown,
|
|
204
|
+
): Promise<{
|
|
205
|
+
model: string;
|
|
206
|
+
providerId: string;
|
|
207
|
+
mode: "typesafe" | "deterministic";
|
|
208
|
+
confidence?: number;
|
|
209
|
+
reason: string;
|
|
210
|
+
} | null> {
|
|
211
|
+
const selection = await selectAutoRoutingTarget(
|
|
212
|
+
rotator,
|
|
213
|
+
buildAutoRoutingCatalog(rotator, route),
|
|
214
|
+
{ route, input, requestedModel: "auto" },
|
|
215
|
+
);
|
|
216
|
+
if (!selection) return null;
|
|
217
|
+
return {
|
|
218
|
+
model: selection.candidate.modelId,
|
|
219
|
+
providerId: selection.candidate.providerId,
|
|
220
|
+
mode: selection.mode,
|
|
221
|
+
confidence: selection.confidence,
|
|
222
|
+
reason: selection.reason,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
114
226
|
export {
|
|
115
227
|
isRecord,
|
|
116
228
|
sanitizeGeminiSchema,
|
|
@@ -658,6 +770,11 @@ export async function streamCompatSse(
|
|
|
658
770
|
const rotatorHeaders = buildRotatorResponseHeaders({
|
|
659
771
|
accountLabel: context?.label,
|
|
660
772
|
model,
|
|
773
|
+
requestedModel: context?.requestedModel,
|
|
774
|
+
selectedProvider: context?.providerId,
|
|
775
|
+
modelSelection: context?.selectionMode,
|
|
776
|
+
selectionConfidence: context?.selectionConfidence,
|
|
777
|
+
selectionReason: context?.selectionReason,
|
|
661
778
|
ttfbMs: Date.now() - (context?.requestStartMs ?? streamStartMs),
|
|
662
779
|
healthScore: context?.account?.healthScore,
|
|
663
780
|
routingPolicy: rotator?.getConfig?.()?.routingPolicy || "timer-first",
|
|
@@ -1386,6 +1503,11 @@ export async function streamResponsesSse(
|
|
|
1386
1503
|
const rotatorHeaders = buildRotatorResponseHeaders({
|
|
1387
1504
|
accountLabel: context?.label,
|
|
1388
1505
|
model: request.model,
|
|
1506
|
+
requestedModel: context?.requestedModel,
|
|
1507
|
+
selectedProvider: context?.providerId,
|
|
1508
|
+
modelSelection: context?.selectionMode,
|
|
1509
|
+
selectionConfidence: context?.selectionConfidence,
|
|
1510
|
+
selectionReason: context?.selectionReason,
|
|
1389
1511
|
ttfbMs: Date.now() - (context?.requestStartMs ?? streamStartMs),
|
|
1390
1512
|
healthScore: context?.account?.healthScore,
|
|
1391
1513
|
routingPolicy: rotator?.getConfig?.()?.routingPolicy || "timer-first",
|
|
@@ -2113,9 +2235,9 @@ async function completeResponsesViaRotator(
|
|
|
2113
2235
|
context,
|
|
2114
2236
|
rotator,
|
|
2115
2237
|
options?.compressionStats,
|
|
2116
|
-
isOpenCodeZenResponsesModel(body.model)
|
|
2238
|
+
body.providerId === OPENCODE_ZEN_PROVIDER_ID || isOpenCodeZenResponsesModel(body.model)
|
|
2117
2239
|
? "opencode-zen"
|
|
2118
|
-
: (rotator?.getOllamaModels?.() ?? []).includes(body.model)
|
|
2240
|
+
: body.providerId === "ollama" || (rotator?.getOllamaModels?.() ?? []).includes(body.model)
|
|
2119
2241
|
? "ollama"
|
|
2120
2242
|
: "google",
|
|
2121
2243
|
);
|
|
@@ -2190,7 +2312,7 @@ async function completeViaRotator(
|
|
|
2190
2312
|
compressionStats?: CompressionStats | null;
|
|
2191
2313
|
}> {
|
|
2192
2314
|
const ollamaModels = rotator?.getOllamaModels?.() ?? [];
|
|
2193
|
-
const isOllamaUpstream = (model: string): boolean => ollamaModels.includes(model);
|
|
2315
|
+
const isOllamaUpstream = (model: string): boolean => body.providerId === "ollama" || ollamaModels.includes(model);
|
|
2194
2316
|
const cfg = typeof rotator?.getConfig === "function" ? rotator.getConfig() : undefined;
|
|
2195
2317
|
const enabled = cfg?.idempotencyEnabled === true;
|
|
2196
2318
|
const windowMs = cfg?.idempotencyWindowMs ?? 2000;
|
|
@@ -2210,7 +2332,7 @@ async function completeViaRotator(
|
|
|
2210
2332
|
async (response, context) => {
|
|
2211
2333
|
if (streamMode === "none") {
|
|
2212
2334
|
const raw = await response.text();
|
|
2213
|
-
const completion = isOpenCodeZenModel(body.model)
|
|
2335
|
+
const completion = body.providerId === OPENCODE_ZEN_PROVIDER_ID || isOpenCodeZenModel(body.model)
|
|
2214
2336
|
? parseOpenAiJson(raw)
|
|
2215
2337
|
: isOllamaUpstream(body.model)
|
|
2216
2338
|
? parseOllamaNdjson(raw)
|
|
@@ -2237,12 +2359,14 @@ async function completeViaRotator(
|
|
|
2237
2359
|
response.body,
|
|
2238
2360
|
req,
|
|
2239
2361
|
res,
|
|
2240
|
-
body.displayModel
|
|
2362
|
+
body.displayModel?.toLowerCase() === "auto"
|
|
2363
|
+
? body.model
|
|
2364
|
+
: body.displayModel || body.model,
|
|
2241
2365
|
streamMode,
|
|
2242
2366
|
context,
|
|
2243
2367
|
rotator,
|
|
2244
2368
|
options?.compressionStats,
|
|
2245
|
-
isOpenCodeZenModel(body.model)
|
|
2369
|
+
body.providerId === OPENCODE_ZEN_PROVIDER_ID || isOpenCodeZenModel(body.model)
|
|
2246
2370
|
? "opencode-zen"
|
|
2247
2371
|
: isOllamaUpstream(body.model)
|
|
2248
2372
|
? "ollama"
|
|
@@ -2768,16 +2892,37 @@ export async function handleGeminiGenerateContent(
|
|
|
2768
2892
|
error: { message: "Model path is required", status: "INVALID_ARGUMENT" },
|
|
2769
2893
|
});
|
|
2770
2894
|
|
|
2771
|
-
const
|
|
2895
|
+
const autoRequested = isAutoModel(model);
|
|
2896
|
+
const auth = await authenticateVirtualKey(req, autoRequested ? undefined : model);
|
|
2772
2897
|
if (!auth.authenticated) {
|
|
2773
2898
|
sendAuthErrorResponse(res, auth);
|
|
2774
2899
|
return;
|
|
2775
2900
|
}
|
|
2776
2901
|
const apiKeyHash = auth.key?.tokenHash || (auth.rawKey ? hashKey(auth.rawKey) : null);
|
|
2777
2902
|
|
|
2903
|
+
const autoSelection = autoRequested
|
|
2904
|
+
? await resolveAutoModel(rotator, "gemini", parsed)
|
|
2905
|
+
: null;
|
|
2906
|
+
if (autoRequested && !autoSelection) {
|
|
2907
|
+
return writeJson(res, 503, {
|
|
2908
|
+
error: { message: "No compatible model is currently available", status: "UNAVAILABLE" },
|
|
2909
|
+
});
|
|
2910
|
+
}
|
|
2911
|
+
if (autoSelection && !authorizeResolvedAutoModel(res, auth, autoSelection.model)) return;
|
|
2912
|
+
const selectedModel = autoSelection?.model ?? model;
|
|
2913
|
+
|
|
2778
2914
|
const body: RequestBody = {
|
|
2779
|
-
model,
|
|
2915
|
+
model: selectedModel,
|
|
2780
2916
|
project: "",
|
|
2917
|
+
...(autoRequested
|
|
2918
|
+
? {
|
|
2919
|
+
displayModel: model,
|
|
2920
|
+
providerId: autoSelection?.providerId,
|
|
2921
|
+
selectionMode: autoSelection?.mode,
|
|
2922
|
+
selectionConfidence: autoSelection?.confidence,
|
|
2923
|
+
selectionReason: autoSelection?.reason,
|
|
2924
|
+
}
|
|
2925
|
+
: {}),
|
|
2781
2926
|
request: {
|
|
2782
2927
|
contents: Array.isArray(parsed.contents) ? parsed.contents : [],
|
|
2783
2928
|
systemInstruction: parsed.systemInstruction,
|
|
@@ -2811,7 +2956,12 @@ export async function handleGeminiGenerateContent(
|
|
|
2811
2956
|
const ttfbMs = result.completion.firstByteMs ?? totalMs;
|
|
2812
2957
|
const rotatorHeaders = buildRotatorResponseHeaders({
|
|
2813
2958
|
accountLabel: result.context?.label,
|
|
2814
|
-
model: model,
|
|
2959
|
+
model: autoRequested ? result.context?.selectedModel ?? selectedModel : model,
|
|
2960
|
+
requestedModel: result.context?.requestedModel,
|
|
2961
|
+
selectedProvider: result.context?.providerId,
|
|
2962
|
+
modelSelection: result.context?.selectionMode,
|
|
2963
|
+
selectionConfidence: result.context?.selectionConfidence,
|
|
2964
|
+
selectionReason: result.context?.selectionReason,
|
|
2815
2965
|
latencyMs: totalMs,
|
|
2816
2966
|
ttfbMs,
|
|
2817
2967
|
inputTokens: result.completion.inputTokens,
|
|
@@ -2872,19 +3022,39 @@ export async function handleOpenAIChatCompletions(
|
|
|
2872
3022
|
},
|
|
2873
3023
|
});
|
|
2874
3024
|
|
|
2875
|
-
const
|
|
3025
|
+
const requestedModel = validation.value.model;
|
|
3026
|
+
const autoRequested = isAutoModel(requestedModel);
|
|
3027
|
+
const auth = await authenticateVirtualKey(req, autoRequested ? undefined : requestedModel);
|
|
2876
3028
|
if (!auth.authenticated) {
|
|
2877
3029
|
sendAuthErrorResponse(res, auth);
|
|
2878
3030
|
return;
|
|
2879
3031
|
}
|
|
2880
3032
|
const apiKeyHash = auth.key?.tokenHash || (auth.rawKey ? hashKey(auth.rawKey) : null);
|
|
2881
3033
|
|
|
2882
|
-
|
|
2883
|
-
await
|
|
3034
|
+
const autoSelection = autoRequested
|
|
3035
|
+
? await resolveAutoModel(rotator, "openai-chat", validation.value)
|
|
3036
|
+
: null;
|
|
3037
|
+
if (autoRequested && !autoSelection) {
|
|
3038
|
+
return writeJson(res, 503, {
|
|
3039
|
+
error: { message: "No compatible model is currently available", type: "upstream_error" },
|
|
3040
|
+
});
|
|
3041
|
+
}
|
|
3042
|
+
if (autoSelection && !authorizeResolvedAutoModel(res, auth, autoSelection.model)) return;
|
|
3043
|
+
const routedRequest = autoSelection
|
|
3044
|
+
? { ...validation.value, model: autoSelection.model }
|
|
3045
|
+
: validation.value;
|
|
3046
|
+
|
|
3047
|
+
if (isCodexModelForRotator(rotator, routedRequest.model)) {
|
|
3048
|
+
await serveCodexChat(req, res, rotator, routedRequest, {
|
|
2884
3049
|
callType: "chat_completion",
|
|
2885
3050
|
apiKeyHash,
|
|
2886
3051
|
requesterIp: req.socket?.remoteAddress || null,
|
|
2887
3052
|
rawRequest: validation.value,
|
|
3053
|
+
providerId: autoSelection?.providerId,
|
|
3054
|
+
requestedModel: autoRequested ? requestedModel : undefined,
|
|
3055
|
+
selectionMode: autoSelection?.mode,
|
|
3056
|
+
selectionConfidence: autoSelection?.confidence,
|
|
3057
|
+
selectionReason: autoSelection?.reason,
|
|
2888
3058
|
});
|
|
2889
3059
|
return;
|
|
2890
3060
|
}
|
|
@@ -2894,19 +3064,28 @@ export async function handleOpenAIChatCompletions(
|
|
|
2894
3064
|
rotator?.getConfig?.()?.compressionMode,
|
|
2895
3065
|
);
|
|
2896
3066
|
const compRes = applyPromptCompression(
|
|
2897
|
-
|
|
3067
|
+
routedRequest.messages,
|
|
2898
3068
|
compMode,
|
|
2899
|
-
{ model:
|
|
3069
|
+
{ model: routedRequest.model },
|
|
2900
3070
|
);
|
|
2901
3071
|
const chatReq = compRes.stats
|
|
2902
|
-
? { ...
|
|
2903
|
-
:
|
|
3072
|
+
? { ...routedRequest, messages: compRes.messages }
|
|
3073
|
+
: routedRequest;
|
|
2904
3074
|
|
|
2905
3075
|
const started = Date.now();
|
|
2906
|
-
const streamMode =
|
|
2907
|
-
const bodyToForward: RequestBody = isOpenCodeZenModel(chatReq.model)
|
|
3076
|
+
const streamMode = routedRequest.stream ? "openai" : "none";
|
|
3077
|
+
const bodyToForward: RequestBody = isOpenCodeZenModel(chatReq.model) || autoSelection?.providerId === OPENCODE_ZEN_PROVIDER_ID
|
|
2908
3078
|
? { project: "", model: chatReq.model, request: chatReq, requestType: "openai-chat" }
|
|
2909
|
-
: (rotator?.getOllamaModels?.().includes(chatReq.model)
|
|
3079
|
+
: (autoSelection?.providerId === "ollama" || rotator?.getOllamaModels?.().includes(chatReq.model)
|
|
3080
|
+
? openAIToOllamaBody(chatReq)
|
|
3081
|
+
: openAIToAntigravityBody(chatReq));
|
|
3082
|
+
if (autoSelection) {
|
|
3083
|
+
bodyToForward.displayModel = requestedModel;
|
|
3084
|
+
bodyToForward.providerId = autoSelection.providerId;
|
|
3085
|
+
bodyToForward.selectionMode = autoSelection.mode;
|
|
3086
|
+
bodyToForward.selectionConfidence = autoSelection.confidence;
|
|
3087
|
+
bodyToForward.selectionReason = autoSelection.reason;
|
|
3088
|
+
}
|
|
2910
3089
|
const result = await completeViaRotator(
|
|
2911
3090
|
req,
|
|
2912
3091
|
res,
|
|
@@ -2952,7 +3131,12 @@ export async function handleOpenAIChatCompletions(
|
|
|
2952
3131
|
const ttfbMs = result.completion.firstByteMs ?? totalMs;
|
|
2953
3132
|
const rotatorHeaders = buildRotatorResponseHeaders({
|
|
2954
3133
|
accountLabel: result.context?.label,
|
|
2955
|
-
model:
|
|
3134
|
+
model: autoRequested ? result.context?.selectedModel ?? routedRequest.model : requestedModel,
|
|
3135
|
+
requestedModel: result.context?.requestedModel,
|
|
3136
|
+
selectedProvider: result.context?.providerId,
|
|
3137
|
+
modelSelection: result.context?.selectionMode,
|
|
3138
|
+
selectionConfidence: result.context?.selectionConfidence,
|
|
3139
|
+
selectionReason: result.context?.selectionReason,
|
|
2956
3140
|
latencyMs: totalMs,
|
|
2957
3141
|
ttfbMs,
|
|
2958
3142
|
inputTokens: result.completion.inputTokens,
|
|
@@ -2966,7 +3150,7 @@ export async function handleOpenAIChatCompletions(
|
|
|
2966
3150
|
id: `chatcmpl-${started.toString(36)}`,
|
|
2967
3151
|
object: "chat.completion",
|
|
2968
3152
|
created: Math.floor(started / 1000),
|
|
2969
|
-
model:
|
|
3153
|
+
model: autoRequested ? result.context?.selectedModel ?? routedRequest.model : requestedModel,
|
|
2970
3154
|
choices: [
|
|
2971
3155
|
{
|
|
2972
3156
|
index: 0,
|
|
@@ -3028,26 +3212,46 @@ export async function handleOpenAIResponsesCreate(
|
|
|
3028
3212
|
},
|
|
3029
3213
|
});
|
|
3030
3214
|
|
|
3031
|
-
const
|
|
3215
|
+
const requestedModel = validation.value.model;
|
|
3216
|
+
const autoRequested = isAutoModel(requestedModel);
|
|
3217
|
+
const auth = await authenticateVirtualKey(req, autoRequested ? undefined : requestedModel);
|
|
3032
3218
|
if (!auth.authenticated) {
|
|
3033
3219
|
sendAuthErrorResponse(res, auth);
|
|
3034
3220
|
return;
|
|
3035
3221
|
}
|
|
3036
3222
|
const apiKeyHash = auth.key?.tokenHash || (auth.rawKey ? hashKey(auth.rawKey) : null);
|
|
3037
3223
|
|
|
3038
|
-
|
|
3039
|
-
await
|
|
3224
|
+
const autoSelection = autoRequested
|
|
3225
|
+
? await resolveAutoModel(rotator, "openai-responses", validation.value)
|
|
3226
|
+
: null;
|
|
3227
|
+
if (autoRequested && !autoSelection) {
|
|
3228
|
+
return writeJson(res, 503, {
|
|
3229
|
+
error: { message: "No compatible model is currently available", type: "upstream_error" },
|
|
3230
|
+
});
|
|
3231
|
+
}
|
|
3232
|
+
if (autoSelection && !authorizeResolvedAutoModel(res, auth, autoSelection.model)) return;
|
|
3233
|
+
const routedValidation = autoSelection
|
|
3234
|
+
? { ...validation.value, model: autoSelection.model }
|
|
3235
|
+
: validation.value;
|
|
3236
|
+
|
|
3237
|
+
if (isCodexModelForRotator(rotator, routedValidation.model)) {
|
|
3238
|
+
await serveCodexResponses(req, res, rotator, routedValidation, {
|
|
3040
3239
|
callType: "responses",
|
|
3041
3240
|
apiKeyHash,
|
|
3042
3241
|
requesterIp: req.socket?.remoteAddress || null,
|
|
3043
3242
|
rawRequest: validation.value,
|
|
3243
|
+
providerId: autoSelection?.providerId,
|
|
3244
|
+
requestedModel: autoRequested ? requestedModel : undefined,
|
|
3245
|
+
selectionMode: autoSelection?.mode,
|
|
3246
|
+
selectionConfidence: autoSelection?.confidence,
|
|
3247
|
+
selectionReason: autoSelection?.reason,
|
|
3044
3248
|
});
|
|
3045
3249
|
return;
|
|
3046
3250
|
}
|
|
3047
3251
|
|
|
3048
3252
|
let converted: ResponsesConversionResult;
|
|
3049
3253
|
try {
|
|
3050
|
-
converted = convertResponsesToChatRequest(
|
|
3254
|
+
converted = convertResponsesToChatRequest(routedValidation);
|
|
3051
3255
|
} catch {
|
|
3052
3256
|
return writeJson(res, 400, {
|
|
3053
3257
|
error: {
|
|
@@ -3072,16 +3276,25 @@ export async function handleOpenAIResponsesCreate(
|
|
|
3072
3276
|
? { ...converted.chatRequest, messages: compRes.messages }
|
|
3073
3277
|
: converted.chatRequest;
|
|
3074
3278
|
|
|
3075
|
-
const requestBody: RequestBody = isOpenCodeZenModel(chatRequest.model)
|
|
3279
|
+
const requestBody: RequestBody = isOpenCodeZenModel(chatRequest.model) || autoSelection?.providerId === OPENCODE_ZEN_PROVIDER_ID
|
|
3076
3280
|
? { project: "", model: chatRequest.model, request: chatRequest, requestType: "openai-responses" }
|
|
3077
|
-
: (rotator?.getOllamaModels?.().includes(chatRequest.model)
|
|
3281
|
+
: (autoSelection?.providerId === "ollama" || rotator?.getOllamaModels?.().includes(chatRequest.model)
|
|
3282
|
+
? openAIToOllamaBody(chatRequest)
|
|
3283
|
+
: openAIToAntigravityBody(chatRequest));
|
|
3284
|
+
if (autoSelection) {
|
|
3285
|
+
requestBody.displayModel = requestedModel;
|
|
3286
|
+
requestBody.providerId = autoSelection.providerId;
|
|
3287
|
+
requestBody.selectionMode = autoSelection.mode;
|
|
3288
|
+
requestBody.selectionConfidence = autoSelection.confidence;
|
|
3289
|
+
requestBody.selectionReason = autoSelection.reason;
|
|
3290
|
+
}
|
|
3078
3291
|
requestBody.requestId = responseId;
|
|
3079
3292
|
|
|
3080
3293
|
if (validation.value.store !== false) {
|
|
3081
3294
|
const expiresAt = Date.now() + 6 * 60 * 60 * 1000;
|
|
3082
3295
|
setStoredResponse(responseId, {
|
|
3083
3296
|
response: buildResponsesResponse(
|
|
3084
|
-
|
|
3297
|
+
routedValidation,
|
|
3085
3298
|
responseId,
|
|
3086
3299
|
createdAt,
|
|
3087
3300
|
{ text: "", inputTokens: 0, outputTokens: 0, toolCalls: [] },
|
|
@@ -3102,7 +3315,7 @@ export async function handleOpenAIResponsesCreate(
|
|
|
3102
3315
|
req,
|
|
3103
3316
|
res,
|
|
3104
3317
|
rotator,
|
|
3105
|
-
|
|
3318
|
+
routedValidation,
|
|
3106
3319
|
requestBody,
|
|
3107
3320
|
responseId,
|
|
3108
3321
|
converted.previousResponseId,
|
|
@@ -3126,7 +3339,7 @@ export async function handleOpenAIResponsesCreate(
|
|
|
3126
3339
|
}
|
|
3127
3340
|
if (validation.value.store !== false) {
|
|
3128
3341
|
const responseObject = buildResponsesResponse(
|
|
3129
|
-
|
|
3342
|
+
routedValidation,
|
|
3130
3343
|
responseId,
|
|
3131
3344
|
createdAt,
|
|
3132
3345
|
result.completion,
|
|
@@ -3168,7 +3381,7 @@ export async function handleOpenAIResponsesCreate(
|
|
|
3168
3381
|
}
|
|
3169
3382
|
|
|
3170
3383
|
const responseObject = buildResponsesResponse(
|
|
3171
|
-
|
|
3384
|
+
routedValidation,
|
|
3172
3385
|
responseId,
|
|
3173
3386
|
createdAt,
|
|
3174
3387
|
result.completion,
|
|
@@ -3189,7 +3402,12 @@ export async function handleOpenAIResponsesCreate(
|
|
|
3189
3402
|
const ttfbMs = result.completion.firstByteMs ?? totalMs;
|
|
3190
3403
|
const rotatorHeaders = buildRotatorResponseHeaders({
|
|
3191
3404
|
accountLabel: result.context?.label,
|
|
3192
|
-
model:
|
|
3405
|
+
model: autoRequested ? result.context?.selectedModel ?? routedValidation.model : requestedModel,
|
|
3406
|
+
requestedModel: result.context?.requestedModel,
|
|
3407
|
+
selectedProvider: result.context?.providerId,
|
|
3408
|
+
modelSelection: result.context?.selectionMode,
|
|
3409
|
+
selectionConfidence: result.context?.selectionConfidence,
|
|
3410
|
+
selectionReason: result.context?.selectionReason,
|
|
3193
3411
|
latencyMs: totalMs,
|
|
3194
3412
|
ttfbMs,
|
|
3195
3413
|
inputTokens: result.completion.inputTokens,
|
|
@@ -3301,34 +3519,59 @@ export async function handleAnthropicMessages(
|
|
|
3301
3519
|
},
|
|
3302
3520
|
});
|
|
3303
3521
|
|
|
3304
|
-
const
|
|
3522
|
+
const requestedModel = validation.value.model;
|
|
3523
|
+
const autoRequested = isAutoModel(requestedModel);
|
|
3524
|
+
const auth = await authenticateVirtualKey(req, autoRequested ? undefined : requestedModel);
|
|
3305
3525
|
if (!auth.authenticated) {
|
|
3306
3526
|
sendAuthErrorResponse(res, auth);
|
|
3307
3527
|
return;
|
|
3308
3528
|
}
|
|
3309
3529
|
const apiKeyHash = auth.key?.tokenHash || (auth.rawKey ? hashKey(auth.rawKey) : null);
|
|
3310
3530
|
|
|
3531
|
+
const autoSelection = autoRequested
|
|
3532
|
+
? await resolveAutoModel(rotator, "anthropic", validation.value)
|
|
3533
|
+
: null;
|
|
3534
|
+
if (autoRequested && !autoSelection) {
|
|
3535
|
+
return writeJson(res, 503, {
|
|
3536
|
+
type: "error",
|
|
3537
|
+
error: { type: "overloaded_error", message: "No compatible model is currently available" },
|
|
3538
|
+
});
|
|
3539
|
+
}
|
|
3540
|
+
if (autoSelection && !authorizeResolvedAutoModel(res, auth, autoSelection.model)) return;
|
|
3541
|
+
const routedValidation = autoSelection
|
|
3542
|
+
? { ...validation.value, model: autoSelection.model }
|
|
3543
|
+
: validation.value;
|
|
3544
|
+
|
|
3311
3545
|
const compMode = parseCompressionMode(
|
|
3312
3546
|
req.headers["x-rotator-compression"],
|
|
3313
3547
|
rotator?.getConfig?.()?.compressionMode,
|
|
3314
3548
|
);
|
|
3315
3549
|
const compRes = applyPromptCompression(
|
|
3316
|
-
|
|
3550
|
+
routedValidation.messages as ChatMessage[],
|
|
3317
3551
|
compMode,
|
|
3318
|
-
{ model:
|
|
3552
|
+
{ model: routedValidation.model },
|
|
3319
3553
|
);
|
|
3320
3554
|
const anthropicReq = compRes.stats
|
|
3321
3555
|
? {
|
|
3322
|
-
...
|
|
3323
|
-
messages: compRes.messages as typeof
|
|
3556
|
+
...routedValidation,
|
|
3557
|
+
messages: compRes.messages as typeof routedValidation.messages,
|
|
3324
3558
|
}
|
|
3325
|
-
:
|
|
3559
|
+
: routedValidation;
|
|
3326
3560
|
|
|
3327
3561
|
const started = Date.now();
|
|
3328
|
-
const streamMode =
|
|
3329
|
-
const bodyToForward: RequestBody = isOpenCodeZenModel(anthropicReq.model)
|
|
3562
|
+
const streamMode = routedValidation.stream ? "anthropic" : "none";
|
|
3563
|
+
const bodyToForward: RequestBody = isOpenCodeZenModel(anthropicReq.model) || autoSelection?.providerId === OPENCODE_ZEN_PROVIDER_ID
|
|
3330
3564
|
? { project: "", model: anthropicReq.model, request: anthropicToOpenAIChatRequest(anthropicReq), requestType: "anthropic" }
|
|
3331
|
-
: (rotator?.getOllamaModels?.().includes(anthropicReq.model)
|
|
3565
|
+
: (autoSelection?.providerId === "ollama" || rotator?.getOllamaModels?.().includes(anthropicReq.model)
|
|
3566
|
+
? anthropicToOllamaBody(anthropicReq)
|
|
3567
|
+
: anthropicToAntigravityBody(anthropicReq));
|
|
3568
|
+
if (autoSelection) {
|
|
3569
|
+
bodyToForward.displayModel = requestedModel;
|
|
3570
|
+
bodyToForward.providerId = autoSelection.providerId;
|
|
3571
|
+
bodyToForward.selectionMode = autoSelection.mode;
|
|
3572
|
+
bodyToForward.selectionConfidence = autoSelection.confidence;
|
|
3573
|
+
bodyToForward.selectionReason = autoSelection.reason;
|
|
3574
|
+
}
|
|
3332
3575
|
const result = await completeViaRotator(
|
|
3333
3576
|
req,
|
|
3334
3577
|
res,
|
|
@@ -3395,7 +3638,12 @@ export async function handleAnthropicMessages(
|
|
|
3395
3638
|
const ttfbMs = result.completion.firstByteMs ?? totalMs;
|
|
3396
3639
|
const rotatorHeaders = buildRotatorResponseHeaders({
|
|
3397
3640
|
accountLabel: result.context?.label,
|
|
3398
|
-
model:
|
|
3641
|
+
model: autoRequested ? result.context?.selectedModel ?? routedValidation.model : requestedModel,
|
|
3642
|
+
requestedModel: result.context?.requestedModel,
|
|
3643
|
+
selectedProvider: result.context?.providerId,
|
|
3644
|
+
modelSelection: result.context?.selectionMode,
|
|
3645
|
+
selectionConfidence: result.context?.selectionConfidence,
|
|
3646
|
+
selectionReason: result.context?.selectionReason,
|
|
3399
3647
|
latencyMs: totalMs,
|
|
3400
3648
|
ttfbMs,
|
|
3401
3649
|
inputTokens: result.completion.inputTokens,
|
|
@@ -3409,7 +3657,7 @@ export async function handleAnthropicMessages(
|
|
|
3409
3657
|
id: `msg_${started.toString(36)}`,
|
|
3410
3658
|
type: "message",
|
|
3411
3659
|
role: "assistant",
|
|
3412
|
-
model:
|
|
3660
|
+
model: autoRequested ? result.context?.selectedModel ?? routedValidation.model : requestedModel,
|
|
3413
3661
|
content: contentBlocks,
|
|
3414
3662
|
stop_reason: stopReason,
|
|
3415
3663
|
stop_sequence: null,
|
package/src/config-defaults.ts
CHANGED
|
@@ -58,6 +58,19 @@ export function applyConfigDefaults(config: Config): Config {
|
|
|
58
58
|
modelSpecs: config.modelSpecs,
|
|
59
59
|
modelAliases: config.modelAliases,
|
|
60
60
|
effortRouting: config.effortRouting,
|
|
61
|
+
typesafeRouting: config.typesafeRouting
|
|
62
|
+
? {
|
|
63
|
+
enabled: config.typesafeRouting.enabled ?? Boolean(config.typesafeRouting.apiKey),
|
|
64
|
+
apiKey: config.typesafeRouting.apiKey,
|
|
65
|
+
model: config.typesafeRouting.model ?? "jev-latest",
|
|
66
|
+
baseURL: config.typesafeRouting.baseURL,
|
|
67
|
+
timeoutMs: config.typesafeRouting.timeoutMs ?? 3_000,
|
|
68
|
+
minConfidence: config.typesafeRouting.minConfidence ?? 0.45,
|
|
69
|
+
maxCandidates: config.typesafeRouting.maxCandidates ?? 32,
|
|
70
|
+
shortlistSize: config.typesafeRouting.shortlistSize ?? 12,
|
|
71
|
+
maxExcerptChars: config.typesafeRouting.maxExcerptChars ?? 12_000,
|
|
72
|
+
}
|
|
73
|
+
: undefined,
|
|
61
74
|
accounts: config.accounts ? config.accounts.map((account) => ({
|
|
62
75
|
...normalizeAccountConfig(account),
|
|
63
76
|
tier: account.tier || "unknown",
|
package/src/dashboard.ts
CHANGED
|
@@ -106,7 +106,7 @@ export function serveConfigApi(
|
|
|
106
106
|
"Content-Type": "application/json",
|
|
107
107
|
"Cache-Control": "no-store",
|
|
108
108
|
});
|
|
109
|
-
res.end(JSON.stringify(rotator.
|
|
109
|
+
res.end(JSON.stringify(rotator.getPublicConfig()));
|
|
110
110
|
}
|
|
111
111
|
|
|
112
112
|
/**
|
|
@@ -145,7 +145,7 @@ export function serveConfigExportApi(
|
|
|
145
145
|
"Content-Disposition":
|
|
146
146
|
'attachment; filename="tuxevil-rotator-config.json"',
|
|
147
147
|
});
|
|
148
|
-
res.end(JSON.stringify(rotator.
|
|
148
|
+
res.end(JSON.stringify(rotator.getPublicConfig(), null, 2));
|
|
149
149
|
}
|
|
150
150
|
|
|
151
151
|
export async function serveConfigImportApi(
|
package/src/db-store.ts
CHANGED
|
@@ -12,7 +12,9 @@ import { rotatorEnv } from "./env.js";
|
|
|
12
12
|
import { applyConfigDefaults } from "./config-defaults.js";
|
|
13
13
|
import {
|
|
14
14
|
decryptAccountsInConfig,
|
|
15
|
+
decryptTypeSafeRoutingInConfig,
|
|
15
16
|
encryptAccountsInConfig,
|
|
17
|
+
encryptTypeSafeRoutingInConfig,
|
|
16
18
|
} from "./token-encryption.js";
|
|
17
19
|
import {
|
|
18
20
|
type ISettingsRepository,
|
|
@@ -94,8 +96,10 @@ export function getCachedConfig(): Config | null {
|
|
|
94
96
|
const validation = validateConfig(parsed);
|
|
95
97
|
if (validation.ok && validation.value) {
|
|
96
98
|
const withDefaults = applyConfigDefaults(validation.value);
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
+
const accountsResult = decryptAccountsInConfig(withDefaults);
|
|
100
|
+
const typesafeResult = decryptTypeSafeRoutingInConfig(accountsResult.config);
|
|
101
|
+
const decryptedConfig = typesafeResult.config;
|
|
102
|
+
if (accountsResult.migrated || typesafeResult.migrated) {
|
|
99
103
|
void setCachedConfig(decryptedConfig).catch((err) => {
|
|
100
104
|
console.error(`Failed to persist migrated accounts config: ${err}`);
|
|
101
105
|
});
|
|
@@ -111,7 +115,9 @@ export function getCachedConfig(): Config | null {
|
|
|
111
115
|
export async function setCachedConfig(config: Config): Promise<void> {
|
|
112
116
|
assertInitialized();
|
|
113
117
|
const withDefaults = applyConfigDefaults(config);
|
|
114
|
-
const encryptedConfig =
|
|
118
|
+
const encryptedConfig = encryptTypeSafeRoutingInConfig(
|
|
119
|
+
encryptAccountsInConfig(withDefaults),
|
|
120
|
+
);
|
|
115
121
|
await repository.set("accounts_json", JSON.stringify(encryptedConfig, null, 2));
|
|
116
122
|
}
|
|
117
123
|
|