text
stringlengths
0
1.47k
* Shortest matching prefix of a failed `old_str` still worth reporting as a
* location hint; below this any HTML shares enough characters to point
* somewhere misleading.
*/
const MIN_ANCHOR_CHARS = 12;
/**
* Replace one exact, unique occurrence of `oldStr` in a rendered card's
* fragment. Iterating by patch instead of re-emitting the whole fragment is
* what keeps a small correction small: the model re-states only the changed
* region, and the card's markup never enters its output twice.
*
* A patch that does not resolve to exactly one site is refused rather than
* guessed at, because both wrong outcomes are silent — a near-miss would edit
* markup the model never saw, and an ambiguous match would edit an arbitrary
* one of several sites. The thrown message carries the surrounding real
* content so the caller can correct `old_str` within the same turn.
*
* @param base - the current fragment of the card being patched.
* @param oldStr - exact text to replace, whitespace included.
* @param newStr - replacement text; empty deletes the matched region.
* @returns the patched fragment.
* @throws Error naming why the patch did not apply; the tool surfaces it as `isError`.
*/
function applyFragmentPatch(base, oldStr, newStr) {
if (oldStr.length === 0) throw new Error("invalid visualization patch: old_str is empty — pass the exact card text to replace");
const first = base.indexOf(oldStr);
if (first === -1) throw new Error(`invalid visualization patch: old_str does not appear in the card. ${nearestAnchor(base, oldStr)}`);
if (base.indexOf(oldStr, first + oldStr.length) !== -1) throw new Error(`invalid visualization patch: old_str appears ${countOccurrences(base, oldStr)} times in the card — extend it with neighbouring lines until exactly one site matches`);
return base.slice(0, first) + newStr + base.slice(first + oldStr.length);
}
/**
* Describe where a failed `old_str` stopped matching: the longest prefix of it
* that does occur, and the card's real text at that site. Prefix occurrence is
* monotone in length, so the longest one is a binary search.
* @param base - the current fragment of the card being patched.
* @param oldStr - the `old_str` that failed to match.
* @returns a sentence naming the divergence point, or advising a full re-render.
*/
function nearestAnchor(base, oldStr) {
let matched = 0;
let beyond = oldStr.length;
while (matched < beyond) {
const mid = Math.ceil((matched + beyond) / 2);
if (base.includes(oldStr.slice(0, mid))) matched = mid;
else beyond = mid - 1;
}
if (matched < MIN_ANCHOR_CHARS) return "None of it matched, so the card is not in the state you assumed — re-render the whole card instead.";
const at = base.indexOf(oldStr.slice(0, matched));
return `Its first ${matched} characters do match, at offset ${at}, where the card actually reads ${JSON.stringify(base.slice(at, at + PATCH_CONTEXT_CHARS))} — correct old_str against that and retry.`;
}
/**
* Count non-overlapping occurrences of a needle, matching the replacement
* semantics {@link applyFragmentPatch} would apply.
* @param base - the text to scan.
* @param needle - the non-empty needle to count.
* @returns the number of non-overlapping occurrences.
*/
function countOccurrences(base, needle) {
let count = 0;
for (let at = base.indexOf(needle); at !== -1; at = base.indexOf(needle, at + needle.length)) count += 1;
return count;
}
/**
* 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
};
}
/**
* UTF-8 byte length without Buffer, so the browser bundle needs no polyfill.
* @param text - the string to measure.
* @returns its UTF-8 encoding length in bytes.
*/
function byteLength(text) {
return new TextEncoder().encode(text).length;
}
//#endregion
//#region src/tool.ts
const DESCRIPTION = `Show the user an interactive HTML visualization, rendered as a live card in the conversation. \`create\` (the default) takes the whole markup in \`fragment\`: literal inline HTML only (no <!doctype>, <html>, <head>, or <body> — the card supplies the document, stylesheet, and theme). To correct a ca...
/**
* Build the `visualize` tool definition over the composed filesystem seam.
* @param ctx - registrant context carrying `ctx.fs` for the workspace copy.
* @param maxFragmentBytes - deployment size ceiling for one fragment.