file-trigger.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /**
  2. * File Trigger Extension
  3. *
  4. * Watches a trigger file and injects its contents into the conversation.
  5. * Useful for external systems to send messages to the agent.
  6. *
  7. * Usage:
  8. * echo "Run the tests" > /tmp/agent-trigger.txt
  9. */
  10. import * as fs from "node:fs";
  11. import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
  12. export default function (pi: ExtensionAPI) {
  13. pi.on("session_start", async (_event, ctx) => {
  14. const triggerFile = "/tmp/agent-trigger.txt";
  15. fs.watch(triggerFile, () => {
  16. try {
  17. const content = fs.readFileSync(triggerFile, "utf-8").trim();
  18. if (content) {
  19. pi.sendMessage(
  20. {
  21. customType: "file-trigger",
  22. content: `External trigger: ${content}`,
  23. display: true,
  24. },
  25. { triggerTurn: true }, // triggerTurn - get LLM to respond
  26. );
  27. fs.writeFileSync(triggerFile, ""); // Clear after reading
  28. }
  29. } catch {
  30. // File might not exist yet
  31. }
  32. });
  33. if (ctx.hasUI) {
  34. ctx.ui.notify(`Watching ${triggerFile}`, "info");
  35. }
  36. });
  37. }