mirror of
https://github.com/badlogic/pi-mono.git
synced 2026-11-05 23:37:25 +00: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
208 lines
6.3 KiB
TypeScript
208 lines
6.3 KiB
TypeScript
import { describe, expect, test } from "vitest";
|
|
import type { Skill } from "../src/core/skills.ts";
|
|
import { createSyntheticSourceInfo } from "../src/core/source-info.ts";
|
|
import { buildSystemPrompt } from "../src/core/system-prompt.ts";
|
|
|
|
const testSkill: Skill = {
|
|
name: "test-skill",
|
|
description: "A test skill.",
|
|
filePath: "/skills/test-skill/SKILL.md",
|
|
baseDir: "/skills/test-skill",
|
|
sourceInfo: createSyntheticSourceInfo("/skills/test-skill/SKILL.md", { source: "test" }),
|
|
disableModelInvocation: false,
|
|
};
|
|
|
|
describe("buildSystemPrompt", () => {
|
|
describe("empty tools", () => {
|
|
test("shows (none) for empty tools list", () => {
|
|
const prompt = buildSystemPrompt({
|
|
selectedTools: [],
|
|
contextFiles: [],
|
|
skills: [],
|
|
cwd: process.cwd(),
|
|
});
|
|
|
|
expect(prompt).toContain("<tools>\n(none)\n");
|
|
});
|
|
|
|
test("shows file paths guideline even with no tools", () => {
|
|
const prompt = buildSystemPrompt({
|
|
selectedTools: [],
|
|
contextFiles: [],
|
|
skills: [],
|
|
cwd: process.cwd(),
|
|
});
|
|
|
|
expect(prompt).toContain("Show file paths clearly");
|
|
});
|
|
});
|
|
|
|
describe("prompt structure", () => {
|
|
test("keeps the default and custom prompt prefixes exact", () => {
|
|
const defaultPrompt = buildSystemPrompt({ cwd: "/tmp", selectedTools: [], contextFiles: [], skills: [] });
|
|
const customPrompt = buildSystemPrompt({
|
|
customPrompt: "You are Exact.",
|
|
cwd: "/tmp",
|
|
selectedTools: [],
|
|
contextFiles: [],
|
|
skills: [],
|
|
});
|
|
|
|
expect(defaultPrompt.startsWith("You are an expert coding assistant operating inside pi")).toBe(true);
|
|
expect(customPrompt.startsWith("You are Exact.\n\n<cwd>")).toBe(true);
|
|
});
|
|
|
|
test("preserves an exact forced prompt without sections", () => {
|
|
expect(buildSystemPrompt({ forceSystemPrompt: "exact", cwd: "/tmp" })).toBe("exact");
|
|
});
|
|
|
|
test("maps appended instructions and project context to stable sections", () => {
|
|
const prompt = buildSystemPrompt({
|
|
customPrompt: "You are Exact.",
|
|
appendSystemPrompt: "Additional instructions.",
|
|
contextFiles: [{ path: "/tmp/AGENTS.md", content: "Project instructions." }],
|
|
selectedTools: [],
|
|
skills: [],
|
|
cwd: "/tmp",
|
|
});
|
|
|
|
expect(prompt).toContain("<addendum>\nAdditional instructions.\n</addendum>");
|
|
expect(prompt).toContain(
|
|
'<project_context>\nProject-specific instructions and guidelines:\n\n<project_instructions path="/tmp/AGENTS.md">',
|
|
);
|
|
expect(prompt).toContain("<cwd>\n/tmp\n</cwd>");
|
|
});
|
|
});
|
|
|
|
describe("default tools", () => {
|
|
test("includes all default tools when snippets are provided", () => {
|
|
const prompt = buildSystemPrompt({
|
|
toolSnippets: {
|
|
read: "Read file contents",
|
|
bash: "Execute bash commands",
|
|
edit: "Make surgical edits",
|
|
write: "Create or overwrite files",
|
|
},
|
|
contextFiles: [],
|
|
skills: [],
|
|
cwd: process.cwd(),
|
|
});
|
|
|
|
expect(prompt).toContain("- read:");
|
|
expect(prompt).toContain("- bash:");
|
|
expect(prompt).toContain("- edit:");
|
|
expect(prompt).toContain("- write:");
|
|
});
|
|
|
|
test.each([
|
|
[["powershell"], "Use PowerShell for file operations"],
|
|
[["bash", "powershell"], "Use bash or PowerShell for file operations"],
|
|
] as const)("uses shell-specific guidance for %j", (selectedTools, expected) => {
|
|
const prompt = buildSystemPrompt({
|
|
selectedTools: [...selectedTools],
|
|
contextFiles: [],
|
|
skills: [],
|
|
cwd: process.cwd(),
|
|
});
|
|
|
|
expect(prompt).toContain(expected);
|
|
});
|
|
|
|
test("instructs models to resolve pi docs and examples under absolute base paths", () => {
|
|
const prompt = buildSystemPrompt({
|
|
contextFiles: [],
|
|
skills: [],
|
|
cwd: process.cwd(),
|
|
});
|
|
|
|
expect(prompt).toContain(
|
|
"- When reading pi docs or examples, resolve docs/... under Additional docs and examples/... under Examples, not the current working directory",
|
|
);
|
|
expect(prompt).toContain("environment variables (docs/environment-variables.md), MCP servers (docs/mcp.md)");
|
|
});
|
|
});
|
|
|
|
describe("custom tool snippets", () => {
|
|
test("includes custom tools in available tools section when promptSnippet is provided", () => {
|
|
const prompt = buildSystemPrompt({
|
|
selectedTools: ["read", "dynamic_tool"],
|
|
toolSnippets: {
|
|
dynamic_tool: "Run dynamic test behavior",
|
|
},
|
|
contextFiles: [],
|
|
skills: [],
|
|
cwd: process.cwd(),
|
|
});
|
|
|
|
expect(prompt).toContain("- dynamic_tool: Run dynamic test behavior");
|
|
});
|
|
|
|
test("omits custom tools from available tools section when promptSnippet is not provided", () => {
|
|
const prompt = buildSystemPrompt({
|
|
selectedTools: ["read", "dynamic_tool"],
|
|
contextFiles: [],
|
|
skills: [],
|
|
cwd: process.cwd(),
|
|
});
|
|
|
|
expect(prompt).not.toContain("dynamic_tool");
|
|
});
|
|
});
|
|
|
|
describe("prompt guidelines", () => {
|
|
test("appends promptGuidelines to default guidelines", () => {
|
|
const prompt = buildSystemPrompt({
|
|
selectedTools: ["read", "dynamic_tool"],
|
|
promptGuidelines: ["Use dynamic_tool for project summaries."],
|
|
contextFiles: [],
|
|
skills: [],
|
|
cwd: process.cwd(),
|
|
});
|
|
|
|
expect(prompt).toContain("- Use dynamic_tool for project summaries.");
|
|
});
|
|
|
|
test("deduplicates and trims promptGuidelines", () => {
|
|
const prompt = buildSystemPrompt({
|
|
selectedTools: ["read", "dynamic_tool"],
|
|
promptGuidelines: ["Use dynamic_tool for summaries.", " Use dynamic_tool for summaries. ", " "],
|
|
contextFiles: [],
|
|
skills: [],
|
|
cwd: process.cwd(),
|
|
});
|
|
|
|
expect(prompt.match(/- Use dynamic_tool for summaries\./g)).toHaveLength(1);
|
|
});
|
|
});
|
|
|
|
describe("skills", () => {
|
|
test.each([
|
|
{ name: "default prompt", customPrompt: undefined },
|
|
{ name: "custom prompt", customPrompt: "Custom system prompt" },
|
|
])("includes skills with only bash in the $name", ({ customPrompt }) => {
|
|
const prompt = buildSystemPrompt({
|
|
customPrompt,
|
|
selectedTools: ["bash"],
|
|
contextFiles: [],
|
|
skills: [testSkill],
|
|
cwd: process.cwd(),
|
|
});
|
|
|
|
expect(prompt).toContain("<skills>");
|
|
expect(prompt).toContain("<available_skills>");
|
|
expect(prompt).toContain("<name>test-skill</name>");
|
|
expect(prompt).toContain("Use bash to load a skill's file");
|
|
});
|
|
|
|
test("omits skills without read or bash", () => {
|
|
const prompt = buildSystemPrompt({
|
|
selectedTools: ["write"],
|
|
contextFiles: [],
|
|
skills: [testSkill],
|
|
cwd: process.cwd(),
|
|
});
|
|
|
|
expect(prompt).not.toContain("<available_skills>");
|
|
});
|
|
});
|
|
});
|