From 645af161a8a9d2be255de3cd7f464bf8a7a1d4bc Mon Sep 17 00:00:00 2001 From: QQ Date: Sat, 14 Nov 2026 14:51:42 +0800 Subject: [PATCH] Import packages/plugin/src/acp/pi-tool-result.ts --- packages/plugin/src/acp/pi-tool-result.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 packages/plugin/src/acp/pi-tool-result.ts diff --git a/packages/plugin/src/acp/pi-tool-result.ts b/packages/plugin/src/acp/pi-tool-result.ts new file mode 100644 index 0000000..5f5f5cb --- /dev/null +++ b/packages/plugin/src/acp/pi-tool-result.ts @@ -0,0 +1,18 @@ +/** Browser-safe decoding of published pi-acp tool result notifications. */ +export function piToolResult(update: Record): { content: Array<{ type: 'text'; text: string }>; isError: boolean } { + let content: Array<{ type: 'text'; text: string }> = Array.isArray(update.content) + ? update.content.flatMap((item: any) => { + if (item?.type === 'content' && item.content?.type === 'text' && typeof item.content.text === 'string') return [{ type: 'text' as const, text: item.content.text }] + if (item?.type === 'diff' && typeof item.path === 'string' && typeof item.newText === 'string' + && (item.oldText === null || typeof item.oldText === 'string')) { + return [{ type: 'text' as const, text: `File: ${item.path}\n--- before\n${item.oldText ?? ''}\n+++ after\n${item.newText}` }] + } + return [] + }) : [] + if (!content.length && Array.isArray(update.rawOutput?.content)) content = update.rawOutput.content + .filter((item: any) => item?.type === 'text' && typeof item.text === 'string') + .map((item: any) => ({ type: 'text' as const, text: item.text })) + const output = update._meta?.terminal_output?.data + if (typeof output === 'string') content.push({ type: 'text', text: output }) + return { content, isError: update.status === 'failed' || update.rawOutput?.isError === true } +}