From 452f1ed40bbf69de32b61ffc93c98545776bfa4e Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Thu, 7 May 2026 19:42:34 -0700 Subject: chore: keep pi extension under .pi --- .pi/extensions/superpowers.ts | 121 +++++++++++++++++++++++++++++++++++++++++ extensions/superpowers.ts | 121 ----------------------------------------- package.json | 2 +- tests/pi/test-pi-extension.mjs | 4 +- 4 files changed, 124 insertions(+), 124 deletions(-) create mode 100644 .pi/extensions/superpowers.ts delete mode 100644 extensions/superpowers.ts diff --git a/.pi/extensions/superpowers.ts b/.pi/extensions/superpowers.ts new file mode 100644 index 0000000..32c1077 --- /dev/null +++ b/.pi/extensions/superpowers.ts @@ -0,0 +1,121 @@ +import { readFileSync } from "node:fs"; +import { dirname, resolve } from "node:path"; +import { fileURLToPath } from "node:url"; +import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; + +const EXTREMELY_IMPORTANT_MARKER = ""; +const BOOTSTRAP_MARKER = "superpowers:using-superpowers bootstrap for pi"; + +const extensionDir = dirname(fileURLToPath(import.meta.url)); +const packageRoot = resolve(extensionDir, "../.."); +const skillsDir = resolve(packageRoot, "skills"); +const bootstrapSkillPath = resolve(skillsDir, "using-superpowers", "SKILL.md"); + +let cachedBootstrap: string | null | undefined; + +export default function superpowersPiExtension(pi: ExtensionAPI) { + let injectBootstrap = true; + + pi.on("resources_discover", async () => ({ + skillPaths: [skillsDir], + })); + + pi.on("session_start", async () => { + injectBootstrap = true; + }); + + pi.on("session_compact", async () => { + injectBootstrap = true; + }); + + pi.on("agent_end", async () => { + injectBootstrap = false; + }); + + pi.on("context", async (event) => { + if (!injectBootstrap) return; + if (event.messages.some(messageContainsBootstrap)) return; + + const bootstrap = getBootstrapContent(); + if (!bootstrap) return; + + const bootstrapMessage = { + role: "user" as const, + content: [{ type: "text" as const, text: bootstrap }], + timestamp: Date.now(), + }; + + const insertAt = firstNonCompactionSummaryIndex(event.messages); + return { + messages: [ + ...event.messages.slice(0, insertAt), + bootstrapMessage, + ...event.messages.slice(insertAt), + ], + }; + }); +} + +function getBootstrapContent(): string | null { + if (cachedBootstrap !== undefined) return cachedBootstrap; + + try { + const skillContent = readFileSync(bootstrapSkillPath, "utf8"); + const body = stripFrontmatter(skillContent); + cachedBootstrap = `${EXTREMELY_IMPORTANT_MARKER} +${BOOTSTRAP_MARKER} + +You have superpowers. + +The using-superpowers skill content is included below and is already loaded for this Pi session. Follow it now. Do not try to load using-superpowers again. + +${body} + +${piToolMapping()} +`; + return cachedBootstrap; + } catch { + cachedBootstrap = null; + return null; + } +} + +function stripFrontmatter(content: string): string { + const match = content.match(/^---\n[\s\S]*?\n---\n([\s\S]*)$/); + return (match ? match[1] : content).trim(); +} + +function piToolMapping(): string { + return `## Pi tool mapping + +Pi has native skills but does not expose Claude Code's \`Skill\` tool. When a Superpowers instruction says to use the \`Skill\` tool, use Pi's native skill system instead: load the relevant \`SKILL.md\` with \`read\` when the skill applies, or let a human invoke \`/skill:name\` explicitly. + +Pi's built-in coding tools are lowercase: \`read\`, \`write\`, \`edit\`, \`bash\`, plus optional \`grep\`, \`find\`, and \`ls\`. Map Claude-style tool names \`Read\`, \`Write\`, \`Edit\`, and \`Bash\` to those Pi tools. + +Pi does not ship a standard \`Task\` subagent tool. If a subagent tool such as \`subagent\` from \`pi-subagents\` is available, use it for Superpowers subagent workflows. If no subagent tool is available, do the work in this session or explain the missing capability instead of inventing tool calls. + +Pi does not ship a standard \`TodoWrite\` task-list tool. If an installed todo/task tool is available, use it. Otherwise track work in plan files or a repo-local \`TODO.md\` when task tracking is needed.`; +} + +function messageContainsBootstrap(message: unknown): boolean { + const content = (message as { content?: unknown }).content; + if (typeof content === "string") return content.includes(BOOTSTRAP_MARKER); + if (!Array.isArray(content)) return false; + return content.some((part) => { + return ( + part && + typeof part === "object" && + (part as { type?: unknown }).type === "text" && + typeof (part as { text?: unknown }).text === "string" && + (part as { text: string }).text.includes(BOOTSTRAP_MARKER) + ); + }); +} + +function firstNonCompactionSummaryIndex(messages: unknown[]): number { + let index = 0; + while ((messages[index] as { role?: unknown } | undefined)?.role === "compactionSummary") { + index += 1; + } + return index; +} diff --git a/extensions/superpowers.ts b/extensions/superpowers.ts deleted file mode 100644 index af44623..0000000 --- a/extensions/superpowers.ts +++ /dev/null @@ -1,121 +0,0 @@ -import { readFileSync } from "node:fs"; -import { dirname, resolve } from "node:path"; -import { fileURLToPath } from "node:url"; -import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; - -const EXTREMELY_IMPORTANT_MARKER = ""; -const BOOTSTRAP_MARKER = "superpowers:using-superpowers bootstrap for pi"; - -const extensionDir = dirname(fileURLToPath(import.meta.url)); -const packageRoot = resolve(extensionDir, ".."); -const skillsDir = resolve(packageRoot, "skills"); -const bootstrapSkillPath = resolve(skillsDir, "using-superpowers", "SKILL.md"); - -let cachedBootstrap: string | null | undefined; - -export default function superpowersPiExtension(pi: ExtensionAPI) { - let injectBootstrap = true; - - pi.on("resources_discover", async () => ({ - skillPaths: [skillsDir], - })); - - pi.on("session_start", async () => { - injectBootstrap = true; - }); - - pi.on("session_compact", async () => { - injectBootstrap = true; - }); - - pi.on("agent_end", async () => { - injectBootstrap = false; - }); - - pi.on("context", async (event) => { - if (!injectBootstrap) return; - if (event.messages.some(messageContainsBootstrap)) return; - - const bootstrap = getBootstrapContent(); - if (!bootstrap) return; - - const bootstrapMessage = { - role: "user" as const, - content: [{ type: "text" as const, text: bootstrap }], - timestamp: Date.now(), - }; - - const insertAt = firstNonCompactionSummaryIndex(event.messages); - return { - messages: [ - ...event.messages.slice(0, insertAt), - bootstrapMessage, - ...event.messages.slice(insertAt), - ], - }; - }); -} - -function getBootstrapContent(): string | null { - if (cachedBootstrap !== undefined) return cachedBootstrap; - - try { - const skillContent = readFileSync(bootstrapSkillPath, "utf8"); - const body = stripFrontmatter(skillContent); - cachedBootstrap = `${EXTREMELY_IMPORTANT_MARKER} -${BOOTSTRAP_MARKER} - -You have superpowers. - -The using-superpowers skill content is included below and is already loaded for this Pi session. Follow it now. Do not try to load using-superpowers again. - -${body} - -${piToolMapping()} -`; - return cachedBootstrap; - } catch { - cachedBootstrap = null; - return null; - } -} - -function stripFrontmatter(content: string): string { - const match = content.match(/^---\n[\s\S]*?\n---\n([\s\S]*)$/); - return (match ? match[1] : content).trim(); -} - -function piToolMapping(): string { - return `## Pi tool mapping - -Pi has native skills but does not expose Claude Code's \`Skill\` tool. When a Superpowers instruction says to use the \`Skill\` tool, use Pi's native skill system instead: load the relevant \`SKILL.md\` with \`read\` when the skill applies, or let a human invoke \`/skill:name\` explicitly. - -Pi's built-in coding tools are lowercase: \`read\`, \`write\`, \`edit\`, \`bash\`, plus optional \`grep\`, \`find\`, and \`ls\`. Map Claude-style tool names \`Read\`, \`Write\`, \`Edit\`, and \`Bash\` to those Pi tools. - -Pi does not ship a standard \`Task\` subagent tool. If a subagent tool such as \`subagent\` from \`pi-subagents\` is available, use it for Superpowers subagent workflows. If no subagent tool is available, do the work in this session or explain the missing capability instead of inventing tool calls. - -Pi does not ship a standard \`TodoWrite\` task-list tool. If an installed todo/task tool is available, use it. Otherwise track work in plan files or a repo-local \`TODO.md\` when task tracking is needed.`; -} - -function messageContainsBootstrap(message: unknown): boolean { - const content = (message as { content?: unknown }).content; - if (typeof content === "string") return content.includes(BOOTSTRAP_MARKER); - if (!Array.isArray(content)) return false; - return content.some((part) => { - return ( - part && - typeof part === "object" && - (part as { type?: unknown }).type === "text" && - typeof (part as { text?: unknown }).text === "string" && - (part as { text: string }).text.includes(BOOTSTRAP_MARKER) - ); - }); -} - -function firstNonCompactionSummaryIndex(messages: unknown[]): number { - let index = 0; - while ((messages[index] as { role?: unknown } | undefined)?.role === "compactionSummary") { - index += 1; - } - return index; -} diff --git a/package.json b/package.json index 00608b4..54c7bdf 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ ], "pi": { "extensions": [ - "./extensions/superpowers.ts" + "./.pi/extensions/superpowers.ts" ], "skills": [ "./skills" diff --git a/tests/pi/test-pi-extension.mjs b/tests/pi/test-pi-extension.mjs index 772f3ef..196e975 100644 --- a/tests/pi/test-pi-extension.mjs +++ b/tests/pi/test-pi-extension.mjs @@ -8,7 +8,7 @@ import test from 'node:test'; const __dirname = dirname(fileURLToPath(import.meta.url)); const repoRoot = resolve(__dirname, '../..'); const packageJsonPath = resolve(repoRoot, 'package.json'); -const extensionPath = resolve(repoRoot, 'extensions/superpowers.ts'); +const extensionPath = resolve(repoRoot, '.pi/extensions/superpowers.ts'); const piToolsPath = resolve(repoRoot, 'skills/using-superpowers/references/pi-tools.md'); async function readPackageJson() { @@ -48,7 +48,7 @@ test('package.json declares a pi package with skills and extension resources', a assert.equal(pkg.name, 'superpowers'); assert.ok(pkg.keywords.includes('pi-package')); assert.deepEqual(pkg.pi.skills, ['./skills']); - assert.deepEqual(pkg.pi.extensions, ['./extensions/superpowers.ts']); + assert.deepEqual(pkg.pi.extensions, ['./.pi/extensions/superpowers.ts']); }); test('extension registers lifecycle hooks without pre-compaction injection', async () => { -- cgit