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
+2
View File
@@ -11,6 +11,8 @@ export * from "./commit-positions.ts";
export * from "./jobs.ts";
export * from "./stores.ts";
export { readRenderSettings, writeRenderSettings } from "../store/render-settings.ts";
export { readDraftSettings, writeDraftSettings } from "../store/draft-settings.ts";
export * from "./settings-body.ts";
export * from "./turn-liveness.ts";
export * from "./observability.ts";
export * from "./model-catalog.ts";
+65
View File
@@ -0,0 +1,65 @@
/**
* The shared settings-PUT body parser — the one seam both settings routes
* use (per-chat: app/api/chats/[id]/settings; pre-session draft:
* app/api/settings/draft). Extracted verbatim from the per-chat route
* (draft-settings wave, 2026-09-18): identical parse, identical errors.
*
* Replace-whole semantics: the client sends the complete desired object;
* absent fields are absent overrides ({} clears everything). No merges, so
* a stale client can never resurrect a cleared override.
*/
import type { ThinkingLevel } from "@earendil-works/pi-agent-core";
import { GRAPH_CONTEXT_TOKENS_MAX, type ChatSettings } from "../core";
/** Parsed-or-rejected — `ok: false` carries the 400's error message. */
export type SettingsBodyResult =
| { ok: true; settings: ChatSettings }
| { ok: false; error: string };
export function parseSettingsBody(body: unknown): SettingsBodyResult {
if (!body || typeof body !== "object" || Array.isArray(body)) {
return { ok: false, error: "Invalid JSON body" };
}
const raw = body as {
model?: { provider?: unknown; id?: unknown };
thinkingLevel?: unknown;
graphContextTokens?: unknown;
};
const settings: ChatSettings = {};
if (raw.model !== undefined) {
if (
typeof raw.model !== "object" ||
raw.model === null ||
typeof raw.model.provider !== "string" ||
typeof raw.model.id !== "string"
) {
return { ok: false, error: "model must be {provider, id}" };
}
settings.model = { provider: raw.model.provider, id: raw.model.id };
}
if (raw.thinkingLevel !== undefined) {
if (typeof raw.thinkingLevel !== "string") {
return { ok: false, error: "thinkingLevel must be a string" };
}
settings.thinkingLevel = raw.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 (raw.graphContextTokens !== undefined) {
if (
typeof raw.graphContextTokens !== "number" ||
!Number.isInteger(raw.graphContextTokens) ||
raw.graphContextTokens < 0 ||
raw.graphContextTokens > GRAPH_CONTEXT_TOKENS_MAX
) {
return {
ok: false,
error: `graphContextTokens must be an integer between 0 and ${GRAPH_CONTEXT_TOKENS_MAX}`,
};
}
settings.graphContextTokens = raw.graphContextTokens;
}
return { ok: true, settings };
}