Skip to content

Commit 68ae235

Browse files
improve(ui): refine chat widget host and image actions (#125250)
* improve(ui): refine chat widget host actions * fix(ui): keep widget pin presentation unchanged * fix(ui): compact widget actions and restore rounded host * style(ui): format widget action menu * test(canvas): refresh widget host document contract * test(ui): model the widget host wrapper * fix(ui): preserve retained widget downloads * fix(ci): register Codex tool coverage * fix(protocol): keep version exports compact * fix(ui): preserve widget action outcomes --------- Co-authored-by: RoboClaw <[email protected]>
1 parent bd6802f commit 68ae235

13 files changed

Lines changed: 201 additions & 32 deletions

File tree

‎docs/tools/show-widget.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ Channel plugins can register a contextual presenter behind the same core tool. I
1919

2020
In Control UI sessions, a Canvas widget can also be pinned to the session dashboard. Set `pin: true` in the tool call, or use **Pin to dashboard** on an existing transcript widget. Pinned HTML runs behind the same dedicated-origin, double-iframe sandbox host used by MCP Apps; the browser never resolves a widget data binding inside the untrusted frame.
2121

22-
For browser embedding, the wrapper document injects four small host bridges around the widget code:
22+
For browser embedding, the wrapper document injects five small host bridges around the widget code:
2323

24-
- A size reporter posts the rendered content height to the embedding chat, which clamps it and fits the iframe (160 to 1200 pixels).
24+
- A size reporter posts the rendered content height to the embedding chat, which clamps it and fits the iframe (48 to 1200 pixels).
2525
- A host bridge defines the legacy `sendPrompt(text)` helper plus the structured `openclaw.prompt`, `openclaw.state`, `openclaw.data`, and `openclaw.cron` APIs. Inline chat prompts retain their private message channel; dashboard APIs use a view-ticket-bound request channel. See [Interactive widgets](#interactive-widgets) and [Dashboard capabilities](#dashboard-capabilities).
2626
- A theme bridge listens for the Control UI's current design tokens and applies them as CSS variables, on load and again on every theme change.
2727
- A snapshot bridge renders the current widget document as a PNG when the embedding chat requests an export.
28+
- A chat-host bridge hides embedded scrollbar chrome when the widget runs inline while preserving scrolling behavior.
2829

2930
Everything else stays inside the frame: the document runs in an opaque origin with a strict Content Security Policy, so widget scripts cannot reach the Control UI, the Gateway, or the network.
3031

‎src/canvas/widget-tool.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,16 +544,17 @@ describe("show_widget", () => {
544544
'<SvG viewBox="0 0 10 10"><circle r="4" /></SvG>',
545545
);
546546

547-
expect(Buffer.byteLength(html)).toBe(13075);
547+
expect(Buffer.byteLength(html)).toBe(13493);
548548
expect(createHash("sha256").update(html).digest("hex")).toBe(
549-
"3dd21b774b05d53d12088018babfc82604cc098fcacb1cca48dff5be7e7f8812",
549+
"e50212b277bc75dc07a1c6c46cc313ff8f5cbfe261211b12d1714ad9d6b8c912",
550550
);
551551
expect(html).toContain("openclaw:widget-host-init-ack");
552552
expect(html).toContain("else push.call(waiting,{send,reject})");
553553
expect(html).toContain("else push.call(promptWaiting,{send,inline,reject})");
554554
expect(html).toContain("openclaw:widget-prompt-host-ready");
555555
expect(html).toContain("widget host capabilities unavailable");
556556
expect(html).toContain("widget prompt host unavailable");
557+
expect(html).toContain("openclaw:widget-chat-host");
557558
expect(html).not.toContain("widget is not hosted on a board");
558559
});
559560

‎src/canvas/wrap.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const WIDGET_BASE_STYLES = `:root{color-scheme:light dark;
5252
--border:#1e2028;--border-strong:#2e3040;
5353
--accent:#ff5c5c;--accent-fill:#d13c3c;--accent-fg:#ffffff;
5454
--ok:#22c55e;--warn:#f59e0b;--danger:#ef4444;--info:#3b82f6}}
55-
*{box-sizing:border-box}html,body{margin:0}
55+
*{box-sizing:border-box}html,body{margin:0}.openclaw-chat-host,.openclaw-chat-host body{scrollbar-width:none}.openclaw-chat-host::-webkit-scrollbar,.openclaw-chat-host body::-webkit-scrollbar{display:none}
5656
body{font:14px/1.5 var(--font-body);color:var(--text)}
5757
h1,h2,h3{margin:0 0 8px;color:var(--text-strong);font-weight:600}
5858
h1{font-size:18px}h2{font-size:16px}h3{font-size:14px}
@@ -177,6 +177,10 @@ export function buildWidgetDocument(
177177
'const value=typeof raw==="string"?raw.trim():"";' +
178178
'if(value&&value.length<=256)set("--"+key,value);else rm("--"+key);}' +
179179
'if(data.mode==="light"||data.mode==="dark")set("color-scheme",data.mode);});})();</script>';
180+
const chatHostBridge =
181+
"<script>(()=>{if(!window.parent||window.parent===window)return;" +
182+
'addEventListener("message",event=>{if(event.source===window.parent&&event.data?.type==="openclaw:widget-chat-host")' +
183+
'document.documentElement.classList.add("openclaw-chat-host");});})();</script>';
180184
/*
181185
* Snapshot requests come from the embedding parent and replies return only
182186
* there. Capture the response channel and rendering primitives before widget
@@ -240,5 +244,5 @@ export function buildWidgetDocument(
240244
: "'none'";
241245
const scriptSources = options.scriptOrigins?.length ? ` ${options.scriptOrigins.join(" ")}` : "";
242246
return `<!doctype html>
243-
<html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src 'unsafe-inline'; script-src 'unsafe-inline'${scriptSources}; img-src data:; connect-src ${connectSources};"><title>${escapeHtml(title)}</title><style>${WIDGET_BASE_STYLES}</style></head><body${bodyClass}>${widgetBridge}${themeBridge}${snapshotBridge}${widgetCode}${sizeReporter}</body></html>`;
247+
<html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src 'unsafe-inline'; script-src 'unsafe-inline'${scriptSources}; img-src data:; connect-src ${connectSources};"><title>${escapeHtml(title)}</title><style>${WIDGET_BASE_STYLES}</style></head><body${bodyClass}>${widgetBridge}${themeBridge}${chatHostBridge}${snapshotBridge}${widgetCode}${sizeReporter}</body></html>`;
244248
}

‎ui/src/components/icons-tools.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ export const toolIcons = {
6868
image: strokeIcon(svg` <rect width="18" height="18" x="3" y="3" rx="2" ry="2" />
6969
<circle cx="9" cy="9" r="2" />
7070
<path d="m21 15-3.086-3.086a2 2 0 0 0-2.828 0L6 21" />`),
71+
copyImage: strokeIcon(svg` <rect width="14" height="14" x="8" y="8" rx="2" />
72+
<path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2" />
73+
<circle cx="13" cy="13" r="1" />
74+
<path d="m21 18-3-3-5 5" />`),
7175
camera: strokeIcon(svg` <path
7276
d="M14.5 4 16 7h3a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V9a2 2 0 0 1 2-2h3l1.5-3z"
7377
/>

‎ui/src/i18n/locales/en.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5995,6 +5995,8 @@ export const en: TranslationMap = {
59955995
toolOutput: "Tool output",
59965996
toolError: "Tool error",
59975997
rawDetails: "Raw details",
5998+
showRawDetails: "Show raw details",
5999+
hideRawDetails: "Hide raw details",
59986000
viewMode: "Tool detail view",
59996001
diff: "Diff",
60006002
raw: "Raw",
@@ -6003,7 +6005,10 @@ export const en: TranslationMap = {
60036005
widgetActions: "Widget actions",
60046006
copyToClipboard: "Copy to clipboard",
60056007
downloadFile: "Download file",
6008+
copyAsImage: "Copy as image",
6009+
downloadAsImage: "Download as image",
60066010
widgetExportRerender: "This widget needs to be re-rendered to export as an image.",
6011+
widgetExportHtmlFallback: "Image unavailable. Downloaded the widget as HTML instead.",
60076012
widgetExportFailed: "Widget export failed. Try again.",
60086013
pinToDashboard: "Pin to dashboard",
60096014
pinToDashboardPending: "Pinning…",

‎ui/src/pages/chat/chat-responsive.browser.test.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,9 +1440,11 @@ describeBrowserLayout.concurrent("chat responsive browser layout", () => {
14401440
<div class="chat-group assistant chat-group--with-footer">
14411441
<div class="chat-group-messages">
14421442
<div class="chat-bubble">
1443-
<div class="chat-tool-card__preview" data-content-kind="mcp-app">
1444-
<div class="chat-tool-card__preview-panel">
1445-
<mcp-app-view style="display:block;width:100%;height:320px"></mcp-app-view>
1443+
<div class="chat-tool-card__widget-host">
1444+
<div class="chat-tool-card__preview" data-content-kind="mcp-app">
1445+
<div class="chat-tool-card__preview-panel">
1446+
<mcp-app-view style="display:block;width:100%;height:320px"></mcp-app-view>
1447+
</div>
14461448
</div>
14471449
</div>
14481450
</div>

‎ui/src/pages/chat/components/chat-message-bubble.ts‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -383,15 +383,21 @@ export function renderGroupedMessage(
383383
const assistantViewContent =
384384
sourceRole === "assistant" && assistantViewBlocks.length > 0
385385
? html`${assistantViewBlocks.map(
386-
(block) => html`${renderToolPreview(block.preview, "chat_message", {
387-
onOpenSidebar,
388-
rawText: block.rawText ?? null,
389-
canvasPluginSurfaceUrl: opts.canvasPluginSurfaceUrl,
390-
boardProvider: opts.boardProvider,
391-
embedSandboxMode: opts.embedSandboxMode ?? "scripts",
392-
sessionKey: opts.sessionKey,
393-
})}
394-
${block.rawText ? renderRawOutputToggle(block.rawText) : nothing}`,
386+
(block) => html`<div class="chat-tool-card__widget-host">
387+
${renderToolPreview(block.preview, "chat_message", {
388+
onOpenSidebar,
389+
rawText: block.rawText ?? null,
390+
canvasPluginSurfaceUrl: opts.canvasPluginSurfaceUrl,
391+
boardProvider: opts.boardProvider,
392+
embedSandboxMode: opts.embedSandboxMode ?? "scripts",
393+
sessionKey: opts.sessionKey,
394+
})}
395+
${block.rawText
396+
? html`<div class="chat-tool-card__widget-raw">
397+
${renderRawOutputToggle(block.rawText)}
398+
</div>`
399+
: nothing}
400+
</div>`,
395401
)}`
396402
: nothing;
397403

‎ui/src/pages/chat/components/chat-message.test.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,13 @@ function createAssistantCanvasBlock(params: {
385385
url?: string;
386386
preferredHeight?: number;
387387
presentationTarget?: "assistant_message" | "tool_card";
388+
mcpApp?: { viewId: string };
388389
}) {
389390
const viewId = `cv_inline_${params.suffix}`;
390-
const preview = createCanvasPreview({ ...params, viewId });
391+
const preview = {
392+
...createCanvasPreview({ ...params, viewId }),
393+
...(params.mcpApp ? { mcpApp: params.mcpApp } : {}),
394+
};
391395
return {
392396
type: "canvas",
393397
preview,
@@ -5499,6 +5503,30 @@ describe("grouped chat rendering", () => {
54995503
).toBe("canvas_render");
55005504
});
55015505

5506+
it("keeps MCP App raw details reachable from its widget menu", () => {
5507+
const container = document.createElement("div");
5508+
const canvas = createAssistantCanvasBlock({
5509+
suffix: "mcp-raw",
5510+
mcpApp: { viewId: "view-mcp-raw" },
5511+
});
5512+
renderAssistantMessage(container, createAssistantMessage([canvas]), {
5513+
sessionKey: "agent:main:main",
5514+
});
5515+
5516+
const dropdown = expectElement(container, "wa-dropdown", HTMLElement);
5517+
expect(dropdown.querySelectorAll("wa-dropdown-item")).toHaveLength(1);
5518+
dropdown.dispatchEvent(
5519+
new CustomEvent("wa-select", {
5520+
detail: { item: { value: "raw-details" } },
5521+
}),
5522+
);
5523+
expect(
5524+
container
5525+
.querySelector(".chat-tool-card__widget-raw .chat-tool-card__raw-toggle")
5526+
?.getAttribute("aria-expanded"),
5527+
).toBe("true");
5528+
});
5529+
55025530
it("opens generic tool details instead of a canvas preview from tool rows", () => {
55035531
const container = document.createElement("div");
55045532
const onOpenSidebar = vi.fn();

‎ui/src/pages/chat/components/widget-card.test.ts‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ describe("widget-card", () => {
189189
expect(canvas.querySelector('button[aria-label="Widget actions"]')).not.toBeNull();
190190
expect(
191191
Array.from(canvas.querySelectorAll("wa-dropdown-item"), (item) => item.textContent?.trim()),
192-
).toEqual(["Copy to clipboard", "Download file"]);
192+
).toEqual(["Copy as image", "Download as image"]);
193193

194194
const app = document.createElement("div");
195195
render(
@@ -214,6 +214,44 @@ describe("widget-card", () => {
214214
expect(app.querySelector("iframe")).toBeNull();
215215
expect(app.querySelector('button[aria-label="Widget actions"]')).toBeNull();
216216

217+
render(
218+
renderToolPreview(
219+
{
220+
kind: "canvas",
221+
surface: "assistant_message",
222+
render: "url",
223+
title: "App",
224+
preferredHeight: 480,
225+
mcpApp: { viewId: "view-dispatch" },
226+
},
227+
"chat_message",
228+
{ sessionKey: "agent:main:main", rawText: "raw app payload" },
229+
),
230+
app,
231+
);
232+
expect(app.querySelector('button[aria-label="Widget actions"]')).not.toBeNull();
233+
expect(
234+
Array.from(app.querySelectorAll("wa-dropdown-item"), (item) => item.textContent?.trim()),
235+
).toEqual(["Show raw details"]);
236+
237+
const external = document.createElement("div");
238+
render(
239+
renderToolPreview(
240+
{
241+
kind: "canvas",
242+
surface: "assistant_message",
243+
render: "url",
244+
url: "https://example.test/widget",
245+
},
246+
"chat_message",
247+
{ allowExternalEmbedUrls: true, rawText: "external raw payload" },
248+
),
249+
external,
250+
);
251+
expect(
252+
Array.from(external.querySelectorAll("wa-dropdown-item"), (item) => item.textContent?.trim()),
253+
).toEqual(["Show raw details"]);
254+
217255
const unknown = document.createElement("div");
218256
render(renderToolPreview({ kind: "unknown" } as never, "chat_message"), unknown);
219257
expect(unknown.childElementCount).toBe(0);
@@ -253,7 +291,6 @@ describe("widget-card", () => {
253291
),
254292
canvas,
255293
);
256-
257294
canvas.querySelector<HTMLButtonElement>("[data-pin-widget]")?.click();
258295
await vi.waitFor(() => {
259296
expect(pinWidget).toHaveBeenCalledWith({

‎ui/src/pages/chat/components/widget-card.ts‎

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ const WIDGET_SIZE_MESSAGE_TYPE = "openclaw:widget-size";
137137
const WIDGET_PROMPT_OFFER_MESSAGE_TYPE = "openclaw:widget-prompt-offer";
138138
const WIDGET_PROMPT_MESSAGE_TYPE = "openclaw:widget-prompt";
139139
const WIDGET_PROMPT_HOST_READY_MESSAGE_TYPE = "openclaw:widget-prompt-host-ready";
140+
const WIDGET_CHAT_HOST_MESSAGE_TYPE = "openclaw:widget-chat-host";
140141
const WIDGET_FRAME_MIN_HEIGHT = 48;
141142
const WIDGET_FRAME_MAX_HEIGHT = 1200;
142143
// Preview frames render inside lit shadow roots, so a document query cannot
@@ -326,6 +327,7 @@ function renderPreviewFrame(params: {
326327
adoptWidgetPromptPort(frame);
327328
}
328329
postWidgetTheme(frame);
330+
frame.contentWindow?.postMessage({ type: WIDGET_CHAT_HOST_MESSAGE_TYPE }, "*");
329331
}
330332
};
331333
return keyed(
@@ -424,6 +426,25 @@ function handleWidgetExportAction(
424426
title: string | undefined,
425427
) {
426428
const value = event.detail.item.value;
429+
if (value === "raw-details") {
430+
const dropdown = event.currentTarget;
431+
const host =
432+
dropdown instanceof HTMLElement ? dropdown.closest(".chat-tool-card__widget-host") : null;
433+
const toggle = host?.querySelector<HTMLButtonElement>(
434+
".chat-tool-card__widget-raw .chat-tool-card__raw-toggle",
435+
);
436+
toggle?.click();
437+
const label =
438+
dropdown instanceof HTMLElement ? dropdown.querySelector("[data-raw-label]") : null;
439+
label?.replaceChildren(
440+
t(
441+
toggle && toggle.getAttribute("aria-expanded") === "true"
442+
? "chat.toolCards.hideRawDetails"
443+
: "chat.toolCards.showRawDetails",
444+
),
445+
);
446+
return;
447+
}
427448
if (value !== "copy" && value !== "download") {
428449
return;
429450
}
@@ -442,15 +463,20 @@ function handleWidgetExportAction(
442463
.then((result) => {
443464
if (result === "rerender-required") {
444465
showToast({ message: t("chat.toolCards.widgetExportRerender") });
466+
} else if (result === "html") {
467+
showToast({ message: t("chat.toolCards.widgetExportHtmlFallback") });
468+
} else if (value === "copy") {
469+
showToast({ message: t("common.copied") });
445470
}
446471
})
447472
.catch(() => {
448473
showToast({ message: t("chat.toolCards.widgetExportFailed") });
449474
});
450475
}
451476

452-
function renderWidgetActions(preview: ToolPreview) {
453-
if (preview.mcpApp || !isInternalCanvasEntryUrl(preview.url)) {
477+
function renderWidgetActions(preview: ToolPreview, hasRawDetails: boolean) {
478+
const canExportImage = !preview.mcpApp && isInternalCanvasEntryUrl(preview.url);
479+
if (!canExportImage && !hasRawDetails) {
454480
return nothing;
455481
}
456482
return html`
@@ -470,8 +496,30 @@ function renderWidgetActions(preview: ToolPreview) {
470496
>
471497
${icons.moreHorizontal}
472498
</button>
473-
<wa-dropdown-item value="copy">${t("chat.toolCards.copyToClipboard")}</wa-dropdown-item>
474-
<wa-dropdown-item value="download">${t("chat.toolCards.downloadFile")}</wa-dropdown-item>
499+
${canExportImage
500+
? html`
501+
<wa-dropdown-item class="session-menu__item" value="copy">
502+
<span slot="icon" class="session-menu__icon" aria-hidden="true"
503+
>${icons.copyImage}</span
504+
>
505+
<span class="session-menu__text">${t("chat.toolCards.copyAsImage")}</span>
506+
</wa-dropdown-item>
507+
<wa-dropdown-item class="session-menu__item" value="download">
508+
<span slot="icon" class="session-menu__icon" aria-hidden="true"
509+
>${icons.download}</span
510+
>
511+
<span class="session-menu__text">${t("chat.toolCards.downloadAsImage")}</span>
512+
</wa-dropdown-item>
513+
`
514+
: nothing}
515+
${hasRawDetails
516+
? html`<wa-dropdown-item class="session-menu__item" value="raw-details">
517+
<span slot="icon" class="session-menu__icon" aria-hidden="true">${icons.fileText}</span>
518+
<span class="session-menu__text" data-raw-label
519+
>${t("chat.toolCards.showRawDetails")}</span
520+
>
521+
</wa-dropdown-item>`
522+
: nothing}
475523
</wa-dropdown>
476524
`;
477525
}
@@ -531,7 +579,7 @@ function renderWidgetCard(
531579
${icons.pin}
532580
</button>`
533581
: nothing;
534-
const widgetActions = renderWidgetActions(preview);
582+
const widgetActions = renderWidgetActions(preview, Boolean(options?.rawText));
535583
const actions =
536584
pinAction === nothing && widgetActions === nothing
537585
? nothing

0 commit comments

Comments
 (0)