fix(backend): validate configured model provider pair

This commit is contained in:
User
2026-07-14 18:39:37 +02:00
parent c463de8da2
commit 3d23d0543c
5 changed files with 61 additions and 7 deletions
+7 -2
View File
@@ -23,7 +23,7 @@ export interface BuildAppDeps {
}
export function buildApp(config: AppConfig, deps?: BuildAppDeps): FastifyInstance {
const app = Fastify({ logger: false });
const app = Fastify({ logger: { level: "warn" }, disableRequestLogging: true });
// Allow any origin in dev/e2e; tighten in production via config if needed.
app.register(cors, {
@@ -45,7 +45,12 @@ export function buildApp(config: AppConfig, deps?: BuildAppDeps): FastifyInstanc
Math.round(config.ollamaEnsureTimeoutMs / 1000),
);
const listModels = deps?.listModels ?? createPiModelLister(config);
const listModels = deps?.listModels ?? createPiModelLister(config, {
warn: (detail) => app.log.warn(
{ component: "pi-model-list", detail },
"Pi enabled-model configuration warning",
),
});
const getSettings = deps?.getSettings ?? (() => effectiveSettings(config, loadSettings(config)));
const authenticate = authPreHandler(config.authMode);
+5 -1
View File
@@ -34,7 +34,11 @@ export function metaRoutes(
const fn = deps.listModels ?? (async () => []);
try {
return { models: await fn() };
} catch {
} catch (error) {
app.log.warn({
component: "pi-model-list",
errorType: error instanceof Error ? error.name : typeof error,
}, "Pi model listing failed");
// Graceful fallback: Pi may not be running; don't crash the server.
return { models: [] as PiModel[] };
}
+7 -3
View File
@@ -25,15 +25,19 @@ export function settingsRoutes(
app.put("/settings", async (req, reply) => {
const b = (req.body ?? {}) as Settings;
if (b.model) {
let available: { id: string }[] = [];
let available: { provider: string; id: string }[] = [];
try {
available = await deps.listModels();
} catch {
available = [];
}
// Only validate when Pi gave us a non-empty list; otherwise allow (degraded).
if (available.length > 0 && !available.some((m) => m.id === b.model)) {
return reply.code(400).send({ error: `Unknown model: ${b.model}` });
if (available.length > 0 && !available.some(
(candidate) => candidate.provider === b.provider && candidate.id === b.model,
)) {
return reply.code(400).send({
error: `Unknown model: ${b.provider ?? "unknown"}/${b.model}`,
});
}
}
const next: Settings = {