Documentation
¶
Overview ¶
Package hooks provides lifecycle hooks for agent tool execution. Hooks allow users to run shell commands or prompts at various points during the agent's execution lifecycle, providing deterministic control over agent behavior.
Index ¶
- type Config
- type Decision
- type EventType
- type Executor
- func (e *Executor) ExecutePostToolUse(ctx context.Context, input *Input) (*Result, error)
- func (e *Executor) ExecutePreToolUse(ctx context.Context, input *Input) (*Result, error)
- func (e *Executor) ExecuteSessionEnd(ctx context.Context, input *Input) (*Result, error)
- func (e *Executor) ExecuteSessionStart(ctx context.Context, input *Input) (*Result, error)
- func (e *Executor) HasPostToolUseHooks() bool
- func (e *Executor) HasPreToolUseHooks() bool
- func (e *Executor) HasSessionEndHooks() bool
- func (e *Executor) HasSessionStartHooks() bool
- type Hook
- type HookSpecificOutput
- type HookType
- type Input
- type MatcherConfig
- type Output
- type Result
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// PreToolUse hooks run before tool execution
PreToolUse []MatcherConfig `json:"pre_tool_use,omitempty" yaml:"pre_tool_use,omitempty"`
// PostToolUse hooks run after tool execution
PostToolUse []MatcherConfig `json:"post_tool_use,omitempty" yaml:"post_tool_use,omitempty"`
// SessionStart hooks run when a session begins
SessionStart []Hook `json:"session_start,omitempty" yaml:"session_start,omitempty"`
// SessionEnd hooks run when a session ends
SessionEnd []Hook `json:"session_end,omitempty" yaml:"session_end,omitempty"`
}
Config represents the hooks configuration for an agent
func FromConfig ¶
func FromConfig(cfg *latest.HooksConfig) *Config
FromConfig converts a latest.HooksConfig to a hooks.Config
type EventType ¶
type EventType string
EventType represents the type of hook event
const ( // PreToolUse is triggered before a tool call executes. // Can allow/deny/modify tool calls; can block with feedback. EventPreToolUse EventType = "pre_tool_use" // PostToolUse is triggered after a tool completes successfully. // Can provide validation, feedback, or additional processing. EventPostToolUse EventType = "post_tool_use" // SessionStart is triggered when a session begins or resumes. // Can load context, setup environment, install dependencies. EventSessionStart EventType = "session_start" // SessionEnd is triggered when a session terminates. // Can perform cleanup, logging, persist session state. EventSessionEnd EventType = "session_end" )
type Executor ¶
type Executor struct {
// contains filtered or unexported fields
}
Executor handles the execution of hooks
func NewExecutor ¶
NewExecutor creates a new hook executor
func (*Executor) ExecutePostToolUse ¶
ExecutePostToolUse runs post-tool-use hooks for a tool
func (*Executor) ExecutePreToolUse ¶
ExecutePreToolUse runs pre-tool-use hooks for a tool
func (*Executor) ExecuteSessionEnd ¶
ExecuteSessionEnd runs session end hooks
func (*Executor) ExecuteSessionStart ¶
ExecuteSessionStart runs session start hooks
func (*Executor) HasPostToolUseHooks ¶
HasPostToolUseHooks returns true if there are any post-tool-use hooks configured
func (*Executor) HasPreToolUseHooks ¶
HasPreToolUseHooks returns true if there are any pre-tool-use hooks configured
func (*Executor) HasSessionEndHooks ¶
HasSessionEndHooks returns true if there are any session end hooks configured
func (*Executor) HasSessionStartHooks ¶
HasSessionStartHooks returns true if there are any session start hooks configured
type Hook ¶
type Hook struct {
// Type specifies whether this is a command or prompt hook
Type HookType `json:"type" yaml:"type"`
// Command is the shell command to execute (for command hooks)
Command string `json:"command,omitempty" yaml:"command,omitempty"`
// Timeout is the execution timeout in seconds (default: 60)
Timeout int `json:"timeout,omitempty" yaml:"timeout,omitempty"`
}
Hook represents a single hook configuration
func (*Hook) GetTimeout ¶
GetTimeout returns the timeout duration, defaulting to 60 seconds
type HookSpecificOutput ¶
type HookSpecificOutput struct {
// HookEventName identifies which event this output is for
HookEventName EventType `json:"hook_event_name,omitempty"`
// PreToolUse fields
PermissionDecision Decision `json:"permission_decision,omitempty"`
PermissionDecisionReason string `json:"permission_decision_reason,omitempty"`
UpdatedInput map[string]any `json:"updated_input,omitempty"`
// PostToolUse/SessionStart fields
AdditionalContext string `json:"additional_context,omitempty"`
}
HookSpecificOutput contains event-specific output fields
type HookType ¶
type HookType string
HookType represents the type of hook action
const ( // HookTypeCommand executes a shell command HookTypeCommand HookType = "command" )
type Input ¶
type Input struct {
// Common fields for all hooks
SessionID string `json:"session_id"`
Cwd string `json:"cwd"`
HookEventName EventType `json:"hook_event_name"`
// Tool-related fields (for PreToolUse and PostToolUse)
ToolName string `json:"tool_name,omitempty"`
ToolUseID string `json:"tool_use_id,omitempty"`
ToolInput map[string]any `json:"tool_input,omitempty"`
// PostToolUse specific
ToolResponse any `json:"tool_response,omitempty"`
// SessionStart specific
Source string `json:"source,omitempty"` // "startup", "resume", "clear", "compact"
// SessionEnd specific
Reason string `json:"reason,omitempty"` // "clear", "logout", "prompt_input_exit", "other"
}
Input represents the JSON input passed to hooks via stdin
type MatcherConfig ¶
type MatcherConfig struct {
// Matcher is a regex pattern to match tool names (e.g., "shell|edit_file")
// Use "*" to match all tools
Matcher string `json:"matcher,omitempty" yaml:"matcher,omitempty"`
// Hooks are the hooks to execute when the matcher matches
Hooks []Hook `json:"hooks" yaml:"hooks"`
}
MatcherConfig represents a hook matcher with its hooks
type Output ¶
type Output struct {
// Continue indicates whether to continue execution (default: true)
Continue *bool `json:"continue,omitempty"`
// StopReason is the message to show when continue=false
StopReason string `json:"stop_reason,omitempty"`
// SuppressOutput hides stdout from transcript
SuppressOutput bool `json:"suppress_output,omitempty"`
// SystemMessage is a warning to show the user
SystemMessage string `json:"system_message,omitempty"`
// Decision is for blocking operations
Decision string `json:"decision,omitempty"`
// Reason is the message explaining the decision
Reason string `json:"reason,omitempty"`
// HookSpecificOutput contains event-specific fields
HookSpecificOutput *HookSpecificOutput `json:"hook_specific_output,omitempty"`
}
Output represents the JSON output from a hook
func (*Output) ShouldContinue ¶
ShouldContinue returns whether execution should continue
type Result ¶
type Result struct {
// Allowed indicates if the operation should proceed
Allowed bool
// Message is feedback to include in the response
Message string
// ModifiedInput contains any modifications to tool input (PreToolUse only)
ModifiedInput map[string]any
// AdditionalContext is context to add (PostToolUse/SessionStart)
AdditionalContext string
// SystemMessage is a warning to show the user
SystemMessage string
// ExitCode is the exit code from the hook command (0 = success, 2 = blocking error)
ExitCode int
// Stderr contains any error output from the hook
Stderr string
}
Result represents the result of executing hooks