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