Repository navigation
Go: implement remaining interface for OpenRouter - #14657
Conversation
📝 WalkthroughWalkthroughAdds OpenRouter URL routes and implements embedding (Encode), rerank, balance, and CheckConnection; removes VolcEngine single-message Chat and consolidates chat into ChatWithMessages and streaming ChatStreamlyWithSender. ChangesOpenRouter Feature Expansion
VolcEngine Chat Consolidation
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #14657 +/- ##
=======================================
Coverage 94.16% 94.16%
=======================================
Files 10 10
Lines 703 703
Branches 112 112
=======================================
Hits 662 662
Misses 25 25
Partials 16 16 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
internal/entity/models/openrouter.go (1)
483-504:⚠️ Potential issue | 🔴 Critical | ⚡ Quick win
Rerankneeds nil/empty validation for inputs before pointer dereference.At Line 484 and Line 503,
modelNameandapiConfig.ApiKeyare dereferenced with no guards. This can panic instead of returning a typed error.Suggested fix
func (o *OpenRouterModel) Rerank(modelName *string, query string, texts []string, apiConfig *APIConfig) ([]float64, error) { if len(texts) == 0 { return []float64{}, nil } + if modelName == nil || *modelName == "" { + return nil, fmt.Errorf("model name is nil or empty") + } + if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" { + return nil, fmt.Errorf("api key is nil or empty") + } var region = "default"🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/entity/models/openrouter.go` around lines 483 - 504, The Rerank implementation dereferences modelName and apiConfig.ApiKey without checks, which can panic; in the Rerank function add explicit guards that validate modelName is non-nil and non-empty and that apiConfig and apiConfig.ApiKey are non-nil/non-empty before using them, returning a clear typed error (e.g., fmt.Errorf) instead of proceeding; update the code paths that build OpenRouterRerankRequest (referencing modelName) and the request header Authorization (referencing apiConfig.ApiKey) to rely on these validations.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/entity/models/openrouter.go`:
- Around line 366-368: The Encode function currently dereferences modelName
(used when building reqBody) without checking for nil/empty, which can panic;
update Encode to validate modelName before dereferencing: check that the pointer
modelName is non-nil and that *modelName is not an empty string, return a clear
error if invalid, and only then use *modelName when constructing reqBody (or use
a safe local variable). Ensure references to modelName, Encode, and reqBody are
updated accordingly.
- Around line 595-608: The Balance function currently dereferences apiConfig and
apiConfig.ApiKey (and uses apiConfig.Region) without nil/empty checks which can
panic; update Balance (and by extension CheckConnection which calls it) to first
validate apiConfig != nil and apiConfig.ApiKey != nil and non-empty before using
*apiConfig.ApiKey, and handle missing apiConfig/ApiKey by returning a clear
error instead of panicking; also guard Region access by checking
apiConfig.Region != nil/empty before reading and keep using o.BaseURL and
o.URLSuffix.Balance as before.
---
Outside diff comments:
In `@internal/entity/models/openrouter.go`:
- Around line 483-504: The Rerank implementation dereferences modelName and
apiConfig.ApiKey without checks, which can panic; in the Rerank function add
explicit guards that validate modelName is non-nil and non-empty and that
apiConfig and apiConfig.ApiKey are non-nil/non-empty before using them,
returning a clear typed error (e.g., fmt.Errorf) instead of proceeding; update
the code paths that build OpenRouterRerankRequest (referencing modelName) and
the request header Authorization (referencing apiConfig.ApiKey) to rely on these
validations.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 23bf7cdf-ea2e-4e8f-8551-8537ee7ea6ff
📒 Files selected for processing (3)
conf/models/openrouter.jsoninternal/entity/models/openrouter.gointernal/entity/models/volcengine.go
💤 Files with no reviewable changes (1)
- internal/entity/models/volcengine.go
| reqBody := map[string]interface{}{ | ||
| "model": *modelName, | ||
| "input": texts, |
There was a problem hiding this comment.
Guard modelName before dereference in Encode (panic risk).
At Line 367, *modelName is used without validating modelName != nil (and non-empty), which can panic on invalid caller input.
Suggested fix
func (o *OpenRouterModel) Encode(modelName *string, texts []string, apiConfig *APIConfig, embeddingConfig *EmbeddingConfig) ([][]float64, error) {
if len(texts) == 0 {
return [][]float64{}, nil
}
+ if modelName == nil || *modelName == "" {
+ return nil, fmt.Errorf("model name is nil or empty")
+ }
var region = "default"📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| reqBody := map[string]interface{}{ | |
| "model": *modelName, | |
| "input": texts, | |
| func (o *OpenRouterModel) Encode(modelName *string, texts []string, apiConfig *APIConfig, embeddingConfig *EmbeddingConfig) ([][]float64, error) { | |
| if len(texts) == 0 { | |
| return [][]float64{}, nil | |
| } | |
| if modelName == nil || *modelName == "" { | |
| return nil, fmt.Errorf("model name is nil or empty") | |
| } | |
| var region = "default" | |
| reqBody := map[string]interface{}{ | |
| "model": *modelName, | |
| "input": texts, |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/entity/models/openrouter.go` around lines 366 - 368, The Encode
function currently dereferences modelName (used when building reqBody) without
checking for nil/empty, which can panic; update Encode to validate modelName
before dereferencing: check that the pointer modelName is non-nil and that
*modelName is not an empty string, return a clear error if invalid, and only
then use *modelName when constructing reqBody (or use a safe local variable).
Ensure references to modelName, Encode, and reqBody are updated accordingly.
| if apiConfig != nil && apiConfig.Region != nil && *apiConfig.Region != "" { | ||
| region = *apiConfig.Region | ||
| } | ||
|
|
||
| url := fmt.Sprintf("%s/%s", o.BaseURL[region], o.URLSuffix.Balance) | ||
|
|
||
| req, err := http.NewRequest("GET", url, nil) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create request: %w", err) | ||
| } | ||
|
|
||
| req.Header.Set("Content-Type", "application/json") | ||
| req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *apiConfig.ApiKey)) | ||
|
|
There was a problem hiding this comment.
Balance can panic on missing apiConfig / API key.
At Line 607, *apiConfig.ApiKey is dereferenced without validation. This is a crash path and also impacts CheckConnection (Line 646) since it delegates to Balance.
Suggested fix
func (o *OpenRouterModel) Balance(apiConfig *APIConfig) (map[string]interface{}, error) {
+ if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" {
+ return nil, fmt.Errorf("api key is nil or empty")
+ }
region := "default"
if apiConfig != nil && apiConfig.Region != nil && *apiConfig.Region != "" {
region = *apiConfig.Region
}🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/entity/models/openrouter.go` around lines 595 - 608, The Balance
function currently dereferences apiConfig and apiConfig.ApiKey (and uses
apiConfig.Region) without nil/empty checks which can panic; update Balance (and
by extension CheckConnection which calls it) to first validate apiConfig != nil
and apiConfig.ApiKey != nil and non-empty before using *apiConfig.ApiKey, and
handle missing apiConfig/ApiKey by returning a clear error instead of panicking;
also guard Region access by checking apiConfig.Region != nil/empty before
reading and keep using o.BaseURL and o.URLSuffix.Balance as before.
### What problem does this PR solve? 1. implement `rerank`, `embedding`, `balance`, `checkConnet` method for `OpenRouter` 2. delete `chat` method in `internal/entity/models/volcengine.go` ### Type of change - [x] New Feature (non-breaking change which adds functionality) - [x] Refactoring
### What problem does this PR solve? 1. implement `rerank`, `embedding`, `balance`, `checkConnet` method for `OpenRouter` 2. delete `chat` method in `internal/entity/models/volcengine.go` ### Type of change - [x] New Feature (non-breaking change which adds functionality) - [x] Refactoring
What problem does this PR solve?
rerank,embedding,balance,checkConnetmethod forOpenRouterchatmethod ininternal/entity/models/volcengine.goType of change