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): keep streaming state alive across turn_end (no send/stop flicker)

Louis Beaumont2db677f85f95
diff --git a/apps/screenpipe-app-tauri/lib/events/pipe-watch-writer.ts b/apps/screenpipe-app-tauri/lib/events/pipe-watch-writer.ts
index 44fcbbe5c..4b4e11712 100644
--- a/apps/screenpipe-app-tauri/lib/events/pipe-watch-writer.ts
+++ b/apps/screenpipe-app-tauri/lib/events/pipe-watch-writer.ts
@@ -288,9 +288,27 @@ function apply(sid: string, payload: AgentInnerEvent): void {
return;
}
+ // ── turn_end: an LLM turn just finished but the agent loop is still
+ // running (next turn will start with message_start, typically across
+ // a tool-call boundary). Settle this turn's accumulator so the next
+ // content event creates a fresh message — but do NOT call endTurn,
+ // which would flip isStreaming/isLoading false and make the chat
+ // input briefly switch from "stop" to "send" between tool calls.
+ if (t === "turn_end") {
+ const cur = useChatStore.getState().sessions[sid];
+ if (cur?.streamingMessageId) {
+ store.actions.setStreaming(sid, {
+ streamingMessageId: null,
+ streamingText: "",
+ contentBlocks: [],
+ });
+ }
+ return;
+ }
+
// ── terminal events: prefer agent_end's authoritative messages array
// if present; otherwise just settle streaming state.
- if (t === "agent_end" || t === "turn_end" || t === "pipe_done") {
+ if (t === "agent_end" || t === "pipe_done") {
const messages = (payload as any).messages;
if (Array.isArray(messages) && messages.length > 0) {
const reconstructed = reconstructFromAgentEnd(messages);
diff --git a/apps/screenpipe-app-tauri/lib/stores/pi-event-router.ts b/apps/screenpipe-app-tauri/lib/stores/pi-event-router.ts
index 80f9d3734..ed396df1e 100644
--- a/apps/screenpipe-app-tauri/lib/stores/pi-event-router.ts
+++ b/apps/screenpipe-app-tauri/lib/stores/pi-event-router.ts
@@ -528,13 +528,31 @@ function applyEventToSessionContent(sid: string, payload: PiInnerEvent) {
return;
}
+ // turn_end fires between LLM turns within a single agent run (typically
+ // across a tool-call boundary). The agent is still streaming — only the
+ // current message's accumulator should be cleared so the next
+ // message_start gets a fresh slate. Calling endTurn here would briefly
+ // flip isStreaming/isLoading false and falsely settle the session
+ // mid-run.
+ if (t === "turn_end") {
+ const cur = store.sessions[sid];
+ if (cur?.streamingMessageId) {
+ store.actions.setStreaming(sid, {
+ streamingMessageId: null,
+ streamingText: "",
+ contentBlocks: [],
+ });
+ }
+ return;
+ }
+
// End of turn — flush streaming state to "settled" message + clear
// in-flight markers. We're in the BACKGROUND-only branch (the early
// `currentId === sid` return above gates this), so the panel won't
// run its own save useEffect for this session. Persist directly so
// a chat that completes while the user is looking elsewhere still
// ends up on disk and survives a restart.
- if (t === "agent_end" || t === "turn_end") {
+ if (t === "agent_end") {
store.actions.endTurn(sid);
void persistBackgroundSession(sid);
return;