Spaces:
Runtime error
Runtime error
File size: 11,837 Bytes
077865a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | import { Router } from 'express';
import type { Request, Response } from 'express';
import { z } from 'zod';
import { getDb } from '../db/index.js';
import { resolveProvider } from '../providers/index.js';
import { encrypt, decrypt, maskKey } from '../lib/crypto.js';
export const keysRouter = Router();
// Active providers β must match providers/index.ts registrations + shared/types.ts Platform.
// Moonshot and MiniMax direct integrations were dropped in V4. HuggingFace
// was dropped in V4 and re-added in V13 via the router.huggingface.co route.
// SambaNova was dropped in V23 (free tier permanently retired).
const PLATFORMS = [
'google', 'groq', 'cerebras', 'nvidia', 'mistral',
'openrouter', 'github', 'cohere', 'cloudflare', 'zhipu', 'ollama',
'kilo', 'pollinations', 'llm7', 'huggingface', 'opencode', 'custom',
] as const;
// `key` is optional so keyless providers (Kilo's anonymous gateway) can be added
// without one; the handler enforces a non-empty key for everyone else.
const addKeySchema = z.object({
platform: z.enum(PLATFORMS),
key: z.string().optional(),
label: z.string().optional(),
});
const updateKeySchema = z.object({
enabled: z.boolean().optional(),
label: z.string().optional(),
}).refine(data => data.enabled !== undefined || data.label !== undefined, {
message: 'At least one of enabled or label must be provided',
});
// List all keys (masked)
keysRouter.get('/', (_req: Request, res: Response) => {
const db = getDb();
const rows = db.prepare('SELECT * FROM api_keys ORDER BY created_at DESC').all() as any[];
const keys = rows.map(row => {
let maskedKey = '****';
try {
const realKey = decrypt(row.encrypted_key, row.iv, row.auth_tag);
maskedKey = maskKey(realKey);
} catch {
maskedKey = '[decrypt failed]';
}
return {
id: row.id,
platform: row.platform,
label: row.label,
maskedKey,
baseUrl: row.base_url ?? null,
status: row.status,
enabled: row.enabled === 1,
createdAt: row.created_at,
lastCheckedAt: row.last_checked_at,
};
});
res.json(keys);
});
// Add a key
keysRouter.post('/', (req: Request, res: Response) => {
const parsed = addKeySchema.safeParse(req.body);
if (!parsed.success) {
res.status(400).json({ error: { message: parsed.error.errors.map(e => e.message).join(', ') } });
return;
}
const { platform, label } = parsed.data;
const isKeyless = resolveProvider(platform)?.keyless === true;
const rawKey = parsed.data.key?.trim() ?? '';
if (!isKeyless && !rawKey) {
res.status(400).json({ error: { message: 'key is required' } });
return;
}
// Keyless providers (Kilo anon) store a sentinel so routing sees the platform
// as configured; the provider omits the auth header on outgoing calls.
const keyToStore = isKeyless ? (rawKey || 'no-key') : rawKey;
const db = getDb();
// A keyless provider needs only one sentinel row β re-enable an existing one
// instead of piling up duplicates each time the user clicks "Add".
if (isKeyless) {
const existing = db.prepare('SELECT id FROM api_keys WHERE platform = ? LIMIT 1').get(platform) as { id: number } | undefined;
if (existing) {
db.prepare("UPDATE api_keys SET enabled = 1, status = 'unknown' WHERE id = ?").run(existing.id);
res.status(200).json({
id: existing.id,
platform,
label: label ?? '',
maskedKey: maskKey(keyToStore),
status: 'unknown',
enabled: true,
});
return;
}
}
const { encrypted, iv, authTag } = encrypt(keyToStore);
const result = db.prepare(`
INSERT INTO api_keys (platform, label, encrypted_key, iv, auth_tag, status, enabled)
VALUES (?, ?, ?, ?, ?, 'unknown', 1)
`).run(platform, label ?? '', encrypted, iv, authTag);
res.status(201).json({
id: result.lastInsertRowid,
platform,
label: label ?? '',
maskedKey: maskKey(keyToStore),
status: 'unknown',
enabled: true,
});
});
// ββ Custom OpenAI-compatible providers (#117, #212) βββββββββββββββββββββββ
// User-configured endpoints (llama.cpp / LM Studio / vLLM / Ollama / any
// OpenAI-compatible base_url). Each DISTINCT base_url gets its own 'custom'
// api_keys row, and every registered model binds to its endpoint's key via
// models.key_id β so several custom providers coexist without overwriting
// each other (#212). Re-submitting an existing base_url updates its key/label;
// re-registering an existing model id re-binds it to the submitted endpoint.
const customProviderSchema = z.object({
baseUrl: z.string().url('baseUrl must be a valid URL'),
model: z.string().min(1, 'model is required'),
displayName: z.string().optional(),
apiKey: z.string().optional(),
label: z.string().optional(),
});
keysRouter.post('/custom', (req: Request, res: Response) => {
const parsed = customProviderSchema.safeParse(req.body);
if (!parsed.success) {
res.status(400).json({ error: { message: parsed.error.errors.map(e => e.message).join(', ') } });
return;
}
const baseUrl = parsed.data.baseUrl.trim().replace(/\/+$/, '');
const modelId = parsed.data.model.trim();
const displayName = (parsed.data.displayName ?? modelId).trim();
// Local servers often need no key; keep a sentinel so there's always a bearer.
const rawKey = parsed.data.apiKey?.trim() || 'no-key';
const label = parsed.data.label ?? 'Custom';
const db = getDb();
const upsert = db.transaction(() => {
// One 'custom' key row PER ENDPOINT (matched on base_url). Re-submitting
// the same endpoint updates its key/label; a new base_url gets its own
// row instead of clobbering the previous provider. (#212)
const existing = db.prepare("SELECT id FROM api_keys WHERE platform = 'custom' AND base_url = ? LIMIT 1")
.get(baseUrl) as { id: number } | undefined;
let keyId: number;
if (existing) {
const { encrypted, iv, authTag } = encrypt(rawKey);
db.prepare("UPDATE api_keys SET label = ?, encrypted_key = ?, iv = ?, auth_tag = ?, status = 'unknown', enabled = 1 WHERE id = ?")
.run(label, encrypted, iv, authTag, existing.id);
keyId = existing.id;
} else {
const { encrypted, iv, authTag } = encrypt(rawKey);
const r = db.prepare(`
INSERT INTO api_keys (platform, label, encrypted_key, iv, auth_tag, status, enabled, base_url)
VALUES ('custom', ?, ?, ?, ?, 'unknown', 1, ?)
`).run(label, encrypted, iv, authTag, baseUrl);
keyId = Number(r.lastInsertRowid);
}
// Register the model bound to THIS endpoint's key. Custom models carry no
// rate limits and sort last in the intelligence preset (size_label tier).
// Re-registering an existing model id re-binds it (model ids are unique
// per platform, so one id can't live on two endpoints at once).
db.prepare(`
INSERT INTO models
(platform, model_id, display_name, intelligence_rank, speed_rank, size_label,
rpm_limit, rpd_limit, tpm_limit, tpd_limit, monthly_token_budget, context_window, enabled, key_id)
VALUES ('custom', ?, ?, 50, 50, 'Custom', NULL, NULL, NULL, NULL, '', NULL, 1, ?)
ON CONFLICT(platform, model_id)
DO UPDATE SET display_name = excluded.display_name, key_id = excluded.key_id, enabled = 1
`).run(modelId, displayName, keyId);
const modelRow = db.prepare("SELECT id FROM models WHERE platform = 'custom' AND model_id = ?").get(modelId) as { id: number };
// Append to the fallback chain if not already present.
const inChain = db.prepare('SELECT 1 FROM fallback_config WHERE model_db_id = ?').get(modelRow.id);
if (!inChain) {
const max = db.prepare('SELECT COALESCE(MAX(priority), 0) AS m FROM fallback_config').get() as { m: number };
db.prepare('INSERT INTO fallback_config (model_db_id, priority, enabled) VALUES (?, ?, 1)').run(modelRow.id, max.m + 1);
}
return { keyId, modelDbId: modelRow.id };
});
const { keyId, modelDbId } = upsert();
res.status(201).json({
success: true,
keyId,
modelDbId,
platform: 'custom',
baseUrl,
model: modelId,
displayName,
maskedKey: maskKey(rawKey),
});
});
// Delete a key
keysRouter.delete('/:id', (req: Request, res: Response) => {
const id = parseInt(req.params.id as string, 10);
if (isNaN(id)) {
res.status(400).json({ error: { message: 'Invalid key ID' } });
return;
}
const db = getDb();
const row = db.prepare('SELECT platform FROM api_keys WHERE id = ?').get(id) as { platform: string } | undefined;
if (!row) {
res.status(404).json({ error: { message: 'Key not found' } });
return;
}
const remove = db.transaction(() => {
db.prepare('DELETE FROM api_keys WHERE id = ?').run(id);
// Custom models exist only because POST /custom registered them alongside
// their endpoint key (#117) β they can't route without it. Cascade away
// the models bound to THIS endpoint (#212); other custom providers keep
// theirs. Legacy rows (key_id NULL) are swept once no custom keys remain,
// so they never linger in the fallback chain forever (#189).
if (row.platform === 'custom') {
db.prepare("DELETE FROM fallback_config WHERE model_db_id IN (SELECT id FROM models WHERE platform = 'custom' AND key_id = ?)").run(id);
db.prepare("DELETE FROM models WHERE platform = 'custom' AND key_id = ?").run(id);
const remaining = db.prepare("SELECT COUNT(*) AS n FROM api_keys WHERE platform = 'custom'").get() as { n: number };
if (remaining.n === 0) {
db.prepare("DELETE FROM fallback_config WHERE model_db_id IN (SELECT id FROM models WHERE platform = 'custom')").run();
db.prepare("DELETE FROM models WHERE platform = 'custom'").run();
}
}
});
remove();
res.json({ success: true });
});
// Toggle all keys for a platform
keysRouter.patch('/platform/:platform', (req: Request, res: Response) => {
const platform = req.params.platform as string;
if (!(PLATFORMS as readonly string[]).includes(platform)) {
res.status(400).json({ error: { message: `Invalid platform '${platform}'` } });
return;
}
const { enabled } = req.body;
if (typeof enabled !== 'boolean') {
res.status(400).json({ error: { message: 'enabled must be a boolean' } });
return;
}
const db = getDb();
const result = db.prepare('UPDATE api_keys SET enabled = ? WHERE platform = ?').run(enabled ? 1 : 0, platform);
res.json({ success: true, enabled, updatedKeys: result.changes });
});
// Update key (toggle enable/disable or edit label)
keysRouter.patch('/:id', (req: Request, res: Response) => {
const id = parseInt(req.params.id as string, 10);
if (isNaN(id)) {
res.status(400).json({ error: { message: 'Invalid key ID' } });
return;
}
const parsed = updateKeySchema.safeParse(req.body);
if (!parsed.success) {
res.status(400).json({ error: { message: parsed.error.errors.map(e => e.message).join(', ') } });
return;
}
const { enabled, label } = parsed.data;
const updates: string[] = [];
const values: (string | number)[] = [];
if (enabled !== undefined) {
updates.push('enabled = ?');
values.push(enabled ? 1 : 0);
}
if (label !== undefined) {
updates.push('label = ?');
values.push(label);
}
values.push(id);
const db = getDb();
const result = db.prepare(`UPDATE api_keys SET ${updates.join(', ')} WHERE id = ?`).run(...values);
if (result.changes === 0) {
res.status(404).json({ error: { message: 'Key not found' } });
return;
}
const response: Record<string, unknown> = { success: true };
if (enabled !== undefined) response.enabled = enabled;
if (label !== undefined) response.label = label;
res.json(response);
});
|