send-user-message.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * Send User Message Example
  3. *
  4. * Demonstrates pi.sendUserMessage() for sending user messages from extensions.
  5. * Unlike pi.sendMessage() which sends custom messages, sendUserMessage() sends
  6. * actual user messages that appear in the conversation as if typed by the user.
  7. *
  8. * Usage:
  9. * /ask What is 2+2? - Sends a user message (always triggers a turn)
  10. * /steer Focus on X - Sends while streaming with steer delivery
  11. * /followup And then? - Sends while streaming with followUp delivery
  12. */
  13. import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
  14. export default function (pi: ExtensionAPI) {
  15. // Simple command that sends a user message
  16. pi.registerCommand("ask", {
  17. description: "Send a user message to the agent",
  18. handler: async (args, ctx) => {
  19. if (!args.trim()) {
  20. ctx.ui.notify("Usage: /ask <message>", "warning");
  21. return;
  22. }
  23. // sendUserMessage always triggers a turn when not streaming
  24. // If streaming, it will throw (no deliverAs specified)
  25. if (!ctx.isIdle()) {
  26. ctx.ui.notify("Agent is busy. Use /steer or /followup instead.", "warning");
  27. return;
  28. }
  29. pi.sendUserMessage(args);
  30. },
  31. });
  32. // Command that steers the agent mid-conversation
  33. pi.registerCommand("steer", {
  34. description: "Send a steering message (interrupts current processing)",
  35. handler: async (args, ctx) => {
  36. if (!args.trim()) {
  37. ctx.ui.notify("Usage: /steer <message>", "warning");
  38. return;
  39. }
  40. if (ctx.isIdle()) {
  41. // Not streaming, just send normally
  42. pi.sendUserMessage(args);
  43. } else {
  44. // Streaming - use steer to interrupt
  45. pi.sendUserMessage(args, { deliverAs: "steer" });
  46. }
  47. },
  48. });
  49. // Command that queues a follow-up message
  50. pi.registerCommand("followup", {
  51. description: "Queue a follow-up message (waits for current processing)",
  52. handler: async (args, ctx) => {
  53. if (!args.trim()) {
  54. ctx.ui.notify("Usage: /followup <message>", "warning");
  55. return;
  56. }
  57. if (ctx.isIdle()) {
  58. // Not streaming, just send normally
  59. pi.sendUserMessage(args);
  60. } else {
  61. // Streaming - queue as follow-up
  62. pi.sendUserMessage(args, { deliverAs: "followUp" });
  63. ctx.ui.notify("Follow-up queued", "info");
  64. }
  65. },
  66. });
  67. // Example with content array (text + images would go here)
  68. pi.registerCommand("askwith", {
  69. description: "Send a user message with structured content",
  70. handler: async (args, ctx) => {
  71. if (!args.trim()) {
  72. ctx.ui.notify("Usage: /askwith <message>", "warning");
  73. return;
  74. }
  75. if (!ctx.isIdle()) {
  76. ctx.ui.notify("Agent is busy", "warning");
  77. return;
  78. }
  79. // sendUserMessage accepts string or (TextContent | ImageContent)[]
  80. pi.sendUserMessage([
  81. { type: "text", text: `User request: ${args}` },
  82. { type: "text", text: "Please respond concisely." },
  83. ]);
  84. },
  85. });
  86. }