# ===== file: lib/client.js ===== window.__ModuleLoader__.load({ id: "@nagi-ovo/dsh-visualize", factory: (require) => { var module = { exports: {} }; var exports = module.exports; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); let react = require("react"); let react_jsx_runtime = require("react/jsx-runtime"); /** * Narrow one persisted `tool/result` meta value to a {@link VisualizeMeta}. * Wire data cannot be trusted to match the compiled shape (an older or newer * host may have logged it), so a mismatch declines to `undefined` — the caller * falls back to the generic presentation instead of throwing on replay. * @param meta - the raw persisted meta value. * @returns the narrowed descriptor, or `undefined` for the generic path. */ function visualizeMetaFrom(meta) { if (typeof meta !== "object" || meta === null) return void 0; const record = meta; if (record["kind"] !== "visualize") return void 0; const { fragment, title, mode, path } = record; if (typeof fragment !== "string" || typeof title !== "string" || typeof path !== "string") return void 0; if (mode !== "inline" && mode !== "wide") return void 0; return { kind: "visualize", fragment, title, mode, path }; } /** * Rebuild a descriptor from the call's own arguments when persisted meta is * missing. Nested dispatches (`run_code` / PTC) never project presentation * meta, but they do log `arguments`; a create's fragment is in that JSON. * * An `update` without a fragment cannot be rebuilt from the wire (the patched * markup lives in execute's value, which nested logs do not keep) and * declines. Incomplete streaming JSON is not parsed via throw/catch: only a * trailing `}` is treated as complete, otherwise a *closed* `"fragment"` * string is taken through {@link extractStreamingFragment}'s scanner. * @param argsRaw - running `block.argsRaw` or settled `block.call.argsRaw`. * @returns a card descriptor, or `undefined` to keep the quiet fallback. */ function visualizeMetaFromArgs(argsRaw) { if (typeof argsRaw !== "string" || argsRaw.length === 0) return void 0; const complete = parseCompleteArgs(argsRaw); if (complete !== void 0) { if (complete["action"] === "update" && typeof complete["fragment"] !== "string") return void 0; return descriptorFromFields(complete["fragment"], complete["title"], complete["mode"], complete["path"]); } const closed = scanFragment(argsRaw, true); if (closed === void 0) return void 0; return descriptorFromFields(closed, void 0, void 0, void 0); } /** * Accept only a complete JSON object, gated on a trailing `}` so a streaming * prefix never enters `JSON.parse`. * @param raw - accumulated argument text. * @returns the object, or `undefined` when the prefix is incomplete or illegal. */ function parseCompleteArgs(raw) { let end = raw.length; while (end > 0) { const ch = raw[end - 1]; if (ch === " " || ch === "\n" || ch === "\r" || ch === " ") { end -= 1; continue; } break; } if (end === 0 || raw[end - 1] !== "}") return void 0; try { const parsed = JSON.parse(raw.slice(0, end)); if (typeof parsed !== "object" || parsed === null) return void 0; return parsed; } catch { return; } } /** * Narrow wire fields into a descriptor. Empty markup after dropping a * trailing half-script is declined rather than mounting a blank frame. */ function descriptorFromFields(fragment, title, mode, path) { if (typeof fragment !== "string") return void 0; if (mode !== void 0 && mode !== "inline" && mode !== "wide") return void 0; const markup = trimStreamingScripts(fragment); if (markup.trim().length === 0) return void 0; return { kind: "visualize", fragment: markup, title: typeof title === "string" && title.trim().length > 0 ? title.trim() : "Visualization", mode: mode === "wide" ? "wide" : "inline", path: typeof path === "string" ? path : "" }; } /** JSON short escapes, keyed by the character after the backslash. */ const JSON_ESCAPES = { "\"": "\"", "\\": "\\", "/": "/", b: "\b", f: "\f", n: "\n", r: "\r", t: " " }; /** * Extract the `fragment` string value from a *possibly incomplete* streaming * tool-call JSON argument prefix. The streaming preview calls this on every * accumulated delta: it scans for the `"fragment":"` opener, then unescapes * characters until the (possibly absent) closing quote, dropping a trailing * half-finished escape sequence rather than misreading it. * @param argsRaw - the accumulated raw argument text, valid JSON or a prefix. * @returns the fragment decoded so far, or `undefined` before the opener streams in. */ function extractStreamingFragment(argsRaw) { return scanFragment(argsRaw, false); } /** * Walk a possibly incomplete `"fragment":"...` value. * @param argsRaw - accumulated argument text. * @param requireClosed - when true, decline until the closing quote arrives * (the settled-card fallback must not mount on a half-streamed string). * @returns the decoded fragment, or `undefined` when the opener is absent * or, if required, the string is still open. */ function scanFragment(argsRaw, requireClosed) { const opener = /"fragment"\s*:\s*"/u.exec(argsRaw); if (!opener) return void 0; let out = ""; for (let i = opener.index + opener[0].length; i < argsRaw.length; i++) { const ch = argsRaw[i]; if (ch === "\"") return out; if (ch !== "\\") { out += ch; continue; } const next = argsRaw[i + 1]; if (next === void 0) return requireClosed ? void 0 : out; if (next === "u") { const hex = argsRaw.slice(i + 2, i + 6); if (hex.length < 4) return requireClosed ? void 0 : out; const code = Number.parseInt(hex, 16); if (Number.isNaN(code)) return requireClosed ? void 0 : out; out += String.fromCharCode(code); i += 5; continue; } const short = JSON_ESCAPES[next]; if (short === void 0) return requireClosed ? void 0 : out; out += short; i += 1; } return requireClosed ? void 0 : out; } /** Matches the last script opener (complete or still missing its `>`). */ const LAST_SCRIPT_OPEN = /]*>?(?![\s\S]*…<\/script>` blocks are kept — they are finished JavaScript the * preview shell executes on arrival, which is how a script-drawn chart paints * during generation — while a trailing block whose `<\/script>` has not * streamed in yet is dropped whole (a half-streamed body is almost never * valid JavaScript). * @param fragment - the fragment prefix streamed so far. * @returns the preview-safe markup. */ function trimStreamingScripts(fragment) { const opener = LAST_SCRIPT_OPEN.exec(fragment); if (!opener) return fragment; const rest = fragment.slice(opener.index + opener[0].length); return /<\/script\s*>/iu.test(rest) ? fragment : fragment.slice(0, opener.index); } //#endregion //#region src/frame-css.ts /** * The stylesheet injected into every visualization frame: theme tokens with * host-bridged overrides, a minimal reset, and the base-class vocabulary the * bundled skill teaches (`.card`, `.btn`, `.viz-*`, form utilities). Kept as a * TS string so the browser bundle needs no CSS loader and the node-side specs * can assert against it. * * Token contract: every `--` here is declared as * `var(--dsh-viz-, )`. The card resolves the host palette * (DSH `--dsw-alias-*` design tokens, whale-blue accent included) and injects * the `--dsh-viz-*` values on the frame's `:root`; outside DSH the fallbacks * keep the frame legible in both appearances via `light-dark()`. * * @module @nagi-ovo/dsh-visualize/frame-css */ /** The frame stylesheet, inlined into the sandboxed srcdoc document. */ const FRAME_CSS = ` :root { color-scheme: light dark; --background: var(--dsh-viz-background, transparent); --foreground: var(--dsh-viz-foreground, light-dark(rgb(26 28 31), rgb(240 242 245))); --card: var(--dsh-viz-card, light-dark(rgb(0 0 0 / 4%), rgb(255 255 255 / 6%))); --card-foreground: var(--dsh-viz-foreground, light-dark(rgb(26 28 31), rgb(240 242 245))); --muted-foreground: var(--dsh-viz-muted-foreground, light-dark(rgb(26 28 31 / 55%), rgb(240 242 245 / 55%))); --border: var(--dsh-viz-border, light-dark(rgb(0 0 0 / 10%), rgb(255 255 255 / 12%))); --primary: var(--dsh-viz-primary, light-dark(rgb(65 118 230), rgb(110 150 240))); --primary-foreground: var(--dsh-viz-primary-foreground, light-dark(rgb(255 255 255), rgb(13 13 13))); --viz-series-1: var(--dsh-viz-primary, light-dark(rgb(65 118 230), rgb(110 150 240))); --viz-series-2: light-dark(rgb(226 116 26), rgb(245 152 66)); --viz-series-3: light-dark(rgb(16 148 82), rgb(72 196 130)); --viz-series-4: light-dark(rgb(146 94 220), rgb(176 132 240)); --viz-series-5: light-dark(rgb(212 66 84), rgb(240 110 126)); --viz-series-6: light-dark(rgb(160 138 22), rgb(206 182 70)); --radius: 8px; --font-size-base: 14px; } * { box-sizing: border-box; } html, body { margin: 0; padding: 0; } body { background: var(--background); color: var(--foreground); font: 400 var(--font-size-base)/1.5 system-ui, -apple-system, 'Segoe UI', sans-serif; } h1, h2, h3 { margin: 0 0 0.5em; font-weight: 500; line-height: 1.3; } h1 { font-size: 1.3em; } h2 { font-size: 1.15em; } h3 { font-size: 1em; } p { margin: 0 0 0.75em; } a { color: var(--primary); } svg text { fill: var(--foreground); font-size: 12px; } .text-small { font-size: 12px; color: var(--muted-foreground); } .card { background: var(--card); color: var(--card-foreground); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; } .btn { appearance: none; display: inline-flex; align-items: center; gap: 6px; padding: 5px 12px; border: 1px solid var(--border); border-radius: var(--radius); background: transparent; color: var(--foreground); font: inherit; cursor: pointer; } .btn:hover { background: color-mix(in oklab, var(--foreground) 6%, transparent); } .btn-primary, .btn[aria-pressed='true'], .btn[aria-selected='true'], .btn.is-selected { background: var(--primary); border-color: var(--primary); color: var(--primary-foreground); } .btn-ghost { border-color: transparent; } .btn:disabled { opacity: 0.5; cursor: default; } .viz-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; } .viz-row { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; } .viz-controls { display: flex; flex-wrap: wrap; align-items: end; gap: 10px; margin-bottom: 12px; } .viz-stat .viz-stat-label, .viz-stat > :first-child { font-size: 12px; color: var(--muted-foreground); } .viz-stat-value { font-size: 1.4em; font-weight: 500; line-height: 1.2; } .viz-badge { display: inline-block; padding: 1px 8px; border-radius: 999px; background: color-mix(in oklab, var(--primary) 14%, transparent); color: var(--primary); font-size: 12px; } .form-label { display: block; font-size: 12px; color: var(--muted-foreground); margin-bottom: 4px; } .form-control, .form-select { width: 100%; padding: 5px 10px; border: 1px solid var(--border); border-radius: var(--radius); background: transparent; color: var(--foreground); font: inherit; } .form-check { display: flex; align-items: center; gap: 6px; } .form-check input { accent-color: var(--primary); } input[type='range'] { accent-color: var(--primary); } table { border-collapse: collapse; width: 100%; } th, td { text-align: left; padding: 5px 10px; border-bottom: 1px solid var(--border); } th { font-weight: 500; color: var(--muted-foreground); font-size: 12px; } .table-responsive { overflow-x: auto; } `; const RESOURCE_SOURCES = [ "blob:", "data:", ...[ "https://cdnjs.cloudflare.com", "https://cdn.jsdelivr.net", "https://esm.sh", "https://fonts.bunny.net", "https://fonts.googleapis.com", "https://fonts.gstatic.com", "https://unpkg.com" ] ].join(" "); /** The frame document's Content-Security-Policy. */ const FRAME_CSP = [ "default-src 'none'", `script-src 'unsafe-inline' 'unsafe-eval' 'wasm-unsafe-eval' ${RESOURCE_SOURCES}`, `style-src 'unsafe-inline' ${RESOURCE_SOURCES}`, `img-src ${RESOURCE_SOURCES}`, `font-src ${RESOURCE_SOURCES}`, `media-src ${RESOURCE_SOURCES}`, "worker-src blob:", "connect-src blob: data:", "frame-src 'none'", "object-src 'none'", "base-uri 'none'", "form-action 'none'" ].join("; "); /** Wire type of the frame→card height report message. */ const HEIGHT_MESSAGE_TYPE = "dsh-visualize:height"; /** Wire type of the card→frame streaming fragment update message. */ const STREAM_MESSAGE_TYPE = "dsh-visualize:stream"; /** * Assemble the complete srcdoc document for one visualization frame. * @param options - fragment, title, bridged palette, and report token. * @returns the HTML document string for the iframe's `srcDoc`. */ function buildFrameDoc(options) { const rootVars = Object.entries(options.themeVars).map(([name, value]) => [name, sanitizeCssValue(value)]).filter(([, value]) => value.length > 0).map(([name, value]) => `--dsh-viz-${name}: ${value};`).join(" "); return ` ${escapeHtml(options.title)} ${options.fragment}