hook

package module
v0.0.0-...-7fbaaab Latest Latest
Warning

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

Go to latest
Published: May 2, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package hook provides a lightweight, generic event system for lifecycle hooks.

It allows registering handlers for arbitrary event types. Handlers run sequentially in registration order, with short-circuit on Abort and chained Modify results. Panicking handlers are recovered and converted to errors without disrupting subsequent handler dispatch.

The hook module is domain-agnostic — applications define their own event types by implementing the Event interface. For example, the agent module defines PreToolCall, PreLLMCall, TurnStart, etc.

Usage:

registry := hook.NewRegistry()
registry.On("my_event", func(ctx context.Context, e hook.Event) hook.Result {
    log.Printf("event: %s", e.Type())
    return hook.Continue()
})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action int

Action determines how the caller proceeds after a hook.

const (
	// ActionContinue lets execution proceed normally.
	ActionContinue Action = iota
	// ActionAbort stops execution with an optional reason.
	ActionAbort
	// ActionModify lets the handler modify data before proceeding.
	ActionModify
)

type Event

type Event interface {
	// Type returns the event type identifier.
	Type() EventType
}

Event is the interface for all hook events. Applications define concrete event types that implement this interface.

type EventType

type EventType string

EventType identifies the kind of hook event. Applications define their own EventType constants.

type Handler

type Handler func(ctx context.Context, event Event) Result

Handler processes a hook event and returns a result. The context carries deadlines, cancellation, and request-scoped values.

type Registry

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

Registry manages hook handlers and dispatches events. Handlers are executed sequentially in registration order. An Abort result short-circuits remaining handlers. Modify results chain — each handler sees the previous modification. Panicking handlers are recovered and converted to errors without disrupting dispatch to subsequent handlers.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates an empty hook registry.

func (*Registry) Clear

func (r *Registry) Clear(eventTypes ...EventType)

Clear removes all handlers for the given event type. If no event type is specified, clears all handlers.

func (*Registry) Emit

func (r *Registry) Emit(ctx context.Context, event Event) Result

Emit dispatches an event to all registered handlers for its type. Handlers run sequentially. First Abort short-circuits. Returns the merged result (last non-Continue result wins). Panicking handlers are recovered: the panic is converted to an error on the Result and dispatch continues to the next handler.

func (*Registry) HasHandlers

func (r *Registry) HasHandlers(eventType EventType) bool

HasHandlers returns true if any handlers are registered for the event type.

func (*Registry) On

func (r *Registry) On(eventType EventType, h Handler) func()

On registers a handler for the given event type. Returns an unsubscribe function that removes the handler.

type Result

type Result struct {
	// Action determines whether to continue, abort, or modify.
	Action Action
	// ModifiedData carries replacement data when Action is Modify.
	ModifiedData any
	// Reason explains why execution was aborted (Action == Abort).
	Reason string
	// Err reports an error from the handler without aborting dispatch.
	Err error
}

Result is returned by hook handlers to control execution flow.

func Abort

func Abort(reason string) Result

Abort returns a Result that stops execution.

func AbortWithError

func AbortWithError(reason string, err error) Result

AbortWithError returns a Result that stops execution with an error.

func Continue

func Continue() Result

Continue returns a Result that lets execution proceed.

func ContinueWithError

func ContinueWithError(err error) Result

ContinueWithError returns a Result that lets execution proceed but records an error.

func Modify

func Modify(data any) Result

Modify returns a Result that replaces event data.

Jump to

Keyboard shortcuts

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