Artifact 16d6f6b8329d1bcb9cf75d93d5982b6075977f8acf79c2caa20829a31591e64f:
- File docs/manual/codemode.md — part of check-in [62f7860a2f] at 2026-11-25 18:12:04 on branch codemode — Add the codemode tool: Pi's scripts on QuickJS (user: dmestas, size: 4529) [annotate] [blame] [check-ins using]
Codemode
The codemode tool lets the model write a JavaScript script that calls pic's other tools. Only
the script's output reaches the model, so one script can run calls at once and filter large results
before the model sees them. It is Pi's codemode, run by QuickJS in a process of its own.
Turn it on
Codemode is off by default. Turn it on for new conversations with the setting, in /settings
(Code mode) or from the shell:
pic settings codemode true
/codemode in the terminal switches it on or off for the session you are in, the conversation open
now included, and says which. PIC_CODEMODE=on or off decides it for one process, before the
setting. --tools +codemode and --exclude-tools codemode choose it like any tool.
Scripts
The tool's input is {"script": "..."}: the JavaScript source as a JSON string. It runs as the body
of an async function, so top-level await and return work. The script has no file system,
network, timers or operating system; it reaches the outside world only through tools.
A script may start with an options line. It must be the first line:
// @options: {"max_output_tokens": 2000, "timeout_ms": 60000}
max_output_tokens(default 10000) limits the output. Longer output keeps its start and end, and the whole is kept with the call: the result names it aspic:output/N, which the read tool opens.timeout_msis a deadline for the whole script. It is unset by default. A tool call that is running at the deadline finishes first; the script then ends.
The result starts with Script completed or Script failed, the wall time, and the output. Text
items appear in order; with more than one, each starts with a ==> text N/M <== line. console
calls follow in one <console_output> block. A failed script keeps its partial output, followed
by Script error:, the error and the calls it made. Tool calls are real: calls made before a
failure are not undone.
Globals
| Global | Purpose |
|---|---|
tools.<name>(args) |
call a tool; resolves to its text output, rejects with an Error carrying its error text |
text(value) |
add a text item: strings as they are, other values as JSON |
image(value) |
add an image: a base64 data: URL, an { image_url } object or an image block |
console.log(...) |
add a line to the <console_output> block; info, warn, error and debug too |
return value |
a top-level return adds the value like text() |
exit() |
end the script successfully |
store(key, value) / load(key) |
keep small JSON values for the rest of the script |
ALL_TOOLS |
every tool the script can call, as { name, description } |
searchTools(query, { limit?, namespace? }) |
rank the tools by relevance (default limit 8) |
describeTool(name) |
a tool's description and declaration, or undefined |
describeNamespace(name) |
an MCP server's tools, or undefined |
A tool is named by its identifier: characters not valid in a JavaScript name become _, so
mcp__dev-radius__search is tools.mcp__dev_radius__search. A read of an image resolves to an
image block { type: "image", data, mimeType, note } that image() shows. The first image a script
shows is attached to its result; the others are named in the output. Use Promise.allSettled() to
keep the results of the calls that succeed.
What the calls are
Each call a script makes is a tool call nested in the script's: it is journaled under it, it goes
through the PreToolUse hooks, with the script's call as parent_tool_use_id, and it runs as a call
of the model's would, at once with the others the script is waiting on. Ordinary PostToolUse hooks
run on each; a required PostToolUse hook runs on the script's output alone. The terminal shows the
script, its calls with their status, then the output and the time it took. Scripts cannot start
other scripts.
Limits
- A script's heap is 256 MB; past it the script gets
InternalError: out of memory. Its stack is 1 MB; deep recursion getsInternalError: stack overflow. - A script may make 256 tool calls; later calls reject.
- Ctrl-C ends a running script with
Script aborted, and the calls it was waiting on with it. - A script waiting on a promise that can never settle, with no call pending, fails at once.
After a crash
A script is never run again. When pic dies while one runs, the next pic records its outcome as unknown, and the result lists each call the script made: finished, outcome unknown, or not run.