feat(frontend): store lastUserEntry + stepMessages; record user input/choice
This commit is contained in:
@@ -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([]);
|
||||
});
|
||||
|
||||
@@ -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: [] }),
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user