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:
@@ -0,0 +1,62 @@
|
||||
"use client";
|
||||
|
||||
/**
|
||||
* The DRAFT (pre-session) settings hook — the shared persistence behind
|
||||
* the two new-session surfaces (the home graph view's topic dock and
|
||||
* /chat/new). The model + thinking + knowledge-context choices live in
|
||||
* ONE server-side record (GET/PUT /api/settings/draft →
|
||||
* data/draft-settings.json), so whatever the learner last left the
|
||||
* controls on is what every later visit shows (owner, draft-settings wave
|
||||
* 2026-09-18): navigating away and back keeps the feel. Per-session
|
||||
* settings are a different record (the chat's own) — untouched here.
|
||||
*
|
||||
* Contract: settings === undefined until the first GET resolves — the
|
||||
* controls render config-pinned defaults in that window, same as today.
|
||||
* The GET is touch-guarded: if the learner changes something before the
|
||||
* load lands, the loaded copy is dropped, never clobbering the choice.
|
||||
* Saves are optimistic (the controls only ever send valid values — the
|
||||
* picker offers catalog models, the slider sends its stops) and
|
||||
* fire-and-forget; a failed write logs, and the next mount falls back to
|
||||
* the persisted truth.
|
||||
*/
|
||||
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import type { ChatSettings } from "@/src/core";
|
||||
|
||||
export function useDraftSettings(): {
|
||||
settings: ChatSettings | undefined;
|
||||
save: (settings: ChatSettings) => void;
|
||||
} {
|
||||
const [settings, setSettings] = useState<ChatSettings | undefined>(undefined);
|
||||
const touchedRef = useRef(false);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
fetch("/api/settings/draft")
|
||||
.then((res) => (res.ok ? (res.json() as Promise<ChatSettings | null>) : null))
|
||||
.then((loaded) => {
|
||||
// Never clobber a choice made while the load was in flight.
|
||||
if (!cancelled && loaded !== null && !touchedRef.current) setSettings(loaded);
|
||||
})
|
||||
.catch(() => {
|
||||
/* transient — defaults stand, the next change rewrites the record */
|
||||
});
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, []);
|
||||
|
||||
const save = useCallback((next: ChatSettings) => {
|
||||
touchedRef.current = true;
|
||||
setSettings(next);
|
||||
void fetch("/api/settings/draft", {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(next),
|
||||
}).catch((err: unknown) => {
|
||||
console.error("draft settings write failed:", err);
|
||||
});
|
||||
}, []);
|
||||
|
||||
return { settings, save };
|
||||
}
|
||||
+10
-8
@@ -5,6 +5,7 @@ import { useRouter } from "next/navigation";
|
||||
import { CornerDownLeftIcon } from "lucide-react";
|
||||
import { GraphView } from "@/components/graph-view";
|
||||
import { GraphContextControl, effectiveContextTokens } from "@/components/graph-context-control";
|
||||
import { useDraftSettings } from "@/components/draft-settings";
|
||||
import { DictationBar, useDictation } from "@/components/dictation";
|
||||
import { SidebarToggle } from "@/components/sidebar";
|
||||
import { StatsBar } from "@/components/stats-bar";
|
||||
@@ -25,10 +26,13 @@ export function Home() {
|
||||
append: (t) => setTopic((v) => (v ? v.replace(/\s+$/, "") + " " + t : t)),
|
||||
});
|
||||
const [busy, setBusy] = useState(false);
|
||||
// The model+thinking selection made pre-chat — lifted from the StatsBar's
|
||||
// ModelControl, carried to the chat POST then PUT-on-create (the model
|
||||
// picker defers its save when chatId is empty; see components/model-picker).
|
||||
const [settings, setSettings] = useState<ChatSettings | undefined>(undefined);
|
||||
// The model+thinking+context selection made pre-chat — the SHARED draft
|
||||
// record (draft-settings wave, 2026-09-18): both new-session surfaces
|
||||
// (this topic dock and /chat/new) read and write ONE persisted choice,
|
||||
// so what the learner last left the controls on is what shows on return.
|
||||
// Still carried to the chat POST then PUT-on-create (the model picker
|
||||
// defers its save when chatId is empty; see components/model-picker).
|
||||
const { settings, save: saveDraft } = useDraftSettings();
|
||||
|
||||
const start = async () => {
|
||||
const t = topic.trim();
|
||||
@@ -140,13 +144,11 @@ export function Home() {
|
||||
turns={[] as TurnUsage[]}
|
||||
chatId=""
|
||||
settings={settings}
|
||||
onSettingsSaved={setSettings}
|
||||
onSettingsSaved={saveDraft}
|
||||
leftSlot={
|
||||
<GraphContextControl
|
||||
value={effectiveContextTokens(settings)}
|
||||
onChange={(tokens) =>
|
||||
setSettings((s) => ({ ...s, graphContextTokens: tokens }))
|
||||
}
|
||||
onChange={(tokens) => saveDraft({ ...settings, graphContextTokens: tokens })}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
|
||||
@@ -6,6 +6,7 @@ import { Conversation, ConversationContent, ConversationEmptyState } from "@/com
|
||||
import { Composer } from "@/components/composer";
|
||||
import { DictationBar, useDictation } from "@/components/dictation";
|
||||
import { GraphContextControl, effectiveContextTokens } from "@/components/graph-context-control";
|
||||
import { useDraftSettings } from "@/components/draft-settings";
|
||||
import { PhaseChip } from "@/components/phase-divider";
|
||||
import { SidebarToggle } from "@/components/sidebar";
|
||||
import { StatsBar } from "@/components/stats-bar";
|
||||
@@ -150,9 +151,13 @@ export function NewSession() {
|
||||
const router = useRouter();
|
||||
const [text, setText] = useState("");
|
||||
const [busy, setBusy] = useState(false);
|
||||
// The pre-chat model+thinking selection, lifted from the StatsBar's
|
||||
// ModelControl (empty chatId → it never PUTs; it hands the choice up).
|
||||
const [settings, setSettings] = useState<ChatSettings | undefined>(undefined);
|
||||
// The pre-chat model+thinking+context selection — the SHARED draft
|
||||
// record (draft-settings wave, 2026-09-18): /chat/new and the home
|
||||
// topic dock read and write ONE persisted choice, so what the learner
|
||||
// last left the controls on is what shows on return. ModelControl with
|
||||
// an empty chatId never PUTs — it hands the choice up; it still rides
|
||||
// the session POST then PUT-on-create.
|
||||
const { settings, save: saveDraft } = useDraftSettings();
|
||||
|
||||
const send = useCallback(() => {
|
||||
if (!text.trim() || busy) return;
|
||||
@@ -172,7 +177,7 @@ export function NewSession() {
|
||||
onChange={setText}
|
||||
onSend={send}
|
||||
settings={settings}
|
||||
onSettingsSaved={setSettings}
|
||||
onSettingsSaved={saveDraft}
|
||||
/>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user