tuxevil-rotator 3.7.1 → 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.
@@ -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,29 @@ 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
+ /** Maximum candidate models sent to Jev per request. */
173
+ maxCandidates?: number;
174
+ /** Maximum diverse candidate models included in Jev's final decision. */
175
+ shortlistSize?: number;
176
+ /** Maximum prompt excerpt characters sent to Jev. */
177
+ maxExcerptChars?: number;
155
178
  }
156
179
 
157
180
  export interface EffortRoutingRule {
package/src/validators.ts CHANGED
@@ -304,6 +304,44 @@ 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.apiKey !== undefined && !isNonEmptyString(routing.apiKey)) {
316
+ errors.push("config.typesafeRouting.apiKey must be a non-empty string when provided");
317
+ }
318
+ if (routing.model !== undefined && !isNonEmptyString(routing.model)) {
319
+ errors.push("config.typesafeRouting.model must be a non-empty string when provided");
320
+ }
321
+ if (routing.baseURL !== undefined && !isNonEmptyString(routing.baseURL)) {
322
+ errors.push("config.typesafeRouting.baseURL must be a non-empty string when provided");
323
+ }
324
+ if (routing.timeoutMs !== undefined && !isPositiveNumber(routing.timeoutMs)) {
325
+ errors.push("config.typesafeRouting.timeoutMs must be a positive number");
326
+ }
327
+ if (routing.minConfidence !== undefined &&
328
+ (typeof routing.minConfidence !== "number" || !Number.isFinite(routing.minConfidence) || routing.minConfidence < 0 || routing.minConfidence > 1)) {
329
+ errors.push("config.typesafeRouting.minConfidence must be between 0 and 1");
330
+ }
331
+ if (routing.maxCandidates !== undefined &&
332
+ (!isNonNegativeInteger(routing.maxCandidates) || routing.maxCandidates < 1 || routing.maxCandidates > 128)) {
333
+ errors.push("config.typesafeRouting.maxCandidates must be an integer between 1 and 128");
334
+ }
335
+ if (routing.shortlistSize !== undefined &&
336
+ (!isNonNegativeInteger(routing.shortlistSize) || routing.shortlistSize < 1 || routing.shortlistSize > 32)) {
337
+ errors.push("config.typesafeRouting.shortlistSize must be an integer between 1 and 32");
338
+ }
339
+ if (routing.maxExcerptChars !== undefined &&
340
+ (!isNonNegativeInteger(routing.maxExcerptChars) || routing.maxExcerptChars < 256 || routing.maxExcerptChars > 50_000)) {
341
+ errors.push("config.typesafeRouting.maxExcerptChars must be an integer between 256 and 50000");
342
+ }
343
+ }
344
+ }
307
345
 
308
346
  return errors.length > 0 ? fail(errors) : ok(value as unknown as Config);
309
347
  }