config.go

  1package config
  2
  3import (
  4	"fmt"
  5	"os"
  6	"slices"
  7	"strings"
  8
  9	"github.com/charmbracelet/crush/internal/fur/provider"
 10	"github.com/tidwall/sjson"
 11)
 12
 13const (
 14	appName              = "crush"
 15	defaultDataDirectory = ".crush"
 16	defaultLogLevel      = "info"
 17)
 18
 19var defaultContextPaths = []string{
 20	".github/copilot-instructions.md",
 21	".cursorrules",
 22	".cursor/rules/",
 23	"CLAUDE.md",
 24	"CLAUDE.local.md",
 25	"GEMINI.md",
 26	"gemini.md",
 27	"crush.md",
 28	"crush.local.md",
 29	"Crush.md",
 30	"Crush.local.md",
 31	"CRUSH.md",
 32	"CRUSH.local.md",
 33}
 34
 35type SelectedModelType string
 36
 37const (
 38	SelectedModelTypeLarge SelectedModelType = "large"
 39	SelectedModelTypeSmall SelectedModelType = "small"
 40)
 41
 42type SelectedModel struct {
 43	// The model id as used by the provider API.
 44	// Required.
 45	Model string `json:"model"`
 46	// The model provider, same as the key/id used in the providers config.
 47	// Required.
 48	Provider string `json:"provider"`
 49
 50	// Only used by models that use the openai provider and need this set.
 51	ReasoningEffort string `json:"reasoning_effort,omitempty"`
 52
 53	// Overrides the default model configuration.
 54	MaxTokens int64 `json:"max_tokens,omitempty"`
 55
 56	// Used by anthropic models that can reason to indicate if the model should think.
 57	Think bool `json:"think,omitempty"`
 58}
 59
 60type ProviderConfig struct {
 61	// The provider's id.
 62	ID string `json:"id,omitempty"`
 63	// The provider's name, used for display purposes.
 64	Name string `json:"name,omitempty"`
 65	// The provider's API endpoint.
 66	BaseURL string `json:"base_url,omitempty"`
 67	// The provider type, e.g. "openai", "anthropic", etc. if empty it defaults to openai.
 68	Type provider.Type `json:"type,omitempty"`
 69	// The provider's API key.
 70	APIKey string `json:"api_key,omitempty"`
 71	// Marks the provider as disabled.
 72	Disable bool `json:"disable,omitempty"`
 73
 74	// Extra headers to send with each request to the provider.
 75	ExtraHeaders map[string]string
 76
 77	// Used to pass extra parameters to the provider.
 78	ExtraParams map[string]string `json:"-"`
 79
 80	// The provider models
 81	Models []provider.Model `json:"models,omitempty"`
 82}
 83
 84type MCPType string
 85
 86const (
 87	MCPStdio MCPType = "stdio"
 88	MCPSse   MCPType = "sse"
 89	MCPHttp  MCPType = "http"
 90)
 91
 92type MCPConfig struct {
 93	Command string   `json:"command,omitempty" `
 94	Env     []string `json:"env,omitempty"`
 95	Args    []string `json:"args,omitempty"`
 96	Type    MCPType  `json:"type"`
 97	URL     string   `json:"url,omitempty"`
 98
 99	// TODO: maybe make it possible to get the value from the env
100	Headers map[string]string `json:"headers,omitempty"`
101}
102
103type LSPConfig struct {
104	Disabled bool     `json:"enabled,omitempty"`
105	Command  string   `json:"command"`
106	Args     []string `json:"args,omitempty"`
107	Options  any      `json:"options,omitempty"`
108}
109
110type TUIOptions struct {
111	CompactMode bool `json:"compact_mode,omitempty"`
112	// Here we can add themes later or any TUI related options
113}
114
115type Options struct {
116	ContextPaths         []string    `json:"context_paths,omitempty"`
117	TUI                  *TUIOptions `json:"tui,omitempty"`
118	Debug                bool        `json:"debug,omitempty"`
119	DebugLSP             bool        `json:"debug_lsp,omitempty"`
120	DisableAutoSummarize bool        `json:"disable_auto_summarize,omitempty"`
121	// Relative to the cwd
122	DataDirectory string `json:"data_directory,omitempty"`
123}
124
125type MCPs map[string]MCPConfig
126
127type MCP struct {
128	Name string    `json:"name"`
129	MCP  MCPConfig `json:"mcp"`
130}
131
132func (m MCPs) Sorted() []MCP {
133	sorted := make([]MCP, 0, len(m))
134	for k, v := range m {
135		sorted = append(sorted, MCP{
136			Name: k,
137			MCP:  v,
138		})
139	}
140	slices.SortFunc(sorted, func(a, b MCP) int {
141		return strings.Compare(a.Name, b.Name)
142	})
143	return sorted
144}
145
146type LSPs map[string]LSPConfig
147
148type LSP struct {
149	Name string    `json:"name"`
150	LSP  LSPConfig `json:"lsp"`
151}
152
153func (l LSPs) Sorted() []LSP {
154	sorted := make([]LSP, 0, len(l))
155	for k, v := range l {
156		sorted = append(sorted, LSP{
157			Name: k,
158			LSP:  v,
159		})
160	}
161	slices.SortFunc(sorted, func(a, b LSP) int {
162		return strings.Compare(a.Name, b.Name)
163	})
164	return sorted
165}
166
167type Agent struct {
168	ID          string `json:"id,omitempty"`
169	Name        string `json:"name,omitempty"`
170	Description string `json:"description,omitempty"`
171	// This is the id of the system prompt used by the agent
172	Disabled bool `json:"disabled,omitempty"`
173
174	Model SelectedModelType `json:"model"`
175
176	// The available tools for the agent
177	//  if this is nil, all tools are available
178	AllowedTools []string `json:"allowed_tools,omitempty"`
179
180	// this tells us which MCPs are available for this agent
181	//  if this is empty all mcps are available
182	//  the string array is the list of tools from the AllowedMCP the agent has available
183	//  if the string array is nil, all tools from the AllowedMCP are available
184	AllowedMCP map[string][]string `json:"allowed_mcp,omitempty"`
185
186	// The list of LSPs that this agent can use
187	//  if this is nil, all LSPs are available
188	AllowedLSP []string `json:"allowed_lsp,omitempty"`
189
190	// Overrides the context paths for this agent
191	ContextPaths []string `json:"context_paths,omitempty"`
192}
193
194// Config holds the configuration for crush.
195type Config struct {
196	// We currently only support large/small as values here.
197	Models map[SelectedModelType]SelectedModel `json:"models,omitempty"`
198
199	// The providers that are configured
200	Providers map[string]ProviderConfig `json:"providers,omitempty"`
201
202	MCP MCPs `json:"mcp,omitempty"`
203
204	LSP LSPs `json:"lsp,omitempty"`
205
206	Options *Options `json:"options,omitempty"`
207
208	// Internal
209	workingDir string `json:"-"`
210	// TODO: most likely remove this concept when I come back to it
211	Agents map[string]Agent `json:"-"`
212	// TODO: find a better way to do this this should probably not be part of the config
213	resolver      VariableResolver
214	dataConfigDir string `json:"-"`
215}
216
217func (c *Config) WorkingDir() string {
218	return c.workingDir
219}
220
221func (c *Config) EnabledProviders() []ProviderConfig {
222	enabled := make([]ProviderConfig, 0, len(c.Providers))
223	for _, p := range c.Providers {
224		if !p.Disable {
225			enabled = append(enabled, p)
226		}
227	}
228	return enabled
229}
230
231// IsConfigured  return true if at least one provider is configured
232func (c *Config) IsConfigured() bool {
233	return len(c.EnabledProviders()) > 0
234}
235
236func (c *Config) GetModel(provider, model string) *provider.Model {
237	if providerConfig, ok := c.Providers[provider]; ok {
238		for _, m := range providerConfig.Models {
239			if m.ID == model {
240				return &m
241			}
242		}
243	}
244	return nil
245}
246
247func (c *Config) GetProviderForModel(modelType SelectedModelType) *ProviderConfig {
248	model, ok := c.Models[modelType]
249	if !ok {
250		return nil
251	}
252	if providerConfig, ok := c.Providers[model.Provider]; ok {
253		return &providerConfig
254	}
255	return nil
256}
257
258func (c *Config) GetModelByType(modelType SelectedModelType) *provider.Model {
259	model, ok := c.Models[modelType]
260	if !ok {
261		return nil
262	}
263	return c.GetModel(model.Provider, model.Model)
264}
265
266func (c *Config) LargeModel() *provider.Model {
267	model, ok := c.Models[SelectedModelTypeLarge]
268	if !ok {
269		return nil
270	}
271	return c.GetModel(model.Provider, model.Model)
272}
273
274func (c *Config) SmallModel() *provider.Model {
275	model, ok := c.Models[SelectedModelTypeSmall]
276	if !ok {
277		return nil
278	}
279	return c.GetModel(model.Provider, model.Model)
280}
281
282func (c *Config) SetCompactMode(enabled bool) error {
283	if c.Options == nil {
284		c.Options = &Options{}
285	}
286	c.Options.TUI.CompactMode = enabled
287	return c.SetConfigField("options.tui.compact_mode", enabled)
288}
289
290func (c *Config) Resolve(key string) (string, error) {
291	if c.resolver == nil {
292		return "", fmt.Errorf("no variable resolver configured")
293	}
294	return c.resolver.ResolveValue(key)
295}
296
297func (c *Config) UpdatePreferredModel(modelType SelectedModelType, model SelectedModel) error {
298	c.Models[modelType] = model
299	if err := c.SetConfigField(fmt.Sprintf("models.%s", modelType), model); err != nil {
300		return fmt.Errorf("failed to update preferred model: %w", err)
301	}
302	return nil
303}
304
305func (c *Config) SetConfigField(key string, value any) error {
306	// read the data
307	data, err := os.ReadFile(c.dataConfigDir)
308	if err != nil {
309		if os.IsNotExist(err) {
310			data = []byte("{}")
311		} else {
312			return fmt.Errorf("failed to read config file: %w", err)
313		}
314	}
315
316	newValue, err := sjson.Set(string(data), key, value)
317	if err != nil {
318		return fmt.Errorf("failed to set config field %s: %w", key, err)
319	}
320	if err := os.WriteFile(c.dataConfigDir, []byte(newValue), 0o644); err != nil {
321		return fmt.Errorf("failed to write config file: %w", err)
322	}
323	return nil
324}