text stringlengths 0 1.47k |
|---|
* @returns the tool definition to register on `ctx.tools`. |
*/ |
function visualizeTool(ctx, maxFragmentBytes) { |
return defineTool({ |
name: VISUALIZE_TOOL_NAME, |
description: DESCRIPTION, |
parameters: { |
action: { |
type: "string", |
enum: ["create", "update"], |
description: "`create` (default) renders a new card from `fragment`. `update` patches the card at `path`, replacing `old_str` with `new_str` — use it for a correction touching fewer than 20 lines in fewer than 5 places, and at most 4 times per reply; re-create the card for anything larger." |
}, |
fragment: { |
type: "string", |
description: "create only, required: the inline HTML fragment to render (markup, style, and script — no document skeleton)." |
}, |
title: { |
type: "string", |
description: "Concise card title. Defaults to \"Visualization\" on create; required on update." |
}, |
mode: { |
type: "string", |
enum: ["inline", "wide"], |
description: "Card width: `inline` (default) or `wide` for side-by-side panel comparisons." |
}, |
path: { |
type: "string", |
description: "update only, required: workspace path of the card to patch, as its own call reported it." |
}, |
old_str: { |
type: "string", |
description: "update only, required: the exact card text to replace, whitespace included. It must appear exactly once — keep it as short as stays unique." |
}, |
new_str: { |
type: "string", |
description: "update only, required: the replacement text. Empty deletes the matched region." |
} |
}, |
output: { |
schema: { |
type: "object", |
additionalProperties: false, |
properties: { |
action: { |
type: "string", |
required: true, |
enum: ["create", "update"] |
}, |
path: { |
type: "string", |
required: true |
}, |
title: { |
type: "string", |
required: true |
}, |
mode: { |
type: "string", |
required: true, |
enum: ["inline", "wide"] |
}, |
sizeBytes: { |
type: "integer", |
required: true |
}, |
fragment: { |
type: "string", |
required: true |
} |
} |
}, |
render: (_args, value) => [{ |
type: "text", |
text: value.action === "update" ? `Patched "${value.title}" in place (${value.sizeBytes} bytes; updated card at ${value.path}). The user sees the corrected visualization in the conversation; patch that path for any further correction.` : `Rendered "${value.title}" inline (${value.sizeBytes} bytes; workspace copy at... |
}], |
presentationMeta: (_args, value) => ({ |
kind: "visualize", |
fragment: value.fragment, |
title: value.title, |
mode: value.mode, |
path: value.path |
}) |
}, |
isConcurrencySafe: (args) => (args.action ?? "create") === "create", |
async execute(args, exec) { |
const action = args.action ?? "create"; |
const sandboxPolicy = ctx.get("sandboxPolicy")?.resolve({ ...exec.agent ? { session: exec.agent.session } : {} }); |
const cwd = sandboxPolicy?.workspaceRoot ?? exec.agent?.session.header.cwd; |
const resolveOpts = { |
...cwd !== void 0 ? { cwd } : {}, |
signal: exec.signal |
}; |
let source; |
let fragment; |
if (action === "update") { |
source = await ctx.fs.resolve(required(args.path, "path", action), resolveOpts); |
fragment = applyFragmentPatch(await ctx.fs.readText(source, exec.signal), required(args.old_str, "old_str", action), present(args.new_str, "new_str", action)); |
} else fragment = required(args.fragment, "fragment", action); |
const sizeBytes = validateFragment(fragment, maxFragmentBytes); |
const title = action === "update" ? required(args.title, "title", action).trim() : args.title?.trim() || "Visualization"; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.