Skip to content

fix(chat): restore the kit's #widget layout for inline visualizations - #2001

Merged
vastsa merged 5 commits into
vastsa:mainfrom
zszz3:fix/gen-ui-stage-spacing
Oct 31, 2026
Merged

vastsa merged 5 commits into
vastsa:mainfrom
zszz3:fix/gen-ui-stage-spacing

Conversation

@zszz3

@zszz3 zszz3 commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

背景

内联可视化卡片里,标题、按钮行、图表、图例、说明文字经常贴在一起,看起来很挤。

原因

我们的 kit 改编自 ZCode。kit 的布局约定是:外壳把片段放进 <div id="widget">,再由 #widget 让顶层块竖排、相互留出 12px 间距。PI-Desktop 移植时,外壳没有加这个容器,片段直接进了 <body>,所以 #widget 这套规则从来没有生效。

另外两个因素叠在了一起:

  • skill 要求模型“给片段根元素一个唯一 id”,模型几乎都会把整个片段包进一个根 div。
  • kit 把 h1–h6、p 的 margin-block 清成了 0。

结果是 body 这一层、根容器里面这一层都没有任何竖向间距。

改动

第一版用 :where() 把 body 或唯一的根容器改成 flex 竖排。评审指出,这会把模型自己写的、默认横排的 display:flex 根元素改成竖排。现在改为恢复 kit 原本的约定:

  • 外壳(gen-ui-document.ts):片段放进 <div id="widget">,kit 的 tooltip/tabs 运行时仍在容器外。
  • kit(visualize.css):
    • #widget 改为 flow-root,直接子块之间用 12px 上边距隔开,不用 flex。这样不会改变任何元素的排列方向,也不会拉伸顶层的行内控件。
    • 规则放在标题/段落 margin 重置之后,以元素级优先级覆盖那条重置。片段自己写的 margin、以及自带 flex/grid 布局的块都以片段为准。
    • 删掉了 ZCode 对 #widget 直接子元素的强制重置(margin/padding/border 均带 !important)。这条规则以前没有生效,现在生效的话会清掉片段自己的样式。
  • skill(visualize.md):要求把标题、控件、图、图例、说明直接写成顶层块,不要整体包一层;需要脚本操作的元素各自加 id。改后 12240 B,仍在 12 KiB 上限内。
  • 文档:NOTICE 记录了对 ZCode 布局约定的改动;更新 unreleased.md 和 E2E 计划的 Coverage。

取舍: 之前生成的“整体包一层根 div”的旧卡片会保持原样,不会自动拉开间距。要给它们做兼容就得重新猜测布局,在横排 flex 下还会造成错位,所以没有做。

测试

E2E(scripts/e2e/gen-ui-main.mjs)对 7 种布局断言了位置和间距:

布局 断言
顶层块(skill 要求的写法) 片段位于 #widget 内;相邻块间距 ≥ 8px;.viz-controls 里的按钮仍在同一行
顶层行内按钮 + 块 按钮在同一行、没有被拉伸;后面的块有间距
自带 padding 的顶层块 padding 保留
旧的单根包裹 几何不变(间距 0、0)
display:flex; gap:8px 默认横排(评审给的例子) dx = 108,dy = 0
flex-direction: column; gap: 5px 间距仍是 5
两列 grid dx = 104,dy = 0

实测

方法: 在本地真实构建的 PI-Desktop 里,通过 renderer IPC 发起对话,开启 inlineVisualizations,卡片宽度 700px,在 app 里用真实的 pi-genui:// iframe 渲染、截图,并测量顶层块之间的间距。

模型: deepseek-flash,DeepSeek 官方 API,以自定义 OpenAI 兼容 provider 接入;thinking 关闭(请求带 thinking: {type: "disabled"}),权限设为 auto。

测试 query(每个跑 2 次):

  1. 帮我讲解一下快排算法,用可视化演示每一步,并给出 Python 实现
  2. 帮我讲解一下螺旋矩阵遍历算法(LeetCode 54),用可视化演示每一步,可切换示例和随机矩阵,并给出 Python 实现
  3. 用折线图对比北京和上海过去 12 个月的 PM2.5 月均值(数据你自拟合理值),并给出可视化。
  4. 用环形图展示一个家庭一个月的开支构成,点击扇区能看到该类的明细。
  5. 画一个电梯状态机:空闲、上行、下行、开门四个状态,点击状态之间的转换能看到说明。
  6. 用热力图展示一周七天 × 24 小时的地铁客流量,颜色越深人越多,悬停显示具体人数。

结果:

  • 12/12 都按新 skill 写成了顶层块,没有一次把整个片段包进一个根元素。
  • 8 张可以正常测量的卡片里,顶层块之间基本都是 12px。少数不是 12px 的,都是模型自己写了内联 margin(如 margin:0、margin-top:4px),按设计以片段为准。
  • 耗时:11 次平均 29.9 s;有 1 次电梯状态机用了 322.7 s,属于离群值。电梯这个 query 在以往各个版本里也要 150~214 s。
  • 另外 4 张卡片有脚本错误,都是模型自己的代码 bug,与布局无关:pie 未定义、把 D3 selection 当成 DOM 节点、读取 undefined.pivot,以及一处语法错误。这些错误都会显示在卡片内。语法错误那一张,Write 检查本来能发现,但最终文件里的错误像是之后的 Edit 引入的,而 Edit 的结果目前不做检查。

验证

  • pnpm test:e2e:gen-ui:通过(包含上面的 7 种布局断言)
  • gen-ui 单测(visualize-skill / gen-ui-protocol / gen-ui-runtime / gen-ui-file-safety / gen-ui-card):56/56 通过
  • pnpm lint、desktop typecheck、node scripts/check-architecture.mjs、node scripts/check-release-docs.mjs、check-pr-base(已合入最新 main 30f3c52):全部通过

未覆盖 / 后续

  • 旧的单根包裹卡片不会自动补上间距(见上文“取舍”)。
  • Edit 的结果不跑片段检查,模型用 Edit 改坏的脚本只能靠卡片内的错误提示暴露。可以另开 PR,让 Edit 也跑一遍检查。
  • 螺旋矩阵切换“随机矩阵”后溢出的问题,以及模型把舞台高度写死导致下方大片空白的问题,都属于片段代码本身,这次没有处理。

Fragments are written straight into the shell body, and the visualize
skill asks for a single id'd root element, so neither the body nor that
root ever spaced its children. The kit's #widget gap does not apply in
PI-Desktop because there is no #widget, and headings and paragraphs have
zero block margins, so titles, controls, charts, legends, and captions
touched (reproduced with deepseek-flash in 2 of 3 quicksort runs).

The kit now stacks the blocks of the body and of a sole root container
with the 12px #widget gap. The rule has zero specificity, so a fragment
that lays out its own root keeps it, and it only applies to containers
with a block child so inline-only roots keep flowing inline. Existing
cards pick it up on their next load.

@vastsa vastsa left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原始间距问题真实存在,但当前改动会改变已经自行布局的片段,暂不能合入。

独立使用本机 Electron/Chromium 加载 main 与候选的完整 visualize.css,片段为 <div id="root" style="display:flex;gap:8px"><div style="width:100px;height:40px">A</div><div style="width:100px;height:40px">B</div></div>。main 的两个块横排(dx=108, dy=0,flex-direction=row);候选变成竖排(dx=0, dy=48,flex-direction=column)。未指定布局的同样根容器,块间距确实从 0 变为 12px;显式 grid 两列的对照仍保持两列。

原因是 :where 的零优先级按属性分别参与层叠:片段自己的 display:flex 和 gap:8px 并不会覆盖新增 flex-direction:column。现有 E2E 只切换 display:block,无法检测这一回归。请把默认排版限制在尚未自行布局的容器,或采用不会改变已有布局方向的间距方案;不要要求已有片段补写 flex-direction:row。请加入 display:flex + gap、依赖默认 row 的根容器回归,同时覆盖普通无布局单根、多顶层块和显式 grid/column,确认现有片段保持布局。

本轮已完成上述真实 Chromium 几何测量及 diff --check。尝试的五文件本地单测因复用环境中 shared 构建导出陈旧等错误未全部通过,不能作为验证通过,也不据此判断为本 PR 引入。尚未运行完整本地 GenUI 用户路径 E2E;已有独立可复现的布局回归,先请完成修复方向。没有修改或推送贡献者代码。

zszz3 added 2 commits October 31, 2026 13:26
The visualization kit adapted from ZCode styles its fragment through a
#widget container that spaces top-level blocks, but the PI-Desktop shell
wrote fragments straight into the document body, so those rules never
applied. The skill also asked for one id'd root, so models wrapped every
block in a single unstyled div and the title, controls, chart, legend,
and captions touched.

The shell now places the fragment inside #widget, which stacks its
top-level blocks with a 12px top margin instead of the earlier
:where() flex column that could turn a default-row flex root into a
column (review on vastsa#2001). Roots that lay themselves out, inline
controls, and fragment margins keep their geometry. The upstream reset
of #widget children is dropped so it does not strip fragment styling,
and the skill now asks for top-level blocks rather than one wrapper.
@zszz3 zszz3 changed the title fix(chat): space blocks inside inline visualizations fix(chat): restore the kit's #widget layout for inline visualizations Oct 31, 2026
@zszz3

zszz3 commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

@vastsa 谢谢 review,你说得对。第一版用 :where() 给根容器补 display:flex; flex-direction:column,零优先级按属性分别参与层叠:片段只写了 display:flex 时,flex-direction: column 会被补上,默认横排就变成了竖排。

这次没有在原方案上继续打补丁,而是修了根上的问题:kit 本来就约定由外壳把片段放进 #widget、由 #widget 负责顶层块的间距,PI-Desktop 移植时漏掉了这个容器。现在的改法:

  • 外壳把片段放进 <div id="widget">。
  • #widget 改为 flow-root,顶层块之间用 12px 上边距隔开,不用 flex,所以不会改变任何元素的排列方向;片段自己写的 margin、以及自带 flex/grid 布局的块,都以片段为准。同时删掉了 ZCode 那条对 #widget 子元素的 !important 重置。
  • skill 改为要求把各个块直接写成顶层元素,不要整体包一层。

你提到的回归场景都加进了 E2E:display:flex; gap:8px 默认横排(dx=108,dy=0)、单根包裹(几何不变)、多个顶层块(间距 ≥8px)、显式 column(gap 5)和 grid(两列)。另外也覆盖了顶层行内按钮不被拉伸、块自带的 padding 保留。

用 deepseek-flash 跑了 6 个 query × 2 次:12/12 都写成了顶层块,块间距基本都是 12px。分支已合入最新 main,测试和数据见 PR 描述。

@vastsa

vastsa commented Oct 31, 2026

Copy link
Copy Markdown
Owner

感谢更新。当前 flow-root + 顶层 margin 的方案已解决上次发现的默认 flex-row 变 column 问题;本机完整 GenUI Electron E2E 在 831ef26 和合入最新 main c85251b 后均通过,包含 native Host、交互/隔离及新增七种布局。

但 Linux CI 仍有可重复的布局断言失败,暂不能合入。我重跑了失败任务,结果为:

这不是日志里的 dbus 警告;实际失败是按钮纵向位置不同。目前还不能断定为纯测试时序。请在失败断言输出 iframe/guest viewport 宽度、按钮的 computed display/width 和容器宽度,检查 fixture:loaded 是否早于嵌入 frame 完成尺寸同步。如果是测量时序,应等到实际 guest viewport 达到夹具预期宽度后再测量,保留同行/不拉伸断言;如果稳定宽度下仍失败,需要修复对应布局。

本地五文件单测以 source hook 规避旧 shared/i18n 构建后为 55/56,其中不相关脚本错误消息测试仍受到 Vite 使用旧 shared 导出的影响,未计为通过。没有更改贡献者代码或放宽测试,也没有把本机通过当作 Linux 门禁通过。请修复并让 Electron CI 通过后继续合入验证。

The new layout checks measured the guest as soon as the fixture reported
loaded. On Linux CI the inline controls landed on two lines (top 12 vs
40, then 47 vs 83) while every local run kept them on one, which points
at measuring before the card had settled the frame's width.

The checks now measure the frame serving that fixture, wait until the
guest viewport matches the mounted iframe width for two frames, and
assert that it did. Failures report the viewport, #widget width, and
each element's box, display, and parent width so a real layout fault
can be told apart from timing.
@zszz3

zszz3 commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

@vastsa 谢谢重跑和定位。按你的建议处理了(42119b6c5,只改了 E2E,布局代码和断言都没动):

  • 测量对象改为加载该 fixture 的那个 frame(按 URL 选),不再取 guests()[0],避免量到还没卸载的上一个 frame。
  • fixture:loaded 之后,先等卡片里 iframe 的宽度确定下来,再等 guest viewport 宽度和它一致、并保持两帧不变,才开始测量;同时断言两者相等。同行和不拉伸的断言保留不变。
  • 断言失败时会输出 guest viewport 宽度、#widget 宽度,以及每个元素的位置、computed display、宽度和父容器宽度,方便区分是测量时序问题还是真的布局错误。

这次 Linux 上的 Trusted extensions Electron E2E 通过了,6 项 CI 全绿。本机连续跑了 3 次 pnpm test:e2e:gen-ui,也都通过。

需要说明的是,CI 只通过了一次,还不能证明之前的失败就是测量时序造成的。如果之后再失败,日志里会带上上面这些宽度数据。如果那时 viewport 已经是稳定宽度、按钮仍然换行,就说明是真的布局问题,我会去修布局,不会放宽断言。

@vastsa vastsa left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已复核最新 E2E 修订:按 URL 选择目标 guest,并等待实际 iframe 与 guest viewport 宽度同步,原有几何断言全部保留。此前已验证缺少 widget 容器的真实问题及默认 flex-row 回归;当前 flow-root 方案同时解决两者,保留片段自身布局。

已非破坏性合入最新 main c85251b 并推送 bfc9be4(maintainer edits 已开启)。该候选本机完整 GenUI Electron E2E 通过,覆盖真实 native Host、七种布局、交互、生命周期及跨会话隔离。复用现有依赖、renderer CSS 和 native binary,没有真实模型调用。初次 pnpm 自动安装因 symlink hoist 目录退出;改用 node 直接执行,补齐 shared/dist 复用链接后通过。语法及 diff 检查通过。

GitHub 实际合入候选与本地测试候选 tree 相同:122b58da39ca11db5a9f4cf69cc00f0c3aa24e1b。最终以本提交全部 CI 门禁通过为合入条件。旧单根包裹卡片保持原布局的取舍已记录;没有修改贡献者产品代码或放宽断言。

@vastsa
vastsa merged commit f5e3668 into vastsa:main Oct 31, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants