feat: add first-class pi target with mcporter/subagent compatibility

This commit is contained in:
Geet Khosla
2026-02-12 23:07:34 +01:00
parent 87e98b24d3
commit e84fef7a56
14 changed files with 1358 additions and 18 deletions

View File

@@ -350,4 +350,80 @@ describe("CLI", () => {
expect(await exists(path.join(codexRoot, "skills", "skill-one", "SKILL.md"))).toBe(true)
expect(await exists(path.join(codexRoot, "AGENTS.md"))).toBe(true)
})
test("convert supports --pi-home for pi output", async () => {
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "cli-pi-home-"))
const piRoot = path.join(tempRoot, ".pi")
const fixtureRoot = path.join(import.meta.dir, "fixtures", "sample-plugin")
const proc = Bun.spawn([
"bun",
"run",
"src/index.ts",
"convert",
fixtureRoot,
"--to",
"pi",
"--pi-home",
piRoot,
], {
cwd: path.join(import.meta.dir, ".."),
stdout: "pipe",
stderr: "pipe",
})
const exitCode = await proc.exited
const stdout = await new Response(proc.stdout).text()
const stderr = await new Response(proc.stderr).text()
if (exitCode !== 0) {
throw new Error(`CLI failed (exit ${exitCode}).\nstdout: ${stdout}\nstderr: ${stderr}`)
}
expect(stdout).toContain("Converted compound-engineering")
expect(stdout).toContain(piRoot)
expect(await exists(path.join(piRoot, "prompts", "workflows-review.md"))).toBe(true)
expect(await exists(path.join(piRoot, "skills", "repo-research-analyst", "SKILL.md"))).toBe(true)
expect(await exists(path.join(piRoot, "extensions", "compound-engineering-compat.ts"))).toBe(true)
expect(await exists(path.join(piRoot, "compound-engineering", "mcporter.json"))).toBe(true)
})
test("install supports --also with pi output", async () => {
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "cli-also-pi-"))
const fixtureRoot = path.join(import.meta.dir, "fixtures", "sample-plugin")
const piRoot = path.join(tempRoot, ".pi")
const proc = Bun.spawn([
"bun",
"run",
"src/index.ts",
"install",
fixtureRoot,
"--to",
"opencode",
"--also",
"pi",
"--pi-home",
piRoot,
"--output",
tempRoot,
], {
cwd: path.join(import.meta.dir, ".."),
stdout: "pipe",
stderr: "pipe",
})
const exitCode = await proc.exited
const stdout = await new Response(proc.stdout).text()
const stderr = await new Response(proc.stderr).text()
if (exitCode !== 0) {
throw new Error(`CLI failed (exit ${exitCode}).\nstdout: ${stdout}\nstderr: ${stderr}`)
}
expect(stdout).toContain("Installed compound-engineering")
expect(stdout).toContain(piRoot)
expect(await exists(path.join(piRoot, "prompts", "workflows-review.md"))).toBe(true)
expect(await exists(path.join(piRoot, "extensions", "compound-engineering-compat.ts"))).toBe(true)
})
})

116
tests/pi-converter.test.ts Normal file
View File

@@ -0,0 +1,116 @@
import { describe, expect, test } from "bun:test"
import path from "path"
import { loadClaudePlugin } from "../src/parsers/claude"
import { convertClaudeToPi } from "../src/converters/claude-to-pi"
import { parseFrontmatter } from "../src/utils/frontmatter"
import type { ClaudePlugin } from "../src/types/claude"
const fixtureRoot = path.join(import.meta.dir, "fixtures", "sample-plugin")
describe("convertClaudeToPi", () => {
test("converts commands, skills, extensions, and MCPorter config", async () => {
const plugin = await loadClaudePlugin(fixtureRoot)
const bundle = convertClaudeToPi(plugin, {
agentMode: "subagent",
inferTemperature: false,
permissions: "none",
})
// Prompts are normalized command names
expect(bundle.prompts.some((prompt) => prompt.name === "workflows-review")).toBe(true)
expect(bundle.prompts.some((prompt) => prompt.name === "plan_review")).toBe(true)
// Commands with disable-model-invocation are excluded
expect(bundle.prompts.some((prompt) => prompt.name === "deploy-docs")).toBe(false)
const workflowsReview = bundle.prompts.find((prompt) => prompt.name === "workflows-review")
expect(workflowsReview).toBeDefined()
const parsedPrompt = parseFrontmatter(workflowsReview!.content)
expect(parsedPrompt.data.description).toBe("Run a multi-agent review workflow")
// Existing skills are copied and agents are converted into generated Pi skills
expect(bundle.skillDirs.some((skill) => skill.name === "skill-one")).toBe(true)
expect(bundle.generatedSkills.some((skill) => skill.name === "repo-research-analyst")).toBe(true)
// Pi compatibility extension is included (with subagent + MCPorter tools)
const compatExtension = bundle.extensions.find((extension) => extension.name === "compound-engineering-compat.ts")
expect(compatExtension).toBeDefined()
expect(compatExtension!.content).toContain('name: "subagent"')
expect(compatExtension!.content).toContain('name: "mcporter_call"')
// Claude MCP config is translated to MCPorter config
expect(bundle.mcporterConfig?.mcpServers.context7?.baseUrl).toBe("https://mcp.context7.com/mcp")
expect(bundle.mcporterConfig?.mcpServers["local-tooling"]?.command).toBe("echo")
})
test("transforms Task calls, AskUserQuestion, slash commands, and todo tool references", () => {
const plugin: ClaudePlugin = {
root: "/tmp/plugin",
manifest: { name: "fixture", version: "1.0.0" },
agents: [],
commands: [
{
name: "workflows:plan",
description: "Plan workflow",
body: [
"Run these in order:",
"- Task repo-research-analyst(feature_description)",
"- Task learnings-researcher(feature_description)",
"Use AskUserQuestion tool for follow-up.",
"Then use /workflows:work and /prompts:deepen-plan.",
"Track progress with TodoWrite and TodoRead.",
].join("\n"),
sourcePath: "/tmp/plugin/commands/plan.md",
},
],
skills: [],
hooks: undefined,
mcpServers: undefined,
}
const bundle = convertClaudeToPi(plugin, {
agentMode: "subagent",
inferTemperature: false,
permissions: "none",
})
expect(bundle.prompts).toHaveLength(1)
const parsedPrompt = parseFrontmatter(bundle.prompts[0].content)
expect(parsedPrompt.body).toContain("Run subagent with agent=\"repo-research-analyst\" and task=\"feature_description\".")
expect(parsedPrompt.body).toContain("Run subagent with agent=\"learnings-researcher\" and task=\"feature_description\".")
expect(parsedPrompt.body).toContain("ask_user_question")
expect(parsedPrompt.body).toContain("/workflows-work")
expect(parsedPrompt.body).toContain("/deepen-plan")
expect(parsedPrompt.body).toContain("file-based todos (todos/ + /skill:file-todos)")
})
test("appends MCPorter compatibility note when command references MCP", () => {
const plugin: ClaudePlugin = {
root: "/tmp/plugin",
manifest: { name: "fixture", version: "1.0.0" },
agents: [],
commands: [
{
name: "docs",
description: "Read MCP docs",
body: "Use MCP servers for docs lookup.",
sourcePath: "/tmp/plugin/commands/docs.md",
},
],
skills: [],
hooks: undefined,
mcpServers: undefined,
}
const bundle = convertClaudeToPi(plugin, {
agentMode: "subagent",
inferTemperature: false,
permissions: "none",
})
const parsedPrompt = parseFrontmatter(bundle.prompts[0].content)
expect(parsedPrompt.body).toContain("Pi + MCPorter note")
expect(parsedPrompt.body).toContain("mcporter_call")
})
})

99
tests/pi-writer.test.ts Normal file
View File

@@ -0,0 +1,99 @@
import { describe, expect, test } from "bun:test"
import { promises as fs } from "fs"
import path from "path"
import os from "os"
import { writePiBundle } from "../src/targets/pi"
import type { PiBundle } from "../src/types/pi"
async function exists(filePath: string): Promise<boolean> {
try {
await fs.access(filePath)
return true
} catch {
return false
}
}
describe("writePiBundle", () => {
test("writes prompts, skills, extensions, mcporter config, and AGENTS.md block", async () => {
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "pi-writer-"))
const outputRoot = path.join(tempRoot, ".pi")
const bundle: PiBundle = {
prompts: [{ name: "workflows-plan", content: "Prompt content" }],
skillDirs: [
{
name: "skill-one",
sourceDir: path.join(import.meta.dir, "fixtures", "sample-plugin", "skills", "skill-one"),
},
],
generatedSkills: [{ name: "repo-research-analyst", content: "---\nname: repo-research-analyst\n---\n\nBody" }],
extensions: [{ name: "compound-engineering-compat.ts", content: "export default function () {}" }],
mcporterConfig: {
mcpServers: {
context7: { baseUrl: "https://mcp.context7.com/mcp" },
},
},
}
await writePiBundle(outputRoot, bundle)
expect(await exists(path.join(outputRoot, "prompts", "workflows-plan.md"))).toBe(true)
expect(await exists(path.join(outputRoot, "skills", "skill-one", "SKILL.md"))).toBe(true)
expect(await exists(path.join(outputRoot, "skills", "repo-research-analyst", "SKILL.md"))).toBe(true)
expect(await exists(path.join(outputRoot, "extensions", "compound-engineering-compat.ts"))).toBe(true)
expect(await exists(path.join(outputRoot, "compound-engineering", "mcporter.json"))).toBe(true)
const agentsPath = path.join(outputRoot, "AGENTS.md")
const agentsContent = await fs.readFile(agentsPath, "utf8")
expect(agentsContent).toContain("BEGIN COMPOUND PI TOOL MAP")
expect(agentsContent).toContain("MCPorter")
})
test("writes to ~/.pi/agent style roots without nesting under .pi", async () => {
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "pi-agent-root-"))
const outputRoot = path.join(tempRoot, "agent")
const bundle: PiBundle = {
prompts: [{ name: "workflows-work", content: "Prompt content" }],
skillDirs: [],
generatedSkills: [],
extensions: [],
}
await writePiBundle(outputRoot, bundle)
expect(await exists(path.join(outputRoot, "prompts", "workflows-work.md"))).toBe(true)
expect(await exists(path.join(outputRoot, ".pi"))).toBe(false)
})
test("backs up existing mcporter config before overwriting", async () => {
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "pi-backup-"))
const outputRoot = path.join(tempRoot, ".pi")
const configPath = path.join(outputRoot, "compound-engineering", "mcporter.json")
await fs.mkdir(path.dirname(configPath), { recursive: true })
await fs.writeFile(configPath, JSON.stringify({ previous: true }, null, 2))
const bundle: PiBundle = {
prompts: [],
skillDirs: [],
generatedSkills: [],
extensions: [],
mcporterConfig: {
mcpServers: {
linear: { baseUrl: "https://mcp.linear.app/mcp" },
},
},
}
await writePiBundle(outputRoot, bundle)
const files = await fs.readdir(path.dirname(configPath))
const backupFileName = files.find((file) => file.startsWith("mcporter.json.bak."))
expect(backupFileName).toBeDefined()
const currentConfig = JSON.parse(await fs.readFile(configPath, "utf8")) as { mcpServers: Record<string, unknown> }
expect(currentConfig.mcpServers.linear).toBeDefined()
})
})

68
tests/sync-pi.test.ts Normal file
View File

@@ -0,0 +1,68 @@
import { describe, expect, test } from "bun:test"
import { promises as fs } from "fs"
import path from "path"
import os from "os"
import { syncToPi } from "../src/sync/pi"
import type { ClaudeHomeConfig } from "../src/parsers/claude-home"
describe("syncToPi", () => {
test("symlinks skills and writes MCPorter config", async () => {
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "sync-pi-"))
const fixtureSkillDir = path.join(import.meta.dir, "fixtures", "sample-plugin", "skills", "skill-one")
const config: ClaudeHomeConfig = {
skills: [
{
name: "skill-one",
sourceDir: fixtureSkillDir,
skillPath: path.join(fixtureSkillDir, "SKILL.md"),
},
],
mcpServers: {
context7: { url: "https://mcp.context7.com/mcp" },
local: { command: "echo", args: ["hello"] },
},
}
await syncToPi(config, tempRoot)
const linkedSkillPath = path.join(tempRoot, "skills", "skill-one")
const linkedStat = await fs.lstat(linkedSkillPath)
expect(linkedStat.isSymbolicLink()).toBe(true)
const mcporterPath = path.join(tempRoot, "compound-engineering", "mcporter.json")
const mcporterConfig = JSON.parse(await fs.readFile(mcporterPath, "utf8")) as {
mcpServers: Record<string, { baseUrl?: string; command?: string }>
}
expect(mcporterConfig.mcpServers.context7?.baseUrl).toBe("https://mcp.context7.com/mcp")
expect(mcporterConfig.mcpServers.local?.command).toBe("echo")
})
test("merges existing MCPorter config", async () => {
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "sync-pi-merge-"))
const mcporterPath = path.join(tempRoot, "compound-engineering", "mcporter.json")
await fs.mkdir(path.dirname(mcporterPath), { recursive: true })
await fs.writeFile(
mcporterPath,
JSON.stringify({ mcpServers: { existing: { baseUrl: "https://example.com/mcp" } } }, null, 2),
)
const config: ClaudeHomeConfig = {
skills: [],
mcpServers: {
context7: { url: "https://mcp.context7.com/mcp" },
},
}
await syncToPi(config, tempRoot)
const merged = JSON.parse(await fs.readFile(mcporterPath, "utf8")) as {
mcpServers: Record<string, { baseUrl?: string }>
}
expect(merged.mcpServers.existing?.baseUrl).toBe("https://example.com/mcp")
expect(merged.mcpServers.context7?.baseUrl).toBe("https://mcp.context7.com/mcp")
})
})