hooks

package
v0.5.7 Latest Latest
Warning

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

Go to latest
Published: May 3, 2026 License: Apache-2.0 Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterBuiltInActions

func RegisterBuiltInActions(m *HookManager)

RegisterBuiltInActions registers the default action handlers.

Types

type ActionHandler

type ActionHandler func(hook *Hook, ctx *EventContext) error

ActionHandler is a function that executes a hook action.

type EventBus

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

EventBus manages event distribution to subscribers.

func NewEventBus

func NewEventBus() *EventBus

NewEventBus creates a new event bus.

func (*EventBus) Publish

func (b *EventBus) Publish(ctx *EventContext)

Publish distributes an event to all subscribers synchronously.

func (*EventBus) PublishAsync

func (b *EventBus) PublishAsync(ctx *EventContext)

PublishAsync distributes an event asynchronously via the queue.

func (*EventBus) Shutdown

func (b *EventBus) Shutdown()

Shutdown stops the event bus processing.

func (*EventBus) Subscribe

func (b *EventBus) Subscribe(event HookEvent, callback func(*EventContext)) *Subscription

Subscribe registers a callback for a specific event type.

func (*EventBus) SubscribeWithFilter

func (b *EventBus) SubscribeWithFilter(event HookEvent, callback func(*EventContext), filter func(*EventContext) bool) *Subscription

SubscribeWithFilter registers a callback with an optional filter function.

type EventContext

type EventContext struct {
	Event        HookEvent              `json:"event"`
	Timestamp    time.Time              `json:"timestamp"`
	Data         map[string]interface{} `json:"data"`
	Request      any                    `json:"request,omitempty"` // interface{} to avoid import cycle
	Provider     string                 `json:"provider,omitempty"`
	Model        string                 `json:"model,omitempty"`
	Error        error                  `json:"-"`
	ErrorMessage string                 `json:"error,omitempty"`
}

EventContext provides the environment for hook execution.

type Hook

type Hook struct {
	ID          string         `yaml:"id" json:"id"`
	Name        string         `yaml:"name" json:"name"`
	Description string         `yaml:"description" json:"description"`
	Event       HookEvent      `yaml:"event" json:"event"`
	Condition   string         `yaml:"condition" json:"condition"`
	Action      HookAction     `yaml:"action" json:"action"`
	Params      map[string]any `yaml:"params" json:"params"`
	Enabled     bool           `yaml:"enabled" json:"enabled"`

	// FilePath is the source file (not in YAML)
	FilePath string `yaml:"-" json:"-"`
}

Hook represents a single automation rule.

type HookAction

type HookAction string

HookAction defines the action to be performed when a hook is triggered.

const (
	ActionRetryWithFallback HookAction = "retry_with_fallback"
	ActionNotifyWebhook     HookAction = "notify_webhook"
	ActionPreferProvider    HookAction = "prefer_provider"
	ActionLogWarning        HookAction = "log_warning"
	ActionRotateCredential  HookAction = "rotate_credential"
	ActionRestartProvider   HookAction = "restart_provider"
	ActionRunCommand        HookAction = "run_command"
)

type HookEvent

type HookEvent string

HookEvent defines the type of event that can trigger a hook.

const (
	EventRequestReceived     HookEvent = "request_received"
	EventRequestFailed       HookEvent = "request_failed"
	EventProviderUnavailable HookEvent = "provider_unavailable"
	EventQuotaWarning        HookEvent = "quota_warning"
	EventQuotaExceeded       HookEvent = "quota_exceeded"
	EventModelDiscovered     HookEvent = "model_discovered"
	EventHealthCheckFailed   HookEvent = "health_check_failed"
	EventRoutingDecision     HookEvent = "routing_decision"
)

type HookManager

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

HookManager manages the lifecycle and execution of automation hooks.

func NewHookManager

func NewHookManager(hooksDir string, eventBus *EventBus) (*HookManager, error)

NewHookManager creates a new hook manager.

func (*HookManager) EvaluateCondition

func (m *HookManager) EvaluateCondition(h *Hook, ctx *EventContext) (bool, error)

EvaluateCondition exposes condition evaluation for testing.

func (*HookManager) GetHook

func (m *HookManager) GetHook(id string) *Hook

GetHook returns a hook by ID.

func (*HookManager) GetHooks

func (m *HookManager) GetHooks() []*Hook

GetHooks returns all loaded hooks flattened.

func (*HookManager) GetHooksDir

func (m *HookManager) GetHooksDir() string

GetHooksDir returns the hooks directory path.

func (*HookManager) LoadHooks

func (m *HookManager) LoadHooks() error

LoadHooks loads all hooks from the hooks directory.

func (*HookManager) RegisterAction

func (m *HookManager) RegisterAction(action HookAction, handler ActionHandler)

RegisterAction registers a handler for a specific action type.

func (*HookManager) StartWatcher

func (m *HookManager) StartWatcher() error

StartWatcher starts a background fsnotify watcher for hot-reloading hooks.

func (*HookManager) StopWatcher

func (m *HookManager) StopWatcher()

StopWatcher stops the file watcher.

func (*HookManager) SubscribeToAllEvents

func (m *HookManager) SubscribeToAllEvents()

subscribeToEvents ensures the manager is subscribed to all relevant events. This is a bit tricky with reloading. Alternative: Subscribe to ALL known events unconditionally at startup.

type Subscription

type Subscription struct {
	ID          string
	Event       HookEvent
	Callback    func(*EventContext)
	Filter      func(*EventContext) bool
	Unsubscribe func()
}

Subscription is a handle for a registered subscriber.

type WebhookHandler

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

WebhookHandler manages webhook execution with rate limiting.

func NewWebhookHandler

func NewWebhookHandler() *WebhookHandler

func (*WebhookHandler) Handle

func (h *WebhookHandler) Handle(hook *Hook, ctx *EventContext) error

Jump to

Keyboard shortcuts

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