pi-mono/packages/codemode/src/source.ts
Armin Ronacher 8562bcf66a feat(coding-agent): codemode and MCP
This adds codemode and MCP support to pi. Codemode (packages/codemode) runs
model-written JavaScript in a QuickJS wasm VM inside a worker. From there the
script calls pi's tools as async functions, uses a per-session store, and can
read the model catalog and run classifiers. The codemode tool in coding-agent
is compatible with Codex's exec tool, and codemode.mode works like Codex's
tool modes (on or only). Large results are written to a JSON temp file. MCP
(packages/mcp) is a standalone client for stdio and streamable HTTP servers,
with OAuth.

The core only gets general mechanisms; codemode, tool_search and MCP are
built-in extensions that use them. Other extensions can replace these
built-ins. Tools declare an exposure: direct, model-only, codemode, deferred
or hidden. prepareLoadout() lets an orchestrating tool change which tool
declarations the model sees. ctx.executeTool() runs nested calls through
validation and the tool_call/tool_result hooks. Those calls emit events with
parentToolCallId and are recorded as bounded nestedCalls on the parent
result, which compaction and HTML export use. Tools can also return
structuredContent with an outputSchema, and can report errors with isError
instead of throwing. MCP servers come from mcp.json (global, or per project
after the project is trusted) or from pi.registerMcpServer(). They are
managed with /mcp and pi mcp list|login|logout, and their runtimes load on
first use.

Closes #10040
2026-09-29 09:12:50 +02:00

115 lines
4.2 KiB
TypeScript

/**
* Codemode source format: JavaScript, optionally preceded by one options line.
*
* ```js
* // @options: {"max_output_tokens": 2000, "timeout_ms": 30000}
* const text = await tools.read({ path: "package.json" });
* text(JSON.parse(text).name);
* ```
*/
export const CODEMODE_OPTIONS_PREFIX = "// @options:";
const SUPPORTED_FIELDS = ["max_output_tokens", "timeout_ms"] as const;
const SUPPORTED_FIELDS_TEXT = "`max_output_tokens` and `timeout_ms`";
/** Largest delay `setTimeout` supports, which bounds `timeout_ms`. */
const MAX_TIMEOUT_MS = 2_147_483_647;
/**
* Lark grammar for providers with grammar-constrained tool input. It only fixes the shape of the
* options line; the options JSON and the code are checked by {@link parseCodemodeSource}.
*/
export const CODEMODE_SOURCE_GRAMMAR = String.raw`
start: options_source | plain_source
options_source: OPTIONS_LINE NEWLINE SOURCE
plain_source: SOURCE
OPTIONS_LINE: /[ \t]*\/\/ @options:[^\r\n]*/
NEWLINE: /\r?\n/
SOURCE: /[\s\S]+/
`;
export interface CodemodeSourceOptions {
/** Token budget for the script's output. */
maxOutputTokens?: number;
/** Hard deadline for the whole script in milliseconds, including tool calls. */
timeoutMs?: number;
}
export interface ParsedCodemodeSource {
/** The script with the options line replaced by an empty line, so line numbers are unchanged. */
code: string;
options: CodemodeSourceOptions;
}
export class CodemodeSourceError extends Error {
constructor(message: string) {
super(message);
this.name = "CodemodeSourceError";
}
}
function isSafeInteger(value: unknown): value is number {
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
}
function parseOptions(directive: string): CodemodeSourceOptions {
if (directive === "") {
throw new CodemodeSourceError(`@options must be a JSON object with supported fields ${SUPPORTED_FIELDS_TEXT}`);
}
let value: unknown;
try {
value = JSON.parse(directive);
} catch (error) {
throw new CodemodeSourceError(
`@options must be valid JSON with supported fields ${SUPPORTED_FIELDS_TEXT}: ${error instanceof Error ? error.message : String(error)}`,
);
}
if (typeof value !== "object" || value === null || Array.isArray(value)) {
throw new CodemodeSourceError(`@options must be a JSON object with supported fields ${SUPPORTED_FIELDS_TEXT}`);
}
const fields = value as Record<string, unknown>;
for (const key of Object.keys(fields)) {
if (!(SUPPORTED_FIELDS as readonly string[]).includes(key)) {
throw new CodemodeSourceError(`@options only supports ${SUPPORTED_FIELDS_TEXT}; got \`${key}\``);
}
}
const options: CodemodeSourceOptions = {};
const { max_output_tokens, timeout_ms } = fields;
if (max_output_tokens !== undefined) {
if (!isSafeInteger(max_output_tokens)) {
throw new CodemodeSourceError("@options field `max_output_tokens` must be a non-negative safe integer");
}
options.maxOutputTokens = max_output_tokens;
}
if (timeout_ms !== undefined) {
if (!isSafeInteger(timeout_ms) || timeout_ms === 0 || timeout_ms > MAX_TIMEOUT_MS) {
throw new CodemodeSourceError(
`@options field \`timeout_ms\` must be a positive integer up to ${MAX_TIMEOUT_MS}`,
);
}
options.timeoutMs = timeout_ms;
}
return options;
}
/**
* Split an optional first-line `// @options: {...}` from the script. Throws
* {@link CodemodeSourceError} for empty input and invalid options.
*/
export function parseCodemodeSource(input: string): ParsedCodemodeSource {
if (input.trim() === "") {
throw new CodemodeSourceError(
'Expected JavaScript source text (non-empty). Provide JS only, optionally with a first line `// @options: {"max_output_tokens": 1000}`.',
);
}
const newline = input.indexOf("\n");
const firstLine = (newline === -1 ? input : input.slice(0, newline)).replace(/\r$/, "");
const trimmed = firstLine.trimStart();
if (!trimmed.startsWith(CODEMODE_OPTIONS_PREFIX)) return { code: input, options: {} };
const code = newline === -1 ? "" : input.slice(newline);
if (code.trim() === "") {
throw new CodemodeSourceError("The @options line must be followed by JavaScript source on subsequent lines");
}
return { code, options: parseOptions(trimmed.slice(CODEMODE_OPTIONS_PREFIX.length).trim()) };
}