diff --git a/.opencode/tool/sleep.ts b/.opencode/tool/sleep.ts new file mode 100644 index 00000000..97ecebb0 --- /dev/null +++ b/.opencode/tool/sleep.ts @@ -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((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` + }, +}) diff --git a/packages/coding-agent/examples/hooks/README.md b/packages/coding-agent/examples/hooks/README.md index 68c557ba..f043bed9 100644 --- a/packages/coding-agent/examples/hooks/README.md +++ b/packages/coding-agent/examples/hooks/README.md @@ -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 diff --git a/packages/coding-agent/examples/hooks/file-trigger.ts b/packages/coding-agent/examples/hooks/file-trigger.ts new file mode 100644 index 00000000..df98d2a2 --- /dev/null +++ b/packages/coding-agent/examples/hooks/file-trigger.ts @@ -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"); + } + }); +} diff --git a/tsconfig.json b/tsconfig.json index c021dc41..815531f2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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/*"],