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

feat(backend): implement routing profiles and schema (#288)

Makibasu0f5d89933da8
diff --git a/server/src/__tests__/routes/custom-provider.test.ts b/server/src/__tests__/routes/custom-provider.test.ts
index 25a535f9..d1fc8d17 100644
--- a/server/src/__tests__/routes/custom-provider.test.ts
+++ b/server/src/__tests__/routes/custom-provider.test.ts
@@ -48,7 +48,7 @@ async function del(app: Express, path: string) {
return { status: res.status, body: data };
}
-describe('resolveProvider (#117)', () => {
+describe('Custom Provider Endpoints', () => {
it('builds a custom provider bound to the supplied base URL', () => {
const p = resolveProvider('custom', 'http://127.0.0.1:8080/v1');
expect(p).toBeDefined();
@@ -66,15 +66,17 @@ describe('resolveProvider (#117)', () => {
});
});
-describe('POST /api/keys/custom (#117)', () => {
- let app: Express;
-
- beforeAll(() => {
- process.env.ENCRYPTION_KEY = '0'.repeat(64);
- initDb(':memory:');
- app = createApp();
- dashToken = mintDashboardToken();
- });
+ describe('POST /api/keys/custom (#117)', () => {
+ let app: Express;
+
+ beforeAll(() => {
+ process.env.ENCRYPTION_KEY = '0'.repeat(64);
+ initDb(':memory:');
+ // Isolate tests to use global fallback_config
+ getDb().prepare("DELETE FROM settings WHERE key = 'active_profile_id'").run();
+ app = createApp();
+ dashToken = mintDashboardToken();
+ });
it('rejects an invalid base URL', async () => {
const { status } = await post(app, '/api/keys/custom', { baseUrl: 'not-a-url', model: 'm' });
@@ -217,7 +219,8 @@ describe('POST /api/keys/custom (#117)', () => {
const db = getDb();
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();
- db.prepare("DELETE FROM api_keys WHERE platform = 'custom'").run();
+ db.prepare('DELETE FROM api_keys').run();
+ db.prepare("DELETE FROM settings WHERE key = 'active_profile_id'").run();
const a = await post(app, '/api/keys/custom', { baseUrl: 'http://127.0.0.1:11434/v1', model: 'llama3:8b', label: 'Ollama box' });
const b = await post(app, '/api/keys/custom', { baseUrl: 'http://127.0.0.1:1234/v1', model: 'qwen3:4b', label: 'LM Studio' });
diff --git a/server/src/__tests__/routes/proxy-tools-routing.test.ts b/server/src/__tests__/routes/proxy-tools-routing.test.ts
index 52a087b9..da2b4d60 100644
--- a/server/src/__tests__/routes/proxy-tools-routing.test.ts
+++ b/server/src/__tests__/routes/proxy-tools-routing.test.ts
@@ -83,6 +83,7 @@ describe('Tools-aware routing', () => {
it('routeRequest skips non-tool models when requireTools is set', () => {
const db = getDb();
setRoutingStrategy('priority');
+ db.prepare("DELETE FROM settings WHERE key = 'active_profile_id'").run();
// One key for google, whose catalog holds both a non-tool model (gemma)
// and tool-capable ones (gemini). Put gemma at the top of the chain.
diff --git a/server/src/__tests__/services/router.test.ts b/server/src/__tests__/services/router.test.ts
index a5a72fcf..e3007aaf 100644
--- a/server/src/__tests__/services/router.test.ts
+++ b/server/src/__tests__/services/router.test.ts
@@ -20,6 +20,8 @@ describe('Router', () => {
// bandit (now the default strategy) doesn't reorder by score.
setRoutingStrategy('priority');
db.prepare('DELETE FROM api_keys').run();
+ // Disable active profile so the router falls back to fallback_config
+ db.prepare("DELETE FROM settings WHERE key = 'active_profile_id'").run();
// Reset fallback order to intelligence ranking
const models = db.prepare('SELECT id, intelligence_rank FROM models ORDER BY intelligence_rank ASC').all() as any[];
const update = db.prepare('UPDATE fallback_config SET priority = ? WHERE model_db_id = ?');
diff --git a/server/src/__tests__/services/routing-exhaustion.test.ts b/server/src/__tests__/services/routing-exhaustion.test.ts
index 72097b2d..77925907 100644
--- a/server/src/__tests__/services/routing-exhaustion.test.ts
+++ b/server/src/__tests__/services/routing-exhaustion.test.ts
@@ -51,6 +51,12 @@ describe('Routing Key Exhaustion', () => {
// models that share the 'google' platform.
setRoutingStrategy('priority');
const db = getDb();
+
+ // Clear seeded DB data to isolate this test's routing logic
+ db.prepare("DELETE FROM settings WHERE key = 'active_profile_id'").run();
+ db.prepare("DELETE FROM fallback_config").run();
+ db.prepare("DELETE FROM profile_models").run();
+ db.prepare("DELETE FROM models").run();
// Setup: 2 models (Pro and Flash)
// Pro is higher priority (priority 1), Flash is lower (priority 2)
diff --git a/server/src/app.ts b/server/src/app.ts
index f89de492..a7573d3d 100644
--- a/server/src/app.ts
+++ b/server/src/app.ts
@@ -8,6 +8,7 @@ import { modelsRouter } from './routes/models.js';
import { proxyRouter } from './routes/proxy.js';
import { responsesRouter } from './routes/responses.js';
import { fallbackRouter } from './routes/fallback.js';
+import { profilesRouter } from './routes/profiles.js';
import { embeddingsRouter } from './routes/embeddings.js';
import { analyticsRouter } from './routes/analytics.js';
import { healthRouter } from './routes/health.js';
@@ -64,6 +65,7 @@ export function createApp() {
// API routes — all admin endpoints sit behind requireAuth.
app.use('/api/keys', requireAuth, keysRouter);
app.use('/api/models', requireAuth, modelsRouter);
+ app.use('/api/profiles', requireAuth, profilesRouter);
app.use('/api/fallback', requireAuth, fallbackRouter);
app.use('/api/embeddings', requireAuth, embeddingsRouter);
app.use('/api/analytics', requireAuth, analyticsRouter);
diff --git a/server/src/db/migrations.ts b/server/src/db/migrations.ts
index a2cc2fbf..d315e50a 100644
--- a/server/src/db/migrations.ts
+++ b/server/src/db/migrations.ts
@@ -46,6 +46,7 @@ export function migrateDbSchema(db: Database.Database) {
migrateEmbeddingsV1(db);
migrateQuirksV1(db);
ensureUnifiedKey(db);
+ migrateProfilesInit(db);
}
function createTables(db: Database.Database) {
@@ -123,6 +124,28 @@ function createTables(db: Database.Database) {
UNIQUE(model_db_id)
);
+ CREATE TABLE IF NOT EXISTS profiles (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ name TEXT NOT NULL,
+ emoji TEXT NOT NULL DEFAULT '',
+ color TEXT NOT NULL DEFAULT '#6366f1',
+ type TEXT NOT NULL DEFAULT 'custom',
+ is_favorite INTEGER NOT NULL DEFAULT 0,
+ sort_order INTEGER NOT NULL DEFAULT 0,
+ auto_sort TEXT,
+ layout_config TEXT,
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
+ );
+
+ CREATE TABLE IF NOT EXISTS profile_models (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ profile_id INTEGER NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
+ model_db_id INTEGER NOT NULL REFERENCES models(id) ON DELETE CASCADE,
+ priority INTEGER NOT NULL,
+ enabled INTEGER NOT NULL DEFAULT 1,
+ UNIQUE(profile_id, model_db_id)
+ );
+
CREATE TABLE IF NOT EXISTS settings (
key TEXT PRIMARY KEY,
value TEXT NOT NULL
@@ -2142,3 +2165,53 @@ function ensureUnifiedKey(db: Database.Database) {
console.log(`\n Your unified API key: ${key}\n`);
}
}
+
+/**
+ * Migration helper to ensure Default profile exists, legacy profiles converted,
+ * and fallback_config synced.
+ */
+function migrateProfilesInit(db: Database.Database) {
+ // 1. Convert any legacy built-in profiles to custom profiles (type = 'custom')
+ db.prepare(`
+ UPDATE profiles
+ SET type = 'custom'
+ WHERE type = 'builtin'
+ `).run();
+
+ // 2. Ensure Default profile exists
+ const hasDefault = db.prepare("SELECT COUNT(*) as cnt FROM profiles WHERE type = 'default'").get() as { cnt: number };
+ if (hasDefault.cnt === 0) {
+ const minOrder = (db.prepare('SELECT COALESCE(MIN(sort_order), 0) AS mn FROM profiles').get() as { mn: number }).mn;
+ const targetOrder = Math.min(-1, minOrder - 1);
+
+ const result = db.prepare(
+ "INSERT INTO profiles (name, emoji, color, type, sort_order) VALUES ('Default', '⚙️', '#6366f1', 'default', ?)"
+ ).run(targetOrder);
+
+ const profileId = result.lastInsertRowid as number;
+
+ // Seed profile models from fallback_config
+ db.prepare(`
+ INSERT INTO profile_models (profile_id, model_db_id, priority, enabled)
+ SELECT ?, model_db_id, priority, enabled
+ FROM fallback_config
+ ORDER BY priority ASC
+ `).run(profileId);
+
+ // Make it the active profile if none is set
+ db.prepare(`
+ INSERT INTO settings (key, value) VALUES ('active_profile_id', ?)
+ ON CONFLICT(key) DO NOTHING
+ `).run(String(profileId));
+
+ console.log('Created Default profile');
+ } else {
+ // If it exists, ensure its emoji is '⚙️'
+ db.prepare(`
+ UPDATE profiles
+ SET emoji = '⚙️'
+ WHERE type = 'default' AND emoji != '⚙️'
+ `).run();
+ }
+}
+
diff --git a/server/src/routes/fallback.ts b/server/src/routes/fallback.ts
index 82699ca4..02674ffb 100644
--- a/server/src/routes/fallback.ts
+++ b/server/src/routes/fallback.ts
@@ -1,48 +1,39 @@
+/**
+ * Express router handles model fallback configuration and token budget reporting.
+ * It integrates named profiles dynamically into the fallback routing logic and aggregates
+ * monthly token consumption and rate limits (RPM/RPD/TPM/TPD) across configured models.
+ */
import { Router } from 'express';
import type { Request, Response } from 'express';
import { z } from 'zod';
import { getDb } from '../db/index.js';
-import { getAllPenalties, getCustomWeights, getRoutingScores, getRoutingStrategy, setCustomWeights, setRoutingStrategy } from '../services/router.js';
+import { getAllPenalties, getRoutingScores, getRoutingStrategy, setRoutingStrategy } from '../services/router.js';
import { BANDIT_PRESETS, type RoutingStrategy } from '../services/scoring.js';
import { parseBudget } from '../lib/budget.js';
export const fallbackRouter = Router();
// ── Bandit routing strategy ─────────────────────────────────────────────────
-// GET /routing → active strategy, preset weights, the saved custom weights,
-// and the per-model score breakdown (reliability / speed /
-// intelligence + guardrails).
+// GET /routing → active strategy, preset weights, and the per-model score
+// breakdown (reliability / speed / intelligence + guardrails).
fallbackRouter.get('/routing', (_req: Request, res: Response) => {
- res.json({ ...getRoutingScores(), customWeights: getCustomWeights() });
+ res.json(getRoutingScores());
});
const routingSchema = z.object({
- strategy: z.enum(['priority', 'balanced', 'smartest', 'fastest', 'reliable', 'custom']),
- // Only meaningful with strategy 'custom'. Any non-negative vector with a
- // positive sum is accepted; the server normalizes it to sum to 1.
- weights: z.object({
- reliability: z.number().min(0).max(1),
- speed: z.number().min(0).max(1),
- intelligence: z.number().min(0).max(1),
- }).refine(w => w.reliability + w.speed + w.intelligence > 0, {
- message: 'weights must not all be zero',
- }).optional(),
+ strategy: z.enum(['priority', 'balanced', 'smartest', 'fastest', 'reliable']),
});
// PUT /routing → switch strategy. Presets are just weight vectors over the three
-// axes; 'custom' uses the user-saved vector; 'priority' falls back to the legacy
-// manual chain order.
+// axes; 'priority' falls back to the legacy manual chain order.
fallbackRouter.put('/routing', (req: Request, res: Response) => {
const parsed = routingSchema.safeParse(req.body);
if (!parsed.success) {
res.status(400).json({ error: { message: parsed.error.errors.map(e => e.message).join(', ') } });
return;
}
- if (parsed.data.strategy === 'custom' && parsed.data.weights) {
- setCustomWeights(parsed.data.weights);
- }
setRoutingStrategy(parsed.data.strategy as RoutingStrategy);
- res.json({ strategy: getRoutingStrategy(), presets: BANDIT_PRESETS, customWeights: getCustomWeights() });
+ res.json({ strategy: getRoutingStrategy(), presets: BANDIT_PRESETS });
});
// Get fallback chain (with dynamic penalties)
@@ -52,9 +43,11 @@ fallbackRouter.get('/', (_req: Request, res: Response) => {
SELECT fc.model_db_id, fc.priority, fc.enabled,
m.platform, m.model_id, m.display_name, m.intelligence_rank,
m.speed_rank, m.size_label, m.rpm_limit, m.rpd_limit,
+ m.tpm_limit, m.tpd_limit,
m.monthly_token_budget, m.supports_vision, m.supports_tools
FROM fallback_config fc
JOIN models m ON m.id = fc.model_db_id
+ WHERE m.enabled = 1
ORDER BY fc.priority ASC
`).all() as any[];
@@ -87,6 +80,8 @@ fallbackRouter.get('/', (_req: Request, res: Response) => {
sizeLabel: r.size_label,
rpmLimit: r.rpm_limit,
rpdLimit: r.rpd_limit,
+ tpmLimit: r.tpm_limit,
+ tpdLimit: r.tpd_limit,
monthlyTokenBudget: r.monthly_token_budget,
supportsVision: r.supports_vision === 1,
supportsTools: r.supports_tools === 1,
@@ -137,19 +132,48 @@ const INTELLIGENCE_TIER =
const SORT_PRESETS: Record<string, string> = {
intelligence: `${INTELLIGENCE_TIER} ASC, m.intelligence_rank ASC`,
speed: 'm.speed_rank ASC',
- budget: "CASE m.monthly_token_budget WHEN '~120M' THEN 1 WHEN '~50-100M' THEN 2 WHEN '~30M' THEN 3 WHEN '~18-45M' THEN 4 WHEN '~18M' THEN 5 WHEN '~15M' THEN 6 WHEN '~12M' THEN 7 WHEN '~6M' THEN 8 WHEN '~5-10M' THEN 9 WHEN '~4M' THEN 10 ELSE 11 END ASC",
};
-fallbackRouter.post('/sort/:preset', (req: Request, res: Response) => {
- const preset = String(req.params.preset);
- const orderBy = SORT_PRESETS[preset];
- if (!orderBy) {
- res.status(400).json({ error: { message: `Unknown preset: ${preset}. Use: intelligence, speed, budget` } });
- return;
+function getBudgetScore(m: { monthly_token_budget: string; tpd_limit: number | null }): number {
+ if (m.tpd_limit != null) return m.tpd_limit * 30;
+
+ const str = m.monthly_token_budget;
+ if (!str) return 0;
+ if (str.toLowerCase().includes('unlimited') || str.includes('∞')) return Infinity;
+
+ const cleanStr = str.split('(')[0];
+ const matches = cleanStr.match(/[\d.]+/g);
+ let maxNum = 0;
+ if (matches) {
+ maxNum = Math.max(...matches.map(mStr => parseFloat(mStr)));
}
+
+ let mult = 1;
+ const upper = cleanStr.toUpperCase();
+ if (upper.includes('B')) mult = 1_000_000_000;
+ else if (upper.includes('M')) mult = 1_000_000;
+ else if (upper.includes('K')) mult = 1_000;
+ return maxNum * mult;
+}
+
+fallbackRouter.post('/sort/:preset', (req: Request, res: Response) => {
+ const preset = String(req.params.preset);
const db = getDb();
- const models = db.prepare(`SELECT m.id FROM models m ORDER BY ${orderBy}`).all() as { id: number }[];
+ let models: { id: number }[] = [];
+
+ if (preset === 'budget') {
+ const allModels = db.prepare(`SELECT id, monthly_token_budget, tpd_limit FROM models`).all() as any[];
+ allModels.sort((a, b) => getBudgetScore(b) - getBudgetScore(a));
+ models = allModels.map(m => ({ id: m.id }));
+ } else {
+ const orderBy = SORT_PRESETS[preset];
+ if (!orderBy) {
+ res.status(400).json({ error: { message: `Unknown preset: ${preset}. Use: intelligence, speed, budget` } });
+ return;
+ }
+ models = db.prepare(`SELECT m.id FROM models m ORDER BY ${orderBy}`).all() as { id: number }[];
+ }
const update = db.prepare('UPDATE fallback_config SET priority = ? WHERE model_db_id = ?');
const reorder = db.transaction(() => {
@@ -174,25 +198,57 @@ fallbackRouter.get('/token-usage', (_req: Request, res: Response) => {
`).all() as { platform: string }[];
const platformSet = new Set(platforms.map(p => p.platform));
- // Get monthly budget per model, ordered by fallback priority
- const models = db.prepare(`
- SELECT m.platform, m.model_id, m.display_name, m.monthly_token_budget,
- fc.priority
- FROM models m
- JOIN fallback_config fc ON fc.model_db_id = m.id
- WHERE m.enabled = 1
- ORDER BY fc.priority ASC
- `).all() as { platform: string; model_id: string; display_name: string; monthly_token_budget: string; priority: number }[];
+ // Check if there is an active profile
+ const settingRow = db.prepare(`SELECT value FROM settings WHERE key = 'active_profile_id'`).get() as { value: string } | undefined;
+ const activeProfileId = settingRow ? (parseInt(settingRow.value) || null) : null;
+
+ // Verify active profile still exists
+ const activeProfile = activeProfileId
+ ? db.prepare('SELECT id FROM profiles WHERE id = ?').get(activeProfileId) as any
+ : null;
+
+ let rawModels: { model_db_id: number; platform: string; model_id: string; display_name: string; monthly_token_budget: string; priority: number; enabled: number; rpm_limit: number | null; rpd_limit: number | null; tpm_limit: number | null; tpd_limit: number | null }[];
+
+ if (activeProfile) {
+ // Profile mode: use profile_models chain (all models in profile, checked against enabled)
+ rawModels = db.prepare(`
+ SELECT m.id as model_db_id, m.platform, m.model_id, m.display_name, m.monthly_token_budget,
+ pm.priority, pm.enabled,
+ m.rpm_limit, m.rpd_limit, m.tpm_limit, m.tpd_limit
+ FROM profile_models pm
+ JOIN models m ON m.id = pm.model_db_id
+ WHERE pm.profile_id = ? AND m.enabled = 1
+ ORDER BY pm.priority ASC
+ `).all(activeProfileId) as any[];
+ } else {
+ // Default mode: use fallback_config (only include enabled models)
+ rawModels = db.prepare(`
+ SELECT m.id as model_db_id, m.platform, m.model_id, m.display_name, m.monthly_token_budget,
+ fc.priority, fc.enabled,
+ m.rpm_limit, m.rpd_limit, m.tpm_limit, m.tpd_limit
+ FROM fallback_config fc
+ JOIN models m ON m.id = fc.model_db_id
+ WHERE m.enabled = 1
+ ORDER BY fc.priority ASC
+ `).all() as any[];
+ }
- // Build per-model breakdown (only platforms with keys)
- const modelBudgets = models
+ // Build per-model breakdown (only platforms with keys), preserving enabled state
+ const modelBudgets = rawModels
.filter(m => platformSet.has(m.platform))
.map(m => ({
+ modelDbId: m.model_db_id,
displayName: m.display_name,
platform: m.platform,
budget: parseBudget(m.monthly_token_budget),
+ enabled: m.enabled === 1,
+ rpmLimit: m.rpm_limit,
+ rpdLimit: m.rpd_limit,
+ tpmLimit: m.tpm_limit,
+ tpdLimit: m.tpd_limit,
}));
+ // Total budget counts all models (both enabled and disabled — they contribute to the pool)
const totalBudget = modelBudgets.reduce((s, m) => s + m.budget, 0);
// Tokens used this month
diff --git a/server/src/routes/profiles.ts b/server/src/routes/profiles.ts
new file mode 100644
index 00000000..7d9890d3
--- /dev/null
+++ b/server/src/routes/profiles.ts
@@ -0,0 +1,509 @@
+/**
+ * Express router handles CRUD endpoints for named model fallback profiles.
+ * Profiles allow users to maintain different prioritized chains of LLMs.
+ * Features include metadata updates, reordering fallback priority, auto-sorting presets,
+ * and built-in safety blocks to prevent modification of default settings.
+ */
+import { Router } from 'express';
+import type { Request, Response } from 'express';
+import { z } from 'zod';
+import { getDb } from '../db/index.js';
+
+export const profilesRouter = Router();
+
+const RESERVED_PROFILE_NAMES = [
+ 'auto', 'smart', 'fast', 'cheap', 'budget',
+ 'intelligence', 'speed', 'active', 'default',
+];
+
+const profileNameSchema = z
+ .string()
+ .min(1, 'Profile name cannot be empty')
+ .max(20, 'Profile name must not exceed 20 characters')
+ .regex(
+ /^[a-zA-Z0-9-_]+$/,
+ 'Only Latin letters, digits, hyphens (-) and underscores (_) are allowed'
+ )
+ .refine(
+ (name) => !RESERVED_PROFILE_NAMES.includes(name.toLowerCase()),
+ 'This name is reserved by the system'
+ );
+
+const createSchema = z.object({
+ name: profileNameSchema,
+ emoji: z.string().max(4).default(''),
+ color: z.string().default('#6366f1'),
+ sourceProfileId: z.number().optional(),
+});
+
+const updateSchema = z.object({
+ name: profileNameSchema.optional(),
+ emoji: z.string().max(4).optional(),
+ color: z.string().optional(),
+ is_favorite: z.boolean().optional(),
+ sort_order: z.number().optional(),
+ auto_sort: z.enum(['intelligence', 'speed', 'budget']).nullable().optional(),
+ layout_config: z.string().nullable().optional(),
+});
+
+function getId(req: Request): number {
+ return parseInt(req.params.id as string);
+}
+
+/**
+ * GET /api/profiles
+ * Fetches all available profiles.
+ * Sorting order: Default profile first -> Favorited profiles -> Custom sorting order.
+ */
+profilesRouter.get('/', (_req: Request, res: Response) => {
+ const db = getDb();
+ const profiles = db.prepare(`
+ SELECT id, name, emoji, color, type, is_favorite, sort_order, auto_sort, layout_config, created_at
+ FROM profiles
+ ORDER BY (CASE WHEN type = 'default' THEN 1 ELSE 0 END) DESC, is_favorite DESC, sort_order ASC, id ASC
+ `).all();
+ res.json(profiles);
+});
+
+// GET /api/profiles/active — get the currently active profile id
+profilesRouter.get('/active', (_req: Request, res: Response) => {
+ const db = getDb();
+ const row = db.prepare(`SELECT value FROM settings WHERE key = 'active_profile_id'`).get() as { value: string } | undefined;
+ const activeProfileId = row ? (parseInt(row.value) || null) : null;
+ res.json({ activeProfileId });
+});
+
+// POST /api/profiles/active — set or clear the active profile
+profilesRouter.post('/active', (req: Request, res: Response) => {
+ const db = getDb();
+ const profileId = req.body?.profileId;
+
+ if (profileId === null || profileId === undefined) {
+ db.prepare(`DELETE FROM settings WHERE key = 'active_profile_id'`).run();
+ res.json({ activeProfileId: null });
+ return;
+ }
+
+ const profile = db.prepare('SELECT id FROM profiles WHERE id = ?').get(Number(profileId)) as any;
+ if (!profile) {
+ res.status(404).json({ error: { message: 'Profile not found' } });
+ return;
+ }
+
+ db.prepare(`
+ INSERT INTO settings (key, value) VALUES ('active_profile_id', ?)
+ ON CONFLICT(key) DO UPDATE SET value = excluded.value
+ `).run(String(profileId));
+ res.json({ activeProfileId: Number(profileId) });
+});
+
+// GET /api/profiles/:id/models — get profile model order
+profilesRouter.get('/:id/models', (req: Request, res: Response) => {
+ const db = getDb();
+ const profileId = getId(req);
+ const profile = db.prepare('SELECT id FROM profiles WHERE id = ?').get(profileId) as any;
+ if (!profile) {
+ res.status(404).json({ error: { message: 'Profile not found' } });
+ return;
+ }
+
+ const rows = db.prepare(`
+ SELECT pm.model_db_id, pm.priority, pm.enabled,
+ m.platform, m.model_id, m.display_name, m.intelligence_rank,
+ m.speed_rank, m.size_label, m.rpm_limit, m.rpd_limit,
+ m.tpm_limit, m.tpd_limit,
+ m.monthly_token_budget
+ FROM profile_models pm
+ JOIN models m ON m.id = pm.model_db_id
+ WHERE pm.profile_id = ? AND m.enabled = 1
+ ORDER BY pm.priority ASC
+ `).all(profileId);
+
+ // Normalize SQLite 0/1 integers to proper booleans for TypeScript client
+ res.json(rows.map((r: any) => ({ ...r, enabled: r.enabled === 1 })));
+});
+
+/**
+ * POST /api/profiles
+ * Creates a new custom profile.
+ * Allows optional cloning of the active profile's model priority and layout configuration.
+ */
+profilesRouter.post('/', (req: Request, res: Response) => {
+ const parsed = createSchema.safeParse(req.body);
+ if (!parsed.success) {
+ res.status(400).json({ error: { message: parsed.error.errors.map(e => e.message).join(', ') } });
+ return;
+ }
+
+ const db = getDb();
+ const { name, emoji, color, sourceProfileId } = parsed.data;
+
+ // Check for case-insensitive duplicate profile names
+ const duplicate = db.prepare('SELECT id FROM profiles WHERE LOWER(name) = LOWER(?)').get(name) as any;
+ if (duplicate) {
+ res.status(409).json({ error: { message: `Profile with name '${name}' already exists` } });
+ return;
+ }
+
+ const maxOrder = (db.prepare('SELECT COALESCE(MAX(sort_order), 0) AS mx FROM profiles').get() as { mx: number }).mx;
+
+ let layoutConfig: string | null = null;
+ let autoSort: string | null = null;
+ if (sourceProfileId) {
+ const source = db.prepare('SELECT layout_config, auto_sort FROM profiles WHERE id = ?').get(sourceProfileId) as any;
+ if (source) {
+ layoutConfig = source.layout_config;
+ autoSort = source.auto_sort;
+ }
+ }
+
+ const result = db.prepare(
+ 'INSERT INTO profiles (name, emoji, color, type, sort_order, layout_config, auto_sort) VALUES (?, ?, ?, ?, ?, ?, ?)'
+ ).run(name, emoji, color, 'custom', maxOrder + 1, layoutConfig, autoSort);
+
+ const profileId = result.lastInsertRowid as number;
+
+ if (sourceProfileId) {
+ const source = db.prepare('SELECT id FROM profiles WHERE id = ?').get(sourceProfileId) as any;
+ if (!source) {
+ copyFromDefault(db, profileId);
+ } else {
+ db.prepare(`
+ INSERT INTO profile_models (profile_id, model_db_id, priority, enabled)
+ SELECT ?, model_db_id, priority, enabled
+ FROM profile_models
+ WHERE profile_id = ?
+ ORDER BY priority ASC
+ `).run(profileId, sourceProfileId);
+ }
+ } else {
+ copyFromDefault(db, profileId);
+ }
+
+ const created = db.prepare('SELECT id, name, emoji, color, type, is_favorite, sort_order, auto_sort, layout_config, created_at FROM profiles WHERE id = ?').get(profileId);
+ res.status(201).json(created);
+});
+
+function copyFromDefault(db: any, profileId: number) {
+ db.prepare(`
+ INSERT INTO profile_models (profile_id, model_db_id, priority, enabled)
+ SELECT ?, model_db_id, priority, enabled
+ FROM fallback_config
+ ORDER BY priority ASC
+ `).run(profileId);
+}
+
+// PUT /api/profiles/:id — update profile metadata
+profilesRouter.put('/:id', (req: Request, res: Response) => {
+ const db = getDb();
+ const profileId = getId(req);
+ const profile = db.prepare('SELECT id, type FROM profiles WHERE id = ?').get(profileId) as any;
+ if (!profile) {
+ res.status(404).json({ error: { message: 'Profile not found' } });
+ return;
+ }
+
+ const parsed = updateSchema.safeParse(req.body);
+ if (!parsed.success) {
+ res.status(400).json({ error: { message: parsed.error.errors.map(e => e.message).join(', ') } });
+ return;
+ }
+
+ // Check for case-insensitive duplicate profile names when editing name
+ if (parsed.data.name !== undefined) {
+ const duplicate = db.prepare('SELECT id FROM profiles WHERE LOWER(name) = LOWER(?) AND id != ?').get(parsed.data.name, profileId) as any;
+ if (duplicate) {
+ res.status(409).json({ error: { message: `Profile with name '${parsed.data.name}' already exists` } });
+ return;
+ }
+ }
+
+ const isProtected = profile.type === 'default' || profile.type === 'builtin';
+ const updates: string[] = [];
+ const values: any[] = [];
+ for (const [key, value] of Object.entries(parsed.data)) {
+ if (value !== undefined) {
+ // Block name/emoji/color edits on protected profiles
+ if (isProtected && (key === 'name' || key === 'emoji' || key === 'color')) {
+ continue;
+ }
+ if (key === 'is_favorite') {
+ updates.push('is_favorite = ?');
+ values.push(value ? 1 : 0);
+ } else {
+ updates.push(`${key} = ?`);
+ values.push(value);
+ }
+ }
+ }
+
+ if (updates.length > 0) {
+ values.push(profileId);
+ db.prepare(`UPDATE profiles SET ${updates.join(', ')} WHERE id = ?`).run(...values);
+ }
+
+ // If auto_sort was updated to a preset, automatically physically sort the models in DB
+ if (parsed.data.auto_sort) {
+ sortProfileModels(db, profileId, parsed.data.auto_sort);
+ }
+
+ const updated = db.prepare('SELECT id, name, emoji, color, type, is_favorite, sort_order, auto_sort, layout_config, created_at FROM profiles WHERE id = ?').get(profileId);
+ res.json(updated);
+});
+
+// PUT /api/profiles/:id/reorder — update model order + enabled for a profile
+const reorderSchema = z.array(z.object({
+ modelDbId: z.number(),
+ priority: z.number(),
+ enabled: z.boolean(),
+}));
+
+profilesRouter.put('/:id/reorder', (req: Request, res: Response) => {
+ const db = getDb();
+ const profileId = getId(req);
+ const profile = db.prepare('SELECT id FROM profiles WHERE id = ?').get(profileId) as any;
+ if (!profile) {
+ res.status(404).json({ error: { message: 'Profile not found' } });
+ return;
+ }
+
+ const parsed = reorderSchema.safeParse(req.body);
+ if (!parsed.success) {
+ res.status(400).json({ error: { message: parsed.error.errors.map(e => e.message).join(', ') } });
+ return;
+ }
+
+ const transaction = db.transaction(() => {
+ db.prepare('DELETE FROM profile_models WHERE profile_id = ?').run(profileId);
+ const insert = db.prepare('INSERT INTO profile_models (profile_id, model_db_id, priority, enabled) VALUES (?, ?, ?, ?)');
+ for (const entry of parsed.data) {
+ insert.run(profileId, entry.modelDbId, entry.priority, entry.enabled ? 1 : 0);
+ }
+ });
+ transaction();
+
+ res.json({ success: true });
+});
+
+// POST /api/profiles/:id/reset — reset a profile to fallback baseline
+profilesRouter.post('/:id/reset', (req: Request, res: Response) => {
+ const db = getDb();
+ const profileId = getId(req);
+ const profile = db.prepare('SELECT id FROM profiles WHERE id = ?').get(profileId) as any;
+ if (!profile) {
+ res.status(404).json({ error: { message: 'Profile not found' } });
+ return;
+ }
+
+ const baselineLayout = JSON.stringify({
+ viewMode: "list",
+ compactMode: false,
+ limitsMode: false,
+ limitsVariant: "circle",
+ sortDisabledToBottom: false,
+ kanbanLayout: [{ id: "board-default", type: "board", title: "Default", emoji: "📋", color: "rgb(99, 102, 241)", collapsed: false, items: [] }],
+ tierLayout: [{ id: "tier-default", type: "tier", title: "Default", emoji: "📋", color: "rgb(99, 102, 241)", collapsed: false, items: [] }]
+ });
+
+ const transaction = db.transaction(() => {
+ // Reset layout_config and auto_sort
+ db.prepare('UPDATE profiles SET layout_config = ?, auto_sort = NULL WHERE id = ?').run(baselineLayout, profileId);
+
+ // Copy models priority/enabled from fallback_config
+ db.prepare('DELETE FROM profile_models WHERE profile_id = ?').run(profileId);
+ db.prepare(`
+ INSERT INTO profile_models (profile_id, model_db_id, priority, enabled)
+ SELECT ?, model_db_id, priority, enabled
+ FROM fallback_config
+ ORDER BY priority ASC
+ `).run(profileId);
+ });
+ transaction();
+
+ const updated = db.prepare('SELECT id, name, emoji, color, type, is_favorite, sort_order, auto_sort, layout_config, created_at FROM profiles WHERE id = ?').get(profileId);
+ res.json(updated);
+});
+
+// DELETE /api/profiles/:id — delete a profile
+profilesRouter.delete('/:id', (req: Request, res: Response) => {
+ const db = getDb();
+ const profileId = getId(req);
+ const profile = db.prepare('SELECT id, type FROM profiles WHERE id = ?').get(profileId) as any;
+ if (!profile) {
+ res.status(404).json({ error: { message: 'Profile not found' } });
+ return;
+ }
+ if (profile.type === 'default' || profile.type === 'builtin') {
+ res.status(400).json({ error: { message: 'Cannot delete the default profile' } });
+ return;
+ }
+ const count = db.prepare('SELECT COUNT(*) as cnt FROM profiles').get() as { cnt: number };
+ if (count.cnt <= 1) {
+ res.status(400).json({ error: { message: 'Cannot delete the last profile' } });
+ return;
+ }
+
+ // If the deleted profile is the currently active one, switch to Default
+ const activeRow = db.prepare(`SELECT value FROM settings WHERE key = 'active_profile_id'`).get() as { value: string } | undefined;
+ const activeId = activeRow ? parseInt(activeRow.value) : null;
+
+ db.prepare('DELETE FROM profiles WHERE id = ?').run(profileId);
+
+ if (activeId === profileId) {
+ const defaultProf = db.prepare("SELECT id FROM profiles WHERE type = 'default' OR type = 'builtin' ORDER BY id ASC LIMIT 1").get() as { id: number } | undefined;
+ const fallbackId = defaultProf?.id ?? (db.prepare('SELECT id FROM profiles ORDER BY sort_order ASC LIMIT 1').get() as { id: number })?.id;
+ if (fallbackId) {
+ db.prepare(`INSERT INTO settings (key, value) VALUES ('active_profile_id', ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value`).run(String(fallbackId));
+ }
+ }
+
+ res.json({ success: true });
+});
+
+// POST /api/profiles/:id/sort/:preset — sort models in a profile by a preset
+const SORT_PRESETS: Record<string, string> = {
+ intelligence: 'm.intelligence_rank ASC',
+ speed: 'm.speed_rank ASC',
+};
+
+function getBudgetScore(m: { monthly_token_budget: string; tpd_limit: number | null }): number {
+ if (m.tpd_limit != null) return m.tpd_limit * 30;
+
+ const str = m.monthly_token_budget;
+ if (!str) return 0;
+ if (str.toLowerCase().includes('unlimited') || str.includes('∞')) return Infinity;
+
+ const cleanStr = str.split('(')[0];
+ const matches = cleanStr.match(/[\d.]+/g);
+ let maxNum = 0;
+ if (matches) {
+ maxNum = Math.max(...matches.map(mStr => parseFloat(mStr)));
+ }
+
+ let mult = 1;
+ const upper = cleanStr.toUpperCase();
+ if (upper.includes('B')) mult = 1_000_000_000;
+ else if (upper.includes('M')) mult = 1_000_000;
+ else if (upper.includes('K')) mult = 1_000;
+
+ return maxNum * mult;
+}
+
+function sortProfileModels(db: any, profileId: number, preset: string) {
+ let models: { id: number }[] = [];
+
+ if (preset === 'budget') {
+ const allModels = db.prepare(`SELECT id, monthly_token_budget, tpd_limit FROM models`).all() as any[];
+ allModels.sort((a, b) => getBudgetScore(b) - getBudgetScore(a));
+ models = allModels.map(m => ({ id: m.id }));
+ } else {
+ const orderBy = SORT_PRESETS[preset];
+ if (!orderBy) {
+ throw new Error(`Unknown preset: ${preset}. Use: intelligence, speed, budget`);
+ }
+ models = db.prepare(`SELECT m.id FROM models m ORDER BY ${orderBy}`).all() as { id: number }[];
+ }
+
+ // Preserve existing enabled flags so sorting doesn't reset disabled models
+ const existing = db.prepare(`
+ SELECT model_db_id, enabled FROM profile_models WHERE profile_id = ?
+ `).all(profileId) as { model_db_id: number; enabled: number }[];
+ const enabledMap = new Map(existing.map(e => [e.model_db_id, e.enabled]));
+
+ const transaction = db.transaction(() => {
+ db.prepare('DELETE FROM profile_models WHERE profile_id = ?').run(profileId);
+ const insert = db.prepare('INSERT INTO profile_models (profile_id, model_db_id, priority, enabled) VALUES (?, ?, ?, ?)');
+ for (let i = 0; i < models.length; i++) {
+ // Use existing enabled state if available, default to 1 for newly added models
+ const enabled = enabledMap.has(models[i].id) ? enabledMap.get(models[i].id) : 1;
+ insert.run(profileId, models[i].id, i + 1, enabled);
+ }
+ });
+ transaction();
+}
+
+profilesRouter.post('/:id/sort/:preset', (req: Request, res: Response) => {
+ const db = getDb();
+ const profileId = getId(req);
+ const profile = db.prepare('SELECT id FROM profiles WHERE id = ?').get(profileId) as any;
+ if (!profile) {
+ res.status(404).json({ error: { message: 'Profile not found' } });
+ return;
+ }
+
+ const preset = String(req.params.preset);
+
+ try {
+ sortProfileModels(db, profileId, preset);
+ res.json({ success: true, preset });
+ } catch (error: any) {
+ res.status(400).json({ error: { message: error.message } });
+ }
+});
+
+// Initialize built-in profiles if they don't exist
+export function seedProfiles(db: any): void {
+ const count = db.prepare("SELECT COUNT(*) as cnt FROM profiles WHERE type = 'default' OR type = 'builtin'").get() as { cnt: number };
+ if (count.cnt > 0) return;
+
+ const builtins: Array<{
+ name: string;
+ emoji: string;
+ color: string;
+ profileType: string;
+ modelScores: Array<{ namePattern: string; score: number }>;
+ }> = [
+ {
+ name: 'Default',
+ emoji: '⭐',
+ color: '#6366f1',
+ profileType: 'default',
+ modelScores: [
+ { namePattern: 'gpt-4o', score: 3 },
+ { namePattern: 'qwen3-coder', score: 2 },
+ { namePattern: 'gemini', score: 1 },
+ ],
+ }
+ ];
+
+ const insertProfile = db.prepare('INSERT INTO profiles (name, emoji, color, type, sort_order) VALUES (?, ?, ?, ?, ?)');
+ const insertModel = db.prepare('INSERT INTO profile_models (profile_id, model_db_id, priority, enabled) VALUES (?, ?, ?, ?)');
+
+ const seed = db.transaction(() => {
+ for (const builtin of builtins) {
+ const result = insertProfile.run(builtin.name, builtin.emoji, builtin.color, builtin.profileType, -1);
+ const profileId = result.lastInsertRowid as number;
+
+ const models = db.prepare('SELECT id, LOWER(display_name) as name FROM models ORDER BY id ASC').all() as { id: number; name: string }[];
+
+ const scored = models.map(m => {
+ let score = 0;
+ for (const s of builtin.modelScores) {
+ if (m.name.includes(s.namePattern)) {
+ score = s.score;
+ break;
+ }
+ }
+ return { ...m, score };
+ });
+
+ scored.sort((a, b) => {
+ if (b.score !== a.score) return b.score - a.score;
+ return a.name.localeCompare(b.name);
+ });
+
+ for (let i = 0; i < scored.length; i++) {
+ insertModel.run(profileId, scored[i].id, i + 1, 1);
+ }
+
+ // Set the default active profile
+ db.prepare(`
+ INSERT INTO settings (key, value) VALUES ('active_profile_id', ?)
+ ON CONFLICT(key) DO UPDATE SET value = excluded.value
+ `).run(String(profileId));
+ }
+ });
+ seed();
+
+ console.log(`Seeded Default profile`);
+}
\ No newline at end of file
diff --git a/server/src/routes/proxy.ts b/server/src/routes/proxy.ts
index b43d7f1b..ff5601d2 100644
--- a/server/src/routes/proxy.ts
+++ b/server/src/routes/proxy.ts
@@ -3,7 +3,7 @@ import { Router } from 'express';
import type { Request, Response } from 'express';
import { z } from 'zod';
import type { ChatMessage, ModelListRow } from '@freellmapi/shared/types.js';
-import { routeRequest, recordRateLimitHit, recordSuccess, hasEnabledVisionModel, hasEnabledToolsModel, type RouteResult } from '../services/router.js';
+import { routeRequest, resolveRoutingChain, recordRateLimitHit, recordSuccess, hasEnabledVisionModel, hasEnabledToolsModel, type RouteResult, type ResolvedChain } from '../services/router.js';
import { recordRequest, recordTokens, setCooldown, getCooldownDurationForLimit, PAYMENT_REQUIRED_COOLDOWN_MS, MODEL_FORBIDDEN_COOLDOWN_MS } from '../services/ratelimit.js';
import { pruneRequestAnalytics } from '../services/request-retention.js';
import { runEmbeddings, EmbeddingsError } from '../services/embeddings.js';
@@ -23,7 +23,9 @@ export const proxyRouter = Router();
const AUTO_MODEL_ID = 'auto';
function isAutoModel(modelId: string | undefined): boolean {
- return modelId === AUTO_MODEL_ID;
+ if (!modelId) return true;
+ const lower = modelId.toLowerCase();
+ return lower === AUTO_MODEL_ID || lower.startsWith(`${AUTO_MODEL_ID}:`);
}
// Constant-time string comparison for the unified API key. Plain `===` leaks
@@ -62,34 +64,24 @@ export function extractApiToken(req: Request): string | undefined {
const stickySessionMap = new Map<string, { modelDbId: number; lastUsed: number }>();
const STICKY_TTL_MS = 30 * 60 * 1000; // 30 min session TTL
-function getSessionKey(messages: ChatMessage[], sessionIdHeader?: string): string {
- // Explicit session pinning: clients that manage their own conversation ids
- // (agent harnesses especially) can send X-Session-Id and get exact
- // affinity regardless of how their message history mutates. (#231)
- if (sessionIdHeader) return `hdr:${sessionIdHeader}`;
-
- // Otherwise the first user message identifies the session — clients re-send
- // the full conversation each turn, so it is stable across turns. Flatten
- // array-of-blocks content before hashing: opencode-style agents send
- // [{type:'text',...}] even for plain text, and the old string-only check
- // silently disabled stickiness for them, re-routing every turn (#231 audit:
- // observed a rank-2 → rank-11 mid-conversation flip). No turn-count suffix:
- // the old ':single'/':multi' split guaranteed a sticky MISS on turn 2,
- // exactly where agents replay the assistant's tool-call dialect and a model
- // switch causes cross-dialect contamination.
+function getSessionKey(messages: ChatMessage[], sessionIdHeader?: string, strategyKey?: string): string {
+ if (sessionIdHeader) {
+ return strategyKey ? `hdr:${sessionIdHeader}::${strategyKey}` : `hdr:${sessionIdHeader}`;
+ }
+
const firstUser = messages.find(m => m.role === 'user');
if (!firstUser) return '';
const text = contentToString(firstUser.content ?? '');
if (!text) return '';
- return crypto.createHash('sha1').update(text).digest('hex');
+ const payload = strategyKey ? `${text}::${strategyKey}` : text;
+ return crypto.createHash('sha1').update(payload).digest('hex');
}
-export function getStickyModel(messages: ChatMessage[], sessionIdHeader?: string): number | undefined {
- // Only apply sticky for multi-turn (has assistant messages = continuation)
+export function getStickyModel(messages: ChatMessage[], sessionIdHeader?: string, strategyKey?: string): number | undefined {
const hasAssistant = messages.some(m => m.role === 'assistant');
if (!hasAssistant) return undefined;
- const key = getSessionKey(messages, sessionIdHeader);
+ const key = getSessionKey(messages, sessionIdHeader, strategyKey);
if (!key) return undefined;
const entry = stickySessionMap.get(key);
@@ -102,8 +94,8 @@ export function getStickyModel(messages: ChatMessage[], sessionIdHeader?: string
return entry.modelDbId;
}
-export function setStickyModel(messages: ChatMessage[], modelDbId: number, sessionIdHeader?: string) {
- const key = getSessionKey(messages, sessionIdHeader);
+export function setStickyModel(messages: ChatMessage[], modelDbId: number, sessionIdHeader?: string, strategyKey?: string) {
+ const key = getSessionKey(messages, sessionIdHeader, strategyKey);
if (!key) return;
stickySessionMap.set(key, { modelDbId, lastUsed: Date.now() });
@@ -643,12 +635,20 @@ proxyRouter.post('/chat/completions', async (req: Request, res: Response) => {
const rawSessionId = req.headers['x-session-id'];
const sessionIdHeader = Array.isArray(rawSessionId) ? rawSessionId[0] : rawSessionId;
+ let resolvedChain: ResolvedChain | undefined;
+ let strategyKey: string | undefined;
+
+ if (isAutoModel(requestedModel)) {
+ resolvedChain = resolveRoutingChain(requestedModel);
+ strategyKey = resolvedChain.strategyKey;
+ }
+
// Context handoff only applies to auto-routed requests. Pinned-model requests
// are deliberate client choices; injecting "you are taking over" there would
// be semantically wrong.
const isAutoRouted = !requestedModel || isAutoModel(requestedModel);
const handoffMode = isAutoRouted ? getContextHandoffMode() : ('off' as const);
- const sessionKey = handoffMode !== 'off' ? getSessionKey(messages, sessionIdHeader) : '';
+ const sessionKey = handoffMode !== 'off' ? getSessionKey(messages, sessionIdHeader, strategyKey) : '';
if (handoffMode !== 'off' && sessionKey) {
recordIncomingMessages(sessionKey, messages);
}
@@ -664,8 +664,7 @@ proxyRouter.post('/chat/completions', async (req: Request, res: Response) => {
// Sticky-session is the fallback when no `model` field was sent at all.
let preferredModel: number | undefined;
if (isAutoModel(requestedModel)) {
- // Explicit "auto" → behave exactly like an omitted model field.
- preferredModel = getStickyModel(messages, sessionIdHeader);
+ preferredModel = getStickyModel(messages, sessionIdHeader, strategyKey);
} else if (requestedModel) {
const db = getDb();
const enabled = db.prepare('SELECT id FROM models WHERE model_id = ? AND enabled = 1').get(requestedModel) as { id: number } | undefined;
@@ -684,7 +683,7 @@ proxyRouter.post('/chat/completions', async (req: Request, res: Response) => {
return;
}
} else {
- preferredModel = getStickyModel(messages, sessionIdHeader);
+ preferredModel = getStickyModel(messages, sessionIdHeader, strategyKey);
}
// For analytics: the model id the client pinned, null when auto-routed
@@ -707,7 +706,7 @@ proxyRouter.post('/chat/completions', async (req: Request, res: Response) => {
// model is on record). Turns where injection can't happen — every turn 1, and
// sessions that never switched — pay no headroom tax.
const routingEstimate = handoffPossible ? estimatedTotal + HANDOFF_MAX_TOKENS : estimatedTotal;
- route = routeRequest(routingEstimate, skipKeys.size > 0 ? skipKeys : undefined, preferredModel, hasImage, wantsTools, skipModels.size > 0 ? skipModels : undefined);
+ route = routeRequest(routingEstimate, skipKeys.size > 0 ? skipKeys : undefined, preferredModel, hasImage, wantsTools, skipModels.size > 0 ? skipModels : undefined, resolvedChain?.chain);
} catch (err: any) {
// No more models available
if (lastError) {
@@ -941,7 +940,7 @@ proxyRouter.post('/chat/completions', async (req: Request, res: Response) => {
recordRequest(route.platform, route.modelId, route.keyId);
recordTokens(route.platform, route.modelId, route.keyId, estimatedInputTokens + injectedHandoffTokens + totalOutputTokens);
recordSuccess(route.modelDbId);
- setStickyModel(messages, route.modelDbId, sessionIdHeader);
+ setStickyModel(messages, route.modelDbId, sessionIdHeader, strategyKey);
if (handoffMode !== 'off' && sessionKey) recordSuccessfulModel({ sessionKey, modelKey });
logRequest(route.platform, route.modelId, route.keyId, 'success', estimatedInputTokens + injectedHandoffTokens, totalOutputTokens, Date.now() - start, null, ttfbMs, pinnedModelId);
return;
@@ -1010,7 +1009,7 @@ proxyRouter.post('/chat/completions', async (req: Request, res: Response) => {
recordRequest(route.platform, route.modelId, route.keyId);
recordTokens(route.platform, route.modelId, route.keyId, totalTokens);
recordSuccess(route.modelDbId);
- setStickyModel(messages, route.modelDbId, sessionIdHeader);
+ setStickyModel(messages, route.modelDbId, sessionIdHeader, strategyKey);
if (handoffMode !== 'off' && sessionKey) recordSuccessfulModel({ sessionKey, modelKey });
res.setHeader('X-Routed-Via', `${route.platform}/${route.modelId}`);
diff --git a/server/src/services/router.ts b/server/src/services/router.ts
index 255b9342..cdab3e78 100644
--- a/server/src/services/router.ts
+++ b/server/src/services/router.ts
@@ -395,14 +395,39 @@ function orderChain(chain: ChainRow[], strategy: RoutingStrategy): ChainRow[] {
* @param requireVision - only consider models that accept image input (#118)
* @param requireTools - only consider models that emit structured tool_calls
*/
-export function routeRequest(estimatedTokens = 1000, skipKeys?: Set<string>, preferredModelDbId?: number, requireVision = false, requireTools = false, skipModels?: Set<number>): RouteResult {
- const db = getDb();
+export interface ResolvedChain {
+ chain: ChainRow[];
+ strategyKey: string;
+}
- const strategy = getRoutingStrategy();
- if (strategy !== 'priority') refreshStatsCache(db);
+const GLOBAL_SORT_ALIASES: Record<string, string> = {
+ smart: 'smart', smartest: 'smart', intelligence: 'smart',
+ fast: 'fast', fastest: 'fast', speed: 'fast',
+ cheap: 'cheap', cheapest: 'cheap', price: 'cheap', budget: 'cheap',
+ reliable: 'reliable', reliability: 'reliable',
+ balanced: 'balanced',
+};
+
+function getActiveChain(db: Database): ChainRow[] {
+ const activeProfileSetting = db.prepare("SELECT value FROM settings WHERE key = 'active_profile_id'").get() as { value: string } | undefined;
+ if (activeProfileSetting) {
+ const profileId = parseInt(activeProfileSetting.value, 10);
+ const chain = db.prepare(`
+ SELECT pm.model_db_id, pm.priority, pm.enabled,
+ m.platform, m.model_id, m.display_name, m.intelligence_rank,
+ m.size_label, m.monthly_token_budget,
+ m.rpm_limit, m.rpd_limit, m.tpm_limit, m.tpd_limit, m.supports_vision,
+ m.supports_tools, m.context_window, m.key_id
+ FROM profile_models pm
+ JOIN models m ON m.id = pm.model_db_id AND m.enabled = 1
+ WHERE pm.profile_id = ?
+ ORDER BY pm.priority ASC
+ `).all(profileId) as ChainRow[];
+
+ if (chain.length > 0) return chain;
+ }
- // Get the enabled fallback chain joined with the fields the scorer needs.
- const chain = db.prepare(`
+ return db.prepare(`
SELECT fc.model_db_id, fc.priority, fc.enabled,
m.platform, m.model_id, m.display_name, m.intelligence_rank,
m.size_label, m.monthly_token_budget,
@@ -410,17 +435,130 @@ export function routeRequest(estimatedTokens = 1000, skipKeys?: Set<string>, pre
m.supports_tools, m.context_window, m.key_id
FROM fallback_config fc
JOIN models m ON m.id = fc.model_db_id AND m.enabled = 1
- WHERE fc.enabled = 1
+ ORDER BY fc.priority ASC
+ `).all() as ChainRow[];
+}
+
+function getChainByProfileName(db: Database, name: string): ChainRow[] | null {
+ const profile = db.prepare("SELECT id FROM profiles WHERE LOWER(name) = ?").get(name.toLowerCase()) as { id: number } | undefined;
+ if (!profile) return null;
+
+ return db.prepare(`
+ SELECT pm.model_db_id, pm.priority, pm.enabled,
+ m.platform, m.model_id, m.display_name, m.intelligence_rank,
+ m.size_label, m.monthly_token_budget,
+ m.rpm_limit, m.rpd_limit, m.tpm_limit, m.tpd_limit, m.supports_vision,
+ m.supports_tools, m.context_window, m.key_id
+ FROM profile_models pm
+ JOIN models m ON m.id = pm.model_db_id AND m.enabled = 1
+ WHERE pm.profile_id = ?
+ ORDER BY pm.priority ASC
+ `).all(profile.id) as ChainRow[];
+}
+
+function getChainByGlobalSort(db: Database, globalAxis: string): ChainRow[] {
+ const allEnabled = db.prepare(`
+ SELECT m.id as model_db_id, 0 as priority, 1 as enabled,
+ m.platform, m.model_id, m.display_name, m.intelligence_rank,
+ m.size_label, m.monthly_token_budget,
+ m.rpm_limit, m.rpd_limit, m.tpm_limit, m.tpd_limit, m.supports_vision,
+ m.supports_tools, m.context_window, m.key_id
+ FROM models m
+ WHERE m.enabled = 1
`).all() as ChainRow[];
+ const strategyMap: Record<string, RoutingStrategy> = {
+ 'smart': 'smartest',
+ 'fast': 'fastest',
+ 'cheap': 'balanced',
+ 'reliable': 'reliable',
+ 'balanced': 'balanced'
+ };
+ const strat = strategyMap[globalAxis] || 'balanced';
+
+ return orderChain(allEnabled, strat);
+}
+
+export function resolveRoutingChain(modelString: string | undefined): ResolvedChain {
+ const db = getDb();
+
+ if (!modelString || modelString.toLowerCase() === 'auto') {
+ return { chain: getActiveChain(db), strategyKey: 'auto' };
+ }
+
+ const lower = modelString.toLowerCase();
+ if (!lower.startsWith('auto:')) {
+ return { chain: getActiveChain(db), strategyKey: 'auto' };
+ }
+
+ const suffix = lower.slice('auto:'.length).trim();
+ if (!suffix) {
+ return { chain: getActiveChain(db), strategyKey: 'auto' };
+ }
+
+ const globalAxis = GLOBAL_SORT_ALIASES[suffix];
+ if (globalAxis) {
+ const chain = getChainByGlobalSort(db, globalAxis);
+ if (chain.length === 0) {
+ const err = new Error(`No enabled models available for global sort '${suffix}'`) as any;
+ err.status = 400;
+ throw err;
+ }
+ return { chain, strategyKey: `auto:${globalAxis}` };
+ }
+
+ const chain = getChainByProfileName(db, suffix);
+ if (!chain) {
+ const err = new Error(`Profile '${suffix}' not found. Use 'auto' for the default profile, or call /v1/models for available options.`) as any;
+ err.status = 400;
+ throw err;
+ }
+
+ const enabledModels = chain.filter(e => e.enabled);
+ if (enabledModels.length === 0) {
+ const err = new Error(`Profile '${suffix}' has no enabled models. Add models to this profile in the dashboard.`) as any;
+ err.status = 400;
+ throw err;
+ }
+
+ return { chain, strategyKey: `auto:${suffix}` };
+}
+
+export function routeRequest(estimatedTokens = 1000, skipKeys?: Set<string>, preferredModelDbId?: number, requireVision = false, requireTools = false, skipModels?: Set<number>, prefetchedChain?: ChainRow[]): RouteResult {
+ const db = getDb();
+
+ const strategy = getRoutingStrategy();
+ if (strategy !== 'priority') refreshStatsCache(db);
+
+ const chain = prefetchedChain ?? getActiveChain(db).filter(e => e.enabled);
+
const sortedChain = orderChain(chain, strategy);
- // Sticky session: move preferred model to front of chain
+ // Sticky session / Explicit pinning: move preferred model to front of chain
if (preferredModelDbId) {
const idx = sortedChain.findIndex(e => e.model_db_id === preferredModelDbId);
- if (idx > 0) {
- const [preferred] = sortedChain.splice(idx, 1);
- sortedChain.unshift(preferred);
+ if (idx >= 0) {
+ if (idx > 0) {
+ const [preferred] = sortedChain.splice(idx, 1);
+ sortedChain.unshift(preferred);
+ }
+ } else {
+ // The requested model is not in the current routing chain (e.g. it's a
+ // custom model or not added to the active profile). We must fulfill the
+ // explicit request by injecting it at the front.
+ const pinnedRow = db.prepare(`
+ SELECT m.id as model_db_id, 0 as priority, 1 as enabled,
+ m.platform, m.model_id, m.display_name, m.intelligence_rank,
+ m.size_label, m.monthly_token_budget,
+ m.rpm_limit, m.rpd_limit, m.tpm_limit, m.tpd_limit, m.supports_vision,
+ m.supports_tools, m.context_window, m.key_id
+ FROM models m
+ WHERE m.id = ? AND m.enabled = 1
+ `).get(preferredModelDbId) as ChainRow | undefined;
+
+ if (pinnedRow) {
+ sortedChain.unshift(pinnedRow);
+ }
}
}