shutdown-command.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * Shutdown Command Extension
  3. *
  4. * Adds a /quit command that allows extensions to trigger clean shutdown.
  5. * Demonstrates how extensions can use ctx.shutdown() to exit pi cleanly.
  6. */
  7. import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
  8. import { Type } from "typebox";
  9. export default function (pi: ExtensionAPI) {
  10. // Register a /quit command that cleanly exits pi
  11. pi.registerCommand("quit", {
  12. description: "Exit pi cleanly",
  13. handler: async (_args, ctx) => {
  14. ctx.shutdown();
  15. },
  16. });
  17. // You can also create a tool that shuts down after completing work
  18. pi.registerTool({
  19. name: "finish_and_exit",
  20. label: "Finish and Exit",
  21. description: "Complete a task and exit pi",
  22. parameters: Type.Object({}),
  23. async execute(_toolCallId, _params, _signal, _onUpdate, ctx) {
  24. // Do any final work here...
  25. // Request graceful shutdown (deferred until agent is idle)
  26. ctx.shutdown();
  27. // This return is sent to the LLM before shutdown occurs
  28. return {
  29. content: [{ type: "text", text: "Shutdown requested. Exiting after this response." }],
  30. details: {},
  31. };
  32. },
  33. });
  34. // You could also create a more complex tool with parameters
  35. pi.registerTool({
  36. name: "deploy_and_exit",
  37. label: "Deploy and Exit",
  38. description: "Deploy the application and exit pi",
  39. parameters: Type.Object({
  40. environment: Type.String({ description: "Target environment (e.g., production, staging)" }),
  41. }),
  42. async execute(_toolCallId, params, _signal, onUpdate, ctx) {
  43. onUpdate?.({ content: [{ type: "text", text: `Deploying to ${params.environment}...` }], details: {} });
  44. // Example deployment logic
  45. // const result = await pi.exec("npm", ["run", "deploy", params.environment], { signal });
  46. // On success, request graceful shutdown
  47. onUpdate?.({ content: [{ type: "text", text: "Deployment complete, exiting..." }], details: {} });
  48. ctx.shutdown();
  49. return {
  50. content: [{ type: "text", text: "Done! Shutdown requested." }],
  51. details: { environment: params.environment },
  52. };
  53. },
  54. });
  55. }