Skip to content
pi-c Codemode
Login

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}

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.

Calls return values

A tool call returns its result: const text = tools.read({ path: "README.md" }). pic runs the call while the script waits. To run calls at once, give them to parallel():

const [a, b] = parallel([
  () => tools.read({ path: "a.txt" }),
  { tool: "read", args: { path: "b.txt" } },
]);

Each item makes one tool call, as a function that returns it or as { tool, args }; the results come back in the items' order. A failed call makes parallel() throw its error once every call has ended; parallel(items, { settled: true }) returns { status: "fulfilled", value } or { status: "rejected", reason } for each instead. Scripts written for Pi still run: await on a call's value works, and so does Promise.all over calls, but those calls run one after another.

Globals

Global Purpose
tools.<name>(args) call a tool; returns its text output, throws an Error carrying its error text
parallel(items, { settled? }) run tool calls at once; their results in order
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 returns 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.

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; the calls of one parallel() run at once. Ordinary PostToolUse hooks run on each; a required PostToolUse hook runs on the script's output alone. An inner write or edit keeps the file as it found it, so /rewind puts back what a script changed, and an inner command's whole output is kept as pic:nested/N for the read tool. The terminal shows the script, its calls with their status, then the output and the time it took. Scripts cannot start other scripts, and a pic runs one script at a time.

Limits

On a build without processes

A build of pic for a platform with no processes runs scripts on Micro QuickJS, in pic's own process, and its codemode description says so. The script API is the same, but the language is ES5 with for...of over arrays and **: write var and function. It has no let or const, arrow functions, template literals, destructuring, spread, optional chaining, classes, Map, Set, promises, async or await, and few of the newer library methods (Object.entries, Array.from, find, includes, startsWith, padStart); Date has only Date.now(). A script that uses them fails with a SyntaxError or TypeError. parallel() still runs calls 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.