Documentation
¶
Overview ¶
Package common provides shared utilities for AI provider implementations
Package common provides shared utilities and infrastructure for AI provider implementations. This includes standardized error handling, authentication helpers, configuration management, health checking, metrics collection, and other common functionality across providers.
Package common provides shared utilities and helper functions for AI providers. It includes file operations, configuration helpers, and other common functionality used across different provider implementations.
Package common provides shared utilities and helper functions for AI provider implementations ¶
Package common provides shared utilities for AI provider implementations.
Package common provides shared utilities for AI provider implementations.
Index ¶
- Variables
- func CreateAnthropicStream(response *http.Response) types.ChatCompletionStream
- func CreateCustomStream(response *http.Response, parser StreamParser) types.ChatCompletionStream
- func CreateErrorStream(err error) types.ChatCompletionStream
- func CreateOpenAIStream(response *http.Response) types.ChatCompletionStream
- func GetModelCacheTTL(providerType types.ProviderType) time.Duration
- func GetStaticFallback(providerType types.ProviderType) []types.Model
- func JSONLineProcessor(data string, target interface{}, ...) (types.ChatCompletionChunk, error, bool)
- func ReadConfigFile(configPath string) ([]byte, error)
- func SSELineProcessor(data string, responseParser func(string) (types.ChatCompletionChunk, bool)) (types.ChatCompletionChunk, error, bool)
- func StreamFromContext(ctx context.Context, baseStream types.ChatCompletionStream) types.ChatCompletionStream
- type APIError
- type APIErrorType
- type AnthropicStreamParser
- type AuthHelper
- func (h *AuthHelper) ClearAuthentication()
- func (h *AuthHelper) ExecuteWithAuth(ctx context.Context, options types.GenerateOptions, ...) (string, *types.Usage, error)
- func (h *AuthHelper) GetAuthMethod() string
- func (h *AuthHelper) GetAuthStatus() map[string]interface{}
- func (h *AuthHelper) HandleAuthError(err error, statusCode int) error
- func (h *AuthHelper) IsAuthenticated() bool
- func (h *AuthHelper) MakeAuthenticatedRequest(ctx context.Context, method, url string, headers map[string]string, ...) (*http.Response, error)
- func (h *AuthHelper) RefreshAllOAuthTokens(ctx context.Context) error
- func (h *AuthHelper) SetAuthHeaders(req *http.Request, authToken string, authType string)
- func (h *AuthHelper) SetProviderSpecificHeaders(req *http.Request)
- func (h *AuthHelper) SetupAPIKeys()
- func (h *AuthHelper) SetupOAuth(refreshFunc oauthmanager.RefreshFunc)
- func (h *AuthHelper) ValidateAuthConfig(authConfig types.AuthConfig) error
- type BaseStream
- type CacheConfig
- type CacheInfo
- type ConfigHelper
- func (h *ConfigHelper) ApplyTopLevelOverrides(config types.ProviderConfig, providerConfig interface{}) error
- func (h *ConfigHelper) ConfigSummary(config types.ProviderConfig) map[string]interface{}
- func (h *ConfigHelper) ExtractAPIKeys(config types.ProviderConfig) []string
- func (h *ConfigHelper) ExtractBaseURL(config types.ProviderConfig) string
- func (h *ConfigHelper) ExtractDefaultModel(config types.ProviderConfig) string
- func (h *ConfigHelper) ExtractDefaultOAuthClientID() string
- func (h *ConfigHelper) ExtractMaxTokens(config types.ProviderConfig) int
- func (h *ConfigHelper) ExtractProviderSpecificConfig(config types.ProviderConfig, target interface{}) error
- func (h *ConfigHelper) ExtractStringField(config types.ProviderConfig, fieldName, fallback string) string
- func (h *ConfigHelper) ExtractStringSliceField(config types.ProviderConfig, fieldName string) []string
- func (h *ConfigHelper) ExtractTimeout(config types.ProviderConfig) time.Duration
- func (h *ConfigHelper) GetProviderCapabilities() (supportsToolCalling, supportsStreaming, supportsResponsesAPI bool)
- func (h *ConfigHelper) MergeWithDefaults(config types.ProviderConfig) types.ProviderConfig
- func (h *ConfigHelper) SanitizeConfigForLogging(config types.ProviderConfig) types.ProviderConfig
- func (h *ConfigHelper) ValidateProviderConfig(config types.ProviderConfig) ValidationResult
- type ContextAwareStream
- type CostInfo
- type ErrorClassifier
- type ErrorStream
- type ErrorSummary
- type ErrorType
- type GenericSSEStream
- type HealthCheckCallback
- type HealthCheckResult
- type HealthChecker
- func (hc *HealthChecker) AddCallback(callback HealthCheckCallback)
- func (hc *HealthChecker) CheckProvider(ctx context.Context, provider *InitializedProvider) error
- func (hc *HealthChecker) GetAllHealthStatus() map[types.ProviderType]ProviderHealth
- func (hc *HealthChecker) GetHealthStatus(providerType types.ProviderType) ProviderHealth
- func (hc *HealthChecker) GetHealthSummary() HealthSummary
- func (hc *HealthChecker) GetHealthyProviders() []types.ProviderType
- func (hc *HealthChecker) GetUnhealthyProviders() []types.ProviderType
- func (hc *HealthChecker) IsHealthy(providerType types.ProviderType) bool
- func (hc *HealthChecker) ResetHealthStatus(providerType types.ProviderType)
- func (hc *HealthChecker) Start()
- func (hc *HealthChecker) Stop()
- type HealthSummary
- type InitializedProvider
- type InitializerConfig
- type MetricsSnapshot
- type MockStream
- type ModelCache
- func (mc *ModelCache) Clear()
- func (mc *ModelCache) Get() []types.Model
- func (mc *ModelCache) GetModels(fetchFunc func() ([]types.Model, error), fallbackFunc func() []types.Model) ([]types.Model, error)
- func (mc *ModelCache) GetTTL() time.Duration
- func (mc *ModelCache) GetTimestamp() time.Time
- func (mc *ModelCache) IsStale() bool
- func (mc *ModelCache) SetTTL(ttl time.Duration)
- func (mc *ModelCache) Update(models []types.Model)
- type ModelCapabilities
- type ModelCapability
- type ModelMetadata
- type ModelMetadataRegistry
- func (r *ModelMetadataRegistry) Clear()
- func (r *ModelMetadataRegistry) EnrichModel(model *types.Model) *types.Model
- func (r *ModelMetadataRegistry) EnrichModels(models []types.Model) []types.Model
- func (r *ModelMetadataRegistry) GetAllModelIDs() []string
- func (r *ModelMetadataRegistry) GetMetadata(modelID string) *ModelMetadata
- func (r *ModelMetadataRegistry) RegisterBulkMetadata(entries map[string]*ModelMetadata)
- func (r *ModelMetadataRegistry) RegisterMetadata(modelID string, metadata *ModelMetadata)
- type ModelRegistry
- func (mr *ModelRegistry) CacheModels(providerType types.ProviderType, models []types.Model)
- func (mr *ModelRegistry) ClearCache(providerType *types.ProviderType)
- func (mr *ModelRegistry) GetCacheInfo() CacheInfo
- func (mr *ModelRegistry) GetCachedModels(providerType types.ProviderType) []types.Model
- func (mr *ModelRegistry) GetModelCapability(modelID string) *ModelCapability
- func (mr *ModelRegistry) GetModelsByProvider(providerType types.ProviderType) []types.Model
- func (mr *ModelRegistry) GetProviderCount() int
- func (mr *ModelRegistry) GetTotalModelCount() int
- func (mr *ModelRegistry) RegisterModel(modelID string, capability *ModelCapability)
- func (mr *ModelRegistry) SearchModels(criteria SearchCriteria) []types.Model
- type ModelUsage
- type OAuthRefreshHelper
- func (h *OAuthRefreshHelper) AnthropicOAuthRefresh(ctx context.Context, cred *types.OAuthCredentialSet) (*types.OAuthCredentialSet, error)
- func (h *OAuthRefreshHelper) GeminiOAuthRefresh(ctx context.Context, cred *types.OAuthCredentialSet) (*types.OAuthCredentialSet, error)
- func (h *OAuthRefreshHelper) GenericOAuthRefresh(ctx context.Context, cred *types.OAuthCredentialSet, tokenURL string) (*types.OAuthCredentialSet, error)
- func (h *OAuthRefreshHelper) OpenAIOAuthRefresh(ctx context.Context, cred *types.OAuthCredentialSet) (*types.OAuthCredentialSet, error)
- func (h *OAuthRefreshHelper) QwenOAuthRefresh(ctx context.Context, cred *types.OAuthCredentialSet) (*types.OAuthCredentialSet, error)
- type OpenAICompatibleParser
- type ProcessLineFunc
- type ProviderHealth
- type ProviderInitializer
- type ProviderMetrics
- func (pm *ProviderMetrics) GetErrorSummary() ErrorSummary
- func (pm *ProviderMetrics) GetMetricsForProvider(providerType types.ProviderType) ProviderMetricsSnapshot
- func (pm *ProviderMetrics) GetSnapshot() MetricsSnapshot
- func (pm *ProviderMetrics) GetTopModels(limit int) []ModelUsage
- func (pm *ProviderMetrics) RecordError(providerType types.ProviderType, errorType string)
- func (pm *ProviderMetrics) RecordHealthCheck(providerType types.ProviderType, success bool)
- func (pm *ProviderMetrics) RecordInitialization(providerType types.ProviderType)
- func (pm *ProviderMetrics) RecordRateLimitHit(providerType types.ProviderType)
- func (pm *ProviderMetrics) RecordRequest(providerType types.ProviderType)
- func (pm *ProviderMetrics) RecordSuccess(providerType types.ProviderType, responseTime time.Duration, tokens int64, ...)
- func (pm *ProviderMetrics) Reset()
- type ProviderMetricsSnapshot
- type ProviderStatus
- type RateLimitHelper
- func (h *RateLimitHelper) CanMakeRequest(model string, estimatedTokens int) bool
- func (h *RateLimitHelper) CheckRateLimitAndWait(model string, estimatedTokens int) bool
- func (h *RateLimitHelper) GetParser() ratelimit.Parser
- func (h *RateLimitHelper) GetRateLimitInfo(model string) (*ratelimit.Info, bool)
- func (h *RateLimitHelper) GetTracker() *ratelimit.Tracker
- func (h *RateLimitHelper) GetWaitTime(model string) time.Duration
- func (h *RateLimitHelper) ParseAndUpdateRateLimits(headers http.Header, model string)
- func (h *RateLimitHelper) ShouldThrottle(model string, threshold float64) bool
- func (h *RateLimitHelper) UpdateRateLimitInfo(info *ratelimit.Info)
- type RateLimitInfo
- type RateLimitTracker
- type RefreshFuncFactory
- func (f *RefreshFuncFactory) CreateAnthropicRefreshFunc() oauthmanager.RefreshFunc
- func (f *RefreshFuncFactory) CreateGeminiRefreshFunc() oauthmanager.RefreshFunc
- func (f *RefreshFuncFactory) CreateGenericRefreshFunc(tokenURL string) oauthmanager.RefreshFunc
- func (f *RefreshFuncFactory) CreateOpenAIRefreshFunc() oauthmanager.RefreshFunc
- func (f *RefreshFuncFactory) CreateQwenRefreshFunc() oauthmanager.RefreshFunc
- type SSELineParser
- type SearchCriteria
- type StandardStreamParser
- type StreamParser
- type StreamProcessor
- type ValidationResult
Constants ¶
This section is empty.
Variables ¶
var AnthropicFallbackModels = []types.Model{ {ID: "claude-opus-4-5-20251101", Name: "Claude Opus 4.5", Provider: types.ProviderTypeAnthropic, MaxTokens: 200000, SupportsStreaming: true, SupportsToolCalling: true, Description: "Claude Opus 4.5 - Most powerful model for complex reasoning"}, {ID: "claude-opus-4-5", Name: "Claude Opus 4.5", Provider: types.ProviderTypeAnthropic, MaxTokens: 200000, SupportsStreaming: true, SupportsToolCalling: true, Description: "Claude Opus 4.5 - Most powerful model for complex reasoning"}, {ID: "claude-opus-4-1-20250805", Name: "Claude Opus 4.1", Provider: types.ProviderTypeAnthropic, MaxTokens: 200000, SupportsStreaming: true, SupportsToolCalling: true, Description: "Claude Opus 4.1 - Advanced reasoning model"}, {ID: "claude-opus-4-1", Name: "Claude Opus 4.1", Provider: types.ProviderTypeAnthropic, MaxTokens: 200000, SupportsStreaming: true, SupportsToolCalling: true, Description: "Claude Opus 4.1 - Advanced reasoning model"}, {ID: "claude-sonnet-4-5-20250929", Name: "Claude Sonnet 4.5", Provider: types.ProviderTypeAnthropic, MaxTokens: 200000, SupportsStreaming: true, SupportsToolCalling: true, Description: "Claude Sonnet 4.5 - Best balance of intelligence and speed"}, {ID: "claude-sonnet-4-5", Name: "Claude Sonnet 4.5", Provider: types.ProviderTypeAnthropic, MaxTokens: 200000, SupportsStreaming: true, SupportsToolCalling: true, Description: "Claude Sonnet 4.5 - Best balance of intelligence and speed"}, {ID: "claude-sonnet-4-20250514", Name: "Claude Sonnet 4", Provider: types.ProviderTypeAnthropic, MaxTokens: 200000, SupportsStreaming: true, SupportsToolCalling: true, Description: "Claude Sonnet 4 - Balanced performance model"}, {ID: "claude-sonnet-4", Name: "Claude Sonnet 4", Provider: types.ProviderTypeAnthropic, MaxTokens: 200000, SupportsStreaming: true, SupportsToolCalling: true, Description: "Claude Sonnet 4 - Balanced performance model"}, {ID: "claude-haiku-4-5-20251001", Name: "Claude Haiku 4.5", Provider: types.ProviderTypeAnthropic, MaxTokens: 200000, SupportsStreaming: true, SupportsToolCalling: true, Description: "Claude Haiku 4.5 - Fastest model for quick tasks"}, {ID: "claude-haiku-4-5", Name: "Claude Haiku 4.5", Provider: types.ProviderTypeAnthropic, MaxTokens: 200000, SupportsStreaming: true, SupportsToolCalling: true, Description: "Claude Haiku 4.5 - Fastest model for quick tasks"}, {ID: "claude-3-5-sonnet-20241022", Name: "Claude 3.5 Sonnet (Oct 2024)", Provider: types.ProviderTypeAnthropic, MaxTokens: 200000, SupportsStreaming: true, SupportsToolCalling: true, Description: "Anthropic's most capable Sonnet model, updated for October 2024"}, {ID: "claude-3-5-haiku-20241022", Name: "Claude 3.5 Haiku (Oct 2024)", Provider: types.ProviderTypeAnthropic, MaxTokens: 200000, SupportsStreaming: true, SupportsToolCalling: true, Description: "Anthropic's fastest Haiku model, updated for October 2024"}, {ID: "claude-3-opus-20240229", Name: "Claude 3 Opus", Provider: types.ProviderTypeAnthropic, MaxTokens: 200000, SupportsStreaming: true, SupportsToolCalling: true, Description: "Anthropic's most powerful model for complex tasks"}, {ID: "claude-3-sonnet-20240229", Name: "Claude 3 Sonnet", Provider: types.ProviderTypeAnthropic, MaxTokens: 200000, SupportsStreaming: true, SupportsToolCalling: true, Description: "Anthropic's balanced model for workloads"}, {ID: "claude-3-haiku-20240307", Name: "Claude 3 Haiku", Provider: types.ProviderTypeAnthropic, MaxTokens: 200000, SupportsStreaming: true, SupportsToolCalling: true, Description: "Anthropic's fastest and most compact model"}, }
AnthropicFallbackModels contains static fallback models for Anthropic
var DefaultCacheConfigs = map[types.ProviderType]CacheConfig{ types.ProviderTypeAnthropic: {ModelCacheTTL: 6 * time.Hour}, types.ProviderTypeOpenAI: {ModelCacheTTL: 24 * time.Hour}, types.ProviderTypeGemini: {ModelCacheTTL: 2 * time.Hour}, types.ProviderTypeCerebras: {ModelCacheTTL: 6 * time.Hour}, types.ProviderTypeQwen: {ModelCacheTTL: 6 * time.Hour}, types.ProviderTypeOpenRouter: {ModelCacheTTL: 12 * time.Hour}, }
DefaultCacheConfigs maps provider types to their default cache configs
var GeminiFallbackModels = []types.Model{ {ID: "gemini-3-pro-preview", Name: "Gemini 3 Pro (Preview)", Provider: types.ProviderTypeGemini, MaxTokens: 2097152, SupportsStreaming: true, SupportsToolCalling: true, Description: "Google's latest Gemini 3 Pro model with advanced capabilities"}, {ID: "gemini-3-pro-image-preview", Name: "Gemini 3 Pro Image (Preview)", Provider: types.ProviderTypeGemini, MaxTokens: 2097152, SupportsStreaming: true, SupportsToolCalling: true, Description: "Gemini 3 Pro with enhanced image understanding"}, {ID: "gemini-2.5-pro", Name: "Gemini 2.5 Pro", Provider: types.ProviderTypeGemini, MaxTokens: 2097152, SupportsStreaming: true, SupportsToolCalling: true, Description: "Stable Gemini 2.5 Pro model with 2M token context"}, {ID: "gemini-2.5-flash", Name: "Gemini 2.5 Flash", Provider: types.ProviderTypeGemini, MaxTokens: 1048576, SupportsStreaming: true, SupportsToolCalling: true, Description: "Fast and efficient Gemini 2.5 Flash model"}, {ID: "gemini-2.5-flash-image", Name: "Gemini 2.5 Flash Image", Provider: types.ProviderTypeGemini, MaxTokens: 1048576, SupportsStreaming: true, SupportsToolCalling: true, Description: "Gemini 2.5 Flash optimized for image tasks"}, {ID: "gemini-2.5-flash-lite", Name: "Gemini 2.5 Flash Lite", Provider: types.ProviderTypeGemini, MaxTokens: 524288, SupportsStreaming: true, SupportsToolCalling: true, Description: "Lightweight version of Gemini 2.5 Flash"}, {ID: "gemini-2.0-flash", Name: "Gemini 2.0 Flash", Provider: types.ProviderTypeGemini, MaxTokens: 1048576, SupportsStreaming: true, SupportsToolCalling: true, Description: "Stable Gemini 2.0 Flash model"}, {ID: "gemini-2.0-flash-001", Name: "Gemini 2.0 Flash 001", Provider: types.ProviderTypeGemini, MaxTokens: 1048576, SupportsStreaming: true, SupportsToolCalling: true, Description: "Gemini 2.0 Flash version 001"}, {ID: "gemini-2.0-flash-lite", Name: "Gemini 2.0 Flash Lite", Provider: types.ProviderTypeGemini, MaxTokens: 524288, SupportsStreaming: true, SupportsToolCalling: true, Description: "Lightweight Gemini 2.0 Flash model"}, {ID: "gemini-2.0-flash-lite-001", Name: "Gemini 2.0 Flash Lite 001", Provider: types.ProviderTypeGemini, MaxTokens: 524288, SupportsStreaming: true, SupportsToolCalling: true, Description: "Gemini 2.0 Flash Lite version 001"}, }
GeminiFallbackModels contains static fallback models for Google Gemini
var OpenAIFallbackModels = []types.Model{ { ID: "gpt-4o", Name: "GPT-4o", Provider: types.ProviderTypeOpenAI, MaxTokens: 128000, SupportsStreaming: true, SupportsToolCalling: true, Description: "OpenAI's latest high-intelligence flagship model", }, { ID: "gpt-4o-mini", Name: "GPT-4o Mini", Provider: types.ProviderTypeOpenAI, MaxTokens: 128000, SupportsStreaming: true, SupportsToolCalling: true, Description: "OpenAI's efficient and affordable small model", }, { ID: "gpt-4-turbo", Name: "GPT-4 Turbo", Provider: types.ProviderTypeOpenAI, MaxTokens: 128000, SupportsStreaming: true, SupportsToolCalling: true, Description: "OpenAI's balanced GPT-4 model", }, { ID: "gpt-3.5-turbo", Name: "GPT-3.5 Turbo", Provider: types.ProviderTypeOpenAI, MaxTokens: 4096, SupportsStreaming: true, SupportsToolCalling: true, Description: "OpenAI's fast and capable model", }, }
OpenAIFallbackModels contains static fallback models for OpenAI
Functions ¶
func CreateAnthropicStream ¶
func CreateAnthropicStream(response *http.Response) types.ChatCompletionStream
CreateAnthropicStream creates a stream for Anthropic responses
func CreateCustomStream ¶
func CreateCustomStream(response *http.Response, parser StreamParser) types.ChatCompletionStream
CreateCustomStream creates a stream with a custom parser
func CreateErrorStream ¶
func CreateErrorStream(err error) types.ChatCompletionStream
CreateErrorStream creates a stream that immediately returns an error
func CreateOpenAIStream ¶
func CreateOpenAIStream(response *http.Response) types.ChatCompletionStream
CreateOpenAIStream creates a stream for OpenAI-compatible responses
func GetModelCacheTTL ¶ added in v1.0.8
func GetModelCacheTTL(providerType types.ProviderType) time.Duration
GetModelCacheTTL returns the model cache TTL for a provider type. If the provider type is not found in DefaultCacheConfigs, it returns a default fallback of 6 hours.
func GetStaticFallback ¶ added in v1.0.8
func GetStaticFallback(providerType types.ProviderType) []types.Model
GetStaticFallback returns fallback models for a provider This is used when the provider's API is unavailable or returns an error
func JSONLineProcessor ¶
func JSONLineProcessor(data string, target interface{}, chunkExtractor func(interface{}) types.ChatCompletionChunk) (types.ChatCompletionChunk, error, bool)
JSONLineProcessor provides standard JSON line processing with error handling
func ReadConfigFile ¶
ReadConfigFile reads a configuration file and returns its raw byte content. This is commonly used by providers to read YAML/JSON configuration files. Returns the raw file content as bytes and any error encountered.
func SSELineProcessor ¶
func SSELineProcessor(data string, responseParser func(string) (types.ChatCompletionChunk, bool)) (types.ChatCompletionChunk, error, bool)
SSELineProcessor provides standard SSE (Server-Sent Events) line processing
func StreamFromContext ¶
func StreamFromContext(ctx context.Context, baseStream types.ChatCompletionStream) types.ChatCompletionStream
StreamFromContext creates a stream from context with cancellation support
Types ¶
type APIError ¶ added in v1.0.4
type APIError struct {
StatusCode int
Type APIErrorType
Message string
RawBody string
Retryable bool
}
APIError represents a standardized provider error
func ClassifyHTTPError ¶ added in v1.0.4
ClassifyHTTPError creates an APIError from HTTP status code This provides a basic classification based on standard HTTP status codes
func (*APIError) IsRateLimit ¶ added in v1.0.4
IsRateLimit checks if the error is a rate limit error
func (*APIError) IsRetryable ¶ added in v1.0.4
IsRetryable checks if the error is retryable
type APIErrorType ¶ added in v1.0.4
type APIErrorType string
APIErrorType classifies API errors
const ( APIErrorTypeRateLimit APIErrorType = "rate_limit" APIErrorTypeAuth APIErrorType = "auth" APIErrorTypeNotFound APIErrorType = "not_found" APIErrorTypeInvalidRequest APIErrorType = "invalid_request" APIErrorTypeServer APIErrorType = "server_error" APIErrorTypeUnknown APIErrorType = "unknown" )
type AnthropicStreamParser ¶
type AnthropicStreamParser struct{}
AnthropicStreamParser provides a parser for Anthropic's streaming format
func NewAnthropicStreamParser ¶
func NewAnthropicStreamParser() *AnthropicStreamParser
NewAnthropicStreamParser creates a new Anthropic stream parser
func (*AnthropicStreamParser) ParseLine ¶
func (p *AnthropicStreamParser) ParseLine(data string) (types.ChatCompletionChunk, bool, error)
ParseLine parses a line from an Anthropic stream
type AuthHelper ¶
type AuthHelper struct {
ProviderName string
KeyManager *keymanager.KeyManager
OAuthManager *oauthmanager.OAuthKeyManager
HTTPClient *http.Client
Config types.ProviderConfig
}
AuthHelper provides shared authentication functionality for providers
func NewAuthHelper ¶
func NewAuthHelper(providerName string, config types.ProviderConfig, client *http.Client) *AuthHelper
NewAuthHelper creates a new authentication helper
func (*AuthHelper) ClearAuthentication ¶
func (h *AuthHelper) ClearAuthentication()
ClearAuthentication clears all authentication credentials
func (*AuthHelper) ExecuteWithAuth ¶
func (h *AuthHelper) ExecuteWithAuth( ctx context.Context, options types.GenerateOptions, oauthOperation func(context.Context, *types.OAuthCredentialSet) (string, *types.Usage, error), apiKeyOperation func(context.Context, string) (string, *types.Usage, error), ) (string, *types.Usage, error)
ExecuteWithAuth executes an operation using available authentication methods Automatically chooses OAuth over API key, with failover support
func (*AuthHelper) GetAuthMethod ¶
func (h *AuthHelper) GetAuthMethod() string
GetAuthMethod returns the currently available authentication method
func (*AuthHelper) GetAuthStatus ¶
func (h *AuthHelper) GetAuthStatus() map[string]interface{}
GetAuthStatus returns a detailed authentication status
func (*AuthHelper) HandleAuthError ¶
func (h *AuthHelper) HandleAuthError(err error, statusCode int) error
HandleAuthError handles authentication-specific errors and provides user-friendly messages
func (*AuthHelper) IsAuthenticated ¶
func (h *AuthHelper) IsAuthenticated() bool
IsAuthenticated checks if any authentication method is configured
func (*AuthHelper) MakeAuthenticatedRequest ¶
func (h *AuthHelper) MakeAuthenticatedRequest( ctx context.Context, method, url string, headers map[string]string, body interface{}, ) (*http.Response, error)
MakeAuthenticatedRequest makes an HTTP request with proper authentication
func (*AuthHelper) RefreshAllOAuthTokens ¶
func (h *AuthHelper) RefreshAllOAuthTokens(ctx context.Context) error
RefreshAllOAuthTokens attempts to refresh all OAuth tokens
func (*AuthHelper) SetAuthHeaders ¶
func (h *AuthHelper) SetAuthHeaders(req *http.Request, authToken string, authType string)
SetAuthHeaders sets appropriate authentication headers on HTTP requests
func (*AuthHelper) SetProviderSpecificHeaders ¶
func (h *AuthHelper) SetProviderSpecificHeaders(req *http.Request)
SetProviderSpecificHeaders sets provider-specific headers beyond auth
func (*AuthHelper) SetupAPIKeys ¶
func (h *AuthHelper) SetupAPIKeys()
SetupAPIKeys configures API key management with support for multiple keys Extracts keys from both config.APIKey and config.ProviderConfig["api_keys"]
func (*AuthHelper) SetupOAuth ¶
func (h *AuthHelper) SetupOAuth(refreshFunc oauthmanager.RefreshFunc)
SetupOAuth configures OAuth management with multiple credential support
func (*AuthHelper) ValidateAuthConfig ¶
func (h *AuthHelper) ValidateAuthConfig(authConfig types.AuthConfig) error
ValidateAuthConfig validates authentication configuration
type BaseStream ¶
type BaseStream struct {
// contains filtered or unexported fields
}
BaseStream provides a base implementation of ChatCompletionStream
func NewBaseStream ¶
func NewBaseStream(processor *StreamProcessor, parser StreamParser) *BaseStream
NewBaseStream creates a new base stream
func (*BaseStream) Next ¶
func (bs *BaseStream) Next() (types.ChatCompletionChunk, error)
Next returns the next chunk from the stream
type CacheConfig ¶ added in v1.0.8
CacheConfig holds cache configuration for providers
type CacheInfo ¶
type CacheInfo struct {
ProviderCount int `json:"provider_count"`
TotalModels int `json:"total_models"`
ProviderModels map[string]int `json:"provider_models"`
CacheTimes map[string]time.Time `json:"cache_times"`
OldestCache time.Time `json:"oldest_cache"`
NewestCache time.Time `json:"newest_cache"`
}
CacheInfo contains information about the model cache
type ConfigHelper ¶
type ConfigHelper struct {
// contains filtered or unexported fields
}
ConfigHelper provides standardized configuration validation, extraction, and defaults for all AI providers, eliminating duplicate configuration code.
func NewConfigHelper ¶
func NewConfigHelper(providerName string, providerType types.ProviderType) *ConfigHelper
NewConfigHelper creates a new configuration helper for a specific provider
func (*ConfigHelper) ApplyTopLevelOverrides ¶
func (h *ConfigHelper) ApplyTopLevelOverrides(config types.ProviderConfig, providerConfig interface{}) error
ApplyTopLevelOverrides applies top-level config fields to provider-specific config
func (*ConfigHelper) ConfigSummary ¶
func (h *ConfigHelper) ConfigSummary(config types.ProviderConfig) map[string]interface{}
ConfigSummary provides a human-readable summary of the configuration
func (*ConfigHelper) ExtractAPIKeys ¶
func (h *ConfigHelper) ExtractAPIKeys(config types.ProviderConfig) []string
ExtractAPIKeys extracts and consolidates API keys from various sources
func (*ConfigHelper) ExtractBaseURL ¶
func (h *ConfigHelper) ExtractBaseURL(config types.ProviderConfig) string
ExtractBaseURL extracts base URL with provider-specific defaults
func (*ConfigHelper) ExtractDefaultModel ¶
func (h *ConfigHelper) ExtractDefaultModel(config types.ProviderConfig) string
ExtractDefaultModel extracts default model from config Note: Does not provide fallback defaults - each provider handles its own defaults via GetDefaultModel()
func (*ConfigHelper) ExtractDefaultOAuthClientID ¶
func (h *ConfigHelper) ExtractDefaultOAuthClientID() string
ExtractDefaultOAuthClientID returns provider-specific default OAuth client IDs
func (*ConfigHelper) ExtractMaxTokens ¶
func (h *ConfigHelper) ExtractMaxTokens(config types.ProviderConfig) int
ExtractMaxTokens extracts max tokens with provider-specific defaults
func (*ConfigHelper) ExtractProviderSpecificConfig ¶
func (h *ConfigHelper) ExtractProviderSpecificConfig(config types.ProviderConfig, target interface{}) error
ExtractProviderSpecificConfig extracts provider-specific configuration into a struct
func (*ConfigHelper) ExtractStringField ¶
func (h *ConfigHelper) ExtractStringField(config types.ProviderConfig, fieldName, fallback string) string
ExtractStringField extracts a string field from provider config with fallback
func (*ConfigHelper) ExtractStringSliceField ¶
func (h *ConfigHelper) ExtractStringSliceField(config types.ProviderConfig, fieldName string) []string
ExtractStringSliceField extracts a string slice field from provider config
func (*ConfigHelper) ExtractTimeout ¶
func (h *ConfigHelper) ExtractTimeout(config types.ProviderConfig) time.Duration
ExtractTimeout extracts timeout with sensible defaults
func (*ConfigHelper) GetProviderCapabilities ¶
func (h *ConfigHelper) GetProviderCapabilities() (supportsToolCalling, supportsStreaming, supportsResponsesAPI bool)
GetProviderCapabilities returns the capabilities flags for the provider
func (*ConfigHelper) MergeWithDefaults ¶
func (h *ConfigHelper) MergeWithDefaults(config types.ProviderConfig) types.ProviderConfig
MergeWithDefaults merges the provided config with provider defaults
func (*ConfigHelper) SanitizeConfigForLogging ¶
func (h *ConfigHelper) SanitizeConfigForLogging(config types.ProviderConfig) types.ProviderConfig
SanitizeConfigForLogging returns a configuration copy with sensitive data removed
func (*ConfigHelper) ValidateProviderConfig ¶
func (h *ConfigHelper) ValidateProviderConfig(config types.ProviderConfig) ValidationResult
ValidateProviderConfig performs comprehensive configuration validation
type ContextAwareStream ¶
type ContextAwareStream struct {
// contains filtered or unexported fields
}
ContextAwareStream wraps a stream with context awareness
func (*ContextAwareStream) Close ¶
func (cas *ContextAwareStream) Close() error
Close closes the underlying stream
func (*ContextAwareStream) Next ¶
func (cas *ContextAwareStream) Next() (types.ChatCompletionChunk, error)
Next returns the next chunk, respecting context cancellation
type ErrorClassifier ¶ added in v1.0.4
ErrorClassifier interface for provider-specific error parsing Providers can implement this to provide more detailed error classification based on their specific API error response formats
type ErrorStream ¶
type ErrorStream struct {
// contains filtered or unexported fields
}
ErrorStream is a stream that always returns an error
func (*ErrorStream) Close ¶
func (es *ErrorStream) Close() error
func (*ErrorStream) Next ¶
func (es *ErrorStream) Next() (types.ChatCompletionChunk, error)
type ErrorSummary ¶
type ErrorSummary struct {
TotalErrors int64 `json:"total_errors"`
ErrorTypes []ErrorType `json:"error_types"`
}
ErrorSummary represents a summary of errors
type ErrorType ¶
type ErrorType struct {
Type string `json:"type"`
Count int64 `json:"count"`
Percentage float64 `json:"percentage"`
}
ErrorType represents an error type with count and percentage
type GenericSSEStream ¶ added in v1.0.4
type GenericSSEStream struct {
// contains filtered or unexported fields
}
GenericSSEStream wraps SSE parsing for any provider that implements SSELineParser. It handles the low-level SSE protocol details (reading lines, handling "data:" prefix) while delegating provider-specific parsing to the SSELineParser implementation.
func NewGenericSSEStream ¶ added in v1.0.4
func NewGenericSSEStream(resp *http.Response, parser SSELineParser) *GenericSSEStream
NewGenericSSEStream creates a new generic SSE stream with the given response and parser. The parser parameter defines how individual SSE lines are parsed into ChatCompletionChunk objects.
func (*GenericSSEStream) Close ¶ added in v1.0.4
func (s *GenericSSEStream) Close() error
Close closes the underlying HTTP response body and cleans up resources.
func (*GenericSSEStream) Next ¶ added in v1.0.4
func (s *GenericSSEStream) Next() (types.ChatCompletionChunk, error)
Next returns the next chunk from the SSE stream. It reads lines from the stream, extracts SSE data, and uses the parser to convert them to chunks. Returns io.EOF when the stream is complete or an error if parsing fails.
type HealthCheckCallback ¶
type HealthCheckCallback func(provider types.ProviderType, health *ProviderHealth)
HealthCheckCallback is called when a health check completes
type HealthCheckResult ¶
type HealthCheckResult struct {
Healthy bool `json:"healthy"`
ResponseTime time.Duration `json:"response_time"`
Error string `json:"error,omitempty"`
Details map[string]interface{} `json:"details,omitempty"`
}
HealthCheckResult represents the result of a single health check
type HealthChecker ¶
type HealthChecker struct {
// contains filtered or unexported fields
}
HealthChecker performs health checks on providers
func NewHealthChecker ¶
func NewHealthChecker(interval time.Duration) *HealthChecker
NewHealthChecker creates a new health checker
func (*HealthChecker) AddCallback ¶
func (hc *HealthChecker) AddCallback(callback HealthCheckCallback)
AddCallback adds a callback to be called when health checks complete
func (*HealthChecker) CheckProvider ¶
func (hc *HealthChecker) CheckProvider(ctx context.Context, provider *InitializedProvider) error
CheckProvider performs a health check on a specific provider
func (*HealthChecker) GetAllHealthStatus ¶
func (hc *HealthChecker) GetAllHealthStatus() map[types.ProviderType]ProviderHealth
GetAllHealthStatus returns health status for all providers
func (*HealthChecker) GetHealthStatus ¶
func (hc *HealthChecker) GetHealthStatus(providerType types.ProviderType) ProviderHealth
GetHealthStatus returns the current health status for a provider
func (*HealthChecker) GetHealthSummary ¶
func (hc *HealthChecker) GetHealthSummary() HealthSummary
GetHealthSummary returns a summary of health status across all providers
func (*HealthChecker) GetHealthyProviders ¶
func (hc *HealthChecker) GetHealthyProviders() []types.ProviderType
GetHealthyProviders returns a list of currently healthy providers
func (*HealthChecker) GetUnhealthyProviders ¶
func (hc *HealthChecker) GetUnhealthyProviders() []types.ProviderType
GetUnhealthyProviders returns a list of currently unhealthy providers
func (*HealthChecker) IsHealthy ¶
func (hc *HealthChecker) IsHealthy(providerType types.ProviderType) bool
IsHealthy checks if a provider is currently healthy
func (*HealthChecker) ResetHealthStatus ¶
func (hc *HealthChecker) ResetHealthStatus(providerType types.ProviderType)
ResetHealthStatus resets the health status for a provider
type HealthSummary ¶
type HealthSummary struct {
TotalProviders int64 `json:"total_providers"`
HealthyProviders int64 `json:"healthy_providers"`
UnhealthyProviders int64 `json:"unhealthy_providers"`
LastCheckTimes map[types.ProviderType]time.Time `json:"last_check_times"`
}
HealthSummary represents a summary of health status across providers
type InitializedProvider ¶
type InitializedProvider struct {
Type types.ProviderType `json:"type"`
Config types.ProviderConfig `json:"config"`
HTTPClient *http.HTTPClient `json:"-"`
AvailableModels []types.Model `json:"available_models,omitempty"`
Metrics *ProviderMetrics `json:"-"`
HealthCheck *HealthChecker `json:"-"`
InitializedAt time.Time `json:"initialized_at"`
Status ProviderStatus `json:"status"`
}
InitializedProvider represents a fully initialized provider
type InitializerConfig ¶
type InitializerConfig struct {
DefaultTimeout time.Duration `json:"default_timeout"`
MaxRetries int `json:"max_retries"`
EnableHealthCheck bool `json:"enable_health_check"`
HealthCheckInterval time.Duration `json:"health_check_interval"`
EnableMetrics bool `json:"enable_metrics"`
AutoDetectModels bool `json:"auto_detect_models"`
CacheModels bool `json:"cache_models"`
ModelCacheTTL time.Duration `json:"model_cache_ttl"`
}
InitializerConfig configures provider initialization
type MetricsSnapshot ¶
type MetricsSnapshot struct {
TotalRequests int64 `json:"total_requests"`
SuccessRequests int64 `json:"success_requests"`
FailedRequests int64 `json:"failed_requests"`
SuccessRate float64 `json:"success_rate"`
AvgResponseTime time.Duration `json:"avg_response_time"`
MinResponseTime time.Duration `json:"min_response_time"`
MaxResponseTime time.Duration `json:"max_response_time"`
TotalTokensUsed int64 `json:"total_tokens_used"`
ErrorsByType map[string]int64 `json:"errors_by_type"`
ErrorsByProvider map[string]int64 `json:"errors_by_provider"`
Initializations map[string]int64 `json:"initializations"`
HealthChecks map[string]int64 `json:"health_checks"`
HealthCheckFails map[string]int64 `json:"health_check_fails"`
ModelUsage map[string]int64 `json:"model_usage"`
RateLimitHits map[string]int64 `json:"rate_limit_hits"`
LastRequestTime map[string]time.Time `json:"last_request_time"`
Uptime time.Duration `json:"uptime"`
}
MetricsSnapshot represents a snapshot of metrics at a point in time
type MockStream ¶
type MockStream struct {
// contains filtered or unexported fields
}
MockStream provides a mock implementation of ChatCompletionStream for testing
func NewMockStream ¶
func NewMockStream(chunks []types.ChatCompletionChunk) *MockStream
NewMockStream creates a new mock stream with the given chunks
func (*MockStream) Next ¶
func (ms *MockStream) Next() (types.ChatCompletionChunk, error)
Next returns the next chunk from the mock stream
type ModelCache ¶
type ModelCache struct {
// contains filtered or unexported fields
}
ModelCache stores cached model list with timestamp and thread-safe access
func NewModelCache ¶
func NewModelCache(ttl time.Duration) *ModelCache
NewModelCache creates a new model cache with the specified TTL
func (*ModelCache) Clear ¶
func (mc *ModelCache) Clear()
Clear empties the cache and resets the timestamp
func (*ModelCache) Get ¶
func (mc *ModelCache) Get() []types.Model
Get returns cached models (thread-safe)
func (*ModelCache) GetModels ¶
func (mc *ModelCache) GetModels(fetchFunc func() ([]types.Model, error), fallbackFunc func() []types.Model) ([]types.Model, error)
GetModels returns cached models if available and fresh, or calls the fetch function This is a convenience method that implements the common cache-check-fetch-update pattern
func (*ModelCache) GetTTL ¶
func (mc *ModelCache) GetTTL() time.Duration
GetTTL returns the cache's time-to-live duration
func (*ModelCache) GetTimestamp ¶
func (mc *ModelCache) GetTimestamp() time.Time
GetTimestamp returns when the cache was last updated
func (*ModelCache) IsStale ¶
func (mc *ModelCache) IsStale() bool
IsStale checks if the cache is expired
func (*ModelCache) SetTTL ¶
func (mc *ModelCache) SetTTL(ttl time.Duration)
SetTTL updates the cache's time-to-live duration
func (*ModelCache) Update ¶
func (mc *ModelCache) Update(models []types.Model)
Update updates the cache with new models (thread-safe)
type ModelCapabilities ¶ added in v1.0.8
ModelCapabilities defines what a model can do
type ModelCapability ¶
type ModelCapability struct {
MaxTokens int `json:"max_tokens"`
SupportsStreaming bool `json:"supports_streaming"`
SupportsTools bool `json:"supports_tools"`
SupportsVision bool `json:"supports_vision"`
Providers []types.ProviderType `json:"providers"`
InputPrice float64 `json:"input_price_per_1k"` // Price per 1K input tokens
OutputPrice float64 `json:"output_price_per_1k"` // Price per 1K output tokens
Categories []string `json:"categories"` // e.g., "text", "code", "multimodal"
}
ModelCapability represents provider model capabilities
type ModelMetadata ¶ added in v1.0.8
type ModelMetadata struct {
DisplayName string
MaxTokens int
Description string
Capabilities ModelCapabilities
CostPerMToken CostInfo
}
ModelMetadata contains comprehensive metadata for a model
type ModelMetadataRegistry ¶ added in v1.0.8
type ModelMetadataRegistry struct {
// contains filtered or unexported fields
}
ModelMetadataRegistry provides centralized model metadata management It stores and enriches model information with display names, descriptions, token limits, and capabilities for all providers.
func GetAnthropicMetadataRegistry ¶ added in v1.0.8
func GetAnthropicMetadataRegistry() *ModelMetadataRegistry
GetAnthropicMetadataRegistry returns a pre-populated registry for Anthropic models
func GetGeminiMetadataRegistry ¶ added in v1.0.8
func GetGeminiMetadataRegistry() *ModelMetadataRegistry
GetGeminiMetadataRegistry returns a pre-populated registry for Gemini models
func GetOpenAIMetadataRegistry ¶ added in v1.0.8
func GetOpenAIMetadataRegistry() *ModelMetadataRegistry
GetOpenAIMetadataRegistry returns a pre-populated registry for OpenAI models
func NewModelMetadataRegistry ¶ added in v1.0.8
func NewModelMetadataRegistry() *ModelMetadataRegistry
NewModelMetadataRegistry creates a new model metadata registry
func (*ModelMetadataRegistry) Clear ¶ added in v1.0.8
func (r *ModelMetadataRegistry) Clear()
Clear removes all registered metadata
func (*ModelMetadataRegistry) EnrichModel ¶ added in v1.0.8
func (r *ModelMetadataRegistry) EnrichModel(model *types.Model) *types.Model
EnrichModel enriches a model with metadata from the registry If metadata is not found, returns the model unchanged
func (*ModelMetadataRegistry) EnrichModels ¶ added in v1.0.8
func (r *ModelMetadataRegistry) EnrichModels(models []types.Model) []types.Model
EnrichModels enriches multiple models with metadata
func (*ModelMetadataRegistry) GetAllModelIDs ¶ added in v1.0.8
func (r *ModelMetadataRegistry) GetAllModelIDs() []string
GetAllModelIDs returns all registered model IDs
func (*ModelMetadataRegistry) GetMetadata ¶ added in v1.0.8
func (r *ModelMetadataRegistry) GetMetadata(modelID string) *ModelMetadata
GetMetadata retrieves metadata for a model ID Returns nil if no metadata is registered for the model
func (*ModelMetadataRegistry) RegisterBulkMetadata ¶ added in v1.0.8
func (r *ModelMetadataRegistry) RegisterBulkMetadata(entries map[string]*ModelMetadata)
RegisterBulkMetadata registers multiple model metadata entries at once
func (*ModelMetadataRegistry) RegisterMetadata ¶ added in v1.0.8
func (r *ModelMetadataRegistry) RegisterMetadata(modelID string, metadata *ModelMetadata)
RegisterMetadata registers metadata for a specific model ID
type ModelRegistry ¶
type ModelRegistry struct {
// contains filtered or unexported fields
}
ModelRegistry manages model information and caching
func NewModelRegistry ¶
func NewModelRegistry(ttl time.Duration) *ModelRegistry
NewModelRegistry creates a new model registry
func (*ModelRegistry) CacheModels ¶
func (mr *ModelRegistry) CacheModels(providerType types.ProviderType, models []types.Model)
CacheModels caches models for a provider
func (*ModelRegistry) ClearCache ¶
func (mr *ModelRegistry) ClearCache(providerType *types.ProviderType)
ClearCache clears the cache for a specific provider or all providers
func (*ModelRegistry) GetCacheInfo ¶
func (mr *ModelRegistry) GetCacheInfo() CacheInfo
GetCacheInfo returns information about the cache
func (*ModelRegistry) GetCachedModels ¶
func (mr *ModelRegistry) GetCachedModels(providerType types.ProviderType) []types.Model
GetCachedModels returns cached models for a provider if not expired
func (*ModelRegistry) GetModelCapability ¶
func (mr *ModelRegistry) GetModelCapability(modelID string) *ModelCapability
GetModelCapability returns the capabilities for a model
func (*ModelRegistry) GetModelsByProvider ¶
func (mr *ModelRegistry) GetModelsByProvider(providerType types.ProviderType) []types.Model
GetModelsByProvider returns all models for a specific provider
func (*ModelRegistry) GetProviderCount ¶
func (mr *ModelRegistry) GetProviderCount() int
GetProviderCount returns the number of providers with cached models
func (*ModelRegistry) GetTotalModelCount ¶
func (mr *ModelRegistry) GetTotalModelCount() int
GetTotalModelCount returns the total number of cached models
func (*ModelRegistry) RegisterModel ¶
func (mr *ModelRegistry) RegisterModel(modelID string, capability *ModelCapability)
RegisterModel registers a model with its capabilities
func (*ModelRegistry) SearchModels ¶
func (mr *ModelRegistry) SearchModels(criteria SearchCriteria) []types.Model
SearchModels searches for models matching criteria
type ModelUsage ¶
ModelUsage represents model usage statistics
type OAuthRefreshHelper ¶
OAuthRefreshHelper provides common OAuth token refresh implementations
func NewOAuthRefreshHelper ¶
func NewOAuthRefreshHelper(providerName string, client *http.Client) *OAuthRefreshHelper
NewOAuthRefreshHelper creates a new OAuth refresh helper
func (*OAuthRefreshHelper) AnthropicOAuthRefresh ¶
func (h *OAuthRefreshHelper) AnthropicOAuthRefresh(ctx context.Context, cred *types.OAuthCredentialSet) (*types.OAuthCredentialSet, error)
AnthropicOAuthRefresh implements Anthropic's OAuth 2.0 token refresh
func (*OAuthRefreshHelper) GeminiOAuthRefresh ¶
func (h *OAuthRefreshHelper) GeminiOAuthRefresh(ctx context.Context, cred *types.OAuthCredentialSet) (*types.OAuthCredentialSet, error)
GeminiOAuthRefresh implements Google's OAuth 2.0 token refresh using the oauth2 library
func (*OAuthRefreshHelper) GenericOAuthRefresh ¶
func (h *OAuthRefreshHelper) GenericOAuthRefresh(ctx context.Context, cred *types.OAuthCredentialSet, tokenURL string) (*types.OAuthCredentialSet, error)
GenericOAuthRefresh provides a generic OAuth 2.0 token refresh implementation
func (*OAuthRefreshHelper) OpenAIOAuthRefresh ¶
func (h *OAuthRefreshHelper) OpenAIOAuthRefresh(ctx context.Context, cred *types.OAuthCredentialSet) (*types.OAuthCredentialSet, error)
OpenAIOAuthRefresh implements OpenAI's OAuth 2.0 token refresh
func (*OAuthRefreshHelper) QwenOAuthRefresh ¶
func (h *OAuthRefreshHelper) QwenOAuthRefresh(ctx context.Context, cred *types.OAuthCredentialSet) (*types.OAuthCredentialSet, error)
QwenOAuthRefresh implements Qwen's OAuth 2.0 token refresh
type OpenAICompatibleParser ¶ added in v1.0.4
type OpenAICompatibleParser struct {
// SkipEmptyContent determines whether to skip chunks with empty content
SkipEmptyContent bool
}
OpenAICompatibleParser handles OpenAI-style SSE responses. This parser works with the standard OpenAI streaming format and can be used by any provider that follows OpenAI's streaming API conventions.
func NewOpenAICompatibleParser ¶ added in v1.0.4
func NewOpenAICompatibleParser() *OpenAICompatibleParser
NewOpenAICompatibleParser creates a new OpenAI-compatible SSE parser with default settings.
func (*OpenAICompatibleParser) IsDone ¶ added in v1.0.4
func (p *OpenAICompatibleParser) IsDone(line string) bool
IsDone checks if the given line indicates stream completion. OpenAI sends "[DONE]" as the final message in the stream.
func (*OpenAICompatibleParser) ParseLine ¶ added in v1.0.4
func (p *OpenAICompatibleParser) ParseLine(line string) (types.ChatCompletionChunk, error)
ParseLine parses a single SSE data line in OpenAI format into a ChatCompletionChunk. The OpenAI format is JSON with the structure:
{
"id": "chatcmpl-...",
"object": "chat.completion.chunk",
"created": 1234567890,
"model": "gpt-4",
"choices": [{
"index": 0,
"delta": {
"role": "assistant",
"content": "Hello"
},
"finish_reason": null
}]
}
type ProcessLineFunc ¶
type ProcessLineFunc func(line string) (types.ChatCompletionChunk, error, bool)
ProcessLineFunc processes a single line from a streaming response
type ProviderHealth ¶
type ProviderHealth struct {
Provider types.ProviderType `json:"provider"`
Healthy bool `json:"healthy"`
LastCheck time.Time `json:"last_check"`
LastSuccess time.Time `json:"last_success"`
LastError time.Time `json:"last_error"`
ErrorMessage string `json:"error_message,omitempty"`
ResponseTime time.Duration `json:"response_time"`
CheckCount int64 `json:"check_count"`
SuccessCount int64 `json:"success_count"`
FailureCount int64 `json:"failure_count"`
Details map[string]interface{} `json:"details,omitempty"`
}
ProviderHealth represents the health status of a provider
type ProviderInitializer ¶
type ProviderInitializer struct {
// contains filtered or unexported fields
}
ProviderInitializer handles common provider initialization patterns
func NewProviderInitializer ¶
func NewProviderInitializer(config InitializerConfig) *ProviderInitializer
NewProviderInitializer creates a new provider initializer
func (*ProviderInitializer) InitializeProvider ¶
func (pi *ProviderInitializer) InitializeProvider( ctx context.Context, providerType types.ProviderType, config types.ProviderConfig, ) (*InitializedProvider, error)
InitializeProvider initializes a provider with common patterns
type ProviderMetrics ¶
type ProviderMetrics struct {
// contains filtered or unexported fields
}
ProviderMetrics tracks performance and usage metrics for providers
func NewProviderMetrics ¶
func NewProviderMetrics() *ProviderMetrics
NewProviderMetrics creates a new provider metrics instance
func (*ProviderMetrics) GetErrorSummary ¶
func (pm *ProviderMetrics) GetErrorSummary() ErrorSummary
GetErrorSummary returns a summary of errors by type
func (*ProviderMetrics) GetMetricsForProvider ¶
func (pm *ProviderMetrics) GetMetricsForProvider(providerType types.ProviderType) ProviderMetricsSnapshot
GetMetricsForProvider returns metrics specific to a provider
func (*ProviderMetrics) GetSnapshot ¶
func (pm *ProviderMetrics) GetSnapshot() MetricsSnapshot
GetSnapshot returns a snapshot of current metrics
func (*ProviderMetrics) GetTopModels ¶
func (pm *ProviderMetrics) GetTopModels(limit int) []ModelUsage
GetTopModels returns the most used models
func (*ProviderMetrics) RecordError ¶
func (pm *ProviderMetrics) RecordError(providerType types.ProviderType, errorType string)
RecordError records a failed request
func (*ProviderMetrics) RecordHealthCheck ¶
func (pm *ProviderMetrics) RecordHealthCheck(providerType types.ProviderType, success bool)
RecordHealthCheck records a health check attempt
func (*ProviderMetrics) RecordInitialization ¶
func (pm *ProviderMetrics) RecordInitialization(providerType types.ProviderType)
RecordInitialization records a provider initialization
func (*ProviderMetrics) RecordRateLimitHit ¶
func (pm *ProviderMetrics) RecordRateLimitHit(providerType types.ProviderType)
RecordRateLimitHit records a rate limit occurrence
func (*ProviderMetrics) RecordRequest ¶
func (pm *ProviderMetrics) RecordRequest(providerType types.ProviderType)
RecordRequest records a request attempt
func (*ProviderMetrics) RecordSuccess ¶
func (pm *ProviderMetrics) RecordSuccess(providerType types.ProviderType, responseTime time.Duration, tokens int64, modelID string)
RecordSuccess records a successful request
type ProviderMetricsSnapshot ¶
type ProviderMetricsSnapshot struct {
Provider string `json:"provider"`
Errors int64 `json:"errors"`
HealthChecks int64 `json:"health_checks"`
HealthCheckFails int64 `json:"health_check_fails"`
RateLimitHits int64 `json:"rate_limit_hits"`
Initializations int64 `json:"initializations"`
LastRequestTime time.Time `json:"last_request_time"`
}
ProviderMetricsSnapshot represents metrics for a specific provider
type ProviderStatus ¶
type ProviderStatus struct {
Healthy bool `json:"healthy"`
LastCheck time.Time `json:"last_check"`
ErrorCount int64 `json:"error_count"`
RequestCount int64 `json:"request_count"`
ResponseTime time.Duration `json:"avg_response_time"`
}
ProviderStatus represents the current status of a provider
type RateLimitHelper ¶
type RateLimitHelper struct {
// contains filtered or unexported fields
}
RateLimitHelper provides shared rate limiting functionality for all AI providers. It encapsulates the common patterns of rate limit tracking, parsing, and enforcement used across different provider implementations.
func NewRateLimitHelper ¶
func NewRateLimitHelper(parser ratelimit.Parser) *RateLimitHelper
NewRateLimitHelper creates a new RateLimitHelper with the given provider-specific parser.
func (*RateLimitHelper) CanMakeRequest ¶
func (h *RateLimitHelper) CanMakeRequest(model string, estimatedTokens int) bool
CanMakeRequest checks if a request can be made for the given model and estimated tokens. Returns true if the request should proceed, false if rate limited.
func (*RateLimitHelper) CheckRateLimitAndWait ¶
func (h *RateLimitHelper) CheckRateLimitAndWait(model string, estimatedTokens int) bool
CheckRateLimitAndWait checks rate limits and waits if necessary before making a request. This combines the common pattern of checking limits and sleeping if needed. Returns true if the caller should proceed with the request, false if rate limited.
func (*RateLimitHelper) GetParser ¶
func (h *RateLimitHelper) GetParser() ratelimit.Parser
GetParser returns the underlying rate limit parser for advanced operations. This should be used sparingly when the helper methods don't provide sufficient functionality.
func (*RateLimitHelper) GetRateLimitInfo ¶
func (h *RateLimitHelper) GetRateLimitInfo(model string) (*ratelimit.Info, bool)
GetRateLimitInfo retrieves the current rate limit information for a model. Returns the info and a boolean indicating whether data exists for the model.
func (*RateLimitHelper) GetTracker ¶
func (h *RateLimitHelper) GetTracker() *ratelimit.Tracker
GetTracker returns the underlying rate limit tracker for advanced operations. This should be used sparingly when the helper methods don't provide sufficient functionality.
func (*RateLimitHelper) GetWaitTime ¶
func (h *RateLimitHelper) GetWaitTime(model string) time.Duration
GetWaitTime returns the duration to wait before the next request can be made. Returns 0 if no waiting is required.
func (*RateLimitHelper) ParseAndUpdateRateLimits ¶
func (h *RateLimitHelper) ParseAndUpdateRateLimits(headers http.Header, model string)
ParseAndUpdateRateLimits parses rate limit headers from an HTTP response and updates the tracker. This is the most common operation performed after receiving API responses.
func (*RateLimitHelper) ShouldThrottle ¶
func (h *RateLimitHelper) ShouldThrottle(model string, threshold float64) bool
ShouldThrottle determines if requests should be throttled based on current usage. threshold is a value between 0 and 1 representing the percentage of limits consumed at which throttling should begin (e.g., 0.8 = throttle at 80% usage).
func (*RateLimitHelper) UpdateRateLimitInfo ¶
func (h *RateLimitHelper) UpdateRateLimitInfo(info *ratelimit.Info)
UpdateRateLimitInfo directly updates the rate limit info for a model. This is useful for providers that get rate limit info from API endpoints rather than response headers.
type RateLimitInfo ¶ added in v1.0.8
type RateLimitInfo struct {
RequestsLimit int
RequestsRemaining int
RequestsReset int64 // Unix timestamp
TokensLimit int
TokensRemaining int
TokensReset int64 // Unix timestamp
RetryAfter int // Seconds to wait
}
RateLimitInfo holds parsed rate limit information in a simplified format. This provides easy access to the most commonly used rate limit fields without requiring knowledge of the underlying ratelimit.Info structure.
type RateLimitTracker ¶ added in v1.0.8
type RateLimitTracker struct {
// contains filtered or unexported fields
}
RateLimitTracker wraps RateLimitHelper with automatic response tracking. It provides a cleaner API that reduces code duplication by combining rate limit parsing and tracking into a single method call.
func NewRateLimitTracker ¶ added in v1.0.8
func NewRateLimitTracker(parser ratelimit.Parser) *RateLimitTracker
NewRateLimitTracker creates a new tracker with the given parser.
func (*RateLimitTracker) GetHelper ¶ added in v1.0.8
func (t *RateLimitTracker) GetHelper() *RateLimitHelper
GetHelper returns the underlying helper for backwards compatibility. This allows access to advanced features like CheckRateLimitAndWait, ShouldThrottle, and provider-specific rate limit information.
func (*RateLimitTracker) GetLastInfo ¶ added in v1.0.8
func (t *RateLimitTracker) GetLastInfo() *RateLimitInfo
GetLastInfo returns the most recent rate limit info. This can be used to check rate limits without making another API call.
func (*RateLimitTracker) GetRetryAfter ¶ added in v1.0.8
func (t *RateLimitTracker) GetRetryAfter() int
GetRetryAfter returns seconds to wait, or 0 if not rate limited.
func (*RateLimitTracker) IsRateLimited ¶ added in v1.0.8
func (t *RateLimitTracker) IsRateLimited() bool
IsRateLimited returns true if we should back off based on the last check. This provides a simple way to check if rate limits have been exceeded.
func (*RateLimitTracker) TrackResponse ¶ added in v1.0.8
func (t *RateLimitTracker) TrackResponse(headers http.Header, model string) *RateLimitInfo
TrackResponse extracts and stores rate limit info from response headers. This is the single point where rate limits are parsed, replacing multiple ParseAndUpdateRateLimits() calls throughout the codebase.
Usage:
info := p.rateLimitTracker.TrackResponse(resp.Header, modelName)
if info != nil && info.RetryAfter > 0 {
// Handle rate limit
}
type RefreshFuncFactory ¶
type RefreshFuncFactory struct {
Helper *OAuthRefreshHelper
}
RefreshFuncFactory creates refresh functions for different providers
func NewRefreshFuncFactory ¶
func NewRefreshFuncFactory(providerName string, client *http.Client) *RefreshFuncFactory
NewRefreshFuncFactory creates a new refresh function factory
func (*RefreshFuncFactory) CreateAnthropicRefreshFunc ¶
func (f *RefreshFuncFactory) CreateAnthropicRefreshFunc() oauthmanager.RefreshFunc
CreateAnthropicRefreshFunc creates an Anthropic refresh function
func (*RefreshFuncFactory) CreateGeminiRefreshFunc ¶
func (f *RefreshFuncFactory) CreateGeminiRefreshFunc() oauthmanager.RefreshFunc
CreateGeminiRefreshFunc creates a Gemini refresh function
func (*RefreshFuncFactory) CreateGenericRefreshFunc ¶
func (f *RefreshFuncFactory) CreateGenericRefreshFunc(tokenURL string) oauthmanager.RefreshFunc
CreateGenericRefreshFunc creates a generic refresh function for custom providers
func (*RefreshFuncFactory) CreateOpenAIRefreshFunc ¶
func (f *RefreshFuncFactory) CreateOpenAIRefreshFunc() oauthmanager.RefreshFunc
CreateOpenAIRefreshFunc creates an OpenAI refresh function
func (*RefreshFuncFactory) CreateQwenRefreshFunc ¶
func (f *RefreshFuncFactory) CreateQwenRefreshFunc() oauthmanager.RefreshFunc
CreateQwenRefreshFunc creates a Qwen refresh function
type SSELineParser ¶ added in v1.0.4
type SSELineParser interface {
// ParseLine parses a single SSE data line into a chunk.
// Returns the parsed chunk and any error that occurred during parsing.
ParseLine(line string) (types.ChatCompletionChunk, error)
// IsDone checks if this line indicates stream completion.
// Common completion signals include "[DONE]" or specific event types.
IsDone(line string) bool
}
SSELineParser defines provider-specific parsing behavior for SSE streams. This interface allows different providers to implement their own parsing logic while using the common SSE stream infrastructure.
type SearchCriteria ¶
type SearchCriteria struct {
Provider *types.ProviderType `json:"provider,omitempty"`
MinTokens *int `json:"min_tokens,omitempty"`
MaxTokens *int `json:"max_tokens,omitempty"`
SupportsStreaming *bool `json:"supports_streaming,omitempty"`
SupportsTools *bool `json:"supports_tools,omitempty"`
Categories []string `json:"categories,omitempty"`
NameContains string `json:"name_contains,omitempty"`
}
SearchCriteria defines search criteria for models
type StandardStreamParser ¶
type StandardStreamParser struct {
// Custom field mappings for provider-specific responses
ContentField string
ReasoningField string // For GLM-4.6, OpenCode/Zen style
ReasoningContentField string // For vLLM/Synthetic style
DoneField string
UsageField string
ToolCallsField string
FinishReason string
}
StandardStreamParser provides a standard parser for OpenAI-compatible streaming responses
func NewStandardStreamParser ¶
func NewStandardStreamParser() *StandardStreamParser
NewStandardStreamParser creates a new standard stream parser with default OpenAI mappings
func (*StandardStreamParser) ParseLine ¶
func (p *StandardStreamParser) ParseLine(data string) (types.ChatCompletionChunk, bool, error)
ParseLine parses a line from the stream using standard OpenAI format
type StreamParser ¶
type StreamParser interface {
ParseLine(data string) (types.ChatCompletionChunk, bool, error)
}
StreamParser defines the interface for parsing streaming responses
type StreamProcessor ¶
type StreamProcessor struct {
// contains filtered or unexported fields
}
StreamProcessor provides common streaming functionality for all providers
func NewStreamProcessor ¶
func NewStreamProcessor(response *http.Response) *StreamProcessor
NewStreamProcessor creates a new stream processor
func (*StreamProcessor) Close ¶
func (sp *StreamProcessor) Close() error
Close closes the stream and cleans up resources
func (*StreamProcessor) IsDone ¶
func (sp *StreamProcessor) IsDone() bool
IsDone returns whether the stream is finished
func (*StreamProcessor) MarkDone ¶
func (sp *StreamProcessor) MarkDone()
MarkDone marks the stream as done
func (*StreamProcessor) NextChunk ¶
func (sp *StreamProcessor) NextChunk(processLine ProcessLineFunc) (types.ChatCompletionChunk, error)
NextChunk reads and processes the next chunk from the stream
type ValidationResult ¶
ValidationResult contains the result of configuration validation
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package testing provides common testing helpers and utilities for AI provider implementations including authentication, configuration, tool calling, and mock server functionality.
|
Package testing provides common testing helpers and utilities for AI provider implementations including authentication, configuration, tool calling, and mock server functionality. |