mirror of
https://github.com/earendil-works/pi.git
synced 2026-10-16 05:10:08 +08:00
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
64 lines
2.5 KiB
TypeScript
64 lines
2.5 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { CodemodeSourceError, parseCodemodeSource } from "../src/source.ts";
|
|
|
|
describe("parseCodemodeSource", () => {
|
|
it("returns plain code unchanged", () => {
|
|
expect(parseCodemodeSource("text('hi')")).toEqual({ code: "text('hi')", options: {} });
|
|
expect(parseCodemodeSource("// just a comment\nreturn 1")).toEqual({
|
|
code: "// just a comment\nreturn 1",
|
|
options: {},
|
|
});
|
|
});
|
|
|
|
it("parses the options line and keeps line numbers", () => {
|
|
expect(parseCodemodeSource('// @options: {"timeout_ms": 10}\nconst a = 1;\ntext(a)')).toEqual({
|
|
code: "\nconst a = 1;\ntext(a)",
|
|
options: { timeoutMs: 10 },
|
|
});
|
|
expect(parseCodemodeSource(' // @options:{"max_output_tokens":0,"timeout_ms":1500}\r\ntext(1)').options).toEqual(
|
|
{
|
|
maxOutputTokens: 0,
|
|
timeoutMs: 1500,
|
|
},
|
|
);
|
|
expect(parseCodemodeSource("// @options: {}\ntext(1)")).toEqual({ code: "\ntext(1)", options: {} });
|
|
});
|
|
|
|
it("only treats the first line as an options line", () => {
|
|
const input = 'text(1)\n// @options: {"timeout_ms": 1}';
|
|
expect(parseCodemodeSource(input)).toEqual({ code: input, options: {} });
|
|
expect(parseCodemodeSource("// @optionsx {}\ntext(1)").options).toEqual({});
|
|
});
|
|
|
|
it("rejects empty input and invalid options", () => {
|
|
const cases: [string, string | RegExp][] = [
|
|
["", /Expected JavaScript source text \(non-empty\)/],
|
|
[" \n", /Expected JavaScript source text \(non-empty\)/],
|
|
["// @options:\ntext(1)", /@options must be a JSON object with supported fields/],
|
|
["// @options: {timeout_ms: 1}\ntext(1)", /@options must be valid JSON with supported fields/],
|
|
["// @options: [1]\ntext(1)", /@options must be a JSON object with supported fields/],
|
|
[
|
|
'// @options: {"yield": 1}\ntext(1)',
|
|
"@options only supports `max_output_tokens` and `timeout_ms`; got `yield`",
|
|
],
|
|
[
|
|
'// @options: {"max_output_tokens": 1.5}\ntext(1)',
|
|
"@options field `max_output_tokens` must be a non-negative safe integer",
|
|
],
|
|
['// @options: {"timeout_ms": 0}\ntext(1)', /@options field `timeout_ms` must be a positive integer/],
|
|
[
|
|
'// @options: {"timeout_ms": 1}',
|
|
"The @options line must be followed by JavaScript source on subsequent lines",
|
|
],
|
|
[
|
|
'// @options: {"timeout_ms": 1}\n \n',
|
|
"The @options line must be followed by JavaScript source on subsequent lines",
|
|
],
|
|
];
|
|
for (const [input, message] of cases) {
|
|
expect(() => parseCodemodeSource(input), input).toThrow(CodemodeSourceError);
|
|
expect(() => parseCodemodeSource(input), input).toThrow(message);
|
|
}
|
|
});
|
|
});
|