project-trust.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * Project Trust Extension
  3. *
  4. * Demonstrates the project_trust event. Install globally or pass via -e:
  5. *
  6. * mkdir -p ~/.pi/agent/extensions
  7. * cp packages/coding-agent/examples/extensions/project-trust.ts ~/.pi/agent/extensions/
  8. *
  9. * Or:
  10. *
  11. * pi -e packages/coding-agent/examples/extensions/project-trust.ts
  12. *
  13. * Try it in a project containing .pi, AGENTS.md/CLAUDE.md, or .agents/skills.
  14. */
  15. import type { ExtensionAPI, ProjectTrustEventResult } from "@earendil-works/pi-coding-agent";
  16. export default function (pi: ExtensionAPI) {
  17. let loadCount = 0;
  18. loadCount++;
  19. // Multiple handlers in one extension are allowed. The first handler that returns
  20. // { trusted: "yes" } or { trusted: "no" } wins and suppresses the built-in
  21. // trust prompt. Return { trusted: "undecided" } to let another handler or the
  22. // built-in flow decide.
  23. pi.on("project_trust", async (event, ctx): Promise<ProjectTrustEventResult> => {
  24. ctx.ui.notify(`project_trust fired for ${event.cwd} (mode: ${ctx.mode}, load: ${loadCount})`, "info");
  25. if (!ctx.hasUI) {
  26. return { trusted: "undecided" };
  27. }
  28. const choice = await ctx.ui.select(`Project trust for:\n${event.cwd}`, [
  29. "Trust and remember",
  30. "Trust with note and remember",
  31. "Trust this session",
  32. "Do not trust this session",
  33. "Let built-in prompt decide",
  34. ]);
  35. if (choice === "Trust with note and remember") {
  36. const note = await ctx.ui.input("Project trust note", "Optional note for this demo");
  37. ctx.ui.notify(note ? `Recorded demo note: ${note}` : "No demo note entered", "info");
  38. return { trusted: "yes", remember: true };
  39. }
  40. if (choice === "Trust and remember") {
  41. return { trusted: "yes", remember: true };
  42. }
  43. if (choice === "Trust this session") {
  44. return { trusted: "yes" };
  45. }
  46. if (choice === "Do not trust this session") {
  47. return { trusted: "no" };
  48. }
  49. if (choice === "Let built-in prompt decide") {
  50. return { trusted: "undecided" };
  51. }
  52. return { trusted: "undecided" };
  53. });
  54. pi.on("session_start", (_event, ctx) => {
  55. ctx.ui.notify(`project-trust example loaded after trust resolution in ${ctx.cwd}`, "info");
  56. });
  57. }