Skip to content

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

refactor(chat): source pipe-watch messages from store, drop mirror effect (#4896)

Ansh Grovera4a5f996c96e
diff --git a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-session-runtime.ts b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-session-runtime.ts
index 8e81ba293..9cdda5a23 100644
--- a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-session-runtime.ts
+++ b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-session-runtime.ts
@@ -17,7 +17,6 @@ import type { ContentBlock, Message } from "@/lib/chat/types";
interface UseChatSessionRuntimeOptions {
conversationId: string | null;
- setMessages: React.Dispatch<React.SetStateAction<Message[]>>;
setIsLoading: React.Dispatch<React.SetStateAction<boolean>>;
setIsStreaming: React.Dispatch<React.SetStateAction<boolean>>;
isLoading: boolean;
@@ -36,7 +35,6 @@ interface UseChatSessionRuntimeOptions {
export function useChatSessionRuntime({
conversationId,
- setMessages,
setIsLoading,
setIsStreaming,
isLoading,
@@ -97,15 +95,8 @@ export function useChatSessionRuntime({
};
}, [conversationId, handleAgentEventDataRef, piSessionIdRef]);
- const pipeWatchMessages = useChatStore((state) =>
- conversationId && state.sessions[conversationId]?.kind === "pipe-watch"
- ? state.sessions[conversationId]?.messages
- : undefined,
- );
- useEffect(() => {
- if (!pipeWatchMessages) return;
- setMessages(pipeWatchMessages as any);
- }, [pipeWatchMessages, setMessages]);
+ // Pipe-watch messages are now sourced directly from the store in the parent
+ // (standalone-chat.tsx) — no store→local mirror effect needed here.
const pipeWatchIsLoading = useChatStore((state) => {
if (!conversationId) return undefined;
diff --git a/apps/screenpipe-app-tauri/components/standalone-chat.tsx b/apps/screenpipe-app-tauri/components/standalone-chat.tsx
index 9aceed20a..33dcdb0d9 100644
--- a/apps/screenpipe-app-tauri/components/standalone-chat.tsx
+++ b/apps/screenpipe-app-tauri/components/standalone-chat.tsx
@@ -80,6 +80,7 @@ import {
usePipeGenerationCompletion,
} from "@/components/chat/standalone/hooks/use-chat-window-events";
import type { ContentBlock, Message } from "@/lib/chat/types";
+import { useChatStore } from "@/lib/stores/chat-store";
import { AGENT_TOPICS, type AgentEventEnvelope } from "@/lib/events/types";
// Session ID is per-conversation — set on mount (new conv) and updated on load/new.
@@ -190,7 +191,10 @@ export function StandaloneChat({
setInput,
inputRef,
});
- const [messages, setMessages] = useState<Message[]>([]);
+ // Local buffer for regular (agent) sessions. Pipe-watch sessions source
+ // their messages from the chat store instead — see the `messages` derivation
+ // below, after `conversationId` is known.
+ const [localMessages, setMessages] = useState<Message[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [isStreaming, setIsStreaming] = useState(false);
const [highlightedMessageId, setHighlightedMessageId] = useState<string | null>(null);
@@ -488,6 +492,21 @@ export function StandaloneChat({
const [conversationId, setConversationId] = useState<string | null>(
initialSessionIdRef.current,
);
+
+ // Pipe-watch sessions keep their messages in the chat store, not in this
+ // component's local state. Read them from the store directly and fall back to
+ // the local buffer for regular sessions, instead of an effect that mirrored
+ // the store into local state (an extra render + a frame of stale messages).
+ // Pipe-watch and regular sessions are mutually exclusive — the agent
+ // foreground handler early-returns for pipe-watch — so the two sources never
+ // feed the same session. Every `messages` reader below is unchanged.
+ const pipeWatchMessages = useChatStore((state) =>
+ conversationId && state.sessions[conversationId]?.kind === "pipe-watch"
+ ? state.sessions[conversationId]?.messages
+ : undefined,
+ );
+ const messages = (pipeWatchMessages ?? localMessages) as Message[];
+
const {
consumePendingAttachments,
stagePendingAttachments,
@@ -769,7 +788,6 @@ export function StandaloneChat({
startPipeExecution,
} = useChatSessionRuntime({
conversationId,
- setMessages,
setIsLoading,
setIsStreaming,
isLoading,