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