hooks

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 15, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SessionStart    = "session.start"
	SessionEnd      = "session.end"
	TurnStart       = "turn.start"
	TurnEnd         = "turn.end"
	ToolCallStart   = "tool_call.start"
	ToolCallEnd     = "tool_call.end"
	ToolCallError   = "tool_call.error"
	FileRead        = "file.read"
	FileWrite       = "file.write"
	FileEdit        = "file.edit"
	FileDelete      = "file.delete"
	CompactionStart = "compaction.start"
	CompactionEnd   = "compaction.end"
	BudgetWarning   = "budget.warning"
	BudgetExceeded  = "budget.exceeded"
	ErrorOccurred   = "error.occurred"
	ErrorRecovered  = "error.recovered"
	ModelSwitch     = "model.switch"
	ProviderSwitch  = "provider.switch"
	UserInput       = "user.input"
	AgentResponse   = "agent.response"
)

LifecycleEventType constants representing agent lifecycle events.

Variables

This section is empty.

Functions

func Execute

func Execute(ctx context.Context, event EventType, data map[string]interface{}) error

Execute runs all hooks for an event on the global registry.

func ExecuteAsync

func ExecuteAsync(ctx context.Context, event EventType, data map[string]interface{})

ExecuteAsync runs hooks asynchronously on the global registry.

func FormatEvent added in v0.2.0

func FormatEvent(event Event) string

FormatEvent returns a human-readable log line for the event.

func LoadHooksDir

func LoadHooksDir(dir string) error

LoadHooksDir loads hooks from a directory.

func Register

func Register(h Hook)

Register adds a hook to the global registry.

func RegisterDecisionHook

func RegisterDecisionHook(fn DecisionHookFn)

RegisterDecisionHook adds a decision hook to the global list.

func ResetDecisionHooks

func ResetDecisionHooks()

ResetDecisionHooks clears all registered decision hooks. Intended for testing.

Types

type DecisionHookFn

type DecisionHookFn func(event string, data map[string]interface{}) *HookDecision

DecisionHookFn is a function that inspects an event and optionally returns a decision. Returning nil means "no opinion" (proceed normally).

type Event added in v0.2.0

type Event struct {
	Name      string
	Timestamp time.Time
	Data      map[string]interface{}
	Source    string
}

Event represents a single lifecycle event emitted by the agent.

type EventBus added in v0.2.0

type EventBus struct {
	Hooks      map[string][]*LifecycleHook
	Listeners  map[string][]chan Event
	History    []Event
	MaxHistory int
	// contains filtered or unexported fields
}

EventBus is the central publish/subscribe mechanism for lifecycle events.

func NewEventBus added in v0.2.0

func NewEventBus() *EventBus

NewEventBus creates a new EventBus with sensible defaults.

func (*EventBus) Emit added in v0.2.0

func (eb *EventBus) Emit(event Event)

Emit fires all hooks for the event type and sends to listeners. Synchronous hooks run in priority order; async hooks run in goroutines.

func (*EventBus) GetHistory added in v0.2.0

func (eb *EventBus) GetHistory(eventType string, limit int) []Event

GetHistory returns the most recent events of the given type, limited to `limit`. If eventType is empty, all events are considered.

func (*EventBus) OnError added in v0.2.0

func (eb *EventBus) OnError(fn func(err error))

OnError registers a convenience hook that fires on ErrorOccurred events.

func (*EventBus) OnFileWrite added in v0.2.0

func (eb *EventBus) OnFileWrite(fn func(path string))

OnFileWrite registers a convenience hook that fires on FileWrite events.

func (*EventBus) OnSessionEnd added in v0.2.0

func (eb *EventBus) OnSessionEnd(fn func(duration time.Duration, tokens int))

OnSessionEnd registers a convenience hook that fires when a session ends.

func (*EventBus) OnToolCall added in v0.2.0

func (eb *EventBus) OnToolCall(fn func(tool string, duration time.Duration))

OnToolCall registers a convenience hook that fires when a tool call completes.

func (*EventBus) Register added in v0.2.0

func (eb *EventBus) Register(hook *LifecycleHook)

Register adds a hook to the bus for its configured event type.

func (*EventBus) Stats added in v0.2.0

func (eb *EventBus) Stats() EventStats

Stats returns aggregate statistics about the event bus.

func (*EventBus) Subscribe added in v0.2.0

func (eb *EventBus) Subscribe(eventType string) <-chan Event

Subscribe returns a channel that receives events of the given type.

func (*EventBus) Unregister added in v0.2.0

func (eb *EventBus) Unregister(hookID string)

Unregister removes a hook by its ID from all event types.

func (*EventBus) Unsubscribe added in v0.2.0

func (eb *EventBus) Unsubscribe(eventType string, ch <-chan Event)

Unsubscribe removes a previously subscribed channel.

type EventStats added in v0.2.0

type EventStats struct {
	TotalEvents int
	ByType      map[string]int
	HookCount   int
	AsyncHooks  int
	AvgHookTime time.Duration
}

EventStats provides aggregate statistics about the event bus.

type EventType

type EventType string

EventType represents a hook event.

const (
	EventPreQuery      EventType = "pre_query"
	EventPostQuery     EventType = "post_query"
	EventPreTool       EventType = "pre_tool"
	EventPostTool      EventType = "post_tool"
	EventPreCompact    EventType = "pre_compact"
	EventPostCompact   EventType = "post_compact"
	EventFileChanged   EventType = "file_changed"
	EventSessionStart  EventType = "session_start"
	EventSessionEnd    EventType = "session_end"
	EventPermissionAsk EventType = "permission_ask"
	EventError         EventType = "error"
)

type Hook

type Hook struct {
	Name     string
	Event    EventType
	Priority int // lower = earlier
	Fn       func(ctx context.Context, data map[string]interface{}) error
}

Hook is a registered hook function.

func BuiltinHooks

func BuiltinHooks() []Hook

BuiltinHooks returns the default set of built-in hooks.

type HookDecision

type HookDecision struct {
	Action        string          `json:"action"` // "allow", "deny", "modify"
	Reason        string          `json:"reason,omitempty"`
	ModifiedInput json.RawMessage `json:"modified_input,omitempty"`
}

HookDecision represents the outcome of a decision hook.

func ExecuteDecisionHooks

func ExecuteDecisionHooks(event string, data map[string]interface{}) *HookDecision

ExecuteDecisionHooks runs all registered decision hooks for the given event. It returns the first non-nil decision. If all hooks return nil, the result is nil (meaning no opinion, proceed normally).

type LifecycleHook added in v0.2.0

type LifecycleHook struct {
	ID       string
	Name     string
	Event    string
	Handler  func(Event) error
	Priority int
	Async    bool
	Enabled  bool
}

LifecycleHook is a registered handler for lifecycle events on the EventBus.

type Registry

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

Registry stores and executes hooks.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new hook registry.

func (*Registry) Execute

func (r *Registry) Execute(ctx context.Context, event EventType, data map[string]interface{}) error

Execute runs all hooks for an event.

func (*Registry) ExecuteAsync

func (r *Registry) ExecuteAsync(ctx context.Context, event EventType, data map[string]interface{})

ExecuteAsync runs hooks asynchronously (fire and forget).

func (*Registry) Register

func (r *Registry) Register(h Hook)

Register adds a hook to the registry.

Jump to

Keyboard shortcuts

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