Documentation
¶
Overview ¶
Package hooks provides lifecycle hooks for Claude Code events.
Index ¶
- Constants
- type Config
- type Engine
- func (e *Engine) Disable()
- func (e *Engine) Enable()
- func (e *Engine) Fire(eventType EventType, data EventData) error
- func (e *Engine) FireNotification(level, message, source string) error
- func (e *Engine) FirePermissionRequest(toolName, action, description string, isSubagent bool) error
- func (e *Engine) FirePostToolUse(toolName, filePath string, success bool, errMsg string, isSubagent bool) error
- func (e *Engine) FirePreCompact(compactionType string, currentSize, estimatedNewSize int) error
- func (e *Engine) FirePreToolUse(toolName, filePath string, isSubagent bool) error
- func (e *Engine) FireSessionEnd(projectPath string, messageCount int) error
- func (e *Engine) FireSessionStart(projectPath, modelID string) error
- func (e *Engine) FireStop(responseLength, tokensUsed int, toolsUsed []string, isSubagent bool) error
- func (e *Engine) FireSubagentStop(taskDescription, subagentType string, responseLength, tokensUsed int, ...) error
- func (e *Engine) FireUserPromptSubmit(messageText string) error
- type Event
- type EventData
- type EventType
- type ExecutionResult
- type Executor
- type HookConfig
- type MatchConfig
- type MuxBridge
- type NotificationData
- type PermissionRequestData
- type PreCompactData
- type SessionEndData
- type SessionStartData
- type Settings
- type StopData
- type SubagentStopData
- type ToolUseData
- type UserPromptSubmitData
Constants ¶
const (
// DefaultHookTimeoutMS is the default timeout for hook execution in milliseconds
DefaultHookTimeoutMS = 5000
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config holds the complete hook configuration for all events
func LoadConfig ¶
LoadConfig loads hook configuration from user and project locations Project config overrides user config
func (*Config) GetHooks ¶
func (c *Config) GetHooks(eventType EventType) []HookConfig
GetHooks returns all hooks configured for a given event type
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine manages hook execution across the application lifecycle
func NewEngineWithConfig ¶
NewEngineWithConfig creates an engine with a specific configuration Useful for testing
func (*Engine) Fire ¶
Fire executes all hooks for a given event Collects and returns all errors from failed hooks as a multi-error
func (*Engine) FireNotification ¶
FireNotification is a convenience method for Notification event
func (*Engine) FirePermissionRequest ¶
FirePermissionRequest is a convenience method for PermissionRequest event
func (*Engine) FirePostToolUse ¶
func (e *Engine) FirePostToolUse(toolName, filePath string, success bool, errMsg string, isSubagent bool) error
FirePostToolUse is a convenience method for PostToolUse event
func (*Engine) FirePreCompact ¶
FirePreCompact is a convenience method for PreCompact event
func (*Engine) FirePreToolUse ¶
FirePreToolUse is a convenience method for PreToolUse event
func (*Engine) FireSessionEnd ¶
FireSessionEnd is a convenience method for SessionEnd event
func (*Engine) FireSessionStart ¶
FireSessionStart is a convenience method for SessionStart event
func (*Engine) FireStop ¶
func (e *Engine) FireStop(responseLength, tokensUsed int, toolsUsed []string, isSubagent bool) error
FireStop is a convenience method for Stop event
func (*Engine) FireSubagentStop ¶
func (e *Engine) FireSubagentStop(taskDescription, subagentType string, responseLength, tokensUsed int, success bool, executionTime float64) error
FireSubagentStop is a convenience method for SubagentStop event
func (*Engine) FireUserPromptSubmit ¶
FireUserPromptSubmit is a convenience method for UserPromptSubmit event
type EventData ¶
type EventData interface {
// ToEnvVars converts the event data to environment variables for shell execution
ToEnvVars() map[string]string
}
EventData contains context-specific data for each event type
type EventType ¶
type EventType string
EventType represents the type of hook event
const ( // EventSessionStart fires when a Claude Code session begins (before first user message) EventSessionStart EventType = "SessionStart" // EventSessionEnd fires when Claude Code session terminates (clean shutdown) EventSessionEnd EventType = "SessionEnd" // EventUserPromptSubmit fires when user sends a message (before Claude processes it) EventUserPromptSubmit EventType = "UserPromptSubmit" // EventPreToolUse fires after Claude creates tool parameters, before tool execution EventPreToolUse EventType = "PreToolUse" // EventPostToolUse fires immediately after a tool completes successfully EventPostToolUse EventType = "PostToolUse" // EventPermissionRequest fires when user is shown a permission dialog for a tool EventPermissionRequest EventType = "PermissionRequest" // EventNotification fires when Claude Code sends notifications to the user EventNotification EventType = "Notification" // EventStop fires when main Claude Code agent finishes responding to user EventStop EventType = "Stop" // EventSubagentStop fires when a subagent finishes responding EventSubagentStop EventType = "SubagentStop" // EventPreCompact fires before Claude Code runs a compact operation EventPreCompact EventType = "PreCompact" )
type ExecutionResult ¶
ExecutionResult contains the result of a hook execution
type Executor ¶
type Executor struct {
// contains filtered or unexported fields
}
Executor executes shell commands for hooks
func NewExecutor ¶
NewExecutor creates a new hook executor
func (*Executor) Execute ¶
func (e *Executor) Execute(hook *HookConfig, event *Event) *ExecutionResult
Execute runs a hook command with the given event context
func (*Executor) ExecuteAsync ¶
func (e *Executor) ExecuteAsync(hook *HookConfig, event *Event)
ExecuteAsync runs a hook command asynchronously (fire and forget) Includes panic recovery to prevent crashes from hook failures
type HookConfig ¶
type HookConfig struct {
// Command is the shell command to execute
Command string `json:"command"`
// Description explains what the hook does
Description string `json:"description,omitempty"`
// Timeout in milliseconds, defaults to DefaultHookTimeoutMS
Timeout int `json:"timeout,omitempty"`
// Env provides environment variables for the hook command
Env map[string]string `json:"env,omitempty"`
// Match defines conditions for when the hook should execute
Match *MatchConfig `json:"match,omitempty"`
// IgnoreFailure continues execution even if hook fails
IgnoreFailure bool `json:"ignoreFailure,omitempty"`
// Async runs the hook in the background without blocking
Async bool `json:"async,omitempty"`
}
HookConfig represents a single hook configuration
func (*HookConfig) GetTimeout ¶
func (hc *HookConfig) GetTimeout() int
GetTimeout returns the timeout in milliseconds, defaulting to DefaultHookTimeoutMS if not set
func (*HookConfig) ShouldExecute ¶
func (hc *HookConfig) ShouldExecute(event *Event) bool
ShouldExecute checks if a hook should execute based on its match configuration
type MatchConfig ¶
type MatchConfig struct {
// ToolName filters hooks to only run for specific tool names
ToolName string `json:"toolName,omitempty"`
// FilePattern is a regex to match against file paths in tool use events
FilePattern string `json:"filePattern,omitempty"`
// IsSubagent filters hooks based on whether execution is in a subagent context
// Uses pointer to distinguish between false and unset
IsSubagent *bool `json:"isSubagent,omitempty"`
// Level filters notification events by severity level
Level string `json:"level,omitempty"`
}
MatchConfig defines conditions for when a hook should execute
type MuxBridge ¶ added in v1.10.0
type MuxBridge struct {
// contains filtered or unexported fields
}
MuxBridge bridges mux's hook system to hex's hook engine. It registers handlers on a mux HookManager that forward events to the hex hook engine for shell command execution.
func NewMuxBridge ¶ added in v1.10.0
NewMuxBridge creates a bridge between mux hooks and the hex engine. Returns both the bridge and the mux HookManager to wire into agents.
type NotificationData ¶
NotificationData contains data for Notification event
func (NotificationData) ToEnvVars ¶
func (d NotificationData) ToEnvVars() map[string]string
ToEnvVars converts NotificationData to environment variables
type PermissionRequestData ¶
type PermissionRequestData struct {
ToolName string
Action string
Description string
IsSubagent bool
}
PermissionRequestData contains data for PermissionRequest event
func (PermissionRequestData) ToEnvVars ¶
func (d PermissionRequestData) ToEnvVars() map[string]string
ToEnvVars converts PermissionRequestData to environment variables
type PreCompactData ¶
PreCompactData contains data for PreCompact event
func (PreCompactData) ToEnvVars ¶
func (d PreCompactData) ToEnvVars() map[string]string
ToEnvVars converts PreCompactData to environment variables
type SessionEndData ¶
SessionEndData contains data for SessionEnd event
func (SessionEndData) ToEnvVars ¶
func (d SessionEndData) ToEnvVars() map[string]string
ToEnvVars converts SessionEndData to environment variables
type SessionStartData ¶
SessionStartData contains data for SessionStart event
func (SessionStartData) ToEnvVars ¶
func (d SessionStartData) ToEnvVars() map[string]string
ToEnvVars converts SessionStartData to environment variables
type Settings ¶
type Settings struct {
Hooks map[EventType]interface{} `json:"hooks,omitempty"`
}
Settings represents the .hex/settings.json structure
type SubagentStopData ¶
type SubagentStopData struct {
TaskDescription string
SubagentType string
ResponseLength int
TokensUsed int
Success bool
ExecutionTime float64 // in seconds
}
SubagentStopData contains data for SubagentStop event
func (SubagentStopData) ToEnvVars ¶
func (d SubagentStopData) ToEnvVars() map[string]string
ToEnvVars converts SubagentStopData to environment variables
type ToolUseData ¶
type ToolUseData struct {
ToolName string
FilePath string // extracted from parameters if available
Success bool // only for PostToolUse
Error string // only for PostToolUse (if failed)
IsSubagent bool
}
ToolUseData contains data for PreToolUse and PostToolUse events
func (ToolUseData) ToEnvVars ¶
func (d ToolUseData) ToEnvVars() map[string]string
ToEnvVars converts ToolUseData to environment variables
type UserPromptSubmitData ¶
UserPromptSubmitData contains data for UserPromptSubmit event
func (UserPromptSubmitData) ToEnvVars ¶
func (d UserPromptSubmitData) ToEnvVars() map[string]string
ToEnvVars converts UserPromptSubmitData to environment variables