Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

fix: non-decryptable keys can block routing (#85)

Ricardoa27dc42a00ea
diff --git a/server/src/__tests__/services/router.test.ts b/server/src/__tests__/services/router.test.ts
index a532a041..e0e9e489 100644
--- a/server/src/__tests__/services/router.test.ts
+++ b/server/src/__tests__/services/router.test.ts
@@ -96,4 +96,26 @@ describe('Router', () => {
const result = routeRequest();
expect(result.platform).toBe('groq');
});
+
+ it('should skip keys that cannot be decrypted and use a valid fallback key', () => {
+ const db = getDb();
+
+ db.prepare(`
+ INSERT INTO api_keys (platform, label, encrypted_key, iv, auth_tag, status, enabled)
+ VALUES (?, ?, ?, ?, ?, ?, ?)
+ `).run('google', 'corrupt', 'not-hex', 'not-hex', 'not-hex', 'healthy', 1);
+
+ const groqKey = encrypt('test-groq-key');
+ db.prepare(`
+ INSERT INTO api_keys (platform, label, encrypted_key, iv, auth_tag, status, enabled)
+ VALUES (?, ?, ?, ?, ?, ?, ?)
+ `).run('groq', 'test', groqKey.encrypted, groqKey.iv, groqKey.authTag, 'healthy', 1);
+
+ const result = routeRequest();
+ const corruptKey = db.prepare("SELECT status FROM api_keys WHERE label = 'corrupt'").get() as { status: string };
+
+ expect(result.platform).toBe('groq');
+ expect(result.apiKey).toBe('test-groq-key');
+ expect(corruptKey.status).toBe('error');
+ });
});
diff --git a/server/src/services/router.ts b/server/src/services/router.ts
index ce540467..26168fa1 100644
--- a/server/src/services/router.ts
+++ b/server/src/services/router.ts
@@ -167,10 +167,10 @@ export function routeRequest(estimatedTokens = 1000, skipKeys?: Set<string>, pre
const provider = getProvider(model.platform as any);
if (!provider) continue;
- // Get all healthy, enabled keys for this platform
+ // Get enabled keys that have not already failed validation or decryption.
const keys = db.prepare(
- 'SELECT * FROM api_keys WHERE platform = ? AND enabled = 1 AND status != ?'
- ).all(model.platform, 'invalid') as KeyRow[];
+ "SELECT * FROM api_keys WHERE platform = ? AND enabled = 1 AND status IN ('healthy', 'unknown')"
+ ).all(model.platform) as KeyRow[];
if (keys.length === 0) continue;
@@ -199,10 +199,17 @@ export function routeRequest(estimatedTokens = 1000, skipKeys?: Set<string>, pre
if (!canMakeRequest(model.platform, model.model_id, key.id, limits)) continue;
if (!canUseTokens(model.platform, model.model_id, key.id, estimatedTokens, limits)) continue;
+ let decryptedKey: string;
+ try {
+ decryptedKey = decrypt(key.encrypted_key, key.iv, key.auth_tag);
+ } catch {
+ db.prepare("UPDATE api_keys SET status = 'error', last_checked_at = datetime('now') WHERE id = ?")
+ .run(key.id);
+ continue;
+ }
+
// We found a working key for this model!
roundRobinIndex.set(rrKey, idx);
- const decryptedKey = decrypt(key.encrypted_key, key.iv, key.auth_tag);
-
return {
provider,
modelId: model.model_id,