types

package
v1.0.41 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2025 License: MIT Imports: 7 Imported by: 1

Documentation

Overview

Package types defines the core types and interfaces for the AI provider kit. It includes standardized request/response formats, provider interfaces, and common data structures used across all AI providers.

Package types defines the core interfaces and data structures for the AI Provider Kit. It includes provider interfaces, configuration types, message formats, tool definitions, and metrics structures used across all provider implementations.

Index

Constants

View Source
const (
	ContentTypeText       = "text"
	ContentTypeImage      = "image"
	ContentTypeDocument   = "document"
	ContentTypeAudio      = "audio"
	ContentTypeToolUse    = "tool_use"
	ContentTypeToolResult = "tool_result"
	ContentTypeThinking   = "thinking"
)

ContentType constants for common content types

View Source
const (
	MediaSourceBase64 = "base64"
	MediaSourceURL    = "url"
)

MediaSourceType constants

Variables

View Source
var (
	ErrNoMessages             = NewValidationError("at least one message is required")
	ErrInvalidTemperature     = NewValidationError("temperature must be between 0 and 2")
	ErrInvalidMaxTokens       = NewValidationError("max_tokens must be non-negative")
	ErrToolChoiceWithoutTools = NewValidationError("tool_choice specified but no tools provided")
)

Common validation errors

Functions

func HasDefaultExtension

func HasDefaultExtension(providerType ProviderType) bool

HasDefaultExtension checks if the default core API has an extension

func IsOAuthProvider added in v1.0.14

func IsOAuthProvider(provider Provider) bool

IsOAuthProvider checks if a provider implements OAuthProvider interface

func IsTestableProvider added in v1.0.14

func IsTestableProvider(provider Provider) bool

IsTestableProvider checks if a provider implements TestableProvider interface

func IsValidationError

func IsValidationError(err error) bool

IsValidationError checks if an error is a validation error

func RegisterDefaultExtension

func RegisterDefaultExtension(providerType ProviderType, extension CoreProviderExtension) error

RegisterDefaultExtension registers an extension with the default core API

Types

type AuthConfig

type AuthConfig struct {
	Method       AuthMethod `json:"method"`
	APIKey       string     `json:"api_key,omitempty"`
	BaseURL      string     `json:"base_url,omitempty"`
	DefaultModel string     `json:"default_model,omitempty"`
}

AuthConfig represents authentication configuration

type AuthMethod

type AuthMethod string

AuthMethod represents the authentication method

const (
	AuthMethodAPIKey      AuthMethod = "api_key"
	AuthMethodBearerToken AuthMethod = "bearer_token"
	AuthMethodOAuth       AuthMethod = "oauth"
	AuthMethodCustom      AuthMethod = "custom"
)

type AuthMethodDetector added in v1.0.17

type AuthMethodDetector interface {
	// IsOAuthConfigured returns true if OAuth credentials are properly configured
	IsOAuthConfigured() bool

	// IsAPIKeyConfigured returns true if API key authentication is properly configured
	IsAPIKeyConfigured() bool
}

AuthMethodDetector defines methods for detecting configured authentication methods. This optional interface is for providers that support multiple authentication methods (e.g., both OAuth and API key) and need to expose which methods are currently configured.

type AuthenticatedProvider

type AuthenticatedProvider interface {
	// Authentication
	Authenticate(ctx context.Context, authConfig AuthConfig) error
	IsAuthenticated() bool
	Logout(ctx context.Context) error
}

AuthenticatedProvider defines methods for authentication management. This interface is for providers that require authentication.

type BaseExtension

type BaseExtension struct {
	// contains filtered or unexported fields
}

BaseExtension provides common functionality for provider extensions

func NewBaseExtension

func NewBaseExtension(name, version, description string, capabilities []string) *BaseExtension

NewBaseExtension creates a new base extension

func (*BaseExtension) Description

func (e *BaseExtension) Description() string

Description returns the extension description

func (*BaseExtension) GetCapabilities

func (e *BaseExtension) GetCapabilities() []string

GetCapabilities returns the extension capabilities

func (*BaseExtension) Name

func (e *BaseExtension) Name() string

Name returns the extension name

func (*BaseExtension) ValidateOptions

func (e *BaseExtension) ValidateOptions(options map[string]interface{}) error

ValidateOptions performs basic validation common to all providers

func (*BaseExtension) Version

func (e *BaseExtension) Version() string

Version returns the extension version

type CapabilityProvider added in v1.0.39

type CapabilityProvider interface {
	// Streaming support
	SupportsStreaming() bool

	// Responses API support
	SupportsResponsesAPI() bool
}

CapabilityProvider provides methods to query provider capabilities. This interface is for providers that expose their feature support.

type ChatChoice

type ChatChoice struct {
	Index        int         `json:"index"`
	Message      ChatMessage `json:"message"`
	FinishReason string      `json:"finish_reason"`
	Delta        ChatMessage `json:"delta"`
}

ChatChoice represents a choice in a chat completion

type ChatCompletionChunk

type ChatCompletionChunk struct {
	ID               string                 `json:"id"`
	Object           string                 `json:"object"`
	Created          int64                  `json:"created"`
	Model            string                 `json:"model"`
	Choices          []ChatChoice           `json:"choices"`
	Usage            Usage                  `json:"usage"`
	Done             bool                   `json:"done"`
	Content          string                 `json:"content"`
	Reasoning        string                 `json:"reasoning,omitempty"`         // Reasoning content for clients that want it
	ReasoningContent string                 `json:"reasoning_content,omitempty"` // Alternative reasoning field
	Error            string                 `json:"error"`
	Metadata         map[string]interface{} `json:"metadata,omitempty"`
}

ChatCompletionChunk represents a chunk of a streaming response

type ChatCompletionStream

type ChatCompletionStream interface {
	Next() (ChatCompletionChunk, error)
	Close() error
}

ChatCompletionStream represents a streaming response

type ChatMessage

type ChatMessage struct {
	Role             string                 `json:"role"`
	Content          string                 `json:"content"`                     // Simple text content (backwards compatible)
	Parts            []ContentPart          `json:"parts,omitempty"`             // Multimodal content parts
	Reasoning        string                 `json:"reasoning,omitempty"`         // Reasoning content (e.g., from GLM-4.6, OpenCode/Zen)
	ReasoningContent string                 `json:"reasoning_content,omitempty"` // Alternative reasoning field (e.g., from vLLM/Synthetic)
	ToolCalls        []ToolCall             `json:"tool_calls,omitempty"`
	ToolCallID       string                 `json:"tool_call_id,omitempty"`
	Metadata         map[string]interface{} `json:"metadata,omitempty"`
}

ChatMessage represents a chat message Supports both simple string content and multimodal content parts. For backwards compatibility, Content can be a string (legacy usage). For multimodal content (images, documents, etc.), use Parts field.

func (*ChatMessage) AddContentPart added in v1.0.13

func (m *ChatMessage) AddContentPart(part ContentPart)

AddContentPart appends a content part to Parts If Content string was set, converts it to a text part first

func (*ChatMessage) GetContentParts added in v1.0.13

func (m *ChatMessage) GetContentParts() []ContentPart

GetContentParts returns content as []ContentPart If Content is a non-empty string and Parts is nil, returns [{Type: "text", Text: content}] If Parts is set, returns Parts

func (*ChatMessage) GetTextContent added in v1.0.13

func (m *ChatMessage) GetTextContent() string

GetTextContent returns concatenated text from all text parts Works with both string Content and Parts array

func (*ChatMessage) HasImages added in v1.0.13

func (m *ChatMessage) HasImages() bool

HasImages returns true if any content part is an image

func (*ChatMessage) HasMedia added in v1.0.13

func (m *ChatMessage) HasMedia() bool

HasMedia returns true if any content part has media (image, document, audio)

func (*ChatMessage) SetContentParts added in v1.0.13

func (m *ChatMessage) SetContentParts(parts []ContentPart)

SetContentParts sets Parts and clears Content string

func (*ChatMessage) SetTextContent added in v1.0.13

func (m *ChatMessage) SetTextContent(text string)

SetTextContent sets Content to a simple string and clears Parts

type ChatProvider

type ChatProvider interface {
	// Core chat capability
	GenerateChatCompletion(ctx context.Context, options GenerateOptions) (ChatCompletionStream, error)
}

ChatProvider defines the core chat completion capability. This interface is for providers that can generate text responses.

type ChatService

type ChatService struct {
	// contains filtered or unexported fields
}

ChatService only needs to generate completions, so it depends only on ChatProvider

func NewChatService

func NewChatService(provider ChatProvider) *ChatService

func (*ChatService) GenerateResponse

func (s *ChatService) GenerateResponse(ctx context.Context, options GenerateOptions) (ChatCompletionStream, error)

type CodeGenerationResult

type CodeGenerationResult struct {
	Code  string `json:"code"`
	Usage *Usage `json:"usage,omitempty"`
}

CodeGenerationResult represents the result of code generation including token usage

type ConfigurableProvider

type ConfigurableProvider interface {
	// Configuration
	Configure(config ProviderConfig) error
	GetConfig() ProviderConfig
}

ConfigurableProvider defines methods for configuration management. This interface is for providers that support runtime configuration.

type ContentPart added in v1.0.13

type ContentPart struct {
	Type string `json:"type"` // "text", "image", "document", "audio", "tool_use", "tool_result", "thinking"

	// Text content
	Text string `json:"text,omitempty"`

	// Media content (images, documents, audio)
	Source *MediaSource `json:"source,omitempty"`

	// Tool use (model calling a tool)
	ID    string                 `json:"id,omitempty"`
	Name  string                 `json:"name,omitempty"`
	Input map[string]interface{} `json:"input,omitempty"`

	// Tool result (response to tool call)
	ToolUseID string      `json:"tool_use_id,omitempty"`
	Content   interface{} `json:"content,omitempty"` // string or []ContentPart

	// Extended thinking
	Thinking string `json:"thinking,omitempty"`

	// Escape hatch for future/provider-specific types
	Extra map[string]interface{} `json:"extra,omitempty"`
}

ContentPart represents a single part of message content (text, image, document, audio, etc.)

func NewDocumentPart added in v1.0.13

func NewDocumentPart(mediaType, base64Data string) ContentPart

NewDocumentPart creates a document content part (e.g., PDF)

func NewImagePart added in v1.0.13

func NewImagePart(mediaType, base64Data string) ContentPart

NewImagePart creates an image content part from base64 data

func NewImageURLPart added in v1.0.13

func NewImageURLPart(mediaType, url string) ContentPart

NewImageURLPart creates an image content part from a URL

func NewTextPart added in v1.0.13

func NewTextPart(text string) ContentPart

NewTextPart creates a text content part

func (*ContentPart) IsMedia added in v1.0.13

func (c *ContentPart) IsMedia() bool

IsMedia returns true if the content part contains media (image, document, audio)

func (*ContentPart) IsText added in v1.0.13

func (c *ContentPart) IsText() bool

IsText returns true if the content part is text

func (*ContentPart) IsToolRelated added in v1.0.13

func (c *ContentPart) IsToolRelated() bool

IsToolRelated returns true if the content part is tool_use or tool_result

type CoreAPI

type CoreAPI struct {
	// contains filtered or unexported fields
}

CoreAPI represents the standardized core API that all providers should implement

func GetDefaultCoreAPI

func GetDefaultCoreAPI() *CoreAPI

GetDefaultCoreAPI returns the default core API instance

func NewCoreAPI

func NewCoreAPI() *CoreAPI

NewCoreAPI creates a new core API instance

func (*CoreAPI) ConvertChunkFromProvider

func (api *CoreAPI) ConvertChunkFromProvider(providerType ProviderType, chunk interface{}) (*StandardStreamChunk, error)

ConvertChunkFromProvider converts a provider-specific chunk to standard format

func (*CoreAPI) ConvertFromProvider

func (api *CoreAPI) ConvertFromProvider(providerType ProviderType, response interface{}) (*StandardResponse, error)

ConvertFromProvider converts a provider-specific response to standard format

func (*CoreAPI) ConvertToProvider

func (api *CoreAPI) ConvertToProvider(providerType ProviderType, request StandardRequest) (interface{}, error)

ConvertToProvider converts a standard request to provider-specific format

func (*CoreAPI) GetExtension

func (api *CoreAPI) GetExtension(providerType ProviderType) (CoreProviderExtension, error)

GetExtension gets a provider extension

func (*CoreAPI) GetProviderCapabilities

func (api *CoreAPI) GetProviderCapabilities(providerType ProviderType) []string

GetProviderCapabilities gets the capabilities of a provider

func (*CoreAPI) HasExtension

func (api *CoreAPI) HasExtension(providerType ProviderType) bool

HasExtension checks if a provider has an extension

func (*CoreAPI) ListExtensions

func (api *CoreAPI) ListExtensions() map[ProviderType]CoreProviderExtension

ListExtensions lists all registered extensions

func (*CoreAPI) RegisterExtension

func (api *CoreAPI) RegisterExtension(providerType ProviderType, extension CoreProviderExtension) error

RegisterExtension registers a provider extension

func (*CoreAPI) ValidateProviderOptions

func (api *CoreAPI) ValidateProviderOptions(providerType ProviderType, options map[string]interface{}) error

ValidateProviderOptions validates provider-specific options

type CoreChatProvider

type CoreChatProvider interface {
	// Core chat completion using standard request/response format
	GenerateStandardCompletion(ctx context.Context, request StandardRequest) (*StandardResponse, error)

	// Streaming chat completion using standard format
	GenerateStandardStream(ctx context.Context, request StandardRequest) (StandardStream, error)

	// Get the provider's core extension for format conversion
	GetCoreExtension() CoreProviderExtension

	// Provider capabilities using the standardized format
	GetStandardCapabilities() []string

	// Validate a standard request for this provider
	ValidateStandardRequest(request StandardRequest) error
}

CoreChatProvider defines the interface for providers that work with the standardized core API This is the new recommended interface that all providers should implement

type CoreProvider

type CoreProvider interface {
	// Basic provider information
	Name() string
	Type() ProviderType
	Description() string
}

CoreProvider defines the essential identity methods that all providers must implement. This interface provides basic information about the provider.

type CoreProviderAdapter

type CoreProviderAdapter struct {
	// contains filtered or unexported fields
}

CoreProviderAdapter adapts existing providers to work with the new standardized API

func NewCoreProviderAdapter

func NewCoreProviderAdapter(provider Provider, extension CoreProviderExtension) *CoreProviderAdapter

NewCoreProviderAdapter creates a new adapter for an existing provider

func (*CoreProviderAdapter) GenerateStandardCompletion

func (a *CoreProviderAdapter) GenerateStandardCompletion(ctx context.Context, request StandardRequest) (*StandardResponse, error)

GenerateStandardCompletion generates a completion using the standard format

func (*CoreProviderAdapter) GenerateStandardStream

func (a *CoreProviderAdapter) GenerateStandardStream(ctx context.Context, request StandardRequest) (StandardStream, error)

GenerateStandardStream generates a streaming completion using the standard format

func (*CoreProviderAdapter) GetCoreExtension

func (a *CoreProviderAdapter) GetCoreExtension() CoreProviderExtension

GetCoreExtension returns the provider's core extension

func (*CoreProviderAdapter) GetStandardCapabilities

func (a *CoreProviderAdapter) GetStandardCapabilities() []string

GetStandardCapabilities returns the provider's capabilities

func (*CoreProviderAdapter) ValidateStandardRequest

func (a *CoreProviderAdapter) ValidateStandardRequest(request StandardRequest) error

ValidateStandardRequest validates a standard request for this provider

type CoreProviderExtension

type CoreProviderExtension interface {
	// Extension information
	Name() string
	Version() string
	Description() string

	// Convert between standard and provider-specific formats
	StandardToProvider(request StandardRequest) (interface{}, error)
	ProviderToStandard(response interface{}) (*StandardResponse, error)
	ProviderToStandardChunk(chunk interface{}) (*StandardStreamChunk, error)

	// Validate provider-specific options
	ValidateOptions(options map[string]interface{}) error

	// Get provider-specific capabilities
	GetCapabilities() []string
}

CoreProviderExtension defines the interface for provider-specific extensions This allows providers to add their own unique capabilities while maintaining compatibility with the standardized core API

func GetDefaultExtension

func GetDefaultExtension(providerType ProviderType) (CoreProviderExtension, error)

GetDefaultExtension gets an extension from the default core API

type CoreRequestBuilder

type CoreRequestBuilder struct {
	// contains filtered or unexported fields
}

CoreRequestBuilder helps build standard requests with validation

func NewCoreRequestBuilder

func NewCoreRequestBuilder() *CoreRequestBuilder

NewCoreRequestBuilder creates a new request builder

func (*CoreRequestBuilder) Build

func (b *CoreRequestBuilder) Build() (*StandardRequest, error)

Build builds the standard request

func (*CoreRequestBuilder) FromGenerateOptions

func (b *CoreRequestBuilder) FromGenerateOptions(options GenerateOptions) *CoreRequestBuilder

FromGenerateOptions converts from legacy GenerateOptions to StandardRequest

func (*CoreRequestBuilder) WithContext

WithContext sets the context for the request

func (*CoreRequestBuilder) WithMaxTokens

func (b *CoreRequestBuilder) WithMaxTokens(maxTokens int) *CoreRequestBuilder

WithMaxTokens sets the maximum tokens for the request

func (*CoreRequestBuilder) WithMessages

func (b *CoreRequestBuilder) WithMessages(messages []ChatMessage) *CoreRequestBuilder

WithMessages sets the messages for the request

func (*CoreRequestBuilder) WithMetadata

func (b *CoreRequestBuilder) WithMetadata(key string, value interface{}) *CoreRequestBuilder

WithMetadata adds metadata to the request

func (*CoreRequestBuilder) WithModel

func (b *CoreRequestBuilder) WithModel(model string) *CoreRequestBuilder

WithModel sets the model for the request

func (*CoreRequestBuilder) WithResponseFormat

func (b *CoreRequestBuilder) WithResponseFormat(format string) *CoreRequestBuilder

WithResponseFormat sets the response format for the request

func (*CoreRequestBuilder) WithStop

func (b *CoreRequestBuilder) WithStop(stop []string) *CoreRequestBuilder

WithStop sets the stop sequences for the request

func (*CoreRequestBuilder) WithStreaming

func (b *CoreRequestBuilder) WithStreaming(stream bool) *CoreRequestBuilder

WithStreaming enables or disables streaming

func (*CoreRequestBuilder) WithTemperature

func (b *CoreRequestBuilder) WithTemperature(temperature float64) *CoreRequestBuilder

WithTemperature sets the temperature for the request

func (*CoreRequestBuilder) WithTimeout

func (b *CoreRequestBuilder) WithTimeout(timeout time.Duration) *CoreRequestBuilder

WithTimeout sets the timeout for the request

func (*CoreRequestBuilder) WithToolChoice

func (b *CoreRequestBuilder) WithToolChoice(toolChoice *ToolChoice) *CoreRequestBuilder

WithToolChoice sets the tool choice for the request

func (*CoreRequestBuilder) WithTools

func (b *CoreRequestBuilder) WithTools(tools []Tool) *CoreRequestBuilder

WithTools sets the tools for the request

type CredentialProvider added in v1.0.25

type CredentialProvider interface {
	// GetCredentials returns the current OAuth credentials for a provider
	// The provider name is used to look up credentials in the storage
	// Returns an empty slice if no credentials are available
	GetCredentials(ctx context.Context, providerName string) ([]*OAuthCredentialSet, error)

	// UpdateCredential updates a specific credential after token refresh
	// This is called by OAuthKeyManager after it refreshes a token
	// The implementation should persist the updated credential
	UpdateCredential(ctx context.Context, providerName string, credential *OAuthCredentialSet) error
}

CredentialProvider is an interface for dynamically providing OAuth credentials This allows external systems to manage credential storage and provide fresh credentials on-demand, rather than relying on cached credentials.

When a CredentialProvider is configured, the OAuthKeyManager will call GetCredentials() to fetch the current credentials instead of using its internal cached copy. This ensures that any external token refresh (e.g., from a separate OAuth extension) is immediately visible.

type CredentialProviderAware added in v1.0.25

type CredentialProviderAware interface {
	// SetCredentialProvider sets a dynamic credential provider for OAuth credentials
	SetCredentialProvider(provider CredentialProvider)
}

CredentialProviderAware is an interface for providers that support dynamic credential providers Providers implementing this interface can have their OAuth credentials managed externally

func AsCredentialProviderAware added in v1.0.25

func AsCredentialProviderAware(provider Provider) (CredentialProviderAware, bool)

AsCredentialProviderAware safely casts a provider to CredentialProviderAware interface

type DefaultExtensionRegistry

type DefaultExtensionRegistry struct {
	// contains filtered or unexported fields
}

DefaultExtensionRegistry implements ExtensionRegistry

func (*DefaultExtensionRegistry) Get

Get gets an extension for a provider type

func (*DefaultExtensionRegistry) Has

func (r *DefaultExtensionRegistry) Has(providerType ProviderType) bool

Has checks if a provider type has a registered extension

func (*DefaultExtensionRegistry) List

List returns all registered extensions

func (*DefaultExtensionRegistry) Register

func (r *DefaultExtensionRegistry) Register(providerType ProviderType, extension CoreProviderExtension) error

Register registers an extension for a provider type

type DefaultProviderFactoryExtensions

type DefaultProviderFactoryExtensions struct {
	ProviderFactory
	// contains filtered or unexported fields
}

DefaultProviderFactoryExtensions implements ProviderFactoryExtensions

func NewDefaultProviderFactoryExtensions

func NewDefaultProviderFactoryExtensions(factory ProviderFactory) *DefaultProviderFactoryExtensions

NewDefaultProviderFactoryExtensions creates a new factory with core API support

func (*DefaultProviderFactoryExtensions) CreateCoreProvider

func (f *DefaultProviderFactoryExtensions) CreateCoreProvider(providerType ProviderType, config ProviderConfig) (CoreChatProvider, error)

CreateCoreProvider creates a core provider adapter

func (*DefaultProviderFactoryExtensions) GetSupportedCoreProviders

func (f *DefaultProviderFactoryExtensions) GetSupportedCoreProviders() []ProviderType

GetSupportedCoreProviders returns provider types that support the core API

func (*DefaultProviderFactoryExtensions) SupportsCoreAPI

func (f *DefaultProviderFactoryExtensions) SupportsCoreAPI(providerType ProviderType) bool

SupportsCoreAPI checks if a provider type supports the core API

type ErrorCode added in v1.0.8

type ErrorCode string

ErrorCode categorizes provider errors

const (
	ErrCodeUnknown        ErrorCode = "unknown"
	ErrCodeAuthentication ErrorCode = "authentication"
	ErrCodeRateLimit      ErrorCode = "rate_limit"
	ErrCodeInvalidRequest ErrorCode = "invalid_request"
	ErrCodeNotFound       ErrorCode = "not_found"
	ErrCodeServerError    ErrorCode = "server_error"
	ErrCodeTimeout        ErrorCode = "timeout"
	ErrCodeNetwork        ErrorCode = "network"
	ErrCodeContextLength  ErrorCode = "context_length"
	ErrCodeContentFilter  ErrorCode = "content_filter"

	// Aliases for TestResult status compatibility.
	// These convenience constants make it easier to map between TestStatus values
	// and ErrorCode constants when implementing health checks and provider testing.
	// Example: if testResult.Status == TestStatusAuthFailed { return NewProviderError(..., ErrCodeAuthFailed, ...) }
	ErrCodeAuthFailed         = ErrCodeAuthentication // Maps to TestStatusAuthFailed
	ErrCodeConnectivityFailed = ErrCodeNetwork        // Maps to TestStatusConnectivityFailed
	ErrCodeTimeoutFailed      = ErrCodeTimeout        // Maps to TestStatusTimeoutFailed
)

func ClassifyHTTPError added in v1.0.8

func ClassifyHTTPError(statusCode int) ErrorCode

ClassifyHTTPError determines error code from HTTP status

type ErrorMetrics

type ErrorMetrics struct {
	// Total error counts
	TotalErrors int64   `json:"total_errors"`
	ErrorRate   float64 `json:"error_rate"` // Calculated: errors / total_requests

	// Error categorization by type
	ErrorsByType map[string]int64 `json:"errors_by_type"` // e.g., "rate_limit", "timeout", "authentication", "invalid_request"

	// Error categorization by HTTP status
	ErrorsByStatus map[string]int64 `json:"errors_by_status"` // e.g., "400", "401", "429", "500", "503"

	// Error categorization by provider
	ErrorsByProvider map[string]int64 `json:"errors_by_provider"`

	// Error categorization by model
	ErrorsByModel map[string]int64 `json:"errors_by_model,omitempty"`

	// Specific error categories
	RateLimitErrors      int64 `json:"rate_limit_errors"`
	TimeoutErrors        int64 `json:"timeout_errors"`
	AuthenticationErrors int64 `json:"authentication_errors"`
	InvalidRequestErrors int64 `json:"invalid_request_errors"`
	ServerErrors         int64 `json:"server_errors"` // 5xx errors
	NetworkErrors        int64 `json:"network_errors"`
	UnknownErrors        int64 `json:"unknown_errors"`

	// Error percentages (for quick analysis)
	RateLimitErrorRate      float64 `json:"rate_limit_error_rate"`     // Calculated: rate_limit_errors / total_errors
	TimeoutErrorRate        float64 `json:"timeout_error_rate"`        // Calculated: timeout_errors / total_errors
	AuthenticationErrorRate float64 `json:"authentication_error_rate"` // Calculated: auth_errors / total_errors
	ServerErrorRate         float64 `json:"server_error_rate"`         // Calculated: server_errors / total_errors

	// Recent error tracking
	ConsecutiveErrors int64     `json:"consecutive_errors"` // Current consecutive error count
	LastError         string    `json:"last_error"`
	LastErrorType     string    `json:"last_error_type"`
	LastErrorTime     time.Time `json:"last_error_time"`

	// Error recovery metrics
	TotalRetries      int64   `json:"total_retries"`
	SuccessfulRetries int64   `json:"successful_retries"`
	FailedRetries     int64   `json:"failed_retries"`
	RetrySuccessRate  float64 `json:"retry_success_rate"` // Calculated: successful_retries / total_retries

	// Last updated
	LastUpdated time.Time `json:"last_updated"`
}

ErrorMetrics represents comprehensive error categorization and tracking

type ExtensionRegistry

type ExtensionRegistry interface {
	// Register an extension for a provider type
	Register(providerType ProviderType, extension CoreProviderExtension) error

	// Get extension for a provider type
	Get(providerType ProviderType) (CoreProviderExtension, error)

	// List all registered extensions
	List() map[ProviderType]CoreProviderExtension

	// Check if a provider type has a registered extension
	Has(providerType ProviderType) bool
}

ExtensionRegistry manages provider extensions

func NewExtensionRegistry

func NewExtensionRegistry() ExtensionRegistry

NewExtensionRegistry creates a new extension registry

type FlexibleMockProvider

type FlexibleMockProvider struct {
	// contains filtered or unexported fields
}

FlexibleMockProvider is a mock implementation that satisfies all provider interfaces for demonstration purposes in the FlexibleProviderFactory example.

IMPORTANT: This is an example implementation that returns simulated data. Do NOT use this in production code.

func (*FlexibleMockProvider) Authenticate

func (p *FlexibleMockProvider) Authenticate(ctx context.Context, authConfig AuthConfig) error

Authenticate authenticates the mock provider (always succeeds)

func (*FlexibleMockProvider) Configure

func (p *FlexibleMockProvider) Configure(config ProviderConfig) error

Configure configures the mock provider with the given config

func (*FlexibleMockProvider) Description

func (p *FlexibleMockProvider) Description() string

func (*FlexibleMockProvider) GenerateChatCompletion

func (p *FlexibleMockProvider) GenerateChatCompletion(ctx context.Context, options GenerateOptions) (ChatCompletionStream, error)

GenerateChatCompletion generates a mock chat completion stream

func (*FlexibleMockProvider) GetConfig

func (p *FlexibleMockProvider) GetConfig() ProviderConfig

func (*FlexibleMockProvider) GetDefaultModel

func (p *FlexibleMockProvider) GetDefaultModel() string

func (*FlexibleMockProvider) GetMetrics

func (p *FlexibleMockProvider) GetMetrics() ProviderMetrics

func (*FlexibleMockProvider) GetModels

func (p *FlexibleMockProvider) GetModels(ctx context.Context) ([]Model, error)

GetModels returns mock models for the provider

func (*FlexibleMockProvider) GetToolFormat

func (p *FlexibleMockProvider) GetToolFormat() ToolFormat

func (*FlexibleMockProvider) HealthCheck

func (p *FlexibleMockProvider) HealthCheck(ctx context.Context) error

HealthCheck performs a health check on the provider

func (*FlexibleMockProvider) InvokeServerTool

func (p *FlexibleMockProvider) InvokeServerTool(ctx context.Context, toolName string, params interface{}) (interface{}, error)

func (*FlexibleMockProvider) IsAuthenticated

func (p *FlexibleMockProvider) IsAuthenticated() bool

func (*FlexibleMockProvider) Logout

func (p *FlexibleMockProvider) Logout(ctx context.Context) error

func (*FlexibleMockProvider) Name

func (p *FlexibleMockProvider) Name() string

Name returns the mock provider name

func (*FlexibleMockProvider) SupportsResponsesAPI

func (p *FlexibleMockProvider) SupportsResponsesAPI() bool

SupportsResponsesAPI indicates if the provider supports the responses API

func (*FlexibleMockProvider) SupportsStreaming

func (p *FlexibleMockProvider) SupportsStreaming() bool

SupportsStreaming indicates if the provider supports streaming responses

func (*FlexibleMockProvider) SupportsToolCalling

func (p *FlexibleMockProvider) SupportsToolCalling() bool

SupportsToolCalling returns true for mock provider

func (*FlexibleMockProvider) Type

type FlexibleMockStream

type FlexibleMockStream struct {
	// contains filtered or unexported fields
}

FlexibleMockStream implements ChatCompletionStream for mock responses

func (*FlexibleMockStream) Close

func (m *FlexibleMockStream) Close() error

func (*FlexibleMockStream) Next

type FlexibleProviderFactory

type FlexibleProviderFactory struct{}

FlexibleProviderFactory demonstrates how to create providers with different interface requirements.

IMPORTANT: This is an example implementation for educational purposes only. It creates mock providers that return simulated data and should NOT be used in production. For production use, integrate with the actual provider factory implementations.

func (*FlexibleProviderFactory) CreateChatProvider

func (f *FlexibleProviderFactory) CreateChatProvider(providerType ProviderType, config ProviderConfig) (ChatProvider, error)

CreateChatProvider creates a provider that implements only the ChatProvider interface. This allows clients to depend only on chat completion functionality.

func (*FlexibleProviderFactory) CreateHealthCheckProvider

func (f *FlexibleProviderFactory) CreateHealthCheckProvider(providerType ProviderType, config ProviderConfig) (HealthCheckProvider, error)

CreateHealthCheckProvider creates a provider that implements only the HealthCheckProvider interface. This allows clients to depend only on health checking functionality.

func (*FlexibleProviderFactory) CreateModelProvider

func (f *FlexibleProviderFactory) CreateModelProvider(providerType ProviderType, config ProviderConfig) (ModelProvider, error)

CreateModelProvider creates a provider that implements only the ModelProvider interface. This allows clients to depend only on model discovery functionality.

type GenerateOptions

type GenerateOptions struct {
	Prompt         string                 `json:"prompt"`
	Model          string                 `json:"model,omitempty"` // Per-request model override
	Context        string                 `json:"context"`
	ContextObj     context.Context        `json:"-"` // Internal context for operations
	OutputFile     string                 `json:"output_file"`
	Language       *string                `json:"language"`
	ContextFiles   []string               `json:"context_files"`
	Messages       []ChatMessage          `json:"messages"`
	MaxTokens      int                    `json:"max_tokens,omitempty"`
	Temperature    float64                `json:"temperature,omitempty"`
	Stop           []string               `json:"stop,omitempty"`
	Stream         bool                   `json:"stream"`
	Tools          []Tool                 `json:"tools,omitempty"`
	ToolChoice     *ToolChoice            `json:"tool_choice,omitempty"` // Fine-grained tool selection control
	ResponseFormat string                 `json:"response_format,omitempty"`
	Timeout        time.Duration          `json:"timeout,omitempty"`
	Metadata       map[string]interface{} `json:"metadata,omitempty"`
}

GenerateOptions represents options for generating content

type HealthCheckProvider

type HealthCheckProvider interface {
	// Health and metrics
	HealthCheck(ctx context.Context) error
	GetMetrics() ProviderMetrics
}

HealthCheckProvider defines methods for health monitoring and metrics. This interface is for providers that expose health and performance information.

type HealthMonitoringService

type HealthMonitoringService struct {
	// contains filtered or unexported fields
}

HealthMonitoringService only needs to check health, so it depends only on HealthCheckProvider

func NewHealthMonitoringService

func NewHealthMonitoringService() *HealthMonitoringService

func (*HealthMonitoringService) AddProvider

func (s *HealthMonitoringService) AddProvider(provider HealthCheckProvider)

func (*HealthMonitoringService) CheckAllHealth

func (s *HealthMonitoringService) CheckAllHealth(ctx context.Context) map[string]error

type HealthStatus

type HealthStatus struct {
	Healthy      bool      `json:"healthy"`
	LastChecked  time.Time `json:"last_checked"`
	Message      string    `json:"message"`
	ResponseTime float64   `json:"response_time"`
	StatusCode   int       `json:"status_code"`
}

HealthStatus represents the health status of a provider

type HookID added in v1.0.7

type HookID string

HookID is a unique identifier for a registered hook.

type LatencyMetrics added in v1.0.7

type LatencyMetrics struct {
	// Summary statistics
	TotalRequests  int64         `json:"total_requests"`  // Number of requests measured
	TotalLatency   time.Duration `json:"total_latency"`   // Sum of all latencies
	AverageLatency time.Duration `json:"average_latency"` // Calculated: total_latency / total_requests

	// Min/Max
	MinLatency time.Duration `json:"min_latency"`
	MaxLatency time.Duration `json:"max_latency"`

	// Percentiles (for detailed latency distribution)
	P50Latency time.Duration `json:"p50_latency"` // Median
	P75Latency time.Duration `json:"p75_latency"`
	P90Latency time.Duration `json:"p90_latency"`
	P95Latency time.Duration `json:"p95_latency"`
	P99Latency time.Duration `json:"p99_latency"`

	// Last updated
	LastUpdated time.Time `json:"last_updated"`
}

LatencyMetrics represents latency statistics including percentiles

type MediaSource added in v1.0.13

type MediaSource struct {
	Type      string `json:"type"`                 // "base64", "url"
	MediaType string `json:"media_type,omitempty"` // MIME type: "image/png", "application/pdf", "audio/wav"
	Data      string `json:"data,omitempty"`       // base64-encoded data
	URL       string `json:"url,omitempty"`        // URL to the media
}

MediaSource represents the source of media content (images, documents, audio)

type MetricEvent added in v1.0.7

type MetricEvent struct {
	// Type of event (request, success, error, etc.)
	Type MetricEventType `json:"type"`

	// Provider identification
	ProviderName string       `json:"provider_name"`
	ProviderType ProviderType `json:"provider_type"`

	// Model identification (may be empty for non-model events)
	ModelID string `json:"model_id,omitempty"`

	// Timestamp when the event occurred
	Timestamp time.Time `json:"timestamp"`

	// Request/response details
	Latency      time.Duration `json:"latency,omitempty"`       // Time taken for request
	TokensUsed   int64         `json:"tokens_used,omitempty"`   // Total tokens consumed
	InputTokens  int64         `json:"input_tokens,omitempty"`  // Input tokens
	OutputTokens int64         `json:"output_tokens,omitempty"` // Output tokens

	// Error details (only for error events)
	ErrorType    string `json:"error_type,omitempty"`    // Categorized error type
	ErrorMessage string `json:"error_message,omitempty"` // Human-readable error

	// HTTP details (optional)
	StatusCode int `json:"status_code,omitempty"` // HTTP status code

	// Streaming context (for streaming events)
	IsStreaming      bool          `json:"is_streaming,omitempty"`        // Indicates this is a streaming request
	StreamSessionID  string        `json:"stream_session_id,omitempty"`   // Correlate related stream events
	StreamChunkIndex int           `json:"stream_chunk_index,omitempty"`  // Which chunk (0-indexed)
	TimeToFirstToken time.Duration `json:"time_to_first_token,omitempty"` // TTFT for stream_start events
	TokensPerSecond  float64       `json:"tokens_per_second,omitempty"`   // Throughput for stream_end events

	// Virtual provider context (for fallback/racing/loadbalance events)
	FromProvider     string                   `json:"from_provider,omitempty"`     // Provider switched from
	ToProvider       string                   `json:"to_provider,omitempty"`       // Provider switched to
	SwitchReason     string                   `json:"switch_reason,omitempty"`     // Why switch occurred (failure, timeout, race_winner)
	AttemptNumber    int                      `json:"attempt_number,omitempty"`    // Which attempt in fallback chain
	RaceParticipants []string                 `json:"race_participants,omitempty"` // Providers in the race
	RaceLatencies    map[string]time.Duration `json:"race_latencies,omitempty"`    // Latencies per racing provider
	RaceWinner       string                   `json:"race_winner,omitempty"`       // Which provider won the race

	// Circuit breaker context
	CircuitState     string `json:"circuit_state,omitempty"`     // Current state: closed, open, half-open
	CircuitFailures  int    `json:"circuit_failures,omitempty"`  // Consecutive failures
	CircuitSuccesses int    `json:"circuit_successes,omitempty"` // Consecutive successes (in half-open)

	// Additional metadata (provider-specific)
	Metadata map[string]interface{} `json:"metadata,omitempty"`
}

MetricEvent represents a single metrics event from a provider. Events are immutable after creation.

type MetricEventType added in v1.0.7

type MetricEventType string

MetricEventType categorizes different types of metrics events.

const (
	// MetricEventRequest indicates a request was initiated
	MetricEventRequest MetricEventType = "request"

	// MetricEventSuccess indicates a request completed successfully
	MetricEventSuccess MetricEventType = "success"

	// MetricEventError indicates a request failed with an error
	MetricEventError MetricEventType = "error"

	// MetricEventRateLimit indicates a rate limit was encountered
	MetricEventRateLimit MetricEventType = "rate_limit"

	// MetricEventTimeout indicates a request timed out
	MetricEventTimeout MetricEventType = "timeout"

	// MetricEventHealthCheck indicates a health check was performed
	MetricEventHealthCheck MetricEventType = "health_check"

	// MetricEventInitialization indicates a provider was initialized
	MetricEventInitialization MetricEventType = "initialization"

	// MetricEventTokenRefresh indicates an OAuth token was refreshed
	MetricEventTokenRefresh MetricEventType = "token_refresh"

	// Streaming events
	// MetricEventStreamStart indicates a streaming request began
	MetricEventStreamStart MetricEventType = "stream_start"

	// MetricEventStreamChunk indicates a chunk was received during streaming
	MetricEventStreamChunk MetricEventType = "stream_chunk"

	// MetricEventStreamEnd indicates a streaming request completed successfully
	MetricEventStreamEnd MetricEventType = "stream_end"

	// MetricEventStreamAbort indicates a streaming request was terminated abnormally
	MetricEventStreamAbort MetricEventType = "stream_abort"

	// Virtual provider events
	// MetricEventProviderSwitch indicates a switch between providers (fallback/racing)
	MetricEventProviderSwitch MetricEventType = "provider_switch"

	// MetricEventRaceComplete indicates a racing operation completed
	MetricEventRaceComplete MetricEventType = "race_complete"

	// MetricEventCircuitOpen indicates a circuit breaker opened
	MetricEventCircuitOpen MetricEventType = "circuit_open"

	// MetricEventCircuitClose indicates a circuit breaker closed
	MetricEventCircuitClose MetricEventType = "circuit_close"
)

func (MetricEventType) IsError added in v1.0.7

func (t MetricEventType) IsError() bool

IsError returns true if this event type represents an error condition.

func (MetricEventType) String added in v1.0.7

func (t MetricEventType) String() string

String returns the string representation of the event type.

type MetricFilter added in v1.0.7

type MetricFilter struct {
	// ProviderNames filters events to specific providers.
	// Empty slice = all providers.
	//
	// Example: []string{"openai-prod", "anthropic-prod"}
	ProviderNames []string `json:"provider_names,omitempty"`

	// ProviderTypes filters events to specific provider types.
	// Empty slice = all provider types.
	//
	// Example: []ProviderType{ProviderTypeOpenAI, ProviderTypeAnthropic}
	ProviderTypes []ProviderType `json:"provider_types,omitempty"`

	// ModelIDs filters events to specific models.
	// Empty slice = all models.
	//
	// Example: []string{"gpt-4", "claude-3-opus"}
	ModelIDs []string `json:"model_ids,omitempty"`

	// EventTypes filters events to specific event types.
	// Empty slice = all event types.
	//
	// Example: []MetricEventType{MetricEventError, MetricEventRateLimit}
	EventTypes []MetricEventType `json:"event_types,omitempty"`

	// MinLatency filters events with latency >= this threshold.
	// Zero value = no minimum threshold.
	//
	// Use case: Monitor only slow requests
	MinLatency time.Duration `json:"min_latency,omitempty"`

	// ErrorTypesOnly filters to only error events with specific error types.
	// Empty slice = all error types.
	// Only applicable when EventTypes includes MetricEventError.
	//
	// Example: []string{"rate_limit", "timeout"}
	ErrorTypesOnly []string `json:"error_types_only,omitempty"`
}

MetricFilter defines criteria for filtering metrics events in subscriptions. Only events matching ALL specified criteria will be delivered. Empty/nil slices mean "match all" for that dimension.

func (MetricFilter) Matches added in v1.0.7

func (f MetricFilter) Matches(event MetricEvent) bool

Matches returns true if the given event matches this filter's criteria.

type MetricsCollector added in v1.0.7

type MetricsCollector interface {
	// ----- Snapshot API (Polling Pattern) -----
	//
	// GetSnapshot returns a complete snapshot of all metrics across all providers and models.
	// This is the primary method for periodic polling and dashboard updates.
	//
	// The snapshot is a point-in-time copy and will not change after being returned.
	// Safe for concurrent access and can be serialized to JSON directly.
	//
	// Use cases:
	//  - Dashboard updates every N seconds
	//  - Periodic export to monitoring systems
	//  - Health check aggregation
	//
	// Example:
	//   snapshot := collector.GetSnapshot()
	//   fmt.Printf("Total requests: %d\n", snapshot.TotalRequests)
	//   json.Marshal(snapshot) // Works seamlessly
	GetSnapshot() MetricsSnapshot

	// GetProviderMetrics returns metrics for a specific provider by its name.
	// Returns nil if the provider is not found.
	//
	// Provider name should match the name used when registering the provider.
	//
	// Use cases:
	//  - Per-provider monitoring dashboards
	//  - Provider-specific alerting
	//  - Comparative analysis between providers
	//
	// Example:
	//   metrics := collector.GetProviderMetrics("openai-prod")
	//   if metrics != nil {
	//     fmt.Printf("Success rate: %.2f%%\n", metrics.SuccessRate*100)
	//   }
	GetProviderMetrics(providerName string) *ProviderMetricsSnapshot

	// GetModelMetrics returns metrics for a specific model across all providers.
	// Returns nil if no metrics exist for the given model ID.
	//
	// Model ID is the standardized model identifier (e.g., "gpt-4", "claude-3-opus").
	//
	// Use cases:
	//  - Model-specific performance analysis
	//  - Cost tracking per model
	//  - Model usage trends
	//
	// Example:
	//   metrics := collector.GetModelMetrics("gpt-4-turbo")
	//   if metrics != nil {
	//     fmt.Printf("Total tokens: %d\n", metrics.TotalTokens)
	//   }
	GetModelMetrics(modelID string) *ModelMetricsSnapshot

	// GetProviderNames returns a sorted list of all provider names currently tracked.
	// Useful for iterating over all providers to fetch individual metrics.
	//
	// Example:
	//   for _, name := range collector.GetProviderNames() {
	//     metrics := collector.GetProviderMetrics(name)
	//     // Process metrics...
	//   }
	GetProviderNames() []string

	// GetModelIDs returns a sorted list of all model IDs currently tracked.
	// Useful for iterating over all models to fetch individual metrics.
	//
	// Example:
	//   for _, modelID := range collector.GetModelIDs() {
	//     metrics := collector.GetModelMetrics(modelID)
	//     // Process metrics...
	//   }
	GetModelIDs() []string

	// ----- Event Streaming API (Pub/Sub Pattern) -----
	//
	// Subscribe creates a new subscription for receiving real-time metrics events.
	// The returned channel will receive all metrics events as they occur.
	//
	// The subscriber MUST continuously read from the channel to avoid blocking the collector.
	// If the buffer fills up (see bufferSize), the oldest events will be dropped (overflow handling).
	//
	// Call Unsubscribe() when done to clean up resources and prevent goroutine leaks.
	//
	// Parameters:
	//   bufferSize: Size of the event channel buffer. Recommended values:
	//               - 100: Low-frequency monitoring (few events per second)
	//               - 1000: Medium-frequency monitoring (tens of events per second)
	//               - 10000: High-frequency monitoring (hundreds of events per second)
	//
	// Use cases:
	//  - Real-time dashboards with WebSocket updates
	//  - Event stream processing
	//  - Live monitoring and alerting
	//
	// Example:
	//   sub := collector.Subscribe(1000)
	//   defer sub.Unsubscribe()
	//
	//   for event := range sub.Events() {
	//     fmt.Printf("Event: %s at %v\n", event.Type, event.Timestamp)
	//   }
	Subscribe(bufferSize int) MetricsSubscription

	// SubscribeFiltered creates a filtered subscription that only receives events matching the filter.
	// This is more efficient than Subscribe() + manual filtering when only specific events are needed.
	//
	// The filter is applied before buffering, reducing memory usage and processing overhead.
	//
	// Parameters:
	//   bufferSize: Size of the event channel buffer (same semantics as Subscribe)
	//   filter: Criteria for selecting which events to receive
	//
	// Use cases:
	//  - Provider-specific monitoring (only events from one provider)
	//  - Error-only monitoring (only failure events)
	//  - Model-specific tracking (only events for specific models)
	//
	// Example:
	//   filter := MetricFilter{
	//     ProviderNames: []string{"openai-prod"},
	//     EventTypes: []MetricEventType{MetricEventRequest, MetricEventError},
	//   }
	//   sub := collector.SubscribeFiltered(500, filter)
	//   defer sub.Unsubscribe()
	//
	//   for event := range sub.Events() {
	//     // Only receives request and error events from openai-prod
	//   }
	SubscribeFiltered(bufferSize int, filter MetricFilter) MetricsSubscription

	// ----- Hook API (Synchronous Callback Pattern) -----
	//
	// RegisterHook registers a synchronous callback that will be invoked for each metrics event.
	// Unlike subscriptions (async), hooks are called inline and can block event processing.
	//
	// Hooks are useful for:
	//  - Immediate actions (alerting, circuit breaking)
	//  - Synchronous processing (updating in-memory caches)
	//  - Low-latency reactions to specific events
	//
	// IMPORTANT: Hook execution blocks the metrics collection pipeline. Keep hook logic fast
	// to avoid degrading overall system performance. For heavy processing, use Subscribe() instead.
	//
	// The returned HookID can be used to unregister the hook later.
	//
	// Parameters:
	//   hook: The hook implementation to register
	//
	// Example:
	//   hook := &MyAlertHook{}
	//   hookID := collector.RegisterHook(hook)
	//   defer collector.UnregisterHook(hookID)
	RegisterHook(hook MetricsHook) HookID

	// UnregisterHook removes a previously registered hook.
	// Safe to call with an invalid HookID (no-op).
	//
	// Parameters:
	//   id: The HookID returned from RegisterHook()
	UnregisterHook(id HookID)

	// ----- Event Recording API (Provider Integration) -----
	//
	// RecordEvent records a single metrics event. This is the primary method used by
	// providers to emit metrics data to the collector.
	//
	// The event will be:
	//  1. Aggregated into snapshot data (for GetSnapshot, etc.)
	//  2. Sent to all active subscriptions (honoring filters)
	//  3. Passed to all registered hooks
	//
	// Thread-safe and designed to be called from provider implementations.
	//
	// Parameters:
	//   ctx: Context for cancellation and timeout (honors context cancellation)
	//   event: The event to record
	//
	// Example (from provider implementation):
	//   collector.RecordEvent(ctx, MetricEvent{
	//     Type: MetricEventRequest,
	//     ProviderName: "openai-prod",
	//     ProviderType: ProviderTypeOpenAI,
	//     ModelID: "gpt-4-turbo",
	//     Timestamp: time.Now(),
	//   })
	RecordEvent(ctx context.Context, event MetricEvent) error

	// RecordEvents records multiple events in a single batch.
	// More efficient than calling RecordEvent repeatedly.
	//
	// All events are processed atomically (all succeed or all fail).
	//
	// Parameters:
	//   ctx: Context for cancellation and timeout
	//   events: Slice of events to record
	//
	// Example:
	//   events := []MetricEvent{event1, event2, event3}
	//   collector.RecordEvents(ctx, events)
	RecordEvents(ctx context.Context, events []MetricEvent) error

	// ----- Reset and Lifecycle -----
	//
	// Reset clears all accumulated metrics data and resets counters to zero.
	// Active subscriptions and hooks are preserved.
	//
	// Use cases:
	//  - Rolling time windows (reset every hour/day)
	//  - Test isolation
	//  - Manual metrics rotation
	//
	// Example:
	//   collector.Reset() // Start fresh without losing subscriptions
	Reset()

	// Close gracefully shuts down the metrics collector.
	// - Closes all active subscriptions
	// - Unregisters all hooks
	// - Flushes any pending events
	//
	// After calling Close(), the collector should not be used.
	Close() error
}

MetricsCollector is the central interface for collecting, aggregating, and distributing metrics across all providers in ai-provider-kit. It serves as a single source of truth for metrics data and supports multiple consumption patterns: polling (snapshots), streaming (events), and synchronous callbacks (hooks).

Thread-safety: All methods are safe for concurrent use by multiple goroutines.

Usage patterns:

  1. Polling: Call GetSnapshot(), GetProviderMetrics(), or GetModelMetrics() periodically
  2. Streaming: Subscribe() to receive real-time events via a channel
  3. Callbacks: RegisterHook() to receive synchronous notifications for each event

Design principles:

  • Zero external dependencies (no OTEL/Prometheus in core)
  • Easy JSON serialization for all data types
  • Minimal memory footprint and CPU overhead
  • Flexible consumption patterns for different use cases

type MetricsHook added in v1.0.7

type MetricsHook interface {
	// OnEvent is called for each metrics event.
	// The context may have a timeout to prevent slow hooks from blocking.
	//
	// IMPORTANT: Keep this method fast (<1ms) to avoid blocking the metrics pipeline.
	// For slow operations, spawn a goroutine or use a subscription instead.
	//
	// Parameters:
	//   ctx: Context with potential timeout for hook execution
	//   event: The event being processed
	OnEvent(ctx context.Context, event MetricEvent)

	// Name returns a human-readable name for this hook (for logging/debugging).
	Name() string

	// Filter returns an optional filter for this hook.
	// If nil, the hook receives all events.
	// If non-nil, only matching events are delivered.
	//
	// This is more efficient than filtering in OnEvent() as it avoids
	// unnecessary hook invocations.
	Filter() *MetricFilter
}

MetricsHook is an interface for synchronous callbacks on metrics events. Hooks are invoked inline during event processing, so they should be fast.

For async/heavy processing, use subscriptions instead.

type MetricsSnapshot added in v1.0.7

type MetricsSnapshot struct {
	// Request metrics
	TotalRequests      int64   `json:"total_requests"`
	SuccessfulRequests int64   `json:"successful_requests"`
	FailedRequests     int64   `json:"failed_requests"`
	SuccessRate        float64 `json:"success_rate"` // Calculated: successful/total

	// Latency metrics
	Latency LatencyMetrics `json:"latency"`

	// Token usage metrics
	Tokens TokenMetrics `json:"tokens"`

	// Error metrics
	Errors ErrorMetrics `json:"errors"`

	// Streaming metrics (aggregated across all streaming requests)
	Streaming *StreamMetrics `json:"streaming,omitempty"`

	// Provider breakdown
	ProviderBreakdown map[string]*ProviderMetricsSnapshot `json:"provider_breakdown"`

	// Model breakdown
	ModelBreakdown map[string]*ModelMetricsSnapshot `json:"model_breakdown"`

	// Timestamps
	LastUpdated      time.Time `json:"last_updated"`
	FirstRequestTime time.Time `json:"first_request_time"`
	Uptime           int64     `json:"uptime_seconds"` // Seconds since first request
}

MetricsSnapshot represents a complete point-in-time snapshot of all aggregate metrics This is the top-level snapshot returned by GetSnapshot()

type MetricsSubscription added in v1.0.7

type MetricsSubscription interface {
	// Events returns the channel for receiving metrics events.
	// The channel is closed when the subscription is unsubscribed or the collector is closed.
	//
	// IMPORTANT: Must be continuously read to prevent blocking the collector.
	//
	// Example:
	//   for event := range subscription.Events() {
	//     processEvent(event)
	//   }
	Events() <-chan MetricEvent

	// Unsubscribe closes the subscription and stops event delivery.
	// The Events() channel will be closed.
	// Safe to call multiple times (idempotent).
	//
	// Should be called when the subscriber is done to free resources.
	// Recommended to use defer: defer subscription.Unsubscribe()
	Unsubscribe()

	// ID returns a unique identifier for this subscription.
	// Useful for debugging and logging.
	ID() string

	// OverflowCount returns the number of events that were dropped due to buffer overflow.
	// A non-zero value indicates the subscriber is not keeping up with event rate.
	//
	// If this value is increasing, consider:
	//  - Increasing the buffer size
	//  - Optimizing event processing logic
	//  - Using filtered subscriptions to reduce event volume
	OverflowCount() int64
}

MetricsSubscription represents an active subscription to metrics events. Subscriptions must be explicitly unsubscribed to prevent resource leaks.

type MockStandardStream

type MockStandardStream struct {
	// contains filtered or unexported fields
}

MockStandardStream implements StandardStream for testing

func NewMockStandardStream

func NewMockStandardStream(chunks []StandardStreamChunk) *MockStandardStream

NewMockStandardStream creates a new mock standard stream

func (*MockStandardStream) Close

func (m *MockStandardStream) Close() error

Close closes the mock stream

func (*MockStandardStream) Done

func (m *MockStandardStream) Done() bool

Done returns true if the mock stream is finished

func (*MockStandardStream) Next

Next returns the next chunk from the mock stream

type Model

type Model struct {
	ID                   string       `json:"id"`
	Name                 string       `json:"name"`
	Provider             ProviderType `json:"provider"`
	Description          string       `json:"description"`
	MaxTokens            int          `json:"max_tokens"`
	InputTokens          int          `json:"input_tokens"`
	OutputTokens         int          `json:"output_tokens"`
	SupportsStreaming    bool         `json:"supports_streaming"`
	SupportsToolCalling  bool         `json:"supports_tool_calling"`
	SupportsResponsesAPI bool         `json:"supports_responses_api"`
	Capabilities         []string     `json:"capabilities"`
	Pricing              Pricing      `json:"pricing"`
}

Model represents an AI model

type ModelCapabilityOverride added in v1.0.37

type ModelCapabilityOverride struct {
	MaxTokens         *int     `json:"max_tokens,omitempty"`
	ContextWindow     *int     `json:"context_window,omitempty"`
	SupportsStreaming *bool    `json:"supports_streaming,omitempty"`
	SupportsTools     *bool    `json:"supports_tools,omitempty"`
	SupportsVision    *bool    `json:"supports_vision,omitempty"`
	Capabilities      []string `json:"capabilities,omitempty"`
}

ModelCapabilityOverride allows users to override model capabilities

type ModelDiscoveryService

type ModelDiscoveryService struct {
	// contains filtered or unexported fields
}

ModelDiscoveryService only needs to discover models, so it depends only on ModelProvider

func NewModelDiscoveryService

func NewModelDiscoveryService() *ModelDiscoveryService

func (*ModelDiscoveryService) AddProvider

func (s *ModelDiscoveryService) AddProvider(provider ModelProvider)

func (*ModelDiscoveryService) GetAllModels

func (s *ModelDiscoveryService) GetAllModels(ctx context.Context) (map[string][]Model, error)

type ModelMetricsSnapshot added in v1.0.7

type ModelMetricsSnapshot struct {
	// Model identification
	ModelID      string       `json:"model_id"`
	Provider     string       `json:"provider"`      // Provider name (instance identifier)
	ProviderType ProviderType `json:"provider_type"` // Provider type

	// Request metrics
	TotalRequests      int64   `json:"total_requests"`
	SuccessfulRequests int64   `json:"successful_requests"`
	FailedRequests     int64   `json:"failed_requests"`
	SuccessRate        float64 `json:"success_rate"` // Calculated: successful/total

	// Latency metrics for this model
	Latency LatencyMetrics `json:"latency"`

	// Token usage for this model
	Tokens TokenMetrics `json:"tokens"`

	// Error metrics for this model
	Errors ErrorMetrics `json:"errors"`

	// Streaming metrics for this model
	Streaming *StreamMetrics `json:"streaming,omitempty"`

	// Model-specific metrics
	AverageTokensPerRequest float64 `json:"average_tokens_per_request"` // Calculated: total_tokens / requests
	EstimatedCostPerRequest float64 `json:"estimated_cost_per_request"` // Calculated: total_cost / requests

	// Timestamps
	LastRequestTime time.Time `json:"last_request_time"`
	LastUpdated     time.Time `json:"last_updated"`
}

ModelMetricsSnapshot represents per-model breakdown metrics Returned by GetModelMetrics(modelID)

type ModelProvider

type ModelProvider interface {
	// Model management
	GetModels(ctx context.Context) ([]Model, error)
	GetDefaultModel() string
}

ModelProvider defines methods for model discovery and management. This interface is for providers that expose multiple models.

type MultiPurposeService

type MultiPurposeService struct {
	// contains filtered or unexported fields
}

MultiPurposeService demonstrates how to compose multiple interfaces

func NewMultiPurposeService

func NewMultiPurposeService(provider Provider) *MultiPurposeService

func (*MultiPurposeService) GetHealth

func (s *MultiPurposeService) GetHealth(ctx context.Context) error

func (*MultiPurposeService) GetProviderInfo

func (s *MultiPurposeService) GetProviderInfo() string

func (*MultiPurposeService) SupportsTools

func (s *MultiPurposeService) SupportsTools() bool

type OAuthConfig

type OAuthConfig struct {
	ClientID     string    `json:"client_id"`
	ClientSecret string    `json:"client_secret"`
	RedirectURL  string    `json:"redirect_url,omitempty"`
	Scopes       []string  `json:"scopes"`
	AuthURL      string    `json:"auth_url,omitempty"`
	TokenURL     string    `json:"token_url,omitempty"`
	RefreshToken string    `json:"refresh_token,omitempty"`
	AccessToken  string    `json:"access_token,omitempty"`
	ExpiresAt    time.Time `json:"expires_at,omitempty"`

	// OnTokenRefresh is called when OAuth tokens are refreshed
	// Parameters: accessToken, refreshToken, expiresAt
	// The callback should return an error if token persistence fails
	OnTokenRefresh func(accessToken, refreshToken string, expiresAt time.Time) error `json:"-"`
}

OAuthConfig represents OAuth configuration

type OAuthCredentialSet

type OAuthCredentialSet struct {
	// Unique identifier for this credential set (e.g., "account-1", "team-account")
	ID string `json:"id"`

	// OAuth client credentials
	ClientID     string `json:"client_id"`
	ClientSecret string `json:"client_secret"`

	// OAuth tokens
	AccessToken  string    `json:"access_token"`
	RefreshToken string    `json:"refresh_token"`
	ExpiresAt    time.Time `json:"expires_at"`

	// OAuth scopes for this credential
	Scopes []string `json:"scopes,omitempty"`

	// Metadata for tracking
	LastRefresh  time.Time `json:"last_refresh,omitempty"`
	RefreshCount int       `json:"refresh_count"`

	// Callback for when tokens are refreshed
	// Parameters: id, accessToken, refreshToken, expiresAt
	// The callback should persist the new tokens and return an error if persistence fails
	OnTokenRefresh func(id, accessToken, refreshToken string, expiresAt time.Time) error `json:"-"`
}

OAuthCredentialSet represents a single set of OAuth credentials for multi-OAuth support This is used by the oauthmanager package for managing multiple OAuth credentials

type OAuthProvider added in v1.0.14

type OAuthProvider interface {
	// Embed the full Provider interface to maintain compatibility
	Provider

	// ValidateToken validates the current OAuth token and returns detailed token information
	// This method should check token expiration, scope validity, and retrieve user info
	ValidateToken(ctx context.Context) (*TokenInfo, error)

	// RefreshToken refreshes the OAuth token using the stored refresh token
	// This method should update the internal token state and persist the new tokens
	RefreshToken(ctx context.Context) error

	// GetAuthURL generates an OAuth authorization URL for re-authentication
	// redirectURI: The URI where the OAuth provider should redirect after authentication
	// state: A random string to prevent CSRF attacks
	// Returns the complete authorization URL that the user should be redirected to
	GetAuthURL(redirectURI string, state string) string
}

OAuthProvider extends the Provider interface with OAuth-specific authentication methods. This interface should be implemented by providers that use OAuth authentication and need token management capabilities.

func AsOAuthProvider added in v1.0.14

func AsOAuthProvider(provider Provider) (OAuthProvider, bool)

AsOAuthProvider safely casts a provider to OAuthProvider interface

type Pricing

type Pricing struct {
	InputTokenPrice  float64 `json:"input_token_price"`
	OutputTokenPrice float64 `json:"output_token_price"`
	Unit             string  `json:"unit"`
}

Pricing contains pricing information for a model

type Provider

Provider represents a complete AI provider with all capabilities. This interface composes all the smaller interfaces for backward compatibility.

When to use smaller interfaces: - Use CoreProvider when you only need basic provider information - Use ModelProvider when you only need to list or select models - Use AuthenticatedProvider when you only need authentication management - Use ConfigurableProvider when you only need to configure the provider - Use ChatProvider when you only need basic chat completion - Use ToolCallingProvider when you only need tool/function calling - Use CapabilityProvider when you only need to check provider capabilities - Use HealthCheckProvider when you only need health monitoring

This approach follows the Interface Segregation Principle, allowing clients to depend only on the methods they actually use.

type ProviderConfig

type ProviderConfig struct {
	Type           ProviderType           `json:"type"`
	Name           string                 `json:"name"`
	BaseURL        string                 `json:"base_url,omitempty"`
	APIKey         string                 `json:"api_key,omitempty"`
	APIKeyEnv      string                 `json:"api_key_env,omitempty"`
	DefaultModel   string                 `json:"default_model,omitempty"`
	Description    string                 `json:"description,omitempty"`
	ProviderConfig map[string]interface{} `json:"provider_config,omitempty"`

	// Multiple OAuth credentials for failover (multi-OAuth)
	OAuthCredentials []*OAuthCredentialSet `json:"oauth_credentials,omitempty"`

	// Model capability overrides - allows users to override model capabilities
	ModelCapabilities map[string]ModelCapabilityOverride `json:"model_capabilities,omitempty"`

	// Feature flags
	SupportsStreaming    bool `json:"supports_streaming"`
	SupportsToolCalling  bool `json:"supports_tool_calling"`
	SupportsResponsesAPI bool `json:"supports_responses_api"`

	// Limits and timeouts
	MaxTokens int           `json:"max_tokens,omitempty"`
	Timeout   time.Duration `json:"timeout,omitempty"`

	// Tool format
	ToolFormat ToolFormat `json:"tool_format,omitempty"`

	// Logging configuration
	EnableVerboseLogging bool `json:"enable_verbose_logging,omitempty"`
}

ProviderConfig represents configuration for a specific provider

type ProviderError added in v1.0.8

type ProviderError struct {
	Code        ErrorCode    // Categorized error code
	Message     string       // Human-readable message
	StatusCode  int          // HTTP status code (0 if not applicable)
	Provider    ProviderType // Which provider generated this error
	Operation   string       // What operation failed (e.g., "chat_completion", "list_models")
	OriginalErr error        // Wrapped original error
	RetryAfter  int          // Seconds to wait before retry (for rate limits)
	RequestID   string       // Provider's request ID if available
}

ProviderError represents a standardized error from a provider

func NewAuthError added in v1.0.8

func NewAuthError(provider ProviderType, message string) *ProviderError

NewAuthError creates a new authentication error

func NewContentFilterError added in v1.0.8

func NewContentFilterError(provider ProviderType, message string) *ProviderError

NewContentFilterError creates a new content filter error

func NewContextLengthError added in v1.0.8

func NewContextLengthError(provider ProviderType, message string) *ProviderError

NewContextLengthError creates a new context length error

func NewInvalidRequestError added in v1.0.8

func NewInvalidRequestError(provider ProviderType, message string) *ProviderError

NewInvalidRequestError creates a new invalid request error

func NewNetworkError added in v1.0.8

func NewNetworkError(provider ProviderType, message string) *ProviderError

NewNetworkError creates a new network error

func NewNotFoundError added in v1.0.8

func NewNotFoundError(provider ProviderType, message string) *ProviderError

NewNotFoundError creates a new not found error

func NewProviderError added in v1.0.8

func NewProviderError(provider ProviderType, code ErrorCode, message string) *ProviderError

NewProviderError creates a new ProviderError

func NewRateLimitError added in v1.0.8

func NewRateLimitError(provider ProviderType, retryAfter int) *ProviderError

NewRateLimitError creates a new rate limit error

func NewServerError added in v1.0.8

func NewServerError(provider ProviderType, statusCode int, message string) *ProviderError

NewServerError creates a new server error

func NewTimeoutError added in v1.0.8

func NewTimeoutError(provider ProviderType, message string) *ProviderError

NewTimeoutError creates a new timeout error

func (*ProviderError) Error added in v1.0.8

func (e *ProviderError) Error() string

Error implements the error interface

func (*ProviderError) IsRetryable added in v1.0.8

func (e *ProviderError) IsRetryable() bool

IsRetryable returns true if the error is potentially recoverable with retry

func (*ProviderError) Unwrap added in v1.0.8

func (e *ProviderError) Unwrap() error

Unwrap returns the original error for errors.Is/As

func (*ProviderError) WithOperation added in v1.0.8

func (e *ProviderError) WithOperation(operation string) *ProviderError

WithOperation sets the operation field and returns the error for chaining

func (*ProviderError) WithOriginalErr added in v1.0.8

func (e *ProviderError) WithOriginalErr(err error) *ProviderError

WithOriginalErr sets the original error field and returns the error for chaining

func (*ProviderError) WithRequestID added in v1.0.8

func (e *ProviderError) WithRequestID(requestID string) *ProviderError

WithRequestID sets the request ID field and returns the error for chaining

func (*ProviderError) WithRetryAfter added in v1.0.8

func (e *ProviderError) WithRetryAfter(retryAfter int) *ProviderError

WithRetryAfter sets the retry after field and returns the error for chaining

func (*ProviderError) WithStatusCode added in v1.0.8

func (e *ProviderError) WithStatusCode(statusCode int) *ProviderError

WithStatusCode sets the status code field and returns the error for chaining

type ProviderFactory

type ProviderFactory interface {
	RegisterProvider(providerType ProviderType, factoryFunc func(ProviderConfig) Provider)
	CreateProvider(providerType ProviderType, config ProviderConfig) (Provider, error)
	GetSupportedProviders() []ProviderType
}

ProviderFactory represents a factory for creating providers

type ProviderFactoryExtensions

type ProviderFactoryExtensions interface {
	ProviderFactory

	// Create a core provider adapter
	CreateCoreProvider(providerType ProviderType, config ProviderConfig) (CoreChatProvider, error)

	// Get supported provider types for core API
	GetSupportedCoreProviders() []ProviderType

	// Check if a provider type supports the core API
	SupportsCoreAPI(providerType ProviderType) bool
}

ProviderFactoryExtensions extends the existing ProviderFactory to work with the core API

type ProviderInfo

type ProviderInfo struct {
	Name           string       `json:"name"`
	Type           ProviderType `json:"type"`
	Description    string       `json:"description"`
	HealthStatus   HealthStatus `json:"health_status"`
	Models         []Model      `json:"models"`
	SupportedTools []string     `json:"supported_tools"`
	DefaultModel   string       `json:"default_model"`
}

ProviderInfo contains information about a provider

type ProviderInfoService

type ProviderInfoService struct {
	// contains filtered or unexported fields
}

ProviderInfoService only needs basic provider info, so it depends only on CoreProvider

func NewProviderInfoService

func NewProviderInfoService() *ProviderInfoService

func (*ProviderInfoService) AddProvider

func (s *ProviderInfoService) AddProvider(provider CoreProvider)

func (*ProviderInfoService) ListProviders

func (s *ProviderInfoService) ListProviders() []ProviderInfo

type ProviderMetrics

type ProviderMetrics struct {
	RequestCount    int64         `json:"request_count"`
	SuccessCount    int64         `json:"success_count"`
	ErrorCount      int64         `json:"error_count"`
	TotalLatency    time.Duration `json:"total_latency"`
	AverageLatency  time.Duration `json:"average_latency"`
	LastRequestTime time.Time     `json:"last_request_time"`
	LastSuccessTime time.Time     `json:"last_success_time"`
	LastErrorTime   time.Time     `json:"last_error_time"`
	LastError       string        `json:"last_error"`
	TokensUsed      int64         `json:"tokens_used"`
	HealthStatus    HealthStatus  `json:"health_status"`
}

ProviderMetrics represents metrics for a provider

type ProviderMetricsSnapshot added in v1.0.7

type ProviderMetricsSnapshot struct {
	// Provider identification
	Provider     string       `json:"provider"`
	ProviderType ProviderType `json:"provider_type"`

	// Request metrics
	TotalRequests      int64   `json:"total_requests"`
	SuccessfulRequests int64   `json:"successful_requests"`
	FailedRequests     int64   `json:"failed_requests"`
	SuccessRate        float64 `json:"success_rate"` // Calculated: successful/total

	// Latency metrics for this provider
	Latency LatencyMetrics `json:"latency"`

	// Token usage for this provider
	Tokens TokenMetrics `json:"tokens"`

	// Error metrics for this provider
	Errors ErrorMetrics `json:"errors"`

	// Streaming metrics for this provider
	Streaming *StreamMetrics `json:"streaming,omitempty"`

	// Provider-specific operations
	Initializations  int64   `json:"initializations"`
	HealthChecks     int64   `json:"health_checks"`
	HealthCheckFails int64   `json:"health_check_fails"`
	HealthCheckRate  float64 `json:"health_check_success_rate"` // Calculated: (checks - fails) / checks
	RateLimitHits    int64   `json:"rate_limit_hits"`

	// Model usage breakdown for this provider
	ModelUsage map[string]int64 `json:"model_usage"`

	// Timestamps
	LastRequestTime time.Time `json:"last_request_time"`
	LastUpdated     time.Time `json:"last_updated"`
}

ProviderMetricsSnapshot represents per-provider breakdown metrics Returned by GetProviderMetrics(providerType)

type ProviderType

type ProviderType string

ProviderType represents the type of AI provider

const (
	ProviderTypeOpenAI     ProviderType = "openai"
	ProviderTypeAnthropic  ProviderType = "anthropic"
	ProviderTypeGemini     ProviderType = "gemini"
	ProviderTypeQwen       ProviderType = "qwen"
	ProviderTypeCerebras   ProviderType = "cerebras"
	ProviderTypeOpenRouter ProviderType = "openrouter"
	ProviderTypeSynthetic  ProviderType = "synthetic"
	ProviderTypexAI        ProviderType = "xai"
	ProviderTypeFireworks  ProviderType = "fireworks"
	ProviderTypeDeepseek   ProviderType = "deepseek"
	ProviderTypeMistral    ProviderType = "mistral"
	ProviderTypeLMStudio   ProviderType = "lmstudio"
	ProviderTypeLlamaCpp   ProviderType = "llamacpp"
	ProviderTypeOllama     ProviderType = "ollama"

	// Virtual providers
	ProviderTypeRacing      ProviderType = "racing"
	ProviderTypeFallback    ProviderType = "fallback"
	ProviderTypeLoadBalance ProviderType = "loadbalance"
)

type RouterHealthStatus

type RouterHealthStatus struct {
	IsHealthy    bool          `json:"IsHealthy"`
	LastChecked  time.Time     `json:"LastChecked"`
	ErrorMessage string        `json:"ErrorMessage,omitempty"`
	ResponseTime time.Duration `json:"ResponseTime"`
}

RouterHealthStatus represents the health status of a provider (router-specific for compatibility)

type RouterMetrics

type RouterMetrics struct {
	TotalRequests       int64                       `json:"total_requests"`
	SuccessfulRequests  int64                       `json:"successful_requests"`
	FailedRequests      int64                       `json:"failed_requests"`
	ProviderUsage       map[string]int64            `json:"provider_usage"`
	AverageResponseTime time.Duration               `json:"average_response_time"`
	ProviderMetrics     map[string]*ProviderMetrics `json:"provider_metrics"`
	LastReset           time.Time                   `json:"last_reset"`
}

RouterMetrics represents metrics for the router

type RunningModel added in v1.0.36

type RunningModel struct {
	Name      string    `json:"name"`       // Model name/identifier
	Model     string    `json:"model"`      // Model identifier (may be same as Name)
	Size      int64     `json:"size"`       // Total model size in bytes
	Digest    string    `json:"digest"`     // Model digest/hash
	ExpiresAt time.Time `json:"expires_at"` // When the model will be unloaded from memory
	SizeVRAM  int64     `json:"size_vram"`  // Size of model in VRAM (GPU memory) in bytes
}

RunningModel represents a model that is currently loaded/running

type StandardChoice

type StandardChoice struct {
	Index        int         `json:"index"`
	Message      ChatMessage `json:"message"`
	FinishReason string      `json:"finish_reason"`
}

StandardChoice represents a choice in a standardized response

type StandardRequest

type StandardRequest struct {
	// Core content
	Messages []ChatMessage `json:"messages"`

	// Model selection
	Model string `json:"model,omitempty"`

	// Generation parameters
	MaxTokens   int      `json:"max_tokens,omitempty"`
	Temperature float64  `json:"temperature,omitempty"`
	Stop        []string `json:"stop,omitempty"`

	// Streaming control
	Stream bool `json:"stream"`

	// Tool support (if provider supports it)
	Tools      []Tool      `json:"tools,omitempty"`
	ToolChoice *ToolChoice `json:"tool_choice,omitempty"`

	// Response format (for providers that support structured output)
	ResponseFormat string `json:"response_format,omitempty"`

	// Context and metadata
	Context  context.Context        `json:"-"`
	Timeout  time.Duration          `json:"-"`
	Metadata map[string]interface{} `json:"metadata,omitempty"`
}

StandardRequest represents the core request format that all providers support This contains only the fields that are universally supported across all providers

func (*StandardRequest) ToGenerateOptions

func (r *StandardRequest) ToGenerateOptions() GenerateOptions

ToGenerateOptions converts from StandardRequest to legacy GenerateOptions

type StandardResponse

type StandardResponse struct {
	// Core response
	ID      string `json:"id"`
	Model   string `json:"model"`
	Object  string `json:"object"`
	Created int64  `json:"created"`

	// Content
	Choices []StandardChoice `json:"choices"`

	// Usage information
	Usage Usage `json:"usage"`

	// Provider-specific metadata
	ProviderMetadata map[string]interface{} `json:"provider_metadata,omitempty"`
}

StandardResponse represents the core response format that all providers return This contains only the fields that are universally supported across all providers

type StandardStream

type StandardStream interface {
	// Next returns the next chunk from the stream
	Next() (*StandardStreamChunk, error)

	// Close closes the stream
	Close() error

	// Done returns true if the stream is finished
	Done() bool
}

StandardStream represents a standardized streaming response

type StandardStreamAdapter

type StandardStreamAdapter struct {
	// contains filtered or unexported fields
}

StandardStreamAdapter adapts legacy streams to the standard stream interface

func (*StandardStreamAdapter) Close

func (s *StandardStreamAdapter) Close() error

Close closes the stream

func (*StandardStreamAdapter) Done

func (s *StandardStreamAdapter) Done() bool

Done returns true if the stream is finished

func (*StandardStreamAdapter) Next

Next returns the next chunk from the stream

type StandardStreamChoice

type StandardStreamChoice struct {
	Index        int         `json:"index"`
	Delta        ChatMessage `json:"delta"`
	FinishReason string      `json:"finish_reason,omitempty"`
}

StandardStreamChoice represents a choice in a streaming chunk

type StandardStreamChunk

type StandardStreamChunk struct {
	ID      string `json:"id"`
	Model   string `json:"model"`
	Object  string `json:"object"`
	Created int64  `json:"created"`

	// Content
	Choices []StandardStreamChoice `json:"choices"`

	// Usage information (may be nil for intermediate chunks)
	Usage *Usage `json:"usage,omitempty"`

	// Stream state
	Done bool `json:"done"`

	// Provider-specific metadata
	ProviderMetadata map[string]interface{} `json:"provider_metadata,omitempty"`
}

StandardStreamChunk represents a chunk in a streaming response

type StreamMetrics added in v1.0.7

type StreamMetrics struct {
	// Streaming request counts
	TotalStreamRequests      int64   `json:"total_stream_requests"`
	SuccessfulStreamRequests int64   `json:"successful_stream_requests"`
	FailedStreamRequests     int64   `json:"failed_stream_requests"`
	StreamSuccessRate        float64 `json:"stream_success_rate"` // Calculated: successful/total

	// Time to First Token (TTFT) metrics
	TimeToFirstToken TimeToFirstTokenMetrics `json:"time_to_first_token"`

	// Throughput metrics
	AverageTokensPerSecond float64 `json:"average_tokens_per_second"` // Calculated: total_tokens / total_duration
	MinTokensPerSecond     float64 `json:"min_tokens_per_second"`
	MaxTokensPerSecond     float64 `json:"max_tokens_per_second"`
	MedianTokensPerSecond  float64 `json:"median_tokens_per_second"`

	// Stream duration statistics
	AverageStreamDuration time.Duration `json:"average_stream_duration"`
	MinStreamDuration     time.Duration `json:"min_stream_duration"`
	MaxStreamDuration     time.Duration `json:"max_stream_duration"`

	// Token statistics for streaming
	TotalStreamedTokens    int64   `json:"total_streamed_tokens"`
	AverageTokensPerStream float64 `json:"average_tokens_per_stream"` // Calculated: total_streamed_tokens / successful_streams

	// Chunk statistics
	TotalChunks            int64   `json:"total_chunks"`
	AverageChunksPerStream float64 `json:"average_chunks_per_stream"` // Calculated: total_chunks / successful_streams
	AverageChunkSize       float64 `json:"average_chunk_size"`        // Calculated: total_streamed_tokens / total_chunks

	// Last updated
	LastUpdated time.Time `json:"last_updated"`
}

StreamMetrics represents streaming-specific metrics

type TestError added in v1.0.14

type TestError struct {
	ErrorType    TestErrorType `json:"error_type"`
	Message      string        `json:"message"`
	Phase        TestPhase     `json:"phase"`
	ProviderType ProviderType  `json:"provider_type"`
	Retryable    bool          `json:"retryable"`
	StatusCode   int           `json:"status_code,omitempty"`
	OriginalErr  string        `json:"original_err,omitempty"`
}

TestError represents a detailed error from provider testing

type TestErrorType added in v1.0.14

type TestErrorType string

TestErrorType represents the type of test error

const (
	TestErrorTypeAuth         TestErrorType = "auth_error"
	TestErrorTypeConnectivity TestErrorType = "connectivity_error"
	TestErrorTypeToken        TestErrorType = "token_error"
	TestErrorTypeOAuth        TestErrorType = "oauth_error"
	TestErrorTypeConfig       TestErrorType = "config_error"
	TestErrorTypeTimeout      TestErrorType = "timeout_error"
	TestErrorTypeRateLimit    TestErrorType = "rate_limit_error"
	TestErrorTypeServerError  TestErrorType = "server_error"
	TestErrorTypeNetwork      TestErrorType = "network_error"
	TestErrorTypeValidation   TestErrorType = "validation_error"
	TestErrorTypeUnknown      TestErrorType = "unknown_error"
)

type TestPhase added in v1.0.14

type TestPhase string

TestPhase represents the current phase of provider testing

const (
	TestPhaseAuthentication TestPhase = "authentication"
	TestPhaseConnectivity   TestPhase = "connectivity"
	TestPhaseConfiguration  TestPhase = "configuration"
	TestPhaseModelFetch     TestPhase = "model_fetch"
	TestPhaseCompleted      TestPhase = "completed"
	TestPhaseFailed         TestPhase = "failed"
)

type TestResult added in v1.0.14

type TestResult struct {
	Status       TestStatus        `json:"status"`
	Error        string            `json:"error,omitempty"`
	Details      map[string]string `json:"details,omitempty"`
	ModelsCount  int               `json:"models_count,omitempty"`
	Phase        TestPhase         `json:"phase"`
	Timestamp    time.Time         `json:"timestamp"`
	Duration     time.Duration     `json:"duration"`
	ProviderType ProviderType      `json:"provider_type"`
	TestError    *TestError        `json:"test_error,omitempty"`
}

TestResult represents the result of a provider test

func NewAuthErrorResult added in v1.0.14

func NewAuthErrorResult(providerType ProviderType, error string, duration time.Duration) *TestResult

NewAuthErrorResult creates a new authentication error test result

func NewConfigErrorResult added in v1.0.14

func NewConfigErrorResult(providerType ProviderType, error string, duration time.Duration) *TestResult

NewConfigErrorResult creates a new configuration error test result

func NewConnectivityErrorResult added in v1.0.14

func NewConnectivityErrorResult(providerType ProviderType, error string, duration time.Duration) *TestResult

NewConnectivityErrorResult creates a new connectivity error test result

func NewErrorResult added in v1.0.14

func NewErrorResult(providerType ProviderType, status TestStatus, error string, phase TestPhase, duration time.Duration) *TestResult

NewErrorResult creates a new error test result

func NewOAuthErrorResult added in v1.0.14

func NewOAuthErrorResult(providerType ProviderType, error string, duration time.Duration) *TestResult

NewOAuthErrorResult creates a new OAuth error test result

func NewRateLimitErrorResult added in v1.0.14

func NewRateLimitErrorResult(providerType ProviderType, error string, retryAfter int, duration time.Duration) *TestResult

NewRateLimitErrorResult creates a new rate limit error test result

func NewServerErrorResult added in v1.0.14

func NewServerErrorResult(providerType ProviderType, error string, statusCode int, duration time.Duration) *TestResult

NewServerErrorResult creates a new server error test result

func NewSuccessResult added in v1.0.14

func NewSuccessResult(providerType ProviderType, modelsCount int, duration time.Duration) *TestResult

NewSuccessResult creates a new successful test result

func NewTimeoutErrorResult added in v1.0.14

func NewTimeoutErrorResult(providerType ProviderType, error string, duration time.Duration) *TestResult

NewTimeoutErrorResult creates a new timeout error test result

func NewTokenErrorResult added in v1.0.14

func NewTokenErrorResult(providerType ProviderType, error string, duration time.Duration) *TestResult

NewTokenErrorResult creates a new token error test result

func NewUnknownErrorResult added in v1.0.14

func NewUnknownErrorResult(providerType ProviderType, error string, phase TestPhase, duration time.Duration) *TestResult

NewUnknownErrorResult creates a new unknown error test result

func TestResultFromJSON added in v1.0.14

func TestResultFromJSON(data []byte) (*TestResult, error)

TestResultFromJSON creates a TestResult from JSON data

func (*TestResult) GetDetail added in v1.0.14

func (tr *TestResult) GetDetail(key string) (string, bool)

GetDetail gets a detail value

func (*TestResult) GetErrorSummary added in v1.0.14

func (tr *TestResult) GetErrorSummary() string

GetErrorSummary returns a summary of the error for logging

func (*TestResult) IsError added in v1.0.14

func (tr *TestResult) IsError() bool

IsError returns true if the test result indicates an error

func (*TestResult) IsRetryable added in v1.0.14

func (tr *TestResult) IsRetryable() bool

IsRetryable returns true if the error is retryable

func (*TestResult) IsSuccess added in v1.0.14

func (tr *TestResult) IsSuccess() bool

IsSuccess returns true if the test result is successful

func (*TestResult) SetDetail added in v1.0.14

func (tr *TestResult) SetDetail(key, value string)

SetDetail sets a detail value

func (*TestResult) SetPhase added in v1.0.14

func (tr *TestResult) SetPhase(phase TestPhase)

SetPhase sets the test phase

func (*TestResult) ToJSON added in v1.0.14

func (tr *TestResult) ToJSON() ([]byte, error)

ToJSON converts the test result to JSON

func (*TestResult) ToJSONString added in v1.0.14

func (tr *TestResult) ToJSONString() (string, error)

ToJSONString converts the test result to a JSON string

func (*TestResult) WithDetail added in v1.0.14

func (tr *TestResult) WithDetail(key, value string) *TestResult

WithDetail adds a detail and returns the result for chaining

func (*TestResult) WithError added in v1.0.14

func (tr *TestResult) WithError(error string) *TestResult

WithError sets the error and returns the result for chaining

func (*TestResult) WithPhase added in v1.0.14

func (tr *TestResult) WithPhase(phase TestPhase) *TestResult

WithPhase sets the phase and returns the result for chaining

func (*TestResult) WithStatus added in v1.0.14

func (tr *TestResult) WithStatus(status TestStatus) *TestResult

WithStatus sets the status and returns the result for chaining

type TestStatus added in v1.0.14

type TestStatus string

TestStatus represents the status of a provider test

const (
	TestStatusSuccess            TestStatus = "success"
	TestStatusAuthFailed         TestStatus = "auth_failed"
	TestStatusConnectivityFailed TestStatus = "connectivity_failed"
	TestStatusOAuthFailed        TestStatus = "oauth_failed"
	TestStatusTokenFailed        TestStatus = "token_failed"
	TestStatusConfigFailed       TestStatus = "config_failed"
	TestStatusTimeoutFailed      TestStatus = "timeout_failed"
	TestStatusRateLimited        TestStatus = "rate_limited"
	TestStatusServerError        TestStatus = "server_error"
	TestStatusUnknownError       TestStatus = "unknown_error"
)

type TestableProvider added in v1.0.14

type TestableProvider interface {
	// Embed the full Provider interface to maintain compatibility
	Provider

	// TestConnectivity performs a connectivity test to verify the provider can reach its service
	// This method should perform a lightweight operation to verify network connectivity,
	// authentication, and basic service availability. It should not perform heavy operations
	// that might impact rate limits or incur costs.
	// Returns an error if connectivity cannot be established
	TestConnectivity(ctx context.Context) error
}

TestableProvider extends the Provider interface with connectivity testing capabilities. This interface should be implemented by providers that need to test their connection to the underlying service independently of health checks.

func AsTestableProvider added in v1.0.14

func AsTestableProvider(provider Provider) (TestableProvider, bool)

AsTestableProvider safely casts a provider to TestableProvider interface

type TimeToFirstTokenMetrics added in v1.0.7

type TimeToFirstTokenMetrics struct {
	// Summary statistics
	TotalMeasurements int64         `json:"total_measurements"`
	AverageTTFT       time.Duration `json:"average_ttft"`

	// Min/Max
	MinTTFT time.Duration `json:"min_ttft"`
	MaxTTFT time.Duration `json:"max_ttft"`

	// Percentiles for TTFT distribution
	P50TTFT time.Duration `json:"p50_ttft"` // Median
	P75TTFT time.Duration `json:"p75_ttft"`
	P90TTFT time.Duration `json:"p90_ttft"`
	P95TTFT time.Duration `json:"p95_ttft"`
	P99TTFT time.Duration `json:"p99_ttft"`

	// Last updated
	LastUpdated time.Time `json:"last_updated"`
}

TimeToFirstTokenMetrics represents TTFT (Time to First Token) statistics

type TokenInfo added in v1.0.14

type TokenInfo struct {
	// Valid indicates whether the token is currently valid
	Valid bool `json:"valid"`

	// ExpiresAt indicates when the token expires
	ExpiresAt time.Time `json:"expires_at"`

	// Scope contains the OAuth scopes granted to the token
	Scope []string `json:"scope"`

	// UserInfo contains user information from the OAuth provider
	UserInfo map[string]interface{} `json:"user_info"`
}

TokenInfo represents authentication token information for OAuth providers This structure provides comprehensive token validation and metadata

func (*TokenInfo) HasScope added in v1.0.14

func (ti *TokenInfo) HasScope(scope string) bool

HasScope returns true if the token includes the specified scope

func (*TokenInfo) IsExpired added in v1.0.14

func (ti *TokenInfo) IsExpired() bool

IsExpired returns true if the token has expired

type TokenMetrics

type TokenMetrics struct {
	// Total token counts
	TotalTokens  int64 `json:"total_tokens"`
	InputTokens  int64 `json:"input_tokens"`
	OutputTokens int64 `json:"output_tokens"`

	// Cached token usage (for providers that support prompt caching)
	CachedTokens        int64 `json:"cached_tokens,omitempty"`
	CacheReadTokens     int64 `json:"cache_read_tokens,omitempty"`
	CacheCreationTokens int64 `json:"cache_creation_tokens,omitempty"`

	// Reasoning tokens (for models with extended thinking)
	ReasoningTokens int64 `json:"reasoning_tokens,omitempty"`

	// Average token usage per request
	AverageInputTokens  float64 `json:"average_input_tokens"`  // Calculated: input_tokens / requests
	AverageOutputTokens float64 `json:"average_output_tokens"` // Calculated: output_tokens / requests
	AverageTotalTokens  float64 `json:"average_total_tokens"`  // Calculated: total_tokens / requests

	// Token efficiency metrics
	InputOutputRatio float64 `json:"input_output_ratio"`       // Calculated: input_tokens / output_tokens
	CacheHitRate     float64 `json:"cache_hit_rate,omitempty"` // Calculated: cache_read_tokens / input_tokens

	// Cost estimation
	EstimatedCost       float64 `json:"estimated_cost"`
	Currency            string  `json:"currency"`
	EstimatedInputCost  float64 `json:"estimated_input_cost"`
	EstimatedOutputCost float64 `json:"estimated_output_cost"`
	EstimatedCachedCost float64 `json:"estimated_cached_cost,omitempty"`

	// Cost per token (for reference)
	InputCostPerToken  float64 `json:"input_cost_per_token,omitempty"`
	OutputCostPerToken float64 `json:"output_cost_per_token,omitempty"`
	CachedCostPerToken float64 `json:"cached_cost_per_token,omitempty"`

	// Last updated
	LastUpdated time.Time `json:"last_updated"`
}

TokenMetrics represents comprehensive token usage breakdown

type TokenStorage

type TokenStorage interface {
	StoreToken(key string, token *OAuthConfig) error
	RetrieveToken(key string) (*OAuthConfig, error)
	DeleteToken(key string) error
	ListTokens() ([]string, error)
}

TokenStorage represents a token storage interface

type Tool

type Tool struct {
	Name        string                 `json:"name"`
	Description string                 `json:"description"`
	InputSchema map[string]interface{} `json:"input_schema"`
}

Tool represents an available tool

type ToolCall

type ToolCall struct {
	ID       string                 `json:"id"`
	Type     string                 `json:"type"`
	Function ToolCallFunction       `json:"function"`
	Metadata map[string]interface{} `json:"metadata,omitempty"`
}

ToolCall represents a tool call

type ToolCallFunction

type ToolCallFunction struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"`
}

ToolCallFunction represents a tool call function

type ToolCallingProvider

type ToolCallingProvider interface {
	// Tool invocation
	InvokeServerTool(ctx context.Context, toolName string, params interface{}) (interface{}, error)

	// Tool format support
	SupportsToolCalling() bool
	GetToolFormat() ToolFormat
}

ToolCallingProvider defines methods for tool/function calling capabilities. This interface is for providers that support external tool invocation.

type ToolChoice

type ToolChoice struct {
	Mode         ToolChoiceMode `json:"mode"`
	FunctionName string         `json:"function_name,omitempty"` // For "specific" mode
}

ToolChoice represents fine-grained control over tool selection

type ToolChoiceMode

type ToolChoiceMode string

ToolChoiceMode represents the mode for tool choice control

const (
	ToolChoiceAuto     ToolChoiceMode = "auto"     // Model decides whether to use tools
	ToolChoiceRequired ToolChoiceMode = "required" // Must use a tool
	ToolChoiceNone     ToolChoiceMode = "none"     // Don't use tools
	ToolChoiceSpecific ToolChoiceMode = "specific" // Force specific tool
)

type ToolExecutionService

type ToolExecutionService struct {
	// contains filtered or unexported fields
}

ToolExecutionService only needs to call tools, so it depends only on ToolCallingProvider

func NewToolExecutionService

func NewToolExecutionService(provider ToolCallingProvider) *ToolExecutionService

func (*ToolExecutionService) ExecuteTool

func (s *ToolExecutionService) ExecuteTool(ctx context.Context, toolName string, params interface{}) (interface{}, error)

type ToolFormat

type ToolFormat string

ToolFormat represents the format used for tool calling

const (
	ToolFormatOpenAI    ToolFormat = "openai"
	ToolFormatAnthropic ToolFormat = "anthropic"
	ToolFormatGemini    ToolFormat = "gemini"
	ToolFormatXML       ToolFormat = "xml"
	ToolFormatHermes    ToolFormat = "hermes"
	ToolFormatText      ToolFormat = "text"
)

type Usage

type Usage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

Usage represents token usage information

type ValidationError

type ValidationError struct {
	Message string
}

ValidationError represents a validation error

func NewValidationError

func NewValidationError(message string) *ValidationError

func (*ValidationError) Error

func (e *ValidationError) Error() string

Jump to

Keyboard shortcuts

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