Parent directory

config.go

2466 bytes
 1// Package config holds the service endpoint and recorder configuration shared
 2// by all kaiwari subcommands. Values come from command-line flags, environment
 3// variables, or built-in defaults; flags win over env vars, which win over
 4// defaults.
 5package config
 6
 7import (
 8	"fmt"
 9	"os"
10
11	"github.com/jessevdk/go-flags"
12)
13
14type LLMConfig struct {
15	BaseURL        string  `long:"url" env:"BASE_URL" default:"https://llama.home.theedgeofrage.com" description:"llama.cpp router base URL"`
16	Temperature    float64 `long:"temperature" env:"TEMPERATURE" default:"1.0"`
17	MaxTokens      int     `long:"max-tokens" env:"MAX_TOKENS" default:"256"`
18	EnableThinking bool    `long:"enable-thinking" env:"ENABLE_THINKING"`
19}
20
21type AudioConfig struct {
22	AudioBaseURL string `long:"url" env:"BASE_URL" default:"http://127.0.0.1:9932" description:"audio.cpp server base URL (TTS + ASR)"`
23}
24
25// Config is the shared option group. Parsed with github.com/jessevdk/go-flags.
26type Config struct {
27	LLMConfig   LLMConfig   `group:"llm" namespace:"llm" env-namespace:"JP_LLM"`
28	AudioConfig AudioConfig `group:"audio" namespace:"audio" env-namespace:"JP_AUDIO"`
29
30	Scenario            string `long:"scenario" env:"JP_SCENARIO_PATH" default:"assets/scenarios/small_city.md" description:"path to the scenario brief file"`
31	DisableModelLoading bool   `long:"disable-model-loading" env:"JP_DISABLE_MODEL_LOADING" description:"connect to external model services instead of spawning them"`
32}
33
34// ServerConfig is the kaiwari-server option group: the shared Config plus the HTTP
35// listen address and bearer token.
36type ServerConfig struct {
37	Config
38
39	Listen string `long:"listen" env:"JP_SERVER_LISTEN" default:"0.0.0.0:7480" description:"address to listen on"`
40	Token  string `long:"token" env:"JP_SERVER_TOKEN" description:"bearer token required on all requests"`
41}
42
43func ParseConfig() *Config {
44	var cfg Config
45	parse("kaiwari", &cfg)
46	return &cfg
47}
48
49// ParseServerConfig parses the kaiwari-server command line into a ServerConfig.
50func ParseServerConfig() *ServerConfig {
51	var cfg ServerConfig
52	parse("kaiwari-server", &cfg)
53	return &cfg
54}
55
56func parse(prog string, target any) {
57	parser := flags.NewParser(target, flags.HelpFlag)
58	if _, err := parser.ParseArgs(os.Args[1:]); err != nil {
59		if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
60			parser.WriteHelp(os.Stdout)
61			os.Exit(0)
62		}
63		fmt.Fprintf(os.Stderr, prog+": %v\n", err)
64		parser.WriteHelp(os.Stderr)
65		os.Exit(1)
66	}
67}