140 lines
3.2 KiB
Go
140 lines
3.2 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.warky.dev/wdevs/amcs/internal/buildinfo"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
func Load(explicitPath string) (*Config, string, error) {
|
|
path := ResolvePath(explicitPath)
|
|
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, path, fmt.Errorf("read config %q: %w", path, err)
|
|
}
|
|
|
|
cfg := defaultConfig()
|
|
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
|
return nil, path, fmt.Errorf("decode config %q: %w", path, err)
|
|
}
|
|
|
|
applyEnvOverrides(&cfg)
|
|
if err := cfg.Validate(); err != nil {
|
|
return nil, path, err
|
|
}
|
|
|
|
return &cfg, path, nil
|
|
}
|
|
|
|
func ResolvePath(explicitPath string) string {
|
|
if path := strings.TrimSpace(explicitPath); path != "" {
|
|
if path != ".yaml" && path != ".yml" {
|
|
return path
|
|
}
|
|
}
|
|
|
|
if envPath := strings.TrimSpace(os.Getenv("AMCS_CONFIG")); envPath != "" {
|
|
return envPath
|
|
}
|
|
|
|
return DefaultConfigPath
|
|
}
|
|
|
|
func defaultConfig() Config {
|
|
info := buildinfo.Current()
|
|
return Config{
|
|
Server: ServerConfig{
|
|
Host: "0.0.0.0",
|
|
Port: 8080,
|
|
ReadTimeout: 10 * time.Minute,
|
|
WriteTimeout: 10 * time.Minute,
|
|
IdleTimeout: 60 * time.Second,
|
|
},
|
|
MCP: MCPConfig{
|
|
Path: "/mcp",
|
|
SSEPath: "/sse",
|
|
ServerName: "amcs",
|
|
Version: info.Version,
|
|
Transport: "streamable_http",
|
|
SessionTimeout: 10 * time.Minute,
|
|
},
|
|
Auth: AuthConfig{
|
|
HeaderName: "x-brain-key",
|
|
QueryParam: "key",
|
|
},
|
|
AI: AIConfig{
|
|
Provider: "litellm",
|
|
Embeddings: AIEmbeddingConfig{
|
|
Model: "openai/text-embedding-3-small",
|
|
Dimensions: 1536,
|
|
},
|
|
Metadata: AIMetadataConfig{
|
|
Model: "gpt-4o-mini",
|
|
Temperature: 0.1,
|
|
Timeout: 10 * time.Second,
|
|
},
|
|
Ollama: OllamaConfig{
|
|
BaseURL: "http://localhost:11434/v1",
|
|
APIKey: "ollama",
|
|
},
|
|
},
|
|
Capture: CaptureConfig{
|
|
Source: DefaultSource,
|
|
MetadataDefaults: CaptureMetadataDefault{
|
|
Type: "observation",
|
|
TopicFallback: "uncategorized",
|
|
},
|
|
},
|
|
Search: SearchConfig{
|
|
DefaultLimit: 10,
|
|
DefaultThreshold: 0.5,
|
|
MaxLimit: 50,
|
|
},
|
|
Logging: LoggingConfig{
|
|
Level: "info",
|
|
Format: "json",
|
|
},
|
|
Backfill: BackfillConfig{
|
|
Enabled: false,
|
|
RunOnStartup: false,
|
|
Interval: 15 * time.Minute,
|
|
BatchSize: 20,
|
|
MaxPerRun: 100,
|
|
},
|
|
MetadataRetry: MetadataRetryConfig{
|
|
Enabled: false,
|
|
RunOnStartup: false,
|
|
Interval: 24 * time.Hour,
|
|
MaxPerRun: 100,
|
|
},
|
|
}
|
|
}
|
|
|
|
func applyEnvOverrides(cfg *Config) {
|
|
overrideString(&cfg.Database.URL, "AMCS_DATABASE_URL")
|
|
overrideString(&cfg.MCP.PublicURL, "AMCS_PUBLIC_URL")
|
|
overrideString(&cfg.AI.LiteLLM.BaseURL, "AMCS_LITELLM_BASE_URL")
|
|
overrideString(&cfg.AI.LiteLLM.APIKey, "AMCS_LITELLM_API_KEY")
|
|
overrideString(&cfg.AI.Ollama.BaseURL, "AMCS_OLLAMA_BASE_URL")
|
|
overrideString(&cfg.AI.Ollama.APIKey, "AMCS_OLLAMA_API_KEY")
|
|
overrideString(&cfg.AI.OpenRouter.APIKey, "AMCS_OPENROUTER_API_KEY")
|
|
|
|
if value, ok := os.LookupEnv("AMCS_SERVER_PORT"); ok {
|
|
if port, err := strconv.Atoi(strings.TrimSpace(value)); err == nil {
|
|
cfg.Server.Port = port
|
|
}
|
|
}
|
|
}
|
|
|
|
func overrideString(target *string, envKey string) {
|
|
if value, ok := os.LookupEnv(envKey); ok {
|
|
*target = strings.TrimSpace(value)
|
|
}
|
|
}
|