/** * /ask [message] — read-only: drop write/edit, append system constraint; optional message sent after switch * /agent [message] — restore tools from before ask; optional message sent after switch * * New/switched sessions reset to agent. Does not filter bash; use a sandbox for hard isolation. * * Not supported with pi -p / print mode: pi.sendUserMessage is fire-and-forget, so -p may exit * before the follow-up turn finishes. Use the interactive TUI instead. */ import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent"; const WRITE = new Set(["edit", "write"]); export default function (pi: ExtensionAPI): void { let ask = false; let prev: string[] | undefined; function setMode(on: boolean, ctx: ExtensionContext): void { if (ask === on) return; ask = on; if (on) { prev = pi.getActiveTools(); pi.setActiveTools(prev.filter((t) => !WRITE.has(t))); } else { pi.setActiveTools(prev ?? pi.getAllTools().map((t) => t.name)); prev = undefined; } // Mode indicator: footer status only (no notify) ctx.ui.setStatus("ask-mode", on ? ctx.ui.theme.fg("warning", "ask") : undefined); } function run(on: boolean, args: string, ctx: ExtensionContext): void { setMode(on, ctx); const text = args.trim(); if (text) { pi.sendUserMessage(text); } } pi.registerCommand("ask", { description: "Switch to ask mode", handler: async (args, ctx) => run(true, args, ctx), }); pi.registerCommand("agent", { description: "Switch to agent mode", handler: async (args, ctx) => run(false, args, ctx), }); pi.on("before_agent_start", async (event) => { if (!ask) return; return { systemPrompt: event.systemPrompt + "\n\n[Ask mode] File modifications are disabled (no write/edit). " + "All other tools and commands remain available — including bash for network lookups (e.g. curl). " + "If the user asks you to create or edit files, tell them to run /agent first.", }; }); pi.on("session_start", async (_event, ctx) => { if (ask) { pi.setActiveTools(prev ?? pi.getAllTools().map((t) => t.name)); } ask = false; prev = undefined; ctx.ui.setStatus("ask-mode", undefined); }); }