Documentation
¶
Overview ¶
Package hooks provides convenience methods for subscribing to SDK events.
This package wraps the runtime's events.EventBus with ergonomic helpers for common observability patterns. All hooks use the runtime's event types directly - this package adds no new event types.
Basic Usage ¶
Use OnEvent to subscribe to all events:
hooks.OnEvent(conv, func(e *events.Event) {
log.Printf("[%s] %s", e.Type, e.Timestamp)
})
Use On to subscribe to specific event types:
hooks.On(conv, events.EventToolCallStarted, func(e *events.Event) {
data := e.Data.(*events.ToolCallStartedData)
log.Printf("Tool: %s", data.ToolName)
})
Convenience Hooks ¶
For common patterns, use the typed convenience methods:
hooks.OnToolCall(conv, func(name string, args map[string]any) {
log.Printf("Calling tool: %s", name)
})
hooks.OnValidationFailed(conv, func(validator string, err error) {
log.Printf("Validation %s failed: %v", validator, err)
})
Index ¶
- func On(source EventSource, eventType events.EventType, handler func(*events.Event))
- func OnEvent(source EventSource, handler func(*events.Event))
- func OnPipelineComplete(source EventSource, handler PipelineHandler)
- func OnProviderCall(source EventSource, handler ProviderCallHandler)
- func OnToolCall(source EventSource, handler ToolCallHandler)
- func OnValidationFailed(source EventSource, handler ValidationHandler)
- type EventSource
- type PipelineHandler
- type ProviderCallHandler
- type ToolCallHandler
- type ValidationHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func On ¶
func On(source EventSource, eventType events.EventType, handler func(*events.Event))
On subscribes to a specific event type from the source.
Use this for targeted event handling:
hooks.On(conv, events.EventToolCallStarted, func(e *events.Event) {
data := e.Data.(*events.ToolCallStartedData)
log.Printf("Tool: %s", data.ToolName)
})
func OnEvent ¶
func OnEvent(source EventSource, handler func(*events.Event))
OnEvent subscribes to all events from the source.
This is useful for logging, debugging, or building custom dashboards:
hooks.OnEvent(conv, func(e *events.Event) {
log.Printf("[%s] %s: %+v", e.Type, e.Timestamp, e.Data)
})
func OnPipelineComplete ¶
func OnPipelineComplete(source EventSource, handler PipelineHandler)
OnPipelineComplete subscribes to pipeline completion events.
Use this for aggregate metrics:
hooks.OnPipelineComplete(conv, func(cost float64, in, out int) {
metrics.RecordCost(cost)
metrics.RecordTokens(in + out)
})
func OnProviderCall ¶
func OnProviderCall(source EventSource, handler ProviderCallHandler)
OnProviderCall subscribes to provider call completion events.
Use this to track costs and token usage:
hooks.OnProviderCall(conv, func(model string, in, out int, cost float64) {
log.Printf("Model %s: %d in, %d out, $%.4f", model, in, out, cost)
})
func OnToolCall ¶
func OnToolCall(source EventSource, handler ToolCallHandler)
OnToolCall subscribes to tool call events.
This provides a simplified interface for monitoring tool execution:
hooks.OnToolCall(conv, func(name string, args map[string]any) {
log.Printf("Tool %s called with %v", name, args)
})
func OnValidationFailed ¶
func OnValidationFailed(source EventSource, handler ValidationHandler)
OnValidationFailed subscribes to validation failure events.
Use this to monitor and log validation issues:
hooks.OnValidationFailed(conv, func(validator string, err error) {
log.Printf("Validation %s failed: %v", validator, err)
})
Types ¶
type EventSource ¶
EventSource is an interface for objects that provide an EventBus. This is typically a [sdk.Conversation].
type PipelineHandler ¶
PipelineHandler is called when a pipeline completes.
type ProviderCallHandler ¶
ProviderCallHandler is called when a provider (LLM) call completes.
type ToolCallHandler ¶
ToolCallHandler is called when a tool is invoked.
type ValidationHandler ¶
ValidationHandler is called when validation fails.