package app import ( "fmt" "net/netip" "net/url" "os" "strconv" "strings" "time" ) type Config struct { AdminPassword string AdminCookieSecure bool AuthStorePath string Addr, Token, DBPath, BrowserPath, KagiSession, KagiToken, SearxURL string JevURL, JevKey, JevModel, LLMURL, LLMKey, LLMModel string Concurrency, Candidates, Passages, EvidenceChars, MaxPageBytes, CacheBytes int JevConcurrency int RequestTimeout time.Duration RenderWaitMS time.Duration ProxyPool []*url.URL AllowPrivate bool Stealth bool Headless bool Public PublicConfig Local LocalConfig // JevPricePerMillion prices decision input tokens on the admin dashboard. JevPricePerMillion float64 // LLMTemperature is the summarizer sampling temperature (LLM_TEMPERATURE, 0-2). LLMTemperature float64 } // LocalConfig is a local decision model asked before the remote one; see LocalTier. type LocalConfig struct { URL, Model string Questions []string Low, High float64 // Scope is "public" (public calls only) or "all". Scope string } // PublicConfig opens the API to callers without a token, for the public // instance: rate limited, logged only on opt-in, and optionally with its own // decision model. type PublicConfig struct { Enabled bool RatePerMinute int DailyLimit int LogPath string JevURL, JevKey, JevModel string // Fallback decision model for public calls, used when the public one fails. FallbackURL, FallbackKey, FallbackModel string // TrustedProxies may set X-Forwarded-For; the client address is read from it. TrustedProxies []netip.Prefix } func readConfig() (Config, error) { c := Config{ AuthStorePath: os.Getenv("AUTH_STORE_PATH"), AdminPassword: os.Getenv("ADMIN_PASSWORD"), Addr: env("BIND_ADDR", "127.0.0.1:8788"), Token: os.Getenv("API_AUTH_TOKEN"), DBPath: env("COMPLETIONS_DB_PATH", "data/completions.db"), BrowserPath: os.Getenv("BROWSER_PATH"), KagiSession: os.Getenv("KAGI_SESSION"), KagiToken: os.Getenv("KAGI_SEARCH_TOKEN"), SearxURL: os.Getenv("SEARX_URL"), JevURL: env("TYPESAFE_BASE_URL", "https://api.typesafe.ai/v1"), JevKey: os.Getenv("TYPESAFE_API_KEY"), JevModel: env("TYPESAFE_MODEL", "jev-1.13.0"), LLMURL: env("LLM_BASE_URL", "https://api.deepseek.com"), LLMKey: os.Getenv("LLM_API_KEY"), LLMModel: env("LLM_MODEL", "deepseek-v4-flash"), Concurrency: 4, Candidates: 20, Passages: 24, EvidenceChars: 5500, MaxPageBytes: 5 << 20, CacheBytes: 32 << 20, JevConcurrency: 8, JevPricePerMillion: 0.042, LLMTemperature: 1.0, RequestTimeout: 90 * time.Second, RenderWaitMS: 1200 * time.Millisecond, Stealth: true, Headless: true, } if c.Token == "" || strings.ContainsAny(c.Token, " \t\r\n") { return c, fmt.Errorf("API_AUTH_TOKEN must be set without whitespace") } if c.AdminPassword != "" && len(c.AdminPassword) < 12 { return c, fmt.Errorf("ADMIN_PASSWORD must be at least 12 bytes, or empty to disable the dashboard") } if value := os.Getenv("ADMIN_COOKIE_SECURE"); value != "" { var err error c.AdminCookieSecure, err = strconv.ParseBool(value) if err != nil { return c, fmt.Errorf("ADMIN_COOKIE_SECURE must be a boolean") } } for name, target := range map[string]*int{"CONCURRENCY": &c.Concurrency, "JEV_CONCURRENCY": &c.JevConcurrency, "SEARCH_CANDIDATES": &c.Candidates, "PASSAGE_CANDIDATES": &c.Passages, "EVIDENCE_CHARS": &c.EvidenceChars, "MAX_PAGE_BYTES": &c.MaxPageBytes, "CACHE_BYTES": &c.CacheBytes} { if s := os.Getenv(name); s != "" { v, err := strconv.Atoi(s) if err != nil || v < 1 { return c, fmt.Errorf("%s must be a positive integer", name) } *target = v } } if c.Concurrency > 16 || c.JevConcurrency > 16 || c.Candidates > 40 || c.Passages > 64 || c.EvidenceChars > 20000 || c.MaxPageBytes > 25<<20 || c.CacheBytes > 256<<20 { return c, fmt.Errorf("configured resource limits exceed supported bounds") } if s := os.Getenv("JEV_PRICE_PER_MILLION"); s != "" { v, err := strconv.ParseFloat(s, 64) if err != nil || v < 0 { return c, fmt.Errorf("JEV_PRICE_PER_MILLION must be a non-negative number") } c.JevPricePerMillion = v } if s := os.Getenv("LLM_TEMPERATURE"); s != "" { v, err := strconv.ParseFloat(s, 64) if err != nil || v < 0 || v > 2 { return c, fmt.Errorf("LLM_TEMPERATURE must be a number from 0 to 2") } c.LLMTemperature = v } if s := os.Getenv("RENDER_WAIT_MS"); s != "" { v, err := strconv.Atoi(s) if err != nil || v < 0 || v > 10000 { return c, fmt.Errorf("RENDER_WAIT_MS must be 0–10000 milliseconds") } c.RenderWaitMS = time.Duration(v) * time.Millisecond } for name, target := range map[string]*bool{"STEALTH": &c.Stealth, "BROWSER_HEADLESS": &c.Headless} { if s := os.Getenv(name); s != "" { v, err := strconv.ParseBool(s) if err != nil { return c, fmt.Errorf("%s must be a boolean", name) } *target = v } } if s := os.Getenv("PROXY_POOL"); s != "" { for _, entry := range strings.Split(s, ",") { entry = strings.TrimSpace(entry) if entry == "" { continue } u, err := url.Parse(entry) if err != nil || u.Host == "" || (u.Scheme != "https" && u.Scheme != "http") { return c, fmt.Errorf("PROXY_POOL entries must be absolute http(s) proxy urls") } c.ProxyPool = append(c.ProxyPool, u) } } if s := os.Getenv("ALLOW_PRIVATE_NETWORK"); s != "" { b, err := strconv.ParseBool(s) if err != nil { return c, err } c.AllowPrivate = b } public, err := readPublicConfig() if err != nil { return c, err } c.Public = public if c.Local, err = readLocalConfig(); err != nil { return c, err } for _, endpoint := range []string{c.JevURL, c.LLMURL, c.Public.JevURL, c.Public.FallbackURL, c.Local.URL} { if endpoint == "" { continue } u, err := url.Parse(endpoint) if err != nil || u.Host == "" || (u.Scheme != "https" && u.Scheme != "http") || u.User != nil { return c, fmt.Errorf("invalid model base url") } } return c, nil } func env(name, fallback string) string { if s := os.Getenv(name); s != "" { return s } return fallback } func readPublicConfig() (PublicConfig, error) { p := PublicConfig{ RatePerMinute: 20, LogPath: env("PUBLIC_LOG_PATH", "data/public.db"), JevURL: os.Getenv("PUBLIC_JEV_URL"), JevKey: os.Getenv("PUBLIC_JEV_KEY"), JevModel: env("PUBLIC_JEV_MODEL", "clef-flash"), FallbackURL: os.Getenv("PUBLIC_JEV_FALLBACK_URL"), FallbackKey: os.Getenv("PUBLIC_JEV_FALLBACK_KEY"), FallbackModel: env("PUBLIC_JEV_FALLBACK_MODEL", "jev-1.13-free"), } if s := os.Getenv("PUBLIC_ACCESS"); s != "" { b, err := strconv.ParseBool(s) if err != nil { return p, fmt.Errorf("PUBLIC_ACCESS must be a boolean") } p.Enabled = b } if s := os.Getenv("PUBLIC_RATE_PER_MINUTE"); s != "" { v, err := strconv.Atoi(s) if err != nil || v < 1 { return p, fmt.Errorf("PUBLIC_RATE_PER_MINUTE must be a positive integer") } p.RatePerMinute = v } // 0, the default, means no daily limit. if s := os.Getenv("PUBLIC_DAILY_LIMIT"); s != "" { v, err := strconv.Atoi(s) if err != nil || v < 0 { return p, fmt.Errorf("PUBLIC_DAILY_LIMIT must be a non-negative integer") } p.DailyLimit = v } if (p.JevURL == "") != (p.JevKey == "") || (p.FallbackURL == "") != (p.FallbackKey == "") { return p, fmt.Errorf("PUBLIC_JEV_URL/KEY and PUBLIC_JEV_FALLBACK_URL/KEY each go together") } if p.FallbackURL != "" && p.JevURL == "" { return p, fmt.Errorf("PUBLIC_JEV_FALLBACK_URL needs PUBLIC_JEV_URL") } for _, entry := range strings.Split(os.Getenv("TRUSTED_PROXIES"), ",") { entry = strings.TrimSpace(entry) if entry == "" { continue } prefix, err := netip.ParsePrefix(entry) if err != nil { addr, e := netip.ParseAddr(entry) if e != nil { return p, fmt.Errorf("TRUSTED_PROXIES entries must be addresses or prefixes") } prefix = netip.PrefixFrom(addr, addr.BitLen()) } p.TrustedProxies = append(p.TrustedProxies, prefix) } return p, nil } func readLocalConfig() (LocalConfig, error) { l := LocalConfig{ URL: os.Getenv("LOCAL_DECISION_URL"), Model: env("LOCAL_DECISION_MODEL", "local-decision"), Low: 0.1, High: 0.9, Scope: env("LOCAL_DECISION_SCOPE", "public"), } for _, name := range strings.Split(env("LOCAL_DECISION_QUESTIONS", "evidence,useful"), ",") { if name = strings.TrimSpace(name); name != "" { l.Questions = append(l.Questions, name) } } for name, target := range map[string]*float64{"LOCAL_DECISION_LOW": &l.Low, "LOCAL_DECISION_HIGH": &l.High} { if s := os.Getenv(name); s != "" { v, err := strconv.ParseFloat(s, 64) if err != nil || v < 0 || v > 1 { return l, fmt.Errorf("%s must be between 0 and 1", name) } *target = v } } if l.Low >= l.High { return l, fmt.Errorf("LOCAL_DECISION_LOW must be below LOCAL_DECISION_HIGH") } if l.Scope != "public" && l.Scope != "all" { return l, fmt.Errorf("LOCAL_DECISION_SCOPE must be public or all") } return l, nil }