Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 64 additions & 8 deletions .pi/extensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,17 @@ interface BeforeAgentStartResult {
systemPrompt?: string
}

interface InputEvent {
text: string
}

type InputEventResult = { action: "continue" } | { action: "transform"; text: string }

interface ExtensionAPI {
on(
event: "input",
handler: (event: InputEvent, ctx: ExtensionContext) => Promise<InputEventResult> | InputEventResult
): void
on(
event: "session_start",
handler: (event: SessionStartEvent, ctx: ExtensionContext) => Promise<void> | void
Expand Down Expand Up @@ -670,7 +680,52 @@ function buildDoctorReport(ctx: ExtensionContext): string {
return lines.join("\n")
}

/** Same placeholder set Pi's prompt-template substitution understands. */
const ARG_PLACEHOLDER = /\$ARGUMENTS|\$@|\$\d|\$\{@/

const commandBodyCache = new Map<string, string | null>()

/**
* Body of an ECC command that has NO argument placeholder, or null.
*
* Claude Code appends `ARGUMENTS: <args>` when a command lacks `$ARGUMENTS`;
* Pi's template expansion silently drops the arguments instead. Most ECC
* commands rely on the Claude behaviour, so the adapter restores it.
*/
function loadPlaceholderlessCommand(name: string): string | null {
if (commandBodyCache.has(name)) {
return commandBodyCache.get(name) ?? null
}
let body: string | null = null
if (/^[A-Za-z0-9._-]+$/.test(name)) {
try {
const raw = fs.readFileSync(path.join(ECC_ROOT, "commands", `${name}.md`), "utf8")
const stripped = raw.replace(/^\uFEFF?---\r?\n[\s\S]*?\r?\n---\r?\n?/, "").trim()
body = ARG_PLACEHOLDER.test(stripped) ? null : stripped
} catch {
body = null
}
}
commandBodyCache.set(name, body)
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
return body
}

export default function (pi: ExtensionAPI): void {
pi.on("input", event => {
if (isDisabledByEnv(process.env.ECC_PI_COMMAND_ARGS)) {
return { action: "continue" }
}
const match = event.text.match(/^\/([^\s:]+)\s+([\s\S]*\S[\s\S]*)$/)
if (!match) {
return { action: "continue" }
}
const body = loadPlaceholderlessCommand(match[1])
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
if (!body) {
return { action: "continue" }
}
return { action: "transform", text: `${body}\n\nARGUMENTS: ${match[2].trim()}` }
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
})

/**
* ECC's SessionStart hook returns context for the model, but Pi has no
* equivalent of Claude Code's `additionalContext` field. It is held here and
Expand Down Expand Up @@ -753,14 +808,15 @@ export default function (pi: ExtensionAPI): void {
pi.registerCommand("ecc-doctor", {
description: "Report ECC adapter status: package root, canonical resources, hooks, companions",
handler: async (_args, ctx) => {
pi.sendMessage(
{
customType: "ecc-doctor",
content: buildDoctorReport(ctx),
display: true,
},
{ deliverAs: "nextTurn" }
)
// No deliverAs: "nextTurn" here. In Pi 1.x that queues the message until
// the user's next prompt, so the report never appears when the command runs.
// Without options (and not streaming) Pi appends and displays it immediately
// without triggering a model turn.
pi.sendMessage({
customType: "ecc-doctor",
content: buildDoctorReport(ctx),
display: true,
})
},
})
}
77 changes: 77 additions & 0 deletions tests/pi/pi-extension-adapter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1665,6 +1665,83 @@ async function main() {
"systemPrompt-override return in before_agent_start"
)
}],

["registers the Pi input event and restores placeholder-less command arguments", () => {
assert.ok(
extensionSource.includes('pi.on("input"'),
"expected .pi/extensions/index.ts to register an input handler via pi.on(...); " +
"Pi runs input handlers before prompt-template expansion, which is the only " +
"place ECC can restore the ARGUMENTS payload Pi otherwise drops"
)
assert.ok(
extensionSource.includes("ECC_PI_COMMAND_ARGS"),
"expected .pi/extensions/index.ts to honor an ECC_PI_COMMAND_ARGS opt-out for " +
"argument restoration; an irreversible transform would make the workaround mandatory"
)
assert.ok(
extensionSource.includes('action: "transform"') &&
extensionSource.includes('action: "continue"'),
"expected the input handler to return Pi's action result shape, either " +
"transform (replace the command with its body plus ARGUMENTS) or continue " +
"(leave Pi's normal expansion untouched)"
)
assert.ok(
extensionSource.includes("ARG_PLACEHOLDER = /\\$ARGUMENTS|\\$@|\\$\\d|\\$\\{@/"),
"expected the placeholder test to recognize $ARGUMENTS, $@, positional $N, and ${@}; " +
"missing any one of Pi's supported placeholders would transform commands Pi already expands"
)
assert.ok(
extensionSource.includes('path.join(ECC_ROOT, "commands"') &&
extensionSource.includes("${name}.md"),
"expected the adapter to read the canonical commands/ directory relative to ECC_ROOT; " +
"reading from cwd would break global installs exactly like the original hook-root defect"
)
assert.ok(
extensionSource.includes("commandBodyCache"),
"expected the placeholder-less command lookup to cache command bodies; the input " +
"handler runs on every prompt and should not re-read command markdown every time"
)
}],

["placeholder-less command transformation (behavioral mirror): /plan args appends ARGUMENTS, /sessions stays untouched", () => {
const placeholder = /\$ARGUMENTS|\$@|\$\d|\$\{@/
const stripFrontmatter = raw => raw.replace(/^\uFEFF?---\r?\n[\s\S]*?\r?\n---\r?\n?/, "").trim()

const planRaw = fs.readFileSync(path.join(repoRoot, "commands", "plan.md"), "utf8")
const planBody = stripFrontmatter(planRaw)
assert.ok(
planBody && !placeholder.test(planBody),
"commands/plan.md must be placeholder-less in this repo for the test to be meaningful; " +
"if it gains a placeholder the transformation should no longer apply"
)

const input = "/plan add a hello world CLI"
const args = "add a hello world CLI"
assert.ok(
/^\/([^\s:]+)\s+([\s\S]*\S[\s\S]*)$/.test(input),
"the command-with-arguments shape should match the handler regex"
)
const transformed = `${planBody}\n\nARGUMENTS: ${args}`
assert.ok(
transformed.includes("ARGUMENTS: add a hello world CLI"),
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
"transforming a placeholder-less command should append ARGUMENTS with the user text"
)
assert.ok(
transformed.startsWith("# Plan Command"),
"the transformed input should be the command body itself, not the slash command name"
)

const sessionsRaw = fs.readFileSync(path.join(repoRoot, "commands", "sessions.md"), "utf8")
const sessionsBody = stripFrontmatter(sessionsRaw)
assert.ok(
placeholder.test(sessionsBody),
"commands/sessions.md must contain a placeholder in this repo for the test to be meaningful"
)
assert.ok(
placeholder.test(sessionsBody),
"a command with $ARGUMENTS/$@/$N/${@} must fall through to Pi unchanged"
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
)
}],
]

for (const [name, fn] of tests) {
Expand Down
Loading