hook

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: AGPL-3.0, Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package hook provides Go-native lifecycle hooks (FR-08): callbacks fired at eight points in the agent loop that can observe state, block or rewrite tool calls, inject context, and gate the Stop transition. A Registry implements harness.HookRunner, so wiring it into a Session needs no glue.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event struct {
	Type      EventType
	ToolName  string
	Input     json.RawMessage
	Result    json.RawMessage
	IsError   bool
	Prompt    string
	AgentType string
	Messages  []llm.Message
}

Event is the payload passed to a hook. Which fields are set depends on Type.

type EventType

type EventType string

EventType enumerates the eight hook points (FR-08.2).

const (
	PreToolUse         EventType = "PreToolUse"
	PostToolUse        EventType = "PostToolUse"
	PostToolUseFailure EventType = "PostToolUseFailure"
	UserPromptSubmit   EventType = "UserPromptSubmit"
	SessionStart       EventType = "SessionStart"
	SessionEnd         EventType = "SessionEnd"
	Stop               EventType = "Stop"
	SubagentStart      EventType = "SubagentStart"
)

type Func

type Func func(ctx context.Context, ev Event) Result

Func is a single hook callback.

type Registry

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

Registry holds hooks by event type and implements harness.HookRunner.

func NewRegistry

func NewRegistry() *Registry

NewRegistry builds an empty registry.

func (*Registry) FireSessionEnd

func (r *Registry) FireSessionEnd(ctx context.Context)

func (*Registry) FireSessionStart

func (r *Registry) FireSessionStart(ctx context.Context)

FireSessionStart / FireSessionEnd are fire-and-forget notifications.

func (*Registry) FireSubagentStart

func (r *Registry) FireSubagentStart(ctx context.Context, agentType string)

FireSubagentStart notifies that a subagent is starting.

func (*Registry) FireUserPromptSubmit

func (r *Registry) FireUserPromptSubmit(ctx context.Context, prompt string) string

FireUserPromptSubmit runs UserPromptSubmit hooks and returns concatenated additional context to inject (empty if none).

func (*Registry) On

func (r *Registry) On(t EventType, f Func) *Registry

On registers a hook for an event type.

func (*Registry) PostToolUse

func (r *Registry) PostToolUse(ctx context.Context, name string, input, result []byte, isErr bool)

PostToolUse runs PostToolUse hooks (and PostToolUseFailure on error).

func (*Registry) PreToolUse

func (r *Registry) PreToolUse(ctx context.Context, name string, input []byte) (bool, string, []byte)

PreToolUse runs PreToolUse hooks. The first block halts; the last non-empty UpdatedInput wins.

func (*Registry) Stop

func (r *Registry) Stop(ctx context.Context, messages []llm.Message) (prevent bool, blockingErrors []string, message string)

Stop runs Stop hooks and reports two distinct outcomes: a hook may hard-terminate the loop (preventContinuation), or it may force the loop to continue by emitting blocking errors injected as messages (FR-08.4). preventContinuation wins if any hook sets it.

type Result

type Result struct {
	// Decision is "block" to halt the action, "approve"/"" to continue.
	Decision string
	// Message explains a block (shown to the model).
	Message string
	// UpdatedInput optionally rewrites a tool's input (PreToolUse).
	UpdatedInput json.RawMessage
	// AdditionalContext is injected into the conversation (UserPromptSubmit, Stop).
	AdditionalContext string
	// PreventContinuation, on a Stop hook, hard-terminates the loop
	// (stop_hook_prevented) even if other logic would continue. A "block"
	// decision instead forces the loop to continue with AdditionalContext
	// injected (stop_hook_blocking).
	PreventContinuation bool
}

Result is a hook's response (FR-08.3).

Jump to

Keyboard shortcuts

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