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`. |
*/ |
export function applyFragmentPatch(base: string, oldStr: string, newStr: string): string { |
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: string, oldStr: string): string { |
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: string, needle: string): number { |
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. |
*/ |
export function visualizeMetaFrom(meta: unknown): VisualizeMeta | undefined { |
if (typeof meta !== 'object' || meta === null) return undefined |
const record = meta as Record<string, unknown> |
if (record['kind'] !== 'visualize') return undefined |
const { fragment, title, mode, path } = record |
if (typeof fragment !== 'string' || typeof title !== 'string' || typeof path !== 'string') return undefined |
if (mode !== 'inline' && mode !== 'wide') return undefined |
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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.