06-extensions.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Extensions Configuration
  3. *
  4. * Extensions intercept agent events and can register custom tools.
  5. * They provide a unified system for extensions, custom tools, commands, and more.
  6. *
  7. * By default, extension files are discovered from:
  8. * - ~/.pi/agent/extensions/
  9. * - <cwd>/.pi/extensions/
  10. * - Paths specified in settings.json "extensions" array
  11. *
  12. * An extension is a TypeScript file that exports a default function:
  13. * export default function (pi: ExtensionAPI) { ... }
  14. */
  15. import {
  16. createAgentSession,
  17. DefaultResourceLoader,
  18. getAgentDir,
  19. SessionManager,
  20. } from "@earendil-works/pi-coding-agent";
  21. // Extensions are discovered automatically from standard locations.
  22. // You can also add paths via settings.json or DefaultResourceLoader options.
  23. const resourceLoader = new DefaultResourceLoader({
  24. cwd: process.cwd(),
  25. agentDir: getAgentDir(),
  26. additionalExtensionPaths: ["./my-logging-extension.ts", "./my-safety-extension.ts"],
  27. extensionFactories: [
  28. (pi) => {
  29. pi.on("agent_start", () => {
  30. console.log("[Inline Extension] Agent starting");
  31. });
  32. },
  33. ],
  34. });
  35. await resourceLoader.reload();
  36. const { session } = await createAgentSession({
  37. resourceLoader,
  38. sessionManager: SessionManager.inMemory(),
  39. });
  40. try {
  41. session.subscribe((event) => {
  42. if (event.type === "message_update" && event.assistantMessageEvent.type === "text_delta") {
  43. process.stdout.write(event.assistantMessageEvent.delta);
  44. }
  45. });
  46. await session.prompt("List files in the current directory.");
  47. console.log();
  48. } finally {
  49. session.dispose();
  50. }
  51. // Example extension file (./my-logging-extension.ts):
  52. /*
  53. import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
  54. export default function (pi: ExtensionAPI) {
  55. pi.on("agent_start", async () => {
  56. console.log("[Extension] Agent starting");
  57. });
  58. pi.on("tool_call", async (event) => {
  59. console.log(\`[Extension] Tool: \${event.toolName}\`);
  60. // Return { block: true, reason: "..." } to block execution
  61. return undefined;
  62. });
  63. pi.on("agent_end", async (event) => {
  64. console.log(\`[Extension] Done, \${event.messages.length} messages\`);
  65. });
  66. // Register a custom tool
  67. pi.registerTool({
  68. name: "my_tool",
  69. label: "My Tool",
  70. description: "Does something useful",
  71. parameters: Type.Object({
  72. input: Type.String(),
  73. }),
  74. execute: async (_toolCallId, params, _signal, _onUpdate, _ctx) => ({
  75. content: [{ type: "text", text: \`Processed: \${params.input}\` }],
  76. details: {},
  77. }),
  78. });
  79. // Register a command
  80. pi.registerCommand("mycommand", {
  81. description: "Do something",
  82. handler: async (args, ctx) => {
  83. ctx.ui.notify(\`Command executed with: \${args}\`);
  84. },
  85. });
  86. }
  87. */