hooks

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

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

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

type EventSource interface {
	EventBus() *events.EventBus
}

EventSource is an interface for objects that provide an EventBus. This is typically a [sdk.Conversation].

type PipelineHandler

type PipelineHandler func(totalCost float64, inputTokens, outputTokens int)

PipelineHandler is called when a pipeline completes.

type ProviderCallHandler

type ProviderCallHandler func(model string, inputTokens, outputTokens int, cost float64)

ProviderCallHandler is called when a provider (LLM) call completes.

type ToolCallHandler

type ToolCallHandler func(name string, args map[string]any)

ToolCallHandler is called when a tool is invoked.

type ValidationHandler

type ValidationHandler func(validatorName string, err error)

ValidationHandler is called when validation fails.

Jump to

Keyboard shortcuts

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