Skip to content

Commit 4a7abe1

Browse files
Workspaces: add trusted chart widgets (#101792)
* feat(workspaces): add trusted chart widgets * fix(workspaces): keep chart helpers private * test(workspaces): prove chart widgets in control UI * fix(workspaces): validate empty chart bounds * fix(workspaces): expand subnormal chart ranges * fix(workspaces): normalize chart ranges * fix(workspaces): keep zero bars at baseline * fix(ui): sync chart widget locales * fix(ui): translate chart widget locales --------- Co-authored-by: Eva <[email protected]> Co-authored-by: Peter Steinberger <[email protected]>
1 parent b1eaf53 commit 4a7abe1

71 files changed

Lines changed: 945 additions & 124 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎docs/web/workspaces.md‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,17 @@ collapse it, hide it, or delete it.
3838

3939
## Built-in widgets
4040

41-
Nine trusted widgets ship with the plugin and render as first-party UI:
41+
Ten trusted widgets ship with the plugin and render as first-party UI:
4242

4343
`stat-card`, `markdown`, `table`, `iframe-embed`, `sessions`, `usage`, `cron`,
44-
`instances`, `activity`.
44+
`instances`, `activity`, `chart`.
45+
46+
The `chart` widget renders dependency-free inline SVG as `line`, `bar`, `area`,
47+
`sparkline`, or `gauge`. Bind `value` to a numeric array or to an object shaped like
48+
`{ "points": [1, { "y": 2 }, { "value": 3 }] }`. Set `props.type` to select the
49+
visual and optionally set finite numeric `props.min` and `props.max` bounds. Invalid
50+
types, bounds, points, and series longer than 500 entries render a safe error state;
51+
empty series render an empty state.
4552

4653
Widgets declare data through **bindings**, they never fetch on their own:
4754

‎extensions/workspaces/src/schema.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ describe("Workspaces document schema", () => {
4242
}, "widgets[0].kind");
4343
});
4444

45+
it("accepts the trusted builtin chart kind", () => {
46+
const doc = validDoc();
47+
doc.tabs[0]!.widgets[0]!.kind = "builtin:chart";
48+
expect(validateWorkspaceDoc(doc).tabs[0]!.widgets[0]!.kind).toBe("builtin:chart");
49+
});
50+
4551
it("rejects a prototype-setter custom widget kind", () => {
4652
expectInvalid((doc) => {
4753
doc.tabs[0]!.widgets[0]!.kind = "custom:__proto__";

‎extensions/workspaces/src/schema.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export const BUILTIN_WIDGET_KINDS = [
7070
"builtin:cron",
7171
"builtin:instances",
7272
"builtin:activity",
73+
"builtin:chart",
7374
] as const;
7475

7576
const BUILTIN_KINDS = new Set<string>(BUILTIN_WIDGET_KINDS);

‎extensions/workspaces/src/tools.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const WIDGET_KIND_DESCRIPTION = [
6161
"builtin:iframe-embed (props {url}),",
6262
"builtin:sessions, builtin:usage, builtin:cron, builtin:instances, builtin:activity",
6363
"(each reads its own rpc binding; see workspace_get for a worked example).",
64-
"Charts are not builtins — author one with workspace_widget_scaffold and use custom:<name>.",
64+
"builtin:chart (binding id `value`; props {type?: line|bar|area|sparkline|gauge, min?: number, max?: number}; value is number[] or {points: Array<number|{y|value}>}).",
6565
].join(" ");
6666

6767
const JsonSchema = Type.Unknown({
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import { mkdir } from "node:fs/promises";
2+
import path from "node:path";
3+
import { chromium, type Browser, type Page } from "playwright";
4+
import { afterAll, beforeAll, describe, expect, it } from "vitest";
5+
import {
6+
canRunPlaywrightChromium,
7+
installMockGateway,
8+
resolvePlaywrightChromiumExecutablePath,
9+
startControlUiE2eServer,
10+
type ControlUiE2eServer,
11+
} from "../test-helpers/control-ui-e2e.ts";
12+
13+
const chromiumExecutablePath = resolvePlaywrightChromiumExecutablePath(chromium.executablePath());
14+
const chromiumAvailable = canRunPlaywrightChromium(chromiumExecutablePath);
15+
const allowMissingChromium = process.env.OPENCLAW_UI_E2E_ALLOW_MISSING_CHROMIUM === "1";
16+
const describeControlUiE2e = chromiumAvailable || !allowMissingChromium ? describe : describe.skip;
17+
const proofDir = process.env.OPENCLAW_UI_E2E_ARTIFACT_DIR?.trim();
18+
19+
const chartTypes = ["line", "bar", "area", "sparkline", "gauge"] as const;
20+
const chartValues = [8, 16, 11, 24, 20, 31];
21+
22+
let browser: Browser;
23+
let server: ControlUiE2eServer;
24+
25+
function workspaceDoc() {
26+
return {
27+
doc: {
28+
schemaVersion: 1,
29+
workspaceVersion: 1,
30+
tabs: [
31+
{
32+
slug: "charts",
33+
title: "Chart gallery",
34+
hidden: false,
35+
createdBy: "agent:main",
36+
widgets: chartTypes.map((type, index) => ({
37+
id: `chart_${type}`,
38+
kind: "builtin:chart",
39+
title: `${type[0]?.toUpperCase()}${type.slice(1)} chart`,
40+
grid:
41+
index < 3
42+
? { x: index * 4, y: 0, w: 4, h: 3 }
43+
: { x: 2 + (index - 3) * 4, y: 3, w: 4, h: 3 },
44+
collapsed: false,
45+
createdBy: "agent:main",
46+
props: { type },
47+
bindings: { value: { source: "static", value: chartValues } },
48+
})),
49+
},
50+
],
51+
widgetsRegistry: {},
52+
prefs: { tabOrder: ["charts"] },
53+
},
54+
workspaceVersion: 1,
55+
};
56+
}
57+
58+
async function captureProof(page: Page): Promise<void> {
59+
if (!proofDir) {
60+
return;
61+
}
62+
await mkdir(proofDir, { recursive: true });
63+
await page.screenshot({
64+
fullPage: true,
65+
path: path.join(proofDir, "workspace-chart-gallery.png"),
66+
});
67+
const widgets = page.locator('[data-test-id="workspace-widget"]');
68+
for (const [index, type] of chartTypes.entries()) {
69+
await widgets
70+
.nth(index)
71+
.screenshot({ path: path.join(proofDir, `workspace-chart-${type}.png`) });
72+
}
73+
}
74+
75+
describeControlUiE2e("Control UI workspace chart widgets", () => {
76+
beforeAll(async () => {
77+
if (!chromiumAvailable) {
78+
throw new Error(`Playwright Chromium is not available at ${chromiumExecutablePath}`);
79+
}
80+
server = await startControlUiE2eServer();
81+
browser = await chromium.launch({ executablePath: chromiumExecutablePath });
82+
});
83+
84+
afterAll(async () => {
85+
await browser?.close();
86+
await server?.close();
87+
});
88+
89+
it("renders every trusted chart type with an accessible data summary", async () => {
90+
const context = await browser.newContext({
91+
locale: "en-US",
92+
serviceWorkers: "block",
93+
viewport: { height: 1000, width: 1440 },
94+
});
95+
const page = await context.newPage();
96+
await installMockGateway(page, {
97+
controlUiTabs: [
98+
{
99+
pluginId: "workspaces",
100+
id: "workspaces",
101+
label: "Workspaces",
102+
group: "control",
103+
},
104+
],
105+
featureMethods: ["workspaces.get"],
106+
methodResponses: { "workspaces.get": workspaceDoc() },
107+
});
108+
109+
try {
110+
const response = await page.goto(`${server.baseUrl}plugin?plugin=workspaces&id=workspaces`);
111+
expect(response?.status()).toBe(200);
112+
const widgets = page.locator('[data-test-id="workspace-widget"]');
113+
await expect.poll(() => widgets.count()).toBe(chartTypes.length);
114+
115+
for (const [index, type] of chartTypes.entries()) {
116+
const widget = widgets.nth(index);
117+
const chart = widget.locator('[data-test-id="workspace-chart"]');
118+
await chart.waitFor({ timeout: 10_000 });
119+
expect(await widget.locator(`.workspace-chart--${type}`).count()).toBe(1);
120+
expect(await chart.getAttribute("role")).toBe("img");
121+
expect(await chart.getAttribute("aria-label")).toBe(
122+
`${type[0]?.toUpperCase()}${type.slice(1)} chart: 6 data points, ranging from 8 to 31.`,
123+
);
124+
}
125+
126+
await captureProof(page);
127+
} finally {
128+
await context.close();
129+
}
130+
});
131+
});

‎ui/src/i18n/.i18n/ar.meta.json‎

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎ui/src/i18n/.i18n/ar.tm.jsonl‎

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎ui/src/i18n/.i18n/catalog-fallbacks.json‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)