YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
fix(chat): user message disappears after switching to streaming chat
Louis Beaumont1858434bacc5
diff --git a/apps/screenpipe-app-tauri/components/standalone-chat.tsx b/apps/screenpipe-app-tauri/components/standalone-chat.tsxindex 42f1cd286..7181c7c21 100644--- a/apps/screenpipe-app-tauri/components/standalone-chat.tsx+++ b/apps/screenpipe-app-tauri/components/standalone-chat.tsx@@ -33,6 +33,7 @@ import { writeTextFile, readFile } from "@tauri-apps/plugin-fs";import { commands } from "@/lib/utils/tauri";import { emit } from "@tauri-apps/api/event";import { useChatConversations } from "@/components/hooks/use-chat-conversations";+import { useChatStore } from "@/lib/stores/chat-store";import { getCurrentWindow } from "@tauri-apps/api/window";import { getCurrentWebview } from "@tauri-apps/api/webview";import { usePlatform } from "@/lib/hooks/use-platform";@@ -3148,6 +3149,51 @@ export function StandaloneChat({setIsLoading(true);setIsStreaming(true);+ // Mirror the user message + assistant placeholder DIRECTLY into the+ // chat-store, synchronously. The snapshot-on-switch path reads+ // `messages` from the React closure, which lags behind setMessages+ // by one render cycle (React batches). If the user clicks "+ new+ // chat" in that gap, the snapshot writes stale messages (without+ // the freshly-sent user message) to the store. Then the router+ // takes over for the now-backgrounded session and only knows about+ // assistant deltas — the user comes back and sees the assistant+ // reply with no preceding user message. By writing both messages+ // here, the store is at least as fresh as the panel and survives+ // any closure staleness.+ const sidNow = piSessionIdRef.current;+ if (sidNow) {+ const storeState = useChatStore.getState();+ if (!storeState.sessions[sidNow]) {+ storeState.actions.upsert({+ id: sidNow,+ title: "new chat",+ preview: "",+ status: "streaming",+ messageCount: 0,+ createdAt: Date.now(),+ updatedAt: Date.now(),+ pinned: false,+ unread: false,+ });+ }+ storeState.actions.appendMessage(sidNow, newUserMessage as any);+ storeState.actions.appendMessage(sidNow, {+ id: assistantMessageId,+ role: "assistant",+ content: "Processing...",+ timestamp: Date.now(),+ model: activePreset?.model,+ provider: activePreset?.provider,+ } as any);+ storeState.actions.setStreaming(sidNow, {+ streamingMessageId: assistantMessageId,+ streamingText: "",+ contentBlocks: [],+ isStreaming: true,+ isLoading: true,+ });+ }+posthog.capture("chat_message_sent", {provider: activePreset?.provider,model: activePreset?.model,diff --git a/apps/screenpipe-app-tauri/lib/__tests__/chat-store.test.ts b/apps/screenpipe-app-tauri/lib/__tests__/chat-store.test.tsindex 3fefe1619..a03619798 100644--- a/apps/screenpipe-app-tauri/lib/__tests__/chat-store.test.ts+++ b/apps/screenpipe-app-tauri/lib/__tests__/chat-store.test.ts@@ -74,6 +74,39 @@ describe("chat-store: snapshotSession", () => {expect(useChatStore.getState().sessions.unknown).toBeUndefined();});+ it("is non-destructive when the snapshot's messages are stale (shorter than store)", () => {+ // Bug: snapshot reads `messages` from a React closure that lags+ // setMessages by one render. If sendPiMessage just queued+ // [userMsg, assistant] and the user clicks "+ new chat" before+ // React commits, the snapshot fires with the OLD closure (length 0).+ // sendPiMessage also writes the new messages to the store directly.+ // The snapshot must NOT regress that: take the longer array.+ useChatStore.getState().actions.upsert(baseRecord({ id: "A" }));+ const fresh = [+ { id: "u1", role: "user", content: "hello", timestamp: 1 },+ { id: "a1", role: "assistant", content: "Processing...", timestamp: 2 },+ ];+ useChatStore.getState().actions.snapshotSession("A", {+ messages: fresh, // direct write (mirrors sendPiMessage)+ streamingText: "",+ streamingMessageId: "a1",+ contentBlocks: [],+ isStreaming: true,+ isLoading: true,+ });+ // Now a stale snapshot arrives (closure captured pre-send messages).+ useChatStore.getState().actions.snapshotSession("A", {+ messages: [], // stale+ streamingText: "",+ streamingMessageId: "a1",+ contentBlocks: [],+ isStreaming: true,+ isLoading: true,+ });+ // Store keeps the freshest (longer) view.+ expect(useChatStore.getState().sessions.A.messages).toEqual(fresh);+ });+it("does NOT bump createdAt — snapshot is plumbing, not new activity", () => {// Bug: clicking a chat reordered the sidebar because the snapshot's// updatedAt bump bubbled up to the sort. Now sort is by createdAtdiff --git a/apps/screenpipe-app-tauri/lib/stores/chat-store.ts b/apps/screenpipe-app-tauri/lib/stores/chat-store.tsindex 905473ac6..753c32c4e 100644--- a/apps/screenpipe-app-tauri/lib/stores/chat-store.ts+++ b/apps/screenpipe-app-tauri/lib/stores/chat-store.ts@@ -438,13 +438,25 @@ export const useChatStore = create<ChatStore>((set) => ({set((s) => {const existing = s.sessions[id];if (!existing) return {};+ // Non-destructive: if the snapshot's messages array is shorter+ // than what the store already has, keep the store's. The+ // snapshot reads `messages` from a React closure which can be+ // stale (one render cycle behind setMessages from sendPiMessage),+ // so a fast switch right after send would otherwise wipe the+ // user message we just appended to the store directly. Take+ // whichever array is longer — both should converge to the same+ // tail, the longer one just has fewer dropped writes.+ const existingMsgs = (existing.messages as unknown[]) ?? [];+ const incomingMsgs = snapshot.messages ?? [];+ const messages =+ incomingMsgs.length >= existingMsgs.length ? incomingMsgs : existingMsgs;return {sessions: {...s.sessions,[id]: {...existing,- messages: snapshot.messages,- messageCount: snapshot.messages.length,+ messages,+ messageCount: messages.length,streamingText: snapshot.streamingText,streamingMessageId: snapshot.streamingMessageId,contentBlocks: snapshot.contentBlocks,diff --git a/apps/screenpipe-app-tauri/src-tauri/Cargo.toml b/apps/screenpipe-app-tauri/src-tauri/Cargo.tomlindex 6e0f0e25a..36c3623f8 100644--- a/apps/screenpipe-app-tauri/src-tauri/Cargo.toml+++ b/apps/screenpipe-app-tauri/src-tauri/Cargo.toml@@ -1,6 +1,6 @@[package]name = "screenpipe-app"-version = "2.4.68"+version = "2.4.69"description = "24/7 memory for your desktop. 100% local."authors = ["you"]license = ""