import { execFile } from "node:child_process"; import { promisify } from "node:util"; import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; import { Type } from "typebox"; import { Check } from "typebox/value"; const run = promisify(execFile); const STATUS = Type.Object({ harnesses: Type.Array( Type.Object({ id: Type.String(), session_naming: Type.Optional(Type.Unknown()) }), ), }); // Rex installs this session-start hook for agents with hook support. The naming flag must match // the installed setting, or the hook reports an outdated setup. async function sessionStartPrompt() { const status = await run("rex", ["skill", "status", "--json"], { timeout: 5_000 }); const parsed: unknown = JSON.parse(status.stdout); const naming = Check(STATUS, parsed) && parsed.harnesses.some((harness) => harness.id === "pi" && harness.session_naming === true); const hook = await run( "rex", ["__internal", "session-start", "--agent", "pi", `--feature-session-naming=${naming}`], { timeout: 5_000 }, ); return hook.stdout.trim() || undefined; } export default function rexExtension(pi: ExtensionAPI) { // Subagents must not rename the user's Rex session. if (!process.env.REX_SESSION || process.env.PI_SUBAGENT_CHILD_AGENT) { return; } const prompt = sessionStartPrompt().catch(() => undefined); pi.on("before_agent_start", async (event) => { const text = await prompt; if (text) { event.systemPromptOptions.sections.rex = text; } }); }