In OpenClaw, what is an internal hook made of, and how does it choose which Gateway events it handles?
answer
- two files in one folder
- frontmatter under metadata.openclaw
- family key or exact action key
- handler.ts first, then handler.js
- a misspelt key registers but never fires
basics
~20 sAn OpenClaw internal hook is a folder holding HOOK.md and a JavaScript or TypeScript handler. HOOK.md's metadata.openclaw.events array subscribes it to exact keys such as command:new or message:received, or to a whole family such as command.
solid answer
~40 sAn internal hook is a directory with two files: `HOOK.md` and a handler. `HOOK.md` carries YAML frontmatter (`name`, `description` and a `metadata.openclaw` object whose `events` array lists what to subscribe to) followed by Markdown explaining the side effects. Discovery uses the first of `handler.ts`, `handler.js`, `index.ts`, `index.js`, and the loader calls the default export with an event carrying `type`, `action`, `sessionKey`, `timestamp`, `context` and `messages`. An event key is either exact, such as `command:new`, `session:auto-reset`, `gateway:startup` or `message:received`, or a bare family (`command`, `session`, `agent`, `gateway`, `message`) that receives every action in it. A hook that declares no events is not loadable, and a misspelt key such as `command:nwe` registers with a warning but never fires.
code
javascript · 5 linesexport default async function handler(event) {
if (event.type !== "command") return;
if (!["new", "reset"].includes(event.action)) return;
console.log(`[reset-note] ${event.action} on ${event.sessionKey}`);
}go deeper
Recall the two files, HOOK.md and a handler, and that metadata.openclaw.events lists what the hook listens to. Name a few real keys such as command:new and message:received.
Explain family against exact subscriptions, the order handlers run in, why a hook with no events will not load, and that enabling happens in config rather than in HOOK.md.
Show you know the traps that make a hook silently useless: a misspelt key that never fires, session:compact not being a wildcard, double calls from overlapping subscriptions, and colon names differing from plugin hook names.
Frame internal hooks as cheap observation points with a fixed event catalog, and be ready to say when a team should reach for a plugin hook or a scheduled job instead.
## What an internal hook is An **internal hook** is a small piece of JavaScript or TypeScript that the OpenClaw **Gateway** (the long-running daemon that connects chat apps to agents) runs inside its own process when it emits an event: a `/new` command, a session reset, a received message, the Gateway starting up. Hooks are for short side effects such as saving context, logging a command or adding a file to the agent's context. They are not a way to schedule work and not a way for outside services to call in; those jobs belong to heartbeats, cron jobs and the inbound webhook endpoints. ## The two files Every hook lives in its own directory and needs both files; a `HOOK.md` alone is not enough. - **`HOOK.md`**: YAML frontmatter followed by human-readable Markdown. The frontmatter holds `name` (defaults to the directory name), `description` (shown in reports) and a `metadata.openclaw` object. - **The handler**: discovery checks `handler.ts`, `handler.js`, `index.ts`, then `index.js`, and uses the first it finds. The loader calls the default export unless `metadata.openclaw.export` names another export. The fields under `metadata.openclaw` that matter most: - `events`: the event keys to subscribe to. At least one is needed or the hook is not loadable. - `requires`: `bins`, `anyBins`, `env` and `config` checks that decide whether the hook is eligible on this host. - `os`: allowed Node platform names such as `darwin` or `linux`. - `hookKey`: the config-entry key, defaulting to the hook name. Whether the hook is switched on is not decided in `HOOK.md` at all. It is decided in config, under `hooks.internal.entries.<hookKey>.enabled`. ```yaml name: reset-note description: "Log a marker when a session is reset" metadata: { "openclaw": { "events": ["command:new", "command:reset"] } } ``` ## Event keys: families and exact actions Each event has a `type` (its family) and an `action`. A subscription is either the bare family or the exact `family:action` key. | Family | Example exact keys | Fires when | | --- | --- | --- | | `command` | `command:new`, `command:reset`, `command:stop` | An authorized `/new`, `/reset` or `/stop` is handled | | `session` | `session:auto-reset`, `session:compact:before`, `session:compact:after`, `session:patch` | A daily or idle rollover, compaction, or a session settings change | | `agent` | `agent:bootstrap` | Workspace bootstrap files are resolved before context injection | | `gateway` | `gateway:startup`, `gateway:shutdown`, `gateway:pre-restart` | The Gateway starts, shuts down or prepares to restart | | `message` | `message:received`, `message:transcribed`, `message:preprocessed`, `message:sent` | An accepted inbound message, its preprocessing, or a send outcome | Rules worth knowing: 1. A **family** subscription receives every action in that family. Subscribing one handler to both `command` and `command:new` makes it run twice on `/new`. 2. For one event, **family listeners run first, then exact listeners**, each group in registration order. The dispatcher awaits each handler in turn. 3. `session:compact` is **not** a family or wildcard; subscribe to the two exact compaction keys. 4. An **unknown key** such as `command:nwe` still registers; the loader warns and `openclaw hooks info` reports it, but core never emits it. Declaring a custom key does not create a trigger. 5. Internal event names use colons (`message:received`). They are not the typed plugin hook names (`message_received`) that plugins register through `api.on(...)`, which is a separate system. ## The handler A handler is a function returning `void` or `Promise<void>`. It receives one event object: - `type` and `action`: the family and the action, for example `command` and `new`. - `sessionKey`: which session the event belongs to (Gateway events carry a Gateway key instead). - `timestamp`: when the event object was created. - `context`: event-specific data, such as `senderId` and `workspaceDir` on a reset or `channelId` and `content` on a received message. - `messages`: an initially empty array of strings that only some producers turn into replies. Filter early: check `event.type` and `event.action` first and return on anything unrelated. ## Where hooks are found The Gateway discovers hook directories from several sources and merges them by name: hooks **bundled** with OpenClaw, hook directories of active **plugins**, **managed** hooks in `~/.openclaw/hooks/` (more precisely `<stateDir>/hooks/`), any `hooks.internal.load.extraDirs`, and the selected agent's **workspace** `hooks/` folder. A workspace hook needs an explicit `enabled: true` entry and can add a new name but cannot replace a bundled, plugin or managed hook of the same name.
- What happens if one OpenClaw hook subscribes to both `command` and `command:new`?On `/new` the same handler runs twice: once as a `command` family listener and once as a `command:new` exact listener. Family listeners run before exact ones. Either subscribe to the family and filter on `event.action`, or list only the exact keys you need.
- Can the hook live in the agent's workspace instead of `~/.openclaw/hooks/`?Yes, in `<workspace>/hooks/<name>/`, but a workspace hook stays off until `hooks.internal.entries.<hookKey>.enabled` is true, even under open discovery, and it cannot replace a bundled, plugin or managed hook with the same name. The Gateway loads directory hooks only from its selected workspace, not from every agent's folder.
- An internal hook is a shell script the Gateway runs on each event.
- Putting enabled: true in HOOK.md frontmatter switches the hook on.
- Subscribing to session:compact catches both compaction phases.
- Declaring a custom event key in HOOK.md makes OpenClaw start emitting it.
- Internal event names are the same as plugin hook names like message_received.