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