// Package config loads localchat settings from the environment and an // optional .env file. package config import ( "bufio" "fmt" "os" "strconv" "strings" "time" ) // DefaultSystemPrompt is used when SYSTEM_PROMPT is not set. const DefaultSystemPrompt = "You are a friendly, concise assistant running fully offline on the user's own machine. Answer clearly. Use Markdown when it helps." // Config holds all runtime settings. type Config struct { Addr string // LOCALCHAT_ADDR: where the web UI listens BaseURL string // LLM_BASE_URL: OpenAI-compatible API root, e.g. http://127.0.0.1:8000/v1 APIKey string // LLM_API_KEY: optional bearer token for the model server Model string // LLM_MODEL: model id; empty means "first model the server lists" SystemPrompt string // SYSTEM_PROMPT Title string // LOCALCHAT_TITLE: name shown in the UI MaxHistory int // MAX_HISTORY: messages sent back to the model as context Timeout time.Duration // LLM_TIMEOUT: upper bound for a single reply } // Load reads the configuration from environment variables, applying defaults. func Load() (Config, error) { c := Config{ Addr: env("LOCALCHAT_ADDR", "127.0.0.1:3000"), BaseURL: strings.TrimRight(env("LLM_BASE_URL", "http://127.0.0.1:8000/v1"), "/"), APIKey: os.Getenv("LLM_API_KEY"), Model: os.Getenv("LLM_MODEL"), SystemPrompt: env("SYSTEM_PROMPT", DefaultSystemPrompt), Title: env("LOCALCHAT_TITLE", "localchat"), MaxHistory: 20, Timeout: 5 * time.Minute, } if v := os.Getenv("MAX_HISTORY"); v != "" { n, err := strconv.Atoi(v) if err != nil || n < 1 { return c, fmt.Errorf("MAX_HISTORY must be a positive integer, got %q", v) } c.MaxHistory = n } if v := os.Getenv("LLM_TIMEOUT"); v != "" { d, err := time.ParseDuration(v) if err != nil { return c, fmt.Errorf("LLM_TIMEOUT: %w", err) } c.Timeout = d } return c, nil } func env(key, fallback string) string { if v, ok := os.LookupEnv(key); ok && v != "" { return v } return fallback } // LoadEnvFile sets variables from a KEY=VALUE file. Variables that are // already set in the environment win, so the real environment can override // the file. Blank lines and lines starting with # are ignored; values may be // wrapped in single or double quotes. func LoadEnvFile(path string) error { f, err := os.Open(path) if err != nil { return err } defer f.Close() sc := bufio.NewScanner(f) for n := 1; sc.Scan(); n++ { line := strings.TrimSpace(sc.Text()) if line == "" || strings.HasPrefix(line, "#") { continue } key, val, ok := strings.Cut(strings.TrimPrefix(line, "export "), "=") if !ok { return fmt.Errorf("%s:%d: expected KEY=VALUE", path, n) } key, val = strings.TrimSpace(key), strings.TrimSpace(val) if len(val) >= 2 && (val[0] == '"' || val[0] == '\'') && val[len(val)-1] == val[0] { val = val[1 : len(val)-1] } if _, set := os.LookupEnv(key); !set { os.Setenv(key, val) } } return sc.Err() }