Documentation
¶
Index ¶
- Variables
- type Message
- type Model
- type ModelFetcher
- type OAuthConfig
- type ParsedToolCall
- type Provider
- type ProviderConfig
- type Registry
- func (r *Registry) AllModels() []Model
- func (r *Registry) CreateStream(ctx context.Context, opts StreamOptions) (<-chan StreamEvent, error)
- func (r *Registry) FetchModels(ctx context.Context, name string) ([]Model, error)
- func (r *Registry) Provider(name string) (Provider, error)
- func (r *Registry) Register(p Provider)
- func (r *Registry) ResolveModel(ref string) (Provider, string, error)
- type Role
- type StreamEvent
- type StreamEventType
- type StreamOptions
- type ToolCall
- type ToolCallResult
- type ToolDefinition
- type ToolSet
- type ToolSpec
- type TypedToolCall
- type Usage
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFound = errors.New("not found") ErrBadRequest = errors.New("bad request") )
Common errors
Functions ¶
This section is empty.
Types ¶
type Message ¶
type Message struct {
ID string `json:"id,omitempty"`
Role Role `json:"role"`
Content string `json:"content"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
}
Message represents a single message in a conversation.
type Model ¶
type Model struct {
ID string `json:"id"`
Name string `json:"name"`
Provider string `json:"provider"`
}
Model represents an LLM model.
type ModelFetcher ¶
ModelFetcher is an optional interface providers can implement to list models dynamically from their API instead of returning a static list.
type OAuthConfig ¶
type OAuthConfig struct {
Access string `json:"access_token"`
Refresh string `json:"refresh_token"`
Expires int64 `json:"expires"` // Unix timestamp in milliseconds
}
OAuthConfig holds OAuth tokens and expiry information.
type ParsedToolCall ¶
ParsedToolCall is the interface for parsed tool call results. Use a type switch on the concrete *TypedToolCall[T] to access typed params.
Example:
switch c := call.(type) {
case *TypedToolCall[GetWeatherParams]:
fmt.Println(c.Params.Location) // strongly typed
case *TypedToolCall[SearchParams]:
fmt.Println(c.Params.Query)
}
type Provider ¶
type Provider interface {
Name() string
Models() []Model
CreateStream(ctx context.Context, opts StreamOptions) (<-chan StreamEvent, error)
}
Provider is the interface each LLM backend must implement.
type ProviderConfig ¶
type ProviderConfig struct {
APIKey string
OAuth *OAuthConfig
}
ProviderConfig holds authentication and configuration for a provider.
func (*ProviderConfig) GetAccessToken ¶
func (c *ProviderConfig) GetAccessToken() string
GetAccessToken returns the current access token (OAuth or API key).
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry holds all registered providers and resolves model references.
func (*Registry) CreateStream ¶
func (r *Registry) CreateStream(ctx context.Context, opts StreamOptions) (<-chan StreamEvent, error)
CreateStream is a convenience that resolves a model ref and delegates to the provider.
func (*Registry) FetchModels ¶
FetchModels returns models for a specific provider. If the provider implements ModelFetcher, it fetches models dynamically from the API. Otherwise it falls back to the static Models() list.
type StreamEvent ¶
type StreamEvent struct {
Type StreamEventType
Delta string
Reasoning string
ToolCall *ToolCall
Error error
Usage *Usage
}
StreamEvent is a single event emitted by a provider during streaming.
type StreamEventType ¶
type StreamEventType string
StreamEventType identifies the kind of streaming event from a provider.
const ( StreamEventDelta StreamEventType = "delta" StreamEventReasoning StreamEventType = "reasoning" StreamEventToolCall StreamEventType = "tool_call" StreamEventDone StreamEventType = "done" StreamEventError StreamEventType = "error" )
type StreamOptions ¶
type StreamOptions struct {
Model string
Messages []Message
Tools []ToolDefinition
}
StreamOptions configures a provider CreateStream call.
type ToolCall ¶
type ToolCall struct {
ID string `json:"id"`
Name string `json:"name"`
Arguments map[string]any `json:"arguments"`
Result *ToolCallResult `json:"result,omitempty"`
}
ToolCall represents a request from the LLM to invoke a tool.
type ToolCallResult ¶
ToolCallResult represents the result of executing a tool.
type ToolDefinition ¶
type ToolDefinition struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters map[string]any `json:"parameters"`
}
ToolDefinition describes a tool that the model can invoke. This is used when sending tools to a provider's API.
func ToolDefinitionFor ¶
func ToolDefinitionFor[T any](name, description string) ToolDefinition
ToolDefinitionFor creates a ToolDefinition from a Go struct type using reflection. The struct's fields are converted to a JSON Schema that describes the tool's parameters.
Field tags:
- `json:"fieldName"` - Sets the parameter name (required)
- `jsonschema:"description=..."` - Describes the parameter
- `jsonschema:"required"` - Marks the parameter as required
- `jsonschema:"enum=val1,enum=val2"` - Restricts to specific values
Example:
type GetWeatherParams struct {
Location string `json:"location" jsonschema:"description=City name,required"`
Unit string `json:"unit" jsonschema:"description=Temperature unit,enum=celsius,enum=fahrenheit"`
}
tool := ToolDefinitionFor[GetWeatherParams]("get_weather", "Get current weather")
type ToolSet ¶
type ToolSet struct {
// contains filtered or unexported fields
}
ToolSet manages a collection of tool specifications. It provides tool definitions for sending to providers and parses raw tool calls into strongly-typed results with validation.
func NewToolSet ¶
func NewToolSet(tools ...toolRegistration) *ToolSet
NewToolSet creates a ToolSet from one or more tool specs.
Example:
tools := NewToolSet(
NewToolSpec[GetWeatherParams]("get_weather", "Get weather"),
NewToolSpec[SearchParams]("search", "Search the web"),
)
func (*ToolSet) Definitions ¶
func (ts *ToolSet) Definitions() []ToolDefinition
Definitions returns all tool definitions for sending to providers.
func (*ToolSet) Parse ¶
func (ts *ToolSet) Parse(calls []ToolCall) ([]ParsedToolCall, error)
Parse converts raw ToolCalls (from stream events) into typed ParsedToolCalls. Each tool call's arguments are validated against its JSON Schema before parsing.
Successfully parsed calls are always returned. Errors from unknown tool names or validation/parse failures are collected and returned as a joined error. The error is non-fatal - you get all successfully parsed calls.
Example:
calls, err := tools.Parse(rawToolCalls)
if err != nil {
log.Printf("parse warnings: %v", err)
}
for _, call := range calls {
switch c := call.(type) {
case *TypedToolCall[GetWeatherParams]:
fmt.Println(c.Params.Location)
}
}
type ToolSpec ¶
type ToolSpec[T any] struct { // contains filtered or unexported fields }
ToolSpec[T] is a type-safe tool specification that pairs a tool name/description with a Go struct that defines the parameter schema. It includes a compiled JSON Schema for runtime validation.
func NewToolSpec ¶
NewToolSpec creates a typed tool specification from a parameter struct. The struct's fields define the JSON Schema for the tool's parameters. Field tags are the same as ToolDefinitionFor: json, jsonschema.
Example:
type GetWeatherParams struct {
Location string `json:"location" jsonschema:"description=City name,required"`
}
spec := NewToolSpec[GetWeatherParams]("get_weather", "Get current weather")
func (*ToolSpec[T]) Definition ¶
func (s *ToolSpec[T]) Definition() ToolDefinition
Definition returns the ToolDefinition for sending to providers.
type TypedToolCall ¶
type TypedToolCall[T any] struct { ID string // Original tool call ID (for sending results back) Name string // Tool name Params T // Parsed, validated parameters }
TypedToolCall[T] holds a parsed tool call with strongly-typed parameters.
func (*TypedToolCall[T]) ToolCallID ¶
func (c *TypedToolCall[T]) ToolCallID() string
ToolCallID returns the tool call ID.
func (*TypedToolCall[T]) ToolName ¶
func (c *TypedToolCall[T]) ToolName() string
ToolName returns the tool name.