From 068c6f197db0639cecbe4f3abf51f6d13f5bf5ec Mon Sep 17 00:00:00 2001 From: mptyl Date: Mon, 29 Jun 2026 21:39:02 +0200 Subject: [PATCH] feat(frontend): store lastUserEntry + stepMessages; record user input/choice --- frontend/src/shell/SteerInput.tsx | 3 +++ frontend/src/shell/WidgetHost.tsx | 5 ++++ frontend/src/store/sessionStore.test.ts | 32 +++++++++++++++++++++++-- frontend/src/store/sessionStore.ts | 8 ++++++- 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/frontend/src/shell/SteerInput.tsx b/frontend/src/shell/SteerInput.tsx index 6913f0b3..2abda292 100644 --- a/frontend/src/shell/SteerInput.tsx +++ b/frontend/src/shell/SteerInput.tsx @@ -6,6 +6,7 @@ import { postSteer, createSession } from "../api/sessions"; import { getSettings, putSettings, type Settings } from "../api/settings"; import { listWorkspaces } from "../api/workspaces"; import { listModels } from "../api/models"; +import { useSessionStore } from "../store/sessionStore"; const THINKING_LEVELS = ["low", "medium", "high"] as const; @@ -23,6 +24,7 @@ interface Props { export function SteerInput({ sessionId, onSessionCreated, onStop, inputRef }: Props) { const [text, setText] = useState(""); const [busy, setBusy] = useState(false); + const setLastUserEntry = useSessionStore((s) => s.setLastUserEntry); async function submit() { const trimmed = text.trim(); @@ -35,6 +37,7 @@ export function SteerInput({ sessionId, onSessionCreated, onStop, inputRef }: Pr const { id } = await createSession({ question: trimmed }); onSessionCreated?.(id); } + setLastUserEntry({ kind: "input", text: trimmed }); setText(""); } finally { setBusy(false); diff --git a/frontend/src/shell/WidgetHost.tsx b/frontend/src/shell/WidgetHost.tsx index ca9f4c8c..8ea978c2 100644 --- a/frontend/src/shell/WidgetHost.tsx +++ b/frontend/src/shell/WidgetHost.tsx @@ -7,6 +7,7 @@ export function WidgetHost({ sessionId }: { sessionId: string | null }) { const pending = useSessionStore((s) => s.pendingWidget); const clearPending = useSessionStore((s) => s.clearPending); const pushToast = useSessionStore((s) => s.pushToast); + const setLastUserEntry = useSessionStore((s) => s.setLastUserEntry); if (!pending) return null; const Renderer = resolve(pending.widget); const onRespond = async (r: UiResponse) => { @@ -25,6 +26,10 @@ export function WidgetHost({ sessionId }: { sessionId: string | null }) { return; } } + setLastUserEntry({ + kind: "choice", + text: r.text ?? r.choices?.join(", ") ?? r.decision?.type ?? r.control ?? "(choice)", + }); clearPending(); }; return ; diff --git a/frontend/src/store/sessionStore.test.ts b/frontend/src/store/sessionStore.test.ts index 1a531a2c..20ed144b 100644 --- a/frontend/src/store/sessionStore.test.ts +++ b/frontend/src/store/sessionStore.test.ts @@ -1,3 +1,4 @@ +import { test, expect, beforeEach } from "vitest"; import { useSessionStore } from "./sessionStore"; beforeEach(() => useSessionStore.getState().resetSession()); @@ -14,9 +15,9 @@ test("text_delta accumulates into transcript", () => { expect(useSessionStore.getState().transcript.at(-1)?.text).toBe("Analisi"); }); -test("info pushes a toast", () => { +test("info appends to stepMessages", () => { useSessionStore.getState().applyEvent({ type: "info", level: "warning", text: "attenzione" }); - expect(useSessionStore.getState().toasts.at(-1)).toEqual({ level: "warning", text: "attenzione" }); + expect(useSessionStore.getState().stepMessages.at(-1)).toEqual({ level: "warning", text: "attenzione" }); }); test("clearPending removes the widget", () => { @@ -42,3 +43,30 @@ test("pushToast appends an error toast", () => { useSessionStore.getState().pushToast({ level: "error", text: "boom" }); expect(useSessionStore.getState().toasts.at(-1)).toEqual({ level: "error", text: "boom" }); }); + +test("info events accumulate in stepMessages", () => { + const { applyEvent } = useSessionStore.getState(); + applyEvent({ type: "info", level: "info", text: "found 3 tables" }); + applyEvent({ type: "info", level: "warning", text: "ambiguous term" }); + expect(useSessionStore.getState().stepMessages).toEqual([ + { level: "info", text: "found 3 tables" }, + { level: "warning", text: "ambiguous term" }, + ]); +}); + +test("setLastUserEntry records the entry and clears stepMessages", () => { + const st = useSessionStore.getState(); + st.applyEvent({ type: "info", level: "info", text: "x" }); + st.setLastUserEntry({ kind: "input", text: "my question" }); + expect(useSessionStore.getState().lastUserEntry).toEqual({ kind: "input", text: "my question" }); + expect(useSessionStore.getState().stepMessages).toEqual([]); +}); + +test("resetSession clears lastUserEntry and stepMessages", () => { + const st = useSessionStore.getState(); + st.setLastUserEntry({ kind: "choice", text: "promote" }); + st.applyEvent({ type: "info", level: "info", text: "y" }); + st.resetSession(); + expect(useSessionStore.getState().lastUserEntry).toBeNull(); + expect(useSessionStore.getState().stepMessages).toEqual([]); +}); diff --git a/frontend/src/store/sessionStore.ts b/frontend/src/store/sessionStore.ts index 2a916ac4..124d1901 100644 --- a/frontend/src/store/sessionStore.ts +++ b/frontend/src/store/sessionStore.ts @@ -10,6 +10,8 @@ interface SessionState { pendingWidget: WidgetDescriptor | null; transcript: Entry[]; toasts: { level: string; text: string }[]; + stepMessages: { level: string; text: string }[]; + lastUserEntry: { kind: "input" | "choice"; text: string } | null; lastSystemEvent: StreamEvent | null; currentPhase: string | null; applyEvent: (e: StreamEvent) => void; @@ -17,12 +19,15 @@ interface SessionState { resetSession: () => void; setPhase: (phase: string | null) => void; pushToast: (toast: { level: string; text: string }) => void; + setLastUserEntry: (e: { kind: "input" | "choice"; text: string }) => void; } const empty = { pendingWidget: null, transcript: [] as Entry[], toasts: [] as { level: string; text: string }[], + stepMessages: [] as { level: string; text: string }[], + lastUserEntry: null as { kind: "input" | "choice"; text: string } | null, lastSystemEvent: null, currentPhase: null as string | null, }; @@ -45,7 +50,7 @@ export const useSessionStore = create((set) => ({ else t.push({ role: "assistant", text: e.text }); return { transcript: t }; } - if (e.type === "info") return { toasts: [...st.toasts, { level: e.level ?? "info", text: e.text }] }; + if (e.type === "info") return { stepMessages: [...st.stepMessages, { level: e.level ?? "info", text: e.text }] }; if (e.type === "system_event") return { lastSystemEvent: e }; return {}; }), @@ -53,4 +58,5 @@ export const useSessionStore = create((set) => ({ resetSession: () => set({ ...empty }), setPhase: (phase) => set({ currentPhase: phase }), pushToast: (toast) => set((st) => ({ toasts: [...st.toasts, toast] })), + setLastUserEntry: (e) => set({ lastUserEntry: e, stepMessages: [] }), }));