【已审核】feat(agent): Bash 工具输出实时终端化显示 - #1439
Open
climashscape wants to merge 1 commit into
Open
Conversation
- 主进程透传 Pi Bash onUpdate 累计输出为 tool_output SDKMessage(增量计算 + 截断重置) - 渲染进程接收 tool_output 事件并维护 ToolActivity.streamingOutput 缓冲 - BashResultRenderer 新增流式终端模式:实时追加、自动滚动跟随、暂停/恢复、 ANSI SGR 颜色解析、stderr 红色高亮、复制、超长输出尾部渲染 - 流式期间 tool_output 不进入 liveMessages,避免污染消息转录 - 新增 tool_output 状态机、ANSI 解析、partialResult 提取单元测试 BDD 自审查修复: - 流式输出按 toolUseId 切片订阅(atomFamily),避免任意 session 的 Bash tick 污染消息树 - store 层引用守卫,no-op 事件不新建 Map 触发整树重渲染 - 流式结束后 CollapsibleResult 默认展开,避免从实时 tail 跳回折叠头部 - 复制按钮 title 明确为可见输出;补充 focus-visible a11y - 主进程按 Bash 白名单过滤 onUpdate;partialResult 提取与 ANSI 解析补充单测
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概述
Agent 模式下执行长命令(npm run build、python train.py 等)时,Bash 工具结果此前是静态渲染:执行期间无任何输出,结束后超过 3000 字符自动折叠仅显示前 15 行,无流式、无 ANSI 颜色、无实时滚动。本 PR 将其升级为真实终端体验。
核心思路:Pi SDK 的 Bash 工具在执行期间本来就通过 onUpdate 每 100ms 推送累计输出快照(
tool_execution_update事件),但主进程此前只转发心跳、丢弃了输出内容。本 PR 把这部分内容捡回来,建立完整的事件流 → 状态 → 渲染链路。改动
数据链路(主进程 → 渲染进程)
packages/shared/src/types/agent.ts:新增SDKToolOutputMessage(tool_output)消息类型 +tool_outputAgentEvent,扩展SDKMessage联合类型apps/electron/src/main/lib/adapters/pi-agent-adapter.ts:从 Pitool_execution_update事件提取 partialResult 文本,按 Bash 白名单过滤,计算增量(首帧/截断重置用replace标记)后透传为tool_output消息;tool_execution_end时清理缓冲防泄漏apps/electron/src/renderer/hooks/useGlobalAgentListeners.ts:tool_output转 AgentEvent,且不写入 liveMessages 转录(避免高频数据污染消息列表)apps/electron/src/renderer/atoms/agent-atoms.ts:ToolActivity.streamingOutput缓冲 +applyAgentEvent增量拼接/replace 重置(引用稳定优化,无变化时返回原引用)前端终端化渲染
bash-result.tsx:重写为固定高度终端块(max-h-320px + 内部滚动),运行中/结束后高度一致。支持实时逐行追加、自动滚动跟随、暂停/恢复滚动、回到底部、ANSI SGR 颜色解析(16/256/truecolor)、stderr 红色高亮、超长输出尾部渲染(500 行)、复制ContentBlock.tsx:按 toolUseId 切片订阅流式输出(atomFamily,避免任意 session 的 Bash tick 污染消息树);单层折叠交互(展开 = 完整输出块,折叠 = 无输出块)tool-result-renderers/index.tsx:透传 streamingOutput/isStreamingOutput测试
agent-atoms.test.ts:tool_output 状态机 5 项(增量/首帧/replace/空增量/不匹配)bash-result.test.ts:ANSI 解析器 10 项(16/256/truecolor、样式、重置)pi-agent-bash.test.ts:partialResult 提取 7 项测试
bun run --filter='@proma/electron' typecheck✅bun test相关单测 40 pass / 0 fail ✅build:main+build:renderer通过 ✅bun run dev真机验证(两轮):短输出 / 60 行长耗时流式滚动 / stderr 高亮 / ANSI 彩色保留 / 800 行超长输出 / 50KB 截断 / 混合错误场景 / 无输出兜底,全部符合预期紧急度
常规
备注
无对应 upstream issue,未关联 Closes。