types

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2025 License: MIT Imports: 6 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.

Index

Constants

This section is empty.

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 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 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 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"`
	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

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 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 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 EnhancedRouterMetrics

type EnhancedRouterMetrics struct {
	TotalRequests      int64 `json:"TotalRequests"`
	SuccessfulRequests int64 `json:"SuccessfulRequests"`
	FailedRequests     int64 `json:"FailedRequests"`
	FallbackAttempts   int64 `json:"FallbackAttempts"`
}

EnhancedRouterMetrics holds router performance metrics (for compatibility with enhanced router)

type ErrorMetrics

type ErrorMetrics struct {
	TotalErrors      int64            `json:"total_errors"`
	ErrorTypes       map[string]int64 `json:"error_types"`
	LastError        string           `json:"last_error"`
	LastErrorTime    time.Time        `json:"last_error_time"`
	ConsecutiveFails int64            `json:"consecutive_fails"`
}

ErrorMetrics represents error-related metrics

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 HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
	Get(url string) (*http.Response, error)
	Post(url string, bodyType string, body io.Reader) (*http.Response, error)
}

HTTPClient represents an HTTP client interface

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 LatencyTracker

type LatencyTracker interface {
	Add(latency time.Duration)
	GetPercentiles() (min, p50, p95, p99, max time.Duration)
}

LatencyTracker represents a latency tracking interface (for compatibility)

type Logger

type Logger interface {
	Debug(msg string, fields ...interface{})
	Info(msg string, fields ...interface{})
	Warn(msg string, fields ...interface{})
	Error(msg string, fields ...interface{})
	Fatal(msg string, fields ...interface{})
	WithField(key string, value interface{}) Logger
	WithFields(fields map[string]interface{}) Logger
}

Logger represents a logger interface

type MetricsTracker

type MetricsTracker interface {
	GetMetrics() ProviderMetricsInfo
	RecordRequest(success bool, latency time.Duration, usage *Usage)
}

MetricsTracker represents a metrics tracking interface (for compatibility)

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 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 ModelMetrics

type ModelMetrics struct {
	ModelID         string        `json:"model_id"`
	Provider        ProviderType  `json:"provider"`
	RequestCount    int64         `json:"request_count"`
	SuccessCount    int64         `json:"success_count"`
	ErrorCount      int64         `json:"error_count"`
	TokensUsed      int64         `json:"tokens_used"`
	AverageLatency  time.Duration `json:"average_latency"`
	LastRequestTime time.Time     `json:"last_request_time"`
	P95ResponseTime time.Duration `json:"p95_response_time"`
	P99ResponseTime time.Duration `json:"p99_response_time"`
}

ModelMetrics represents metrics for a specific model

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 Options

type Options interface {
	Get(key string) (interface{}, bool)
	Set(key string, value interface{})
	GetString(key string) string
	GetInt(key string) int
	GetBool(key string) bool
	GetDuration(key string) time.Duration
	GetStringSlice(key string) []string
}

Options represents configuration options for a provider

type OverallLatencyMetrics

type OverallLatencyMetrics struct {
	MinLatency time.Duration `json:"MinLatency"`
	P50Latency time.Duration `json:"P50Latency"`
	P95Latency time.Duration `json:"P95Latency"`
	P99Latency time.Duration `json:"P99Latency"`
	MaxLatency time.Duration `json:"MaxLatency"`
}

OverallLatencyMetrics represents overall latency percentiles (for compatibility)

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 StreamingProvider when you only need to check streaming support - Use ResponsesAPIProvider when you only need responses API support - 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"`

	// 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"`
}

ProviderConfig represents configuration for a specific provider

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 ProviderMetricsInfo

type ProviderMetricsInfo struct {
	Name            string        `json:"name"`
	IsModel         bool          `json:"is_model"`
	RequestCount    int64         `json:"request_count"`
	SuccessCount    int64         `json:"success_count"`
	ErrorCount      int64         `json:"error_count"`
	AverageLatency  time.Duration `json:"average_latency"`
	LastRequestTime time.Time     `json:"last_request_time"`
	TokensUsed      int64         `json:"tokens_used"`
}

ProviderMetricsInfo represents detailed provider metrics for compatibility with router

type ProviderRegistry

type ProviderRegistry interface {
	Register(provider Provider) error
	Unregister(name string) error
	Get(name string) (Provider, error)
	List() []Provider
	ListByType(providerType ProviderType) []Provider
	GetAvailable() []Provider
	GetHealthy() []Provider
}

ProviderRegistry represents a registry of providers

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 ResponsesAPIProvider

type ResponsesAPIProvider interface {
	// Responses API support
	SupportsResponsesAPI() bool
}

ResponsesAPIProvider defines support for structured responses API. This interface is for providers that support structured response formats.

type Router

type Router interface {
	SelectProvider(prompt string, options interface{}) (Provider, error)
	GetAvailableProviders() []ProviderInfo
	GetProvider(name string) (Provider, error)
	SetPreference(providerName string) error
}

Router represents a router for provider selection

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 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 StreamingProvider

type StreamingProvider interface {
	// Streaming support
	SupportsStreaming() bool
}

StreamingProvider defines streaming capabilities. This interface is for providers that support real-time streaming responses.

type TokenMetrics

type TokenMetrics struct {
	InputTokens   int64     `json:"input_tokens"`
	OutputTokens  int64     `json:"output_tokens"`
	TotalTokens   int64     `json:"total_tokens"`
	EstimatedCost float64   `json:"estimated_cost"`
	Currency      string    `json:"currency"`
	LastUpdated   time.Time `json:"last_updated"`
}

TokenMetrics represents token usage metrics

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"
	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