14 // configEndpointChatGPT names the ChatGPT backend with the subscription
16 configEndpointChatGPT = "chatgpt"
18 // configEndpointAPI names the OpenAI API with an API key.
19 configEndpointAPI = "api"
22 // loadConfig reads config.json in dir over the defaults. A missing file gives
23 // the defaults. An unknown field is an error, so a typo does not become a
25 func loadConfig(dir string) (Config, error) {
26 config := defaultConfig()
27 data, err := os.ReadFile(filepath.Join(dir, "config.json"))
28 if errors.Is(err, os.ErrNotExist) {
33 return Config{}, fmt.Errorf("read config: %w", err)
36 decoder := json.NewDecoder(bytes.NewReader(data))
37 decoder.DisallowUnknownFields()
39 err = decoder.Decode(&config)
40 if err == nil && !errors.Is(decoder.Decode(&struct{}{}), io.EOF) {
41 err = errors.New("data after the JSON object")
45 err = config.validate()
49 return Config{}, fmt.Errorf("config.json: %w", err)
55 // Config holds the settings of config.json in the data directory.
57 Endpoint string `json:"endpoint"`
58 Compactor ModelConfig `json:"compactor"`
61 // validate makes sure that each setting has a valid value.
62 func (c Config) validate() error {
63 if c.Endpoint != configEndpointChatGPT && c.Endpoint != configEndpointAPI {
64 return fmt.Errorf("endpoint %q is not %q or %q", c.Endpoint, configEndpointChatGPT, configEndpointAPI)
67 if c.Compactor.Model == "" || c.Compactor.Effort == "" {
68 return errors.New("compactor needs a model and an effort")
74 // ModelConfig names a model and its reasoning effort.
75 type ModelConfig struct {
76 Model string `json:"model"`
77 Effort string `json:"effort"`
80 // defaultConfig returns the settings that apply when config.json does not
82 func defaultConfig() Config {
84 Endpoint: configEndpointChatGPT,
85 Compactor: ModelConfig{