Created
July 23, 2026 00:40
-
-
Save Astro36/8f6d91eef2391af5c10368c84e9a3dea to your computer and use it in GitHub Desktop.
Pi Agent - Confirm before write/edit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; | |
| import { resolve, dirname } from "node:path"; | |
| export default function (pi: ExtensionAPI) { | |
| const approvedFiles = new Set<string>(); | |
| const approvedDirs = new Set<string>(); | |
| const isApproved = (filePath: string) => { | |
| if (approvedFiles.has(filePath)) return true; | |
| let dir = dirname(filePath); | |
| for (;;) { | |
| if (approvedDirs.has(dir)) return true; | |
| const parent = dirname(dir); | |
| if (parent === dir) return false; | |
| dir = parent; | |
| } | |
| }; | |
| pi.on("session_start", async () => { | |
| approvedFiles.clear(); | |
| approvedDirs.clear(); | |
| }); | |
| pi.on("tool_call", async (event, ctx) => { | |
| if (event.toolName !== "write" && event.toolName !== "edit") return; | |
| const filePath = resolve(ctx.cwd, event.input.path as string); | |
| if (isApproved(filePath)) return; | |
| const choice = await ctx.ui.select(`📝 ${event.toolName}: ${filePath}`, [ | |
| "Allow once", | |
| "Always allow this file", | |
| "Always allow this directory", | |
| "Deny", | |
| ]); | |
| if (choice === "Always allow this file") approvedFiles.add(filePath); | |
| else if (choice === "Always allow this directory") approvedDirs.add(dirname(filePath)); | |
| else if (choice === "Deny") | |
| return { block: true, reason: `User explicitly denied ${event.toolName} to ${filePath}. Do not retry.` }; | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment