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
- Variables
- type Config
- type Diagnostics
- type Event
- type Hook
- type Listing
- type ProjectHooks
- type Runner
- func (r *Runner) List(workingDir string) Listing
- func (r *Runner) Notify(ctx context.Context, scope Scope, kind, message string)
- func (r *Runner) PostToolUse(ctx context.Context, scope Scope, call ToolCall, response ToolResponse) string
- func (r *Runner) PreCompact(ctx context.Context, scope Scope, trigger string)
- func (r *Runner) PreToolUse(ctx context.Context, scope Scope, call ToolCall) ToolDecision
- func (r *Runner) SessionStart(ctx context.Context, scope Scope, source string) string
- func (r *Runner) Stop(ctx context.Context, scope Scope, event Event, active bool, reply string) string
- func (r *Runner) Trust(workingDir, sum string) (Listing, error)
- type Scope
- type ToolCall
- type ToolDecision
- type ToolResponse
Constants ¶
const ( Allow = "allow" Deny = "deny" Ask = "ask" )
Permission decisions of PreToolUse hooks.
const DefaultTimeout = 600 * time.Second
DefaultTimeout bounds a hook whose timeout is 0.
const ProjectFile = ".matrixclaw/hooks.json"
ProjectFile is where a project keeps its hooks, relative to its directory.
Variables ¶
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") )
var Events = []Event{PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, PreCompact, Notification}
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 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.
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) Notify ¶
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 ¶
PreCompact runs the PreCompact hooks of trigger ("auto" or "manual") before a summary; they only observe.
func (*Runner) PreToolUse ¶
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 ¶
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.
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 ¶
ToolResponse is a finished call's result as a PostToolUse hook reads it.