Claude.Hooks.Hook.Behaviour behaviour (claude v0.2.3)
View SourceBehaviour for implementing Claude Code hooks.
Hooks must implement:
config/0to return their hook configuration as a %Claude.Hooks.Hook{} structconfig/1to return their hook configuration with user-provided configrun/1to execute the hook logic with JSON inputrun/2to execute the hook logic with JSON input and user configdescription/0to provide a human-readable description
Using the macro
You can use the use Claude.Hooks.Hook.Behaviour macro to reduce boilerplate:
defmodule MyHook do
use Claude.Hooks.Hook.Behaviour,
event: :post_tool_use,
matcher: "Write|Edit",
description: "My custom hook"
def run(input) do
# Your hook logic here
:ok
end
# Override to handle user config
def run(input, user_config) do
# Your hook logic with user config
:ok
end
endThe macro automatically:
- Implements the behaviour callbacks
- Generates the config/0 and config/1 functions
- Provides helper functions for common patterns
Summary
Callbacks
Returns the hook configuration as a %Claude.Hook{} struct.
Returns the hook configuration with user-provided config.
Returns a human-readable description of what this hook does.
Executes the hook logic with stdin JSON input.
Executes the hook logic with stdin JSON input and user configuration.
Callbacks
@callback config() :: Claude.Hooks.Hook.t()
Returns the hook configuration as a %Claude.Hook{} struct.
This configuration will be automatically encoded to JSON and written to the settings.json file during installation.
@callback config(user_config :: map()) :: Claude.Hooks.Hook.t()
Returns the hook configuration with user-provided config.
@callback description() :: String.t()
Returns a human-readable description of what this hook does.
Executes the hook logic with stdin JSON input.
Parameters
json_input- The raw JSON string from stdin containing the full hook data
Return values
:ok- Hook executed successfully{:error, reason}- Hook execution failed
Executes the hook logic with stdin JSON input and user configuration.
Parameters
json_input- The raw JSON string from stdin containing the full hook datauser_config- The user configuration from .claude.exs
Return values
:ok- Hook executed successfully{:error, reason}- Hook execution failed