Files
Epicure/apps/web/lib/site-settings.ts
T
Arnaud affa6f5c3d feat: admin-configurable default AI providers/models (v0.30.0)
Add DEFAULT_{TEXT,VISION,MEAL_PLAN}_{PROVIDER,MODEL} site settings,
editable from Settings -> Admin (new AdminDefaultModelForm, mirroring
the per-user model-prefs UI). getDefaultProviderWithKey now accepts
the use case and checks the admin default (after the user's own BYOK
key, before the old "first configured site key" heuristic) so admins
can pin a specific provider/model per feature instead of it being
whichever key happens to exist.

Wired into scale/substitute/batch-cook/meal-plan generation routes,
which previously called getDefaultProviderWithKey() without a use
case and so never had visibility into per-feature routing at all.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-14 13:54:24 +02:00

108 lines
3.0 KiB
TypeScript

import { db, siteSettings, eq } from "@epicure/db";
import { encrypt, decrypt } from "@/lib/encrypt";
export type SiteSettingKey =
| "OPENAI_API_KEY"
| "ANTHROPIC_API_KEY"
| "OPENROUTER_API_KEY"
| "OPENROUTER_DEFAULT_MODEL"
| "OLLAMA_BASE_URL"
| "NEXT_PUBLIC_VAPID_PUBLIC_KEY"
| "VAPID_PRIVATE_KEY"
| "SIGNUPS_DISABLED"
| "DEFAULT_TEXT_PROVIDER"
| "DEFAULT_TEXT_MODEL"
| "DEFAULT_VISION_PROVIDER"
| "DEFAULT_VISION_MODEL"
| "DEFAULT_MEAL_PLAN_PROVIDER"
| "DEFAULT_MEAL_PLAN_MODEL";
const SECRET_KEYS: SiteSettingKey[] = [
"OPENAI_API_KEY",
"ANTHROPIC_API_KEY",
"OPENROUTER_API_KEY",
"VAPID_PRIVATE_KEY",
];
export function isSecretKey(key: SiteSettingKey): boolean {
return SECRET_KEYS.includes(key);
}
export async function getSiteSetting(key: SiteSettingKey): Promise<string | null> {
try {
const row = await db.query.siteSettings.findFirst({ where: eq(siteSettings.key, key) });
if (row?.value) {
return row.isSecret ? decrypt(row.value) : row.value;
}
} catch {
// fall through to env
}
return process.env[key] ?? null;
}
export async function getAllSiteSettings(): Promise<Record<string, { value: string | null; isSecret: boolean; fromDb: boolean }>> {
const rows = await db.query.siteSettings.findMany();
const dbMap = new Map(rows.map((r) => [r.key, r]));
const KNOWN_KEYS: SiteSettingKey[] = [
"OPENAI_API_KEY",
"ANTHROPIC_API_KEY",
"OPENROUTER_API_KEY",
"OPENROUTER_DEFAULT_MODEL",
"OLLAMA_BASE_URL",
"NEXT_PUBLIC_VAPID_PUBLIC_KEY",
"VAPID_PRIVATE_KEY",
"DEFAULT_TEXT_PROVIDER",
"DEFAULT_TEXT_MODEL",
"DEFAULT_VISION_PROVIDER",
"DEFAULT_VISION_MODEL",
"DEFAULT_MEAL_PLAN_PROVIDER",
"DEFAULT_MEAL_PLAN_MODEL",
];
const result: Record<string, { value: string | null; isSecret: boolean; fromDb: boolean }> = {};
for (const k of KNOWN_KEYS) {
const row = dbMap.get(k);
const secret = isSecretKey(k);
if (row) {
result[k] = {
value: secret ? "••••••••••••" : (row.value ?? null),
isSecret: secret,
fromDb: true,
};
} else {
const envVal = process.env[k];
result[k] = {
value: envVal ? (secret ? "••••••••••••" : envVal) : null,
isSecret: secret,
fromDb: false,
};
}
}
return result;
}
export async function isSignupsDisabled(): Promise<boolean> {
return (await getSiteSetting("SIGNUPS_DISABLED")) === "true";
}
export async function setSiteSetting(
key: SiteSettingKey,
value: string | null,
updatedById: string
): Promise<void> {
if (value === null || value === "") {
await db.delete(siteSettings).where(eq(siteSettings.key, key));
return;
}
const secret = isSecretKey(key);
const stored = secret ? encrypt(value) : value;
await db
.insert(siteSettings)
.values({ key, value: stored, isSecret: secret, updatedById, updatedAt: new Date() })
.onConflictDoUpdate({
target: siteSettings.key,
set: { value: stored, updatedAt: new Date(), updatedById },
});
}