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 pipe-watch tool return rendering

Louis Beaumont23a4db5048e3
diff --git a/apps/screenpipe-app-tauri/components/__tests__/pipe-ndjson-to-chat.test.ts b/apps/screenpipe-app-tauri/components/__tests__/pipe-ndjson-to-chat.test.ts
index 81b109f31..6cd52a076 100644
--- a/apps/screenpipe-app-tauri/components/__tests__/pipe-ndjson-to-chat.test.ts
+++ b/apps/screenpipe-app-tauri/components/__tests__/pipe-ndjson-to-chat.test.ts
@@ -283,6 +283,30 @@ describe("parsePipeNdjsonToMessages", () => {
expect(msgs[3].content).toBe("No real meetings detected. Just ambient audio.");
});
+ it("hides Codex-style function return messages from agent_end", () => {
+ const stdout = [
+ '{"type":"agent_end","messages":[' +
+ '{"role":"user","content":[{"type":"text","text":"Analyze my screen"}]},' +
+ '{"role":"assistant","content":[{"type":"text","text":"Let me search."},{"type":"toolCall","id":"t1","name":"bash","arguments":{"command":"curl localhost:3030/search"}}]},' +
+ '{"role":"user","content":[{"type":"text","text":"Return of functions.bash:0\\n{\\"data\\":[{\\"content\\":{\\"chunk_type\\":\\"refined\\"}}]}"}]},' +
+ '{"role":"assistant","content":[{"type":"text","text":"I found usable screen data."}]}' +
+ ']}',
+ ].join("\n");
+
+ const msgs = parsePipeNdjsonToMessages(stdout);
+
+ expect(msgs.map((m) => m.content)).toEqual([
+ "Analyze my screen",
+ "Let me search.",
+ "I found usable screen data.",
+ ]);
+ expect(
+ msgs.some((m) => String(m.content).includes("Return of functions.bash")),
+ ).toBe(false);
+ const toolBlock = msgs[1].contentBlocks?.find((b: any) => b.type === "tool");
+ expect(toolBlock?.toolCall.result).toContain('"data"');
+ });
+
it("parses agent_end when text_delta was filtered (no streaming text)", () => {
// This simulates what happens after filter_ndjson_stdout removes text_delta
const stdout = [
diff --git a/apps/screenpipe-app-tauri/lib/events/__tests__/pipe-watch-writer.test.ts b/apps/screenpipe-app-tauri/lib/events/__tests__/pipe-watch-writer.test.ts
index 9117c5706..0352bd96f 100644
--- a/apps/screenpipe-app-tauri/lib/events/__tests__/pipe-watch-writer.test.ts
+++ b/apps/screenpipe-app-tauri/lib/events/__tests__/pipe-watch-writer.test.ts
@@ -203,6 +203,55 @@ describe("pipe-watch-writer: agent_end takes precedence", () => {
expect(session.isStreaming).toBe(false);
expect(session.isLoading).toBe(false);
});
+
+ it("does not render Codex-style function returns as chat messages", () => {
+ seedPipeWatchSession();
+ __testing.inject(
+ env({
+ type: "agent_end",
+ messages: [
+ { role: "user", content: [{ type: "text", text: "analyze my screen" }] },
+ {
+ role: "assistant",
+ content: [
+ { type: "text", text: "Let me search." },
+ {
+ type: "toolCall",
+ id: "bash-1",
+ name: "bash",
+ arguments: { command: "curl localhost:3030/search" },
+ },
+ ],
+ },
+ {
+ role: "user",
+ content: [
+ {
+ type: "text",
+ text: 'Return of functions.bash:0\n{"data":[{"content":{"chunk_type":"refined"}}]}',
+ },
+ ],
+ },
+ {
+ role: "assistant",
+ content: [{ type: "text", text: "Found screen data." }],
+ },
+ ],
+ }),
+ );
+
+ const messages = useChatStore.getState().sessions[SID]!.messages! as any[];
+ expect(messages.map((m) => m.content)).toEqual([
+ "analyze my screen",
+ "Let me search.",
+ "Found screen data.",
+ ]);
+ expect(
+ messages.some((m) => String(m.content).includes("Return of functions.bash")),
+ ).toBe(false);
+ const toolBlock = messages[1].contentBlocks.find((b: any) => b.type === "tool");
+ expect(toolBlock.toolCall.result).toContain('"data"');
+ });
});
describe("pipe-watch-writer: regression — switch-away preserves messages", () => {
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 4b4e11712..e5e9d2da1 100644
--- a/apps/screenpipe-app-tauri/lib/events/pipe-watch-writer.ts
+++ b/apps/screenpipe-app-tauri/lib/events/pipe-watch-writer.ts
@@ -335,30 +335,21 @@ function apply(sid: string, payload: AgentInnerEvent): void {
* post-hoc views render the same shape. */
function reconstructFromAgentEnd(agentMessages: any[]): any[] {
const out: any[] = [];
- let toolBlocksFromAssistant: any[] = [];
for (let i = 0; i < agentMessages.length; i++) {
const m = agentMessages[i];
- if (!m || (m.role !== "assistant" && m.role !== "user" && m.role !== "tool")) continue;
+ if (!m) continue;
- if (m.role === "tool") {
+ const text = extractText(m.content);
+ if (isToolReturnMessage(m, text)) {
// Tool result — splice its result text into the matching tool
// block on the most recent assistant message.
- if (out.length === 0) continue;
- const last = out[out.length - 1];
- if (!last.contentBlocks) continue;
- const blocks = last.contentBlocks as any[];
const toolCallId = m.toolCallId || m.tool_call_id;
- const resultText = extractText(m.content);
- for (const b of blocks) {
- if (b.type === "tool" && b.toolCall?.id === toolCallId) {
- b.toolCall.result = resultText.slice(0, 2000);
- break;
- }
- }
+ attachToolResult(out, toolCallId, toolReturnResultText(text));
continue;
}
- const text = extractText(m.content);
+ if (m.role !== "assistant" && m.role !== "user") continue;
+
const tools = extractToolCalls(m.content || [], i);
const blocks: any[] = [];
if (text.trim()) blocks.push({ type: "text", text });
@@ -374,9 +365,7 @@ function reconstructFromAgentEnd(agentMessages: any[]): any[] {
timestamp: Date.now(),
...(blocks.length ? { contentBlocks: blocks } : {}),
});
- if (m.role === "assistant") toolBlocksFromAssistant = tools;
}
- void toolBlocksFromAssistant;
return out;
}
@@ -408,6 +397,41 @@ function extractToolCalls(content: any[], idx: number): any[] {
return out;
}
+function isToolReturnMessage(message: any, text: string): boolean {
+ const role = message?.role;
+ if (role === "tool" || role === "toolResult") return true;
+ if (role !== "user" && role !== "assistant") return false;
+ return /^#{0,6}\s*Return of (?:functions\.)?[A-Za-z0-9_-]+:\d+\b/.test(text.trim());
+}
+
+function toolReturnResultText(text: string): string {
+ return text
+ .replace(/^#{0,6}\s*Return of (?:functions\.)?[A-Za-z0-9_-]+:\d+\s*/i, "")
+ .trim();
+}
+
+function attachToolResult(out: any[], toolCallId: string | undefined, resultText: string): void {
+ if (!resultText) return;
+ for (let i = out.length - 1; i >= 0; i--) {
+ const blocks = out[i]?.contentBlocks as any[] | undefined;
+ if (!Array.isArray(blocks)) continue;
+ const exact = toolCallId
+ ? blocks.find((b) => b.type === "tool" && b.toolCall?.id === toolCallId)
+ : undefined;
+ const fallback = [...blocks]
+ .reverse()
+ .find((b) => b.type === "tool" && !b.toolCall?.result);
+ const target = exact || fallback;
+ if (target?.toolCall) {
+ target.toolCall.result =
+ resultText.length > 2000
+ ? `${resultText.slice(0, 2000)}\n... (truncated)`
+ : resultText;
+ return;
+ }
+ }
+}
+
// Test-only helpers — call into the writer without going through the
// Tauri listener. Lets unit tests exercise the reducer in isolation.
export const __testing = {
diff --git a/apps/screenpipe-app-tauri/lib/pipe-ndjson-to-chat.ts b/apps/screenpipe-app-tauri/lib/pipe-ndjson-to-chat.ts
index 12cdd60a6..e3902f047 100644
--- a/apps/screenpipe-app-tauri/lib/pipe-ndjson-to-chat.ts
+++ b/apps/screenpipe-app-tauri/lib/pipe-ndjson-to-chat.ts
@@ -72,9 +72,23 @@ export function parsePipeNdjsonToMessages(raw: string): ChatMessage[] {
const msg = agentEndMessages[i];
const role = msg.role;
const content = msg.content;
+ const text = extractText(content);
+
+ if (isToolReturnMessage(msg, text)) {
+ const resultText = toolReturnResultText(text);
+ if (resultText && lastToolBlocks.length > 0) {
+ const lastTool = lastToolBlocks[lastToolBlocks.length - 1];
+ if (lastTool?.toolCall && !lastTool.toolCall.result) {
+ lastTool.toolCall.result =
+ resultText.length > 2000
+ ? resultText.slice(0, 2000) + "\n... (truncated)"
+ : resultText;
+ }
+ }
+ continue;
+ }
if (role === "user") {
- const text = extractText(content);
if (!text.trim()) continue;
const isPipePrompt = text.includes("Time range:") && text.includes("Execute the pipe now.");
const chatMsg: any = {
@@ -99,7 +113,6 @@ export function parsePipeNdjsonToMessages(raw: string): ChatMessage[] {
}
if (role === "assistant") {
- const text = extractText(content);
const toolBlocks = Array.isArray(content) ? extractToolCalls(content, i) : [];
lastToolBlocks = toolBlocks;
const contentBlocks: any[] = [];
@@ -286,6 +299,19 @@ export function parsePipeNdjsonToMessages(raw: string): ChatMessage[] {
return messages;
}
+function isToolReturnMessage(message: any, text: string): boolean {
+ const role = message?.role;
+ if (role === "tool" || role === "toolResult") return true;
+ if (role !== "user" && role !== "assistant") return false;
+ return /^#{0,6}\s*Return of (?:functions\.)?[A-Za-z0-9_-]+:\d+\b/.test(text.trim());
+}
+
+function toolReturnResultText(text: string): string {
+ return text
+ .replace(/^#{0,6}\s*Return of (?:functions\.)?[A-Za-z0-9_-]+:\d+\s*/i, "")
+ .trim();
+}
+
/**
* Create a ChatConversation from a pipe execution's raw stdout.
*/
diff --git a/apps/screenpipe-app-tauri/src-tauri/Cargo.lock b/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
index 52c40e2ef..8e7c6cc8f 100644
--- a/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
+++ b/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
@@ -10580,7 +10580,7 @@ dependencies = [
[[package]]
name = "screenpipe-app"
-version = "2.4.229"
+version = "2.4.230"
dependencies = [
"aes",
"anyhow",
diff --git a/apps/screenpipe-app-tauri/src-tauri/Cargo.toml b/apps/screenpipe-app-tauri/src-tauri/Cargo.toml
index 7cdd3ffcc..3337c7afb 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.229"
+version = "2.4.230"
description = "24/7 memory for your desktop. 100% local."
authors = ["you"]
license = ""