Add support for multiple providers
This commit is contained in:
@@ -3,14 +3,11 @@ package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"code.chimeric.al/chimerical/odidere/internal/job"
|
||||
"code.chimeric.al/chimerical/odidere/internal/llm"
|
||||
"code.chimeric.al/chimerical/odidere/internal/tool"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
@@ -30,6 +27,9 @@ func (cfg TTS) Validate() error {
|
||||
if cfg.URL == "" {
|
||||
return fmt.Errorf("missing URL")
|
||||
}
|
||||
if _, err := url.Parse(cfg.URL); err != nil {
|
||||
return fmt.Errorf("invalid URL %q: %w", cfg.URL, err)
|
||||
}
|
||||
if cfg.Voice == "" {
|
||||
return fmt.Errorf("missing voice")
|
||||
}
|
||||
@@ -42,8 +42,6 @@ func (cfg TTS) Validate() error {
|
||||
}
|
||||
|
||||
// STT holds the configuration for the frontend-facing speech-to-text server.
|
||||
// The URL is passed to the browser via a meta tag; the browser calls
|
||||
// whisper-server directly.
|
||||
type STT struct {
|
||||
// URL is the base URL of the whisper-server.
|
||||
// For example, "http://localhost:8178".
|
||||
@@ -54,6 +52,118 @@ func (cfg STT) Validate() error {
|
||||
if cfg.URL == "" {
|
||||
return fmt.Errorf("missing URL")
|
||||
}
|
||||
if _, err := url.Parse(cfg.URL); err != nil {
|
||||
return fmt.Errorf("invalid URL %q: %w", cfg.URL, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ProviderConfig holds the configuration for a single LLM provider.
|
||||
type ProviderConfig struct {
|
||||
// Name uniquely identifies this provider (e.g., "llama.cpp", "openrouter").
|
||||
Name string `yaml:"name"`
|
||||
// URL is the base URL of the OpenAI-compatible API endpoint.
|
||||
URL string `yaml:"url"`
|
||||
// Key is the API key for authentication.
|
||||
Key string `yaml:"key"`
|
||||
// Timeout is the maximum duration for a query (e.g., "5m").
|
||||
// Defaults to 5 minutes if empty.
|
||||
Timeout string `yaml:"timeout"`
|
||||
// Models is an optional whitelist of model IDs for this provider.
|
||||
Models []string `yaml:"models,omitempty"`
|
||||
}
|
||||
|
||||
func (cfg ProviderConfig) Validate() error {
|
||||
if cfg.Name == "" {
|
||||
return fmt.Errorf("missing name")
|
||||
}
|
||||
if cfg.URL == "" {
|
||||
return fmt.Errorf("missing URL")
|
||||
}
|
||||
if _, err := url.Parse(cfg.URL); err != nil {
|
||||
return fmt.Errorf("invalid URL %q: %w", cfg.URL, err)
|
||||
}
|
||||
if cfg.Timeout != "" {
|
||||
if _, err := time.ParseDuration(cfg.Timeout); err != nil {
|
||||
return fmt.Errorf("invalid timeout: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ModelConfig specifies the provider and model to use.
|
||||
type ModelConfig struct {
|
||||
// Provider is the provider name (e.g., "llama.cpp").
|
||||
Provider string `yaml:"provider,omitempty"`
|
||||
// Model is the model ID within that provider (e.g., "qwen3.6-27b").
|
||||
Model string `yaml:"model,omitempty"`
|
||||
}
|
||||
|
||||
// JobConfig holds the user-facing configuration for a job.
|
||||
type JobConfig struct {
|
||||
// MaxIterations caps the number of agent loop iterations.
|
||||
MaxIterations int `yaml:"max_iterations,omitempty"`
|
||||
// Name uniquely identifies the job within the config.
|
||||
Name string `yaml:"name"`
|
||||
// Model is the optional model for this job.
|
||||
Model ModelConfig `yaml:"model,omitempty"`
|
||||
// Schedule is a cron expression (e.g., "0 9 * * *").
|
||||
Schedule string `yaml:"schedule"`
|
||||
// SystemMessage overrides the global system message for this job.
|
||||
SystemMessage string `yaml:"system_message,omitempty"`
|
||||
// Task is the user-level instruction to give the LLM.
|
||||
Task string `yaml:"task"`
|
||||
// Timeout limits the job execution time (e.g., "10m").
|
||||
Timeout string `yaml:"timeout,omitempty"`
|
||||
}
|
||||
|
||||
func (cfg JobConfig) Validate() error {
|
||||
if cfg.Name == "" {
|
||||
return fmt.Errorf("missing name")
|
||||
}
|
||||
if cfg.Task == "" {
|
||||
return fmt.Errorf("missing task")
|
||||
}
|
||||
if cfg.Schedule == "" {
|
||||
return fmt.Errorf("missing schedule")
|
||||
}
|
||||
if cfg.MaxIterations < 0 {
|
||||
return fmt.Errorf("max_iterations must be non-negative")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ToolConfig represents an external tool definition.
|
||||
type ToolConfig struct {
|
||||
// Name uniquely identifies the tool within a registry.
|
||||
Name string `yaml:"name"`
|
||||
// Description explains the tool's purpose for the LLM.
|
||||
Description string `yaml:"description"`
|
||||
// Command is the executable path or name.
|
||||
Command string `yaml:"command"`
|
||||
// Arguments are Go templates expanded with LLM-provided parameters.
|
||||
Arguments []string `yaml:"arguments"`
|
||||
// Parameters is a JSON Schema describing expected input from the LLM.
|
||||
Parameters map[string]any `yaml:"parameters"`
|
||||
// Timeout limits execution time (e.g., "30s", "5m").
|
||||
Timeout string `yaml:"timeout"`
|
||||
}
|
||||
|
||||
func (cfg ToolConfig) Validate() error {
|
||||
if cfg.Name == "" {
|
||||
return fmt.Errorf("missing name")
|
||||
}
|
||||
if cfg.Description == "" {
|
||||
return fmt.Errorf("missing description")
|
||||
}
|
||||
if cfg.Command == "" {
|
||||
return fmt.Errorf("missing command")
|
||||
}
|
||||
if cfg.Timeout != "" {
|
||||
if _, err := time.ParseDuration(cfg.Timeout); err != nil {
|
||||
return fmt.Errorf("invalid timeout: %v", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -61,27 +171,27 @@ func (cfg STT) Validate() error {
|
||||
type Config struct {
|
||||
// Address is the host:port to listen on.
|
||||
Address string `yaml:"address"`
|
||||
|
||||
// ShutdownTimeout is the maximum time to wait for graceful shutdown.
|
||||
// Defaults to "30s".
|
||||
ShutdownTimeout string `yaml:"shutdown_timeout"`
|
||||
// shutdownTimeout is the parsed duration, set during Load.
|
||||
shutdownTimeout time.Duration `yaml:"-"`
|
||||
|
||||
// LLM configures the language model client.
|
||||
LLM llm.Config `yaml:"llm"`
|
||||
// SystemMessage is the default system prompt prepended to all conversations.
|
||||
SystemMessage string `yaml:"system_message"`
|
||||
// DefaultModel specifies the default provider and model.
|
||||
DefaultModel ModelConfig `yaml:"default_model"`
|
||||
// Providers defines the available LLM providers.
|
||||
Providers []ProviderConfig `yaml:"providers"`
|
||||
// STT configures the speech-to-text server URL passed to the frontend.
|
||||
STT STT `yaml:"stt"`
|
||||
// Jobs defines scheduled autonomous tasks.
|
||||
Jobs []job.Config `yaml:"jobs"`
|
||||
Jobs []JobConfig `yaml:"jobs"`
|
||||
// Tools defines external commands available to the LLM.
|
||||
Tools []tool.Tool `yaml:"tools"`
|
||||
Tools []ToolConfig `yaml:"tools"`
|
||||
// TTS configures the text-to-speech client.
|
||||
TTS TTS `yaml:"tts"`
|
||||
}
|
||||
|
||||
// ApplyDefaults sets default values for optional configuration fields.
|
||||
// Called automatically by Load before validation.
|
||||
func (cfg *Config) ApplyDefaults() {
|
||||
if cfg.ShutdownTimeout == "" {
|
||||
cfg.ShutdownTimeout = "30s"
|
||||
@@ -97,14 +207,23 @@ func (cfg *Config) GetShutdownTimeout() time.Duration {
|
||||
}
|
||||
|
||||
// Validate checks that all required fields are present and valid.
|
||||
// Delegates to each subsection's Validate method.
|
||||
func (cfg *Config) Validate() error {
|
||||
if cfg.Address == "" {
|
||||
return fmt.Errorf("address required")
|
||||
}
|
||||
|
||||
if err := cfg.LLM.Validate(); err != nil {
|
||||
return fmt.Errorf("invalid llm config: %w", err)
|
||||
if cfg.DefaultModel.Provider == "" {
|
||||
return fmt.Errorf("default_model.provider required")
|
||||
}
|
||||
if cfg.DefaultModel.Model == "" {
|
||||
return fmt.Errorf("default_model.model required")
|
||||
}
|
||||
if len(cfg.Providers) == 0 {
|
||||
return fmt.Errorf("at least one provider required")
|
||||
}
|
||||
for i, p := range cfg.Providers {
|
||||
if err := p.Validate(); err != nil {
|
||||
return fmt.Errorf("providers[%d] invalid: %w", i, err)
|
||||
}
|
||||
}
|
||||
if err := cfg.STT.Validate(); err != nil {
|
||||
return fmt.Errorf("invalid stt config: %w", err)
|
||||
@@ -113,28 +232,23 @@ func (cfg *Config) Validate() error {
|
||||
return fmt.Errorf("invalid tts config: %w", err)
|
||||
}
|
||||
if _, err := time.ParseDuration(cfg.ShutdownTimeout); err != nil {
|
||||
return fmt.Errorf(
|
||||
"invalid shutdown_timeout %q: %w",
|
||||
cfg.ShutdownTimeout, err,
|
||||
)
|
||||
return fmt.Errorf("invalid shutdown_timeout %q: %w", cfg.ShutdownTimeout, err)
|
||||
}
|
||||
for i, jc := range cfg.Jobs {
|
||||
if err := jc.Validate(); err != nil {
|
||||
return fmt.Errorf("jobs[%d] invalid: %w", i, err)
|
||||
}
|
||||
}
|
||||
for i, t := range cfg.Tools {
|
||||
if err := t.Validate(); err != nil {
|
||||
for i, tc := range cfg.Tools {
|
||||
if err := tc.Validate(); err != nil {
|
||||
return fmt.Errorf("tools[%d] invalid: %w", i, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Load reads a YAML configuration file, expands environment variables,
|
||||
// applies defaults, and validates the result. Returns an error if the
|
||||
// file cannot be read, parsed, or contains invalid configuration.
|
||||
// applies defaults, and validates the result.
|
||||
func Load(path string) (*Config, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
@@ -142,23 +256,18 @@ func Load(path string) (*Config, error) {
|
||||
}
|
||||
|
||||
// Expand environment variables.
|
||||
// Unset variables are replaced with empty strings.
|
||||
re := regexp.MustCompile(`\$\{([^}]+)\}`)
|
||||
expanded := re.ReplaceAllStringFunc(
|
||||
string(data),
|
||||
func(match string) string {
|
||||
// Extract variable name from ${VAR}.
|
||||
v := match[2 : len(match)-1]
|
||||
return os.Getenv(v)
|
||||
},
|
||||
)
|
||||
expanded := re.ReplaceAllStringFunc(string(data), func(match string) string {
|
||||
v := match[2 : len(match)-1]
|
||||
return os.Getenv(v)
|
||||
})
|
||||
|
||||
var cfg Config
|
||||
if err := yaml.Unmarshal([]byte(expanded), &cfg); err != nil {
|
||||
return nil, fmt.Errorf("parse config: %w", err)
|
||||
}
|
||||
cfg.ApplyDefaults()
|
||||
|
||||
cfg.ApplyDefaults()
|
||||
if err := cfg.Validate(); err != nil {
|
||||
return nil, fmt.Errorf("invalid config: %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user