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

fix(chat): persist user message before provider response

Louis Beaumont5565a7516c7d
diff --git a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-pi-send-transport.ts b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-pi-send-transport.ts
index 5581ad0eb..b702203a9 100644
--- a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-pi-send-transport.ts
+++ b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-pi-send-transport.ts
@@ -307,20 +307,18 @@ export function usePiSendTransport(options: PiSendTransportOptions) {
// one id.
setConversationId(turnSessionId);
- let nextRowsAfterUserAppend: Message[] | null = null;
- setMessages((prev) => {
- const next = [...prev, newUserMessage];
- nextRowsAfterUserAppend = next;
- return next;
+ // Persist from the current render snapshot instead of assigning inside the
+ // state updater. React may defer updater callbacks; when that happened, a
+ // slow provider response left a brand-new conversation without a file until
+ // the response completed (or without one at all if the app exited first).
+ const nextRowsAfterUserAppend = [...messages, newUserMessage];
+ setMessages((prev) => [...prev, newUserMessage]);
+ // conversationId state hasn't committed yet this tick, so force the
+ // immediate save under the same id explicitly.
+ void saveConversation(nextRowsAfterUserAppend, {
+ refreshHistory: false,
+ idOverride: turnSessionId,
});
- if (nextRowsAfterUserAppend) {
- // conversationId state hasn't committed yet this tick, so force the
- // immediate save under the same id explicitly.
- void saveConversation(nextRowsAfterUserAppend, {
- refreshHistory: false,
- idOverride: turnSessionId,
- });
- }
setInput("");
if (inputRef.current) inputRef.current.style.height = "auto";
setIsLoading(true);