text
stringlengths
0
1.47k
* 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.
*/
export function visualizeMetaFromArgs(argsRaw: unknown): VisualizeMeta | undefined {
if (typeof argsRaw !== 'string' || argsRaw.length === 0) return undefined
const complete = parseCompleteArgs(argsRaw)
if (complete !== undefined) {
// Default action is create. Update's full markup is not in the arguments.
if (complete['action'] === 'update' && typeof complete['fragment'] !== 'string') return undefined
return descriptorFromFields(complete['fragment'], complete['title'], complete['mode'], complete['path'])
}
const closed = scanFragment(argsRaw, true)
if (closed === undefined) return undefined
return descriptorFromFields(closed, undefined, undefined, undefined)
}
/**
* 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: string): Record<string, unknown> | undefined {
let end = raw.length
while (end > 0) {
const ch = raw[end - 1]!
if (ch === ' ' || ch === '\n' || ch === '\r' || ch === '\t') {
end -= 1
continue
}
break
}
if (end === 0 || raw[end - 1] !== '}') return undefined
try {
const parsed: unknown = JSON.parse(raw.slice(0, end))
if (typeof parsed !== 'object' || parsed === null) return undefined
return parsed as Record<string, unknown>
} catch {
return undefined
}
}
/**
* 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: unknown,
title: unknown,
mode: unknown,
path: unknown,
): VisualizeMeta | undefined {
if (typeof fragment !== 'string') return undefined
if (mode !== undefined && mode !== 'inline' && mode !== 'wide') return undefined
const markup = trimStreamingScripts(fragment)
if (markup.trim().length === 0) return undefined
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 : '',
}
}
/**
* 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: string): number {
return new TextEncoder().encode(text).length
}
/** JSON short escapes, keyed by the character after the backslash. */
const JSON_ESCAPES: Record<string, string> = {
'"': '"',
'\\': '\\',
'/': '/',
b: '\b',
f: '\f',
n: '\n',
r: '\r
# ===== file: examples/rail-skyline-session/joint.py =====
# /// script
# requires-python = ">=3.10"
# dependencies = ["numpy", "pillow"]
# ///
"""Solve the camera position from pier column positions.
This is a case-specific script for episode 13 with that photo's parameters hard-coded: the 17 pier pixel columns LEFT/RIGHT, the ridge points ridge,
the candidate center lat0/lon0 and the line name are all example parameters from one real case. Kept only as a reproduction record; not guaranteed to run on another photo.