package config import ( "fmt" "os" "time" "gopkg.in/yaml.v3" ) // Application 应用配置顶层结构 type Application struct { App AppSection `yaml:"app"` Server ServerSection `yaml:"server"` Agent AgentSection `yaml:"agent"` LLM LLMSection `yaml:"llm"` } // AppSection 应用基础信息 type AppSection struct { Name string `yaml:"name"` Env string `yaml:"env"` } // ServerSection 服务器配置 type ServerSection struct { Addr string `yaml:"addr"` } // AgentSection Agent 配置路径列表 type AgentSection struct { ConfigPaths []string `yaml:"config-paths"` } // LLMSection LLM 相关配置 type LLMSection struct { RequestTimeout string `yaml:"request-timeout"` } const defaultLLMRequestTimeout = 5 * time.Minute // RequestTimeoutDuration 解析 LLM 请求超时时间 func (s LLMSection) RequestTimeoutDuration() (time.Duration, error) { if s.RequestTimeout == "" { return defaultLLMRequestTimeout, nil } d, err := time.ParseDuration(s.RequestTimeout) if err != nil { return 0, fmt.Errorf("invalid llm.request-timeout %q: %w", s.RequestTimeout, err) } if d <= 0 { return 0, fmt.Errorf("llm.request-timeout must be positive, got %q", s.RequestTimeout) } return d, nil } // LoadApplication 从指定路径加载应用配置 func LoadApplication(path string) (Application, error) { data, err := os.ReadFile(path) if err != nil { return Application{}, fmt.Errorf("read application config %s: %w", path, err) } var app Application if err := yaml.Unmarshal(data, &app); err != nil { return Application{}, fmt.Errorf("parse application config: %w", err) } // 设置默认值 if app.Server.Addr == "" { app.Server.Addr = ":8091" } if app.App.Env == "" { app.App.Env = "local" } return app, nil }