input-transform-streaming.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /**
  2. * Streaming-Aware Input Gate
  3. *
  4. * Demonstrates `event.streamingBehavior` to skip expensive pre-processing
  5. * during mid-stream steering, where low latency matters.
  6. *
  7. * This extension prepends `git diff --stat` output when the user mentions
  8. * file changes, giving the model immediate context. During steering the
  9. * exec call is skipped so the correction reaches the model without delay.
  10. *
  11. * Start pi with this extension:
  12. * pi -e ./examples/extensions/input-transform-streaming.ts
  13. */
  14. import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
  15. const TRIGGER = /\b(changes?|diff|modified)\b/i;
  16. export default function (pi: ExtensionAPI) {
  17. pi.on("input", async (event) => {
  18. // During steering, skip the exec call — corrections should be fast
  19. if (event.streamingBehavior === "steer") {
  20. return { action: "continue" };
  21. }
  22. if (!TRIGGER.test(event.text)) {
  23. return { action: "continue" };
  24. }
  25. const { stdout, code } = await pi.exec("git", ["diff", "--stat"]);
  26. if (code !== 0 || !stdout.trim()) {
  27. return { action: "continue" };
  28. }
  29. return {
  30. action: "transform",
  31. text: `${event.text}\n\nCurrent uncommitted changes:\n\`\`\`\n${stdout.trim()}\n\`\`\``,
  32. };
  33. });
  34. }