2026-02-13 15:03:02 +00:00
|
|
|
// Package config loads and validates YAML configuration for odidere.
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"regexp"
|
|
|
|
|
"time"
|
|
|
|
|
|
2026-06-02 12:03:15 +00:00
|
|
|
"code.chimeric.al/chimerical/odidere/internal/job"
|
2026-02-21 19:47:00 +00:00
|
|
|
"code.chimeric.al/chimerical/odidere/internal/llm"
|
|
|
|
|
"code.chimeric.al/chimerical/odidere/internal/stt"
|
|
|
|
|
"code.chimeric.al/chimerical/odidere/internal/tool"
|
2026-02-13 15:03:02 +00:00
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
)
|
|
|
|
|
|
2026-06-13 18:55:16 +00:00
|
|
|
// TTS holds the configuration for TTS.
|
|
|
|
|
type TTS struct {
|
|
|
|
|
// URL is the base URL of kokoro-fastapi.
|
|
|
|
|
// For example, "http://localhost:8880".
|
|
|
|
|
URL string `yaml:"url"`
|
|
|
|
|
// Voice is the default voice ID to use (e.g., "af_heart").
|
|
|
|
|
Voice string `yaml:"voice"`
|
|
|
|
|
// Timeout is the maximum duration for a synthesis request.
|
|
|
|
|
// Defaults to 60s if empty.
|
|
|
|
|
Timeout string `yaml:"timeout"`
|
|
|
|
|
// VoiceMap maps whisper language names to voice IDs.
|
|
|
|
|
// Used by SelectVoice to auto-select voices based on detected
|
|
|
|
|
// language.
|
|
|
|
|
VoiceMap map[string]string `yaml:"voice_map"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (cfg TTS) Validate() error {
|
|
|
|
|
if cfg.URL == "" {
|
|
|
|
|
return fmt.Errorf("missing URL")
|
|
|
|
|
}
|
|
|
|
|
if cfg.Voice == "" {
|
|
|
|
|
return fmt.Errorf("missing voice")
|
|
|
|
|
}
|
|
|
|
|
if cfg.Timeout != "" {
|
|
|
|
|
if _, err := time.ParseDuration(cfg.Timeout); err != nil {
|
|
|
|
|
return fmt.Errorf("invalid timeout: %w", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-13 15:03:02 +00:00
|
|
|
// Config holds all application configuration sections.
|
|
|
|
|
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"`
|
|
|
|
|
// STT configures the speech-to-text client.
|
|
|
|
|
STT stt.Config `yaml:"stt"`
|
2026-06-02 12:03:15 +00:00
|
|
|
// Jobs defines scheduled autonomous tasks.
|
|
|
|
|
Jobs []job.Config `yaml:"jobs"`
|
2026-02-13 15:03:02 +00:00
|
|
|
// Tools defines external commands available to the LLM.
|
|
|
|
|
Tools []tool.Tool `yaml:"tools"`
|
|
|
|
|
// TTS configures the text-to-speech client.
|
2026-06-13 18:55:16 +00:00
|
|
|
TTS TTS `yaml:"tts"`
|
2026-02-13 15:03:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ApplyDefaults sets default values for optional configuration fields.
|
|
|
|
|
// Called automatically by Load before validation.
|
|
|
|
|
func (cfg *Config) ApplyDefaults() {
|
|
|
|
|
if cfg.ShutdownTimeout == "" {
|
|
|
|
|
cfg.ShutdownTimeout = "30s"
|
|
|
|
|
}
|
|
|
|
|
if cfg.Address == "" {
|
|
|
|
|
cfg.Address = ":8080"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetShutdownTimeout returns the parsed shutdown timeout duration.
|
|
|
|
|
func (cfg *Config) GetShutdownTimeout() time.Duration {
|
|
|
|
|
return cfg.shutdownTimeout
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 err := cfg.STT.Validate(); err != nil {
|
|
|
|
|
return fmt.Errorf("invalid stt config: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if err := cfg.TTS.Validate(); err != nil {
|
|
|
|
|
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,
|
|
|
|
|
)
|
|
|
|
|
}
|
2026-06-02 12:03:15 +00:00
|
|
|
for i, jc := range cfg.Jobs {
|
|
|
|
|
if err := jc.Validate(); err != nil {
|
|
|
|
|
return fmt.Errorf("jobs[%d] invalid: %w", i, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-13 15:03:02 +00:00
|
|
|
for i, t := range cfg.Tools {
|
|
|
|
|
if err := t.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.
|
|
|
|
|
func Load(path string) (*Config, error) {
|
|
|
|
|
data, err := os.ReadFile(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("read file: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var cfg Config
|
|
|
|
|
if err := yaml.Unmarshal([]byte(expanded), &cfg); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("parse config: %w", err)
|
|
|
|
|
}
|
|
|
|
|
cfg.ApplyDefaults()
|
|
|
|
|
|
|
|
|
|
if err := cfg.Validate(); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("invalid config: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
d, err := time.ParseDuration(cfg.ShutdownTimeout)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("parse shutdown timeout: %v", err)
|
|
|
|
|
}
|
|
|
|
|
cfg.shutdownTimeout = d
|
|
|
|
|
|
|
|
|
|
return &cfg, nil
|
|
|
|
|
}
|