Import packages/plugin/src/acp/pi-tool-result.ts

This commit is contained in:
QQ 2026-11-14 14:51:42 +08:00
parent 5e576937f2
commit 645af161a8
1 changed files with 18 additions and 0 deletions

View File

@ -0,0 +1,18 @@
/** Browser-safe decoding of published pi-acp tool result notifications. */
export function piToolResult(update: Record<string, any>): { 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 }
}