hooks

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 21 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package hooks runs user commands at the agent's seams (before and after tool calls, when a run would stop, at its start and before a summary) with the Claude Code hook protocol, and the built-in checks after file edits.

Index

Constants

View Source
const (
	Allow = "allow"
	Deny  = "deny"
	Ask   = "ask"
)

Permission decisions of PreToolUse hooks.

View Source
const DefaultTimeout = 600 * time.Second

DefaultTimeout bounds a hook whose timeout is 0.

View Source
const ProjectFile = ".matrixclaw/hooks.json"

ProjectFile is where a project keeps its hooks, relative to its directory.

Variables

View Source
var (
	ErrNoProjectHooks = errors.New("this session's project has no " + ProjectFile)
	ErrProjectChanged = errors.New("the project hooks file changed since it was listed; list it again")
)

Events is the closed list of events a hook may name.

Functions

This section is empty.

Types

type Config

type Config struct {
	Hooks       []Hook
	Diagnostics Diagnostics
}

Config is what the runner follows, read for every hook: setup.json's hooks and diagnostics.

type Diagnostics

type Diagnostics struct {
	Disabled bool              `json:"disabled,omitempty"`
	Commands map[string]string `json:"commands,omitempty"`
	LSP      lsp.Config        `json:"lsp,omitzero"`
}

Diagnostics configures the checks after edits: Disabled turns them all off; Commands replaces the check of a file extension (".go"), language server or built-in command, with a bash command run in the session's working directory, and an empty command turns that extension's check off; LSP configures the language servers.

func (Diagnostics) Validate

func (d Diagnostics) Validate() error

Validate reports extension keys that are not like ".go" and what makes the language server settings unusable.

type Event

type Event string

Event names when a hook runs.

const (
	PreToolUse   Event = "PreToolUse"
	PostToolUse  Event = "PostToolUse"
	Stop         Event = "Stop"
	SubagentStop Event = "SubagentStop"
	SessionStart Event = "SessionStart"
	PreCompact   Event = "PreCompact"
	Notification Event = "Notification"
)

type Hook

type Hook struct {
	Event   Event  `json:"event"`
	Matcher string `json:"matcher,omitempty"`
	Command string `json:"command"`
	Timeout int    `json:"timeout,omitempty"`
}

Hook is one configured command. Matcher selects the tool (tool events), the source (SessionStart), the trigger (PreCompact) or the notification type; empty or "*" matches all. Timeout is in seconds, 0 for DefaultTimeout.

func (Hook) Validate

func (h Hook) Validate() error

Validate reports what makes the hook unusable.

type Listing

type Listing struct {
	Hooks   []Hook        `json:"hooks"`
	Project *ProjectHooks `json:"project,omitempty"`
}

Listing is every hook a session meets: setup.json's and its project's.

type ProjectHooks

type ProjectHooks struct {
	Path    string `json:"path"`
	Dir     string `json:"dir"`
	SHA256  string `json:"sha256"`
	Trusted bool   `json:"trusted"`
	Hooks   []Hook `json:"hooks,omitempty"`
	Error   string `json:"error,omitempty"`
}

ProjectHooks is a project's hooks file as the owner sees it: hooks run only while Trusted, which holds while the file keeps the SHA256 the owner trusted.

type Runner

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

Runner runs setup.json's hooks, the hooks of trusted project files and the checks after edits: language servers, else bash commands with the environment of shell commands.

func NewRunner

func NewRunner(env func() []string, config func() Config, trustPath string, servers *lsp.Manager) *Runner

NewRunner returns a runner; env (nil for the daemon's) is the commands' environment, trustPath the file keeping trusted project hooks files and servers the language servers that check edits (nil for none).

func (*Runner) List

func (r *Runner) List(workingDir string) Listing

List is every hook a session in workingDir meets.

func (*Runner) Notify

func (r *Runner) Notify(ctx context.Context, scope Scope, kind, message string)

Notify runs the Notification hooks of kind ("permission_prompt") with message.

func (*Runner) PostToolUse

func (r *Runner) PostToolUse(ctx context.Context, scope Scope, call ToolCall, response ToolResponse) string

PostToolUse runs the PostToolUse hooks of a finished call, then the check after an edit, and returns what they tell the model ("" for nothing).

func (*Runner) PreCompact

func (r *Runner) PreCompact(ctx context.Context, scope Scope, trigger string)

PreCompact runs the PreCompact hooks of trigger ("auto" or "manual") before a summary; they only observe.

func (*Runner) PreToolUse

func (r *Runner) PreToolUse(ctx context.Context, scope Scope, call ToolCall) ToolDecision

PreToolUse runs the call's PreToolUse hooks in order, each seeing the input the ones before rewrote. Exit 2 or a deny ends the call at once; otherwise ask wins over allow.

func (*Runner) SessionStart

func (r *Runner) SessionStart(ctx context.Context, scope Scope, source string) string

SessionStart runs the SessionStart hooks of source ("startup" for a session's first run, "resume" for a later one) and returns the context they print.

func (*Runner) Stop

func (r *Runner) Stop(ctx context.Context, scope Scope, event Event, active bool, reply string) string

Stop runs the Stop (or SubagentStop) hooks of a run that would end and returns why it must go on, "" to let it end; active says a Stop hook already kept it going.

func (*Runner) Trust

func (r *Runner) Trust(workingDir, sum string) (Listing, error)

Trust trusts the project hooks file of workingDir while it has the SHA256 sum the owner was shown.

type Scope

type Scope struct {
	SessionID      string
	RunID          string
	WorkingDir     string
	PermissionMode string
}

Scope is the session a hook runs for.

type ToolCall

type ToolCall struct {
	ID    string
	Name  string
	Input json.RawMessage
	Files []string
}

ToolCall is the call a tool hook runs for; Files are the resolved paths a file tool targets (several for apply_patch).

type ToolDecision

type ToolDecision struct {
	Permission string
	Reason     string
	Input      json.RawMessage
	Context    string
}

ToolDecision is what the PreToolUse hooks of a call decided: Permission is Allow, Deny, Ask or "" for none, Input the arguments they rewrote the call to (nil keeps them), Context text for the model.

type ToolResponse

type ToolResponse struct {
	Content string `json:"content"`
	Status  string `json:"status"`
}

ToolResponse is a finished call's result as a PostToolUse hook reads it.

Jump to

Keyboard shortcuts

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