config.go

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