Skip to content

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

Keep the chart order steady in the view menu

Fei24116c10a8f52162
diff --git a/ds-bot/client.js b/ds-bot/client.js
index 9ead3a7f..fa98b9f8 100644
--- a/ds-bot/client.js
+++ b/ds-bot/client.js
@@ -5150,7 +5150,7 @@ function ChartTable({ data }) {
)
);
}
-function WidgetChart({ node, lookup, registerChart }) {
+function WidgetChart({ node, lookup, registerChart, refreshCharts }) {
const data = chartData(node, lookup);
const { kind, categories, series, unit } = data;
const box = (0, import_react6.useRef)(null);
@@ -5162,6 +5162,8 @@ function WidgetChart({ node, lookup, registerChart }) {
const [copied, setCopied] = (0, import_react6.useState)(false);
const labelId = (0, import_react6.useId)();
const menuKey = (0, import_react6.useId)();
+ const table = (0, import_react6.useRef)(asTable);
+ table.current = asTable;
const shown = series.map((_, index) => !hidden.includes(index));
const toggle = (index) => {
if (hidden.includes(index)) setHidden(hidden.filter((item) => item !== index));
@@ -5191,17 +5193,27 @@ function WidgetChart({ node, lookup, registerChart }) {
setTimeout(() => setCopied(false), 1200);
});
};
+ const toggleTable = () => {
+ table.current = !table.current;
+ setAsTable(table.current);
+ };
+ const menuLabel = typeof node.caption === "string" && node.caption !== "" ? node.caption : null;
const action = (0, import_react6.useRef)(null);
- action.current = { copy: copy2, save: () => {
+ action.current = { label: menuLabel, copy: copy2, save: () => {
if (svg.current) saveImage(svg.current, "chart.png");
- } };
+ }, toggleTable };
(0, import_react6.useEffect)(() => registerChart?.(menuKey, {
- label: typeof node.caption === "string" && node.caption !== "" ? node.caption : null,
- asTable,
- toggleTable: () => setAsTable((value) => !value),
+ get label() {
+ return action.current.label;
+ },
+ get asTable() {
+ return table.current;
+ },
+ toggleTable: () => action.current.toggleTable(),
copy: () => action.current.copy(),
save: () => action.current.save()
- }), [registerChart, menuKey, label, asTable]);
+ }), [registerChart, menuKey]);
+ (0, import_react6.useEffect)(() => refreshCharts?.(), [refreshCharts, asTable, menuLabel]);
const legend = isDonut ? (0, import_react6.createElement)("div", { className: "bt-chart-legend bt-chart-legend-list" }, categories.map((name, at) => (0, import_react6.createElement)(
"div",
{
@@ -5769,7 +5781,7 @@ function WidgetNode({ node, ctx }) {
case "table":
return (0, import_react8.createElement)(WidgetTable, { node, lookup });
case "chart":
- return (0, import_react8.createElement)(WidgetChart, { node, lookup, registerChart: ctx.registerChart });
+ return (0, import_react8.createElement)(WidgetChart, { node, lookup, registerChart: ctx.registerChart, refreshCharts: ctx.refreshCharts });
case "progress":
return (0, import_react8.createElement)(WidgetProgress, { node, lookup });
case "callout":
@@ -5798,7 +5810,7 @@ function WidgetNode({ node, ctx }) {
return null;
}
}
-function WidgetView({ view, onSubmit, settled = false, answer = null, busy = false, registerChart }) {
+function WidgetView({ view, onSubmit, settled = false, answer = null, busy = false, registerChart, refreshCharts }) {
const inputs = viewInputs(view);
const [values, setValues] = (0, import_react8.useState)(() => Object.fromEntries(inputs.map((node) => [node.id, inputDefault(node)])));
const sent = answer?.values?.inputs;
@@ -5814,25 +5826,30 @@ function WidgetView({ view, onSubmit, settled = false, answer = null, busy = fal
values: current,
locked: settled || busy,
setInput: (id, value) => setValues((before) => ({ ...before, [id]: value })),
- registerChart
+ registerChart,
+ refreshCharts
};
return (0, import_react8.createElement)(WidgetNode, { node: view, ctx });
}
function useChartHandles() {
const [, bump] = (0, import_react8.useState)(0);
const charts = (0, import_react8.useRef)(/* @__PURE__ */ new Map());
- const register = (0, import_react8.useRef)(null);
- if (register.current === null) {
- register.current = (key, handle) => {
- charts.current.set(key, handle);
- bump((value) => value + 1);
- return () => {
- charts.current.delete(key);
- bump((value) => value + 1);
- };
+ const fns = (0, import_react8.useRef)(null);
+ if (fns.current === null) {
+ const refresh = () => bump((value) => value + 1);
+ fns.current = {
+ registerChart: (key, handle) => {
+ charts.current.set(key, handle);
+ refresh();
+ return () => {
+ charts.current.delete(key);
+ refresh();
+ };
+ },
+ refreshCharts: refresh
};
}
- return { charts: [...charts.current.values()], registerChart: register.current };
+ return { charts: [...charts.current.values()], ...fns.current };
}
function ViewMenuList({ charts, pick: pick2 }) {
return (0, import_react8.createElement)(
@@ -5884,7 +5901,7 @@ function ViewMenu({ charts }) {
}
function WidgetCard({ title, view, onSubmit, onDismiss, busy = false, raised = false, settled = false, answer = null }) {
const titleId = (0, import_react8.useId)();
- const { charts, registerChart } = useChartHandles();
+ const { charts, registerChart, refreshCharts } = useChartHandles();
const framed = raised || hasControls(view);
const className = ["bt-view", framed ? "bt-q" : "bt-view-bare", settled ? "bt-q-settled" : "", raised ? "bt-q-raised" : ""].filter(Boolean).join(" ");
const head = title || onDismiss || countType(view, "chart") > 0 ? (0, import_react8.createElement)(
@@ -5904,7 +5921,7 @@ function WidgetCard({ title, view, onSubmit, onDismiss, busy = false, raised = f
"aria-busy": busy || void 0
},
head,
- (0, import_react8.createElement)("div", { className: "bt-view-body" }, (0, import_react8.createElement)(WidgetView, { view, onSubmit, settled, answer, busy, registerChart }))
+ (0, import_react8.createElement)("div", { className: "bt-view-body" }, (0, import_react8.createElement)(WidgetView, { view, onSubmit, settled, answer, busy, registerChart, refreshCharts }))
);
}
function PendingView({ sessionId, view, actions, inline }) {
diff --git a/ds-bot/src/client/widget-cell.js b/ds-bot/src/client/widget-cell.js
index c543ead7..25e6e9c4 100644
--- a/ds-bot/src/client/widget-cell.js
+++ b/ds-bot/src/client/widget-cell.js
@@ -299,7 +299,7 @@ function WidgetNode({ node, ctx }) {
}
case 'metric': return h(WidgetMetric, { node, lookup })
case 'table': return h(WidgetTable, { node, lookup })
- case 'chart': return h(WidgetChart, { node, lookup, registerChart: ctx.registerChart })
+ case 'chart': return h(WidgetChart, { node, lookup, registerChart: ctx.registerChart, refreshCharts: ctx.refreshCharts })
case 'progress': return h(WidgetProgress, { node, lookup })
case 'callout': return h(WidgetCallout, { node, lookup })
case 'sources': return h(WidgetSources, { node })
@@ -320,7 +320,7 @@ function WidgetNode({ node, ctx }) {
// them as they were sent. Every answer carries the inputs' values. Letter keys
// answer a lone options block from the keyboard; with more than one in a card the
// same letter would hit both.
-export function WidgetView({ view, onSubmit, settled = false, answer = null, busy = false, registerChart }) {
+export function WidgetView({ view, onSubmit, settled = false, answer = null, busy = false, registerChart, refreshCharts }) {
const inputs = viewInputs(view)
const [values, setValues] = useState(() => Object.fromEntries(inputs.map(node => [node.id, inputDefault(node)])))
const sent = answer?.values?.inputs
@@ -329,7 +329,7 @@ export function WidgetView({ view, onSubmit, settled = false, answer = null, bus
const ctx = {
onSubmit: submit, settled, answer, busy, keys: !settled && countType(view, 'options') === 1,
lookup: viewScope(view, current), values: current, locked: settled || busy,
- setInput: (id, value) => setValues(before => ({ ...before, [id]: value })), registerChart,
+ setInput: (id, value) => setValues(before => ({ ...before, [id]: value })), registerChart, refreshCharts,
}
return h(WidgetNode, { node: view, ctx })
}
@@ -340,15 +340,19 @@ export function WidgetView({ view, onSubmit, settled = false, answer = null, bus
function useChartHandles() {
const [, bump] = useState(0)
const charts = useRef(new Map())
- const register = useRef(null)
- if (register.current === null) {
- register.current = (key, handle) => {
- charts.current.set(key, handle)
- bump(value => value + 1)
- return () => { charts.current.delete(key); bump(value => value + 1) }
+ const fns = useRef(null)
+ if (fns.current === null) {
+ const refresh = () => bump(value => value + 1)
+ fns.current = {
+ registerChart: (key, handle) => {
+ charts.current.set(key, handle)
+ refresh()
+ return () => { charts.current.delete(key); refresh() }
+ },
+ refreshCharts: refresh,
}
}
- return { charts: [...charts.current.values()], registerChart: register.current }
+ return { charts: [...charts.current.values()], ...fns.current }
}
// The dropdown's items: one flat list for a lone chart, one labelled group per
@@ -386,7 +390,7 @@ function ViewMenu({ charts }) {
// view that still waits for its answer has a dismiss button.
function WidgetCard({ title, view, onSubmit, onDismiss, busy = false, raised = false, settled = false, answer = null }) {
const titleId = useId()
- const { charts, registerChart } = useChartHandles()
+ const { charts, registerChart, refreshCharts } = useChartHandles()
const framed = raised || hasControls(view)
const className = ['bt-view', framed ? 'bt-q' : 'bt-view-bare', settled ? 'bt-q-settled' : '', raised ? 'bt-q-raised' : ''].filter(Boolean).join(' ')
const head = title || onDismiss || countType(view, 'chart') > 0
@@ -399,7 +403,7 @@ function WidgetCard({ title, view, onSubmit, onDismiss, busy = false, raised = f
className, role: 'group', 'aria-labelledby': title ? titleId : undefined, 'aria-label': title ? undefined : t('View'), 'aria-busy': busy || undefined,
},
head,
- h('div', { className: 'bt-view-body' }, h(WidgetView, { view, onSubmit, settled, answer, busy, registerChart })))
+ h('div', { className: 'bt-view-body' }, h(WidgetView, { view, onSubmit, settled, answer, busy, registerChart, refreshCharts })))
}
// A view waiting for its answer, inline at its call or raised in the dock. Like a
diff --git a/ds-bot/src/client/widget-chart.js b/ds-bot/src/client/widget-chart.js
index 5dfe5ec0..b65d6baf 100644
--- a/ds-bot/src/client/widget-chart.js
+++ b/ds-bot/src/client/widget-chart.js
@@ -283,7 +283,7 @@ function ChartTable({ data }) {
series.map((item, index) => h('td', { key: index, className: 'bt-num' }, withUnit(item.values[at], series.length === 1 && !item.name ? '' : unit))))))))
}
-export function WidgetChart({ node, lookup, registerChart }) {
+export function WidgetChart({ node, lookup, registerChart, refreshCharts }) {
const data = chartData(node, lookup)
const { kind, categories, series, unit } = data
const box = useRef(null)
@@ -295,6 +295,8 @@ export function WidgetChart({ node, lookup, registerChart }) {
const [copied, setCopied] = useState(false)
const labelId = useId()
const menuKey = useId()
+ const table = useRef(asTable)
+ table.current = asTable
const shown = series.map((_, index) => !hidden.includes(index))
const toggle = (index) => {
if (hidden.includes(index)) setHidden(hidden.filter(item => item !== index))
@@ -320,17 +322,23 @@ export function WidgetChart({ node, lookup, registerChart }) {
const copy = () => {
void navigator.clipboard?.writeText(chartCsv(data)).then(() => { setCopied(true); setTimeout(() => setCopied(false), 1200) })
}
+ const toggleTable = () => { table.current = !table.current; setAsTable(table.current) }
+ const menuLabel = typeof node.caption === 'string' && node.caption !== '' ? node.caption : null
const action = useRef(null)
- action.current = { copy, save: () => { if (svg.current) saveImage(svg.current, 'chart.png') } }
- // The card's ⋯ menu shows the chart's actions; a new handle on every table
- // switch keeps its first item's wording current.
+ action.current = { label: menuLabel, copy, save: () => { if (svg.current) saveImage(svg.current, 'chart.png') }, toggleTable }
+ // The card's ⋯ menu shows the chart's actions. Register once on mount — a
+ // re-register would move the chart to the end of the card's group order —
+ // and let the getters hand the menu the current name and table state.
useEffect(() => registerChart?.(menuKey, {
- label: typeof node.caption === 'string' && node.caption !== '' ? node.caption : null,
- asTable,
- toggleTable: () => setAsTable(value => !value),
+ get label() { return action.current.label },
+ get asTable() { return table.current },
+ toggleTable: () => action.current.toggleTable(),
copy: () => action.current.copy(),
save: () => action.current.save(),
- }), [registerChart, menuKey, label, asTable])
+ }), [registerChart, menuKey])
+ // A table switch or a new caption asks the card for a fresh menu, without
+ // touching the registration order.
+ useEffect(() => refreshCharts?.(), [refreshCharts, asTable, menuLabel])
const legend = isDonut
? h('div', { className: 'bt-chart-legend bt-chart-legend-list' }, categories.map((name, at) => h('div', {
key: at, className: 'bt-chart-key', 'data-dim': active !== null && active !== at ? '' : undefined,
diff --git a/ds-bot/test/tool-cells.test.js b/ds-bot/test/tool-cells.test.js
index 76009316..f14b5434 100644
--- a/ds-bot/test/tool-cells.test.js
+++ b/ds-bot/test/tool-cells.test.js
@@ -13,7 +13,7 @@ const { outputFiles } = await build({
"export { QuestionDock } from './src/client/questions.js'",
"export { SecretDock } from './src/client/secret-card.js'",
"export { countCards } from './src/client/sources.js'",
- "export { ViewMenuList } from './src/client/widget-cell.js'",
+ "export { ViewMenuList, WidgetView } from './src/client/widget-cell.js'",
].join('\n'),
resolveDir: fileURLToPath(new URL('..', import.meta.url)),
},
@@ -46,7 +46,7 @@ runInNewContext(outputFiles[0].text, {
throw new Error(`Unexpected platform module ${name}`)
},
})
-const { ToolCell, TriggerCell, callTree, stackReplies, QuestionDock, SecretDock, countCards, ViewMenuList } = module.exports
+const { ToolCell, TriggerCell, callTree, stackReplies, QuestionDock, SecretDock, countCards, ViewMenuList, WidgetView } = module.exports
// Runs the effects collected since the last render; returns the cleanups.
const mount = () => {
@@ -690,6 +690,26 @@ test('a view works its formulas out, draws a chart, and keeps html in a sandboxe
'View as chart', 'Copy data', 'Download image',
'View as table', 'Copy data', 'Download image',
], 'each chart keeps its three actions and its table state')
+ // A table switch must not re-register the chart: the groups keep mount order,
+ // so the toggled chart stays first and its item reads back as the table (#476).
+ const ordered = []
+ expand(WidgetView({
+ view: {
+ type: 'stack',
+ children: [
+ { type: 'chart', kind: 'bar', categories: ['A'], series: [{ values: [1] }], caption: 'One' },
+ { type: 'chart', kind: 'bar', categories: ['A'], series: [{ values: [2] }], caption: 'Two' },
+ { type: 'chart', kind: 'bar', categories: ['A'], series: [{ values: [3] }] },
+ ],
+ },
+ registerChart: (key, handle) => { ordered.push(handle); return () => { ordered.splice(ordered.indexOf(handle), 1) } },
+ }))
+ mount()
+ assert.equal(ordered.length, 3)
+ ordered[0].toggleTable()
+ const orderedMenu = expand(ViewMenuList({ charts: ordered, pick: run => run }))
+ assert.deepEqual(findAll(orderedMenu, item => item.props?.className === 'bt-view-menu-label').map(text), ['One', 'Two', 'Chart 3'], 'the toggled chart keeps its group position')
+ assert.deepEqual(menuItems(orderedMenu).slice(0, 3), ['View as chart', 'Copy data', 'Download image'], 'the first group shows the table state')
const frame = find(cell({ type: 'html', html: '<b>hi</b>', summary: 'A greeting' }), item => item.type === 'iframe')
assert.equal(frame.props.sandbox, 'allow-scripts')