fokemal / server /src /db /index.ts
automindy's picture
Upload 603 files
5569a86 verified
Raw
History Blame Contribute Delete
50.7 kB
import crypto from 'crypto';
import { initClient, getDb, type TursoDb } from './client.js';
import { initEncryptionKey } from '../lib/crypto.js';
import { applyModelPricing } from './model-pricing.js';
export { getDb } from './client.js';
export type { TursoDb } from './client.js';
export async function initDb(dbUrl?: string): Promise<TursoDb> {
const db = initClient(dbUrl);
await createTables(db);
await initEncryptionKey(db);
await seedModels(db);
await migrateModels(db);
await migrateModelsV2(db);
await migrateModelsV3Ranks(db);
await migrateModelsV4(db);
await migrateModelsV5(db);
await migrateModelsV6(db);
await migrateModelsV7(db);
await migrateModelsV8(db);
await migrateModelsV9(db);
await migrateModelsV10(db);
await migrateModelsV11(db);
await migrateModelsV12(db);
await migrateModelsV13(db);
await migrateModelsV14(db);
await migrateModelsV15(db);
await migrateModelsV16Vision(db);
await migrateModelsV17IntelligenceTiers(db);
await migrateModelsV18OpenCodeZen(db);
await migrateModelsV19Gemma4(db);
await migrateModelsV20KiloFree(db);
await migrateModelsV21PruneDead(db);
await migrateModelsV22Tools(db);
await applyModelPricing(db);
await migrateEmbeddingsV1(db);
await ensureUnifiedKey(db);
const url = dbUrl ?? process.env.TURSO_DATABASE_URL ?? 'file:./data/freeapi.db';
console.log(`Database initialized at ${url}`);
return db;
}
async function createTables(db: TursoDb) {
// Turso doesn't support multi-statement exec in one call reliably,
// so we split the statements.
const statements = [
`CREATE TABLE IF NOT EXISTS models (
id INTEGER PRIMARY KEY AUTOINCREMENT,
platform TEXT NOT NULL,
model_id TEXT NOT NULL,
display_name TEXT NOT NULL,
intelligence_rank INTEGER NOT NULL,
speed_rank INTEGER NOT NULL,
size_label TEXT NOT NULL DEFAULT '',
rpm_limit INTEGER,
rpd_limit INTEGER,
tpm_limit INTEGER,
tpd_limit INTEGER,
monthly_token_budget TEXT NOT NULL DEFAULT '',
context_window INTEGER,
enabled INTEGER NOT NULL DEFAULT 1,
supports_vision INTEGER NOT NULL DEFAULT 0,
UNIQUE(platform, model_id)
)`,
`CREATE TABLE IF NOT EXISTS api_keys (
id INTEGER PRIMARY KEY AUTOINCREMENT,
platform TEXT NOT NULL,
label TEXT NOT NULL DEFAULT '',
encrypted_key TEXT NOT NULL,
iv TEXT NOT NULL,
auth_tag TEXT NOT NULL,
status TEXT NOT NULL DEFAULT 'unknown',
enabled INTEGER NOT NULL DEFAULT 1,
created_at TEXT NOT NULL DEFAULT (datetime('now')),
last_checked_at TEXT
)`,
`CREATE TABLE IF NOT EXISTS requests (
id INTEGER PRIMARY KEY AUTOINCREMENT,
platform TEXT NOT NULL,
model_id TEXT NOT NULL,
key_id INTEGER,
status TEXT NOT NULL,
input_tokens INTEGER NOT NULL DEFAULT 0,
output_tokens INTEGER NOT NULL DEFAULT 0,
latency_ms INTEGER NOT NULL DEFAULT 0,
error TEXT,
created_at TEXT NOT NULL DEFAULT (datetime('now'))
)`,
`CREATE TABLE IF NOT EXISTS rate_limit_usage (
id INTEGER PRIMARY KEY AUTOINCREMENT,
platform TEXT NOT NULL,
model_id TEXT NOT NULL,
key_id INTEGER NOT NULL,
kind TEXT NOT NULL CHECK (kind IN ('request', 'tokens')),
tokens INTEGER NOT NULL DEFAULT 0,
created_at_ms INTEGER NOT NULL,
created_at TEXT NOT NULL DEFAULT (datetime('now'))
)`,
`CREATE TABLE IF NOT EXISTS rate_limit_cooldowns (
platform TEXT NOT NULL,
model_id TEXT NOT NULL,
key_id INTEGER NOT NULL,
expires_at_ms INTEGER NOT NULL,
created_at TEXT NOT NULL DEFAULT (datetime('now')),
PRIMARY KEY (platform, model_id, key_id)
)`,
`CREATE TABLE IF NOT EXISTS fallback_config (
id INTEGER PRIMARY KEY AUTOINCREMENT,
model_db_id INTEGER NOT NULL REFERENCES models(id),
priority INTEGER NOT NULL,
enabled INTEGER NOT NULL DEFAULT 1,
UNIQUE(model_db_id)
)`,
`CREATE TABLE IF NOT EXISTS settings (
key TEXT PRIMARY KEY,
value TEXT NOT NULL
)`,
`CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT NOT NULL UNIQUE,
password_hash TEXT NOT NULL,
created_at TEXT NOT NULL DEFAULT (datetime('now'))
)`,
`CREATE TABLE IF NOT EXISTS sessions (
token_hash TEXT PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
expires_at_ms INTEGER NOT NULL,
created_at TEXT NOT NULL DEFAULT (datetime('now'))
)`,
`CREATE INDEX IF NOT EXISTS idx_sessions_user ON sessions(user_id)`,
`CREATE INDEX IF NOT EXISTS idx_requests_created_at ON requests(created_at)`,
`CREATE INDEX IF NOT EXISTS idx_requests_platform ON requests(platform)`,
`CREATE INDEX IF NOT EXISTS idx_rate_limit_usage_lookup ON rate_limit_usage(platform, model_id, key_id, kind, created_at_ms)`,
`CREATE INDEX IF NOT EXISTS idx_rate_limit_cooldowns_expires ON rate_limit_cooldowns(expires_at_ms)`,
`CREATE INDEX IF NOT EXISTS idx_api_keys_platform ON api_keys(platform)`,
];
for (const sql of statements) {
await db.run(sql);
}
await ensureRequestKeyIdColumn(db);
await ensureApiKeysBaseUrlColumn(db);
await ensureModelsKeyIdColumn(db);
await ensureRequestTtfbColumn(db);
await ensureRequestRequestedModelColumn(db);
}
async function ensureColumn(db: TursoDb, table: string, column: string, definition: string) {
const columns = await db.all<{ name: string }>(`PRAGMA table_info(${table})`);
if (!columns.some(col => col.name === column)) {
await db.run(`ALTER TABLE ${table} ADD COLUMN ${column} ${definition}`);
}
}
async function ensureRequestRequestedModelColumn(db: TursoDb) {
await ensureColumn(db, 'requests', 'requested_model', 'TEXT');
}
async function ensureRequestTtfbColumn(db: TursoDb) {
await ensureColumn(db, 'requests', 'ttfb_ms', 'INTEGER');
}
async function ensureRequestKeyIdColumn(db: TursoDb) {
await ensureColumn(db, 'requests', 'key_id', 'INTEGER');
await db.run('CREATE INDEX IF NOT EXISTS idx_requests_key_id ON requests(key_id)');
}
async function ensureApiKeysBaseUrlColumn(db: TursoDb) {
await ensureColumn(db, 'api_keys', 'base_url', 'TEXT');
}
async function ensureModelsKeyIdColumn(db: TursoDb) {
const columns = await db.all<{ name: string }>('PRAGMA table_info(models)');
if (!columns.some(col => col.name === 'key_id')) {
await db.run('ALTER TABLE models ADD COLUMN key_id INTEGER');
await db.run(`
UPDATE models
SET key_id = (SELECT id FROM api_keys WHERE platform = 'custom' ORDER BY id LIMIT 1)
WHERE platform = 'custom' AND key_id IS NULL
`);
}
}
// ── Helper: insert models + backfill fallback ──────────────────────────────
async function insertModels(db: TursoDb, models: Array<any[]>) {
for (const m of models) {
await db.run(
`INSERT OR IGNORE 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)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
m,
);
}
}
async function backfillFallback(db: TursoDb) {
const missing = await db.all<{ id: number }>(
`SELECT m.id FROM models m
LEFT JOIN fallback_config f ON m.id = f.model_db_id
WHERE f.id IS NULL ORDER BY m.intelligence_rank ASC`,
);
if (missing.length > 0) {
const row = await db.get<{ mx: number }>('SELECT COALESCE(MAX(priority), 0) AS mx FROM fallback_config');
const maxPriority = row?.mx ?? 0;
for (let i = 0; i < missing.length; i++) {
await db.run(
'INSERT INTO fallback_config (model_db_id, priority, enabled) VALUES (?, ?, 1)',
[missing[i].id, maxPriority + i + 1],
);
}
}
}
async function removeModels(db: TursoDb, removals: Array<[string, string]>) {
for (const [p, m] of removals) {
await db.run(
'DELETE FROM fallback_config WHERE model_db_id IN (SELECT id FROM models WHERE platform = ? AND model_id = ?)',
[p, m],
);
await db.run('DELETE FROM models WHERE platform = ? AND model_id = ?', [p, m]);
}
}
// ── Seed & Migrations ──────────────────────────────────────────────────────
async function seedModels(db: TursoDb) {
const count = await db.get<{ cnt: number }>('SELECT COUNT(*) as cnt FROM models');
if ((count?.cnt ?? 0) > 0) return;
const models = [
['google', 'gemini-2.5-pro', 'Gemini 2.5 Pro', 1, 8, 'Frontier', 5, 100, 250000, null, '~12M', 1048576],
['google', 'gemini-2.5-flash', 'Gemini 2.5 Flash', 4, 5, 'Large', 10, 20, 250000, null, '~3M', 1048576],
['google', 'gemini-2.5-flash-lite', 'Gemini 2.5 Flash-Lite', 8, 3, 'Medium', 15, 1000, 250000, null, '~120M', 1048576],
['openrouter', 'deepseek/deepseek-v3.1:free', 'DeepSeek V3.1 (free)', 2, 10, 'Frontier', 20, 200, null, null, '~6M', 131072],
['openrouter', 'moonshotai/kimi-k2:free', 'Kimi K2 (free)', 2, 9, 'Frontier', 20, 200, null, null, '~6M', 131072],
['openrouter', 'qwen/qwen3-coder:free', 'Qwen3 Coder (free)', 3, 9, 'Frontier', 20, 200, null, null, '~6M', 262144],
['openrouter', 'z-ai/glm-4.5-air:free', 'GLM-4.5 Air (free)', 4, 9, 'Large', 20, 200, null, null, '~6M', 131072],
['cerebras', 'qwen-3-coder-480b', 'Qwen3-Coder 480B', 2, 1, 'Frontier', 30, null, 60000, 1000000, '~30M', 131072],
['cerebras', 'llama-4-maverick-17b-128e-instruct', 'Llama 4 Maverick', 3, 1, 'Frontier', 30, null, 60000, 1000000, '~30M', 131072],
['cerebras', 'qwen3-235b', 'Qwen3 235B', 3, 1, 'Large', 30, null, 60000, 1000000, '~30M', 8192],
['cerebras', 'gpt-oss-120b', 'GPT-OSS 120B', 3, 1, 'Large', 30, null, 60000, 1000000, '~30M', 131072],
['github', 'openai/gpt-5', 'GPT-5 (GitHub)', 1, 7, 'Frontier', 10, 50, null, null, '~18M', 128000],
['sambanova', 'Meta-Llama-3.3-70B-Instruct', 'Llama 3.3 70B', 6, 9, 'Large', 20, null, null, 200000, '~6M', 8192],
['mistral', 'mistral-large-latest', 'Mistral Large 3', 7, 8, 'Large', 2, null, 500000, null, '~50-100M', 131072],
['mistral', 'magistral-medium-latest', 'Magistral Medium', 4, 8, 'Large', 2, null, 500000, null, '~50-100M', 40000],
['mistral', 'codestral-latest', 'Codestral', 6, 6, 'Medium', 2, null, 500000, null, '~50-100M', 32000],
['groq', 'llama-3.3-70b-versatile', 'Llama 3.3 70B', 9, 2, 'Medium', 30, 1000, 6000, 500000, '~15M', 131072],
['groq', 'llama-4-scout-17b-16e-instruct', 'Llama 4 Scout', 10, 2, 'Medium', 30, 1000, 6000, 1000000, '~30M', 131072],
['nvidia', 'meta/llama-3.1-70b-instruct', 'Llama 3.1 70B (NV)', 11, 6, 'Large', 40, null, null, null, 'credits-based', 131072],
['cohere', 'command-r-plus-08-2024', 'Command R+ (08-2024)', 12, 11, 'Large', 20, 33, null, null, '~1-2M', 131072],
['cloudflare', '@cf/meta/llama-3.1-70b-instruct', 'Llama 3.1 70B (CF)', 13, 11, 'Medium', null, null, null, null, '~18-45M', 131072],
['huggingface', 'accounts/fireworks/models/llama-v3p3-70b-instruct', 'Llama 3.3 70B (HF)', 14, 11, 'Medium', null, null, null, null, '~1-3M', 131072],
['zhipu', 'glm-4.5-flash', 'GLM-4.5 Flash', 5, 4, 'Large', null, null, null, 1000000, '~30M', 131072],
['moonshot', 'kimi-latest', 'Kimi Latest', 4, 8, 'Large', 60, null, null, 500000, '~15M', 200000],
['minimax', 'MiniMax-M1', 'MiniMax M1', 5, 8, 'Large', 20, null, 1000000, null, '~30M', 200000],
];
await insertModels(db, models);
// Seed default fallback config
const allModels = await db.all<{ id: number; intelligence_rank: number }>(
'SELECT id, intelligence_rank FROM models ORDER BY intelligence_rank ASC',
);
for (let i = 0; i < allModels.length; i++) {
await db.run(
'INSERT INTO fallback_config (model_db_id, priority, enabled) VALUES (?, ?, 1)',
[allModels[i].id, i + 1],
);
}
console.log(`Seeded ${models.length} models and fallback config`);
}
async function migrateModels(db: TursoDb) {
// DeepSeek R1 (free) -> DeepSeek V3.1 (free)
await db.run(
`UPDATE models SET model_id = ?, display_name = ?, intelligence_rank = ?,
monthly_token_budget = ?, rpd_limit = COALESCE(?, rpd_limit),
context_window = COALESCE(?, context_window),
size_label = COALESCE(?, size_label)
WHERE platform = ? AND model_id = ?`,
['deepseek/deepseek-v3.1:free', 'DeepSeek V3.1 (free)', 2, '~6M', 200, 131072, 'Frontier', 'openrouter', 'deepseek/deepseek-r1:free'],
);
// GitHub GPT-4o -> GPT-5
await db.run(
`UPDATE models SET model_id = ?, display_name = ?, intelligence_rank = ?,
monthly_token_budget = ?, rpd_limit = COALESCE(?, rpd_limit),
context_window = COALESCE(?, context_window),
size_label = COALESCE(?, size_label)
WHERE platform = ? AND model_id = ?`,
['openai/gpt-5', 'GPT-5 (GitHub)', 1, '~18M', null, 128000, 'Frontier', 'github', 'gpt-4o'],
);
// Correct stale limits
await db.run(`UPDATE models SET rpd_limit = 20, monthly_token_budget = '~3M' WHERE platform = 'google' AND model_id = 'gemini-2.5-flash'`);
await db.run(`UPDATE models SET rpm_limit = 20 WHERE platform = 'sambanova' AND model_id = 'Meta-Llama-3.3-70B-Instruct'`);
await db.run(`UPDATE models SET tpm_limit = 6000 WHERE platform = 'groq' AND model_id = 'llama-4-scout-17b-16e-instruct'`);
await db.run(`UPDATE models SET monthly_token_budget = '~1-2M' WHERE platform = 'cohere' AND model_id = 'command-r-plus-08-2024'`);
await db.run(`UPDATE models SET monthly_token_budget = '~1-3M' WHERE platform = 'huggingface' AND model_id = 'accounts/fireworks/models/llama-v3p3-70b-instruct'`);
await db.run(`UPDATE models SET monthly_token_budget = 'credits-based', enabled = 0 WHERE platform = 'nvidia' AND model_id = 'meta/llama-3.1-70b-instruct'`);
// Insert new models
const newModels = [
['cerebras', 'qwen-3-coder-480b', 'Qwen3-Coder 480B', 2, 1, 'Frontier', 30, null, 60000, 1000000, '~30M', 131072],
['cerebras', 'llama-4-maverick-17b-128e-instruct', 'Llama 4 Maverick', 3, 1, 'Frontier', 30, null, 60000, 1000000, '~30M', 131072],
['cerebras', 'gpt-oss-120b', 'GPT-OSS 120B', 3, 1, 'Large', 30, null, 60000, 1000000, '~30M', 131072],
['openrouter', 'deepseek/deepseek-v3.1:free', 'DeepSeek V3.1 (free)', 2, 10, 'Frontier', 20, 200, null, null, '~6M', 131072],
['openrouter', 'moonshotai/kimi-k2:free', 'Kimi K2 (free)', 2, 9, 'Frontier', 20, 200, null, null, '~6M', 131072],
['openrouter', 'qwen/qwen3-coder:free', 'Qwen3 Coder (free)', 3, 9, 'Frontier', 20, 200, null, null, '~6M', 262144],
['openrouter', 'z-ai/glm-4.5-air:free', 'GLM-4.5 Air (free)', 4, 9, 'Large', 20, 200, null, null, '~6M', 131072],
['mistral', 'magistral-medium-latest', 'Magistral Medium', 4, 8, 'Large', 2, null, 500000, null, '~50-100M', 40000],
['mistral', 'codestral-latest', 'Codestral', 6, 6, 'Medium', 2, null, 500000, null, '~50-100M', 32000],
['zhipu', 'glm-4.5-flash', 'GLM-4.5 Flash', 5, 4, 'Large', null, null, null, 1000000, '~30M', 131072],
['moonshot', 'kimi-latest', 'Kimi Latest', 4, 8, 'Large', 60, null, null, 500000, '~15M', 200000],
['minimax', 'MiniMax-M1', 'MiniMax M1', 5, 8, 'Large', 20, null, 1000000, null, '~30M', 200000],
];
await insertModels(db, newModels);
await backfillFallback(db);
}
async function migrateModelsV2(db: TursoDb) {
const removals: Array<[string, string]> = [
['cerebras', 'qwen-3-coder-480b'],
['cerebras', 'llama-4-maverick-17b-128e-instruct'],
['cerebras', 'gpt-oss-120b'],
['openrouter', 'deepseek/deepseek-v3.1:free'],
['openrouter', 'moonshotai/kimi-k2:free'],
];
await removeModels(db, removals);
await db.run(`
UPDATE models
SET model_id = 'gpt-4o', display_name = 'GPT-4o', intelligence_rank = 5,
size_label = 'Large', context_window = 8000, monthly_token_budget = '~18M'
WHERE platform = 'github' AND model_id = 'openai/gpt-5'
`);
await db.run(`
UPDATE models SET model_id = 'meta-llama/llama-4-scout-17b-16e-instruct'
WHERE platform = 'groq' AND model_id = 'llama-4-scout-17b-16e-instruct'
`);
const additions = [
['openrouter', 'nvidia/nemotron-3-super-120b-a12b:free', 'Nemotron 3 Super 120B (free)', 2, 9, 'Frontier', 20, 200, null, null, '~6M', 262144],
['openrouter', 'qwen/qwen3-next-80b-a3b-instruct:free', 'Qwen3-Next 80B (free)', 3, 9, 'Large', 20, 200, null, null, '~6M', 262144],
['openrouter', 'minimax/minimax-m2.5:free', 'MiniMax M2.5 (free)', 3, 9, 'Large', 20, 200, null, null, '~6M', 196608],
['openrouter', 'google/gemma-4-31b-it:free', 'Gemma 4 31B (free)', 5, 9, 'Medium', 20, 200, null, null, '~6M', 262144],
];
await insertModels(db, additions);
await backfillFallback(db);
}
async function migrateModelsV3Ranks(db: TursoDb) {
const ranks: Array<[number, string, string]> = [
[1, 'openrouter', 'minimax/minimax-m2.5:free'],
[2, 'openrouter', 'qwen/qwen3-coder:free'],
[3, 'openrouter', 'qwen/qwen3-next-80b-a3b-instruct:free'],
[4, 'moonshot', 'kimi-latest'],
[5, 'cerebras', 'qwen-3-235b-a22b-instruct-2507'],
[6, 'google', 'gemini-2.5-pro'],
[7, 'openrouter', 'z-ai/glm-4.5-air:free'],
[8, 'openrouter', 'openai/gpt-oss-120b:free'],
[9, 'openrouter', 'nvidia/nemotron-3-super-120b-a12b:free'],
[10, 'minimax', 'MiniMax-M1'],
[11, 'mistral', 'codestral-latest'],
[12, 'mistral', 'mistral-large-latest'],
[13, 'mistral', 'magistral-medium-latest'],
[14, 'google', 'gemini-2.5-flash'],
[15, 'zhipu', 'glm-4.5-flash'],
[16, 'groq', 'llama-3.3-70b-versatile'],
[16, 'sambanova', 'Meta-Llama-3.3-70B-Instruct'],
[16, 'openrouter', 'meta-llama/llama-3.3-70b-instruct:free'],
[16, 'huggingface', 'accounts/fireworks/models/llama-v3p3-70b-instruct'],
[17, 'openrouter', 'nousresearch/hermes-3-llama-3.1-405b:free'],
[18, 'groq', 'meta-llama/llama-4-scout-17b-16e-instruct'],
[19, 'openrouter', 'google/gemma-4-31b-it:free'],
[20, 'google', 'gemini-2.5-flash-lite'],
[21, 'github', 'gpt-4o'],
[22, 'nvidia', 'meta/llama-3.1-70b-instruct'],
[22, 'cloudflare', '@cf/meta/llama-3.1-70b-instruct'],
[23, 'cohere', 'command-r-plus-08-2024'],
];
for (const [rank, platform, modelId] of ranks) {
await db.run('UPDATE models SET intelligence_rank = ? WHERE platform = ? AND model_id = ?', [rank, platform, modelId]);
}
}
async function migrateModelsV4(db: TursoDb) {
const removals: Array<[string, string]> = [
['moonshot', 'kimi-latest'],
['minimax', 'MiniMax-M1'],
['openrouter', 'google/gemma-4-31b-it:free'],
['huggingface', 'accounts/fireworks/models/llama-v3p3-70b-instruct'],
];
await removeModels(db, removals);
await db.run(`
UPDATE models
SET model_id = '@cf/meta/llama-3.3-70b-instruct-fp8-fast',
display_name = 'Llama 3.3 70B fp8-fast (CF)',
context_window = 131072
WHERE platform = 'cloudflare' AND model_id = '@cf/meta/llama-3.1-70b-instruct'
`);
await db.run(`UPDATE models SET tpm_limit = 12000 WHERE platform = 'groq' AND model_id = 'llama-3.3-70b-versatile'`);
await db.run(`UPDATE models SET rpd_limit = 20 WHERE platform = 'sambanova' AND model_id = 'Meta-Llama-3.3-70B-Instruct'`);
await db.run(`UPDATE models SET rpd_limit = 14400 WHERE platform = 'cerebras' AND model_id = 'qwen-3-235b-a22b-instruct-2507'`);
await db.run(`UPDATE models SET rpd_limit = 250, monthly_token_budget = '~25M' WHERE platform = 'google' AND model_id = 'gemini-2.5-flash'`);
await db.run(`UPDATE models SET rpd_limit = 50, monthly_token_budget = '~6M' WHERE platform = 'google' AND model_id = 'gemini-2.5-pro'`);
const additions = [
['openrouter', 'inclusionai/ling-2.6-flash:free', 'Ling 2.6 Flash (free)', 7, 9, 'Large', 20, 200, null, null, '~6M', 262144],
['openrouter', 'arcee-ai/trinity-large-preview:free', 'Trinity Large Preview (free)', 13, 9, 'Frontier', 20, 200, null, null, '~6M', 131072],
['openrouter', 'nvidia/nemotron-3-nano-30b-a3b:free', 'Nemotron 3 Nano 30B (free)', 22, 9, 'Medium', 20, 200, null, null, '~6M', 262144],
['openrouter', 'openai/gpt-oss-120b:free', 'GPT-OSS 120B (free)', 6, 9, 'Large', 20, 200, null, null, '~6M', 131072],
['openrouter', 'openai/gpt-oss-20b:free', 'GPT-OSS 20B (free)', 18, 9, 'Medium', 20, 200, null, null, '~6M', 131072],
['openrouter', 'meta-llama/llama-3.3-70b-instruct:free', 'Llama 3.3 70B (free)', 17, 9, 'Medium', 20, 200, null, null, '~6M', 131072],
['sambanova', 'DeepSeek-V3.1', 'DeepSeek V3.1', 5, 9, 'Frontier', 20, 20, null, 200000, '~3M', 131072],
['sambanova', 'DeepSeek-V3.2', 'DeepSeek V3.2', 4, 9, 'Frontier', 20, 20, null, 200000, '~3M', 131072],
['sambanova', 'Llama-4-Maverick-17B-128E-Instruct', 'Llama 4 Maverick', 11, 9, 'Large', 20, 20, null, 200000, '~3M', 8192],
['sambanova', 'gpt-oss-120b', 'GPT-OSS 120B (SambaNova)', 6, 9, 'Large', 20, 20, null, 200000, '~3M', 131072],
['groq', 'openai/gpt-oss-120b', 'GPT-OSS 120B (Groq)', 6, 2, 'Large', 30, 1000, 8000, 200000, '~6M', 131072],
['groq', 'openai/gpt-oss-20b', 'GPT-OSS 20B (Groq)', 18, 2, 'Medium', 30, 1000, 8000, 200000, '~6M', 131072],
['groq', 'qwen/qwen3-32b', 'Qwen3 32B (Groq)', 19, 2, 'Medium', 60, 1000, 6000, 500000, '~15M', 131072],
['groq', 'llama-3.1-8b-instant', 'Llama 3.1 8B Instant', 28, 2, 'Small', 30, 14400, 6000, 500000, '~15M', 131072],
['mistral', 'devstral-latest', 'Devstral', 16, 8, 'Medium', 2, null, 500000, null, '~50-100M', 131072],
['mistral', 'mistral-medium-latest', 'Mistral Medium 3.5', 14, 8, 'Large', 2, null, 500000, null, '~50-100M', 131072],
['github', 'openai/gpt-4.1', 'GPT-4.1 (GitHub)', 20, 7, 'Large', 10, 50, null, null, '~9M', 128000],
['cohere', 'command-a-03-2025', 'Command-A (03-2025)', 27, 11, 'Large', 20, 33, null, null, '~1-2M', 131072],
['cloudflare', '@cf/openai/gpt-oss-120b', 'GPT-OSS 120B (CF)', 6, 11, 'Large', null, null, null, null, '~18-45M', 131072],
['cloudflare', '@cf/zai-org/glm-4.7-flash', 'GLM-4.7 Flash (CF)', 10, 11, 'Large', null, null, null, null, '~18-45M', 131072],
['cloudflare', '@cf/meta/llama-4-scout-17b-16e-instruct', 'Llama 4 Scout (CF)', 12, 11, 'Large', null, null, null, null, '~18-45M', 131072],
];
await insertModels(db, additions);
await backfillFallback(db);
// Re-rank
const ranks: Array<[number, string, string]> = [
[1, 'openrouter', 'minimax/minimax-m2.5:free'],
[2, 'openrouter', 'qwen/qwen3-coder:free'],
[3, 'openrouter', 'qwen/qwen3-next-80b-a3b-instruct:free'],
[4, 'sambanova', 'DeepSeek-V3.2'],
[5, 'sambanova', 'DeepSeek-V3.1'],
[6, 'cerebras', 'qwen-3-235b-a22b-instruct-2507'],
[6, 'openrouter', 'openai/gpt-oss-120b:free'],
[6, 'groq', 'openai/gpt-oss-120b'],
[6, 'sambanova', 'gpt-oss-120b'],
[6, 'cloudflare', '@cf/openai/gpt-oss-120b'],
[7, 'openrouter', 'inclusionai/ling-2.6-flash:free'],
[8, 'openrouter', 'z-ai/glm-4.5-air:free'],
[10, 'cloudflare', '@cf/zai-org/glm-4.7-flash'],
[11, 'sambanova', 'Llama-4-Maverick-17B-128E-Instruct'],
[12, 'groq', 'meta-llama/llama-4-scout-17b-16e-instruct'],
[12, 'cloudflare', '@cf/meta/llama-4-scout-17b-16e-instruct'],
[13, 'openrouter', 'arcee-ai/trinity-large-preview:free'],
[14, 'google', 'gemini-2.5-pro'],
[14, 'mistral', 'mistral-large-latest'],
[14, 'mistral', 'mistral-medium-latest'],
[16, 'mistral', 'devstral-latest'],
[16, 'mistral', 'codestral-latest'],
[17, 'groq', 'llama-3.3-70b-versatile'],
[17, 'sambanova', 'Meta-Llama-3.3-70B-Instruct'],
[17, 'cloudflare', '@cf/meta/llama-3.3-70b-instruct-fp8-fast'],
[17, 'openrouter', 'meta-llama/llama-3.3-70b-instruct:free'],
[17, 'nvidia', 'meta/llama-3.1-70b-instruct'],
[18, 'openrouter', 'openai/gpt-oss-20b:free'],
[18, 'groq', 'openai/gpt-oss-20b'],
[19, 'groq', 'qwen/qwen3-32b'],
[20, 'google', 'gemini-2.5-flash'],
[20, 'github', 'openai/gpt-4.1'],
[21, 'mistral', 'magistral-medium-latest'],
[22, 'openrouter', 'nvidia/nemotron-3-super-120b-a12b:free'],
[23, 'openrouter', 'nvidia/nemotron-3-nano-30b-a3b:free'],
[24, 'zhipu', 'glm-4.5-flash'],
[25, 'github', 'gpt-4o'],
[26, 'google', 'gemini-2.5-flash-lite'],
[27, 'cohere', 'command-a-03-2025'],
[27, 'cohere', 'command-r-plus-08-2024'],
[28, 'groq', 'llama-3.1-8b-instant'],
];
for (const [r, p, m] of ranks) {
await db.run('UPDATE models SET intelligence_rank = ? WHERE platform = ? AND model_id = ?', [r, p, m]);
}
}
async function migrateModelsV5(db: TursoDb) {
await db.run(`UPDATE models SET enabled = 0 WHERE platform = 'google' AND model_id = 'gemini-2.5-pro'`);
await insertModels(db, [
['cerebras', 'zai-glm-4.7', 'GLM-4.7 (Cerebras)', 7, 1, 'Frontier', 10, 100, null, null, '~3M', 8192],
]);
await backfillFallback(db);
}
async function migrateModelsV6(db: TursoDb) {
await removeModels(db, [['openrouter', 'arcee-ai/trinity-large-preview:free']]);
await db.run(`UPDATE models SET rpd_limit = 20, monthly_token_budget = '~3M' WHERE platform = 'google' AND model_id = 'gemini-2.5-flash'`);
await db.run(`UPDATE models SET rpd_limit = 20, monthly_token_budget = '~3M' WHERE platform = 'google' AND model_id = 'gemini-2.5-flash-lite'`);
const additions = [
['cloudflare', '@cf/moonshotai/kimi-k2.5', 'Kimi K2.5 (CF)', 3, 11, 'Frontier', null, null, null, null, '~10-20M', 262144],
['cloudflare', '@cf/qwen/qwen3-30b-a3b-fp8', 'Qwen3 30B-A3B fp8 (CF)', 7, 11, 'Large', null, null, null, null, '~18-45M', 131072],
['cloudflare', '@cf/deepseek-ai/deepseek-r1-distill-qwen-32b', 'DeepSeek R1 Distill Qwen 32B (CF)', 9, 11, 'Large', null, null, null, null, '~3-5M', 131072],
['google', 'gemini-3.1-flash-lite-preview', 'Gemini 3.1 Flash-Lite Preview', 18, 3, 'Medium', 15, 20, 250000, null, '~3M', 1048576],
['google', 'gemini-3-flash-preview', 'Gemini 3 Flash Preview', 11, 5, 'Large', 10, 20, 250000, null, '~3M', 1048576],
['google', 'gemini-3.1-pro-preview', 'Gemini 3.1 Pro Preview', 1, 8, 'Frontier', 5, 20, 250000, null, '~3M', 1048576],
['openrouter', 'google/gemma-4-31b-it:free', 'Gemma 4 31B (free)', 19, 9, 'Medium', 20, 200, null, null, '~6M', 262144],
['openrouter', 'liquid/lfm-2.5-1.2b-instruct:free', 'Liquid LFM 2.5 1.2B (free)', 30, 10, 'Small', 20, 200, null, null, '~6M', 32768],
];
await insertModels(db, additions);
await backfillFallback(db);
}
async function migrateModelsV7(db: TursoDb) {
await removeModels(db, [['openrouter', 'inclusionai/ling-2.6-flash:free']]);
const additions = [
['openrouter', 'inclusionai/ling-2.6-1t:free', 'Ling 2.6 1T (free)', 4, 9, 'Frontier', 20, 200, null, null, '~6M', 262144],
['openrouter', 'tencent/hy3-preview:free', 'Tencent HY3 Preview (free)', 7, 9, 'Frontier', 20, 200, null, null, '~6M', 262144],
['openrouter', 'poolside/laguna-m.1:free', 'Poolside Laguna M.1 (free)', 13, 9, 'Large', 20, 200, null, null, '~6M', 131072],
['openrouter', 'google/gemma-4-26b-a4b-it:free', 'Gemma 4 26B-A4B (free)', 22, 9, 'Medium', 20, 200, null, null, '~6M', 262144],
['openrouter', 'nvidia/nemotron-3-nano-omni-30b-a3b-reasoning:free', 'Nemotron 3 Nano 30B Reasoning (free)', 23, 9, 'Medium', 20, 200, null, null, '~6M', 262144],
['openrouter', 'poolside/laguna-xs.2:free', 'Poolside Laguna XS.2 (free)', 26, 10, 'Medium', 20, 200, null, null, '~6M', 131072],
['openrouter', 'nvidia/nemotron-nano-9b-v2:free', 'Nemotron Nano 9B v2 (free)', 28, 10, 'Medium', 20, 200, null, null, '~6M', 128000],
['openrouter', 'liquid/lfm-2.5-1.2b-thinking:free', 'Liquid LFM 2.5 1.2B Thinking (free)', 30, 10, 'Small', 20, 200, null, null, '~6M', 32768],
['zhipu', 'glm-4.7-flash', 'GLM-4.7 Flash', 18, 4, 'Large', null, null, null, 1000000, '~30M', 131072],
];
await insertModels(db, additions);
await backfillFallback(db);
}
async function migrateModelsV8(db: TursoDb) {
const additions = [
['sambanova', 'DeepSeek-V3.1-cb', 'DeepSeek V3.1 (CB)', 5, 9, 'Frontier', 20, 20, null, 200000, '~3M', 131072],
['sambanova', 'gemma-3-12b-it', 'Gemma 3 12B (SambaNova)', 22, 9, 'Medium', 20, 20, null, 200000, '~3M', 131072],
['cloudflare', '@cf/moonshotai/kimi-k2.6', 'Kimi K2.6 (CF)', 2, 11, 'Frontier', null, null, null, null, '~10-20M', 262144],
['cloudflare', '@cf/ibm-granite/granite-4.0-h-micro', 'Granite 4.0 H Micro (CF)', 29, 11, 'Small', null, null, null, null, '~5-10M', 131072],
];
await insertModels(db, additions);
await backfillFallback(db);
}
async function migrateModelsV9(db: TursoDb) {
await db.run("UPDATE models SET enabled = 0 WHERE platform = 'cerebras' AND model_id = 'zai-glm-4.7'");
}
async function migrateModelsV10(db: TursoDb) {
const additions = [
['ollama', 'qwen3-coder:480b', 'Qwen3-Coder 480B (Ollama)', 2, 9, 'Frontier', null, null, null, null, '~5-10M', 262144],
['ollama', 'mistral-large-3:675b', 'Mistral Large 3 675B (Ollama)', 3, 9, 'Frontier', null, null, null, null, '~5-10M', 131072],
['ollama', 'deepseek-v3.2', 'DeepSeek V3.2 (Ollama)', 4, 9, 'Frontier', null, null, null, null, '~5-10M', 131072],
['ollama', 'cogito-2.1:671b', 'Cogito 2.1 671B (Ollama)', 4, 9, 'Frontier', null, null, null, null, '~5-10M', 131072],
['ollama', 'kimi-k2-thinking', 'Kimi K2 Thinking (Ollama)', 5, 9, 'Frontier', null, null, null, null, '~5-10M', 131072],
['ollama', 'glm-4.7', 'GLM-4.7 (Ollama)', 6, 9, 'Frontier', null, null, null, null, '~5-10M', 131072],
['ollama', 'gpt-oss:120b', 'GPT-OSS 120B (Ollama)', 6, 9, 'Large', null, null, null, null, '~10-20M', 131072],
['ollama', 'devstral-2:123b', 'Devstral 2 123B (Ollama)', 8, 10, 'Large', null, null, null, null, '~10-20M', 131072],
['ollama', 'gpt-oss:20b', 'GPT-OSS 20B (Ollama)', 18, 10, 'Medium', null, null, null, null, '~20-30M', 131072],
['ollama', 'gemma4:31b', 'Gemma 4 31B (Ollama)', 22, 10, 'Medium', null, null, null, null, '~20-30M', 131072],
];
await insertModels(db, additions);
await backfillFallback(db);
}
async function migrateModelsV11(db: TursoDb) {
await db.run(`UPDATE models SET model_id = 'qwen-3-235b-a22b-instruct-2507' WHERE platform = 'cerebras' AND model_id = 'qwen3-235b'`);
await db.run(`UPDATE models SET enabled = 1, monthly_token_budget = '~3M (1k credits)' WHERE platform = 'nvidia' AND model_id = 'meta/llama-3.1-70b-instruct'`);
const additions = [
['nvidia', 'meta/llama-3.3-70b-instruct', 'Llama 3.3 70B (NV)', 17, 6, 'Large', 40, null, null, null, '~3M (credits)', 131072],
['nvidia', 'meta/llama-4-maverick-17b-128e-instruct', 'Llama 4 Maverick (NV)', 11, 6, 'Large', 40, null, null, null, '~3M (credits)', 131072],
['nvidia', 'deepseek-ai/deepseek-v4-pro', 'DeepSeek V4 Pro (NV)', 3, 9, 'Frontier', 40, null, null, null, '~2M (credits)', 131072],
['nvidia', 'mistralai/mistral-large-3-675b-instruct-2512', 'Mistral Large 3 675B (NV)', 3, 9, 'Frontier', 40, null, null, null, '~2M (credits)', 131072],
['nvidia', 'minimaxai/minimax-m2.7', 'MiniMax M2.7 (NV)', 3, 9, 'Frontier', 40, null, null, null, '~2M (credits)', 196608],
['nvidia', 'nvidia/nemotron-3-super-120b-a12b', 'Nemotron 3 Super 120B (NV)', 22, 9, 'Frontier', 40, null, null, null, '~2M (credits)', 262144],
['nvidia', 'nvidia/nemotron-3-nano-30b-a3b', 'Nemotron 3 Nano 30B (NV)', 22, 9, 'Medium', 40, null, null, null, '~3M (credits)', 262144],
['nvidia', 'google/gemma-4-31b-it', 'Gemma 4 31B (NV)', 19, 9, 'Medium', 40, null, null, null, '~3M (credits)', 262144],
['nvidia', 'moonshotai/kimi-k2.6', 'Kimi K2.6 (NV)', 3, 9, 'Frontier', 40, null, null, null, '~2M (credits)', 131072],
['cerebras', 'gpt-oss-120b', 'GPT-OSS 120B (Cerebras)', 6, 1, 'Large', 30, 1000, 60000, 1000000, '~30M', 131072],
['cerebras', 'llama3.1-8b', 'Llama 3.1 8B (Cerebras)', 28, 1, 'Small', 30, 1000, 60000, 1000000, '~30M', 131072],
['groq', 'groq/compound', 'Compound (Groq)', 6, 2, 'Large', 30, 1000, 8000, 200000, '~6M', 131072],
['groq', 'groq/compound-mini', 'Compound Mini (Groq)', 18, 2, 'Medium', 30, 1000, 8000, 200000, '~6M', 131072],
['kilo', 'nvidia/nemotron-3-super-120b-a12b:free', 'Nemotron 3 Super 120B (Kilo)', 22, 9, 'Frontier', null, null, null, null, '~2-3M (200/hr)', 262144],
['pollinations', 'openai-fast', 'GPT-OSS 20B (Pollinations)', 18, 10, 'Medium', null, null, null, null, '~? (anon)', 131072],
['llm7', 'gpt-oss-20b', 'GPT-OSS 20B (LLM7)', 18, 10, 'Medium', 100, null, null, null, '~2-3M (100/hr)', 131072],
['llm7', 'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo', 'Llama 3.1 8B Turbo (LLM7)', 28, 10, 'Small', 100, null, null, null, '~2-3M (100/hr)', 131072],
['llm7', 'codestral-latest', 'Codestral (LLM7)', 16, 8, 'Medium', 100, null, null, null, '~2-3M (100/hr)', 32000],
['llm7', 'ministral-8b-2512', 'Ministral 8B (LLM7)', 28, 10, 'Small', 100, null, null, null, '~2-3M (100/hr)', 131072],
['llm7', 'GLM-4.6V-Flash', 'GLM-4.6V Flash (LLM7)', 15, 9, 'Large', 100, null, null, null, '~2-3M (100/hr)', 131072],
];
await insertModels(db, additions);
await backfillFallback(db);
}
async function migrateModelsV12(db: TursoDb) {
await removeModels(db, [
['openrouter', 'inclusionai/ling-2.6-1t:free'],
['openrouter', 'tencent/hy3-preview:free'],
]);
await db.run(`UPDATE models SET context_window = 1000000 WHERE platform = 'openrouter' AND model_id = 'nvidia/nemotron-3-super-120b-a12b:free'`);
await db.run(`UPDATE models SET context_window = 1048576 WHERE platform = 'openrouter' AND model_id = 'qwen/qwen3-coder:free'`);
const additions = [
['openrouter', 'arcee-ai/trinity-large-thinking:free', 'Trinity Large Thinking (free)', 5, 9, 'Frontier', 20, 200, null, null, '~6M', 262144],
['openrouter', 'baidu/cobuddy:free', 'CoBuddy (free)', 6, 9, 'Large', 20, 200, null, null, '~6M', 131072],
['openrouter', 'openrouter/owl-alpha', 'Owl Alpha (OR-house)', 5, 9, 'Frontier', 20, 200, null, null, '~6M', 1048576],
['openrouter', 'nousresearch/hermes-3-llama-3.1-405b:free', 'Hermes 3 405B (free)', 17, 9, 'Large', 20, 200, null, null, '~6M', 131072],
];
await insertModels(db, additions);
await backfillFallback(db);
}
async function migrateModelsV13(db: TursoDb) {
// Disables
for (const [p, m] of [
['google', 'gemini-3.1-pro-preview'],
['ollama', 'kimi-k2-thinking'],
['ollama', 'mistral-large-3:675b'],
['ollama', 'deepseek-v3.2'],
] as Array<[string, string]>) {
await db.run('UPDATE models SET enabled = 0 WHERE platform = ? AND model_id = ?', [p, m]);
}
// Hard removals
await removeModels(db, [
['sambanova', 'DeepSeek-V3.1-cb'],
['cloudflare', '@cf/moonshotai/kimi-k2.5'],
]);
// Limit corrections
await db.run(`UPDATE models SET rpm_limit = 5, rpd_limit = 2400, tpm_limit = 30000, tpd_limit = 1000000 WHERE platform = 'cerebras' AND model_id IN ('qwen-3-235b-a22b-instruct-2507', 'gpt-oss-120b', 'llama3.1-8b')`);
await db.run(`UPDATE models SET tpd_limit = 100000 WHERE platform = 'groq' AND model_id = 'llama-3.3-70b-versatile'`);
await db.run(`UPDATE models SET tpm_limit = 30000 WHERE platform = 'groq' AND model_id = 'meta-llama/llama-4-scout-17b-16e-instruct'`);
await db.run(`UPDATE models SET rpd_limit = 250, tpm_limit = 70000, tpd_limit = NULL WHERE platform = 'groq' AND model_id IN ('groq/compound', 'groq/compound-mini')`);
await db.run(`UPDATE models SET context_window = 32768 WHERE platform = 'sambanova' AND model_id = 'DeepSeek-V3.2'`);
await db.run(`UPDATE models SET context_window = 24000 WHERE platform = 'cloudflare' AND model_id = '@cf/meta/llama-3.3-70b-instruct-fp8-fast'`);
await db.run(`UPDATE models SET context_window = 256000 WHERE platform = 'mistral' AND model_id = 'codestral-latest'`);
await db.run(`UPDATE models SET context_window = 262144 WHERE platform = 'mistral' AND model_id = 'devstral-latest'`);
await db.run(`UPDATE models SET context_window = 131072 WHERE platform = 'mistral' AND model_id = 'magistral-medium-latest'`);
await db.run(`UPDATE models SET context_window = 262144 WHERE platform = 'mistral' AND model_id = 'mistral-large-latest'`);
const additions = [
['groq', 'openai/gpt-oss-safeguard-20b', 'GPT-OSS Safeguard 20B (Groq)', 18, 2, 'Medium', 30, 1000, 8000, 200000, '~6M', 131072],
['cloudflare', '@cf/nvidia/nemotron-3-120b-a12b', 'Nemotron 3 120B (CF)', 9, 11, 'Frontier', null, null, null, null, '~5-10M', 262144],
['cloudflare', '@cf/google/gemma-4-26b-a4b-it', 'Gemma 4 26B-A4B it (CF)', 22, 11, 'Medium', null, null, null, null, '~10-20M', 262144],
['google', 'gemini-3.5-flash', 'Gemini 3.5 Flash', 3, 5, 'Large', 10, 20, 250000, null, '~3M', 1048576],
['nvidia', 'deepseek-ai/deepseek-v4-flash', 'DeepSeek V4 Flash (NV)', 4, 9, 'Frontier', 40, null, null, null, '~3M (credits)', 131072],
['nvidia', 'z-ai/glm-5.1', 'GLM-5.1 (NV, slow cold-start)', 5, 9, 'Frontier', 40, null, null, null, '~3M (credits)', 200000],
['nvidia', 'qwen/qwen3-coder-480b-a35b-instruct', 'Qwen3-Coder 480B (NV)', 2, 9, 'Frontier', 40, null, null, null, '~3M (credits)', 262144],
['mistral', 'mistral-small-latest', 'Mistral Small 4', 14, 8, 'Medium', 2, null, 500000, null, '~50-100M', 262144],
['mistral', 'ministral-8b-latest', 'Ministral 3 8B', 28, 8, 'Small', 2, null, 500000, null, '~50-100M', 262144],
['cohere', 'command-a-reasoning-08-2025', 'Command A Reasoning (08-2025)', 13, 11, 'Large', 20, 33, null, null, '~1-2M', 256000],
['cohere', 'command-r-08-2024', 'Command R (08-2024)', 25, 11, 'Medium', 20, 33, null, null, '~1-2M', 131072],
['ollama', 'qwen3-coder-next', 'Qwen3-Coder Next (Ollama)', 3, 9, 'Large', null, null, null, null, '~10-20M', 262144],
['huggingface', 'deepseek-ai/DeepSeek-V4-Flash', 'DeepSeek V4 Flash (HF)', 4, 9, 'Frontier', null, null, null, null, '~1-3M', 131072],
['huggingface', 'moonshotai/Kimi-K2.6', 'Kimi K2.6 (HF)', 3, 9, 'Frontier', null, null, null, null, '~1-3M', 262144],
['huggingface', 'Qwen/Qwen3-Coder-Next', 'Qwen3-Coder Next (HF)', 3, 9, 'Large', null, null, null, null, '~1-3M', 262144],
];
await insertModels(db, additions);
await backfillFallback(db);
}
async function migrateModelsV14(db: TursoDb) {
await db.run(`UPDATE models SET enabled = 0 WHERE platform = 'cerebras' AND model_id IN ('qwen-3-235b-a22b-instruct-2507', 'llama3.1-8b')`);
}
async function migrateModelsV15(db: TursoDb) {
await db.run(`DELETE FROM fallback_config WHERE model_db_id IN (SELECT id FROM models WHERE platform = 'siliconflow')`);
await db.run(`DELETE FROM models WHERE platform = 'siliconflow'`);
}
async function migrateModelsV16Vision(db: TursoDb) {
await ensureColumn(db, 'models', 'supports_vision', 'INTEGER NOT NULL DEFAULT 0');
await db.run('UPDATE models SET supports_vision = 0');
await db.run("UPDATE models SET supports_vision = 1 WHERE platform = 'google'");
await db.run(`UPDATE models SET supports_vision = 1 WHERE LOWER(model_id) LIKE '%llama-4%' AND platform NOT IN ('cloudflare', 'cohere')`);
await db.run(`UPDATE models SET supports_vision = 1 WHERE platform = 'github' AND (model_id LIKE '%gpt-4o%' OR model_id LIKE '%gpt-4.1%' OR model_id LIKE '%gpt-5%')`);
}
async function migrateModelsV17IntelligenceTiers(db: TursoDb) {
await db.run(`UPDATE models SET size_label = 'Frontier' WHERE LOWER(model_id) LIKE '%gemini-3.1-pro%' OR LOWER(model_id) LIKE '%gemini-3.5-flash%' OR LOWER(model_id) LIKE '%gemini-3-flash%' OR LOWER(model_id) LIKE '%kimi-k2.6%' OR LOWER(model_id) LIKE '%kimi-k2-thinking%' OR LOWER(model_id) LIKE '%deepseek-v4-pro%' OR LOWER(model_id) LIKE '%deepseek-v4-flash%' OR LOWER(model_id) LIKE '%glm-5.1%' OR LOWER(model_id) LIKE '%minimax-m2.7%'`);
await db.run(`UPDATE models SET size_label = 'Large' WHERE LOWER(model_id) LIKE '%minimax-m2.5%' OR LOWER(model_id) LIKE '%qwen3-next%' OR LOWER(model_id) LIKE '%qwen3-coder-next%' OR LOWER(model_id) LIKE '%gpt-oss-120b%' OR LOWER(model_id) LIKE '%gpt-oss:120b%' OR LOWER(model_id) LIKE '%glm-4.7%' OR LOWER(model_id) LIKE '%nemotron-3-super%' OR LOWER(model_id) LIKE '%nemotron-3-120b%' OR LOWER(model_id) LIKE '%gemini-2.5-pro%' OR LOWER(model_id) LIKE '%deepseek-v3.2%' OR LOWER(model_id) LIKE '%deepseek-v3.1%' OR LOWER(model_id) LIKE '%trinity-large%' OR LOWER(model_id) LIKE '%mistral-medium%' OR LOWER(model_id) LIKE '%magistral-medium%' OR LOWER(model_id) LIKE '%gpt-4.1%' OR LOWER(model_id) LIKE '%gemma-4-31b%' OR LOWER(model_id) LIKE '%gemma4:31b%' OR LOWER(model_id) LIKE '%gemma-4-26b%' OR LOWER(model_id) LIKE '%gemini-3.1-flash-lite%'`);
await db.run(`UPDATE models SET size_label = 'Medium' WHERE (LOWER(model_id) LIKE '%qwen3-coder%' AND LOWER(model_id) NOT LIKE '%qwen3-coder-next%') OR LOWER(model_id) LIKE '%qwen-3-235b%' OR LOWER(model_id) LIKE '%qwen3-235b%' OR LOWER(model_id) LIKE '%mistral-large%' OR LOWER(model_id) LIKE '%gpt-oss-20b%' OR LOWER(model_id) LIKE '%gpt-oss:20b%' OR LOWER(model_id) LIKE '%gpt-oss-safeguard-20b%' OR model_id = 'openai-fast' OR LOWER(model_id) LIKE '%glm-4.5-air%' OR LOWER(model_id) LIKE '%devstral-2%' OR LOWER(model_id) LIKE '%deepseek-r1-distill%' OR LOWER(model_id) LIKE '%qwen3-30b%' OR LOWER(model_id) LIKE '%qwen3-32b%' OR LOWER(model_id) LIKE '%llama-4-maverick%' OR LOWER(model_id) LIKE '%llama-4-scout%' OR LOWER(model_id) LIKE '%llama-3.3-70b%' OR LOWER(model_id) LIKE '%llama-3.1-70b%' OR (LOWER(model_id) LIKE '%gemini-2.5-flash%' AND LOWER(model_id) NOT LIKE '%flash-lite%') OR LOWER(model_id) LIKE '%gemini-2.5-flash-lite%' OR LOWER(model_id) LIKE '%gpt-4o%' OR LOWER(model_id) LIKE '%command-a-03-2025%' OR LOWER(model_id) LIKE '%command-r-plus%' OR LOWER(model_id) LIKE '%nemotron-3-nano%' OR LOWER(model_id) LIKE '%nemotron-nano-9b%'`);
await db.run(`UPDATE models SET size_label = 'Small' WHERE LOWER(model_id) LIKE '%gemma-3-12b%' OR LOWER(model_id) LIKE '%command-r-08-2024%' OR LOWER(model_id) LIKE '%codestral%' OR LOWER(model_id) LIKE '%llama-3.1-8b%' OR LOWER(model_id) LIKE '%llama3.1-8b%' OR LOWER(model_id) LIKE '%meta-llama-3.1-8b%' OR LOWER(model_id) LIKE '%ministral-8b%' OR LOWER(model_id) LIKE '%granite-4.0-h-micro%' OR LOWER(model_id) LIKE '%lfm-2.5-1.2b%'`);
}
async function migrateModelsV18OpenCodeZen(db: TursoDb) {
const additions = [
['opencode', 'big-pickle', 'Big Pickle (OpenCode Zen, stealth)', 10, 4, 'Large', 20, 200, null, null, 'promo (trial)', 131072],
['opencode', 'deepseek-v4-flash-free', 'DeepSeek V4 Flash Free (OpenCode Zen)', 4, 4, 'Frontier', 20, 200, null, null, 'promo (trial)', 131072],
['opencode', 'mimo-v2.5-free', 'MiMo-V2.5 Free (OpenCode Zen)', 14, 4, 'Medium', 20, 200, null, null, 'promo (trial)', 131072],
['opencode', 'nemotron-3-super-free', 'Nemotron 3 Super Free (OpenCode Zen)', 12, 4, 'Large', 20, 200, null, null, 'promo (trial)', 131072],
];
await insertModels(db, additions);
await backfillFallback(db);
}
async function migrateModelsV19Gemma4(db: TursoDb) {
const additions = [
['google', 'gemma-4-31b-it', 'Gemma 4 31B IT', 19, 4, 'Large', 15, 1000, 250000, null, '~30M', 32768],
['google', 'gemma-4-26b-a4b-it', 'Gemma 4 26B IT', 20, 4, 'Large', 15, 1000, 250000, null, '~30M', 32768],
];
await insertModels(db, additions);
await backfillFallback(db);
}
async function migrateModelsV20KiloFree(db: TursoDb) {
const additions = [
['kilo', 'poolside/laguna-m.1:free', 'Poolside Laguna M.1 (Kilo)', 13, 8, 'Large', null, null, null, null, 'free · 200/hr per IP', 262144],
['kilo', 'poolside/laguna-xs.2:free', 'Poolside Laguna XS.2 (Kilo)', 16, 4, 'Medium', null, null, null, null, 'free · 200/hr per IP', 262144],
['kilo', 'nvidia/nemotron-3-super-120b-a12b:free', 'Nemotron 3 Super 120B (Kilo)', 12, 5, 'Large', null, null, null, null, 'free · 200/hr per IP (trial)', 1000000],
['kilo', 'stepfun/step-3.7-flash:free', 'StepFun Step 3.7 Flash (Kilo)', 14, 3, 'Medium', null, null, null, null, 'free · 200/hr per IP', 262144],
];
await insertModels(db, additions);
await backfillFallback(db);
}
async function migrateModelsV21PruneDead(db: TursoDb) {
const dead: Array<[string, string]> = [
['llm7', 'gpt-oss-20b'],
['llm7', 'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo'],
['llm7', 'ministral-8b-2512'],
['llm7', 'GLM-4.6V-Flash'],
['openrouter', 'arcee-ai/trinity-large-thinking:free'],
['openrouter', 'minimax/minimax-m2.5:free'],
['openrouter', 'baidu/cobuddy:free'],
];
for (const [platform, modelId] of dead) {
const row = await db.get<{ id: number }>('SELECT id FROM models WHERE platform = ? AND model_id = ?', [platform, modelId]);
if (!row) continue;
await db.run('DELETE FROM fallback_config WHERE model_db_id = ?', [row.id]);
await db.run('DELETE FROM models WHERE id = ?', [row.id]);
}
await db.run("UPDATE models SET enabled = 1 WHERE platform = 'cerebras' AND model_id = 'zai-glm-4.7'");
await db.run(`UPDATE fallback_config SET enabled = 1 WHERE model_db_id = (SELECT id FROM models WHERE platform = 'cerebras' AND model_id = 'zai-glm-4.7')`);
}
async function migrateModelsV22Tools(db: TursoDb) {
await ensureColumn(db, 'models', 'supports_tools', 'INTEGER NOT NULL DEFAULT 0');
await db.run('UPDATE models SET supports_tools = 0');
await db.run(`
UPDATE models SET supports_tools = 1
WHERE (
LOWER(model_id) LIKE '%gpt-oss%'
OR ((LOWER(model_id) LIKE '%llama-3%' OR LOWER(model_id) LIKE '%llama-4%')
AND LOWER(model_id) NOT LIKE '%hermes%')
OR LOWER(model_id) LIKE '%gemini-%'
OR LOWER(model_id) LIKE '%glm-%'
OR LOWER(model_id) LIKE '%qwen3%'
OR LOWER(model_id) LIKE '%qwen-3%'
OR LOWER(model_id) LIKE '%deepseek-v%'
OR LOWER(model_id) LIKE '%kimi-k2%'
OR LOWER(model_id) LIKE '%minimax-m2%'
OR LOWER(model_id) LIKE '%mistral-large%'
OR LOWER(model_id) LIKE '%mistral-medium%'
OR LOWER(model_id) LIKE '%mistral-small%'
OR LOWER(model_id) LIKE '%magistral%'
OR LOWER(model_id) LIKE '%codestral%'
OR LOWER(model_id) LIKE '%devstral%'
OR LOWER(model_id) LIKE '%ministral%'
OR LOWER(model_id) LIKE '%command-a%'
OR LOWER(model_id) LIKE '%command-r%'
OR LOWER(model_id) LIKE '%gpt-4o%'
OR LOWER(model_id) LIKE '%gpt-4.1%'
OR LOWER(model_id) LIKE '%gpt-5%'
OR LOWER(model_id) LIKE '%nemotron-3-super%'
)
`);
}
async function migrateEmbeddingsV1(db: TursoDb) {
await db.run(`
CREATE TABLE IF NOT EXISTS embedding_models (
id INTEGER PRIMARY KEY AUTOINCREMENT,
family TEXT NOT NULL,
platform TEXT NOT NULL,
model_id TEXT NOT NULL,
display_name TEXT NOT NULL,
dimensions INTEGER NOT NULL,
max_input_tokens INTEGER,
priority INTEGER NOT NULL DEFAULT 0,
enabled INTEGER NOT NULL DEFAULT 1,
quota_label TEXT NOT NULL DEFAULT '',
UNIQUE(platform, model_id)
)
`);
await ensureColumn(db, 'requests', 'request_type', "TEXT NOT NULL DEFAULT 'chat'");
const rows = [
['gemini-embedding-001', 'google', 'gemini-embedding-001', 'Gemini Embedding', 3072, 2048, 1, 1, '100 rpm · 1K req/day'],
['llama-nemotron-embed-vl-1b-v2', 'nvidia', 'nvidia/llama-nemotron-embed-vl-1b-v2', 'Nemotron Embed VL 1B', 2048, 8192, 1, 1, '~40 rpm'],
['llama-nemotron-embed-vl-1b-v2', 'openrouter', 'nvidia/llama-nemotron-embed-vl-1b-v2', 'Nemotron Embed VL 1B (OR)', 2048, 8192, 2, 1, '$0/M tok'],
['llama-nemotron-embed-1b-v2', 'nvidia', 'nvidia/llama-nemotron-embed-1b-v2', 'Nemotron Embed 1B', 2048, 8192, 1, 1, '~40 rpm'],
['nv-embedqa-e5-v5', 'nvidia', 'nvidia/nv-embedqa-e5-v5', 'NV-EmbedQA E5 v5', 1024, 512, 1, 1, '~40 rpm'],
['text-embedding-3-small', 'github', 'openai/text-embedding-3-small', 'Text Embedding 3 Small', 1536, 8191, 1, 1, 'rate-limited free'],
['text-embedding-3-large', 'github', 'openai/text-embedding-3-large', 'Text Embedding 3 Large', 3072, 8191, 1, 1, 'rate-limited free'],
['bge-m3', 'cloudflare', '@cf/baai/bge-m3', 'BGE-M3', 1024, 8192, 1, 1, '10K neurons/day (shared)'],
['bge-m3', 'huggingface', 'BAAI/bge-m3', 'BGE-M3 (HF)', 1024, 8192, 2, 1, '$0.10/mo credits'],
['embeddinggemma-300m', 'cloudflare', '@cf/google/embeddinggemma-300m', 'EmbeddingGemma 300M', 768, 2048, 1, 1, '10K neurons/day (shared)'],
['qwen3-embedding-0.6b', 'cloudflare', '@cf/qwen/qwen3-embedding-0.6b', 'Qwen3 Embedding 0.6B', 1024, 4096, 1, 1, '10K neurons/day (shared)'],
['embed-v4.0', 'cohere', 'embed-v4.0', 'Cohere Embed v4', 1536, 128000, 1, 0, '1K calls/mo (shared w/ chat)'],
];
for (const r of rows) {
await db.run(
`INSERT OR IGNORE INTO embedding_models
(family, platform, model_id, display_name, dimensions, max_input_tokens, priority, enabled, quota_label)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
r,
);
}
const def = await db.get("SELECT value FROM settings WHERE key = 'embeddings_default_family'");
if (!def) {
await db.run("INSERT INTO settings (key, value) VALUES ('embeddings_default_family', 'gemini-embedding-001')");
}
}
async function ensureUnifiedKey(db: TursoDb) {
const existing = await db.get<{ value: string }>("SELECT value FROM settings WHERE key = 'unified_api_key'");
if (!existing) {
const key = `freellmapi-${crypto.randomBytes(24).toString('hex')}`;
await db.run("INSERT INTO settings (key, value) VALUES ('unified_api_key', ?)", [key]);
console.log(`\n Your unified API key: ${key}\n`);
}
}
export async function getUnifiedApiKey(): Promise<string> {
const db = getDb();
const row = await db.get<{ value: string }>("SELECT value FROM settings WHERE key = 'unified_api_key'");
return row!.value;
}
export async function regenerateUnifiedKey(): Promise<string> {
const db = getDb();
const key = `freellmapi-${crypto.randomBytes(24).toString('hex')}`;
await db.run("UPDATE settings SET value = ? WHERE key = 'unified_api_key'", [key]);
return key;
}
// Generic key/value settings accessors (used by routing strategy, etc.).
export async function getSetting(key: string): Promise<string | undefined> {
const db = getDb();
const row = await db.get<{ value: string }>('SELECT value FROM settings WHERE key = ?', [key]);
return row?.value;
}
export async function setSetting(key: string, value: string): Promise<void> {
const db = getDb();
await db.run(
`INSERT INTO settings (key, value) VALUES (?, ?)
ON CONFLICT(key) DO UPDATE SET value = excluded.value`,
[key, value],
);
}