tldraw offline Skill
Work with the tldraw offline desktop app (offline.tldraw.com): read the open
canvas, make edits, and write document scripts — JavaScript embedded in a
.tldraw file that runs on load and gives the file durable behavior. The app
runs a local HTTP API (default localhost:7236) that a coding agent drives
with plain curl from its terminal — this is exactly how the app's own homepage
demo (Codex editing a canvas live) works. The agent does NOT use computer-use /
GUI clicking, and does NOT hand-edit the .tldraw file directly. Keep tldraw
offline open while you work.
When to Use
- The user has tldraw offline open and asks you to build or modify a canvas
(diagrams, wireframes, layouts).
- You want to add durable behavior to a drawing (reactive shapes, interactive
buttons, animation, connection logic) via an embedded document script.
Do NOT hand-place shapes to imitate a drawing — write the code that generates
them. Agents are far better at scripting the canvas than at drawing on it.
Prerequisites
- tldraw offline installed and running, with a document open. Releases:
https://github.com/tldraw/tldraw-offline/releases/latest (macOS DMG, Windows
x64/Arm64, Linux
x86_64/arm64 AppImage or amd64/arm64 .deb).
- Agent skills installed in the app:
Develop → Install Agent Skills. The
app writes its own tldraw skill into ~/.codex/skills/, ~/.claude/skills/,
~/.cursor/skills/, and ~/.gemini/skills/ — teaching that agent the curl
recipes below. (This Hermes skill mirrors that guidance for Hermes.)
- The local control API. On launch the app writes
server.json to its config
dir (Linux ~/.config/tldraw/, macOS ~/Library/Application Support/tldraw/,
Windows %APPDATA%\tldraw\) with port (default 7236), a bearer token,
pid, and startedAt. Every request except GET / needs
Authorization: Bearer <token>. A clean quit removes server.json; if it's
present but the port doesn't answer, the app quit uncleanly — treat as not
running.
- Re-read port + token on EVERY shell call. Each terminal call is a fresh
shell, so an
exported token does not persist — "export once and reuse" sends
an empty token and 401s. Read both inline at the top of each call:
PORT=$(jq -r .port <server.json>); TOKEN=$(jq -r .token <server.json>).
- No account or network needed for local editing.
How to Run
Two distinct workflows. Pick by whether the change must survive a reload.
A. One-off canvas edits (/exec) — layout, generating shapes, cleanup. This
is a live edit, not saved script:
BASE=http://localhost:7236
TOKEN=$(python3 -c "import json;print(json.load(open('$HOME/.config/tldraw/server.json'))['token'])")
# find the focused document id
DOC=$(curl -s "$BASE/api/search" -X POST -H 'content-type: application/json' \
-H "Authorization: Bearer $TOKEN" \
-d '{"code":"return (await api.getFocusedDoc()).id"}' | python3 -c "import sys,json;print(json.load(sys.stdin)['result'])")
# run code with the live `editor` + `helpers` in scope
curl -s "$BASE/api/doc/$DOC/exec" -X POST -H 'content-type: application/json' \
-H "Authorization: Bearer $TOKEN" \
-d '{"code":"const {createShapeId,toRichText}=await import(\"tldraw\"); editor.createShape({id:createShapeId(),type:\"geo\",x:0,y:0,props:{geo:\"rectangle\",w:200,h:100,color:\"blue\",fill:\"solid\",richText:toRichText(\"hello\")}}); return editor.getCurrentPageShapes().length"}'
B. Durable behavior (script/main.js) — reactive/interactive logic that must
survive reload. Edit the file on disk; the app's watcher applies it:
# get the live script file path for the doc
curl -s "$BASE/api/doc/$DOC/script-workspace" -X POST \
-H "Authorization: Bearer $TOKEN" # -> result.mainJsPath, result.isDefaultScript
# edit result.mainJsPath with read_file / patch / write_file (see scripts/main.js)
# then confirm the watcher applied it:
curl -s "$BASE/api/doc/$DOC/script-status" -H "Authorization: Bearer $TOKEN"
The ready-to-adapt document script is scripts/main.js.
Quick Reference
The document-script contract (verified against the app's bundled
script-context.d.ts):
import { createShapeId, toRichText } from 'tldraw' // primitives: import, not globals
export default function ({ editor, helpers, signal }) {
editor.run(() => { // batch = one undo step
helpers.createShapeIfMissing({ // idempotent furniture
id: createShapeId('node-1'), type: 'geo', x: 0, y: 0,
props: { geo: 'rectangle', w: 200, h: 100, richText: toRichText('hi') },
})
})
const stop = editor.store.listen(() => { /* react */ }) // fires the tick AFTER a commit
signal.addEventListener('abort', () => stop()) // REQUIRED cleanup on rerun/close
}
ctx.editor — the live Editor (createShape, updateShape, deleteShapes,
getCurrentPageShapes, getShape, getBindingsFromShape, zoomToFit,
on('tick'|'event', fn), run(fn, { history: 'ignore' })).
ctx.helpers — createShapeIfMissing, createShapesIfMissing,
createArrowBetweenShapes(from, to, { arrowheadEnd }), translateShapes,
onShapeTranslate(id, fn, { signal }), richTextToPlainText, boxShapes,
getLints.
ctx.signal — AbortSignal; attach every listener/interval teardown to it.
config.js (separate file) registers custom shape/tool/component utils and
runs before mount; main.js runs against the mounted editor and reruns on save.
Interactive UI (clickable buttons that drive state)
Drawn shapes can behave like a real app — the thing a static whiteboard can't do.
Full example: scripts/counter.js (a number display + MINUS/RESET/PLUS buttons).
Verification boundary — read this before claiming interaction works or doesn't.
The app's OWN agent playbook says to verify a clickable-UI script with "one
simulated click and one state read" via /exec (editor.dispatch a pointer
event, await a tick, read the shape's state) — NOT by driving a real mouse. By
that standard the counter is verified: dispatched clicks stepped it
0 → 1 → 2 → 1 → 0. Two caveats worth writing down:
- The script only runs once the app's file-watcher applies it. On Linux that
watcher uses inotify; a host with an exhausted
fs.inotify.max_user_instances
logs inotify_add_watch ... No space left on device, script-status shows
state: "not-watching" / hasEntry: false, and the script never executes.
This is a host limit, not a script bug — a normal desktop is unaffected.
- Driving the canvas with computer-use needs FOREGROUND delivery, not
background. cua-driver's default
background delivery returns
background_unavailable against Electron's occluded renderer — but that is the
first rung, not a wall. cua-driver returns an escalation: "foreground" hint;
set delivery_mode: "foreground" (pair with bring_to_front) and its X11 XTest
path (x11_xtest_fg) clicks land — this dismisses the "Run Script" consent
dialog and clicks the canvas. Verified by the Cua team on this exact v1.11.0
AppImage (Linux/X11). Do NOT conclude "Electron rejects synthetic clicks" and
give up in background mode — climb to foreground. (The real product path is
still /exec, not clicks; this note is for computer-use-driven testing.)
The pattern:
export default function ({ editor, helpers, signal }) {
// 1. Build buttons idempotently; tag each with meta so the handler finds them.
// Give buttons a visible label AND a meta.action.
// 2. Hit-test pointer_down in PAGE coordinates against the button bounds:
const inside = (b, p) => p.x >= b.x && p.x <= b.x + b.w && p.y >= b.y && p.y <= b.y + b.h
function onEvent(info) {
if (!info || info.name !== 'pointer_down') return
let p = null
try { if (info.point && editor.screenToPage) p = editor.screenToPage(info.point) } catch {}
p = p ?? editor.inputs?.currentPagePoint
if (!p) return
const hit = editor.getCurrentPageShapes().find(
(s) => s.meta?.ui === 'button' &&
inside({ x: s.x, y: s.y, w: s.props.w, h: s.props.h }, p)
)
if (hit) runAction(hit.meta.action) // mutate state; store it in a shape's meta
}
editor.on('event', onEvent)
signal.addEventListener('abort', () => editor.off('event', onEvent)) // REQUIRED
}
- Find buttons by
meta (or visible label via helpers.richTextToPlainText),
not by hard-coded coordinates.
- One script owns both build and read. If the shapes are created by one code
path (with
meta.action: 'inc') and the handler reads another convention
(meta.action === 'PLUS'), clicks silently do nothing. Ship the buttons built
by the same script that handles them, or ship an empty canvas so the script
builds them fresh — never pre-bake mismatched shapes into the file's db.
- Keep app state in a shape's
meta (e.g. meta.count) and render it as that
shape's richText label, so it survives save and is readable for verification.
- Detach the listener on
signal abort. Skipping this is not cosmetic: on
the next save the old onEvent stays attached alongside the new one, so every
click fires twice and a counter jumps by 2 instead of 1.
- For continuous motion use
editor.on('tick', fn); for a moving anchor with
attached pieces use helpers.onShapeTranslate(id, fn, { signal }).
Shipping a self-running scripted .tldraw
A .tldraw is a zip of metadata.json + session.json + db.sqlite + assets/
script/ (only those entries are packable). For the script to auto-run without
the "This document contains a script → Run Script" consent dialog:
metadata.json must carry a script manifest: { "sha256": "<digest>" }, where
the digest is sha256 over each sorted script/ path as `${path}\0${sha256hex(bytes)}\n`.
A mismatch is rejected as tampered.
- Pre-trust the digest by adding it to
~/.tldraw/script-trust.json
({ "trusted": ["<digest>"] }, or $TLDRAW_SCRIPT_TRUST). The app skips consent
when isScriptTrusted(digest) is true.
Procedure
- Read the current token/port from
server.json. Find the target doc with
api.getFocusedDoc() (or api.getDocs()); name it explicitly if several are
open.
- For layout/generation, use
/exec. For durable behavior, edit
script/main.js via /script-workspace.
- Make scripts idempotent: create durable shapes with
helpers.createShapeIfMissing
and stable createShapeId('name') ids. Scripts rerun on every load.
- Keep script-owned writes out of the user's undo stack:
editor.run(fn, { history: 'ignore' }) (or helpers.translateShapes, which
already does).
- For reactivity,
editor.store.listen(cb) and tear it down on signal abort.
For interaction, editor.on('event', h) (hit-test pointer_down in page
coords); for animation, editor.on('tick', h).
- For a single moving anchor + attached internals, prefer
helpers.onShapeTranslate(anchorId, fn, { signal }) over a broad store
listener — a broad listener can turn your own writes into feedback loops.
Shape props (validated against tldraw SDK v5 schema)
editor.createShape / createShapeIfMissing accept partial props (shape utils
fill defaults). When building raw records for a file snapshot, every prop
below is required (run scripts/validate_shapes.mjs):
| Shape |
Required props |
note |
richText, color, labelColor, size, font, align, verticalAlign, growY, fontSizeAdjustment, url, scale, textLastEditedBy |
text |
richText, color, size, font, textAlign, w, scale, autoSize |
frame |
w, h, name, color |
geo |
geo, w, h, color, fill, richText (+ dash/size/etc. defaulted) |
richText must be toRichText('...') — a bare string is rejected. color enum:
black grey light-violet violet blue light-blue yellow orange green light-green light-red red white. font enum: draw sans serif mono.
Pitfalls
store.listen fires on the tick AFTER a commit, not synchronously. If you
write a shape and immediately read state expecting the listener to have run, it
hasn't. Verified live: an in-turn read shows 0 fires; after one setTimeout
tick it shows 1. Same reason the app notes editor.dispatch is async — await a
tick before verifying.
ctx, not globals. The entry is export default function ({ editor, helpers, signal }). There is no bare editor global in a document script.
createShapeId / toRichText / Vec come from import ... from 'tldraw'.
richText, not text. Text/note/geo labels use richText: toRichText(s).
- Raw records need every prop;
createShape does not. In-app pass only the
props you care about; a hand-built .tldraw snapshot needs the full set (table).
- Scripts rerun on every load — be idempotent. Use
createShapeIfMissing
with stable ids or you duplicate content and clobber user edits.
- Clean up on
signal. signal.addEventListener('abort', () => stop()) for
every store.listen / editor.on / setInterval; the signal fires before
rerun and on close.
- Keep script writes out of undo:
editor.run(fn, { history: 'ignore' }).
editor.on('tick') pauses when the window is hidden (it is a RAF loop);
setInterval keeps firing but Electron throttles it to ~1/s in the background.
- The API needs the bearer token from
server.json; the port can be non-default
(server.listen(0) picks one) — always read the file, don't hardcode 7236.
- Only
tldraw / react / react-dom import — not a Node project.
Verification
- Shape schema (offline, no app):
node scripts/validate_shapes.mjs — builds
the real tldraw schema and validates note/text/frame. Passing prints 3/3.
- Live canvas edits: after
/exec, read back with /api/search →
api.getShapes(docId) (returns { page, viewport, shapes }) and
api.getBindings(docId) (array). Confirm expected shapes/bindings exist. Grab
api.getScreenshot(docId) (returns { filePath, ... }) and inspect the PNG/JPEG
with vision_analyze.
- Durable script applied:
GET /api/doc/:id/script-status. Success is
state: "applied" (currentDiskDigest === lastAppliedDigest === manifestSha256,
pendingApply === false, lastApplyError === null). If it stays "pending"
after a short retry, report that instead of claiming success; "error" means
the apply failed — read errorLogPath.
Source: NousResearch/hermes-agent → optional-skills/creative/tldraw-offline/SKILL.md
1---2name: tldraw-offline3description: "Drive and script tldraw offline canvases with an agent."4category: ai-agents-and-harness5source_repo: NousResearch/hermes-agent6source_path: "optional-skills/creative/tldraw-offline/SKILL.md"7source_url: https://github.com/NousResearch/hermes-agent/blob/HEAD/optional-skills/creative/tldraw-offline/SKILL.md8---91011# tldraw offline Skill1213Work with the tldraw offline desktop app (offline.tldraw.com): read the open14canvas, make edits, and write **document scripts** — JavaScript embedded in a15`.tldraw` file that runs on load and gives the file durable behavior. The app16runs a **local HTTP API** (default `localhost:7236`) that a coding agent drives17with plain `curl` from its terminal — this is exactly how the app's own homepage18demo (Codex editing a canvas live) works. The agent does NOT use computer-use /19GUI clicking, and does NOT hand-edit the `.tldraw` file directly. Keep tldraw20offline open while you work.2122## When to Use2324- The user has tldraw offline open and asks you to build or modify a canvas25 (diagrams, wireframes, layouts).26- You want to add durable behavior to a drawing (reactive shapes, interactive27 buttons, animation, connection logic) via an embedded document script.2829Do NOT hand-place shapes to imitate a drawing — write the code that generates30them. Agents are far better at scripting the canvas than at drawing on it.3132## Prerequisites3334- **tldraw offline installed and running**, with a document open. Releases:35 https://github.com/tldraw/tldraw-offline/releases/latest (macOS DMG, Windows36 x64/Arm64, Linux `x86_64`/`arm64` AppImage or amd64/arm64 `.deb`).37- **Agent skills installed in the app**: `Develop → Install Agent Skills`. The38 app writes its own tldraw skill into `~/.codex/skills/`, `~/.claude/skills/`,39 `~/.cursor/skills/`, and `~/.gemini/skills/` — teaching that agent the `curl`40 recipes below. (This Hermes skill mirrors that guidance for Hermes.)41- **The local control API.** On launch the app writes `server.json` to its config42 dir (Linux `~/.config/tldraw/`, macOS `~/Library/Application Support/tldraw/`,43 Windows `%APPDATA%\tldraw\`) with `port` (default `7236`), a bearer `token`,44 `pid`, and `startedAt`. Every request except `GET /` needs45 `Authorization: Bearer <token>`. A clean quit removes `server.json`; if it's46 present but the port doesn't answer, the app quit uncleanly — treat as not47 running.48- **Re-read port + token on EVERY shell call.** Each terminal call is a fresh49 shell, so an `export`ed token does not persist — "export once and reuse" sends50 an empty token and 401s. Read both inline at the top of each call:51 `PORT=$(jq -r .port <server.json>); TOKEN=$(jq -r .token <server.json>)`.52- No account or network needed for local editing.5354## How to Run5556Two distinct workflows. Pick by whether the change must survive a reload.5758**A. One-off canvas edits (`/exec`)** — layout, generating shapes, cleanup. This59is a live edit, not saved script:6061```bash62BASE=http://localhost:723663TOKEN=$(python3 -c "import json;print(json.load(open('$HOME/.config/tldraw/server.json'))['token'])")64# find the focused document id65DOC=$(curl -s "$BASE/api/search" -X POST -H 'content-type: application/json' \66 -H "Authorization: Bearer $TOKEN" \67 -d '{"code":"return (await api.getFocusedDoc()).id"}' | python3 -c "import sys,json;print(json.load(sys.stdin)['result'])")68# run code with the live `editor` + `helpers` in scope69curl -s "$BASE/api/doc/$DOC/exec" -X POST -H 'content-type: application/json' \70 -H "Authorization: Bearer $TOKEN" \71 -d '{"code":"const {createShapeId,toRichText}=await import(\"tldraw\"); editor.createShape({id:createShapeId(),type:\"geo\",x:0,y:0,props:{geo:\"rectangle\",w:200,h:100,color:\"blue\",fill:\"solid\",richText:toRichText(\"hello\")}}); return editor.getCurrentPageShapes().length"}'72```7374**B. Durable behavior (`script/main.js`)** — reactive/interactive logic that must75survive reload. Edit the file on disk; the app's watcher applies it:7677```bash78# get the live script file path for the doc79curl -s "$BASE/api/doc/$DOC/script-workspace" -X POST \80 -H "Authorization: Bearer $TOKEN" # -> result.mainJsPath, result.isDefaultScript81# edit result.mainJsPath with read_file / patch / write_file (see scripts/main.js)82# then confirm the watcher applied it:83curl -s "$BASE/api/doc/$DOC/script-status" -H "Authorization: Bearer $TOKEN"84```8586The ready-to-adapt document script is `scripts/main.js`.8788## Quick Reference8990The document-script contract (verified against the app's bundled91`script-context.d.ts`):9293```js94import { createShapeId, toRichText } from 'tldraw' // primitives: import, not globals9596export default function ({ editor, helpers, signal }) {97 editor.run(() => { // batch = one undo step98 helpers.createShapeIfMissing({ // idempotent furniture99 id: createShapeId('node-1'), type: 'geo', x: 0, y: 0,100 props: { geo: 'rectangle', w: 200, h: 100, richText: toRichText('hi') },101 })102 })103104 const stop = editor.store.listen(() => { /* react */ }) // fires the tick AFTER a commit105 signal.addEventListener('abort', () => stop()) // REQUIRED cleanup on rerun/close106}107```108109- `ctx.editor` — the live `Editor` (`createShape`, `updateShape`, `deleteShapes`,110 `getCurrentPageShapes`, `getShape`, `getBindingsFromShape`, `zoomToFit`,111 `on('tick'|'event', fn)`, `run(fn, { history: 'ignore' })`).112- `ctx.helpers` — `createShapeIfMissing`, `createShapesIfMissing`,113 `createArrowBetweenShapes(from, to, { arrowheadEnd })`, `translateShapes`,114 `onShapeTranslate(id, fn, { signal })`, `richTextToPlainText`, `boxShapes`,115 `getLints`.116- `ctx.signal` — `AbortSignal`; attach every listener/interval teardown to it.117- `config.js` (separate file) registers custom shape/tool/component utils and118 runs before mount; `main.js` runs against the mounted editor and reruns on save.119120## Interactive UI (clickable buttons that drive state)121122Drawn shapes can behave like a real app — the thing a static whiteboard can't do.123Full example: `scripts/counter.js` (a number display + MINUS/RESET/PLUS buttons).124125Verification boundary — read this before claiming interaction works or doesn't.126The app's OWN agent playbook says to verify a clickable-UI script with "one127simulated click and one state read" via `/exec` (`editor.dispatch` a pointer128event, await a tick, read the shape's state) — NOT by driving a real mouse. By129that standard the counter is verified: dispatched clicks stepped it130`0 → 1 → 2 → 1 → 0`. Two caveats worth writing down:131- **The script only runs once the app's file-watcher applies it.** On Linux that132 watcher uses inotify; a host with an exhausted `fs.inotify.max_user_instances`133 logs `inotify_add_watch ... No space left on device`, `script-status` shows134 `state: "not-watching"` / `hasEntry: false`, and the script never executes.135 This is a host limit, not a script bug — a normal desktop is unaffected.136- **Driving the canvas with computer-use needs FOREGROUND delivery, not137 background.** cua-driver's default `background` delivery returns138 `background_unavailable` against Electron's occluded renderer — but that is the139 first rung, not a wall. cua-driver returns an `escalation: "foreground"` hint;140 set `delivery_mode: "foreground"` (pair with `bring_to_front`) and its X11 XTest141 path (`x11_xtest_fg`) clicks land — this dismisses the "Run Script" consent142 dialog and clicks the canvas. Verified by the Cua team on this exact v1.11.0143 AppImage (Linux/X11). Do NOT conclude "Electron rejects synthetic clicks" and144 give up in background mode — climb to foreground. (The real product path is145 still `/exec`, not clicks; this note is for computer-use-driven testing.)146147The pattern:148149```js150export default function ({ editor, helpers, signal }) {151 // 1. Build buttons idempotently; tag each with meta so the handler finds them.152 // Give buttons a visible label AND a meta.action.153 // 2. Hit-test pointer_down in PAGE coordinates against the button bounds:154 const inside = (b, p) => p.x >= b.x && p.x <= b.x + b.w && p.y >= b.y && p.y <= b.y + b.h155 function onEvent(info) {156 if (!info || info.name !== 'pointer_down') return157 let p = null158 try { if (info.point && editor.screenToPage) p = editor.screenToPage(info.point) } catch {}159 p = p ?? editor.inputs?.currentPagePoint160 if (!p) return161 const hit = editor.getCurrentPageShapes().find(162 (s) => s.meta?.ui === 'button' &&163 inside({ x: s.x, y: s.y, w: s.props.w, h: s.props.h }, p)164 )165 if (hit) runAction(hit.meta.action) // mutate state; store it in a shape's meta166 }167 editor.on('event', onEvent)168 signal.addEventListener('abort', () => editor.off('event', onEvent)) // REQUIRED169}170```171172- Find buttons by `meta` (or visible label via `helpers.richTextToPlainText`),173 not by hard-coded coordinates.174- **One script owns both build and read.** If the shapes are created by one code175 path (with `meta.action: 'inc'`) and the handler reads another convention176 (`meta.action === 'PLUS'`), clicks silently do nothing. Ship the buttons built177 by the same script that handles them, or ship an empty canvas so the script178 builds them fresh — never pre-bake mismatched shapes into the file's db.179- Keep app state in a shape's `meta` (e.g. `meta.count`) and render it as that180 shape's `richText` label, so it survives save and is readable for verification.181- **Detach the listener on `signal` abort.** Skipping this is not cosmetic: on182 the next save the old `onEvent` stays attached alongside the new one, so every183 click fires twice and a counter jumps by 2 instead of 1.184- For continuous motion use `editor.on('tick', fn)`; for a moving anchor with185 attached pieces use `helpers.onShapeTranslate(id, fn, { signal })`.186187### Shipping a self-running scripted `.tldraw`188189A `.tldraw` is a zip of `metadata.json` + `session.json` + `db.sqlite` + `assets/`190+ `script/` (only those entries are packable). For the script to auto-run without191the "This document contains a script → Run Script" consent dialog:192193- `metadata.json` must carry a `script` manifest: `{ "sha256": "<digest>" }`, where194 the digest is `sha256` over each sorted `script/` path as `` `${path}\0${sha256hex(bytes)}\n` ``.195 A mismatch is rejected as tampered.196- Pre-trust the digest by adding it to `~/.tldraw/script-trust.json`197 (`{ "trusted": ["<digest>"] }`, or `$TLDRAW_SCRIPT_TRUST`). The app skips consent198 when `isScriptTrusted(digest)` is true.199200## Procedure2012021. Read the current token/port from `server.json`. Find the target doc with203 `api.getFocusedDoc()` (or `api.getDocs()`); name it explicitly if several are204 open.2052. For layout/generation, use `/exec`. For durable behavior, edit206 `script/main.js` via `/script-workspace`.2073. Make scripts idempotent: create durable shapes with `helpers.createShapeIfMissing`208 and stable `createShapeId('name')` ids. Scripts rerun on every load.2094. Keep script-owned writes out of the user's undo stack:210 `editor.run(fn, { history: 'ignore' })` (or `helpers.translateShapes`, which211 already does).2125. For reactivity, `editor.store.listen(cb)` and tear it down on `signal` abort.213 For interaction, `editor.on('event', h)` (hit-test `pointer_down` in page214 coords); for animation, `editor.on('tick', h)`.2156. For a single moving anchor + attached internals, prefer216 `helpers.onShapeTranslate(anchorId, fn, { signal })` over a broad store217 listener — a broad listener can turn your own writes into feedback loops.218219## Shape props (validated against tldraw SDK v5 schema)220221`editor.createShape` / `createShapeIfMissing` accept partial props (shape utils222fill defaults). When building **raw records** for a file snapshot, every prop223below is required (run `scripts/validate_shapes.mjs`):224225| Shape | Required props |226|-------|----------------|227| `note` | `richText`, `color`, `labelColor`, `size`, `font`, `align`, `verticalAlign`, `growY`, `fontSizeAdjustment`, `url`, `scale`, `textLastEditedBy` |228| `text` | `richText`, `color`, `size`, `font`, `textAlign`, `w`, `scale`, `autoSize` |229| `frame` | `w`, `h`, `name`, `color` |230| `geo` | `geo`, `w`, `h`, `color`, `fill`, `richText` (+ dash/size/etc. defaulted) |231232`richText` must be `toRichText('...')` — a bare string is rejected. `color` enum:233`black grey light-violet violet blue light-blue yellow orange green light-green234light-red red white`. `font` enum: `draw sans serif mono`.235236## Pitfalls237238- **`store.listen` fires on the tick AFTER a commit, not synchronously.** If you239 write a shape and immediately read state expecting the listener to have run, it240 hasn't. Verified live: an in-turn read shows 0 fires; after one `setTimeout`241 tick it shows 1. Same reason the app notes `editor.dispatch` is async — await a242 tick before verifying.243- **`ctx`, not globals.** The entry is `export default function ({ editor,244 helpers, signal })`. There is no bare `editor` global in a document script.245 `createShapeId` / `toRichText` / `Vec` come from `import ... from 'tldraw'`.246- **`richText`, not `text`.** Text/note/geo labels use `richText: toRichText(s)`.247- **Raw records need every prop; `createShape` does not.** In-app pass only the248 props you care about; a hand-built `.tldraw` snapshot needs the full set (table).249- **Scripts rerun on every load — be idempotent.** Use `createShapeIfMissing`250 with stable ids or you duplicate content and clobber user edits.251- **Clean up on `signal`.** `signal.addEventListener('abort', () => stop())` for252 every `store.listen` / `editor.on` / `setInterval`; the signal fires before253 rerun and on close.254- **Keep script writes out of undo:** `editor.run(fn, { history: 'ignore' })`.255- **`editor.on('tick')` pauses when the window is hidden** (it is a RAF loop);256 `setInterval` keeps firing but Electron throttles it to ~1/s in the background.257- **The API needs the bearer token** from `server.json`; the port can be non-default258 (`server.listen(0)` picks one) — always read the file, don't hardcode `7236`.259- **Only `tldraw` / `react` / `react-dom` import** — not a Node project.260261## Verification262263- **Shape schema (offline, no app):** `node scripts/validate_shapes.mjs` — builds264 the real tldraw schema and validates note/text/frame. Passing prints `3/3`.265- **Live canvas edits:** after `/exec`, read back with `/api/search` →266 `api.getShapes(docId)` (returns `{ page, viewport, shapes }`) and267 `api.getBindings(docId)` (array). Confirm expected shapes/bindings exist. Grab268 `api.getScreenshot(docId)` (returns `{ filePath, ... }`) and inspect the PNG/JPEG269 with `vision_analyze`.270- **Durable script applied:** `GET /api/doc/:id/script-status`. Success is271 `state: "applied"` (`currentDiskDigest === lastAppliedDigest === manifestSha256`,272 `pendingApply === false`, `lastApplyError === null`). If it stays `"pending"`273 after a short retry, report that instead of claiming success; `"error"` means274 the apply failed — read `errorLogPath`.275276---277278**Source:** [`NousResearch/hermes-agent`](https://github.com/NousResearch/hermes-agent) → `optional-skills/creative/tldraw-offline/SKILL.md`