fix(frontend): 修复 PR #18 审阅问题并补充回归测试
审阅意见逐项修复: 1. 主题包安装丢弃用户 CSS inspectThemePackage 之前只解析 YAML 清单,ThemesView 安装时另外 生成一套硬编码调色板,用户提供的 CSS 被整份丢掉。现在定义单文件 格式(YAML 清单 + `---` + CSS),parseThemePackage 取出真实 CSS 并原样安装;CSS 安全校验提前到预览阶段;按内容识别并拒绝 ZIP。 2. 主题恢复竞态导致页面无 data-theme initTheme 之前没有 await loadCustomThemes,自定义主题还没进 allThemes,applyTheme 找不到主题直接 return。现在先同步落一个 内置主题兜底(不写 localStorage,避免冲掉用户存的自定义主题 id), 加载完成后再切到真正保存的那个;主题失效或列表加载失败时回退并 通过 themeLoadWarning 告知用户,不再静默。 3. Trace 建树依赖事件相邻顺序 后端真实顺序是 ModelCallStarted → ModelCallCompleted → Usage → ToolCall/ToolResult,工具在模型调用完成后才执行且并发跑,相邻性 不可用。改为按 model_call_id / parent_model_call_id / tool_call_id 关联;ToolResult 回填 ToolCall 的状态与耗时,结束后不再显示 running;SSE 断点恢复的孤立事件退回顶层而不是丢弃。 4. Trace 叶子节点无法查看数据 行的 click 是 `children.length && toggleExpand`,而详情 v-if 又 要求 `children.length === 0`,两个条件互斥。拆成 expandedNodes 与 detailNodes 两个状态集合;展开箭头改为独立按钮,行支持键盘 与 aria-expanded;引用节点补「定位」按钮。同时修正 Usage 卡片 字段(后端只发累计 token_usage)。 5. 引用定位逻辑三处重复且各自有缺陷 抽出 navigateToCitation(依赖注入,可独立测试)+ useCitationNavigation。 调用顺序固化:必须先 await loadFile 再 highlightBlock,否则 editor store 的 loadFile 末尾会把高亮清掉;loadFile 失败时不跳转。 AgentView / ChatView / AppShell 统一走这一处。 6. 插件命令 UI 重复实现 抽出 PluginCommandPanel 复用 PluginMcpPanel 的 schema 驱动表单, 删除 PluginsView 里的劣化副本。effect 现在真的执行 navigate / refresh(此前只拼成文本显示);补上必填校验与布尔字段初始值, 修正「显示否但不提交该键」的不一致。 补充回归测试 64 项(相关 spec 由 25 项增至 89 项),并对 2、3、4 三项 缺陷做了变异验证:把修复回退成原写法后对应测试确实失败。 涉及 traceService / theme store / themePackageService / pluginCommandForm / useCitationNavigation / TraceTimeline,其中后三个为新增文件。 vue-tsc -b、vitest(32 文件 182 项)、vite build 全部通过。
This commit is contained in:
@@ -1,74 +1,121 @@
|
||||
import type { AgentEvent, TraceNode, TraceNodeType } from '@/contracts'
|
||||
|
||||
/**
|
||||
* 把扁平事件流折叠成调用树。
|
||||
*
|
||||
* 归属关系一律走 id,不依赖事件相邻顺序 —— 后端的真实顺序是
|
||||
* ModelCallStarted → ModelCallCompleted → Usage → ToolCall/ToolResult,
|
||||
* 工具在模型调用「完成」之后才执行,并且多个工具是并发跑的
|
||||
* (runtime.py 里 asyncio.gather + Semaphore),事件会交错到达。
|
||||
* 因此工具事件用 data.parent_model_call_id 找父节点,
|
||||
* ToolResult 用 data.tool_call_id 回填对应 ToolCall 的状态。
|
||||
*/
|
||||
export function buildTraceNodes(events: AgentEvent[]): TraceNode[] {
|
||||
const nodes: TraceNode[] = []
|
||||
let currentModelCallId: string | null = null
|
||||
const roots: TraceNode[] = []
|
||||
/** model_call_id -> 模型调用节点 */
|
||||
const modelCalls = new Map<string, TraceNode>()
|
||||
/** tool_call_id -> 工具调用节点,供 ToolResult 回填状态 */
|
||||
const toolCalls = new Map<string, TraceNode>()
|
||||
|
||||
for (const event of events) {
|
||||
const type = mapEventType(event.event)
|
||||
const id = `seq-${event.sequence}`
|
||||
const title = getNodeTitle(event)
|
||||
const subtitle = getNodeSubtitle(event)
|
||||
const status = getNodeStatus(event)
|
||||
|
||||
const node: TraceNode = {
|
||||
id,
|
||||
id: `seq-${event.sequence}`,
|
||||
sequence: event.sequence,
|
||||
type,
|
||||
title,
|
||||
subtitle,
|
||||
status,
|
||||
type: mapEventType(event.event),
|
||||
title: getNodeTitle(event),
|
||||
subtitle: getNodeSubtitle(event),
|
||||
status: getNodeStatus(event),
|
||||
data: event.data,
|
||||
timestamp: event.timestamp,
|
||||
children: [],
|
||||
}
|
||||
const modelCallId = asId(event.data.model_call_id)
|
||||
const parentModelCallId = asId(event.data.parent_model_call_id)
|
||||
const toolCallId = asId(event.data.tool_call_id)
|
||||
|
||||
if (event.event === 'ModelCallStarted') {
|
||||
currentModelCallId = id
|
||||
node.children = []
|
||||
nodes.push(node)
|
||||
continue
|
||||
}
|
||||
|
||||
if (event.event === 'ModelCallCompleted' || event.event === 'ModelCallFailed') {
|
||||
if (currentModelCallId) {
|
||||
const modelCall = findNodeById(nodes, currentModelCallId)
|
||||
if (modelCall) {
|
||||
modelCall.status = event.event === 'ModelCallCompleted' ? 'completed' : 'error'
|
||||
if (event.data.duration_ms != null) {
|
||||
modelCall.duration_ms = event.data.duration_ms as number
|
||||
}
|
||||
if (event.data.finish_reason) {
|
||||
modelCall.subtitle = `${modelCall.subtitle ?? ''} · ${String(event.data.finish_reason)}`
|
||||
}
|
||||
}
|
||||
currentModelCallId = null
|
||||
switch (event.event) {
|
||||
case 'ModelCallStarted': {
|
||||
if (modelCallId) modelCalls.set(modelCallId, node)
|
||||
roots.push(node)
|
||||
continue
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if (currentModelCallId && type !== 'run' && type !== 'complete' && type !== 'error') {
|
||||
const parent = findNodeById(nodes, currentModelCallId)
|
||||
if (parent) {
|
||||
node.parent_id = currentModelCallId
|
||||
parent.children.push(node)
|
||||
// 完成/失败事件不单独成节点,只更新对应模型调用的状态。
|
||||
case 'ModelCallCompleted':
|
||||
case 'ModelCallFailed': {
|
||||
const target = modelCallId ? modelCalls.get(modelCallId) : undefined
|
||||
if (!target) {
|
||||
// 找不到配对的 Started(例如 SSE 断点恢复后只拿到后半段),保留为顶层节点。
|
||||
roots.push(node)
|
||||
continue
|
||||
}
|
||||
target.status = event.event === 'ModelCallCompleted' ? 'completed' : 'error'
|
||||
const duration = asNumber(event.data.duration_ms)
|
||||
if (duration != null) target.duration_ms = duration
|
||||
const extra = event.event === 'ModelCallCompleted'
|
||||
? asText(event.data.finish_reason)
|
||||
: asText(event.data.error_code)
|
||||
if (extra) target.subtitle = target.subtitle ? `${target.subtitle} · ${extra}` : extra
|
||||
continue
|
||||
}
|
||||
|
||||
// ToolResult 只回填对应 ToolCall,避免工具结束后仍显示 running。
|
||||
case 'ToolResult': {
|
||||
const target = toolCallId ? toolCalls.get(toolCallId) : undefined
|
||||
if (!target) {
|
||||
attach(node, parentModelCallId, modelCalls, roots)
|
||||
continue
|
||||
}
|
||||
target.status = event.data.success === false ? 'error' : 'completed'
|
||||
const duration = asNumber(event.data.duration_ms)
|
||||
if (duration != null) target.duration_ms = duration
|
||||
const detail = event.data.success === false
|
||||
? asText(event.data.error_code) ?? '失败'
|
||||
: undefined
|
||||
if (detail) target.subtitle = target.subtitle ? `${target.subtitle} · ${detail}` : detail
|
||||
// 结果数据合并到调用节点,展开详情时才能看到 output。
|
||||
target.data = { ...target.data, result: event.data }
|
||||
continue
|
||||
}
|
||||
|
||||
case 'ToolCall': {
|
||||
if (toolCallId) toolCalls.set(toolCallId, node)
|
||||
attach(node, parentModelCallId, modelCalls, roots)
|
||||
continue
|
||||
}
|
||||
|
||||
default: {
|
||||
attach(node, parentModelCallId, modelCalls, roots)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
nodes.push(node)
|
||||
}
|
||||
|
||||
return nodes
|
||||
return roots
|
||||
}
|
||||
|
||||
function findNodeById(nodes: TraceNode[], id: string): TraceNode | null {
|
||||
for (const node of nodes) {
|
||||
if (node.id === id) return node
|
||||
const found = findNodeById(node.children, id)
|
||||
if (found) return found
|
||||
/** 有已知父模型调用就挂进去,否则留在顶层。 */
|
||||
function attach(
|
||||
node: TraceNode,
|
||||
parentModelCallId: string | null,
|
||||
modelCalls: Map<string, TraceNode>,
|
||||
roots: TraceNode[],
|
||||
) {
|
||||
const parent = parentModelCallId ? modelCalls.get(parentModelCallId) : undefined
|
||||
if (parent) {
|
||||
node.parent_id = parent.id
|
||||
parent.children.push(node)
|
||||
return
|
||||
}
|
||||
return null
|
||||
roots.push(node)
|
||||
}
|
||||
|
||||
function asId(value: unknown): string | null {
|
||||
return typeof value === 'string' && value !== '' ? value : null
|
||||
}
|
||||
|
||||
function asText(value: unknown): string | undefined {
|
||||
return typeof value === 'string' && value !== '' ? value : undefined
|
||||
}
|
||||
|
||||
function mapEventType(eventType: AgentEvent['event']): TraceNodeType {
|
||||
@@ -132,12 +179,12 @@ function getNodeSubtitle(event: AgentEvent): string | undefined {
|
||||
case 'Citation':
|
||||
return data.heading_path ? String(data.heading_path) : undefined
|
||||
case 'Usage': {
|
||||
// total_tokens 优先;缺失时回退到 input+output 之和。
|
||||
const total = asNumber(data.total_tokens)
|
||||
if (total != null) return `${total} tokens`
|
||||
// 后端发的是累计 token_usage(runtime.py),其余字段仅作兼容回退。
|
||||
const usage = asNumber(data.token_usage) ?? asNumber(data.total_tokens)
|
||||
if (usage != null) return `${usage} tokens`
|
||||
const input = asNumber(data.input_tokens)
|
||||
const output = asNumber(data.output_tokens)
|
||||
if (input == null && output == null) return '- tokens'
|
||||
if (input == null && output == null) return undefined
|
||||
return `${(input ?? 0) + (output ?? 0)} tokens`
|
||||
}
|
||||
case 'PermissionRequired':
|
||||
@@ -154,14 +201,19 @@ function getNodeStatus(event: AgentEvent): TraceNode['status'] {
|
||||
case 'RunFailed':
|
||||
case 'ModelCallFailed':
|
||||
return 'error'
|
||||
case 'ToolResult':
|
||||
// 只在 ToolResult 没配上 ToolCall 时(SSE 断点恢复)才成为独立节点,
|
||||
// 那时也要按 success 显示,不能一律算成功。
|
||||
return event.data.success === false ? 'error' : 'completed'
|
||||
case 'RunCompleted':
|
||||
case 'RunCancelled':
|
||||
case 'ModelCallCompleted':
|
||||
case 'ToolResult':
|
||||
case 'Usage':
|
||||
case 'PermissionResolved':
|
||||
return 'completed'
|
||||
case 'ToolCall':
|
||||
// 后端的 ToolCall 事件不带 status,起始一律 running,
|
||||
// 由后到的 ToolResult 回填最终状态。
|
||||
if (event.data.status === 'completed') return 'completed'
|
||||
if (event.data.status === 'error') return 'error'
|
||||
return 'running'
|
||||
|
||||
Reference in New Issue
Block a user