Skip to content

Commit c670d32

Browse files
committed
Merge testing into master: OpenCode default for AI helpers
2 parents 6f14cb7 + 6069d48 commit c670d32

2 files changed

Lines changed: 21 additions & 22 deletions

File tree

internal/api/ui_api.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,20 +2171,19 @@ func openRouterChat(c *gin.Context, systemPrompt, userPrompt string) (string, er
21712171
return result.Choices[0].Message.Content, nil
21722172
}
21732173

2174-
// aiChat routes a system+user prompt to the first available AI provider.
2175-
// Preference: an OpenRouter key (UI X-OpenRouter-Key header or OPENROUTER_API_KEY)
2176-
// is used first; otherwise (or if OpenRouter errors) it falls back to the shared
2177-
// brain provider chain (OpenCode → Z.ai → Gemini). This keeps the dashboard AI
2178-
// helpers working when the user only configured the free OpenCode provider.
2174+
// aiChat routes a system+user prompt to the shared brain provider chain, which
2175+
// prefers OpenCode (the free default) → OpenRouter → Z.ai → Gemini based on which
2176+
// keys are configured. The only exception is an OpenRouter key pasted directly in
2177+
// the UI (X-OpenRouter-Key header): when present, that explicit choice is honored
2178+
// first. An OpenRouter key set merely via env does NOT override the OpenCode
2179+
// default — it's just a fallback in the chain.
21792180
func aiChat(c *gin.Context, systemPrompt, userPrompt string) (string, error) {
2180-
hasOR := strings.TrimSpace(c.GetHeader("X-OpenRouter-Key")) != "" ||
2181-
strings.TrimSpace(os.Getenv("OPENROUTER_API_KEY")) != ""
2182-
if hasOR {
2181+
if strings.TrimSpace(c.GetHeader("X-OpenRouter-Key")) != "" {
21832182
out, err := openRouterChat(c, systemPrompt, userPrompt)
21842183
if err == nil {
21852184
return out, nil
21862185
}
2187-
log.Printf("[API] OpenRouter chat failed, falling back to OpenCode/Gemini: %v", err)
2186+
log.Printf("[API] OpenRouter (UI key) failed, falling back to provider chain: %v", err)
21882187
}
21892188
return brain.ChatWithAI(nil, userPrompt, systemPrompt)
21902189
}

internal/brain/brain.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -501,25 +501,25 @@ Command Outputs:
501501
}
502502

503503
// callAI routes to whichever AI provider has a key configured.
504-
// Priority: OpenRouter (premium models) → OpenCode (free) → Z.ai → Gemini.
504+
// Priority: OpenCode (free default) → OpenRouter → Z.ai → Gemini.
505505
func callAI(prompt, orKey, geminiKey string) (string, error) {
506506
ocKey := strings.TrimSpace(os.Getenv("OPENCODE_API_KEY"))
507507
zaiKey := strings.TrimSpace(os.Getenv("ZHIPU_API_KEY"))
508508

509-
if orKey != "" {
510-
res, err := callOpenRouter(prompt, orKey)
509+
if ocKey != "" {
510+
res, err := callOpenCode(prompt, ocKey)
511511
if err == nil {
512512
return res, nil
513513
}
514-
logger.GetLogger().Infof("[BRAIN] OpenRouter failed, falling back to other providers: %v", err)
514+
logger.GetLogger().Infof("[BRAIN] OpenCode failed, falling back to other providers: %v", err)
515515
}
516516

517-
if ocKey != "" {
518-
res, err := callOpenCode(prompt, ocKey)
517+
if orKey != "" {
518+
res, err := callOpenRouter(prompt, orKey)
519519
if err == nil {
520520
return res, nil
521521
}
522-
logger.GetLogger().Infof("[BRAIN] OpenCode failed, falling back to other providers: %v", err)
522+
logger.GetLogger().Infof("[BRAIN] OpenRouter failed, falling back to other providers: %v", err)
523523
}
524524

525525
if zaiKey != "" {
@@ -683,20 +683,20 @@ func ChatWithAI(history []Message, userMessage string, systemPrompt string) (str
683683
messages = append(messages, history...)
684684
messages = append(messages, Message{Role: "user", Content: userMessage})
685685

686-
if openRouterKey != "" {
687-
res, err := callOpenRouterMulti(messages, openRouterKey)
686+
if openCodeKey != "" {
687+
res, err := callOpenCodeMulti(messages, openCodeKey)
688688
if err == nil {
689689
return res, nil
690690
}
691-
logger.GetLogger().Infof("[BRAIN] OpenRouter failed, falling back to other providers: %v", err)
691+
logger.GetLogger().Infof("[BRAIN] OpenCode failed, falling back to other providers: %v", err)
692692
}
693693

694-
if openCodeKey != "" {
695-
res, err := callOpenCodeMulti(messages, openCodeKey)
694+
if openRouterKey != "" {
695+
res, err := callOpenRouterMulti(messages, openRouterKey)
696696
if err == nil {
697697
return res, nil
698698
}
699-
logger.GetLogger().Infof("[BRAIN] OpenCode failed, falling back to other providers: %v", err)
699+
logger.GetLogger().Infof("[BRAIN] OpenRouter failed, falling back to other providers: %v", err)
700700
}
701701

702702
zaiKey := strings.TrimSpace(os.Getenv("ZHIPU_API_KEY"))

0 commit comments

Comments
 (0)