mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-16 15:02:32 +00:00
Add file-trigger hook example
This commit is contained in:
parent
e7097d911a
commit
0ef73ad1dc
4 changed files with 62 additions and 0 deletions
36
packages/coding-agent/examples/hooks/file-trigger.ts
Normal file
36
packages/coding-agent/examples/hooks/file-trigger.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/**
|
||||
* File Trigger Hook
|
||||
*
|
||||
* Watches a trigger file and injects its contents into the conversation.
|
||||
* Useful for external systems to send messages to the agent.
|
||||
*
|
||||
* Usage:
|
||||
* echo "Run the tests" > /tmp/agent-trigger.txt
|
||||
*/
|
||||
|
||||
import * as fs from "node:fs";
|
||||
import type { HookAPI } from "@mariozechner/pi-coding-agent/hooks";
|
||||
|
||||
export default function (pi: HookAPI) {
|
||||
pi.on("session", async (event, ctx) => {
|
||||
if (event.reason !== "start") return;
|
||||
|
||||
const triggerFile = "/tmp/agent-trigger.txt";
|
||||
|
||||
fs.watch(triggerFile, () => {
|
||||
try {
|
||||
const content = fs.readFileSync(triggerFile, "utf-8").trim();
|
||||
if (content) {
|
||||
pi.send(`External trigger: ${content}`);
|
||||
fs.writeFileSync(triggerFile, ""); // Clear after reading
|
||||
}
|
||||
} catch {
|
||||
// File might not exist yet
|
||||
}
|
||||
});
|
||||
|
||||
if (ctx.hasUI) {
|
||||
ctx.ui.notify(`Watching ${triggerFile}`, "info");
|
||||
}
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue