okami.mom/web-extract-go

git clone https://git.nkp.pet/okami.mom/web-extract-go

albedosummaries: LLM_TEMPERATURE sets the summarizer temperature (default 1.0)20f80af

main
9.5 KiB257 linesraw
1package app
2
3import (
4	"fmt"
5	"net/netip"
6	"net/url"
7	"os"
8	"strconv"
9	"strings"
10	"time"
11)
12
13type Config struct {
14	AdminPassword                                                              string
15	AdminCookieSecure                                                          bool
16	AuthStorePath                                                              string
17	Addr, Token, DBPath, BrowserPath, KagiSession, KagiToken, SearxURL         string
18	JevURL, JevKey, JevModel, LLMURL, LLMKey, LLMModel                         string
19	Concurrency, Candidates, Passages, EvidenceChars, MaxPageBytes, CacheBytes int
20	JevConcurrency                                                             int
21	RequestTimeout                                                             time.Duration
22	RenderWaitMS                                                               time.Duration
23	ProxyPool                                                                  []*url.URL
24	AllowPrivate                                                               bool
25	Stealth                                                                    bool
26	Headless                                                                   bool
27	Public                                                                     PublicConfig
28	Local                                                                      LocalConfig
29	// JevPricePerMillion prices decision input tokens on the admin dashboard.
30	JevPricePerMillion float64
31	// LLMTemperature is the summarizer sampling temperature (LLM_TEMPERATURE, 0-2).
32	LLMTemperature float64
33}
34
35// LocalConfig is a local decision model asked before the remote one; see LocalTier.
36type LocalConfig struct {
37	URL, Model string
38	Questions  []string
39	Low, High  float64
40	// Scope is "public" (public calls only) or "all".
41	Scope string
42}
43
44// PublicConfig opens the API to callers without a token, for the public
45// instance: rate limited, logged only on opt-in, and optionally with its own
46// decision model.
47type PublicConfig struct {
48	Enabled                  bool
49	RatePerMinute            int
50	DailyLimit               int
51	LogPath                  string
52	JevURL, JevKey, JevModel string
53	// Fallback decision model for public calls, used when the public one fails.
54	FallbackURL, FallbackKey, FallbackModel string
55	// TrustedProxies may set X-Forwarded-For; the client address is read from it.
56	TrustedProxies []netip.Prefix
57}
58
59func readConfig() (Config, error) {
60	c := Config{
61		AuthStorePath: os.Getenv("AUTH_STORE_PATH"),
62		AdminPassword: os.Getenv("ADMIN_PASSWORD"),
63		Addr:          env("BIND_ADDR", "127.0.0.1:8788"), Token: os.Getenv("API_AUTH_TOKEN"),
64		DBPath: env("COMPLETIONS_DB_PATH", "data/completions.db"), BrowserPath: os.Getenv("BROWSER_PATH"),
65		KagiSession: os.Getenv("KAGI_SESSION"), KagiToken: os.Getenv("KAGI_SEARCH_TOKEN"), SearxURL: os.Getenv("SEARX_URL"),
66		JevURL: env("TYPESAFE_BASE_URL", "https://api.typesafe.ai/v1"), JevKey: os.Getenv("TYPESAFE_API_KEY"), JevModel: env("TYPESAFE_MODEL", "jev-1.13.0"),
67		LLMURL: env("LLM_BASE_URL", "https://api.deepseek.com"), LLMKey: os.Getenv("LLM_API_KEY"), LLMModel: env("LLM_MODEL", "deepseek-v4-flash"),
68		Concurrency: 4, Candidates: 20, Passages: 24, EvidenceChars: 5500, MaxPageBytes: 5 << 20, CacheBytes: 32 << 20,
69		JevConcurrency: 8, JevPricePerMillion: 0.042, LLMTemperature: 1.0,
70		RequestTimeout: 90 * time.Second,
71		RenderWaitMS:   1200 * time.Millisecond,
72		Stealth:        true,
73		Headless:       true,
74	}
75	if c.Token == "" || strings.ContainsAny(c.Token, " \t\r\n") {
76		return c, fmt.Errorf("API_AUTH_TOKEN must be set without whitespace")
77	}
78	if c.AdminPassword != "" && len(c.AdminPassword) < 12 {
79		return c, fmt.Errorf("ADMIN_PASSWORD must be at least 12 bytes, or empty to disable the dashboard")
80	}
81	if value := os.Getenv("ADMIN_COOKIE_SECURE"); value != "" {
82		var err error
83		c.AdminCookieSecure, err = strconv.ParseBool(value)
84		if err != nil {
85			return c, fmt.Errorf("ADMIN_COOKIE_SECURE must be a boolean")
86		}
87	}
88	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} {
89		if s := os.Getenv(name); s != "" {
90			v, err := strconv.Atoi(s)
91			if err != nil || v < 1 {
92				return c, fmt.Errorf("%s must be a positive integer", name)
93			}
94			*target = v
95		}
96	}
97	if c.Concurrency > 16 || c.JevConcurrency > 16 || c.Candidates > 40 || c.Passages > 64 || c.EvidenceChars > 20000 || c.MaxPageBytes > 25<<20 || c.CacheBytes > 256<<20 {
98		return c, fmt.Errorf("configured resource limits exceed supported bounds")
99	}
100	if s := os.Getenv("JEV_PRICE_PER_MILLION"); s != "" {
101		v, err := strconv.ParseFloat(s, 64)
102		if err != nil || v < 0 {
103			return c, fmt.Errorf("JEV_PRICE_PER_MILLION must be a non-negative number")
104		}
105		c.JevPricePerMillion = v
106	}
107	if s := os.Getenv("LLM_TEMPERATURE"); s != "" {
108		v, err := strconv.ParseFloat(s, 64)
109		if err != nil || v < 0 || v > 2 {
110			return c, fmt.Errorf("LLM_TEMPERATURE must be a number from 0 to 2")
111		}
112		c.LLMTemperature = v
113	}
114	if s := os.Getenv("RENDER_WAIT_MS"); s != "" {
115		v, err := strconv.Atoi(s)
116		if err != nil || v < 0 || v > 10000 {
117			return c, fmt.Errorf("RENDER_WAIT_MS must be 0–10000 milliseconds")
118		}
119		c.RenderWaitMS = time.Duration(v) * time.Millisecond
120	}
121	for name, target := range map[string]*bool{"STEALTH": &c.Stealth, "BROWSER_HEADLESS": &c.Headless} {
122		if s := os.Getenv(name); s != "" {
123			v, err := strconv.ParseBool(s)
124			if err != nil {
125				return c, fmt.Errorf("%s must be a boolean", name)
126			}
127			*target = v
128		}
129	}
130	if s := os.Getenv("PROXY_POOL"); s != "" {
131		for _, entry := range strings.Split(s, ",") {
132			entry = strings.TrimSpace(entry)
133			if entry == "" {
134				continue
135			}
136			u, err := url.Parse(entry)
137			if err != nil || u.Host == "" || (u.Scheme != "https" && u.Scheme != "http") {
138				return c, fmt.Errorf("PROXY_POOL entries must be absolute http(s) proxy urls")
139			}
140			c.ProxyPool = append(c.ProxyPool, u)
141		}
142	}
143	if s := os.Getenv("ALLOW_PRIVATE_NETWORK"); s != "" {
144		b, err := strconv.ParseBool(s)
145		if err != nil {
146			return c, err
147		}
148		c.AllowPrivate = b
149	}
150	public, err := readPublicConfig()
151	if err != nil {
152		return c, err
153	}
154	c.Public = public
155	if c.Local, err = readLocalConfig(); err != nil {
156		return c, err
157	}
158	for _, endpoint := range []string{c.JevURL, c.LLMURL, c.Public.JevURL, c.Public.FallbackURL, c.Local.URL} {
159		if endpoint == "" {
160			continue
161		}
162		u, err := url.Parse(endpoint)
163		if err != nil || u.Host == "" || (u.Scheme != "https" && u.Scheme != "http") || u.User != nil {
164			return c, fmt.Errorf("invalid model base url")
165		}
166	}
167	return c, nil
168}
169
170func env(name, fallback string) string {
171	if s := os.Getenv(name); s != "" {
172		return s
173	}
174	return fallback
175}
176
177func readPublicConfig() (PublicConfig, error) {
178	p := PublicConfig{
179		RatePerMinute: 20,
180		LogPath:       env("PUBLIC_LOG_PATH", "data/public.db"),
181		JevURL:        os.Getenv("PUBLIC_JEV_URL"), JevKey: os.Getenv("PUBLIC_JEV_KEY"), JevModel: env("PUBLIC_JEV_MODEL", "clef-flash"),
182		FallbackURL: os.Getenv("PUBLIC_JEV_FALLBACK_URL"), FallbackKey: os.Getenv("PUBLIC_JEV_FALLBACK_KEY"),
183		FallbackModel: env("PUBLIC_JEV_FALLBACK_MODEL", "jev-1.13-free"),
184	}
185	if s := os.Getenv("PUBLIC_ACCESS"); s != "" {
186		b, err := strconv.ParseBool(s)
187		if err != nil {
188			return p, fmt.Errorf("PUBLIC_ACCESS must be a boolean")
189		}
190		p.Enabled = b
191	}
192	if s := os.Getenv("PUBLIC_RATE_PER_MINUTE"); s != "" {
193		v, err := strconv.Atoi(s)
194		if err != nil || v < 1 {
195			return p, fmt.Errorf("PUBLIC_RATE_PER_MINUTE must be a positive integer")
196		}
197		p.RatePerMinute = v
198	}
199	// 0, the default, means no daily limit.
200	if s := os.Getenv("PUBLIC_DAILY_LIMIT"); s != "" {
201		v, err := strconv.Atoi(s)
202		if err != nil || v < 0 {
203			return p, fmt.Errorf("PUBLIC_DAILY_LIMIT must be a non-negative integer")
204		}
205		p.DailyLimit = v
206	}
207	if (p.JevURL == "") != (p.JevKey == "") || (p.FallbackURL == "") != (p.FallbackKey == "") {
208		return p, fmt.Errorf("PUBLIC_JEV_URL/KEY and PUBLIC_JEV_FALLBACK_URL/KEY each go together")
209	}
210	if p.FallbackURL != "" && p.JevURL == "" {
211		return p, fmt.Errorf("PUBLIC_JEV_FALLBACK_URL needs PUBLIC_JEV_URL")
212	}
213	for _, entry := range strings.Split(os.Getenv("TRUSTED_PROXIES"), ",") {
214		entry = strings.TrimSpace(entry)
215		if entry == "" {
216			continue
217		}
218		prefix, err := netip.ParsePrefix(entry)
219		if err != nil {
220			addr, e := netip.ParseAddr(entry)
221			if e != nil {
222				return p, fmt.Errorf("TRUSTED_PROXIES entries must be addresses or prefixes")
223			}
224			prefix = netip.PrefixFrom(addr, addr.BitLen())
225		}
226		p.TrustedProxies = append(p.TrustedProxies, prefix)
227	}
228	return p, nil
229}
230
231func readLocalConfig() (LocalConfig, error) {
232	l := LocalConfig{
233		URL: os.Getenv("LOCAL_DECISION_URL"), Model: env("LOCAL_DECISION_MODEL", "local-decision"),
234		Low: 0.1, High: 0.9, Scope: env("LOCAL_DECISION_SCOPE", "public"),
235	}
236	for _, name := range strings.Split(env("LOCAL_DECISION_QUESTIONS", "evidence,useful"), ",") {
237		if name = strings.TrimSpace(name); name != "" {
238			l.Questions = append(l.Questions, name)
239		}
240	}
241	for name, target := range map[string]*float64{"LOCAL_DECISION_LOW": &l.Low, "LOCAL_DECISION_HIGH": &l.High} {
242		if s := os.Getenv(name); s != "" {
243			v, err := strconv.ParseFloat(s, 64)
244			if err != nil || v < 0 || v > 1 {
245				return l, fmt.Errorf("%s must be between 0 and 1", name)
246			}
247			*target = v
248		}
249	}
250	if l.Low >= l.High {
251		return l, fmt.Errorf("LOCAL_DECISION_LOW must be below LOCAL_DECISION_HIGH")
252	}
253	if l.Scope != "public" && l.Scope != "all" {
254		return l, fmt.Errorf("LOCAL_DECISION_SCOPE must be public or all")
255	}
256	return l, nil
257}