protected-paths.ts 808 B

123456789101112131415161718192021222324252627282930
  1. /**
  2. * Protected Paths Extension
  3. *
  4. * Blocks write and edit operations to protected paths.
  5. * Useful for preventing accidental modifications to sensitive files.
  6. */
  7. import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
  8. export default function (pi: ExtensionAPI) {
  9. const protectedPaths = [".env", ".git/", "node_modules/"];
  10. pi.on("tool_call", async (event, ctx) => {
  11. if (event.toolName !== "write" && event.toolName !== "edit") {
  12. return undefined;
  13. }
  14. const path = event.input.path as string;
  15. const isProtected = protectedPaths.some((p) => path.includes(p));
  16. if (isProtected) {
  17. if (ctx.hasUI) {
  18. ctx.ui.notify(`Blocked write to protected path: ${path}`, "warning");
  19. }
  20. return { block: true, reason: `Path "${path}" is protected` };
  21. }
  22. return undefined;
  23. });
  24. }