feat: draft settings persistence — shared model/thinking/context record for the new-session surfaces

Both new-session pages (the home graph view's topic dock and /chat/new)
held their model + thinking + knowledge-context choices in plain
useState(undefined) — every navigation reset them to config defaults.

Now both surfaces read/write ONE server-side record
(data/draft-settings.json via GET/PUT /api/settings/draft, the
render-settings pattern): whatever the learner last left the controls on
is what every later visit shows. useDraftSettings loads on mount with a
touch-guard (a slow GET never clobbers a fast choice), saves
optimistically. Session creation still carries the choice into the new
chat exactly as before; per-session settings are untouched.

The PUT validation is the exact per-chat contract — parseSettingsBody
extracted verbatim from app/api/chats/[id]/settings and shared by both
routes (unknown model/level → 400 with the supported list, never a
silent clamp). 400s on garbage, {} = the clear-overrides write.

Live-verified: Cmd+K+Enter model pick, Shift+Tab thinking cycle, context
slider — all held across home → session → home → /chat/new, and carried
into a created session's record.
This commit is contained in:
amosblomqvist
2026-09-18 09:25:40 +03:00
parent 597b3f43e1
commit a94a108e74
10 changed files with 359 additions and 59 deletions
+11 -47
View File
@@ -1,6 +1,5 @@
import { getChat, validateChatSettings, stores } from "@/src/server";
import type { ThinkingLevel } from "@earendil-works/pi-agent-core";
import { GRAPH_CONTEXT_TOKENS_MAX, type ChatSettings } from "@/src/core";
import { getChat, validateChatSettings, parseSettingsBody, stores } from "@/src/server";
import type { ChatSettings } from "@/src/core";
/**
* The per-chat model control (model-control wave): replace-whole settings —
@@ -10,60 +9,25 @@ import { GRAPH_CONTEXT_TOKENS_MAX, type ChatSettings } from "@/src/core";
* thinking level is a 400 carrying the supported list, never a silent clamp
* — the user must see what the model actually accepts. Effective next turn
* by construction (agents are stateless per turn).
*
* The body parsing itself is shared with the draft route
* (src/server/settings-body.ts, draft-settings wave 2026-09-18).
*/
export async function PUT(req: Request, { params }: { params: Promise<{ id: string }> }) {
const { id } = await params;
if (!getChat(id)) return Response.json({ error: "Not found" }, { status: 404 });
const body = (await req.json().catch(() => null)) as
| { model?: { provider?: unknown; id?: unknown }; thinkingLevel?: unknown; graphContextTokens?: unknown }
| null;
if (!body || typeof body !== "object") {
return Response.json({ error: "Invalid JSON body" }, { status: 400 });
}
const settings: ChatSettings = {};
if (body.model !== undefined) {
if (
typeof body.model !== "object" ||
body.model === null ||
typeof body.model.provider !== "string" ||
typeof body.model.id !== "string"
) {
return Response.json({ error: "model must be {provider, id}" }, { status: 400 });
}
settings.model = { provider: body.model.provider, id: body.model.id };
}
if (body.thinkingLevel !== undefined) {
if (typeof body.thinkingLevel !== "string") {
return Response.json({ error: "thinkingLevel must be a string" }, { status: 400 });
}
settings.thinkingLevel = body.thinkingLevel as ThinkingLevel;
}
// Graph context injection budget (vision-context-injection.md): integer
// tokens, 0 (off) to GRAPH_CONTEXT_TOKENS_MAX, no silent clamping — the
// slider is the only writer, and it only ever sends multiples of the step.
if (body.graphContextTokens !== undefined) {
if (
typeof body.graphContextTokens !== "number" ||
!Number.isInteger(body.graphContextTokens) ||
body.graphContextTokens < 0 ||
body.graphContextTokens > GRAPH_CONTEXT_TOKENS_MAX
) {
return Response.json(
{ error: `graphContextTokens must be an integer between 0 and ${GRAPH_CONTEXT_TOKENS_MAX}` },
{ status: 400 },
);
}
settings.graphContextTokens = body.graphContextTokens;
}
const body = await req.json().catch(() => null);
const parsed = parseSettingsBody(body);
if (!parsed.ok) return Response.json({ error: parsed.error }, { status: 400 });
const verdict = validateChatSettings(settings);
const verdict = validateChatSettings(parsed.settings);
if (!verdict.ok) {
return Response.json({ error: verdict.error, supported: verdict.supported }, { status: 400 });
}
// Replace-whole persists the validated object verbatim — no merges, so a
// stale client can never resurrect a cleared override.
stores.chatStore.saveSettings(id, settings);
return Response.json({ settings });
stores.chatStore.saveSettings(id, parsed.settings);
return Response.json({ settings: parsed.settings satisfies ChatSettings });
}
+39
View File
@@ -0,0 +1,39 @@
/**
* GET/PUT /api/settings/draft — server persistence for the DRAFT (pre-
* session) settings: the model + thinking-level + knowledge-context
* selections made on the new-session surfaces (the home graph view's topic
* dock and /chat/new). Both surfaces share ONE record (owner, 2026-09-18):
* whatever the learner last left the controls on is what the next visit
* shows. The file lives at data/draft-settings.json
* (config.DRAFT_SETTINGS_FILE).
*
* GET → 200 {settings} once saved, 200 null on first boot (the surfaces
* keep config-pinned defaults until the learner chooses something).
* PUT → 200 {settings} — the VALIDATED copy persisted verbatim. The exact
* per-chat contract (parseSettingsBody + the catalog gate): unknown
* provider/model or an unsupported thinking level is a 400 carrying
* the supported list, never a silent clamp.
*
* Per-session settings (PUT /api/chats/:id/settings) are a separate record
* untouched by this — the draft choice still rides along on session
* creation exactly as before.
*/
import { readDraftSettings, writeDraftSettings, parseSettingsBody, validateChatSettings } from "@/src/server";
export const dynamic = "force-dynamic";
export async function GET() {
return Response.json(readDraftSettings());
}
export async function PUT(request: Request) {
const body = await request.json().catch(() => null);
const parsed = parseSettingsBody(body);
if (!parsed.ok) return Response.json({ error: parsed.error }, { status: 400 });
const verdict = validateChatSettings(parsed.settings);
if (!verdict.ok) {
return Response.json({ error: verdict.error, supported: verdict.supported }, { status: 400 });
}
writeDraftSettings(parsed.settings);
return Response.json({ settings: parsed.settings });
}