input-transform.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * Input Transform Example - demonstrates the `input` event for intercepting user input.
  3. *
  4. * Start pi with this extension:
  5. * pi -e ./examples/extensions/input-transform.ts
  6. *
  7. * Then type these inside pi:
  8. * ?quick What is TypeScript? → "Respond briefly: What is TypeScript?"
  9. * ping → "pong" (instant, no LLM)
  10. * time → current time (instant, no LLM)
  11. */
  12. import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
  13. export default function (pi: ExtensionAPI) {
  14. pi.on("input", async (event, ctx) => {
  15. // Source-based logic: skip processing for extension-injected messages
  16. if (event.source === "extension") {
  17. return { action: "continue" };
  18. }
  19. // Transform: ?quick prefix for brief responses
  20. if (event.text.startsWith("?quick ")) {
  21. const query = event.text.slice(7).trim();
  22. if (!query) {
  23. ctx.ui.notify("Usage: ?quick <question>", "warning");
  24. return { action: "handled" };
  25. }
  26. return { action: "transform", text: `Respond briefly in 1-2 sentences: ${query}` };
  27. }
  28. // Handle: instant responses without LLM (extension shows its own feedback)
  29. if (event.text.toLowerCase() === "ping") {
  30. ctx.ui.notify("pong", "info");
  31. return { action: "handled" };
  32. }
  33. if (event.text.toLowerCase() === "time") {
  34. ctx.ui.notify(new Date().toLocaleString(), "info");
  35. return { action: "handled" };
  36. }
  37. return { action: "continue" };
  38. });
  39. }