hooks

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decision added in v0.2.0

type Decision struct {
	AdditionalContext string
	UpdatedInput      json.RawMessage
}

Decision is the normalized, allowed-with-extras result handed back to callers. A blocking hook is surfaced as an error by the Run* methods, so Decision only carries the payload that applies when the run proceeds.

type EventType

type EventType string

EventType identifies when a hook fires.

const (
	PreToolUse         EventType = "PreToolUse"
	PostToolUse        EventType = "PostToolUse"
	Notification       EventType = "Notification"
	PostStopValidation EventType = "PostStopValidation"
	TaskCreated        EventType = "TaskCreated"
	TaskCompleted      EventType = "TaskCompleted"
	SessionStart       EventType = "SessionStart"
	SessionEnd         EventType = "SessionEnd"
	UserPromptSubmit   EventType = "UserPromptSubmit"
	SubagentStop       EventType = "SubagentStop"
)

type Payload

type Payload struct {
	Event        EventType       `json:"event"`
	Tool         string          `json:"tool,omitempty"`
	Args         json.RawMessage `json:"args,omitempty"`
	Output       json.RawMessage `json:"output,omitempty"`
	IsError      bool            `json:"is_error,omitempty"`
	Message      string          `json:"message,omitempty"`
	Task         *TaskSnapshot   `json:"task,omitempty"`
	PreviousTask *TaskSnapshot   `json:"previous_task,omitempty"`
	StatusFrom   string          `json:"status_from,omitempty"`
	StatusTo     string          `json:"status_to,omitempty"`
	Prompt       string          `json:"prompt,omitempty"` // UserPromptSubmit
	Agent        string          `json:"agent,omitempty"`  // SubagentStop: teammate name
}

Payload is the JSON written to the hook command's stdin.

type Runner

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

Runner manages and executes hooks.

func New

func New(cfg config.HooksConfig, sessionID string, engine *approval.Engine, model agentcore.ChatModel) *Runner

New parses a HooksConfig and returns a Runner. Returns nil if no valid hooks are found. The model parameter is used for prompt-type hooks and may be nil.

func (*Runner) Middleware

func (r *Runner) Middleware() agentcore.ToolMiddleware

Middleware returns a ToolMiddleware that fires PostToolUse hooks after each tool execution. PreToolUse runs earlier, inside the tool gate (WrapGate), so permission decisions see hook-rewritten arguments; by the time this middleware runs, call.Args already carries the approved final form.

func (*Runner) RunNotification

func (r *Runner) RunNotification(_ context.Context, message string)

RunNotification fires matching Notification hooks asynchronously. Uses a detached context so hooks survive parent cancellation.

func (*Runner) RunPostStopValidation added in v0.1.0

func (r *Runner) RunPostStopValidation(ctx context.Context) (failOutput string)

RunPostStopValidation executes matching PostStopValidation hooks synchronously. Returns the output of the first failing hook (non-zero exit, exit-2 block, or approval denial), or "" when every validation passes.

func (*Runner) RunPostToolUse

func (r *Runner) RunPostToolUse(_ context.Context, toolName string, args, output json.RawMessage, isError bool)

RunPostToolUse fires matching PostToolUse hooks asynchronously. Uses a detached context so hooks survive parent cancellation.

func (*Runner) RunPreToolUse

func (r *Runner) RunPreToolUse(ctx context.Context, toolName string, args json.RawMessage) (Decision, error)

RunPreToolUse evaluates PreToolUse hooks. A blocking hook that signals a block returns an error; otherwise the returned Decision may carry an updated tool input or additional context.

func (*Runner) RunSessionEnd added in v0.1.0

func (r *Runner) RunSessionEnd(ctx context.Context)

RunSessionEnd fires SessionEnd hooks asynchronously.

func (*Runner) RunSessionStart added in v0.1.0

func (r *Runner) RunSessionStart(ctx context.Context)

RunSessionStart fires SessionStart hooks asynchronously.

func (*Runner) RunSubagentStop added in v0.2.0

func (r *Runner) RunSubagentStop(_ context.Context, agentName string)

RunSubagentStop fires SubagentStop hooks asynchronously when a teammate's agent loop exits. The hook matcher is tested against the teammate name (an empty matcher fires for every teammate). Observation only — by the time it runs the teammate has already exited, so it cannot keep it alive.

func (*Runner) RunTaskCompleted added in v0.1.0

func (r *Runner) RunTaskCompleted(ctx context.Context, previous, current TaskSnapshot) error

RunTaskCompleted executes TaskCompleted hooks synchronously. Any blocking hook aborts the completion transition.

func (*Runner) RunTaskCreated added in v0.1.0

func (r *Runner) RunTaskCreated(ctx context.Context, task TaskSnapshot) error

RunTaskCreated executes TaskCreated hooks synchronously. Any blocking hook aborts the task creation so callers can roll back.

func (*Runner) RunUserPromptSubmit added in v0.1.0

func (r *Runner) RunUserPromptSubmit(ctx context.Context, prompt string) (Decision, error)

RunUserPromptSubmit evaluates UserPromptSubmit hooks. A blocking hook rejects the prompt with an error; otherwise the returned Decision may carry additional context to prepend to the turn.

func (*Runner) WrapGate added in v0.3.0

func (r *Runner) WrapGate(next agentcore.ToolGate) agentcore.ToolGate

WrapGate returns a ToolGate that runs PreToolUse hooks before delegating to next (the permission gate). Ordering matches Claude Code: hooks fire first — a blocking hook denies the call, an updated_input rewrite is applied to the request — and the permission decision is then made on the FINAL arguments. The rewrite is surfaced to the kernel via GateDecision.UpdatedArgs so the tool executes exactly what was approved.

type TaskSnapshot added in v0.1.0

type TaskSnapshot struct {
	ID          string         `json:"id"`
	Subject     string         `json:"subject"`
	Description string         `json:"description,omitempty"`
	ActiveForm  string         `json:"active_form,omitempty"`
	Status      string         `json:"status"`
	Owner       string         `json:"owner,omitempty"`
	Blocks      []string       `json:"blocks,omitempty"`
	BlockedBy   []string       `json:"blocked_by,omitempty"`
	Metadata    map[string]any `json:"metadata,omitempty"`
}

TaskSnapshot is the task payload exposed to lifecycle hooks.

Jump to

Keyboard shortcuts

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