Support custom hooks
To upvote this issue, give it a thumbs up. See this list for the most upvoted issues.
Same of https://docs.anthropic.com/en/docs/claude-code/hooks-guide
The events I can think we could support as of now:
- preToolCall
- postToolCall
- prePrompt
- postPrompt
Those are easy to support as we already have a single place for them in chat.clj
Something to think: How the UX for user should be? I can think in sending a system message we a text "Running hook abc", maybe user could customize this message too.
After some thought, I will write this config api:
"hooks": {
"notify-me": { // any name
"type": "preToolCall", // I thought on 'preToolCall' | 'postToolCall' | 'preToolCallApproval' | 'prePrompt' | 'postPrompt'
"matcher": "*", // regex on tool call name: clojure-mcp__edit_file
"actions": [
{
"type": "shell",
"shell": "notify-send \"preToolCall $0\"" // stdin will be a json like claude does, stdout will be used for the block message content or context for prePrompt, dunno
}
]
}
},
@bombaywalla I'm starting to impl this feature, and one of the things we will need to do is touch the tool call state machine: we will need to check/run preToolCall and postToolCall hooks:
- preToolCall: needs to run immediatly before check approval, after all toolCallPrepare were sent
- postToolCall: needs to run immediatly after toolCalled/toolCallRejected was sent
Does that makes sense for you?
I just read up on Claude Code hooks, so my understanding may be minimal...
My thoughts...
The hook name seems superfluous to me. How about:
"hooks": {
"preToolCall": [
{
"name": "hook1",
"matcher": "*",
"actions": [ ...]
},
{
"name": "hook2",
"matcher": "*",
"actions": [ ...]
}],
...
}
That way, if there are multiple hooks of the same type, the order of execution is specified by the array sequence. [[Added later]] On more reflection, I still think the array is a better idea, but it might be good to also include a "name" attribute to the hook so that we can identify the hook in log/error messages.
My question would be: what takes precedence for approval - the config? the user ? the hook? The precedence should determine where the hook is called. My sense is that the same as yours, that it should be before the check approval. Or alternatively, the check approval itself could run the hook. I understand that the hook can return :reject and that would reject the tool call. But what if the hook returns :approve? Or :ask? In those cases, does the config or user play any further part?
[[Added later]] In reading the Claude Code docs again, it seems that the intent of the hook is a "last opportunity" to abort the call, dynamically, with run-time information. If so, then the hook needs to come at the end of the approval chain - just before the future of the call is created. or, alternatively, at the end of check approval, if previously, the approval results in :approve.
As to the postToolCall, my reading of the Claude Code docs is that they run the hook after the completion of the call. So, just after the :competed status in the state machine.
My feeling is that we should mimic what Claude Code does - in order to maintain the knowledge of users who use both ECA and Claude Code. Unless there is a very good reason to break compatibility.
One general question I have is whether all hooks return a value or only some? In any case, we need to document the semantics of the return values.
@bombaywalla my idea is to mention in a expandable block in the UI a "Running hook
The problem with array is that is harder to merge if having in multiple configs (global config, + local config + future place configs)
If so, then the hook needs to come at the end of the approval chain
Hum, yeah, sounds like a good idea, thanks, I will do that.
One general question I have is whether all hooks return a value or only some? In any case, we need to document the semantics of the return values.
yes, documentation will be needed for those, but my idea is support similar to claude, maybe just exit status for now.
The problem with array is that is harder to merge if having in multiple configs (global config, + local config + future place configs)
Fair enough. Not impossible, but certainly harder. If there are multiple hooks of the same type, then what is the specified order of execution?
then what is the specified order of execution?
Like claude, I intend to do in parallel, or with alphabetic order
It's coming!
Hooks are available now from 0.70.1! Make sure to update your eca client as well
Currently hooks (preToolCall, postToolCall, etc.) are triggered only after the tool call has been approved. Is there a possibility to add a hook that would be triggered when the approval prompt appears, so it’s possible to react to this event (e.g. log or show a notification)? For example, something like onToolApprovalPrompt
Yeah, I think it'd be a good addition, I wonder if that should more generic for "waitingUserAction" or something, but sounds good anyway, @Artawower can you open a new feature request issue for that please?