Files
pi/packages/coding-agent/test/edit-tool-legacy-input.test.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

117 lines
3.7 KiB
TypeScript

import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import type { ExtensionToolContext } from "../src/core/extensions/types.ts";
import { createEditToolDefinition } from "../src/core/tools/edit.ts";
const tempDirs: string[] = [];
async function createTempDir(): Promise<string> {
const dir = await mkdtemp(join(tmpdir(), "pi-edit-legacy-input-"));
tempDirs.push(dir);
return dir;
}
afterEach(async () => {
await Promise.all(tempDirs.splice(0, tempDirs.length).map((dir) => rm(dir, { recursive: true, force: true })));
});
describe("edit tool prepareArguments", () => {
it("keeps legacy fields out of the public schema", () => {
const definition = createEditToolDefinition(process.cwd());
expect(definition.parameters.properties).not.toHaveProperty("oldText");
expect(definition.parameters.properties).not.toHaveProperty("newText");
});
it("folds top-level oldText/newText into edits", () => {
const definition = createEditToolDefinition(process.cwd());
const prepared = definition.prepareArguments!({
path: "file.txt",
oldText: "before",
newText: "after",
});
expect(prepared).toEqual({
path: "file.txt",
edits: [{ oldText: "before", newText: "after" }],
});
});
it("appends legacy replacement to existing edits", () => {
const definition = createEditToolDefinition(process.cwd());
const prepared = definition.prepareArguments!({
path: "file.txt",
edits: [{ oldText: "a", newText: "b" }],
oldText: "c",
newText: "d",
});
expect(prepared).toEqual({
path: "file.txt",
edits: [
{ oldText: "a", newText: "b" },
{ oldText: "c", newText: "d" },
],
});
});
it("passes through valid input unchanged", () => {
const definition = createEditToolDefinition(process.cwd());
const input = {
path: "file.txt",
edits: [{ oldText: "a", newText: "b" }],
};
const prepared = definition.prepareArguments!(input);
expect(prepared).toBe(input);
});
it("passes through non-object input unchanged", () => {
const definition = createEditToolDefinition(process.cwd());
expect(definition.prepareArguments!(null)).toBe(null);
expect(definition.prepareArguments!(undefined)).toBe(undefined);
expect(definition.prepareArguments!("garbage")).toBe("garbage");
});
it("prepared args execute correctly", async () => {
const dir = await createTempDir();
const filePath = join(dir, "legacy.txt");
await writeFile(filePath, "before\n", "utf8");
const definition = createEditToolDefinition(dir);
const prepared = definition.prepareArguments!({
path: "legacy.txt",
oldText: "before",
newText: "after",
});
const result = await definition.execute("tool-1", prepared, undefined, undefined, {} as ExtensionToolContext);
expect(result.content).toEqual([{ type: "text", text: "Successfully replaced 1 block(s) in legacy.txt." }]);
expect(await readFile(filePath, "utf8")).toBe("after\n");
});
});
describe("edit tool stringified edits", () => {
it("parses edits from a JSON string", () => {
const definition = createEditToolDefinition(process.cwd());
const prepared = definition.prepareArguments!({
path: "file.txt",
edits: JSON.stringify([{ oldText: "a", newText: "b" }]),
});
expect(prepared).toEqual({
path: "file.txt",
edits: [{ oldText: "a", newText: "b" }],
});
});
it("leaves edits alone when the string is not valid JSON", () => {
const definition = createEditToolDefinition(process.cwd());
const prepared = definition.prepareArguments!({
path: "file.txt",
edits: "not json",
});
expect(prepared).toEqual({
path: "file.txt",
edits: "not json",
});
});
});