#7580: feat: add message:received internal hook with prompt injection
agents
## Motivation
The OpenClaw documentation lists `message:received` as a [planned event (Future Event)](https://docs.openclaw.ai/hooks#future-events) since the introduction of the hooks system, but the implementation was pending. This PR implements the missing event, completing the lifecycle event coverage for inbound messages.
## What was implemented
- Added `"message"` type to `InternalHookEventType` in `internal-hooks.ts`
- Trigger `message:received` event in `dispatchInboundMessage()` before agent processing
- **Context injection**: hooks can add messages to `hookEvent.messages[]` which will be concatenated to `BodyForAgent`, allowing invisible injection of instructions/context into the LLM prompt
- Debug logs for development tracing
## Advantages
1. **Extensibility**: Allows hooks to react to every received message (not just commands)
2. **Context injection**: Hooks can dynamically modify agent input based on rules (e.g., detect patterns and inject specific instructions)
3. **Documentation parity**: Implements a feature already documented as "Future Event"
4. **Zero breaking changes**: Pure addition, does not alter existing behavior
## Use cases
### 1. Pattern-based context injection
```typescript
const handler: HookHandler = async (event) => {
if (event.type !== "message" || event.action !== "received") return;
if (event.context.body?.includes("urgent")) {
event.messages.push("⚠️ User mentioned urgency. Prioritize this request.");
}
};
```
### 2. Pre-processing validation/auditing
```typescript
const handler: HookHandler = async (event) => {
if (event.type === "message" && event.action === "received") {
console.log(`[audit] Message from ${event.context.senderId} at ${event.timestamp}`);
}
};
```
### 3. Trigger external automations
```typescript
const handler: HookHandler = async (event) => {
if (event.type === "message" && event.action === "received") {
void notifyExternalSystem(event.context);
}
};
```
## Tests
- [x] Hook is triggered for every inbound message
- [x] Injected messages appear in agent prompt
- [ ] No performance impact (async, non-blocking)
- [x] Debug logs appear in development mode
## Breaking changes
None. Pure feature addition.
## Related documentation
- [Hooks - Future Events](https://docs.openclaw.ai/hooks#future-events)
- Implements: `message:received` (previously listed as "Future Event")
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR implements the previously-documented `message:received` internal hook by extending `InternalHookEventType` with `"message"` and triggering the hook from `dispatchInboundMessage()` before reply/agent processing. It also introduces “context injection” by allowing hook handlers to push strings onto `event.messages`, which are then concatenated into `finalized.BodyForAgent`.
The change fits into the existing internal hook registry (`registerInternalHook` / `triggerInternalHook`) by following the same `type` + `action` dispatch scheme used for other lifecycle events.
<h3>Confidence Score: 3/5</h3>
- Reasonably safe to merge, but there are a couple of behavioral and logging concerns that should be addressed first.
- Core changes are small and additive, but the current implementation (1) can corrupt `BodyForAgent` when undefined during injection, and (2) adds synchronous hook execution plus unconditional debug logging that may affect latency and leak message content. These are straightforward to fix but meaningful in production paths.
- src/auto-reply/dispatch.ts
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#11597: feat(hooks): implement message:received hook
by gnufoo · 2026-02-08
90.7%
#7545: feat(hooks): add message:received hook for pre-turn automation
by wangtian24 · 2026-02-02
87.4%
#19922: feat(hooks): add message:received and message:sent hook events
by NOVA-Openclaw · 2026-02-18
87.3%
#10706: feat(hooks): add message:received internal hook for Telegram
by thebtf · 2026-02-06
84.9%
#11732: feat(plugins): add injectMessages to before_agent_start hook
by antra-tess · 2026-02-08
82.1%
#19565: feat: add agent lifecycle hook events (session, message, error)
by tag-assistant · 2026-02-17
81.9%
#16618: feat: bridge message lifecycle hooks to workspace hook system
by DarlingtonDeveloper · 2026-02-14
81.8%
#15577: feat(hooks): add message:preprocessed hook event
by heybeaux · 2026-02-13
80.9%
#9677: feat: expose incomingMessage in bootstrap hook context
by speedbal · 2026-02-05
80.9%
#10109: feat(plugins): invoke message_received and message_sent hooks
by nezovskii · 2026-02-06
80.8%