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()
38 err = decoder.Decode(&config)
39 if err == nil && !errors.Is(decoder.Decode(&struct{}{}), io.EOF) {
40 err = errors.New("data after the JSON object")
44 err = config.validate()
48 return Config{}, fmt.Errorf("config.json: %w", err)
54 // Config holds the settings of config.json in the data directory.
56 Endpoint string `json:"endpoint"`
57 Compactor ModelConfig `json:"compactor"`
60 // validate makes sure that each setting has a valid value.
61 func (c Config) validate() error {
62 if c.Endpoint != configEndpointChatGPT && c.Endpoint != configEndpointAPI {
63 return fmt.Errorf("endpoint %q is not %q or %q", c.Endpoint, configEndpointChatGPT, configEndpointAPI)
66 if c.Compactor.Model == "" || c.Compactor.Effort == "" {
67 return errors.New("compactor needs a model and an effort")
73 // ModelConfig names a model and its reasoning effort.
74 type ModelConfig struct {
75 Model string `json:"model"`
76 Effort string `json:"effort"`
79 // defaultConfig returns the settings that apply when config.json does not
81 func defaultConfig() Config {
83 Endpoint: configEndpointChatGPT,
84 Compactor: ModelConfig{