Skip to content
Talk to an engineer

Canvas

Code editor

A code editor beside the chat for a query, a formula or a JSON payload the user and the agent write together. It highlights and checks the code as it changes, marks the lines the agent edits, and runs a preview through your app.

KindCanvas · CODE · EDIT_MODEAgent waitsNoMobileNo canvas: a snippet card

Use it when: A query, a formula, a script or a config the user and the agent write together: a SQL report, a filter formula, a webhook payload. The agent writes it into the editor, keeps it valid, and the user runs a preview before your app saves it.

Use something else when: A snippet the agent shows once and the user copies (a chat message with a code block). Settings with fixed choices (form canvas). Mobile: the canvas isn’t available; use the snippet card (mountMobile).

The template callsThe agent getsWhen
canvas.setState({ code })Canvas state600 ms after the user stops typing, and on blur. The agent edits the same field and sends it whole.
canvas.setState({ lastRun })Canvas stateAfter each run: ok, the row count or the error, so the agent can fix what failed.
displayed()FUNCTION_TOOL_DISPLAYEDMobile snippet card: as soon as it shows.

Besides its data, the template draws these as itself, with mountState(). Pick one on the preview with State.

StateWhen it shows
LoadingWhile execute loads the saved query, or the agent’s first draft, into the editor. The skeleton has the editor’s shape.
EmptyThere’s no code to show yet, for example a saved query that was deleted. The agent offers to write one.
Errorexecute couldn’t load the query or the schema. Nothing changed; Try again loads it in place.
mountState({ root, head, title: "…", state: { kind: "error", text: "…", retry } });

The hosted action and the in-page handler below show each case. The state types are LoadingState, EmptyState and ErrorState in fs-kit.

FieldTypeNotes
canvasTitle (optional)stringThe canvas header (setTitle, up to 80 characters). Default: title.
titlestringWhat the code does, e.g. “Churned accounts last quarter”.
intro (optional)stringOne line under the title.
languageCodeLanguageHighlighting and the built-in check: sql, json, formula or text.
codestringThe code. The user and the agent both write here; the agent sends it whole and keeps it valid.
runLabel (optional)stringThe Run button. Default: “Run”.
lastRun (optional)CodeCanvasRunSet by the canvas after each run, so the agent sees whether it worked.
FieldTypeNotes
okbooleanWhether it ran.
atstringWhen (ISO 8601).
rows (optional)numberRows returned.
error (optional)stringWhat failed, as your run() threw it.
FieldTypeNotes
columns (optional)string[]Column names.
rows (optional)(string | number | boolean | null)[][]Up to about 50 rows; one value per column.
output (optional)stringPlain output instead of rows, e.g. a webhook’s response.
note (optional)stringA note beside the result, e.g. “First 50 of 1,204 rows”.
FieldTypeNotes
messagestringWhat to fix, in plain words.
line (optional)number1-based line.
FieldTypeNotes
run() (optional)(code: string, language: CodeLanguage) => Promise<CodeCanvasResult>Runs a preview through your app. Throw to show the error; put “line N” in the message to point at a line.
validate() (optional)(code: string, language: CodeLanguage) => CodeCanvasProblem | null | Promise<CodeCanvasProblem | null>Your own check (a parser, a dry run). Return null when it’s valid. Default: the built-in check for the language.
save() (optional)(code: string) => Promise<void>Saves the code in your app, e.g. as a saved report. Shows a Save button. Throw to show why it wasn’t saved.
onChange() (optional)(state: CodeCanvasState) => voidCalled each time the code is shared with the agent (debounced).

Copy the template and the shared kit into your code: agent/ui/ in an agent repo, or anywhere in your app. The template imports only ./fs-kit.js. Then wire it up from the hosted action or the in-page handler.

The hosted action needs an agent repo with one folder per action (agent/actions/<action_key>/index.ts). If your repo keeps its actions in agent/actions/index.ts, use the in-page handler: it runs the same template in your app.

All templates in machine-readable form: /ui-catalog/catalog.json.