
























@@ -15,6 +15,18 @@ There are two kinds of hooks in OpenClaw:
15151616Hooks can also be bundled inside plugins. `openclaw hooks list` shows both standalone hooks and plugin-managed hooks.
171718+## Choose the right surface
19+20+OpenClaw has several extension surfaces that look similar but solve different problems:
21+22+| If you want to... | Use... | Why |
23+| --------------------------------------------------------------------------------------------------------------------- | ------------------------------------- | --------------------------------------------------------------------------------------------- |
24+| Save a snapshot on `/new`, log `/reset`, call an external API after `message:sent`, or add coarse operator automation | Internal hooks (`HOOK.md`, this page) | File-based hooks are meant for operator-managed side effects and command/lifecycle automation |
25+| Rewrite prompts, block tools, cancel outbound messages, or add ordered middleware/policy | Typed plugin hooks via `api.on(...)` | Typed hooks have explicit contracts, priorities, merge rules, and block/cancel semantics |
26+| Add telemetry-only export or observability | Diagnostic events | Observability is a separate event bus, not a policy hook surface |
27+28+Use internal hooks when you want automation that behaves like a small installed integration. Use typed plugin hooks when you need runtime lifecycle control.
29+1830## Quick start
19312032```bash
@@ -101,14 +113,19 @@ const handler = async (event) => {
101113console.log(`[my-hook] New command triggered`);
102114// Your logic here
103115104-// Optionally send message to user
116+// Optionally send a reply on replyable surfaces
105117event.messages.push("Hook executed!");
106118};
107119108120export default handler;
109121```
110122111-Each event includes: `type`, `action`, `sessionKey`, `timestamp`, `messages` (push to send to user), and `context` (event-specific data). Agent and tool plugin hook contexts can also include `trace`, a read-only W3C-compatible diagnostic trace context that plugins may pass into structured logs for OTEL correlation.
123+Each event includes: `type`, `action`, `sessionKey`, `timestamp`, `messages` (push replies here on replyable surfaces only), and `context` (event-specific data). Agent and tool plugin hook contexts can also include `trace`, a read-only W3C-compatible diagnostic trace context that plugins may pass into structured logs for OTEL correlation.
124+125+`event.messages` is only delivered automatically on replyable surfaces such as
126+`command:*` and `message:received`. Lifecycle-only events such as
127+`agent:bootstrap`, `session:*`, `gateway:*`, or `message:sent` do not have a
128+reply channel and ignore pushed messages.
112129113130### Event context highlights
114131@@ -253,6 +270,11 @@ intercepting tool calls, modifying prompts, controlling message flow, and more.
253270Use plugin hooks when you need `before_tool_call`, `before_agent_reply`,
254271`before_install`, or other in-process lifecycle hooks.
255272273+Plugin-managed internal hooks are different: they participate in this page's
274+coarse command/lifecycle event system and show up in `openclaw hooks list` as
275+`plugin:<id>`. Use those for side effects and compatibility with hook packs, not
276+for ordered middleware or policy gates.
277+256278For the complete plugin hook reference, see [Plugin hooks](/plugins/hooks).
257279258280## Configuration
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。