Skip to content

fix: tweak the logic to fix config merging #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ func Load(workingDir string, debug bool) (*Config, error) {

configureViper()
setDefaults(debug)
setProviderDefaults()

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

setProviderDefaults()

// Apply configuration to the struct
if err := viper.Unmarshal(cfg); err != nil {
return cfg, fmt.Errorf("failed to unmarshal config: %w", err)
Expand Down Expand Up @@ -213,7 +214,8 @@ func setDefaults(debug bool) {
}
}

// setProviderDefaults configures LLM provider defaults based on environment variables.
// setProviderDefaults configures LLM provider defaults based on provider provided by
// environment variables and configuration file.
func setProviderDefaults() {
// Set all API keys we can find in the environment
if apiKey := os.Getenv("ANTHROPIC_API_KEY"); apiKey != "" {
Expand All @@ -228,48 +230,53 @@ func setProviderDefaults() {
if apiKey := os.Getenv("GROQ_API_KEY"); apiKey != "" {
viper.SetDefault("providers.groq.apiKey", apiKey)
}
if apiKey := os.Getenv("OPENROUTER_API_KEY"); apiKey != "" {
viper.SetDefault("providers.openrouter.apiKey", apiKey)
}

// Use this order to set the default models
// 1. Anthropic
// 2. OpenAI
// 3. Google Gemini
// 4. Groq
// 5. AWS Bedrock
// 5. OpenRouter
// 6. AWS Bedrock
// 7. Azure

// Anthropic configuration
if apiKey := os.Getenv("ANTHROPIC_API_KEY"); apiKey != "" {
if viper.Get("providers.anthropic.apiKey") != "" {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We set these values right at the beginning of setProviderDefaults

viper.SetDefault("agents.coder.model", models.Claude37Sonnet)
viper.SetDefault("agents.task.model", models.Claude37Sonnet)
viper.SetDefault("agents.title.model", models.Claude37Sonnet)
return
}

// OpenAI configuration
if apiKey := os.Getenv("OPENAI_API_KEY"); apiKey != "" {
if viper.Get("providers.openai.apiKey") != "" {
viper.SetDefault("agents.coder.model", models.GPT41)
viper.SetDefault("agents.task.model", models.GPT41Mini)
viper.SetDefault("agents.title.model", models.GPT41Mini)
return
}

// Google Gemini configuration
if apiKey := os.Getenv("GEMINI_API_KEY"); apiKey != "" {
if viper.Get("providers.google.gemini.apiKey") != "" {
viper.SetDefault("agents.coder.model", models.Gemini25)
viper.SetDefault("agents.task.model", models.Gemini25Flash)
viper.SetDefault("agents.title.model", models.Gemini25Flash)
return
}

// Groq configuration
if apiKey := os.Getenv("GROQ_API_KEY"); apiKey != "" {
if viper.Get("providers.groq.apiKey") != "" {
viper.SetDefault("agents.coder.model", models.QWENQwq)
viper.SetDefault("agents.task.model", models.QWENQwq)
viper.SetDefault("agents.title.model", models.QWENQwq)
return
}

// OpenRouter configuration
if apiKey := os.Getenv("OPENROUTER_API_KEY"); apiKey != "" {
viper.SetDefault("providers.openrouter.apiKey", apiKey)
if viper.Get("providers.openrouter.apiKey") != "" {
viper.SetDefault("agents.coder.model", models.OpenRouterClaude37Sonnet)
viper.SetDefault("agents.task.model", models.OpenRouterClaude37Sonnet)
viper.SetDefault("agents.title.model", models.OpenRouterClaude35Haiku)
Expand Down
12 changes: 7 additions & 5 deletions internal/llm/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ const (

// Providers in order of popularity
var ProviderPopularity = map[ModelProvider]int{
ProviderAnthropic: 1,
ProviderOpenAI: 2,
ProviderGemini: 3,
ProviderGROQ: 4,
ProviderBedrock: 5,
ProviderAnthropic: 1,
ProviderOpenAI: 2,
ProviderGemini: 3,
ProviderGROQ: 4,
ProviderOpenRouter: 5,
ProviderBedrock: 6,
ProviderAzure: 7,
}

var SupportedModels = map[ModelID]Model{
Expand Down