- sessionStore.applyEvent: on ui_request set currentPhase normalized from the descriptor's phase to its short id (e.g. "F4_schema_linking" -> "F4"), so WorkflowBar actually highlights the active phase; falls back to the existing phase when the descriptor has none. setPhase kept for resume/getSession. - Add pushToast store action; WidgetHost wraps postResponse in try/catch, pushes an error toast and keeps the widget pending on failure (clearPending only on success) so the user can retry. - Tests: store currentPhase normalization + no-phase passthrough + pushToast; new WorkflowBar.test.tsx asserting the matching phase is highlighted. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
57 lines
1.8 KiB
TypeScript
57 lines
1.8 KiB
TypeScript
import { create } from "zustand";
|
|
import type { StreamEvent, WidgetDescriptor } from "../api/types";
|
|
|
|
interface Entry {
|
|
role: "assistant";
|
|
text: string;
|
|
}
|
|
|
|
interface SessionState {
|
|
pendingWidget: WidgetDescriptor | null;
|
|
transcript: Entry[];
|
|
toasts: { level: string; text: string }[];
|
|
lastSystemEvent: StreamEvent | null;
|
|
currentPhase: string | null;
|
|
applyEvent: (e: StreamEvent) => void;
|
|
clearPending: () => void;
|
|
resetSession: () => void;
|
|
setPhase: (phase: string | null) => void;
|
|
pushToast: (toast: { level: string; text: string }) => void;
|
|
}
|
|
|
|
const empty = {
|
|
pendingWidget: null,
|
|
transcript: [] as Entry[],
|
|
toasts: [] as { level: string; text: string }[],
|
|
lastSystemEvent: null,
|
|
currentPhase: null as string | null,
|
|
};
|
|
|
|
export const useSessionStore = create<SessionState>((set) => ({
|
|
...empty,
|
|
applyEvent: (e) =>
|
|
set((st) => {
|
|
if (e.type === "ui_request")
|
|
return {
|
|
pendingWidget: e.ui_request,
|
|
currentPhase: e.ui_request.phase
|
|
? e.ui_request.phase.split("_")[0]
|
|
: st.currentPhase,
|
|
};
|
|
if (e.type === "text_delta") {
|
|
const t = [...st.transcript];
|
|
const last = t.at(-1);
|
|
if (last && !st.pendingWidget) t[t.length - 1] = { role: "assistant", text: last.text + e.text };
|
|
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 === "system_event") return { lastSystemEvent: e };
|
|
return {};
|
|
}),
|
|
clearPending: () => set({ pendingWidget: null }),
|
|
resetSession: () => set({ ...empty }),
|
|
setPhase: (phase) => set({ currentPhase: phase }),
|
|
pushToast: (toast) => set((st) => ({ toasts: [...st.toasts, toast] })),
|
|
}));
|