config

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 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"
)

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.

Types

type Config

type Config struct {
	Server       ServerConfig              `mapstructure:"server" yaml:"server"`
	DefaultRoute string                    `mapstructure:"default_route" yaml:"default_route"`
	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() *RouteRule

DefaultRouteConfig returns the default route rule, or nil if not set.

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"`
	Format   string   `mapstructure:"format" yaml:"format"`
	LogoURL  string   `mapstructure:"logo_url" yaml:"logo_url"`
	Sponsor  bool     `mapstructure:"sponsor" yaml:"sponsor"`
	ThinkTag string   `mapstructure:"think_tag" yaml:"think_tag,omitempty"`
	Models   []string `mapstructure:"models" yaml:"models,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"`
	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"`
}

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