Skip to content

A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.

Polish chart views on the bench and record the third view design

Fei2411292ace2e83f8
diff --git a/docs/plans/2026-10-20-widget-views.md b/docs/plans/2026-10-20-widget-views.md
index 9ca27941..a31d8d44 100644
--- a/docs/plans/2026-10-20-widget-views.md
+++ b/docs/plans/2026-10-20-widget-views.md
@@ -1,7 +1,7 @@
# Bot 视图(widget views)
- 日期:2026-10-20
-- 相关:#386
+- 相关:#386、#468(第三版:图表、输入和公式、沙箱 html)
## 为什么
@@ -19,32 +19,78 @@ Bot 的回答现在只有 Markdown 文本。有些回答天生是结构化的:
- 蓝图是有上限的 JSON,可以被 `parts.js` 压成摘要进交接包;历史回放用同一份蓝图重渲染。
- 复用提问卡的整条回路:state 登记、ToolCell 内联画卡、隐藏回合浮到输入框上方、回答作为用户消息回注。视图卡是提问卡的泛化,但存储、回复格式各自独立,不与提问卡共用一个 map。
+第三版(2026-10-23,#468)在第二版上加三样东西,第二版的规则不变:
+
+- 图表和本地交互:目录加图表、输入控件和公式。用户拖滑块、改数字时,页面按 Bot 写好的公式当场重算,不调模型、不花 token。
+- 目录画不了的内容(自定义图形、小游戏、流程图)可以写成 `html` 组件,放进沙箱 iframe。常用的展示仍走蓝图:省 token、跟主题、有校验,出错时模型能拿到带路径的错误重写。
+- 外框:只展示的视图直接放在回答里,不带框;有输入或回答控件的视图带浅框。去掉「视图」标签,标题改成可选的小标题。
+
## 已定的行为
### 工具
-- `show_view`:在自己的聊天里给用户看一张视图卡。参数:`title`(一句话标题)、`view`(JSON 蓝图,见下)、`await_reply`(可选)。
-- 组合规则:蓝图里含交互组件(options/confirm/form)时必须 `await_reply: true`;纯展示蓝图配 `await_reply: true` 拒绝(回合会空等一张不能提交的卡)。缺省按蓝图中是否含交互组件推断;`exec.concludeTurn()` 按**生效后**的 `await_reply` 调用,在校验通过、state 落盘之后。
+- `show_view`:在自己的聊天里给用户看一个视图。参数:`title`(可选,小标题)、`view`(JSON 蓝图,见下)、`await_reply`(可选)。
+- 组合规则:蓝图里含回答组件(options/confirm/form/submit)时必须 `await_reply: true`;输入控件(slider/number/choice/toggle)不算回答组件;纯展示蓝图配 `await_reply: true` 拒绝(回合会空等一张不能提交的卡)。缺省按蓝图中是否含交互组件推断;`exec.concludeTurn()` 按**生效后**的 `await_reply` 调用,在校验通过、state 落盘之后。
- 群聊会话拒绝使用(和 `ask_user` 同一句提示)。
- 校验失败原子拒绝:不登记 state、不结束回合,工具结果给出可修复的错误(带路径和允许值,如 `view.children[2].type: unknown component "chart"; allowed: stack, text, …`;超限时带实测值和上限)。
- 上限:序列化后 8 KiB(UTF-8 字节,含 title)、组件树深 8 层、组件节点 64 个;表格行 ≤ 50、列 ≤ 8;form 字段 ≤ 6;options 选项 ≤ 8。嵌套的 JSON 结构不算深度,只算组件树。
-### 组件目录(第一版)
+### 组件目录
蓝图是一个组件节点:`{ type: string, …props, children?: node[] }`。
-- `stack`:纵向排列子节点(唯一容器),`children` 必填。
-- `text`:一段 Markdown 文本。不渲染原始 HTML;链接只 http(s),新窗口打开;不允许模型控制 style、事件、className 等 DOM 属性。
-- `metric`:`{ label, value, tone? }`。
-- `table`:`{ columns: [{ key, label }], rows: [[…]] }`,只读。
+排版:
+
+- `stack`:纵向排列子节点。`row`:横向排列,窄时换行。两者 `children` 必填(1–16 个)。最外层的容器可以带 `let: { 名字: "公式" }`。
+
+展示:
+
+- `text`:`{ text, tone?: muted }`,一段 Markdown。不渲染原始 HTML;链接只 http(s),新窗口打开;不允许模型控制 style、事件、className 等 DOM 属性。
+- `metric`:`{ label, value, tone?: good|warn|bad, caption? }`。
+- `table`:`{ columns: [{ key, label }], rows: [[…]] }`,只读。整列是数字时右对齐。
+- `chart`:`{ kind?: bar|hbar|line|area|donut, categories: [标签], series: [{ name?, values: [数字] }], unit?, caption? }`。每个系列的值和类别一样多;环形图只有一个系列。用 SVG 画在拿到的宽度上,颜色只用主题变量(第一个系列用强调色)。悬停或方向键显示一个类别的数值、其余变淡;多系列有图例,点一下隐藏或显示一个系列;右上角菜单:以表格查看、复制数据(CSV)、下载图片(PNG)。
+- `progress`:`{ label?, percent }` 或 `{ label?, value, max }`,可带 `valueLabel`(右侧文字,如「剩余 ¥10」)和 `tone`。
+- `callout`:`{ text, tone?: info|good|warn|bad }`,一段提示。
+- `sources`:`{ items: [{ label, url }] }`,来源小标签,只收 http(s),新窗口打开。
+- `html`:`{ html, summary, height? }`,见下面「沙箱 html」。
+
+输入(值只留在页面里,不发给 Bot,不结束回合):
+
+- `slider`:`{ id, label, min, max, step?, value, unit? }`。
+- `number`:`{ id, label, value, min?, max?, step?, unit? }`。
+- `choice`:`{ id, label, options: [{ label, value }], value? }`。四个以内的短选项并排成分段按钮,其余竖排。
+- `toggle`:`{ id, label, value? }`。
+
+回答(结束回合,答案是用户的下一条消息):
+
- `options`:`{ id, options: [{ label, description? }], multi? }`。单选按下即提交;多选切换勾选、出 Submit 按钮(和提问卡一致)。
- `confirm`:`{ id, confirmLabel?, cancelLabel? }`,按下即提交。
- `form`:`{ id, fields: [{ id, label, placeholder? }], submitLabel? }`,短文本输入框 + 提交按钮。说明里禁止用 form 收集密钥——密钥走 `request_secret`。
-- `progress`:`{ label?, percent }`,静态快照(没有后续更新操作)。
+- `submit`:`{ id, label? }`,把输入控件当前的值发给 Bot(「发给 Bot」)。
+
+标识约束:交互组件和输入的 `id` 在整棵树里唯一;输入的 `id` 必须是公式能引用的名字(字母、数字、下划线,不以数字开头);form 内字段 `id` 在该 form 内唯一;options 的选项以 label 为身份(和提问卡一致);choice 的选项值唯一。
+
+### 公式
+
+- 写法:`{ "=": "表达式" }`。能写公式的属性:metric 的 value、caption、tone;text 的 text;progress 的 percent、value、max、valueLabel、tone;callout 的 text、tone;表格单元格;图表的每个值。
+- 表达式只读输入的 `id` 和 `let` 里的名字:数字、'文字'、`+ - * / %`、比较、`&& || !`、`a ? b : c`,以及 `min max abs floor ceil sqrt pow round clamp fixed format percent`。没有属性访问、赋值、循环,碰不到页面。解析和计算在 `src/shared/formula.js`,主机和界面共用。
+- 主机按 Bot 设的初始值把每个公式算一遍:解析失败、用了不存在的名字、`let` 互相引用、结果类型不对(如 tone 不在允许值里),都原样拒绝并给出路径。
+- 页面上每次输入变化都重算;某个公式在用户的值下算不出来时,这一处显示占位,不影响别处。
+
+### 沙箱 html
+
+- 只在目录画不了时用;`summary` 必填,交接和摘要只用它。
+- 放进 `sandbox="allow-scripts"` 的 iframe(`srcdoc`),不给 same-origin:碰不到应用、存储和接口。文档开头先写禁止联网的 CSP(`default-src 'none'`,只放行内联脚本和样式、`data:` 图片和字体),再放模型的内容。
+- 注入主题变量(`--bt-ink`、`--bt-line`、`--bt-accent` 等)和配色方案;切换明暗时发消息更新。框里的脚本只能报告自己的高度,iframe 跟着变高。
+- iframe 里没有提交通道,不能回答 Bot,也不能结束回合;它的状态不保存。
+- iframe 跳到别的页面时(第二次 load),换成一句提示,不再显示。
+
+### 外框
-标识约束:交互组件的 `id` 在整棵树里唯一;form 内字段 `id` 在该 form 内唯一;options 的选项以 label 为身份(和提问卡一致)。
+- 只展示的视图不带框,直接接在回答里,宽度跟正文一样;有输入或回答控件的视图带浅框;浮到输入框上方的待答视图保持凸起样式。
+- 没有「视图」标签;`title` 有值时显示成一行小标题。
-一张视图可含多个交互组件;**一次提交结算整张卡**:只带触发组件的值 `values`(options:`{ selected: string[] }`;confirm:`{ confirmed: boolean }`;form:`{ fields: { [fieldId]: string } }`),其他组件未提交的输入丢弃,已答渲染里它们的控件不再可用。空提交:options 至少一项、confirm 必有一个按钮值、form 允许全空字段。文本值上限 2,000 字。
+一张视图可含多个交互组件;**一次提交结算整张卡**:只带触发组件的值 `values`(options:`{ selected: string[] }`;confirm:`{ confirmed: boolean }`;form:`{ fields: { [fieldId]: string } }`;submit:`{}`),其他组件未提交的输入丢弃,已答渲染里它们的控件不再可用。视图有输入控件时,任何一次提交都带上 `inputs: { [inputId]: 值 }`:没发来的按初始值补齐,服务端逐个校验(范围、选项值、布尔)。已答视图按发出时的值显示,控件只读。空提交:options 至少一项、confirm 必有一个按钮值、form 允许全空字段。文本值上限 2,000 字。
### 状态与生命周期
@@ -63,21 +109,23 @@ Bot 的回答现在只有 Markdown 文本。有些回答天生是结构化的:
### 压缩与交接
-- `viewCardText`(和 `cardText` 同构):`[View card] 标题` + 一行组件清单(带关键数值/选项,如 `metric: 进度=70%; options: A/B/C`,每个组件至多 40 字、至多 8 个)+ 已答时的答案摘要。已答摘要从回复事件取,不从 state 取。
-- 纯展示视图同样压成一行摘要。
+- `viewCardText`(和 `cardText` 同构):`[View card] 标题`(没有标题时只有 `[View card]`)+ 一行组件清单(带关键数值/选项,如 `metric: 进度=70%; options: A/B/C`,每个组件至多 40 字、至多 8 个)+ 已答时的答案摘要。已答摘要从回复事件取,不从 state 取。
+- 纯展示视图同样压成一行摘要。公式按初始值算出结果再写进摘要;输入写成 `slider: 标签=值`;图表写类别和第一个系列的值;`html` 写它的 `summary`。
+- 答案摘要里,输入写成 `标签=值`(choice 写选项标签)。
## 不做什么
-- 不渲染模型生成的 HTML/JS,不引入 iframe;不嵌入外部页面。
+- 模型生成的 HTML/JS 只在 `html` 组件的沙箱 iframe 里跑;其余组件不执行模型代码。不嵌入外部页面。
- 不自建 MCP Apps host;DSH 官方支持 SEP-1865 后再评估把 connectors 的 MCP UI 接进来。
-- 第一版不做图表组件(chart)。
- 不引入 OpenUI Lang。它(Apache-2.0、渐进解析渲染、比 JSON 省约 2/3 token)的真实优势是蓝图随 token 流式出现;本设计等工具参数完整并校验通过才渲染,接近上限的大蓝图会有可感的首屏延迟。8 个组件的小卡片暂不值这个解析器;实测接近上限的调用后重新评估。
-- 视图不进群聊。
+- 视图不进群聊。群里没有画视图的地方(群页面读的是 DS Bot 自己的群记录,每条只有文字),交互视图还会和「谁来回答、按顺序发言」的群聊规则冲突,要单独设计。
+- 用户拖过的值不保存:卸载后回到 Bot 设的初始值;Bot 默认不知道,用户按 `submit` 才发过去。
## 已知限制
-- 蓝图保得住,**填到一半的表单保不住**:虚拟滚动或换段卸载后,未提交的输入回到初始值。
-- 表达力受目录限制:目录外的展示(自定义图形、地图、编辑器)做不到。
+- 蓝图保得住,**填到一半的表单保不住**:虚拟滚动或换段卸载后,未提交的输入、拖过的值、html 框里的状态都回到初始值。
+- 目录外的展示要写成 `html`:比蓝图费 token,样式靠模型照着主题变量写,脚本出错时模型收不到错误。
+- `html` 框禁止联网,但挡不住它把自己导航到外部地址(数据可以跟着网址出去);导航发生后框被换成提示,请求本身已经发出。
- 蓝图质量依赖模型按 schema 输出的能力;校验拒绝消耗一次工具调用。
- 已答视图在会话压缩后只剩一行摘要,Bot 看不到当时的完整蓝图。
- 8 KiB 上限是按 DSH 会话日志里 `ask_user_question` 参数的现有形状定的起点,未在真实持久化日志里验证过 DSH/供应商的硬上限;冒烟测试要实测接近上限和超限的调用。
@@ -87,3 +135,4 @@ Bot 的回答现在只有 Markdown 文本。有些回答天生是结构化的:
- 2026-10-20:初稿(声明式目录,第二版)。评审:Antigravity 否决第一版 iframe 方案;Codex 一审(ship with changes):state.views 独立、已答视图在回复节点、交互组合规则、未提交输入的限制、摘要带关键值。
- 2026-10-20:Codex 二审(ship with changes):concludeTurn 绑定生效后的 await_reply;回复格式改判别字段双分支;标识唯一性和 values 形状定死;首次提交结算整张卡;views 与 questions 共存规则写明。
- 2026-10-20:Codex 三审签字(ship,无需再改)。
+- 2026-10-23:第三版(#468):图表、输入控件和公式、row/callout/sources、沙箱 html;只展示的视图去掉外框和「视图」标签,标题可选。
diff --git a/docs/plans/README.md b/docs/plans/README.md
index 8429b188..4c229aca 100644
--- a/docs/plans/README.md
+++ b/docs/plans/README.md
@@ -45,6 +45,6 @@
| [长消息](2026-10-16-long-messages.md) | state.json 只存开头;超过 2 万字的 Bot 消息转成 DSH 文件;成员和界面怎么读全文 |
| [桌面应用](2026-10-16-desktop-app.md) | 壳、引擎和界面怎么分;锁定的 DSH 版本、独立的数据位置、首次启动和导入、插件怎么兼容 DSH、安装和更新 |
| [Bot 之间派活](2026-10-16-bot-handoffs.md) | Bot 看得到对方的状态;没读的消息合成一条;派活树:下面的活交回后才往上交,不许发给在等结果的 Bot;不设总量和链长上限 |
-| [Bot 视图](2026-10-20-widget-views.md) | Bot 在聊天里画结构化的视图卡:固定组件目录的 JSON 蓝图,自己渲染;提问卡回路的泛化 |
+| [Bot 视图](2026-10-20-widget-views.md) | Bot 在回答里画视图:固定组件目录的 JSON 蓝图(含图表、输入和本地公式),目录外的放沙箱 html;提问卡回路的泛化 |
| [旁路问答(btw)](2026-10-21-btw.md) | 人顺带问一句:以与会话同构的请求做一次不执行工具的回答,不打断、不进模型可见历史;Bot 之间的 `ask_bot` 另行提案 |
| [消息布局和宽度](2026-10-22-message-layout.md) | 气泡 / 平铺两种布局;平铺时 Bot 的话带头像、名字、时间;气泡宽度和平铺正文宽度各用一个滑块,按正文列的百分比调 |
diff --git a/ds-bot/client.js b/ds-bot/client.js
index 8720b33a..0883475f 100644
--- a/ds-bot/client.js
+++ b/ds-bot/client.js
@@ -5219,7 +5219,8 @@ function WidgetChart({ node, lookup }) {
setActive((current) => current === null ? 0 : (current + keys[event.key] + categories.length) % categories.length);
};
const point = active === null ? null : drawn.points[active];
- const tipRows = isDonut ? active === null ? [] : [{ color: colorAt(active), name: "", value: `${withUnit(drawn.values[active], unit)} · ${drawn.total > 0 ? Math.round(drawn.values[active] / drawn.total * 100) : 0}%` }] : active === null ? [] : series.filter((_, index) => shown[index]).map((item) => ({ color: item.color, name: item.name, value: withUnit(item.values[active], unit) }));
+ const share = (at) => unit === "%" ? withUnit(drawn.values[at], unit) : `${withUnit(drawn.values[at], unit)} · ${drawn.total > 0 ? Math.round(drawn.values[at] / drawn.total * 100) : 0}%`;
+ const tipRows = active === null || isDonut ? [] : series.filter((_, index) => shown[index]).map((item) => ({ color: item.color, name: item.name, value: withUnit(item.values[active], unit) }));
const copy2 = () => {
void navigator.clipboard?.writeText(chartCsv(data)).then(() => {
setCopied(true);
@@ -5237,7 +5238,7 @@ function WidgetChart({ node, lookup }) {
},
(0, import_react6.createElement)("i", { style: { background: colorAt(at) } }),
(0, import_react6.createElement)("span", { className: "bt-chart-key-name" }, name),
- (0, import_react6.createElement)("span", { className: "bt-chart-key-value" }, `${withUnit(drawn.values[at], unit)} · ${drawn.total > 0 ? Math.round(drawn.values[at] / drawn.total * 100) : 0}%`)
+ (0, import_react6.createElement)("span", { className: "bt-chart-key-value" }, share(at))
))) : series.length > 1 ? (0, import_react6.createElement)("div", { className: "bt-chart-legend" }, series.map((item, index) => (0, import_react6.createElement)("button", {
key: index,
type: "button",
@@ -5264,9 +5265,14 @@ function WidgetChart({ node, lookup }) {
onPointerLeave: () => setActive(null)
},
drawn.parts,
- isDonut ? (0, import_react6.createElement)("text", { className: "bt-chart-center", x: svgWidth / 2, y: drawn.height / 2 + 5, textAnchor: "middle" }, active === null ? withUnit(drawn.total, unit) : fit(categories[active], drawn.size * 0.55, 15)) : drawn.hits.map((hit, at) => (0, import_react6.createElement)("rect", { key: `h${at}`, className: "bt-chart-hit", x: hit.x, y: hit.y, width: hit.w, height: hit.h, onPointerEnter: () => setActive(at) }))
+ isDonut ? active === null ? (0, import_react6.createElement)("text", { className: "bt-chart-center", x: svgWidth / 2, y: drawn.height / 2 + 5, textAnchor: "middle" }, unit === "%" ? "" : withUnit(drawn.total, unit)) : (0, import_react6.createElement)(
+ import_react6.Fragment,
+ null,
+ (0, import_react6.createElement)("text", { className: "bt-chart-center-name", x: svgWidth / 2, y: drawn.height / 2 - 4, textAnchor: "middle" }, fit(categories[active], drawn.size * 0.56, LABEL)),
+ (0, import_react6.createElement)("text", { className: "bt-chart-center", x: svgWidth / 2, y: drawn.height / 2 + 14, textAnchor: "middle" }, share(active))
+ ) : drawn.hits.map((hit, at) => (0, import_react6.createElement)("rect", { key: `h${at}`, className: "bt-chart-hit", x: hit.x, y: hit.y, width: hit.w, height: hit.h, onPointerEnter: () => setActive(at) }))
),
- point && !asTable ? (0, import_react6.createElement)(
+ point && !asTable && !isDonut ? (0, import_react6.createElement)(
"div",
{ className: "bt-chart-tip", role: "status", style: { left: point.x, top: point.y } },
(0, import_react6.createElement)("b", null, categories[active]),
@@ -5276,7 +5282,7 @@ function WidgetChart({ node, lookup }) {
const body = asTable ? (0, import_react6.createElement)(ChartTable, { data }) : isDonut ? (0, import_react6.createElement)("div", { className: legendBeside ? "bt-chart-donut bt-chart-donut-beside" : "bt-chart-donut", onPointerLeave: () => setActive(null) }, plot, legend) : (0, import_react6.createElement)(import_react6.Fragment, null, legend, plot);
return (0, import_react6.createElement)(
"figure",
- { ref: box, className: "bt-chart", "data-kind": kind },
+ { ref: box, className: "bt-chart", "data-kind": kind, "data-table": asTable ? "" : void 0 },
(0, import_react6.createElement)("span", { id: labelId, hidden: true }, label),
(0, import_react6.createElement)(ChartMenu, { asTable, onTable: () => setAsTable((value) => !value), onCopy: copy2, onSave: () => {
if (svg.current) saveImage(svg.current, "chart.png");
@@ -5292,12 +5298,14 @@ var CHART_CSS = `
.bt-chart{position:relative;margin:0;min-width:0;display:flex;flex-direction:column;gap:8px}
.bt-chart-plot{position:relative;min-width:0}
.bt-chart-svg{display:block;overflow:visible;outline:none;font-family:inherit}
+.bt-chart-svg text{pointer-events:none}
.bt-chart-svg:focus-visible{outline:2px solid var(--bt-accent);outline-offset:4px;border-radius:4px}
.bt-chart-grid{stroke:var(--bt-line);stroke-width:1;stroke-dasharray:3 3}
.bt-chart-tick{font-size:${TICK}px;fill:var(--bt-ink-3);font-variant-numeric:tabular-nums}
.bt-chart-label{font-size:${LABEL}px;fill:var(--bt-ink-2)}
.bt-chart-value{font-size:${LABEL}px;fill:var(--bt-ink-2);font-variant-numeric:tabular-nums}
.bt-chart-center{font-size:15px;font-weight:600;fill:var(--bt-ink);font-variant-numeric:tabular-nums}
+.bt-chart-center-name{font-size:${LABEL}px;fill:var(--bt-ink-2)}
.bt-chart-mark{transition:opacity .12s ease}
.bt-chart-mark[data-dim]{opacity:.28}
.bt-chart-line{fill:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:round}
@@ -5323,6 +5331,7 @@ var CHART_CSS = `
.bt-chart-donut-beside .bt-chart-legend-list{flex:1 1 auto;max-width:280px}
.bt-chart-tools{position:absolute;top:-4px;right:0;z-index:5;opacity:0;transition:opacity .12s ease}
.bt-chart:hover .bt-chart-tools,.bt-chart-tools:focus-within,.bt-chart-tools[data-open]{opacity:1}
+.bt-chart[data-table] .bt-chart-tools{position:relative;top:0;align-self:flex-end;order:-1;margin-bottom:-4px;opacity:1}
@media (hover:none){.bt-chart-tools{opacity:1}}
.bt-chart-more{width:28px;height:28px;padding:0;border:0;border-radius:50%;background:none;color:var(--bt-ink-2);display:flex;align-items:center;justify-content:center;cursor:pointer;transition:background-color .12s ease,color .12s ease}
.bt-chart-more:hover,.bt-chart-tools[data-open] .bt-chart-more{background:var(--bt-hover);color:var(--bt-ink)}
@@ -5332,7 +5341,7 @@ var CHART_CSS = `
.bt-menu.bt-chart-menu button{gap:8px}
.bt-chart-copied{position:absolute;top:2px;right:36px;font-size:12px;line-height:16px;color:var(--bt-ink-2);animation:bt-veil-in .16s ease-out}
.bt-widget-table .bt-num{text-align:end;font-variant-numeric:tabular-nums}
-.bt-widget-table tbody th{padding:6px 10px;font-weight:400;text-align:start;color:var(--bt-ink)}
+.bt-widget-table tbody th{padding:6px 10px;font-weight:400;text-align:start;color:var(--bt-ink);border-bottom:0}
@media (prefers-reduced-motion:reduce){.bt-chart-mark,.bt-chart-dot,.bt-chart-tools{transition:none}.bt-chart-tip{animation:none}}
`;
@@ -6023,7 +6032,7 @@ var WIDGET_CSS = `
.bt-q-raised .bt-view-head{padding:14px 16px 4px 20px}
.bt-q-raised .bt-view-body{padding:0 16px 14px 20px}
.bt-q-raised .bt-view-body .bt-q-list{padding:0;margin:0 -8px}
-.bt-q-inline .bt-view{margin:0;width:100%;max-width:min(calc(var(--bt-msg-w,.8) * 100%),calc(100% - 82px));order:0}
+.bt-q-inline .bt-view{margin:0;width:100%;max-width:min(100%,max(calc(var(--bt-msg-w,.8) * 100%),560px));order:0}
body[data-bt-layout=flat] .bt-q-inline .bt-view{max-width:calc(var(--bt-flat-w,1) * 100%)}
.bt-q-inline:has(>.bt-view-bare){margin:8px 0 12px}
@media (prefers-reduced-motion:reduce){.bt-widget-progress-fill,.bt-view-radio-dot{transition:none}.bt-view-bare{animation:none}}
diff --git a/ds-bot/dev/mock-llm.mjs b/ds-bot/dev/mock-llm.mjs
index a868e3da..7d30f5eb 100644
--- a/ds-bot/dev/mock-llm.mjs
+++ b/ds-bot/dev/mock-llm.mjs
@@ -237,6 +237,7 @@ function decide(body) {
// The user's answer can ride in the same message as the card's tool result.
case 'ask_user': if (textOf(last.content).trim() === '') return [text('')]
break
+ case 'show_view': return [text(resultText.startsWith('The view is on screen') ? '这些都是示例数据,换成你的数据我再画一次。' : `没画成:${resultText}`)]
case 'ask_user_question': {
let picked = ''
try { picked = JSON.parse(resultText).answers?.[0]?.selected?.[0] ?? '' } catch {}
@@ -509,6 +510,48 @@ function decide(body) {
// blueprint just under the byte limit.
return [tool('show_view', { title: 'A long note', view: { type: 'stack', children: [0, 1, 2, 3].map(() => ({ type: 'text', text: 'x'.repeat(1900) })) }, await_reply: false })]
}
+ // 视图图表, 视图计算器 and 视图网页 cover the chart kinds, inputs with formulas
+ // and a submit, and the sandboxed html frame.
+ if (/^\s*视图图表/.test(userText) && tools.has('show_view')) {
+ return [text('B 最快,D 最慢。下面是四个模型的首字延迟(示例数据):'), tool('show_view', {
+ view: {
+ type: 'stack',
+ children: [
+ { type: 'chart', kind: 'bar', categories: ['Model A', 'Model B', 'Model C', 'Model D'], series: [{ name: 'p50', values: [420, 310, 520, 680] }, { name: 'p95', values: [690, 540, 810, 1150] }], unit: 'ms', caption: 'Time to first token, sample data' },
+ { type: 'row', children: [{ type: 'chart', kind: 'line', categories: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'], series: [{ name: 'Requests', values: [120, 180, 160, 240, 210] }] }, { type: 'chart', kind: 'donut', categories: ['Cached', 'Fresh', 'Failed'], series: [{ values: [62, 33, 5] }], unit: '%' }] },
+ { type: 'sources', items: [{ label: 'Bench notes', url: 'https://example.com/bench' }] },
+ ],
+ },
+ })]
+ }
+ if (/^\s*视图计算器/.test(userText) && tools.has('show_view')) {
+ return [text('拖一下滑块看看每月要花多少:'), tool('show_view', {
+ title: 'Monthly cost',
+ view: {
+ type: 'stack',
+ let: { tokens: 'req * 30 * 2000 / 1e6', cost: 'tokens * (1 - hit) * 2 + tokens * hit * 0.5' },
+ children: [
+ { type: 'slider', id: 'req', label: 'Requests a day', min: 100, max: 5000, step: 100, value: 1200 },
+ { type: 'choice', id: 'hit', label: 'Cache hits', options: [{ label: 'Low', value: 0.2 }, { label: 'Mid', value: 0.5 }, { label: 'High', value: 0.8 }], value: 0.5 },
+ { type: 'row', children: [
+ { type: 'metric', label: 'Cost a month', value: { '=': "'¥' + format(cost, 0)" }, tone: { '=': "cost > 100 ? 'bad' : 'good'" } },
+ { type: 'metric', label: 'Tokens a month', value: { '=': "format(tokens, 1) + ' M'" } },
+ ] },
+ { type: 'progress', label: 'Basic plan (¥100)', value: { '=': 'min(cost, 100)' }, max: 100, valueLabel: { '=': "cost > 100 ? '¥' + format(cost - 100, 0) + ' over' : '¥' + format(100 - cost, 0) + ' left'" }, tone: { '=': "cost > 100 ? 'bad' : cost > 80 ? 'warn' : 'good'" } },
+ { type: 'callout', text: 'Assumes 2,000 tokens a request and list prices.', tone: 'info' },
+ { type: 'submit', id: 'send', label: 'Send to Bot' },
+ ],
+ },
+ })]
+ }
+ if (/^\s*视图网页/.test(userText) && tools.has('show_view')) {
+ return [tool('show_view', {
+ view: {
+ type: 'html', summary: 'Six seats, two taken', height: 96,
+ html: '<style>.g{display:grid;grid-template-columns:repeat(6,1fr);gap:8px}.s{height:40px;border-radius:8px;background:var(--bt-hover);border:0;color:var(--bt-ink);font:inherit;cursor:pointer}.s[data-on]{background:var(--bt-accent);color:#fff}.s:disabled{opacity:.4}</style><div class="g"></div><p id="n" style="color:var(--bt-ink-2);margin:8px 0 0">Pick a seat</p><script>const g=document.querySelector(".g");for(let i=1;i<=6;i++){const b=document.createElement("button");b.className="s";b.textContent="A"+i;if(i===2||i===5)b.disabled=true;b.onclick=()=>{b.toggleAttribute("data-on");document.getElementById("n").textContent=[...g.querySelectorAll("[data-on]")].map(x=>x.textContent).join(", ")||"Pick a seat"};g.append(b)}</script>',
+ },
+ })]
+ }
// Streams slowly, then checks the team: exercises the activity row stepping aside
// for a streaming reply and coming back for the tool.
if (/^\s*慢慢说/.test(userText) && tools.has('list_bots')) {
diff --git a/ds-bot/src/client/widget-cell.js b/ds-bot/src/client/widget-cell.js
index 3359d6c8..51fcaef3 100644
--- a/ds-bot/src/client/widget-cell.js
+++ b/ds-bot/src/client/widget-cell.js
@@ -519,7 +519,7 @@ export const WIDGET_CSS = `
.bt-q-raised .bt-view-head{padding:14px 16px 4px 20px}
.bt-q-raised .bt-view-body{padding:0 16px 14px 20px}
.bt-q-raised .bt-view-body .bt-q-list{padding:0;margin:0 -8px}
-.bt-q-inline .bt-view{margin:0;width:100%;max-width:min(calc(var(--bt-msg-w,.8) * 100%),calc(100% - 82px));order:0}
+.bt-q-inline .bt-view{margin:0;width:100%;max-width:min(100%,max(calc(var(--bt-msg-w,.8) * 100%),560px));order:0}
body[data-bt-layout=flat] .bt-q-inline .bt-view{max-width:calc(var(--bt-flat-w,1) * 100%)}
.bt-q-inline:has(>.bt-view-bare){margin:8px 0 12px}
@media (prefers-reduced-motion:reduce){.bt-widget-progress-fill,.bt-view-radio-dot{transition:none}.bt-view-bare{animation:none}}
diff --git a/ds-bot/src/client/widget-chart.js b/ds-bot/src/client/widget-chart.js
index a09cc15d..30a80856 100644
--- a/ds-bot/src/client/widget-chart.js
+++ b/ds-bot/src/client/widget-chart.js
@@ -334,9 +334,9 @@ export function WidgetChart({ node, lookup }) {
setActive(current => (current === null ? 0 : (current + keys[event.key] + categories.length) % categories.length))
}
const point = active === null ? null : drawn.points[active]
- const tipRows = isDonut
- ? (active === null ? [] : [{ color: colorAt(active), name: '', value: `${withUnit(drawn.values[active], unit)} · ${drawn.total > 0 ? Math.round((drawn.values[active] / drawn.total) * 100) : 0}%` }])
- : (active === null ? [] : series.filter((_, index) => shown[index]).map(item => ({ color: item.color, name: item.name, value: withUnit(item.values[active], unit) })))
+ // A share in a donut already in percent needs no second percent.
+ const share = at => (unit === '%' ? withUnit(drawn.values[at], unit) : `${withUnit(drawn.values[at], unit)} · ${drawn.total > 0 ? Math.round((drawn.values[at] / drawn.total) * 100) : 0}%`)
+ const tipRows = active === null || isDonut ? [] : series.filter((_, index) => shown[index]).map(item => ({ color: item.color, name: item.name, value: withUnit(item.values[active], unit) }))
const copy = () => {
void navigator.clipboard?.writeText(chartCsv(data)).then(() => { setCopied(true); setTimeout(() => setCopied(false), 1200) })
}
@@ -345,7 +345,7 @@ export function WidgetChart({ node, lookup }) {
key: at, className: 'bt-chart-key', 'data-dim': active !== null && active !== at ? '' : undefined,
onPointerEnter: () => setActive(at), onPointerLeave: () => setActive(null),
}, h('i', { style: { background: colorAt(at) } }), h('span', { className: 'bt-chart-key-name' }, name),
- h('span', { className: 'bt-chart-key-value' }, `${withUnit(drawn.values[at], unit)} · ${drawn.total > 0 ? Math.round((drawn.values[at] / drawn.total) * 100) : 0}%`))))
+ h('span', { className: 'bt-chart-key-value' }, share(at)))))
: series.length > 1 ? h('div', { className: 'bt-chart-legend' }, series.map((item, index) => h('button', {
key: index, type: 'button', className: 'bt-chart-key', 'aria-pressed': shown[index], onClick: () => toggle(index),
}, h('i', { style: { background: item.color } }), item.name || `${t('Series')} ${index + 1}`))) : null
@@ -357,15 +357,19 @@ export function WidgetChart({ node, lookup }) {
},
drawn.parts,
isDonut
- ? h('text', { className: 'bt-chart-center', x: svgWidth / 2, y: drawn.height / 2 + 5, textAnchor: 'middle' }, active === null ? withUnit(drawn.total, unit) : fit(categories[active], drawn.size * 0.55, 15))
+ ? (active === null
+ ? h('text', { className: 'bt-chart-center', x: svgWidth / 2, y: drawn.height / 2 + 5, textAnchor: 'middle' }, unit === '%' ? '' : withUnit(drawn.total, unit))
+ : h(Fragment, null,
+ h('text', { className: 'bt-chart-center-name', x: svgWidth / 2, y: drawn.height / 2 - 4, textAnchor: 'middle' }, fit(categories[active], drawn.size * 0.56, LABEL)),
+ h('text', { className: 'bt-chart-center', x: svgWidth / 2, y: drawn.height / 2 + 14, textAnchor: 'middle' }, share(active))))
: drawn.hits.map((hit, at) => h('rect', { key: `h${at}`, className: 'bt-chart-hit', x: hit.x, y: hit.y, width: hit.w, height: hit.h, onPointerEnter: () => setActive(at) }))),
- point && !asTable ? h('div', { className: 'bt-chart-tip', role: 'status', style: { left: point.x, top: point.y } },
+ point && !asTable && !isDonut ? h('div', { className: 'bt-chart-tip', role: 'status', style: { left: point.x, top: point.y } },
h('b', null, categories[active]),
tipRows.map((row, index) => h('span', { key: index }, h('i', { style: { background: row.color } }), row.name ? `${row.name} ` : '', row.value))) : null)
const body = asTable ? h(ChartTable, { data })
: isDonut ? h('div', { className: legendBeside ? 'bt-chart-donut bt-chart-donut-beside' : 'bt-chart-donut', onPointerLeave: () => setActive(null) }, plot, legend)
: h(Fragment, null, legend, plot)
- return h('figure', { ref: box, className: 'bt-chart', 'data-kind': kind },
+ return h('figure', { ref: box, className: 'bt-chart', 'data-kind': kind, 'data-table': asTable ? '' : undefined },
h('span', { id: labelId, hidden: true }, label),
h(ChartMenu, { asTable, onTable: () => setAsTable(value => !value), onCopy: copy, onSave: () => { if (svg.current) saveImage(svg.current, 'chart.png') } }),
body,
@@ -379,12 +383,14 @@ export const CHART_CSS = `
.bt-chart{position:relative;margin:0;min-width:0;display:flex;flex-direction:column;gap:8px}
.bt-chart-plot{position:relative;min-width:0}
.bt-chart-svg{display:block;overflow:visible;outline:none;font-family:inherit}
+.bt-chart-svg text{pointer-events:none}
.bt-chart-svg:focus-visible{outline:2px solid var(--bt-accent);outline-offset:4px;border-radius:4px}
.bt-chart-grid{stroke:var(--bt-line);stroke-width:1;stroke-dasharray:3 3}
.bt-chart-tick{font-size:${TICK}px;fill:var(--bt-ink-3);font-variant-numeric:tabular-nums}
.bt-chart-label{font-size:${LABEL}px;fill:var(--bt-ink-2)}
.bt-chart-value{font-size:${LABEL}px;fill:var(--bt-ink-2);font-variant-numeric:tabular-nums}
.bt-chart-center{font-size:15px;font-weight:600;fill:var(--bt-ink);font-variant-numeric:tabular-nums}
+.bt-chart-center-name{font-size:${LABEL}px;fill:var(--bt-ink-2)}
.bt-chart-mark{transition:opacity .12s ease}
.bt-chart-mark[data-dim]{opacity:.28}
.bt-chart-line{fill:none;stroke-width:2;stroke-linecap:round;stroke-linejoin:round}
@@ -410,6 +416,7 @@ export const CHART_CSS = `
.bt-chart-donut-beside .bt-chart-legend-list{flex:1 1 auto;max-width:280px}
.bt-chart-tools{position:absolute;top:-4px;right:0;z-index:5;opacity:0;transition:opacity .12s ease}
.bt-chart:hover .bt-chart-tools,.bt-chart-tools:focus-within,.bt-chart-tools[data-open]{opacity:1}
+.bt-chart[data-table] .bt-chart-tools{position:relative;top:0;align-self:flex-end;order:-1;margin-bottom:-4px;opacity:1}
@media (hover:none){.bt-chart-tools{opacity:1}}
.bt-chart-more{width:28px;height:28px;padding:0;border:0;border-radius:50%;background:none;color:var(--bt-ink-2);display:flex;align-items:center;justify-content:center;cursor:pointer;transition:background-color .12s ease,color .12s ease}
.bt-chart-more:hover,.bt-chart-tools[data-open] .bt-chart-more{background:var(--bt-hover);color:var(--bt-ink)}
@@ -419,6 +426,6 @@ export const CHART_CSS = `
.bt-menu.bt-chart-menu button{gap:8px}
.bt-chart-copied{position:absolute;top:2px;right:36px;font-size:12px;line-height:16px;color:var(--bt-ink-2);animation:bt-veil-in .16s ease-out}
.bt-widget-table .bt-num{text-align:end;font-variant-numeric:tabular-nums}
-.bt-widget-table tbody th{padding:6px 10px;font-weight:400;text-align:start;color:var(--bt-ink)}
+.bt-widget-table tbody th{padding:6px 10px;font-weight:400;text-align:start;color:var(--bt-ink);border-bottom:0}
@media (prefers-reduced-motion:reduce){.bt-chart-mark,.bt-chart-dot,.bt-chart-tools{transition:none}.bt-chart-tip{animation:none}}
`