Repository navigation
Conversation
Inline visualizations were slow to author. Every model-written fragment carried its own resize, theme-redraw, axis, and tick-overlap code plus a pinned D3 tag, and the visualize skill asked the model to read the file back, which often grew into shell, browser, and screenshot checks. The visualization shell now preloads the bundled D3 and a small `viz` helper layer (container sizing and redraw, axes, label overlap, and a declarative `viz.render` for common charts), and the skill asks for one write followed by the reply. Fragments written under the earlier skill keep rendering; their pinned D3 tag is dropped so it cannot replace the preloaded global. Measured in the app with deepseek-flash on ten queries, total time went from 1004.6 s to 498.0 s and model round trips from 95 to 33.
vastsa
left a comment
There was a problem hiding this comment.
图表辅助层解决了重复样板代码,改动也覆盖了 D3 预加载和旧片段兼容。暂不能合入:这个新硬规则禁止对生成结果做任何验证,而 PR 自己的 10 个样例中已经有 1 个因为容器 ID 写错而显示空白卡片;当前脚本错误也只写入 console。请保留一个轻量的结果检查路径,或让运行时把渲染错误呈现在卡片里,再用错误样例验证用户能发现并修正空白图。感谢整理性能数据和兼容性处理。
|
|
||
| ## Hard rules | ||
|
|
||
| - One `Write`, then the reply. Do not verify the fragment: no Bash or shell |
There was a problem hiding this comment.
这条规则把验证完全禁掉了,但 PR 的 n=1 实测已有一个片段因传错容器 ID 而成为空白卡片;当前运行时只把这类异常写入 console。模型在“一次 Write 后立即回复”的约束下无法发现这个用户可见故障。请允许一次低成本的结果检查,或先在卡片内呈现渲染错误,并为错误容器 ID 增加覆盖。
Review of the preload change pointed out that the skill now forbids any verification, yet one of ten live fragments rendered a blank card because it targeted a missing container id, and the error only reached the guest console. The Write result for a fragment in the session visualization directory now carries a static check: inline scripts are compiled (never run) and literal ids passed to viz.render, viz.chart, getElementById, or querySelector must exist in the fragment. Problems are listed so the model can fix them with one Edit; a clean write keeps the plain result. The guest runtime also reports its first uncaught error to the card, which keeps the frame and shows the message as bounded plain text. The skill keeps its ban on self-verification, because a live run without it brought back Bash, browser, and read-back detours (82 vs 30 round trips over ten queries). The viz helpers also get review fixes: date-only strings parse as local dates, year-like values skip thousands separators, re-rendering a container replaces the previous chart, destroy is idempotent, and arc and heatmap specs report missing channels.
|
感谢 review,两点都按建议补上了(084818cb7,并已合入最新 main): 1. 低成本结果检查:Write 回执自带静态检查
2. 运行时错误显示在卡片里
3. skill 的禁令为什么保留 4. 另外修了 helper review 里的 9 处问题:日期字符串按本地日期解析、年份不加千分位、同一容器重复渲染会替换旧图、 最新实测(deepseek-flash,同样 10 条 query,n=1)
本次比首版慢主要是模型选择的写法不同:只有 4/10 用了 验证:gen-ui E2E 11 项通过;desktop 单测 4400/4404(唯一失败是本机会 kill 参数含 |
背景
#1826 合入的 inline 可视化出图慢:模型每次都要手写 resize/主题重绘/坐标轴/刻度防重叠代码和 D3 标签,skill 还要求写完回读,经常演变成跑 shell、开浏览器、截图验证,一次出图 1~3 分钟。
怎么改的
viz辅助层(apps/desktop/resources/gen-ui/visualize-helpers.js):viz.chart/plot/axes/extent/pruneOverlaps/series/color/duration/regression:负责容器尺寸、resize 和主题重绘、坐标轴、标签防重叠等样板代码;viz.render(el, spec):声明式出常见图表(bar 含横向/分组/堆叠、line、area、point 含趋势线、heatmap、arc 环形),自带坐标轴、图例、数值标签和 tooltip,update()支持交互更新。visualize.md,11988 B,仍在 12 KiB 上限内):删掉“写完回读/Review”,新增 Hard rules(一次 Write 后直接回复,不跑 shell、不开浏览器、不截图、不翻源码);Charts 一节改成以viz.render为主,viz.chart用于自定义图。d3;旧片段照常渲染。CSP、沙箱和协议都没有改动。visualize.css增加坐标轴/图例/数值标签样式;ADR、spec(中英文)、E2E 场景文档、NOTICE、unreleased 同步更新。耗时对比
测试方法:用真实构建的 PI-Desktop,通过 renderer IPC(CDP)发 prompt,经本地代理转发到 DeepSeek 官方 API。模型 deepseek-flash(以自定义 OpenAI 兼容 provider 接入,会话 thinking 设为 off,权限 auto),每个 query 跑 1 次(n=1),计时从发出 prompt 到 agent_end。四个版本的条件完全相同。
viz辅助,不再回读viz.render(即本 PR)按写法拆分(③ 对比原版):
viz.render的 5 个:453.4 s → 139.9 s(−69%),平均片段 9.5 KB → 2.3 KB,平均每个 28.0 s原版慢的典型路径:q1 Skill → Bash → Write → Read → ToolSearch → BrowserPreview → Read → Edit ×3 → Read → Edit;q8 调用 Playwright 截图,共 33 轮。
使用的 query
验证
pnpm build:js、pnpm --filter @pi-desktop/desktop typecheck、pnpm lint、node scripts/check-architecture.mjs:通过viz在<head>中先于片段加载、片段自带的固定版本 D3 标签被移除、skill 里提到的viz.*都真实存在pnpm test:e2e:gen-ui:通过,10 项检查;新增viz.chart尺寸探针和viz.render出图探针live-voice-gemini-adapter,原因是本机会 kill 命令行中带 "gemini" 的进程,与本 PR 无关;换别名路径单独运行可以通过已知限制 / 后续方向
viz.render,另外 3 个仍然手写viz.chart,可以在 Hard rules 里要求标准图表必须用viz.render。viz.render还不支持多图层叠加(例如直方图 + 曲线)。