feat(frontend): store lastUserEntry + stepMessages; record user input/choice
This commit is contained in:
@@ -6,6 +6,7 @@ import { postSteer, createSession } from "../api/sessions";
|
|||||||
import { getSettings, putSettings, type Settings } from "../api/settings";
|
import { getSettings, putSettings, type Settings } from "../api/settings";
|
||||||
import { listWorkspaces } from "../api/workspaces";
|
import { listWorkspaces } from "../api/workspaces";
|
||||||
import { listModels } from "../api/models";
|
import { listModels } from "../api/models";
|
||||||
|
import { useSessionStore } from "../store/sessionStore";
|
||||||
|
|
||||||
const THINKING_LEVELS = ["low", "medium", "high"] as const;
|
const THINKING_LEVELS = ["low", "medium", "high"] as const;
|
||||||
|
|
||||||
@@ -23,6 +24,7 @@ interface Props {
|
|||||||
export function SteerInput({ sessionId, onSessionCreated, onStop, inputRef }: Props) {
|
export function SteerInput({ sessionId, onSessionCreated, onStop, inputRef }: Props) {
|
||||||
const [text, setText] = useState("");
|
const [text, setText] = useState("");
|
||||||
const [busy, setBusy] = useState(false);
|
const [busy, setBusy] = useState(false);
|
||||||
|
const setLastUserEntry = useSessionStore((s) => s.setLastUserEntry);
|
||||||
|
|
||||||
async function submit() {
|
async function submit() {
|
||||||
const trimmed = text.trim();
|
const trimmed = text.trim();
|
||||||
@@ -35,6 +37,7 @@ export function SteerInput({ sessionId, onSessionCreated, onStop, inputRef }: Pr
|
|||||||
const { id } = await createSession({ question: trimmed });
|
const { id } = await createSession({ question: trimmed });
|
||||||
onSessionCreated?.(id);
|
onSessionCreated?.(id);
|
||||||
}
|
}
|
||||||
|
setLastUserEntry({ kind: "input", text: trimmed });
|
||||||
setText("");
|
setText("");
|
||||||
} finally {
|
} finally {
|
||||||
setBusy(false);
|
setBusy(false);
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ export function WidgetHost({ sessionId }: { sessionId: string | null }) {
|
|||||||
const pending = useSessionStore((s) => s.pendingWidget);
|
const pending = useSessionStore((s) => s.pendingWidget);
|
||||||
const clearPending = useSessionStore((s) => s.clearPending);
|
const clearPending = useSessionStore((s) => s.clearPending);
|
||||||
const pushToast = useSessionStore((s) => s.pushToast);
|
const pushToast = useSessionStore((s) => s.pushToast);
|
||||||
|
const setLastUserEntry = useSessionStore((s) => s.setLastUserEntry);
|
||||||
if (!pending) return null;
|
if (!pending) return null;
|
||||||
const Renderer = resolve(pending.widget);
|
const Renderer = resolve(pending.widget);
|
||||||
const onRespond = async (r: UiResponse) => {
|
const onRespond = async (r: UiResponse) => {
|
||||||
@@ -25,6 +26,10 @@ export function WidgetHost({ sessionId }: { sessionId: string | null }) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
setLastUserEntry({
|
||||||
|
kind: "choice",
|
||||||
|
text: r.text ?? r.choices?.join(", ") ?? r.decision?.type ?? r.control ?? "(choice)",
|
||||||
|
});
|
||||||
clearPending();
|
clearPending();
|
||||||
};
|
};
|
||||||
return <Renderer descriptor={pending} onRespond={onRespond} />;
|
return <Renderer descriptor={pending} onRespond={onRespond} />;
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { test, expect, beforeEach } from "vitest";
|
||||||
import { useSessionStore } from "./sessionStore";
|
import { useSessionStore } from "./sessionStore";
|
||||||
|
|
||||||
beforeEach(() => useSessionStore.getState().resetSession());
|
beforeEach(() => useSessionStore.getState().resetSession());
|
||||||
@@ -14,9 +15,9 @@ test("text_delta accumulates into transcript", () => {
|
|||||||
expect(useSessionStore.getState().transcript.at(-1)?.text).toBe("Analisi");
|
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" });
|
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", () => {
|
test("clearPending removes the widget", () => {
|
||||||
@@ -42,3 +43,30 @@ test("pushToast appends an error toast", () => {
|
|||||||
useSessionStore.getState().pushToast({ level: "error", text: "boom" });
|
useSessionStore.getState().pushToast({ level: "error", text: "boom" });
|
||||||
expect(useSessionStore.getState().toasts.at(-1)).toEqual({ 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;
|
pendingWidget: WidgetDescriptor | null;
|
||||||
transcript: Entry[];
|
transcript: Entry[];
|
||||||
toasts: { level: string; text: string }[];
|
toasts: { level: string; text: string }[];
|
||||||
|
stepMessages: { level: string; text: string }[];
|
||||||
|
lastUserEntry: { kind: "input" | "choice"; text: string } | null;
|
||||||
lastSystemEvent: StreamEvent | null;
|
lastSystemEvent: StreamEvent | null;
|
||||||
currentPhase: string | null;
|
currentPhase: string | null;
|
||||||
applyEvent: (e: StreamEvent) => void;
|
applyEvent: (e: StreamEvent) => void;
|
||||||
@@ -17,12 +19,15 @@ interface SessionState {
|
|||||||
resetSession: () => void;
|
resetSession: () => void;
|
||||||
setPhase: (phase: string | null) => void;
|
setPhase: (phase: string | null) => void;
|
||||||
pushToast: (toast: { level: string; text: string }) => void;
|
pushToast: (toast: { level: string; text: string }) => void;
|
||||||
|
setLastUserEntry: (e: { kind: "input" | "choice"; text: string }) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const empty = {
|
const empty = {
|
||||||
pendingWidget: null,
|
pendingWidget: null,
|
||||||
transcript: [] as Entry[],
|
transcript: [] as Entry[],
|
||||||
toasts: [] as { level: string; text: string }[],
|
toasts: [] as { level: string; text: string }[],
|
||||||
|
stepMessages: [] as { level: string; text: string }[],
|
||||||
|
lastUserEntry: null as { kind: "input" | "choice"; text: string } | null,
|
||||||
lastSystemEvent: null,
|
lastSystemEvent: null,
|
||||||
currentPhase: null as string | null,
|
currentPhase: null as string | null,
|
||||||
};
|
};
|
||||||
@@ -45,7 +50,7 @@ export const useSessionStore = create<SessionState>((set) => ({
|
|||||||
else t.push({ role: "assistant", text: e.text });
|
else t.push({ role: "assistant", text: e.text });
|
||||||
return { transcript: t };
|
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 };
|
if (e.type === "system_event") return { lastSystemEvent: e };
|
||||||
return {};
|
return {};
|
||||||
}),
|
}),
|
||||||
@@ -53,4 +58,5 @@ export const useSessionStore = create<SessionState>((set) => ({
|
|||||||
resetSession: () => set({ ...empty }),
|
resetSession: () => set({ ...empty }),
|
||||||
setPhase: (phase) => set({ currentPhase: phase }),
|
setPhase: (phase) => set({ currentPhase: phase }),
|
||||||
pushToast: (toast) => set((st) => ({ toasts: [...st.toasts, toast] })),
|
pushToast: (toast) => set((st) => ({ toasts: [...st.toasts, toast] })),
|
||||||
|
setLastUserEntry: (e) => set({ lastUserEntry: e, stepMessages: [] }),
|
||||||
}));
|
}));
|
||||||
|
|||||||
Reference in New Issue
Block a user