hooks

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AllHook

AllHook represents a strongly typed hook object; individual methods remain optional via the narrow interfaces above.

type Decision

type Decision int

Decision captures the permission outcome encoded in the hook exit code. Claude Code spec: 0=success(parse JSON), 2=blocking error(stderr), other=non-blocking(log stderr & continue).

const (
	DecisionAllow         Decision = iota // exit 0: success, parse JSON stdout
	DecisionBlockingError                 // exit 2: blocking error, stderr is message
	DecisionNonBlocking                   // exit 1,3+: non-blocking, log & continue
)

func (Decision) String

func (d Decision) String() string

type Executor

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

Executor executes hooks by spawning shell commands with JSON stdin payloads.

func NewExecutor

func NewExecutor(opts ...ExecutorOption) *Executor

NewExecutor constructs a shell-based hook executor.

func (*Executor) Close

func (e *Executor) Close()

Close is present for API parity; no resources are held.

func (*Executor) Execute

func (e *Executor) Execute(ctx context.Context, evt events.Event) ([]Result, error)

Execute runs all matching hooks for the provided event and returns their results.

func (*Executor) Publish

func (e *Executor) Publish(evt events.Event) error

Publish executes matching hooks for the event using a background context. It preserves the previous API while delegating to Execute.

func (*Executor) Register

func (e *Executor) Register(hooks ...ShellHook)

Register adds shell hooks to the executor. Hooks are matched by event type and selector.

type ExecutorOption

type ExecutorOption func(*Executor)

ExecutorOption configures optional behaviour.

func WithCommand

func WithCommand(cmd string) ExecutorOption

WithCommand defines the fallback shell command used when a hook omits Command.

func WithErrorHandler

func WithErrorHandler(fn func(events.EventType, error)) ExecutorOption

WithErrorHandler installs an async error sink. Errors are still returned to callers.

func WithMiddleware

func WithMiddleware(mw ...middleware.Middleware) ExecutorOption

WithMiddleware wraps execution with the provided middleware chain.

func WithTimeout

func WithTimeout(d time.Duration) ExecutorOption

WithTimeout sets the default timeout per hook run. Zero uses the default budget.

func WithWorkDir

func WithWorkDir(dir string) ExecutorOption

WithWorkDir sets the working directory for hook command execution.

type HookOutput

type HookOutput struct {
	Continue      *bool  `json:"continue,omitempty"`
	StopReason    string `json:"stopReason,omitempty"`
	Decision      string `json:"decision,omitempty"`
	Reason        string `json:"reason,omitempty"`
	SystemMessage string `json:"systemMessage,omitempty"`

	HookSpecificOutput *HookSpecificOutput `json:"hookSpecificOutput,omitempty"`
}

HookOutput is the structured JSON output from hooks on exit 0.

type HookSpecificOutput

type HookSpecificOutput struct {
	HookEventName string `json:"hookEventName,omitempty"`

	// PreToolUse specific
	PermissionDecision       string         `json:"permissionDecision,omitempty"`
	PermissionDecisionReason string         `json:"permissionDecisionReason,omitempty"`
	UpdatedInput             map[string]any `json:"updatedInput,omitempty"`
	AdditionalContext        string         `json:"additionalContext,omitempty"`
}

HookSpecificOutput carries event-specific fields from hook JSON output.

type NotificationHook

type NotificationHook interface {
	Notification(context.Context, events.NotificationPayload) error
}

Individual hook interfaces allow hook implementations to opt-in to only the callbacks they care about while keeping type safety.

type PostToolUseHook

type PostToolUseHook interface {
	PostToolUse(context.Context, events.ToolResultPayload) error
}

Individual hook interfaces allow hook implementations to opt-in to only the callbacks they care about while keeping type safety.

type PreToolUseHook

type PreToolUseHook interface {
	PreToolUse(context.Context, events.ToolUsePayload) error
}

Individual hook interfaces allow hook implementations to opt-in to only the callbacks they care about while keeping type safety.

type Result

type Result struct {
	Event    events.Event
	Decision Decision
	ExitCode int
	Output   *HookOutput // parsed JSON stdout on exit 0
	Stdout   string
	Stderr   string
}

Result captures the full outcome of executing a shell hook.

type Selector

type Selector struct {
	ToolName *regexp.Regexp
	Pattern  *regexp.Regexp
}

Selector filters hooks by matcher target and/or payload pattern.

func NewSelector

func NewSelector(toolPattern, payloadPattern string) (Selector, error)

NewSelector compiles optional regex patterns. Empty strings are treated as wildcards.

func (Selector) Match

func (s Selector) Match(evt events.Event) bool

Match returns true when the event satisfies all configured selectors.

type ShellHook

type ShellHook struct {
	Event         events.EventType
	Command       string
	Selector      Selector
	Timeout       time.Duration
	Env           map[string]string
	Name          string // optional label for debugging
	Async         bool   // fire-and-forget execution
	Once          bool   // execute only once per session
	StatusMessage string // status message shown during execution
}

ShellHook describes a single shell command bound to an event type.

type StopHook

type StopHook interface {
	Stop(context.Context, events.StopPayload) error
}

Individual hook interfaces allow hook implementations to opt-in to only the callbacks they care about while keeping type safety.

type UserPromptSubmitHook

type UserPromptSubmitHook interface {
	UserPromptSubmit(context.Context, events.UserPromptPayload) error
}

Individual hook interfaces allow hook implementations to opt-in to only the callbacks they care about while keeping type safety.

Jump to

Keyboard shortcuts

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