Add support for multiple providers

This commit is contained in:
dwrz
2026-06-19 14:31:41 +00:00
parent c71300b1bf
commit f5e72b1c7a
12 changed files with 948 additions and 869 deletions

View File

@@ -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)
}

View File

@@ -7,15 +7,20 @@ import (
"time"
)
// validConfig returns a minimal valid YAML configuration.
// validConfig returns a minimal valid YAML configuration using the
// multi-provider format.
func validConfig() string {
return `
address: ":9090"
shutdown_timeout: "60s"
llm:
default_model:
provider: testprovider
model: test-model
url: http://localhost:8080
providers:
- name: testprovider
url: http://localhost:8080
stt:
url: http://localhost:8178
@@ -71,9 +76,13 @@ func TestLoad(t *testing.T) {
{
name: "defaults applied",
config: `
llm:
default_model:
provider: testprovider
model: test-model
url: http://localhost:8080
providers:
- name: testprovider
url: http://localhost:8080
stt:
url: http://localhost:8178
@@ -106,10 +115,14 @@ tts:
{
name: "env expansion",
config: `
llm:
default_model:
provider: testprovider
model: test-model
url: http://localhost:8080
key: ${TEST_LLM_KEY}
providers:
- name: testprovider
url: http://localhost:8080
key: ${TEST_LLM_KEY}
stt:
url: http://localhost:8178
@@ -122,10 +135,13 @@ tts:
t.Setenv("TEST_LLM_KEY", "secret-api-key")
},
check: func(t *testing.T, cfg *Config) {
if cfg.LLM.Key != "secret-api-key" {
if len(cfg.Providers) != 1 {
t.Fatalf("len(Providers) = %d, want 1", len(cfg.Providers))
}
if cfg.Providers[0].Key != "secret-api-key" {
t.Errorf(
"LLM.Key = %q, want %q",
cfg.LLM.Key, "secret-api-key",
"Providers[0].Key = %q, want %q",
cfg.Providers[0].Key, "secret-api-key",
)
}
},
@@ -133,10 +149,14 @@ tts:
{
name: "env expansion unset var becomes empty",
config: `
llm:
default_model:
provider: testprovider
model: test-model
url: http://localhost:8080
key: ${TEST_UNSET_VAR}
providers:
- name: testprovider
url: http://localhost:8080
key: ${TEST_UNSET_VAR}
stt:
url: http://localhost:8178
@@ -146,17 +166,20 @@ tts:
voice: af_heart
`,
check: func(t *testing.T, cfg *Config) {
if cfg.LLM.Key != "" {
if len(cfg.Providers) != 1 {
t.Fatalf("len(Providers) = %d, want 1", len(cfg.Providers))
}
if cfg.Providers[0].Key != "" {
t.Errorf(
"LLM.Key = %q, want empty",
cfg.LLM.Key,
"Providers[0].Key = %q, want empty",
cfg.Providers[0].Key,
)
}
},
},
{
name: "invalid yaml",
config: `llm: [invalid yaml`,
config: `providers: [invalid yaml`,
shouldErr: true,
},
{
@@ -164,9 +187,13 @@ tts:
config: `
shutdown_timeout: "not-a-duration"
llm:
default_model:
provider: testprovider
model: test-model
url: http://localhost:8080
providers:
- name: testprovider
url: http://localhost:8080
stt:
url: http://localhost:8178
@@ -178,10 +205,14 @@ tts:
shouldErr: true,
},
{
name: "missing llm model",
name: "missing default_model.provider",
config: `
llm:
url: http://localhost:8080
default_model:
model: test-model
providers:
- name: testprovider
url: http://localhost:8080
stt:
url: http://localhost:8178
@@ -193,11 +224,34 @@ tts:
shouldErr: true,
},
{
name: "missing llm url",
name: "missing default_model.model",
config: `
llm:
default_model:
provider: testprovider
providers:
- name: testprovider
url: http://localhost:8080
stt:
url: http://localhost:8178
tts:
url: http://localhost:8880
voice: af_heart
`,
shouldErr: true,
},
{
name: "missing provider url",
config: `
default_model:
provider: testprovider
model: test-model
providers:
- name: testprovider
stt:
url: http://localhost:8178
@@ -210,9 +264,13 @@ tts:
{
name: "missing stt url",
config: `
llm:
default_model:
provider: testprovider
model: test-model
url: http://localhost:8080
providers:
- name: testprovider
url: http://localhost:8080
stt:
url: ""
@@ -226,9 +284,13 @@ tts:
{
name: "missing tts url",
config: `
llm:
default_model:
provider: testprovider
model: test-model
url: http://localhost:8080
providers:
- name: testprovider
url: http://localhost:8080
stt:
url: http://localhost:8178
@@ -241,9 +303,13 @@ tts:
{
name: "missing tts voice",
config: `
llm:
default_model:
provider: testprovider
model: test-model
url: http://localhost:8080
providers:
- name: testprovider
url: http://localhost:8080
stt:
url: http://localhost:8178
@@ -256,9 +322,13 @@ tts:
{
name: "config with tools",
config: `
llm:
default_model:
provider: testprovider
model: test-model
url: http://localhost:8080
providers:
- name: testprovider
url: http://localhost:8080
stt:
url: http://localhost:8178
@@ -292,9 +362,13 @@ tools:
{
name: "invalid tool",
config: `
llm:
default_model:
provider: testprovider
model: test-model
url: http://localhost:8080
providers:
- name: testprovider
url: http://localhost:8080
stt:
url: http://localhost:8178
@@ -388,9 +462,13 @@ func TestLoad_Jobs(t *testing.T) {
{
name: "valid job config",
config: `
llm:
default_model:
provider: testprovider
model: test-model
url: http://localhost:8080
providers:
- name: testprovider
url: http://localhost:8080
stt:
url: http://localhost:8178
@@ -402,7 +480,6 @@ tts:
jobs:
- name: daily_summary
schedule: "0 9 * * *"
enabled: true
system_message: "You are a research assistant."
task: "Summarize today's news about AI."
timeout: "10m"
@@ -432,17 +509,18 @@ jobs:
if j.MaxIterations != 20 {
t.Errorf("MaxIterations = %d, want 20", j.MaxIterations)
}
if !j.IsEnabled() {
t.Error("IsEnabled() = false, want true")
}
},
},
{
name: "job with minimal config (defaults)",
config: `
llm:
default_model:
provider: testprovider
model: test-model
url: http://localhost:8080
providers:
- name: testprovider
url: http://localhost:8080
stt:
url: http://localhost:8178
@@ -466,41 +544,16 @@ jobs:
}
},
},
{
name: "disabled job",
config: `
llm:
model: test-model
url: http://localhost:8080
stt:
url: http://localhost:8178
tts:
url: http://localhost:8880
voice: af_heart
jobs:
- name: disabled_job
schedule: "0 0 * * *"
enabled: false
task: "This should not run."
`,
check: func(t *testing.T, cfg *Config) {
if len(cfg.Jobs) != 1 {
t.Fatalf("len(Jobs) = %d, want 1", len(cfg.Jobs))
}
if cfg.Jobs[0].IsEnabled() {
t.Error("IsEnabled() = true, want false")
}
},
},
{
name: "job missing name",
config: `
llm:
default_model:
provider: testprovider
model: test-model
url: http://localhost:8080
providers:
- name: testprovider
url: http://localhost:8080
stt:
url: http://localhost:8178
@@ -518,9 +571,13 @@ jobs:
{
name: "job missing task",
config: `
llm:
default_model:
provider: testprovider
model: test-model
url: http://localhost:8080
providers:
- name: testprovider
url: http://localhost:8080
stt:
url: http://localhost:8178