Skip to content

Commit 0311320

Browse files
committed
fix: tweak the logic in config to ensure that env vs file configurations merge properly
1 parent 98e2910 commit 0311320

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

internal/config/config.go

+16-9
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ func Load(workingDir string, debug bool) (*Config, error) {
121121

122122
configureViper()
123123
setDefaults(debug)
124-
setProviderDefaults()
125124

126125
// Read global config
127126
if err := readConfig(viper.ReadInConfig()); err != nil {
@@ -131,6 +130,8 @@ func Load(workingDir string, debug bool) (*Config, error) {
131130
// Load and merge local config
132131
mergeLocalConfig(workingDir)
133132

133+
setProviderDefaults()
134+
134135
// Apply configuration to the struct
135136
if err := viper.Unmarshal(cfg); err != nil {
136137
return cfg, fmt.Errorf("failed to unmarshal config: %w", err)
@@ -213,7 +214,8 @@ func setDefaults(debug bool) {
213214
}
214215
}
215216

216-
// setProviderDefaults configures LLM provider defaults based on environment variables.
217+
// setProviderDefaults configures LLM provider defaults based on provider provided by
218+
// environment variables and configuration file.
217219
func setProviderDefaults() {
218220
// Set all API keys we can find in the environment
219221
if apiKey := os.Getenv("ANTHROPIC_API_KEY"); apiKey != "" {
@@ -228,48 +230,53 @@ func setProviderDefaults() {
228230
if apiKey := os.Getenv("GROQ_API_KEY"); apiKey != "" {
229231
viper.SetDefault("providers.groq.apiKey", apiKey)
230232
}
233+
if apiKey := os.Getenv("OPENROUTER_API_KEY"); apiKey != "" {
234+
viper.SetDefault("providers.openrouter.apiKey", apiKey)
235+
}
231236

232237
// Use this order to set the default models
233238
// 1. Anthropic
234239
// 2. OpenAI
235240
// 3. Google Gemini
236241
// 4. Groq
237-
// 5. AWS Bedrock
242+
// 5. OpenRouter
243+
// 6. AWS Bedrock
244+
// 7. Azure
245+
238246
// Anthropic configuration
239-
if apiKey := os.Getenv("ANTHROPIC_API_KEY"); apiKey != "" {
247+
if viper.Get("providers.anthropic.apiKey") != "" {
240248
viper.SetDefault("agents.coder.model", models.Claude37Sonnet)
241249
viper.SetDefault("agents.task.model", models.Claude37Sonnet)
242250
viper.SetDefault("agents.title.model", models.Claude37Sonnet)
243251
return
244252
}
245253

246254
// OpenAI configuration
247-
if apiKey := os.Getenv("OPENAI_API_KEY"); apiKey != "" {
255+
if viper.Get("providers.openai.apiKey") != "" {
248256
viper.SetDefault("agents.coder.model", models.GPT41)
249257
viper.SetDefault("agents.task.model", models.GPT41Mini)
250258
viper.SetDefault("agents.title.model", models.GPT41Mini)
251259
return
252260
}
253261

254262
// Google Gemini configuration
255-
if apiKey := os.Getenv("GEMINI_API_KEY"); apiKey != "" {
263+
if viper.Get("providers.google.gemini.apiKey") != "" {
256264
viper.SetDefault("agents.coder.model", models.Gemini25)
257265
viper.SetDefault("agents.task.model", models.Gemini25Flash)
258266
viper.SetDefault("agents.title.model", models.Gemini25Flash)
259267
return
260268
}
261269

262270
// Groq configuration
263-
if apiKey := os.Getenv("GROQ_API_KEY"); apiKey != "" {
271+
if viper.Get("providers.groq.apiKey") != "" {
264272
viper.SetDefault("agents.coder.model", models.QWENQwq)
265273
viper.SetDefault("agents.task.model", models.QWENQwq)
266274
viper.SetDefault("agents.title.model", models.QWENQwq)
267275
return
268276
}
269277

270278
// OpenRouter configuration
271-
if apiKey := os.Getenv("OPENROUTER_API_KEY"); apiKey != "" {
272-
viper.SetDefault("providers.openrouter.apiKey", apiKey)
279+
if viper.Get("providers.openrouter.apiKey") != "" {
273280
viper.SetDefault("agents.coder.model", models.OpenRouterClaude37Sonnet)
274281
viper.SetDefault("agents.task.model", models.OpenRouterClaude37Sonnet)
275282
viper.SetDefault("agents.title.model", models.OpenRouterClaude35Haiku)

internal/llm/models/models.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ const (
3535

3636
// Providers in order of popularity
3737
var ProviderPopularity = map[ModelProvider]int{
38-
ProviderAnthropic: 1,
39-
ProviderOpenAI: 2,
40-
ProviderGemini: 3,
41-
ProviderGROQ: 4,
42-
ProviderBedrock: 5,
38+
ProviderAnthropic: 1,
39+
ProviderOpenAI: 2,
40+
ProviderGemini: 3,
41+
ProviderGROQ: 4,
42+
ProviderOpenRouter: 5,
43+
ProviderBedrock: 6,
44+
ProviderAzure: 7,
4345
}
4446

4547
var SupportedModels = map[ModelID]Model{

0 commit comments

Comments
 (0)