config.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package config
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. "github.com/kujtimiihoxha/termai/internal/llm/models"
  7. "github.com/spf13/viper"
  8. )
  9. type MCPType string
  10. const (
  11. MCPStdio MCPType = "stdio"
  12. MCPSse MCPType = "sse"
  13. )
  14. type MCPServer struct {
  15. Command string `json:"command"`
  16. Env []string `json:"env"`
  17. Args []string `json:"args"`
  18. Type MCPType `json:"type"`
  19. URL string `json:"url"`
  20. Headers map[string]string `json:"headers"`
  21. // TODO: add permissions configuration
  22. // TODO: add the ability to specify the tools to import
  23. }
  24. type Model struct {
  25. Coder models.ModelID `json:"coder"`
  26. CoderMaxTokens int64 `json:"coderMaxTokens"`
  27. Task models.ModelID `json:"task"`
  28. TaskMaxTokens int64 `json:"taskMaxTokens"`
  29. // TODO: Maybe support multiple models for different purposes
  30. }
  31. type AnthropicConfig struct {
  32. DisableCache bool `json:"disableCache"`
  33. UseBedrock bool `json:"useBedrock"`
  34. }
  35. type Provider struct {
  36. APIKey string `json:"apiKey"`
  37. Enabled bool `json:"enabled"`
  38. }
  39. type Data struct {
  40. Directory string `json:"directory"`
  41. }
  42. type Log struct {
  43. Level string `json:"level"`
  44. }
  45. type LSPConfig struct {
  46. Disabled bool `json:"enabled"`
  47. Command string `json:"command"`
  48. Args []string `json:"args"`
  49. Options any `json:"options"`
  50. }
  51. type Config struct {
  52. Data *Data `json:"data,omitempty"`
  53. Log *Log `json:"log,omitempty"`
  54. MCPServers map[string]MCPServer `json:"mcpServers,omitempty"`
  55. Providers map[models.ModelProvider]Provider `json:"providers,omitempty"`
  56. LSP map[string]LSPConfig `json:"lsp,omitempty"`
  57. Model *Model `json:"model,omitempty"`
  58. Debug bool `json:"debug,omitempty"`
  59. }
  60. var cfg *Config
  61. const (
  62. defaultDataDirectory = ".termai"
  63. defaultLogLevel = "info"
  64. defaultMaxTokens = int64(5000)
  65. termai = "termai"
  66. )
  67. func Load(debug bool) error {
  68. if cfg != nil {
  69. return nil
  70. }
  71. viper.SetConfigName(fmt.Sprintf(".%s", termai))
  72. viper.SetConfigType("json")
  73. viper.AddConfigPath("$HOME")
  74. viper.AddConfigPath(fmt.Sprintf("$XDG_CONFIG_HOME/%s", termai))
  75. viper.SetEnvPrefix(strings.ToUpper(termai))
  76. // Add defaults
  77. viper.SetDefault("data.directory", defaultDataDirectory)
  78. if debug {
  79. viper.SetDefault("debug", true)
  80. viper.Set("log.level", "debug")
  81. } else {
  82. viper.SetDefault("debug", false)
  83. viper.SetDefault("log.level", defaultLogLevel)
  84. }
  85. defaultModelSet := false
  86. if os.Getenv("ANTHROPIC_API_KEY") != "" {
  87. viper.SetDefault("providers.anthropic.apiKey", os.Getenv("ANTHROPIC_API_KEY"))
  88. viper.SetDefault("providers.anthropic.enabled", true)
  89. viper.SetDefault("model.coder", models.Claude37Sonnet)
  90. viper.SetDefault("model.task", models.Claude37Sonnet)
  91. defaultModelSet = true
  92. }
  93. if os.Getenv("OPENAI_API_KEY") != "" {
  94. viper.SetDefault("providers.openai.apiKey", os.Getenv("OPENAI_API_KEY"))
  95. viper.SetDefault("providers.openai.enabled", true)
  96. if !defaultModelSet {
  97. viper.SetDefault("model.coder", models.GPT41)
  98. viper.SetDefault("model.task", models.GPT41)
  99. defaultModelSet = true
  100. }
  101. }
  102. if os.Getenv("GEMINI_API_KEY") != "" {
  103. viper.SetDefault("providers.gemini.apiKey", os.Getenv("GEMINI_API_KEY"))
  104. viper.SetDefault("providers.gemini.enabled", true)
  105. if !defaultModelSet {
  106. viper.SetDefault("model.coder", models.GRMINI20Flash)
  107. viper.SetDefault("model.task", models.GRMINI20Flash)
  108. defaultModelSet = true
  109. }
  110. }
  111. if os.Getenv("GROQ_API_KEY") != "" {
  112. viper.SetDefault("providers.groq.apiKey", os.Getenv("GROQ_API_KEY"))
  113. viper.SetDefault("providers.groq.enabled", true)
  114. if !defaultModelSet {
  115. viper.SetDefault("model.coder", models.QWENQwq)
  116. viper.SetDefault("model.task", models.QWENQwq)
  117. defaultModelSet = true
  118. }
  119. }
  120. viper.SetDefault("providers.bedrock.enabled", true)
  121. // TODO: add more providers
  122. cfg = &Config{}
  123. err := viper.ReadInConfig()
  124. if err != nil {
  125. if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
  126. return err
  127. }
  128. }
  129. local := viper.New()
  130. local.SetConfigName(fmt.Sprintf(".%s", termai))
  131. local.SetConfigType("json")
  132. local.AddConfigPath(".")
  133. // load local config, this will override the global config
  134. if err = local.ReadInConfig(); err == nil {
  135. viper.MergeConfigMap(local.AllSettings())
  136. }
  137. viper.Unmarshal(cfg)
  138. if cfg.Model != nil && cfg.Model.CoderMaxTokens <= 0 {
  139. cfg.Model.CoderMaxTokens = defaultMaxTokens
  140. }
  141. if cfg.Model != nil && cfg.Model.TaskMaxTokens <= 0 {
  142. cfg.Model.TaskMaxTokens = defaultMaxTokens
  143. }
  144. for _, v := range cfg.MCPServers {
  145. if v.Type == "" {
  146. v.Type = MCPStdio
  147. }
  148. }
  149. workdir, err := os.Getwd()
  150. if err != nil {
  151. return err
  152. }
  153. viper.Set("wd", workdir)
  154. return nil
  155. }
  156. func Get() *Config {
  157. if cfg == nil {
  158. err := Load(false)
  159. if err != nil {
  160. panic(err)
  161. }
  162. }
  163. return cfg
  164. }
  165. func WorkingDirectory() string {
  166. return viper.GetString("wd")
  167. }
  168. func Write() error {
  169. return viper.WriteConfig()
  170. }