rpc-demo.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * RPC Extension UI Demo
  3. *
  4. * Purpose-built extension that exercises all RPC-supported extension UI methods.
  5. * Designed to be loaded alongside the rpc-extension-ui-example.ts script to
  6. * demonstrate the full extension UI protocol.
  7. *
  8. * UI methods exercised:
  9. * - select() - on tool_call for dangerous bash commands
  10. * - confirm() - on session_before_switch
  11. * - input() - via /rpc-input command
  12. * - editor() - via /rpc-editor command
  13. * - notify() - after each dialog completes
  14. * - setStatus() - on turn_start/turn_end
  15. * - setWidget() - on session_start
  16. * - setTitle() - on session_start
  17. * - setEditorText() - via /rpc-prefill command
  18. */
  19. import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
  20. export default function (pi: ExtensionAPI) {
  21. let turnCount = 0;
  22. // -- setTitle, setWidget, setStatus on session lifecycle --
  23. pi.on("session_start", async (event, ctx) => {
  24. ctx.ui.setTitle(event.reason === "new" ? "pi RPC Demo (new session)" : "pi RPC Demo");
  25. ctx.ui.setWidget("rpc-demo", ["--- RPC Extension UI Demo ---", "Loaded and ready."]);
  26. ctx.ui.setStatus("rpc-demo", `Turns: ${turnCount}`);
  27. });
  28. // -- setStatus on turn lifecycle --
  29. pi.on("turn_start", async (_event, ctx) => {
  30. turnCount++;
  31. ctx.ui.setStatus("rpc-demo", `Turn ${turnCount} running...`);
  32. });
  33. pi.on("turn_end", async (_event, ctx) => {
  34. ctx.ui.setStatus("rpc-demo", `Turn ${turnCount} done`);
  35. });
  36. // -- select on dangerous tool calls --
  37. pi.on("tool_call", async (event, ctx) => {
  38. if (event.toolName !== "bash") return undefined;
  39. const command = event.input.command as string;
  40. const isDangerous = /\brm\s+(-rf?|--recursive)/i.test(command) || /\bsudo\b/i.test(command);
  41. if (isDangerous) {
  42. if (!ctx.hasUI) {
  43. return { block: true, reason: "Dangerous command blocked (no UI)" };
  44. }
  45. const choice = await ctx.ui.select(`Dangerous command: ${command}`, ["Allow", "Block"]);
  46. if (choice !== "Allow") {
  47. ctx.ui.notify("Command blocked by user", "warning");
  48. return { block: true, reason: "Blocked by user" };
  49. }
  50. ctx.ui.notify("Command allowed", "info");
  51. }
  52. return undefined;
  53. });
  54. // -- confirm on session clear --
  55. pi.on("session_before_switch", async (event, ctx) => {
  56. if (event.reason !== "new") return;
  57. if (!ctx.hasUI) return;
  58. const confirmed = await ctx.ui.confirm("Clear session?", "All messages will be lost.");
  59. if (!confirmed) {
  60. ctx.ui.notify("Clear cancelled", "info");
  61. return { cancel: true };
  62. }
  63. });
  64. // -- input via command --
  65. pi.registerCommand("rpc-input", {
  66. description: "Prompt for text input (demonstrates ctx.ui.input in RPC)",
  67. handler: async (_args, ctx) => {
  68. const value = await ctx.ui.input("Enter a value", "type something...");
  69. if (value) {
  70. ctx.ui.notify(`You entered: ${value}`, "info");
  71. } else {
  72. ctx.ui.notify("Input cancelled", "info");
  73. }
  74. },
  75. });
  76. // -- editor via command --
  77. pi.registerCommand("rpc-editor", {
  78. description: "Open multi-line editor (demonstrates ctx.ui.editor in RPC)",
  79. handler: async (_args, ctx) => {
  80. const text = await ctx.ui.editor("Edit some text", "Line 1\nLine 2\nLine 3");
  81. if (text) {
  82. ctx.ui.notify(`Editor submitted (${text.split("\n").length} lines)`, "info");
  83. } else {
  84. ctx.ui.notify("Editor cancelled", "info");
  85. }
  86. },
  87. });
  88. // -- setEditorText via command --
  89. pi.registerCommand("rpc-prefill", {
  90. description: "Prefill the input editor (demonstrates ctx.ui.setEditorText in RPC)",
  91. handler: async (_args, ctx) => {
  92. ctx.ui.setEditorText("This text was set by the rpc-demo extension.");
  93. ctx.ui.notify("Editor prefilled", "info");
  94. },
  95. });
  96. }