Add file-trigger hook example

This commit is contained in:
Mario Zechner 2025-12-17 16:06:10 +01:00
parent e7097d911a
commit 0ef73ad1dc
4 changed files with 62 additions and 0 deletions

22
.opencode/tool/sleep.ts Normal file
View file

@ -0,0 +1,22 @@
import { tool } from "@opencode-ai/plugin"
export default tool({
description: "Sleep for a specified number of milliseconds",
args: {
ms: tool.schema.number().describe("Number of milliseconds to sleep"),
},
async execute(args, context) {
if (context.abort.aborted) {
throw new Error("Sleep aborted")
}
await new Promise<void>((resolve, reject) => {
const timeout = setTimeout(resolve, args.ms)
const onAbort = () => {
clearTimeout(timeout)
reject(new Error("Sleep aborted"))
}
context.abort.addEventListener("abort", onAbort, { once: true })
})
return `Slept for ${args.ms}ms`
},
})

View file

@ -13,6 +13,9 @@ Creates git stash checkpoints at each turn, allowing code restoration when branc
### protected-paths.ts
Blocks writes to protected paths (.env, .git/, node_modules/).
### file-trigger.ts
Watches a trigger file and injects its contents into the conversation. Useful for external systems (CI, file watchers, webhooks) to send messages to the agent.
## Usage
```bash

View 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");
}
});
}

View file

@ -10,6 +10,7 @@
"@mariozechner/pi-agent-core": ["./packages/agent/src/index.ts"],
"@mariozechner/pi-agent-core/*": ["./packages/agent/src/*"],
"@mariozechner/pi-coding-agent": ["./packages/coding-agent/src/index.ts"],
"@mariozechner/pi-coding-agent/hooks": ["./packages/coding-agent/src/core/hooks/index.ts"],
"@mariozechner/pi-coding-agent/*": ["./packages/coding-agent/src/*"],
"@mariozechner/pi-mom": ["./packages/mom/src/index.ts"],
"@mariozechner/pi-mom/*": ["./packages/mom/src/*"],