feat(plugins): add before_dispatch hook for pre-LLM message interception#43422
feat(plugins): add before_dispatch hook for pre-LLM message interception#43422loveyana wants to merge 7 commits intoopenclaw:mainfrom
before_dispatch hook for pre-LLM message interception#43422Conversation
…ption Add a new plugin hook `before_dispatch` that fires in `dispatchReplyFromConfig` after `message_received` hooks and before LLM invocation. This allows plugins to block the entire dispatch and optionally send a direct reply to the user. Use case: security plugins (e.g. authentication gates) can prevent unauthenticated sessions from consuming LLM tokens by blocking the dispatch early and returning a "please login" message. Refs: openclaw#43418 Made-with: Cursor
Greptile SummaryThis PR adds a new Key observations:
Confidence Score: 3/5
Last reviewed commit: e84a114 |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e84a11411a
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
…tics Address bot review feedback: - Use actual sendFinalReply() return for queuedFinal instead of Boolean proxy - Add JSDoc note about fail-open error semantics for security plugins Made-with: Cursor
|
All bot review conversations have been addressed — replied inline and pushed a follow-up commit ( @vincentkoc @joshavant — would appreciate a review when you have a moment. This adds a The change is small (+104 lines across 3 files), follows existing hook patterns, and all 111 existing tests pass. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: fafeff9d19
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
|
This is a super useful feature indeed! LGTM |
|
I think this is a very useful patch. The existing hooks does not have similar capabilities, so that we can hook on the message processing link and do more secure authentication and identification. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a8abdf8980
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9e76a214d8
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6b91ebf4a1
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| if (beforeDispatchResult?.block) { | ||
| const queuedFinal = beforeDispatchResult.replyText | ||
| ? dispatcher.sendFinalReply({ text: beforeDispatchResult.replyText }) | ||
| : false; | ||
| recordProcessed("skipped", { reason: "before_dispatch_blocked" }); | ||
| return { queuedFinal, counts: dispatcher.getQueuedCounts() }; |
There was a problem hiding this comment.
Emit message_received hooks before returning blocked dispatch
The new early return in the before_dispatch block exits dispatchReplyFromConfig before the existing inbound hook fanout runs (runMessageReceived and internal message:received are below this branch), so any turn blocked by before_dispatch is now invisible to plugins and internal consumers that rely on inbound message events for audit/metrics/state updates. This creates a regression specifically when a plugin returns { block: true }: the user gets a reply, but downstream hook-based observers never see the inbound message.
Useful? React with 👍 / 👎.
|
Thanks for the contribution @loveyana — the gap you identified was real and the use case is legit. However, the Closing as superseded — the feature you need is already available. Let us know if the landed version doesn't cover your auth gate use case and we can iterate from there. |
Summary
before_dispatchplugin hook that fires indispatchReplyFromConfigaftermessage_receivedand before LLM invocation{ block: true, replyText: "..." }to abort the dispatch entirely and send a direct reply, preventing any LLM token consumptionChanges
src/plugins/types.ts"before_dispatch"toPluginHookName, definePluginHookBeforeDispatchEventandPluginHookBeforeDispatchResulttypes, add handler toPluginHookHandlerMapsrc/plugins/hooks.tsrunBeforeDispatch()method — runs handlers sequentially, first{ block: true }winssrc/auto-reply/reply/dispatch-from-config.tshookRunner.runBeforeDispatch()aftermessage_receivedhooks and beforemarkProcessing()Hook Signature
Why existing hooks are insufficient
message_receivedmessage_sendingsrc/infra/outbound/deliver.ts; channel plugins with custom dispatchers (Feishu, webchat) bypass itbefore_message_writebefore_agent_startAuthentication gate flow
Closes #43418
AI-Assisted PR
tsc --noEmit, all existing tests pass (44 + 19 + 48 = 111 tests)Test plan
npx tsc --noEmitpasses (zero type errors)npx vitest run src/auto-reply/reply/dispatch-from-config.test.ts— 44 tests passnpx vitest run src/plugins/hooks.*.test.ts— 19 tests passnpx vitest run src/plugins/loader.test.ts src/plugins/wired-hooks-message.test.ts— 48 tests pass