hooks

package
v1.18.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 7, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

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

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

func (*Config) IsEmpty

func (c *Config) IsEmpty() bool

IsEmpty returns true if no hooks are configured

type Decision

type Decision string

Decision represents a permission decision from a hook

const (
	// DecisionAllow allows the operation to proceed
	DecisionAllow Decision = "allow"

	// DecisionDeny blocks the operation
	DecisionDeny Decision = "deny"

	// DecisionAsk prompts the user for confirmation (PreToolUse only)
	DecisionAsk Decision = "ask"
)

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

func NewExecutor(config *Config, workingDir string, env []string) *Executor

NewExecutor creates a new hook executor

func (*Executor) ExecutePostToolUse

func (e *Executor) ExecutePostToolUse(ctx context.Context, input *Input) (*Result, error)

ExecutePostToolUse runs post-tool-use hooks for a tool

func (*Executor) ExecutePreToolUse

func (e *Executor) ExecutePreToolUse(ctx context.Context, input *Input) (*Result, error)

ExecutePreToolUse runs pre-tool-use hooks for a tool

func (*Executor) ExecuteSessionEnd

func (e *Executor) ExecuteSessionEnd(ctx context.Context, input *Input) (*Result, error)

ExecuteSessionEnd runs session end hooks

func (*Executor) ExecuteSessionStart

func (e *Executor) ExecuteSessionStart(ctx context.Context, input *Input) (*Result, error)

ExecuteSessionStart runs session start hooks

func (*Executor) HasPostToolUseHooks

func (e *Executor) HasPostToolUseHooks() bool

HasPostToolUseHooks returns true if there are any post-tool-use hooks configured

func (*Executor) HasPreToolUseHooks

func (e *Executor) HasPreToolUseHooks() bool

HasPreToolUseHooks returns true if there are any pre-tool-use hooks configured

func (*Executor) HasSessionEndHooks

func (e *Executor) HasSessionEndHooks() bool

HasSessionEndHooks returns true if there are any session end hooks configured

func (*Executor) HasSessionStartHooks

func (e *Executor) HasSessionStartHooks() bool

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

func (h *Hook) GetTimeout() time.Duration

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

func (*Input) ToJSON

func (i *Input) ToJSON() ([]byte, error)

ToJSON serializes the input to JSON

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) IsBlocked

func (o *Output) IsBlocked() bool

IsBlocked returns true if the decision is to block

func (*Output) ShouldContinue

func (o *Output) ShouldContinue() bool

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL