hooks

package
v0.29.0 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidateHookConfig

func ValidateHookConfig(config *HookConfig) error

ValidateHookConfig validates the entire hook configuration

Types

type CommonInput

type CommonInput struct {
	SessionID      string    `json:"session_id"`      // Unique session identifier
	TranscriptPath string    `json:"transcript_path"` // Path to transcript file (if enabled)
	CWD            string    `json:"cwd"`             // Current working directory
	HookEventName  HookEvent `json:"hook_event_name"` // The hook event type
	Timestamp      int64     `json:"timestamp"`       // Unix timestamp when hook fired
	Model          string    `json:"model"`           // AI model being used
	Interactive    bool      `json:"interactive"`     // Whether in interactive mode
}

CommonInput contains fields common to all hook inputs

type Executor

type Executor struct {
	// contains filtered or unexported fields
}

Executor handles hook execution

func NewExecutor

func NewExecutor(config *HookConfig, sessionID, transcriptPath string) *Executor

NewExecutor creates a new hook executor

func (*Executor) ExecuteHooks

func (e *Executor) ExecuteHooks(ctx context.Context, event HookEvent, input interface{}) (*HookOutput, error)

ExecuteHooks runs all matching hooks for an event

func (*Executor) PopulateCommonFields

func (e *Executor) PopulateCommonFields(event HookEvent) CommonInput

PopulateCommonFields fills in the common fields for any hook input

func (*Executor) SetInteractive

func (e *Executor) SetInteractive(interactive bool)

SetInteractive sets whether we're in interactive mode

func (*Executor) SetModel

func (e *Executor) SetModel(model string)

SetModel sets the model name for hook context

type HookConfig

type HookConfig struct {
	Hooks map[HookEvent][]HookMatcher `yaml:"hooks" json:"hooks"`
}

HookConfig represents the complete hooks configuration

func LoadHooksConfig

func LoadHooksConfig(customPaths ...string) (*HookConfig, error)

LoadHooksConfig loads and merges hook configurations from multiple sources

type HookEntry

type HookEntry struct {
	Type    string `yaml:"type" json:"type"`
	Command string `yaml:"command" json:"command"`
	Timeout int    `yaml:"timeout,omitempty" json:"timeout,omitempty"`
}

HookEntry defines a single hook command

type HookEvent

type HookEvent string

HookEvent represents a point in MCPHost's lifecycle where hooks can be executed

const (
	// PreToolUse fires before any tool execution
	PreToolUse HookEvent = "PreToolUse"

	// PostToolUse fires after tool execution completes
	PostToolUse HookEvent = "PostToolUse"

	// UserPromptSubmit fires when user submits a prompt
	UserPromptSubmit HookEvent = "UserPromptSubmit"

	// Stop fires when the main agent finishes responding
	Stop HookEvent = "Stop"
)

func (HookEvent) IsValid

func (e HookEvent) IsValid() bool

IsValid returns true if the event is a valid hook event

func (HookEvent) RequiresMatcher

func (e HookEvent) RequiresMatcher() bool

RequiresMatcher returns true if the event uses tool matchers

type HookMatcher

type HookMatcher struct {
	Matcher string      `yaml:"matcher,omitempty" json:"matcher,omitempty"`
	Merge   string      `yaml:"_merge,omitempty" json:"_merge,omitempty"`
	Hooks   []HookEntry `yaml:"hooks" json:"hooks"`
}

HookMatcher matches specific tools and defines hooks to execute

type HookOutput

type HookOutput struct {
	Continue       *bool  `json:"continue,omitempty"`
	StopReason     string `json:"stopReason,omitempty"`
	SuppressOutput bool   `json:"suppressOutput,omitempty"`
	Decision       string `json:"decision,omitempty"` // "approve", "block", or ""
	Reason         string `json:"reason,omitempty"`
}

HookOutput represents the JSON output from a hook

type PostToolUseInput

type PostToolUseInput struct {
	CommonInput
	ToolName     string          `json:"tool_name"`
	ToolInput    json.RawMessage `json:"tool_input"`
	ToolResponse json.RawMessage `json:"tool_response"`
}

PostToolUseInput is passed to PostToolUse hooks

type PreToolUseInput

type PreToolUseInput struct {
	CommonInput
	ToolName  string          `json:"tool_name"`
	ToolInput json.RawMessage `json:"tool_input"`
}

PreToolUseInput is passed to PreToolUse hooks

type StopInput

type StopInput struct {
	CommonInput
	StopHookActive bool            `json:"stop_hook_active"`
	Response       string          `json:"response"`       // The agent's final response
	StopReason     string          `json:"stop_reason"`    // "completed", "cancelled", "error"
	Meta           json.RawMessage `json:"meta,omitempty"` // Additional metadata (e.g., token usage, model info)
}

StopInput is passed to Stop hooks

type UserPromptSubmitInput

type UserPromptSubmitInput struct {
	CommonInput
	Prompt string `json:"prompt"`
}

UserPromptSubmitInput is passed to UserPromptSubmit hooks

Jump to

Keyboard shortcuts

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