config.go 32 KB

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