sidebar turn-state: instant lh:turn-state propagation, poll demoted to backstop

This commit is contained in:
amosblomqvist
2026-10-02 03:05:48 +03:00
parent 2a1bac89ed
commit a927884ced
2 changed files with 48 additions and 0 deletions
+18
View File
@@ -61,6 +61,24 @@ export function useChatSession({ chatId, initialMessages, initiallyInFlight }: U
// The view + state, derived per model change from the model.
const state = useMemo<SessionState>(() => model.getState(), [model, version] as const);
// THE TURN-STATE BROADCAST (owner, sidebar round 2026-10-02): the same
// optimistic machine, propagated — the instant a turn starts locally
// (submitted: send, answer, session start, reattach, wake attach) the
// sidebar learns it without waiting for its 2.5s poll; at settle the
// event clears the row and triggers one immediate refetch. The model
// stays pure; this adapter owns the window seam (the lh:chat-renamed
// pattern).
const prevStatusRef = useRef(state.status);
useEffect(() => {
const was = prevStatusRef.current;
prevStatusRef.current = state.status;
if (was === state.status) return;
const running = state.status === "submitted" || state.status === "streaming";
const wasRunning = was === "submitted" || was === "streaming";
if (running === wasRunning) return; // submitted→streaming: still running; error↔ready: not
window.dispatchEvent(new CustomEvent("lh:turn-state", { detail: { id: chatId, running } }));
}, [state.status, chatId]);
// The reattach settled terminal → the sync-and-continue seam (chat.tsx).
const setReattachSettled = useCallback(
(fn: () => void) => {