hooks

package
v0.7.1 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	EventDaemonStarted    = "daemon.started"
	EventDaemonStopped    = "daemon.stopped"
	EventSessionStarted   = "session.started"
	EventSessionStopped   = "session.stopped"
	EventSessionUploaded  = "session.uploaded"
	EventSessionAvailable = "session.available"
	EventMurmurReceived   = "murmur.received"
	EventMurmurCritical   = "murmur.critical"
	EventSyncCompleted    = "sync.completed"
	EventSyncFailed       = "sync.failed"
	EventAgentRegistered  = "agent.registered"
	EventAgentIdle        = "agent.idle"
)

Event name constants. Dot-separated: category.action.

Variables

This section is empty.

Functions

func AgentIdlePayload

func AgentIdlePayload(id string, idleSince time.Time) map[string]any

AgentIdlePayload builds the payload for agent.idle events.

func AgentPayload

func AgentPayload(id, agentType, principal string) map[string]any

AgentPayload builds the payload for agent.registered events.

func AllEventNames

func AllEventNames() []string

AllEventNames returns all valid event names sorted alphabetically.

func HooksFilePath

func HooksFilePath() string

HooksFilePath returns the path to the hooks.yaml config file.

func MurmurPayload

func MurmurPayload(id, agentID, principal, topic, importance, content string) map[string]any

MurmurPayload builds the payload for murmur.received and murmur.critical events.

func SaveHook

func SaveHook(hook HookConfig) error

SaveHook appends a hook to the hooks.yaml file. Creates the file if it doesn't exist. Uses atomic write-via-rename to prevent corruption from crashes or concurrent writers.

func SessionAvailablePayload

func SessionAvailablePayload(name, url, principal, agentID string) map[string]any

SessionAvailablePayload builds the payload for session.available events.

func SessionPayload

func SessionPayload(name, agentID string) map[string]any

SessionPayload builds the payload for session.started events.

func SessionStoppedPayload

func SessionStoppedPayload(name, agentID string, dur time.Duration) map[string]any

SessionStoppedPayload builds the payload for session.stopped with duration.

func SessionUploadedPayload

func SessionUploadedPayload(name, url, agentID string, dur time.Duration) map[string]any

SessionUploadedPayload builds the payload for session.uploaded events.

func SyncFailedPayload

func SyncFailedPayload(workspace, syncType, errMsg string) map[string]any

SyncFailedPayload builds the payload for sync.failed events.

func SyncPayload

func SyncPayload(workspace, syncType string, dur time.Duration) map[string]any

SyncPayload builds the payload for sync.completed events.

func ValidEvent

func ValidEvent(name string) bool

ValidEvent returns true if name is a known event name.

Types

type Event

type Event struct {
	Name      string         `json:"event"`
	Timestamp time.Time      `json:"timestamp"`
	Project   string         `json:"project_root"`
	RepoID    string         `json:"repo_id"`
	Payload   map[string]any `json:"-"` // merged into top-level JSON, not nested
}

Event represents a daemon event that can trigger notifications and hooks.

func (Event) Marshal

func (e Event) Marshal() ([]byte, error)

Marshal serializes the event into JSON, merging the common envelope with the Payload fields at the top level.

type EventBus

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

EventBus dispatches daemon events to hook scripts. All dispatch is asynchronous — Emit() never blocks the caller.

func New

func New(logger *slog.Logger) *EventBus

New creates an EventBus.

func (*EventBus) Emit

func (b *EventBus) Emit(ctx context.Context, event Event)

Emit dispatches an event asynchronously. It never blocks the caller.

func (*EventBus) SetHookDispatch

func (b *EventBus) SetHookDispatch(fn func(ctx context.Context, event Event))

SetHookDispatch sets the function called to dispatch events to hook scripts.

type HookConfig

type HookConfig struct {
	Event   string `yaml:"event" json:"event"`
	Command string `yaml:"command" json:"command"`
}

HookConfig defines a user-registered hook script triggered by daemon events.

func LoadHooks

func LoadHooks() ([]HookConfig, error)

LoadHooks reads and validates hook configurations from hooks.yaml. Returns an empty slice (not error) if the file doesn't exist.

func LoadHooksFrom

func LoadHooksFrom(path string) ([]HookConfig, error)

LoadHooksFrom reads hooks from a specific file path. Exported for testing.

type HookRunner

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

HookRunner executes user-defined hook scripts for matching events. Scripts receive the event as JSON on stdin with a hard 500ms timeout.

func NewHookRunner

func NewHookRunner(hooks []HookConfig, logger *slog.Logger) *HookRunner

NewHookRunner creates a HookRunner with the given hooks.

func (*HookRunner) Dispatch

func (r *HookRunner) Dispatch(ctx context.Context, event Event)

Dispatch finds hooks matching the event and runs each in a background goroutine. Never blocks the caller.

func (*HookRunner) SetHooks

func (r *HookRunner) SetHooks(hooks []HookConfig)

SetHooks replaces the current hook configuration.

func (*HookRunner) Wait

func (r *HookRunner) Wait()

Wait blocks until all dispatched hooks have finished. Intended for tests; production callers should treat Dispatch as fire-and-forget.

Jump to

Keyboard shortcuts

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