tuxevil-rotator 3.7.2 → 3.9.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 +20 -1
- package/README.md +5 -1
- package/package.json +2 -1
- package/src/auto-routing.ts +582 -0
- package/src/compat.ts +292 -44
- package/src/config-defaults.ts +14 -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 +189 -2
- package/src/providers/google-antigravity/forward.ts +8 -1
- package/src/providers/openai-codex/catalog.ts +8 -6
- 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/static/dashboard.js +10 -1
- package/src/token-encryption.ts +91 -0
- package/src/types.ts +44 -3
- package/src/validators.ts +41 -0
package/src/token-encryption.ts
CHANGED
|
@@ -65,6 +65,13 @@ export function isEncryptedToken(token: string): boolean {
|
|
|
65
65
|
);
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
+
/** Checks whether any persisted secret uses one of the encrypted formats. */
|
|
69
|
+
export const isEncryptedSecret = isEncryptedToken;
|
|
70
|
+
|
|
71
|
+
export function isRedactedSecret(value: string | undefined): boolean {
|
|
72
|
+
return value === "[configured]";
|
|
73
|
+
}
|
|
74
|
+
|
|
68
75
|
/**
|
|
69
76
|
* Encrypts a plain-text OAuth refresh token using AES-256-GCM.
|
|
70
77
|
* Format: enc:v2:<salt_hex>:<iv_hex>:<tag_hex>:<ciphertext_hex>
|
|
@@ -119,6 +126,90 @@ export function decryptRefreshToken(
|
|
|
119
126
|
return decrypted;
|
|
120
127
|
}
|
|
121
128
|
|
|
129
|
+
/** Encrypts a non-OAuth secret using the same authenticated format. */
|
|
130
|
+
export function encryptSecret(secret: string, keyInput: string): string {
|
|
131
|
+
return encryptRefreshToken(secret, keyInput);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Decrypts a non-OAuth secret using the same authenticated format. */
|
|
135
|
+
export function decryptSecret(secret: string, keyInput: string): string {
|
|
136
|
+
return decryptRefreshToken(secret, keyInput);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Encrypts the optional TypeSafe credential before it reaches persistence.
|
|
141
|
+
* A newly supplied API key must never be silently written in plaintext.
|
|
142
|
+
*/
|
|
143
|
+
export function encryptTypeSafeRoutingInConfig(
|
|
144
|
+
config: Config,
|
|
145
|
+
keyInput?: string,
|
|
146
|
+
): Config {
|
|
147
|
+
const routing = config.typesafeRouting;
|
|
148
|
+
if (!routing?.apiKey || isEncryptedSecret(routing.apiKey) || isRedactedSecret(routing.apiKey)) return config;
|
|
149
|
+
const key = keyInput || getEncryptionKey();
|
|
150
|
+
if (!key) {
|
|
151
|
+
throw new Error(
|
|
152
|
+
"TypeSafe API key cannot be persisted without ENCRYPTION_KEY",
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
...config,
|
|
157
|
+
typesafeRouting: {
|
|
158
|
+
...routing,
|
|
159
|
+
apiKey: encryptSecret(routing.apiKey, key),
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Decrypts the persisted TypeSafe credential for runtime use. If the
|
|
166
|
+
* encryption key is unavailable, the ciphertext remains in place and Jev is
|
|
167
|
+
* treated as unavailable by the routing layer.
|
|
168
|
+
*/
|
|
169
|
+
export function decryptTypeSafeRoutingInConfig(
|
|
170
|
+
config: Config,
|
|
171
|
+
keyInput?: string,
|
|
172
|
+
): { config: Config; migrated: boolean } {
|
|
173
|
+
const routing = config.typesafeRouting;
|
|
174
|
+
if (!routing?.apiKey || !isEncryptedSecret(routing.apiKey)) {
|
|
175
|
+
return { config, migrated: Boolean(routing?.apiKey && (keyInput || getEncryptionKey())) };
|
|
176
|
+
}
|
|
177
|
+
const key = keyInput || getEncryptionKey();
|
|
178
|
+
if (!key) {
|
|
179
|
+
if (!missingKeyWarned) {
|
|
180
|
+
tokenLog.warn(
|
|
181
|
+
"Found encrypted TypeSafe API key but ENCRYPTION_KEY is not set; Jev auto-routing is disabled until it is configured.",
|
|
182
|
+
);
|
|
183
|
+
missingKeyWarned = true;
|
|
184
|
+
}
|
|
185
|
+
return { config, migrated: false };
|
|
186
|
+
}
|
|
187
|
+
try {
|
|
188
|
+
return {
|
|
189
|
+
config: {
|
|
190
|
+
...config,
|
|
191
|
+
typesafeRouting: { ...routing, apiKey: decryptSecret(routing.apiKey, key) },
|
|
192
|
+
},
|
|
193
|
+
migrated: false,
|
|
194
|
+
};
|
|
195
|
+
} catch (err) {
|
|
196
|
+
tokenLog.error(`Failed to decrypt TypeSafe API key: ${err}`);
|
|
197
|
+
return { config, migrated: false };
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** Returns a config safe for the admin UI/export surface. */
|
|
202
|
+
export function redactTypeSafeRoutingInConfig(config: Config): Config {
|
|
203
|
+
if (!config.typesafeRouting?.apiKey) return structuredClone(config);
|
|
204
|
+
return {
|
|
205
|
+
...structuredClone(config),
|
|
206
|
+
typesafeRouting: {
|
|
207
|
+
...config.typesafeRouting,
|
|
208
|
+
apiKey: "[configured]",
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
|
|
122
213
|
/**
|
|
123
214
|
* Transforms a Config object by encrypting all plain account refresh tokens
|
|
124
215
|
* if an encryption key is present.
|
package/src/types.ts
CHANGED
|
@@ -152,6 +152,31 @@ export interface Config {
|
|
|
152
152
|
// Config-driven alias routing based on reasoning_effort.
|
|
153
153
|
// Absent, null, or empty object = disabled.
|
|
154
154
|
effortRouting?: Record<string, EffortRoutingRule> | null;
|
|
155
|
+
/** Optional semantic model routing through TypeSafe Jev. */
|
|
156
|
+
typesafeRouting?: TypeSafeRoutingConfig;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export interface TypeSafeRoutingConfig {
|
|
160
|
+
/** Explicit opt-in. When omitted, a configured apiKey enables the feature. */
|
|
161
|
+
enabled?: boolean;
|
|
162
|
+
/** TypeSafe API key. Persisted encrypted by the settings layer. */
|
|
163
|
+
apiKey?: string;
|
|
164
|
+
/** Jev model alias. Defaults to the provider's stable latest alias. */
|
|
165
|
+
model?: string;
|
|
166
|
+
/** Optional API root override for self-hosted/test environments. */
|
|
167
|
+
baseURL?: string;
|
|
168
|
+
/** Per-attempt timeout for the routing decision. */
|
|
169
|
+
timeoutMs?: number;
|
|
170
|
+
/** Minimum accepted Jev confidence before deterministic fallback. */
|
|
171
|
+
minConfidence?: number;
|
|
172
|
+
/** Ask Jev for a recommendation but continue serving the deterministic route. */
|
|
173
|
+
shadowMode?: boolean;
|
|
174
|
+
/** Maximum candidate models sent to Jev per request. */
|
|
175
|
+
maxCandidates?: number;
|
|
176
|
+
/** Maximum diverse candidate models included in Jev's final decision. */
|
|
177
|
+
shortlistSize?: number;
|
|
178
|
+
/** Maximum prompt excerpt characters sent to Jev. */
|
|
179
|
+
maxExcerptChars?: number;
|
|
155
180
|
}
|
|
156
181
|
|
|
157
182
|
export interface EffortRoutingRule {
|
|
@@ -890,9 +915,25 @@ export const MODEL_PRICING: Record<string, ModelPricing> = {
|
|
|
890
915
|
},
|
|
891
916
|
"gpt-oss-120b-medium": { inputPer1M: 2.0, outputPer1M: 10.0 },
|
|
892
917
|
|
|
893
|
-
// OpenAI Codex
|
|
894
|
-
// 2026-
|
|
895
|
-
//
|
|
918
|
+
// OpenAI Codex API-equivalent standard rates, checked against
|
|
919
|
+
// https://developers.openai.com/api/docs/pricing on 2026-09-22.
|
|
920
|
+
// Spend logs use aggregate input/output tokens;
|
|
921
|
+
// cache rates are metadata and are not applied separately.
|
|
922
|
+
"gpt-6-astra": {
|
|
923
|
+
inputPer1M: 10.0,
|
|
924
|
+
outputPer1M: 50.0,
|
|
925
|
+
cachingPer1M: 1.0,
|
|
926
|
+
},
|
|
927
|
+
"gpt-6-sol": {
|
|
928
|
+
inputPer1M: 2.0,
|
|
929
|
+
outputPer1M: 10.0,
|
|
930
|
+
cachingPer1M: 0.2,
|
|
931
|
+
},
|
|
932
|
+
"gpt-6-luna": {
|
|
933
|
+
inputPer1M: 0.1,
|
|
934
|
+
outputPer1M: 0.5,
|
|
935
|
+
cachingPer1M: 0.01,
|
|
936
|
+
},
|
|
896
937
|
"gpt-5.6-sol": {
|
|
897
938
|
inputPer1M: 5.0,
|
|
898
939
|
outputPer1M: 30.0,
|
package/src/validators.ts
CHANGED
|
@@ -304,6 +304,47 @@ export function validateConfig(value: unknown): ValidationResult<Config> {
|
|
|
304
304
|
}
|
|
305
305
|
}
|
|
306
306
|
}
|
|
307
|
+
if (value.typesafeRouting !== undefined) {
|
|
308
|
+
if (!isRecord(value.typesafeRouting)) {
|
|
309
|
+
errors.push("config.typesafeRouting must be an object when provided");
|
|
310
|
+
} else {
|
|
311
|
+
const routing = value.typesafeRouting;
|
|
312
|
+
if (routing.enabled !== undefined && typeof routing.enabled !== "boolean") {
|
|
313
|
+
errors.push("config.typesafeRouting.enabled must be a boolean");
|
|
314
|
+
}
|
|
315
|
+
if (routing.shadowMode !== undefined && typeof routing.shadowMode !== "boolean") {
|
|
316
|
+
errors.push("config.typesafeRouting.shadowMode must be a boolean");
|
|
317
|
+
}
|
|
318
|
+
if (routing.apiKey !== undefined && !isNonEmptyString(routing.apiKey)) {
|
|
319
|
+
errors.push("config.typesafeRouting.apiKey must be a non-empty string when provided");
|
|
320
|
+
}
|
|
321
|
+
if (routing.model !== undefined && !isNonEmptyString(routing.model)) {
|
|
322
|
+
errors.push("config.typesafeRouting.model must be a non-empty string when provided");
|
|
323
|
+
}
|
|
324
|
+
if (routing.baseURL !== undefined && !isNonEmptyString(routing.baseURL)) {
|
|
325
|
+
errors.push("config.typesafeRouting.baseURL must be a non-empty string when provided");
|
|
326
|
+
}
|
|
327
|
+
if (routing.timeoutMs !== undefined && !isPositiveNumber(routing.timeoutMs)) {
|
|
328
|
+
errors.push("config.typesafeRouting.timeoutMs must be a positive number");
|
|
329
|
+
}
|
|
330
|
+
if (routing.minConfidence !== undefined &&
|
|
331
|
+
(typeof routing.minConfidence !== "number" || !Number.isFinite(routing.minConfidence) || routing.minConfidence < 0 || routing.minConfidence > 1)) {
|
|
332
|
+
errors.push("config.typesafeRouting.minConfidence must be between 0 and 1");
|
|
333
|
+
}
|
|
334
|
+
if (routing.maxCandidates !== undefined &&
|
|
335
|
+
(!isNonNegativeInteger(routing.maxCandidates) || routing.maxCandidates < 1 || routing.maxCandidates > 128)) {
|
|
336
|
+
errors.push("config.typesafeRouting.maxCandidates must be an integer between 1 and 128");
|
|
337
|
+
}
|
|
338
|
+
if (routing.shortlistSize !== undefined &&
|
|
339
|
+
(!isNonNegativeInteger(routing.shortlistSize) || routing.shortlistSize < 1 || routing.shortlistSize > 32)) {
|
|
340
|
+
errors.push("config.typesafeRouting.shortlistSize must be an integer between 1 and 32");
|
|
341
|
+
}
|
|
342
|
+
if (routing.maxExcerptChars !== undefined &&
|
|
343
|
+
(!isNonNegativeInteger(routing.maxExcerptChars) || routing.maxExcerptChars < 256 || routing.maxExcerptChars > 50_000)) {
|
|
344
|
+
errors.push("config.typesafeRouting.maxExcerptChars must be an integer between 256 and 50000");
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
307
348
|
|
|
308
349
|
return errors.length > 0 ? fail(errors) : ok(value as unknown as Config);
|
|
309
350
|
}
|