diff --git a/packages/adapters/pi-local/src/ui/parse-stdout.test.ts b/packages/adapters/pi-local/src/ui/parse-stdout.test.ts new file mode 100644 index 00000000000..491d3a55486 --- /dev/null +++ b/packages/adapters/pi-local/src/ui/parse-stdout.test.ts @@ -0,0 +1,59 @@ +import { describe, expect, it } from "vitest"; +import { parsePiStdoutLine } from "./parse-stdout.js"; + +const ts = "2026-08-20T10:00:00.000Z"; + +describe("parsePiStdoutLine message_end", () => { + it("renders user messages as user transcript entries", () => { + const line = JSON.stringify({ + type: "message_end", + message: { + role: "user", + content: [{ type: "text", text: "## Paperclip Wake Payload" }], + }, + }); + + expect(parsePiStdoutLine(line, ts)).toEqual([ + { kind: "user", ts, text: "## Paperclip Wake Payload" }, + ]); + }); + + it("does not duplicate tool-result messages", () => { + const line = JSON.stringify({ + type: "message_end", + message: { + role: "toolResult", + content: [{ type: "text", text: "file contents" }], + }, + }); + + expect(parsePiStdoutLine(line, ts)).toEqual([]); + }); + + it("keeps assistant text and thinking entries", () => { + const line = JSON.stringify({ + type: "message_end", + message: { + role: "assistant", + content: [ + { type: "thinking", thinking: "checking" }, + { type: "text", text: "done" }, + ], + }, + }); + + expect(parsePiStdoutLine(line, ts)).toEqual([ + { kind: "thinking", ts, text: "checking" }, + { kind: "assistant", ts, text: "done" }, + ]); + }); + + it("ignores unsupported message roles", () => { + const line = JSON.stringify({ + type: "message_end", + message: { role: "system", content: "internal message" }, + }); + + expect(parsePiStdoutLine(line, ts)).toEqual([]); + }); +}); diff --git a/packages/adapters/pi-local/src/ui/parse-stdout.ts b/packages/adapters/pi-local/src/ui/parse-stdout.ts index 9b377412b4d..69b04f7602f 100644 --- a/packages/adapters/pi-local/src/ui/parse-stdout.ts +++ b/packages/adapters/pi-local/src/ui/parse-stdout.ts @@ -232,25 +232,30 @@ export function parsePiStdoutLine(line: string, ts: string): TranscriptEntry[] { if (type === "message_end") { const message = asRecord(parsed.message); - if (message) { - const content = message.content as string | Array<{ type: string; text?: string; thinking?: string }>; - const { text, thinking } = extractTextContent(content); - - const entries: TranscriptEntry[] = []; - - // Emit final thinking block if present - if (thinking) { - entries.push({ kind: "thinking", ts, text: thinking }); - } - - // Emit final text block if present - if (text) { - entries.push({ kind: "assistant", ts, text }); - } - - return entries; + if (!message) return []; + + const role = asString(message.role); + const content = message.content as string | Array<{ type: string; text?: string; thinking?: string }>; + const { text, thinking } = extractTextContent(content); + + if (role === "user") { + return text ? [{ kind: "user", ts, text }] : []; } - return []; + if (role !== "assistant") return []; + + const entries: TranscriptEntry[] = []; + + // Emit final thinking block if present + if (thinking) { + entries.push({ kind: "thinking", ts, text: thinking }); + } + + // Emit final text block if present + if (text) { + entries.push({ kind: "assistant", ts, text }); + } + + return entries; } // Tool execution