text
stringlengths
0
1.47k
* 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 = /<script\b[^>]*>?(?![\s\S]*<script\b)/iu;
/**
* Prepare a streamed fragment prefix for the live preview: complete
* `<script>…<\/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