Documentation
¶
Overview ¶
Package hooks runs user hook commands on agent events. Hook definitions come from Scribe's config files and from Claude Code-compatible settings.json files, so existing Claude hooks work unchanged.
Trust: the user's own files (global Scribe config and ~/.claude/settings.json) always run. Anything inside a project (its .scribe/config.json and .claude/settings*.json) runs only when the project is trusted.
Index ¶
- Variables
- func LoadConfigHooks(root string) (global, project []config.Hook, err error)
- func Trust(root string) error
- func TrustPath() string
- func Trusted(root string) bool
- func Untrust(root string) error
- func WithToolUseID(ctx context.Context, id string) context.Context
- type EventCount
- type Options
- type Runner
- func (r *Runner) Count() int
- func (r *Runner) EventCounts() []EventCount
- func (r *Runner) Notification(ctx context.Context, msg string)
- func (r *Runner) PostCompact(ctx context.Context)
- func (r *Runner) PostTool(ctx context.Context, tool string, args json.RawMessage, res tools.Result) string
- func (r *Runner) PostToolFailure(ctx context.Context, tool string, args json.RawMessage, res tools.Result) string
- func (r *Runner) PreCompact(ctx context.Context)
- func (r *Runner) PreTool(ctx context.Context, tool string, args json.RawMessage) string
- func (r *Runner) PreToolUpdate(ctx context.Context, tool string, args json.RawMessage) (json.RawMessage, string)
- func (r *Runner) Prompt(ctx context.Context, prompt string) (extra, block string)
- func (r *Runner) SessionEnd(ctx context.Context)
- func (r *Runner) SessionStart(ctx context.Context)
- func (r *Runner) SetEventOff(event string, off bool)
- func (r *Runner) SetStatus(f func(string))
- func (r *Runner) Stop(ctx context.Context, stats agent.TurnStats)
- func (r *Runner) SubagentStart(ctx context.Context, role, task string)
- func (r *Runner) SubagentStop(ctx context.Context, role, task, errText string)
- func (r *Runner) Warnings() []string
Constants ¶
This section is empty.
Variables ¶
var Events = []string{
evPreTool, evPostTool, evPostToolFailure, evPrompt, evStop, evStopFailure,
evSessionStart, evSessionEnd, evPreCompact, evPostCompact,
evSubagentStart, evSubagentStop, evNotification,
}
Events lists every hook event the runner fires, in Claude Code's names. The settings files may name any event; these are the ones that run.
Functions ¶
func LoadConfigHooks ¶
LoadConfigHooks reads the hooks arrays of the global and project config files separately, so the caller can gate the project one on trust.
func Trust ¶
Trust records that the project at root may run its own hooks and MCP servers. Only call this after the user has said yes.
func TrustPath ¶
func TrustPath() string
TrustPath is where the list of trusted project roots is kept.
func Trusted ¶
Trusted reports whether the project at root may run its own hooks and MCP servers (the repo's .claude/settings.json, .scribe/config.json, .mcp.json). A missing or unreadable trust file means untrusted.
func WithToolUseID ¶
WithToolUseID returns ctx carrying the provider's id for the tool call about to run. The tool hooks put it in their stdin as tool_use_id, so a hook can tie its record to one call. The agent sets it before it calls the hooks; without it the field is an empty string.
Types ¶
type EventCount ¶
EventCount is one hook event in the configured hooks, with how many hooks it runs and whether it is switched off for this session.
type Options ¶
type Options struct {
Root string // project root; hooks run here
SessionID string
// ConfigGlobal and ConfigProject are the hooks arrays of the Scribe
// global and project config files (see LoadConfigHooks).
ConfigGlobal []config.Hook
ConfigProject []config.Hook
// UserSettings is a Claude settings.json (default ~/.claude/settings.json).
UserSettings string
// Trusted allows project-level hooks (project config, repo settings).
Trusted bool
}
Options configures a Runner. Use NewFromDisk for the normal case.
type Runner ¶
type Runner struct {
// contains filtered or unexported fields
}
Runner runs hooks. It implements agent.Hooks. Methods are safe for concurrent use.
func NewFromDisk ¶
NewFromDisk builds a Runner from the config files, ~/.claude/settings.json and the project's trust state.
func (*Runner) EventCounts ¶
func (r *Runner) EventCounts() []EventCount
EventCounts lists the events the configured hooks use, in first-seen order.
func (*Runner) Notification ¶
Notification runs Notification hooks with msg.
func (*Runner) PostCompact ¶
PostCompact runs PostCompact hooks once the history has been compacted.
func (*Runner) PostTool ¶
func (r *Runner) PostTool(ctx context.Context, tool string, args json.RawMessage, res tools.Result) string
PostTool implements agent.Hooks. Every matching hook runs; their extra text is joined. Blocks are not meaningful after a call and are ignored.
func (*Runner) PostToolFailure ¶
func (r *Runner) PostToolFailure(ctx context.Context, tool string, args json.RawMessage, res tools.Result) string
PostToolFailure runs the PostToolUseFailure hooks for a call that failed. PostTool calls it for failed calls that reached the tool; the agent calls it directly for a call refused before it ran (unknown tool, blocked by a hook, not available for this step), so every failure is reported once. Extra text is returned for the model, as PostTool's is.
func (*Runner) PreCompact ¶
PreCompact runs PreCompact hooks before the history is compacted.
func (*Runner) PreToolUpdate ¶
func (r *Runner) PreToolUpdate(ctx context.Context, tool string, args json.RawMessage) (json.RawMessage, string)
PreToolUpdate implements the agent's toolInputRewriter. It runs the same PreToolUse hooks as PreTool, in order, and a hook's hookSpecificOutput.updatedInput replaces the tool input for the hooks after it and for the call. The first block wins, and the input returned is the one the call runs with. An updatedInput that is not a JSON object is ignored with a warning, since a tool takes an object of named arguments.
func (*Runner) SessionEnd ¶
SessionEnd runs SessionEnd hooks.
func (*Runner) SessionStart ¶
SessionStart runs SessionStart hooks.
func (*Runner) SetEventOff ¶
SetEventOff switches every hook of one event off, or back on, for this runner only. Nothing is written to any config file.
func (*Runner) SetStatus ¶
SetStatus sets where a hook's statusMessage goes while the hook runs. The app shows it as a notice in the transcript. Nil turns it off.
func (*Runner) SubagentStart ¶
SubagentStart runs SubagentStart hooks as a subagent of role begins task.
func (*Runner) SubagentStop ¶
SubagentStop runs SubagentStop hooks when that subagent ends. errText is empty when it finished normally.