feat(frontend): store lastUserEntry + stepMessages; record user input/choice

This commit is contained in:
2026-06-29 21:39:02 +02:00
parent 3a97ab3b2f
commit 068c6f197d
4 changed files with 45 additions and 3 deletions
+3
View File
@@ -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);
+5
View File
@@ -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 <Renderer descriptor={pending} onRespond={onRespond} />;
+30 -2
View File
@@ -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([]);
});
+7 -1
View File
@@ -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<SessionState>((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<SessionState>((set) => ({
resetSession: () => set({ ...empty }),
setPhase: (phase) => set({ currentPhase: phase }),
pushToast: (toast) => set((st) => ({ toasts: [...st.toasts, toast] })),
setLastUserEntry: (e) => set({ lastUserEntry: e, stepMessages: [] }),
}));