4.2 KiB
Run code in a persistent kernel, using a series of codeblocks acting as cells.
Each cell is introduced by a header line of the form:===== <info> =====
where each side is at least 5 equal signs. Everything between one header and the next (or end of input) is the cell's code, verbatim. The info is space-separated tokens, all optional, in any order:
- Language: {{#if py}}
pyfor Python{{/if}}{{#ifAll py js}}, {{/ifAll}}{{#if js}}js/tsfor JavaScript{{/if}}.{{#ifAll py js}} Omitted → inherit the previous cell's language (the first cell defaults to Python, falling back to JavaScript when Python is unavailable).{{else}} Omitted → inherit the previous cell's language.{{/ifAll}} - Title shorthand:
py:"…",js:"…",ts:"…"set the language and the cell title together. - Attributes:
id:"…"— cell title (when language is unchanged or already set).t:<duration>— per-cell timeout. Duration is digits with optionalms/s/munits (e.g.t:500ms,t:15s,t:2m). Default 30s.rst— wipe this cell's own language kernel before running.{{#ifAll py js}} Other languages are untouched.{{/ifAll}}
Work incrementally: one logical step per cell (imports, define, test, use). Pass multiple small cells in one call. Define small reusable functions you can debug individually. You MUST put workflow explanations in the assistant message or cell title — never inside cell code.
On failure: errors identify the failing cell (e.g., "Cell 3 failed"). Resubmit only the fixed cell (or fixed cell + remaining cells).
{{#ifAll py js}}The same helpers are available in both runtimes with the same positional argument order. Python takes the trailing options as keyword args; JavaScript takes the same options as a trailing object literal. JavaScript helpers are async and `await`able; Python helpers run synchronously.{{else}}{{#if py}}Helpers run synchronously. Trailing options are passed as keyword arguments.{{/if}}{{#if js}}Helpers are async and `await`able. Trailing options are passed as a final object literal.{{/if}}{{/ifAll}} ``` display(value) → None Render a value in the current cell output. print(value, ...) → None Print to the cell's text output. read(path, offset?=1, limit?=None) → str Read file contents as text. offset/limit are 1-indexed line bounds. write(path, content) → str Write content to a file (creates parent directories). Returns the resolved path. append(path, content) → str Append content to a file. Returns the resolved path. tree(path?=".", max_depth?=3, show_hidden?=False) → str Render a directory tree. diff(a, b) → str Unified diff between two files. run(cmd, cwd?=None, timeout?=None) → {stdout, stderr, exit_code} Run a shell command. env(key?=None, value?=None) → str | None | dict No args → full environment as dict. One arg → value of `key`. Two args → set `key=value` and return value. output(*ids, format?="raw", query?=None, offset?=None, limit?=None) → str | dict | list[dict] Read task/agent output by ID. Single id returns text/dict; multiple ids return a list. ```{{#if js}}JavaScript only: tool.<name>(args) invokes any session tool directly (e.g. await tool.read({ path: "src/foo.ts" })).
{{/if}}
===== py:"load config" ===== data = json.loads(read('package.json')) display(data) {{/if}}{{#ifAll py js}} {{/ifAll}}{{#if js}}===== js:"js summary" rst ===== const data = JSON.parse(await read('package.json')); display(data); return data.name; {{/if}}