config

package
v0.1.13 Latest Latest
Warning

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

Go to latest
Published: May 8, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultHost             = "127.0.0.1"
	DefaultPort             = 12345
	DataDirName             = ".ai-switch"
	UsageDBName             = "usage.db"
	ConfigFile              = "config.yaml"
	PidFileName             = "ai-switch.pid"
	DefaultLogRetentionDays = 30
)

Variables

View Source
var ProviderPresets []ProviderPreset

Functions

func DataDir

func DataDir() (string, error)

DataDir returns the path to the data directory (~/.ai-switch/).

func DefaultConfigPath

func DefaultConfigPath(flagPath string) (string, error)

DefaultConfigPath returns the config file path following the priority: 1. provided path (from -c flag) 2. ~/.ai-switch/config.yaml

func EnsureDataDir

func EnsureDataDir() (string, error)

EnsureDataDir creates the data directory if it does not exist.

func SplitProviderModel

func SplitProviderModel(value, defaultProvider string) (string, string)

SplitProviderModel splits "provider|model" format. Plain names use defaultProvider.

func ValidFormat

func ValidFormat(f string) bool

func WriteConfig

func WriteConfig(path string, cfg *Config) error

WriteConfig marshals the config and writes it to the given file path atomically.

Types

type Config

type Config struct {
	Server                ServerConfig              `mapstructure:"server" yaml:"server"`
	LogRetentionDays      int                       `mapstructure:"log_retention_days" yaml:"log_retention_days,omitempty"`
	DefaultRoute          string                    `mapstructure:"default_route" yaml:"default_route,omitempty"`
	DefaultAnthropicRoute string                    `mapstructure:"default_anthropic_route" yaml:"default_anthropic_route,omitempty"`
	DefaultResponsesRoute string                    `mapstructure:"default_responses_route" yaml:"default_responses_route,omitempty"`
	DefaultChatRoute      string                    `mapstructure:"default_chat_route" yaml:"default_chat_route,omitempty"`
	Providers             map[string]ProviderConfig `mapstructure:"providers" yaml:"providers"`
	Routes                map[string]RouteRule      `mapstructure:"routes" yaml:"routes"`
}

func Load

func Load(path string) (*Config, error)

func (*Config) DefaultRouteConfig

func (c *Config) DefaultRouteConfig(protocol string) *RouteRule

DefaultRouteConfig returns the default route rule for the given protocol, falling back to the global default_route. Returns nil if none configured.

type Provider

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

Provider holds the current configuration with thread-safe access.

func NewProvider

func NewProvider(cfg *Config, path string) *Provider

NewProvider creates a new config provider with the initial config.

func (*Provider) Get

func (p *Provider) Get() *Config

Get returns a snapshot of the current config. The returned pointer must not be modified by callers.

func (*Provider) Path

func (p *Provider) Path() string

Path returns the config file path.

func (*Provider) Reload

func (p *Provider) Reload() error

Reload re-reads the config file and swaps the configuration. In-flight requests continue using the old config.

type ProviderConfig

type ProviderConfig struct {
	Name         string   `mapstructure:"name" yaml:"name"`
	BaseURL      string   `mapstructure:"base_url" yaml:"base_url"`
	Path         string   `mapstructure:"path" yaml:"path"`
	APIKey       string   `mapstructure:"api_key" yaml:"api_key"`
	FallbackKeys []string `mapstructure:"fallback_keys" yaml:"fallback_keys,omitempty"`
	Format       string   `mapstructure:"format" yaml:"format"`
	LogoURL      string   `mapstructure:"logo_url" yaml:"logo_url"`
	ThinkTag     string   `mapstructure:"think_tag" yaml:"think_tag,omitempty"`
	Models       []string `mapstructure:"models" yaml:"models,omitempty"`
	EnableProxy  bool     `mapstructure:"enable_proxy" yaml:"enable_proxy,omitempty"`
}

type ProviderPreset

type ProviderPreset struct {
	Key       string `json:"key" yaml:"key"`
	Name      string `json:"name" yaml:"name"`
	BaseURL   string `json:"base_url" yaml:"base_url"`
	Format    string `json:"format" yaml:"format"`
	Icon      string `json:"icon" yaml:"icon"`
	IconColor string `json:"icon_color" yaml:"icon_color"`
	Category  string `json:"category" yaml:"category"`
	APIKeyURL string `json:"api_key_url" yaml:"api_key_url"`
	IsPartner bool   `json:"is_partner" yaml:"is_partner"`
}

type RouteRule

type RouteRule struct {
	Provider             string            `mapstructure:"provider" yaml:"provider"`
	DefaultModel         string            `mapstructure:"default_model" yaml:"default_model"`
	Disabled             bool              `mapstructure:"disabled" yaml:"disabled,omitempty"`
	SceneMap             map[string]string `mapstructure:"scene_map" yaml:"scene_map"`
	ModelMap             map[string]string `mapstructure:"model_map" yaml:"model_map"`
	LongContextThreshold int               `mapstructure:"long_context_threshold" yaml:"long_context_threshold,omitempty"`
}

type ServerConfig

type ServerConfig struct {
	Host       string   `mapstructure:"host" yaml:"host"`
	Port       int      `mapstructure:"port" yaml:"port"`
	AllowedIPs []string `mapstructure:"allowed_ips" yaml:"allowed_ips,omitempty"`
	ProxyURL   string   `mapstructure:"proxy_url" yaml:"proxy_url,omitempty"`
}

func (ServerConfig) IsLocalhost added in v0.1.13

func (s ServerConfig) IsLocalhost() bool

IsLocalhost returns true if the host is 127.0.0.1 or localhost.

type Severity

type Severity string
const (
	SeverityError   Severity = "error"
	SeverityWarning Severity = "warning"
)

type ValidationIssue

type ValidationIssue struct {
	Severity Severity
	Message  string
}

type ValidationResult

type ValidationResult struct {
	Errors   []ValidationIssue
	Warnings []ValidationIssue
}

func Validate

func Validate(cfg *Config) *ValidationResult

func (*ValidationResult) ErrorMessages

func (r *ValidationResult) ErrorMessages() []string

func (*ValidationResult) HasErrors

func (r *ValidationResult) HasErrors() bool

func (*ValidationResult) HasOnlyWarnings

func (r *ValidationResult) HasOnlyWarnings() bool

func (*ValidationResult) WarningMessages

func (r *ValidationResult) WarningMessages() []string

Jump to

Keyboard shortcuts

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