Documentation
¶
Overview ¶
Package hooks provides synchronous interception points for provider calls, tool execution, and session lifecycle in the PromptKit runtime.
Index ¶
- Variables
- type ChunkInterceptor
- type Decision
- type HookDeniedError
- type Option
- type ProviderHook
- type ProviderRequest
- type ProviderResponse
- type Registry
- func (r *Registry) HasChunkInterceptors() bool
- func (r *Registry) IsEmpty() bool
- func (r *Registry) RunAfterProviderCall(ctx context.Context, req *ProviderRequest, resp *ProviderResponse) Decision
- func (r *Registry) RunAfterToolExecution(ctx context.Context, req ToolRequest, resp ToolResponse) Decision
- func (r *Registry) RunBeforeProviderCall(ctx context.Context, req *ProviderRequest) Decision
- func (r *Registry) RunBeforeToolExecution(ctx context.Context, req ToolRequest) Decision
- func (r *Registry) RunOnChunk(ctx context.Context, chunk *providers.StreamChunk) Decision
- func (r *Registry) RunSessionEnd(ctx context.Context, event SessionEvent) error
- func (r *Registry) RunSessionStart(ctx context.Context, event SessionEvent) error
- func (r *Registry) RunSessionUpdate(ctx context.Context, event SessionEvent) error
- type SessionEvent
- type SessionHook
- type ToolHook
- type ToolRequest
- type ToolResponse
Constants ¶
This section is empty.
Variables ¶
var Allow = Decision{Allow: true} //nolint:gochecknoglobals // convenience sentinel
Allow is the zero-cost approval decision.
Functions ¶
This section is empty.
Types ¶
type ChunkInterceptor ¶
type ChunkInterceptor interface {
OnChunk(ctx context.Context, chunk *providers.StreamChunk) Decision
}
ChunkInterceptor is an opt-in streaming extension for ProviderHook. ProviderHooks that also implement ChunkInterceptor will have OnChunk called for each streaming chunk, enabling early abort.
type HookDeniedError ¶
type HookDeniedError struct {
HookName string
HookType string // "provider_before", "provider_after", "chunk", "tool_before", "tool_after"
Reason string
Metadata map[string]any
}
HookDeniedError is returned when a hook denies an operation.
func (*HookDeniedError) Error ¶
func (e *HookDeniedError) Error() string
type Option ¶
type Option func(*Registry)
Option configures a Registry during construction.
func WithProviderHook ¶
func WithProviderHook(h ProviderHook) Option
WithProviderHook registers a provider hook.
func WithSessionHook ¶
func WithSessionHook(h SessionHook) Option
WithSessionHook registers a session hook.
type ProviderHook ¶
type ProviderHook interface {
Name() string
BeforeCall(ctx context.Context, req *ProviderRequest) Decision
AfterCall(ctx context.Context, req *ProviderRequest, resp *ProviderResponse) Decision
}
ProviderHook intercepts LLM provider calls.
type ProviderRequest ¶
type ProviderRequest struct {
ProviderID string
Model string
Messages []types.Message
SystemPrompt string
Round int
Metadata map[string]any
}
ProviderRequest describes an LLM call about to be made.
type ProviderResponse ¶
type ProviderResponse struct {
ProviderID string
Model string
Message types.Message
Round int
LatencyMs int64
}
ProviderResponse describes a completed LLM call.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry holds registered hooks and provides chain-execution methods. A nil *Registry is safe to use — all Run* methods return Allow / nil.
func NewRegistry ¶
NewRegistry creates a Registry with the given options.
func (*Registry) HasChunkInterceptors ¶
HasChunkInterceptors returns true if any registered provider hook implements ChunkInterceptor.
func (*Registry) RunAfterProviderCall ¶
func (r *Registry) RunAfterProviderCall(ctx context.Context, req *ProviderRequest, resp *ProviderResponse) Decision
RunAfterProviderCall executes all provider hooks' AfterCall in order. First deny wins and short-circuits.
func (*Registry) RunAfterToolExecution ¶
func (r *Registry) RunAfterToolExecution(ctx context.Context, req ToolRequest, resp ToolResponse) Decision
RunAfterToolExecution executes all tool hooks' AfterExecution in order. First deny wins and short-circuits.
func (*Registry) RunBeforeProviderCall ¶
func (r *Registry) RunBeforeProviderCall(ctx context.Context, req *ProviderRequest) Decision
RunBeforeProviderCall executes all provider hooks' BeforeCall in order. First deny wins and short-circuits.
func (*Registry) RunBeforeToolExecution ¶
func (r *Registry) RunBeforeToolExecution(ctx context.Context, req ToolRequest) Decision
RunBeforeToolExecution executes all tool hooks' BeforeExecution in order. First deny wins and short-circuits.
func (*Registry) RunOnChunk ¶
RunOnChunk executes all chunk interceptors in order. First deny wins and short-circuits.
func (*Registry) RunSessionEnd ¶
func (r *Registry) RunSessionEnd(ctx context.Context, event SessionEvent) error
RunSessionEnd executes all session hooks' OnSessionEnd in order. First error short-circuits.
func (*Registry) RunSessionStart ¶
func (r *Registry) RunSessionStart(ctx context.Context, event SessionEvent) error
RunSessionStart executes all session hooks' OnSessionStart in order. First error short-circuits.
func (*Registry) RunSessionUpdate ¶
func (r *Registry) RunSessionUpdate(ctx context.Context, event SessionEvent) error
RunSessionUpdate executes all session hooks' OnSessionUpdate in order. First error short-circuits.
type SessionEvent ¶
type SessionEvent struct {
SessionID string
ConversationID string
Messages []types.Message
TurnIndex int
Metadata map[string]any
}
SessionEvent carries context for session lifecycle hooks.
type SessionHook ¶
type SessionHook interface {
Name() string
OnSessionStart(ctx context.Context, event SessionEvent) error
OnSessionUpdate(ctx context.Context, event SessionEvent) error
OnSessionEnd(ctx context.Context, event SessionEvent) error
}
SessionHook tracks session lifecycle.
type ToolHook ¶
type ToolHook interface {
Name() string
BeforeExecution(ctx context.Context, req ToolRequest) Decision
AfterExecution(ctx context.Context, req ToolRequest, resp ToolResponse) Decision
}
ToolHook intercepts tool execution (LLM-initiated calls only).
type ToolRequest ¶
type ToolRequest struct {
Name string
Args json.RawMessage
CallID string
}
ToolRequest describes a tool call about to be executed.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package guardrails provides built-in ProviderHook implementations that replace the legacy validator system with hook-based guardrails.
|
Package guardrails provides built-in ProviderHook implementations that replace the legacy validator system with hook-based guardrails. |