feat(backend): /settings GET+PUT, /models PiModel shape, app wiring
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -1,14 +1,15 @@
|
||||
import { readdirSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import type { PiModel } from "../pi/list-models.js";
|
||||
|
||||
export type ListModelsFn = () => Promise<string[]>;
|
||||
export type ListModelsFn = () => Promise<PiModel[]>;
|
||||
|
||||
/**
|
||||
* List YAML workspace configs found in <harnessDir>/workspaces/*.yaml.
|
||||
* Returns [{name, file}] — no database credentials or secrets.
|
||||
*/
|
||||
function listWorkspaces(harnessDir: string): { name: string; file: string }[] {
|
||||
export function listWorkspaces(harnessDir: string): { name: string; file: string }[] {
|
||||
const dir = join(harnessDir, "workspaces");
|
||||
let entries: string[];
|
||||
try {
|
||||
@@ -18,10 +19,7 @@ function listWorkspaces(harnessDir: string): { name: string; file: string }[] {
|
||||
}
|
||||
return entries
|
||||
.filter((f) => f.endsWith(".yaml") || f.endsWith(".yml"))
|
||||
.map((f) => ({
|
||||
name: f.replace(/\.ya?ml$/, ""),
|
||||
file: f,
|
||||
}));
|
||||
.map((f) => ({ name: f.replace(/\.ya?ml$/, ""), file: f }));
|
||||
}
|
||||
|
||||
export function metaRoutes(
|
||||
@@ -32,28 +30,13 @@ export function metaRoutes(
|
||||
return listWorkspaces(deps.harnessDir);
|
||||
});
|
||||
|
||||
app.get("/models", async (_req, reply) => {
|
||||
const fn = deps.listModels ?? defaultListModels;
|
||||
app.get("/models", async () => {
|
||||
const fn = deps.listModels ?? (async () => []);
|
||||
try {
|
||||
const models = await fn();
|
||||
return { models };
|
||||
return { models: await fn() };
|
||||
} catch {
|
||||
// Graceful fallback: Pi may not be running; don't crash the server.
|
||||
return { models: [] };
|
||||
return { models: [] as PiModel[] };
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Default implementation: spawns a short-lived `tht` invocation that asks a Pi
|
||||
* process for available models via get_available_models. This is intentionally
|
||||
* behind the injectable seam so tests can stub it without spawning real processes.
|
||||
*
|
||||
* In the MVP we return an empty list — the real spawn path can be wired in later
|
||||
* once a Pi-side "list models" RPC stabilises.
|
||||
*/
|
||||
async function defaultListModels(): Promise<string[]> {
|
||||
// Real ephemeral Pi spawn left for a follow-up task.
|
||||
// Returning [] here triggers the graceful fallback seen by clients.
|
||||
return [];
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import type { AppConfig } from "../config.js";
|
||||
import { loadSettings, saveSettings, type Settings } from "../settings/settings-store.js";
|
||||
import { listWorkspaces, type ListModelsFn } from "./meta.js";
|
||||
|
||||
/** Merge stored settings over env/first-workspace defaults. */
|
||||
export function effectiveSettings(cfg: AppConfig, stored: Settings): Settings {
|
||||
const workspaces = listWorkspaces(cfg.harnessDir);
|
||||
return {
|
||||
workspace: stored.workspace ?? (workspaces[0]?.name),
|
||||
provider: stored.provider ?? cfg.defaults.provider,
|
||||
model: stored.model ?? cfg.defaults.model,
|
||||
thinking: stored.thinking ?? cfg.defaults.thinking,
|
||||
};
|
||||
}
|
||||
|
||||
export function settingsRoutes(
|
||||
app: FastifyInstance,
|
||||
deps: { cfg: AppConfig; listModels: ListModelsFn },
|
||||
): void {
|
||||
app.get("/settings", async () => {
|
||||
return effectiveSettings(deps.cfg, loadSettings(deps.cfg));
|
||||
});
|
||||
|
||||
app.put("/settings", async (req, reply) => {
|
||||
const b = (req.body ?? {}) as Settings;
|
||||
if (b.model) {
|
||||
let available: { 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}` });
|
||||
}
|
||||
}
|
||||
const next: Settings = {
|
||||
workspace: b.workspace,
|
||||
provider: b.provider,
|
||||
model: b.model,
|
||||
thinking: b.thinking,
|
||||
};
|
||||
saveSettings(deps.cfg, next);
|
||||
return effectiveSettings(deps.cfg, next);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user