config.go

  1package config
  2
  3import (
  4	"context"
  5	"fmt"
  6	"log/slog"
  7	"net/http"
  8	"net/url"
  9	"os"
 10	"slices"
 11	"strings"
 12	"time"
 13
 14	"github.com/charmbracelet/catwalk/pkg/catwalk"
 15	"github.com/charmbracelet/crush/internal/csync"
 16	"github.com/charmbracelet/crush/internal/env"
 17	"github.com/tidwall/sjson"
 18)
 19
 20const (
 21	appName              = "crush"
 22	defaultDataDirectory = ".crush"
 23)
 24
 25var defaultContextPaths = []string{
 26	".github/copilot-instructions.md",
 27	".cursorrules",
 28	".cursor/rules/",
 29	"CLAUDE.md",
 30	"CLAUDE.local.md",
 31	"GEMINI.md",
 32	"gemini.md",
 33	"crush.md",
 34	"crush.local.md",
 35	"Crush.md",
 36	"Crush.local.md",
 37	"CRUSH.md",
 38	"CRUSH.local.md",
 39	"AGENTS.md",
 40	"agents.md",
 41	"Agents.md",
 42}
 43
 44type SelectedModelType string
 45
 46const (
 47	SelectedModelTypeLarge SelectedModelType = "large"
 48	SelectedModelTypeSmall SelectedModelType = "small"
 49)
 50
 51type SelectedModel struct {
 52	// The model id as used by the provider API.
 53	// Required.
 54	Model string `json:"model" jsonschema:"required,description=The model ID as used by the provider API,example=gpt-4o"`
 55	// The model provider, same as the key/id used in the providers config.
 56	// Required.
 57	Provider string `json:"provider" jsonschema:"required,description=The model provider ID that matches a key in the providers config,example=openai"`
 58
 59	// Only used by models that use the openai provider and need this set.
 60	ReasoningEffort string `json:"reasoning_effort,omitempty" jsonschema:"description=Reasoning effort level for OpenAI models that support it,enum=low,enum=medium,enum=high"`
 61
 62	// Overrides the default model configuration.
 63	MaxTokens int64 `json:"max_tokens,omitempty" jsonschema:"description=Maximum number of tokens for model responses,minimum=1,maximum=200000,example=4096"`
 64
 65	// Used by anthropic models that can reason to indicate if the model should think.
 66	Think bool `json:"think,omitempty" jsonschema:"description=Enable thinking mode for Anthropic models that support reasoning"`
 67}
 68
 69type ProviderConfig struct {
 70	// The provider's id.
 71	ID string `json:"id,omitempty" jsonschema:"description=Unique identifier for the provider,example=openai"`
 72	// The provider's name, used for display purposes.
 73	Name string `json:"name,omitempty" jsonschema:"description=Human-readable name for the provider,example=OpenAI"`
 74	// The provider's API endpoint.
 75	BaseURL string `json:"base_url,omitempty" jsonschema:"description=Base URL for the provider's API,format=uri,example=https://api.openai.com/v1"`
 76	// The provider type, e.g. "openai", "anthropic", etc. if empty it defaults to openai.
 77	Type catwalk.Type `json:"type,omitempty" jsonschema:"description=Provider type that determines the API format,enum=openai,enum=anthropic,enum=gemini,enum=azure,enum=vertexai,default=openai"`
 78	// The provider's API key.
 79	APIKey string `json:"api_key,omitempty" jsonschema:"description=API key for authentication with the provider,example=$OPENAI_API_KEY"`
 80	// Marks the provider as disabled.
 81	Disable bool `json:"disable,omitempty" jsonschema:"description=Whether this provider is disabled,default=false"`
 82
 83	// Custom system prompt prefix.
 84	SystemPromptPrefix string `json:"system_prompt_prefix,omitempty" jsonschema:"description=Custom prefix to add to system prompts for this provider"`
 85
 86	// Extra headers to send with each request to the provider.
 87	ExtraHeaders map[string]string `json:"extra_headers,omitempty" jsonschema:"description=Additional HTTP headers to send with requests"`
 88	// Extra body
 89	ExtraBody map[string]any `json:"extra_body,omitempty" jsonschema:"description=Additional fields to include in request bodies"`
 90
 91	// Used to pass extra parameters to the provider.
 92	ExtraParams map[string]string `json:"-"`
 93
 94	// The provider models
 95	Models []catwalk.Model `json:"models,omitempty" jsonschema:"description=List of models available from this provider"`
 96}
 97
 98type MCPType string
 99
100const (
101	MCPStdio MCPType = "stdio"
102	MCPSse   MCPType = "sse"
103	MCPHttp  MCPType = "http"
104)
105
106type MCPConfig struct {
107	Command  string            `json:"command,omitempty" jsonschema:"description=Command to execute for stdio MCP servers,example=npx"`
108	Env      map[string]string `json:"env,omitempty" jsonschema:"description=Environment variables to set for the MCP server"`
109	Args     []string          `json:"args,omitempty" jsonschema:"description=Arguments to pass to the MCP server command"`
110	Type     MCPType           `json:"type" jsonschema:"required,description=Type of MCP connection,enum=stdio,enum=sse,enum=http,default=stdio"`
111	URL      string            `json:"url,omitempty" jsonschema:"description=URL for HTTP or SSE MCP servers,format=uri,example=http://localhost:3000/mcp"`
112	Disabled bool              `json:"disabled,omitempty" jsonschema:"description=Whether this MCP server is disabled,default=false"`
113	Timeout  int               `json:"timeout,omitempty" jsonschema:"description=Timeout in seconds for MCP server connections,default=15,example=30,example=60,example=120"`
114
115	// TODO: maybe make it possible to get the value from the env
116	Headers map[string]string `json:"headers,omitempty" jsonschema:"description=HTTP headers for HTTP/SSE MCP servers"`
117}
118
119type LSPConfig struct {
120	Disabled    bool              `json:"disabled,omitempty" jsonschema:"description=Whether this LSP server is disabled,default=false"`
121	Command     string            `json:"command,omitempty" jsonschema:"required,description=Command to execute for the LSP server,example=gopls"`
122	Args        []string          `json:"args,omitempty" jsonschema:"description=Arguments to pass to the LSP server command"`
123	Env         map[string]string `json:"env,omitempty" jsonschema:"description=Environment variables to set to the LSP server command"`
124	FileTypes   []string          `json:"filetypes,omitempty" jsonschema:"description=File types this LSP server handles,example=go,example=mod,example=rs,example=c,example=js,example=ts"`
125	RootMarkers []string          `json:"root_markers,omitempty" jsonschema:"description=Files or directories that indicate the project root,example=go.mod,example=package.json,example=Cargo.toml"`
126	InitOptions map[string]any    `json:"init_options,omitempty" jsonschema:"description=Initialization options passed to the LSP server during initialize request"`
127	Options     map[string]any    `json:"options,omitempty" jsonschema:"description=LSP server-specific settings passed during initialization"`
128}
129
130type TUIOptions struct {
131	CompactMode bool   `json:"compact_mode,omitempty" jsonschema:"description=Enable compact mode for the TUI interface,default=false"`
132	DiffMode    string `json:"diff_mode,omitempty" jsonschema:"description=Diff mode for the TUI interface,enum=unified,enum=split"`
133	// Here we can add themes later or any TUI related options
134	//
135
136	Completions Completions `json:"completions,omitzero" jsonschema:"description=Completions UI options"`
137}
138
139// Completions defines options for the completions UI.
140type Completions struct {
141	MaxDepth *int `json:"max_depth,omitempty" jsonschema:"description=Maximum depth for the ls tool,default=0,example=10"`
142	MaxItems *int `json:"max_items,omitempty" jsonschema:"description=Maximum number of items to return for the ls tool,default=1000,example=100"`
143}
144
145func (c Completions) Limits() (depth, items int) {
146	return ptrValOr(c.MaxDepth, -1), ptrValOr(c.MaxItems, -1)
147}
148
149type Permissions struct {
150	AllowedTools []string `json:"allowed_tools,omitempty" jsonschema:"description=List of tools that don't require permission prompts,example=bash,example=view"` // Tools that don't require permission prompts
151	SkipRequests bool     `json:"-"`                                                                                                                              // Automatically accept all permissions (YOLO mode)
152}
153
154type Attribution struct {
155	CoAuthoredBy  bool `json:"co_authored_by,omitempty" jsonschema:"description=Add Co-Authored-By trailer to commit messages,default=true"`
156	GeneratedWith bool `json:"generated_with,omitempty" jsonschema:"description=Add Generated with Crush line to commit messages and issues and PRs,default=true"`
157}
158
159type Options struct {
160	ContextPaths              []string     `json:"context_paths,omitempty" jsonschema:"description=Paths to files containing context information for the AI,example=.cursorrules,example=CRUSH.md"`
161	TUI                       *TUIOptions  `json:"tui,omitempty" jsonschema:"description=Terminal user interface options"`
162	Debug                     bool         `json:"debug,omitempty" jsonschema:"description=Enable debug logging,default=false"`
163	DebugLSP                  bool         `json:"debug_lsp,omitempty" jsonschema:"description=Enable debug logging for LSP servers,default=false"`
164	DisableAutoSummarize      bool         `json:"disable_auto_summarize,omitempty" jsonschema:"description=Disable automatic conversation summarization,default=false"`
165	DataDirectory             string       `json:"data_directory,omitempty" jsonschema:"description=Directory for storing application data (relative to working directory),default=.crush,example=.crush"` // Relative to the cwd
166	DisabledTools             []string     `json:"disabled_tools" jsonschema:"description=Tools to disable"`
167	DisableProviderAutoUpdate bool         `json:"disable_provider_auto_update,omitempty" jsonschema:"description=Disable providers auto-update,default=false"`
168	Attribution               *Attribution `json:"attribution,omitempty" jsonschema:"description=Attribution settings for generated content"`
169	DisableMetrics            bool         `json:"disable_metrics,omitempty" jsonschema:"description=Disable sending metrics,default=false"`
170}
171
172type MCPs map[string]MCPConfig
173
174type MCP struct {
175	Name string    `json:"name"`
176	MCP  MCPConfig `json:"mcp"`
177}
178
179func (m MCPs) Sorted() []MCP {
180	sorted := make([]MCP, 0, len(m))
181	for k, v := range m {
182		sorted = append(sorted, MCP{
183			Name: k,
184			MCP:  v,
185		})
186	}
187	slices.SortFunc(sorted, func(a, b MCP) int {
188		return strings.Compare(a.Name, b.Name)
189	})
190	return sorted
191}
192
193type LSPs map[string]LSPConfig
194
195type LSP struct {
196	Name string    `json:"name"`
197	LSP  LSPConfig `json:"lsp"`
198}
199
200func (l LSPs) Sorted() []LSP {
201	sorted := make([]LSP, 0, len(l))
202	for k, v := range l {
203		sorted = append(sorted, LSP{
204			Name: k,
205			LSP:  v,
206		})
207	}
208	slices.SortFunc(sorted, func(a, b LSP) int {
209		return strings.Compare(a.Name, b.Name)
210	})
211	return sorted
212}
213
214func (l LSPConfig) ResolvedEnv() []string {
215	return resolveEnvs(l.Env)
216}
217
218func (m MCPConfig) ResolvedEnv() []string {
219	return resolveEnvs(m.Env)
220}
221
222func (m MCPConfig) ResolvedHeaders() map[string]string {
223	resolver := NewShellVariableResolver(env.New())
224	for e, v := range m.Headers {
225		var err error
226		m.Headers[e], err = resolver.ResolveValue(v)
227		if err != nil {
228			slog.Error("error resolving header variable", "error", err, "variable", e, "value", v)
229			continue
230		}
231	}
232	return m.Headers
233}
234
235type Agent struct {
236	ID          string `json:"id,omitempty"`
237	Name        string `json:"name,omitempty"`
238	Description string `json:"description,omitempty"`
239	// This is the id of the system prompt used by the agent
240	Disabled bool `json:"disabled,omitempty"`
241
242	Model SelectedModelType `json:"model" jsonschema:"required,description=The model type to use for this agent,enum=large,enum=small,default=large"`
243
244	// The available tools for the agent
245	//  if this is nil, all tools are available
246	AllowedTools []string `json:"allowed_tools,omitempty"`
247
248	// this tells us which MCPs are available for this agent
249	//  if this is empty all mcps are available
250	//  the string array is the list of tools from the AllowedMCP the agent has available
251	//  if the string array is nil, all tools from the AllowedMCP are available
252	AllowedMCP map[string][]string `json:"allowed_mcp,omitempty"`
253
254	// The list of LSPs that this agent can use
255	//  if this is nil, all LSPs are available
256	AllowedLSP []string `json:"allowed_lsp,omitempty"`
257
258	// Overrides the context paths for this agent
259	ContextPaths []string `json:"context_paths,omitempty"`
260}
261
262type Tools struct {
263	Ls ToolLs `json:"ls,omitzero"`
264}
265
266type ToolLs struct {
267	MaxDepth *int `json:"max_depth,omitempty" jsonschema:"description=Maximum depth for the ls tool,default=0,example=10"`
268	MaxItems *int `json:"max_items,omitempty" jsonschema:"description=Maximum number of items to return for the ls tool,default=1000,example=100"`
269}
270
271func (t ToolLs) Limits() (depth, items int) {
272	return ptrValOr(t.MaxDepth, -1), ptrValOr(t.MaxItems, -1)
273}
274
275// Config holds the configuration for crush.
276type Config struct {
277	Schema string `json:"$schema,omitempty"`
278
279	// We currently only support large/small as values here.
280	Models map[SelectedModelType]SelectedModel `json:"models,omitempty" jsonschema:"description=Model configurations for different model types,example={\"large\":{\"model\":\"gpt-4o\",\"provider\":\"openai\"}}"`
281
282	// The providers that are configured
283	Providers *csync.Map[string, ProviderConfig] `json:"providers,omitempty" jsonschema:"description=AI provider configurations"`
284
285	MCP MCPs `json:"mcp,omitempty" jsonschema:"description=Model Context Protocol server configurations"`
286
287	LSP LSPs `json:"lsp,omitempty" jsonschema:"description=Language Server Protocol configurations"`
288
289	Options *Options `json:"options,omitempty" jsonschema:"description=General application options"`
290
291	Permissions *Permissions `json:"permissions,omitempty" jsonschema:"description=Permission settings for tool usage"`
292
293	Tools Tools `json:"tools,omitzero" jsonschema:"description=Tool configurations"`
294
295	// Internal
296	workingDir string `json:"-"`
297	// TODO: most likely remove this concept when I come back to it
298	Agents map[string]Agent `json:"-"`
299	// TODO: find a better way to do this this should probably not be part of the config
300	resolver       VariableResolver
301	dataConfigDir  string             `json:"-"`
302	knownProviders []catwalk.Provider `json:"-"`
303}
304
305func (c *Config) WorkingDir() string {
306	return c.workingDir
307}
308
309func (c *Config) EnabledProviders() []ProviderConfig {
310	var enabled []ProviderConfig
311	for p := range c.Providers.Seq() {
312		if !p.Disable {
313			enabled = append(enabled, p)
314		}
315	}
316	return enabled
317}
318
319// IsConfigured  return true if at least one provider is configured
320func (c *Config) IsConfigured() bool {
321	return len(c.EnabledProviders()) > 0
322}
323
324func (c *Config) GetModel(provider, model string) *catwalk.Model {
325	if providerConfig, ok := c.Providers.Get(provider); ok {
326		for _, m := range providerConfig.Models {
327			if m.ID == model {
328				return &m
329			}
330		}
331	}
332	return nil
333}
334
335func (c *Config) GetProviderForModel(modelType SelectedModelType) *ProviderConfig {
336	model, ok := c.Models[modelType]
337	if !ok {
338		return nil
339	}
340	if providerConfig, ok := c.Providers.Get(model.Provider); ok {
341		return &providerConfig
342	}
343	return nil
344}
345
346func (c *Config) GetModelByType(modelType SelectedModelType) *catwalk.Model {
347	model, ok := c.Models[modelType]
348	if !ok {
349		return nil
350	}
351	return c.GetModel(model.Provider, model.Model)
352}
353
354func (c *Config) LargeModel() *catwalk.Model {
355	model, ok := c.Models[SelectedModelTypeLarge]
356	if !ok {
357		return nil
358	}
359	return c.GetModel(model.Provider, model.Model)
360}
361
362func (c *Config) SmallModel() *catwalk.Model {
363	model, ok := c.Models[SelectedModelTypeSmall]
364	if !ok {
365		return nil
366	}
367	return c.GetModel(model.Provider, model.Model)
368}
369
370func (c *Config) SetCompactMode(enabled bool) error {
371	if c.Options == nil {
372		c.Options = &Options{}
373	}
374	c.Options.TUI.CompactMode = enabled
375	return c.SetConfigField("options.tui.compact_mode", enabled)
376}
377
378func (c *Config) Resolve(key string) (string, error) {
379	if c.resolver == nil {
380		return "", fmt.Errorf("no variable resolver configured")
381	}
382	return c.resolver.ResolveValue(key)
383}
384
385func (c *Config) UpdatePreferredModel(modelType SelectedModelType, model SelectedModel) error {
386	c.Models[modelType] = model
387	if err := c.SetConfigField(fmt.Sprintf("models.%s", modelType), model); err != nil {
388		return fmt.Errorf("failed to update preferred model: %w", err)
389	}
390	return nil
391}
392
393func (c *Config) SetConfigField(key string, value any) error {
394	// read the data
395	data, err := os.ReadFile(c.dataConfigDir)
396	if err != nil {
397		if os.IsNotExist(err) {
398			data = []byte("{}")
399		} else {
400			return fmt.Errorf("failed to read config file: %w", err)
401		}
402	}
403
404	newValue, err := sjson.Set(string(data), key, value)
405	if err != nil {
406		return fmt.Errorf("failed to set config field %s: %w", key, err)
407	}
408	if err := os.WriteFile(c.dataConfigDir, []byte(newValue), 0o600); err != nil {
409		return fmt.Errorf("failed to write config file: %w", err)
410	}
411	return nil
412}
413
414func (c *Config) SetProviderAPIKey(providerID, apiKey string) error {
415	// First save to the config file
416	err := c.SetConfigField("providers."+providerID+".api_key", apiKey)
417	if err != nil {
418		return fmt.Errorf("failed to save API key to config file: %w", err)
419	}
420
421	providerConfig, exists := c.Providers.Get(providerID)
422	if exists {
423		providerConfig.APIKey = apiKey
424		c.Providers.Set(providerID, providerConfig)
425		return nil
426	}
427
428	var foundProvider *catwalk.Provider
429	for _, p := range c.knownProviders {
430		if string(p.ID) == providerID {
431			foundProvider = &p
432			break
433		}
434	}
435
436	if foundProvider != nil {
437		// Create new provider config based on known provider
438		providerConfig = ProviderConfig{
439			ID:           providerID,
440			Name:         foundProvider.Name,
441			BaseURL:      foundProvider.APIEndpoint,
442			Type:         foundProvider.Type,
443			APIKey:       apiKey,
444			Disable:      false,
445			ExtraHeaders: make(map[string]string),
446			ExtraParams:  make(map[string]string),
447			Models:       foundProvider.Models,
448		}
449	} else {
450		return fmt.Errorf("provider with ID %s not found in known providers", providerID)
451	}
452	// Store the updated provider config
453	c.Providers.Set(providerID, providerConfig)
454	return nil
455}
456
457func allToolNames() []string {
458	return []string{
459		"agent",
460		"bash",
461		"download",
462		"edit",
463		"multiedit",
464		"fetch",
465		"glob",
466		"grep",
467		"ls",
468		"sourcegraph",
469		"view",
470		"write",
471	}
472}
473
474func resolveAllowedTools(allTools []string, disabledTools []string) []string {
475	if disabledTools == nil {
476		return allTools
477	}
478	// filter out disabled tools (exclude mode)
479	return filterSlice(allTools, disabledTools, false)
480}
481
482func resolveReadOnlyTools(tools []string) []string {
483	readOnlyTools := []string{"glob", "grep", "ls", "sourcegraph", "view"}
484	// filter to only include tools that are in allowedtools (include mode)
485	return filterSlice(tools, readOnlyTools, true)
486}
487
488func filterSlice(data []string, mask []string, include bool) []string {
489	filtered := []string{}
490	for _, s := range data {
491		// if include is true, we include items that ARE in the mask
492		// if include is false, we include items that are NOT in the mask
493		if include == slices.Contains(mask, s) {
494			filtered = append(filtered, s)
495		}
496	}
497	return filtered
498}
499
500func (c *Config) SetupAgents() {
501	allowedTools := resolveAllowedTools(allToolNames(), c.Options.DisabledTools)
502
503	agents := map[string]Agent{
504		"coder": {
505			ID:           "coder",
506			Name:         "Coder",
507			Description:  "An agent that helps with executing coding tasks.",
508			Model:        SelectedModelTypeLarge,
509			ContextPaths: c.Options.ContextPaths,
510			AllowedTools: allowedTools,
511		},
512		"task": {
513			ID:           "task",
514			Name:         "Task",
515			Description:  "An agent that helps with searching for context and finding implementation details.",
516			Model:        SelectedModelTypeLarge,
517			ContextPaths: c.Options.ContextPaths,
518			AllowedTools: resolveReadOnlyTools(allowedTools),
519			// NO MCPs or LSPs by default
520			AllowedMCP: map[string][]string{},
521			AllowedLSP: []string{},
522		},
523	}
524	c.Agents = agents
525}
526
527func (c *Config) Resolver() VariableResolver {
528	return c.resolver
529}
530
531func (c *ProviderConfig) TestConnection(resolver VariableResolver) error {
532	testURL := ""
533	headers := make(map[string]string)
534	apiKey, _ := resolver.ResolveValue(c.APIKey)
535	switch c.Type {
536	case catwalk.TypeOpenAI:
537		baseURL, _ := resolver.ResolveValue(c.BaseURL)
538		if baseURL == "" {
539			baseURL = "https://api.openai.com/v1"
540		}
541		if c.ID == string(catwalk.InferenceProviderOpenRouter) {
542			testURL = baseURL + "/credits"
543		} else {
544			testURL = baseURL + "/models"
545		}
546		headers["Authorization"] = "Bearer " + apiKey
547	case catwalk.TypeAnthropic:
548		baseURL, _ := resolver.ResolveValue(c.BaseURL)
549		if baseURL == "" {
550			baseURL = "https://api.anthropic.com/v1"
551		}
552		testURL = baseURL + "/models"
553		headers["x-api-key"] = apiKey
554		headers["anthropic-version"] = "2023-06-01"
555	case catwalk.TypeGemini:
556		baseURL, _ := resolver.ResolveValue(c.BaseURL)
557		if baseURL == "" {
558			baseURL = "https://generativelanguage.googleapis.com"
559		}
560		testURL = baseURL + "/v1beta/models?key=" + url.QueryEscape(apiKey)
561	}
562	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
563	defer cancel()
564	client := &http.Client{}
565	req, err := http.NewRequestWithContext(ctx, "GET", testURL, nil)
566	if err != nil {
567		return fmt.Errorf("failed to create request for provider %s: %w", c.ID, err)
568	}
569	for k, v := range headers {
570		req.Header.Set(k, v)
571	}
572	for k, v := range c.ExtraHeaders {
573		req.Header.Set(k, v)
574	}
575	b, err := client.Do(req)
576	if err != nil {
577		return fmt.Errorf("failed to create request for provider %s: %w", c.ID, err)
578	}
579	if c.ID == string(catwalk.InferenceProviderZAI) {
580		if b.StatusCode == http.StatusUnauthorized {
581			// for z.ai just check if the http response is not 401
582			return fmt.Errorf("failed to connect to provider %s: %s", c.ID, b.Status)
583		}
584	} else {
585		if b.StatusCode != http.StatusOK {
586			return fmt.Errorf("failed to connect to provider %s: %s", c.ID, b.Status)
587		}
588	}
589	_ = b.Body.Close()
590	return nil
591}
592
593func resolveEnvs(envs map[string]string) []string {
594	resolver := NewShellVariableResolver(env.New())
595	for e, v := range envs {
596		var err error
597		envs[e], err = resolver.ResolveValue(v)
598		if err != nil {
599			slog.Error("error resolving environment variable", "error", err, "variable", e, "value", v)
600			continue
601		}
602	}
603
604	res := make([]string, 0, len(envs))
605	for k, v := range envs {
606		res = append(res, fmt.Sprintf("%s=%s", k, v))
607	}
608	return res
609}
610
611func ptrValOr[T any](t *T, el T) T {
612	if t == nil {
613		return el
614	}
615	return *t
616}