hooks

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2026 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultConfigPath = "/data/.memoh/hooks.json"
	DefaultWorkDir    = "/data"

	EventPreToolUse               = "PreToolUse"
	EventPostToolUse              = "PostToolUse"
	EventToolError                = "ToolError"
	EventSessionStart             = "SessionStart"
	EventUserMessageReceived      = "UserMessageReceived"
	EventBeforePromptBuild        = "BeforePromptBuild"
	EventAfterPromptBuild         = "AfterPromptBuild"
	EventBeforeModelCall          = "BeforeModelCall"
	EventAfterModelCall           = "AfterModelCall"
	EventTurnEnd                  = "TurnEnd"
	EventTurnError                = "TurnError"
	EventBeforeMemorySearch       = "BeforeMemorySearch"
	EventAfterMemorySearch        = "AfterMemorySearch"
	EventBeforeMemoryWrite        = "BeforeMemoryWrite"
	EventAfterMemoryWrite         = "AfterMemoryWrite"
	EventMemoryExtracted          = "MemoryExtracted"
	EventWorkspaceStart           = "WorkspaceStart"
	EventWorkspaceStop            = "WorkspaceStop"
	EventBeforeWorkspaceCommand   = "BeforeWorkspaceCommand"
	EventAfterWorkspaceCommand    = "AfterWorkspaceCommand"
	EventBeforeFileWrite          = "BeforeFileWrite"
	EventAfterFileWrite           = "AfterFileWrite"
	EventBeforeApprovalCreate     = "BeforeApprovalCreate"
	EventApprovalRequested        = "ApprovalRequested"
	EventApprovalResolved         = "ApprovalResolved"
	EventApprovalTimeout          = "ApprovalTimeout"
	EventInboundMessageNormalized = "InboundMessageNormalized"
	EventBeforeOutboundMessage    = "BeforeOutboundMessage"
	EventAfterOutboundMessage     = "AfterOutboundMessage"
	EventChannelDeliveryFailed    = "ChannelDeliveryFailed"
	EventPreCompact               = "PreCompact"
	EventPostCompact              = "PostCompact"
	EventSubagentStart            = "SubagentStart"
	EventSubagentStop             = "SubagentStop"
)
View Source
const (
	ActionCommand = "command"
	ActionTool    = "tool"
	ActionMCPTool = "mcp_tool"
)
View Source
const (
	DecisionAllow         = "allow"
	DecisionDeny          = "deny"
	DecisionAskApproval   = "ask_approval"
	DecisionAppendContext = "append_context"
)
View Source
const (
	WarningInvalidAppendSystemSection       = "invalid_append_system_section"
	WarningAppendSystemSectionOutputLimited = "hook_system_section_output_limited"
	WarningSystemSectionRequiredClamped     = "hook_system_section_required_clamped"
)
View Source
const (
	OnErrorIgnore = "ignore"
	OnErrorFail   = "fail"
	OnErrorBlock  = "block"
)

Variables

View Source
var (
	ErrDenied      = errors.New("hook denied action")
	ErrUnsupported = errors.New("unsupported hook action")
)

Functions

func EventCatalog

func EventCatalog() []string

func RuntimeSupported

func RuntimeSupported(event string) bool

Types

type ActionResult

type ActionResult struct {
	ActionType           string                `json:"action_type,omitempty"`
	Name                 string                `json:"name,omitempty"`
	Decision             string                `json:"decision,omitempty"`
	Reason               string                `json:"reason,omitempty"`
	Stdout               string                `json:"stdout,omitempty"`
	Stderr               string                `json:"stderr,omitempty"`
	ExitCode             int32                 `json:"exit_code,omitempty"`
	Result               any                   `json:"result,omitempty"`
	AppendSystemSections []SystemSectionOutput `json:"append_system_sections,omitempty"`
	Warnings             []OutputWarning       `json:"warnings,omitempty"`
	Metadata             map[string]any        `json:"metadata,omitempty"`
	Error                string                `json:"error,omitempty"`
	// contains filtered or unexported fields
}

type Condition

type Condition struct {
	Expr string `json:"expr,omitempty"`
}

type Config

type Config struct {
	Version  int               `json:"version"`
	Enabled  *bool             `json:"enabled,omitempty"`
	Defaults Defaults          `json:"defaults,omitempty"`
	Env      map[string]string `json:"env,omitempty"`
	Hooks    []Hook            `json:"hooks,omitempty"`
}

func ParseConfig

func ParseConfig(data []byte) (Config, error)

func (Config) Match

func (c Config) Match(req Request) []Hook

type Defaults

type Defaults struct {
	Timeout            string `json:"timeout,omitempty"`
	OnError            string `json:"on_error,omitempty"`
	MaxOutputBytes     int    `json:"max_output_bytes,omitempty"`
	TriggerNestedHooks *bool  `json:"trigger_nested_hooks,omitempty"`
}

type Hook

type Hook struct {
	Name       string       `json:"name,omitempty"`
	Event      string       `json:"event"`
	Matcher    string       `json:"matcher,omitempty"`
	Enabled    *bool        `json:"enabled,omitempty"`
	Priority   int          `json:"priority,omitempty"`
	Conditions []Condition  `json:"conditions,omitempty"`
	Actions    []HookAction `json:"actions,omitempty"`
	// contains filtered or unexported fields
}

type HookAction

type HookAction struct {
	Type               string         `json:"type"`
	Command            string         `json:"command,omitempty"`
	Tool               string         `json:"tool,omitempty"`
	Server             string         `json:"server,omitempty"`
	Input              map[string]any `json:"input,omitempty"`
	Timeout            string         `json:"timeout,omitempty"`
	OnError            string         `json:"on_error,omitempty"`
	WorkDir            string         `json:"work_dir,omitempty"`
	TriggerNestedHooks *bool          `json:"trigger_nested_hooks,omitempty"`
}

type OutputWarning

type OutputWarning struct {
	Code      string `json:"code"`
	Message   string `json:"message,omitempty"`
	HookName  string `json:"hook_name,omitempty"`
	SectionID string `json:"section_id,omitempty"`
	// contains filtered or unexported fields
}

type Request

type Request struct {
	Version   int            `json:"version"`
	Event     string         `json:"event"`
	HookName  string         `json:"hook_name,omitempty"`
	BotID     string         `json:"bot_id,omitempty"`
	SessionID string         `json:"session_id,omitempty"`
	ChatID    string         `json:"chat_id,omitempty"`
	Workspace WorkspaceInfo  `json:"workspace,omitempty"`
	Tool      *ToolPayload   `json:"tool,omitempty"`
	Approval  map[string]any `json:"approval,omitempty"`
	Turn      map[string]any `json:"turn,omitempty"`
	Memory    map[string]any `json:"memory,omitempty"`
	Channel   map[string]any `json:"channel,omitempty"`
	Extra     map[string]any `json:"extra,omitempty"`
	Error     string         `json:"error,omitempty"`
}

type Result

type Result struct {
	Decision             string                `json:"decision,omitempty"`
	Reason               string                `json:"reason,omitempty"`
	AppendContext        string                `json:"append_context,omitempty"`
	AppendSystemSections []SystemSectionOutput `json:"append_system_sections,omitempty"`
	Warnings             []OutputWarning       `json:"warnings,omitempty"`
	HooksMatched         int                   `json:"hooks_matched"`
	ActionsRun           int                   `json:"actions_run"`
	RuntimeSupported     bool                  `json:"runtime_supported"`
	ActionResults        []ActionResult        `json:"action_results,omitempty"`
	Metadata             map[string]any        `json:"metadata,omitempty"`
	// contains filtered or unexported fields
}

type Service

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

func NewService

func NewService(log *slog.Logger, provider bridge.Provider) *Service

func (*Service) Load

func (s *Service) Load(ctx context.Context, botID string) (Config, bool, error)

func (*Service) LoadEffective

func (s *Service) LoadEffective(ctx context.Context, botID string) (Config, bool, error)

func (*Service) Run

func (s *Service) Run(ctx context.Context, req Request, runner ToolRunner) (Result, error)

func (*Service) RunConfig

func (s *Service) RunConfig(ctx context.Context, cfg Config, req Request, runner ToolRunner) (Result, error)

type SystemSectionCache

type SystemSectionCache string
const (
	SystemSectionCacheDynamic SystemSectionCache = "dynamic"
	SystemSectionCacheStable  SystemSectionCache = "stable"
)

type SystemSectionOutput

type SystemSectionOutput struct {
	HookName  string                 `json:"hook_name,omitempty"`
	ID        string                 `json:"id,omitempty"`
	Text      string                 `json:"text"`
	Retention SystemSectionRetention `json:"retention"`
	Cache     SystemSectionCache     `json:"cache"`

	WarningCodes []string `json:"-"`
	// contains filtered or unexported fields
}

type SystemSectionRetention

type SystemSectionRetention string
const (
	SystemSectionRetentionOptional  SystemSectionRetention = "optional"
	SystemSectionRetentionPreferred SystemSectionRetention = "preferred"
)

type ToolPayload

type ToolPayload struct {
	Name   string `json:"name,omitempty"`
	CallID string `json:"call_id,omitempty"`
	Input  any    `json:"input,omitempty"`
	Result any    `json:"result,omitempty"`
	Error  string `json:"error,omitempty"`
}

type ToolRunner

type ToolRunner interface {
	RunHookTool(ctx context.Context, toolName string, input map[string]any) (any, error)
}

type WorkspaceInfo

type WorkspaceInfo struct {
	CWD     string `json:"cwd,omitempty"`
	Runtime string `json:"runtime,omitempty"`
}

Jump to

Keyboard shortcuts

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