hooks

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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"
	PreCompact         EventType = "PreCompact"
	PostCompact        EventType = "PostCompact"
	UserPromptSubmit   EventType = "UserPromptSubmit"
)

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
	Reason       string          `json:"reason,omitempty"`        // Pre/PostCompact
	TokensBefore int             `json:"tokens_before,omitempty"` // Pre/PostCompact
	TokensAfter  int             `json:"tokens_after,omitempty"`  // PostCompact
}

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

type Result

type Result struct {
	Blocked bool   `json:"blocked"`
	Reason  string `json:"reason,omitempty"`
}

Result is the expected JSON structure from a blocking hook's stdout.

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 runs PreToolUse and PostToolUse hooks around each tool execution.

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) RunPostCompact added in v0.1.0

func (r *Runner) RunPostCompact(ctx context.Context, reason string, tokensBefore, tokensAfter int)

RunPostCompact fires PostCompact hooks asynchronously.

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 combined stderr/error output of the first failing hook, or "" on success.

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) RunPreCompact added in v0.1.0

func (r *Runner) RunPreCompact(ctx context.Context, reason string, tokensBefore int)

RunPreCompact fires PreCompact hooks asynchronously.

func (*Runner) RunPreToolUse

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

RunPreToolUse executes all matching PreToolUse hooks. A blocking hook that exits non-zero or returns {"blocked":true} returns an error.

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) RunTaskCompleted added in v0.1.0

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

RunTaskCompleted executes TaskCompleted hooks synchronously. Any hook error 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 hook error 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) error

RunUserPromptSubmit executes UserPromptSubmit hooks synchronously. A blocking hook that returns {"blocked":true} rejects the user's input.

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