hooks

package
v2.22.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 1 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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. It runs once per provider call. Some providers report part of a call's usage after the call completed (Gemini 3 Live reports a tool-call turn's usage after its tool calls); OnProviderUsage delivers that, and a call's total is the cost given here plus its OnProviderUsage callbacks.

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 OnProviderUsage added in v2.22.0

func OnProviderUsage(source EventSource, handler ProviderUsageHandler)

OnProviderUsage subscribes to provider.call.usage events: usage a provider reported for a call after the call completed. A call's total cost is the cost OnProviderCall reported for it plus these callbacks for its CallID.

hooks.OnProviderUsage(conv, func(u hooks.ProviderUsage) {
    log.Printf("call %s: %d in, %d out, $%.4f more", u.CallID, u.InputTokens, u.OutputTokens, u.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.Bus
}

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 ProviderUsage added in v2.22.0

type ProviderUsage struct {
	CallID       string
	Model        string
	InputTokens  int
	OutputTokens int
	CachedTokens int
	Cost         float64
}

ProviderUsage is usage a provider reported for a call after the call completed: CallID names the call (as on provider.call.completed).

type ProviderUsageHandler added in v2.22.0

type ProviderUsageHandler func(usage ProviderUsage)

ProviderUsageHandler is called with usage reported for a provider call after it completed.

type SubscriptionOwner added in v2.13.0

type SubscriptionOwner interface {
	OwnSubscription(unsubscribe func())
}

SubscriptionOwner is an EventSource that removes subscriptions when it closes. [sdk.Conversation] implements it, so a handler registered through this package stops with the conversation, including on a bus supplied with sdk.WithEventBus that outlives it.

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