import type { UiWidgetEvent } from "../../protocol"; import { renderWidgetBridgeScript } from "./widget-bridge.js"; export type VisualBlockShellInput = { widgetId: string; title: string; fallback: string; srcdoc: string; }; export function visualBlockSummary(event: Partial): string { if (event.type === "widget_error") { return [event.widgetId, "error", event.message, "agent: fix widget HTML/JS"] .filter(Boolean) .join(" · "); } return [event.title, event.widgetId, event.status, event.fallback] .filter(Boolean) .join(" · "); } export function renderVisualBlockShell(input: VisualBlockShellInput): string { return `
${escapeHtml(input.title)} ${escapeHtml(input.widgetId)}
${escapeHtml(input.fallback)}
`; } export function renderVisualBlockComponentJs(): string { return `function renderVisualBlock(row, event) { row.classList.add('ui-visual-block-row'); row.textContent = ''; const header = document.createElement('header'); header.textContent = [event.title, event.widgetId, event.status, event.label, event.message].filter(Boolean).join(' · '); row.append(header); if (event.fallback) { const fallback = document.createElement('div'); fallback.className = 'ui-visual-fallback'; fallback.textContent = event.fallback; row.append(fallback); } if (event.type === 'widget_mount' || event.type === 'widget_update') { const iframe = document.createElement('iframe'); iframe.title = event.title || event.widgetId; iframe.srcdoc = widgetBridgeScript(event.widgetId) + (event.html || ''); row.append(iframe); } if (event.type === 'widget_error') { const hint = document.createElement('div'); hint.className = 'ui-visual-error-hint'; hint.textContent = 'agent: fix widget HTML/JS'; row.append(hint); } } function widgetBridgeScript(widgetId) { return ${JSON.stringify(renderWidgetBridgeScript("__WIDGET_ID__"))}.replaceAll('__WIDGET_ID__', widgetId); } `; } function escapeAttr(value: string): string { return escapeHtml(value).replaceAll('"', """); } function escapeHtml(value: string): string { return value .replaceAll("&", "&") .replaceAll("<", "<") .replaceAll(">", ">"); }