typ

package
v0.260124.1430 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2026 License: MPL-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsValidTactic

func IsValidTactic(tacticStr string) bool

IsValidTactic checks if the given tactic string is valid

Types

type AuthType

type AuthType string

AuthType represents the authentication type for a provider

const (
	AuthTypeAPIKey AuthType = "api_key"
	AuthTypeOAuth  AuthType = "oauth"
)

type HybridParams added in v0.260124.900

type HybridParams struct {
	RequestThreshold int64 `json:"request_threshold"` // Request threshold for hybrid tactic
	TokenThreshold   int64 `json:"token_threshold"`   // Token threshold for hybrid tactic
}

HybridParams holds parameters for hybrid tactic

func AsHybridParams added in v0.260124.900

func AsHybridParams(p TacticParams) (HybridParams, bool)

type HybridTactic added in v0.260124.900

type HybridTactic struct {
	RequestThreshold int64 // Threshold for request count before switching
	TokenThreshold   int64 // Threshold for token consumption before switching
}

HybridTactic implements a hybrid load balancing strategy

func NewHybridTactic added in v0.260124.900

func NewHybridTactic(requestThreshold, tokenThreshold int64) *HybridTactic

NewHybridTactic creates a new hybrid tactic

func (*HybridTactic) GetName added in v0.260124.900

func (ht *HybridTactic) GetName() string

func (*HybridTactic) GetType added in v0.260124.900

func (ht *HybridTactic) GetType() loadbalance.TacticType

func (*HybridTactic) SelectService added in v0.260124.900

func (ht *HybridTactic) SelectService(rule *Rule) *loadbalance.Service

SelectService selects service based on both request count and token consumption

type LoadBalancingTactic

type LoadBalancingTactic interface {
	SelectService(rule *Rule) *loadbalance.Service
	GetName() string
	GetType() loadbalance.TacticType
}

LoadBalancingTactic defines the interface for load balancing strategies

func CreateTacticWithTypedParams

func CreateTacticWithTypedParams(tacticType loadbalance.TacticType, params TacticParams) LoadBalancingTactic

func GetDefaultTactic

func GetDefaultTactic(tType loadbalance.TacticType) LoadBalancingTactic

type OAuthDetail

type OAuthDetail struct {
	AccessToken  string                 `json:"access_token"`  // OAuth access token
	ProviderType string                 `json:"provider_type"` // anthropic, google, etc. for token manager lookup
	UserID       string                 `json:"user_id"`       // OAuth user identifier
	RefreshToken string                 `json:"refresh_token"` // Token for refreshing access token
	ExpiresAt    string                 `json:"expires_at"`    // Token expiration time (RFC3339)
	ExtraFields  map[string]interface{} `json:"extra_fields"`  // Any extra field for some special clients
}

OAuthDetail contains OAuth-specific authentication information

func (*OAuthDetail) IsExpired

func (o *OAuthDetail) IsExpired() bool

IsExpired checks if the OAuth token is expired

type Provider

type Provider struct {
	UUID          string            `json:"uuid"`
	Name          string            `json:"name"`
	APIBase       string            `json:"api_base"`
	APIStyle      protocol.APIStyle `json:"api_style"` // "openai" or "anthropic", defaults to "openai"
	Token         string            `json:"token"`     // API key for api_key auth type
	NoKeyRequired bool              `json:"no_key_required"`
	Enabled       bool              `json:"enabled"`
	ProxyURL      string            `json:"proxy_url"`              // HTTP or SOCKS proxy URL (e.g., "http://127.0.0.1:7890" or "socks5://127.0.0.1:1080")
	Timeout       int64             `json:"timeout,omitempty"`      // Request timeout in seconds (default: 1800 = 30 minutes)
	Tags          []string          `json:"tags,omitempty"`         // Provider tags for categorization
	Models        []string          `json:"models,omitempty"`       // Available models for this provider (cached)
	LastUpdated   string            `json:"last_updated,omitempty"` // Last update timestamp

	// Auth configuration
	AuthType    AuthType     `json:"auth_type"`              // api_key or oauth
	OAuthDetail *OAuthDetail `json:"oauth_detail,omitempty"` // OAuth credentials (only for oauth auth type)
}

Provider represents an AI model api key and provider configuration

func (*Provider) GetAccessToken

func (p *Provider) GetAccessToken() string

GetAccessToken returns the access token based on auth type

func (*Provider) IsOAuthExpired

func (p *Provider) IsOAuthExpired() bool

IsOAuthExpired checks if the OAuth token is expired (only valid for oauth auth type)

type RandomParams

type RandomParams struct{}

RandomParams represents parameters for random tactic (currently empty but extensible)

func AsRandomParams

func AsRandomParams(p TacticParams) (RandomParams, bool)

type RandomTactic

type RandomTactic struct{}

RandomTactic implements random selection with weighted probability

func NewRandomTactic

func NewRandomTactic() *RandomTactic

NewRandomTactic creates a new random tactic

func (*RandomTactic) GetName

func (rt *RandomTactic) GetName() string

func (*RandomTactic) GetType

func (rt *RandomTactic) GetType() loadbalance.TacticType

func (*RandomTactic) SelectService

func (rt *RandomTactic) SelectService(rule *Rule) *loadbalance.Service

SelectService selects a service randomly based on weights

type RoundRobinParams added in v0.260124.900

type RoundRobinParams struct {
	RequestThreshold int64 `json:"request_threshold"` // Number of requests per service before switching
}

RoundRobinParams holds parameters for round-robin tactic

func AsRoundRobinParams added in v0.260124.900

func AsRoundRobinParams(p TacticParams) (RoundRobinParams, bool)

Type assertion helpers for TacticParams

type RoundRobinTactic added in v0.260124.900

type RoundRobinTactic struct {
	RequestThreshold int64 // Number of requests per service before switching
}

RoundRobinTactic implements round-robin load balancing based on request count

func NewRoundRobinTactic added in v0.260124.900

func NewRoundRobinTactic(requestThreshold ...int64) *RoundRobinTactic

NewRoundRobinTactic creates a new round-robin tactic with optional threshold parameter

func (*RoundRobinTactic) GetName added in v0.260124.900

func (rr *RoundRobinTactic) GetName() string

func (*RoundRobinTactic) GetType added in v0.260124.900

func (rr *RoundRobinTactic) GetType() loadbalance.TacticType

func (*RoundRobinTactic) SelectService added in v0.260124.900

func (rr *RoundRobinTactic) SelectService(rule *Rule) *loadbalance.Service

SelectService selects the next service based on round-robin with request threshold

type Rule

type Rule struct {
	UUID                string                `json:"uuid"`
	Scenario            RuleScenario          `json:"scenario,required" yaml:"scenario"` // openai, anthropic, claude_code; defaults to openai
	RequestModel        string                `json:"request_model" yaml:"request_model"`
	ResponseModel       string                `json:"response_model" yaml:"response_model"`
	Description         string                `json:"description"`
	Services            []loadbalance.Service `json:"services" yaml:"services"`
	CurrentServiceIndex int                   `json:"current_service_index" yaml:"current_service_index"`
	// Unified Tactic Configuration
	LBTactic Tactic `json:"lb_tactic" yaml:"lb_tactic"`
	Active   bool   `json:"active" yaml:"active"`
	// Smart Routing Configuration
	SmartEnabled bool                        `json:"smart_enabled" yaml:"smart_enabled"`
	SmartRouting []smartrouting.SmartRouting `json:"smart_routing,omitempty" yaml:"smart_routing,omitempty"`
}

Rule represents a request/response configuration with load balancing support

func (*Rule) GetActiveServices

func (r *Rule) GetActiveServices() []*loadbalance.Service

GetActiveServices returns all active services with initialized stats

func (*Rule) GetCurrentService

func (r *Rule) GetCurrentService() *loadbalance.Service

GetCurrentService returns the current active service based on CurrentServiceIndex

func (*Rule) GetDefaultModel

func (r *Rule) GetDefaultModel() string

GetDefaultModel returns the model from the currently selected service using load balancing tactic

func (*Rule) GetDefaultProvider

func (r *Rule) GetDefaultProvider() string

GetDefaultProvider returns the provider from the currently selected service using load balancing tactic

func (*Rule) GetScenario

func (r *Rule) GetScenario() RuleScenario

GetScenario returns the scenario, defaulting to openai if empty

func (*Rule) GetServices

func (r *Rule) GetServices() []loadbalance.Service

GetServices returns the services to use for this rule

func (*Rule) GetTacticType

func (r *Rule) GetTacticType() loadbalance.TacticType

GetTacticType returns the load balancing tactic type

func (*Rule) ToJSON

func (r *Rule) ToJSON() interface{}

ToJSON implementation

type RuleScenario

type RuleScenario string

RuleScenario represents the scenario for a routing rule

const (
	ScenarioOpenAI     RuleScenario = "openai"
	ScenarioAnthropic  RuleScenario = "anthropic"
	ScenarioClaudeCode RuleScenario = "claude_code"
)

type ScenarioConfig

type ScenarioConfig struct {
	Scenario   RuleScenario           `json:"scenario" yaml:"scenario"`
	Flags      ScenarioFlags          `json:"flags" yaml:"flags"`                               // Scenario configuration flags
	Extensions map[string]interface{} `json:"extensions,omitempty" yaml:"extensions,omitempty"` // Reserved for future extensions
}

ScenarioConfig represents configuration for a specific scenario

func (*ScenarioConfig) GetDefaultFlags

func (sc *ScenarioConfig) GetDefaultFlags() ScenarioFlags

GetDefaultFlags returns default flags for a scenario

type ScenarioFlags

type ScenarioFlags struct {
	Unified  bool `json:"unified" yaml:"unified"`   // Single configuration for all models
	Separate bool `json:"separate" yaml:"separate"` // Separate configuration for each model
	Smart    bool `json:"smart" yaml:"smart"`       // Smart mode with automatic optimization
}

ScenarioFlags represents configuration flags for a scenario

type Tactic

type Tactic struct {
	Type   loadbalance.TacticType `json:"type" yaml:"type"`
	Params TacticParams           `json:"params" yaml:"params"`
}

Tactic bundles the strategy type and its parameters together

func ParseTacticFromMap

func ParseTacticFromMap(tacticType loadbalance.TacticType, params map[string]interface{}) Tactic

ParseTacticFromMap creates a Tactic from a tactic type and parameter map. This is useful for parsing API request parameters into a Tactic configuration.

func (*Tactic) Instantiate

func (tc *Tactic) Instantiate() LoadBalancingTactic

Instantiate converts the configuration into functional logic

func (*Tactic) UnmarshalJSON

func (tc *Tactic) UnmarshalJSON(data []byte) error

UnmarshalJSON handles the polymorphic decoding of TacticParams

type TacticParams

type TacticParams interface {
	// contains filtered or unexported methods
}

TacticParams represents parameters for different load balancing tactics This is a sealed type that can only be one of the specific tactic parameter types

func DefaultHybridParams

func DefaultHybridParams() TacticParams

func DefaultRandomParams

func DefaultRandomParams() TacticParams

func DefaultRoundRobinParams

func DefaultRoundRobinParams() TacticParams

DefaultParams returns default parameters for each tactic type

func DefaultTokenBasedParams

func DefaultTokenBasedParams() TacticParams

func NewHybridParams

func NewHybridParams(requestThreshold, tokenThreshold int64) TacticParams

func NewRandomParams

func NewRandomParams() TacticParams

func NewRoundRobinParams added in v0.260124.900

func NewRoundRobinParams(threshold int64) TacticParams

Helper constructors for creating tactic parameters

func NewTokenBasedParams

func NewTokenBasedParams(threshold int64) TacticParams

type TokenBasedParams

type TokenBasedParams struct {
	TokenThreshold int64 `json:"token_threshold"` // Threshold for token consumption before switching
}

TokenBasedParams holds parameters for token-based tactic

func AsTokenBasedParams

func AsTokenBasedParams(p TacticParams) (TokenBasedParams, bool)

type TokenBasedTactic

type TokenBasedTactic struct {
	TokenThreshold int64 // Threshold for token consumption before switching
}

TokenBasedTactic implements load balancing based on token consumption

func NewTokenBasedTactic

func NewTokenBasedTactic(tokenThreshold int64) *TokenBasedTactic

NewTokenBasedTactic creates a new token-based tactic

func (*TokenBasedTactic) GetName

func (tb *TokenBasedTactic) GetName() string

func (*TokenBasedTactic) GetType

func (tb *TokenBasedTactic) GetType() loadbalance.TacticType

func (*TokenBasedTactic) SelectService

func (tb *TokenBasedTactic) SelectService(rule *Rule) *loadbalance.Service

SelectService selects service based on token consumption thresholds

Jump to

Keyboard shortcuts

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