hooks

package
v0.0.0-beta Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package hooks provides user-configurable shell hooks that run before and after LLM tool calls.

Hooks are defined in .aura/config/hooks/**/*.yaml, matched by tool name regex, and executed via mvdan/sh. They receive JSON context on stdin and control behavior through exit codes and optional JSON stdout.

Exit code semantics:

  • 0: success — parse stdout for optional {"message": "...", "deny": true, "reason": "..."}
  • 2: block (pre) or feedback (post) — stderr is returned as the message
  • other: non-blocking — stderr (or stdout) is returned as feedback to the LLM

Index

Constants

View Source
const DefaultTimeout = 10 * time.Second

DefaultTimeout is the default hook execution timeout.

Variables

This section is empty.

Functions

func Order

func Order(hks config.Hooks) (pre, post []string, err error)

Order splits hooks by event and returns topologically sorted name lists. Useful for display without compiling regexes.

Types

type Entry

type Entry struct {
	Name    string         // hook name from config map key
	Matcher *regexp.Regexp // nil = match all tools
	Files   string         // glob pattern for filepath.Match against basenames
	Command string
	Timeout time.Duration
	Silent  bool // suppress all output and exit codes
}

Entry is a compiled hook ready to match and execute.

func (Entry) MatchedPaths

func (e Entry) MatchedPaths(filePaths []string) []string

MatchedPaths returns file paths that match the entry's files glob. If no files glob is set, returns all paths unchanged.

func (Entry) Matches

func (e Entry) Matches(toolName string) bool

Matches returns true if the entry's matcher regex matches the tool name (or matcher is nil = match all).

type Event

type Event struct {
	HookEvent string `json:"hook_event"`
	Tool      struct {
		Name   string `json:"name"`
		Input  any    `json:"input"`
		Output string `json:"output,omitempty"`
	} `json:"tool"`
	CWD       string   `json:"cwd"`
	FilePaths []string `json:"file_paths,omitempty"`
}

Event is the JSON payload piped to hook stdin.

type Result

type Result struct {
	Blocked bool
	Message string
}

Result is the outcome of running hooks for a single event.

type Runner

type Runner struct {
	Pre     []Entry
	Post    []Entry
	OnStart func(hookName string) // called before each matching hook executes; nil = no-op
}

Runner holds compiled pre and post hooks. Zero value is safe — all methods return immediately when no hooks are configured. This is a value type; no nil checks needed at call sites.

func New

func New(hks config.Hooks) (Runner, error)

New compiles hook entries from config and orders them by dependency DAG. Panics on invalid regex (fail-fast). Returns error on dependency cycles or missing deps.

func (Runner) RunPost

func (r Runner) RunPost(ctx context.Context, toolName string, toolInput any, toolOutput, cwd string) Result

RunPost runs matching post-hooks sequentially. Collects messages.

func (Runner) RunPre

func (r Runner) RunPre(ctx context.Context, toolName string, toolInput any, cwd string) Result

RunPre runs matching pre-hooks sequentially. Returns on first block.

Jump to

Keyboard shortcuts

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