fix(trace): Trace Viewer 改为按 runId 分组渲染,显示所有轮次

修复 v0.3.8 引入的"覆盖"问题:sendMessage 清空 traceSteps 导致
前一条消息的 trace 被永久丢失。

方案 A+ — 既不叠加也不覆盖,按 runId 分组展示所有轮次:
- agent-store.ts: 回退 sendMessage 的 traceSteps: [] 清空(保留历史)
  保留 tokenUsage 重置(当前 run 统计应该重置)
- TraceViewer.tsx: 重写渲染逻辑
  - 新增 groupByRun 工具函数,按 runId 分组 + 计算轮次序号
  - 每个 run 独立 section,带"第 N 轮"标题 + 状态徽标(进行中/已完成)+ 步数
  - 当前 run 高亮(primary 边框 + 浅色背景 + MessageSquare 图标)
  - 历史 run 弱化(divider 边框 + 透明背景 + 0.85 透明度)
  - 顶部统计"共 N 轮"
  - isCurrent 判定改为"当前 run 的最后一个 step"(之前是"全局最后一个")

useAgentStream 的 runId 隔离逻辑(v0.3.8 已加)无需改动,
正好支撑多 run 共存设计:isSameIteration 校验 runId,跨 run 孤立
TERMINATED 跳过不创建 step。

冒烟测试场景:
- 多轮对话 → 每轮独立显示,不再叠加
- 历史 trace 不丢失 → 切换会话回来仍可见所有轮次
- 当前 run 高亮 → 视觉聚焦"正在进行的活动"
This commit is contained in:
2026-07-20 21:57:03 +08:00
parent dde21f0b3f
commit 5ec32f33da
2 changed files with 126 additions and 14 deletions
+3 -3
View File
@@ -267,9 +267,9 @@ export const useAgentStore = create<AgentState>((set, get) => ({
isStreaming: true,
currentRunId: null, // 将在首个 streamEvent 中由 runId 设置
currentIteration: 0,
// 重置 trace 状态:防止新消息在上一条消息的 trace 上面叠加渲染
// 前一条消息的 trace 已在 done 事件中通过 saveTraceData() 持久化到 DB
traceSteps: [],
// 方案 A: 不清空 traceSteps,避免前一条消息的 trace 被永久覆盖
// TraceViewer 按 runId 过滤显示,只展示当前 run 的 steps
// 历史 trace 仍在 DB 中,切换会话回来可恢复
tokenUsage: { inputTokens: 0, outputTokens: 0, totalTokens: 0 },
}));