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.
When to use it
Section titled “When to use it”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).
What the agent receives
Section titled “What the agent receives”| The template calls | The agent gets | When |
|---|---|---|
canvas.setState({ code }) | Canvas state | 600 ms after the user stops typing, and on blur. The agent edits the same field and sends it whole. |
canvas.setState({ lastRun }) | Canvas state | After each run: ok, the row count or the error, so the agent can fix what failed. |
displayed() | FUNCTION_TOOL_DISPLAYED | Mobile snippet card: as soon as it shows. |
States
Section titled “States”Besides its data, the template draws these as itself, with mountState(). Pick one on the preview with State.
| State | When it shows |
|---|---|
| Loading | While execute loads the saved query, or the agent’s first draft, into the editor. The skeleton has the editor’s shape. |
| Empty | There’s no code to show yet, for example a saved query that was deleted. The agent offers to write one. |
| Error | execute 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.
CodeCanvasState
Section titled “CodeCanvasState”| Field | Type | Notes |
|---|---|---|
canvasTitle (optional) | string | The canvas header (setTitle, up to 80 characters). Default: title. |
title | string | What the code does, e.g. “Churned accounts last quarter”. |
intro (optional) | string | One line under the title. |
language | CodeLanguage | Highlighting and the built-in check: sql, json, formula or text. |
code | string | The code. The user and the agent both write here; the agent sends it whole and keeps it valid. |
runLabel (optional) | string | The Run button. Default: “Run”. |
lastRun (optional) | CodeCanvasRun | Set by the canvas after each run, so the agent sees whether it worked. |
CodeCanvasRun
Section titled “CodeCanvasRun”| Field | Type | Notes |
|---|---|---|
ok | boolean | Whether it ran. |
at | string | When (ISO 8601). |
rows (optional) | number | Rows returned. |
error (optional) | string | What failed, as your run() threw it. |
CodeCanvasResult
Section titled “CodeCanvasResult”| Field | Type | Notes |
|---|---|---|
columns (optional) | string[] | Column names. |
rows (optional) | (string | number | boolean | null)[][] | Up to about 50 rows; one value per column. |
output (optional) | string | Plain output instead of rows, e.g. a webhook’s response. |
note (optional) | string | A note beside the result, e.g. “First 50 of 1,204 rows”. |
CodeCanvasProblem
Section titled “CodeCanvasProblem”| Field | Type | Notes |
|---|---|---|
message | string | What to fix, in plain words. |
line (optional) | number | 1-based line. |
CodeCanvasActions
Section titled “CodeCanvasActions”| Field | Type | Notes |
|---|---|---|
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) => void | Called each time the code is shared with the agent (debounced). |
Install
Section titled “Install”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.
- Hosted action: TypeScript, JavaScript
- In-page handler: TypeScript, JavaScript
- code-canvas: TypeScript, JavaScript
- fs-kit: TypeScript, JavaScript
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.