Repository navigation
Go: implement provider: OrcaRouter - #15235
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughAdds OrcaRouter as a new model provider: configuration, factory registration, and an OrcaRouterModel implementing chat (sync & streaming), TTS, model listing, and several stubbed methods. ChangesOrcaRouter Model Provider Integration
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 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 |
There was a problem hiding this comment.
Actionable comments posted: 6
🤖 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/orcarouter.go`:
- Around line 315-318: The AudioSpeech method on OrcaRouterModel dereferences
pointer parameters (modelName and apiConfig.ApiKey) without nil/empty checks
which can cause panics; update AudioSpeech to validate that modelName and
apiConfig are non-nil and that *modelName and apiConfig.ApiKey (and any other
pointer fields used later in the method) are non-empty before use, returning a
descriptive error if invalid. Locate usages inside AudioSpeech (and the adjacent
blocks referenced around the same function) and add the same guard checks where
modelName, apiConfig, or ttsConfig pointer fields are dereferenced so all
nullable inputs are validated consistently.
- Around line 163-166: The ChatStreamlyWithSender function can panic because it
dereferences apiConfig.ApiKey without nil checks; update ChatStreamlyWithSender
to validate that apiConfig is not nil and that apiConfig.ApiKey is not nil/empty
before using *apiConfig.ApiKey (the problematic dereference around lines
223–225), and return a clear error if the API key is missing; ensure any other
uses in this function that access apiConfig or ApiKey follow the same nil/empty
checks.
- Around line 385-389: ListModels currently dereferences *apiConfig.ApiKey (and
accesses apiConfig fields) without nil checks, which can panic when apiConfig or
apiConfig.ApiKey is nil; update ListModels to safely handle nil by first
checking if apiConfig != nil and apiConfig.ApiKey != nil before using
*apiConfig.ApiKey (and similarly guard any other dereferences like
apiConfig.Region), and return a clear error when the API key is missing rather
than allowing a panic; look for references to ListModels, APIConfig, and ApiKey
to apply the guards and the early error return.
- Around line 133-141: The code indexes choices[0] without checking for an empty
slice, which can panic; in the block where you extract choices from result
(variable names choices and firstChoice), first verify len(choices) > 0 and
return an error like "empty choices in response" if it's zero before attempting
choices[0]; then perform the type assertion for firstChoice
(map[string]interface{}) as before. This ensures no out-of-range access when the
provider returns an empty choices array.
- Around line 423-434: The JSON parsing in ListModels uses unchecked type
assertions on result["data"], each model, and modelMap["id"] which can panic;
update the code in the ListModels function to safely validate types and presence
using comma-ok checks (e.g., check result["data"] is []interface{}), iterate
only when that succeeds, check each model is map[string]interface{} before
accessing "id", validate that modelMap["id"] is a string, and return a
descriptive error if any check fails instead of panicking; reference the
variables result, models, modelMap and the place where you currently do
result["data"].([]interface{})/model.(map[string]interface{})/modelMap["id"].(string)
to locate and replace the unsafe assertions.
- Around line 37-39: The NewInstance method currently returns nil; change it to
return a non-nil ModelDriver by creating and returning a new OrcaRouterModel
instance (copying any relevant fields from the receiver o and setting the
provided baseURL map). Make sure to deep-copy or clone the baseURL map (to avoid
shared mutation) and return the pointer cast as ModelDriver so callers receive a
valid, non-nil driver; ensure the returned type satisfies the ModelDriver
interface.
🪄 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: 83a42232-3439-4c38-8a42-6dd62c5889c2
📒 Files selected for processing (3)
conf/models/orcarouter.jsoninternal/entity/models/factory.gointernal/entity/models/orcarouter.go
| func (o *OrcaRouterModel) NewInstance(baseURL map[string]string) ModelDriver { | ||
| return nil | ||
| } |
There was a problem hiding this comment.
NewInstance returns nil, which can break caller flow.
Returning nil here can cause downstream nil dereference when a new driver instance is expected.
Proposed fix
func (o *OrcaRouterModel) NewInstance(baseURL map[string]string) ModelDriver {
- return nil
+ return NewOrcaRouterModel(baseURL, o.URLSuffix)
}🤖 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/orcarouter.go` around lines 37 - 39, The NewInstance
method currently returns nil; change it to return a non-nil ModelDriver by
creating and returning a new OrcaRouterModel instance (copying any relevant
fields from the receiver o and setting the provided baseURL map). Make sure to
deep-copy or clone the baseURL map (to avoid shared mutation) and return the
pointer cast as ModelDriver so callers receive a valid, non-nil driver; ensure
the returned type satisfies the ModelDriver interface.
| choices, ok := result["choices"].([]interface{}) | ||
| if !ok { | ||
| return nil, fmt.Errorf("no choices in response") | ||
| } | ||
|
|
||
| firstChoice, ok := choices[0].(map[string]interface{}) | ||
| if !ok { | ||
| return nil, fmt.Errorf("no choices in response") | ||
| } |
There was a problem hiding this comment.
Guard empty choices before indexing.
At Line 138, choices[0] can panic when the provider returns an empty choices array.
Proposed fix
- choices, ok := result["choices"].([]interface{})
- if !ok {
+ choices, ok := result["choices"].([]interface{})
+ if !ok || len(choices) == 0 {
return nil, fmt.Errorf("no choices in response")
}📝 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.
| choices, ok := result["choices"].([]interface{}) | |
| if !ok { | |
| return nil, fmt.Errorf("no choices in response") | |
| } | |
| firstChoice, ok := choices[0].(map[string]interface{}) | |
| if !ok { | |
| return nil, fmt.Errorf("no choices in response") | |
| } | |
| choices, ok := result["choices"].([]interface{}) | |
| if !ok || len(choices) == 0 { | |
| return nil, fmt.Errorf("no choices in response") | |
| } | |
| firstChoice, ok := choices[0].(map[string]interface{}) | |
| if !ok { | |
| return nil, fmt.Errorf("no choices in response") | |
| } |
🤖 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/orcarouter.go` around lines 133 - 141, The code
indexes choices[0] without checking for an empty slice, which can panic; in the
block where you extract choices from result (variable names choices and
firstChoice), first verify len(choices) > 0 and return an error like "empty
choices in response" if it's zero before attempting choices[0]; then perform the
type assertion for firstChoice (map[string]interface{}) as before. This ensures
no out-of-range access when the provider returns an empty choices array.
| func (o *OrcaRouterModel) ChatStreamlyWithSender(modelName string, messages []Message, apiConfig *APIConfig, modelConfig *ChatConfig, sender func(*string, *string) error) error { | ||
| if len(messages) == 0 { | ||
| return fmt.Errorf("messages is empty") | ||
| } |
There was a problem hiding this comment.
Streaming chat misses API key validation and can panic.
At Line 224, *apiConfig.ApiKey is dereferenced without checking apiConfig/ApiKey.
Proposed fix
func (o *OrcaRouterModel) ChatStreamlyWithSender(modelName string, messages []Message, apiConfig *APIConfig, modelConfig *ChatConfig, sender func(*string, *string) error) error {
+ if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" {
+ return fmt.Errorf("api key is nil or empty")
+ }
if len(messages) == 0 {
return fmt.Errorf("messages is empty")
}Also applies to: 223-225
🤖 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/orcarouter.go` around lines 163 - 166, The
ChatStreamlyWithSender function can panic because it dereferences
apiConfig.ApiKey without nil checks; update ChatStreamlyWithSender to validate
that apiConfig is not nil and that apiConfig.ApiKey is not nil/empty before
using *apiConfig.ApiKey (the problematic dereference around lines 223–225), and
return a clear error if the API key is missing; ensure any other uses in this
function that access apiConfig or ApiKey follow the same nil/empty checks.
| func (o *OrcaRouterModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) { | ||
| if audioContent == nil || *audioContent == "" { | ||
| return nil, fmt.Errorf("audio content is empty") | ||
| } |
There was a problem hiding this comment.
AudioSpeech dereferences nullable inputs without guards.
modelName and apiConfig.ApiKey are used as pointers without validation; this can panic on invalid inputs.
Proposed fix
func (o *OrcaRouterModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
+ 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")
+ }
if audioContent == nil || *audioContent == "" {
return nil, fmt.Errorf("audio content is empty")
}Also applies to: 327-329, 352-353
🤖 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/orcarouter.go` around lines 315 - 318, The AudioSpeech
method on OrcaRouterModel dereferences pointer parameters (modelName and
apiConfig.ApiKey) without nil/empty checks which can cause panics; update
AudioSpeech to validate that modelName and apiConfig are non-nil and that
*modelName and apiConfig.ApiKey (and any other pointer fields used later in the
method) are non-empty before use, returning a descriptive error if invalid.
Locate usages inside AudioSpeech (and the adjacent blocks referenced around the
same function) and add the same guard checks where modelName, apiConfig, or
ttsConfig pointer fields are dereferenced so all nullable inputs are validated
consistently.
| func (o *OrcaRouterModel) ListModels(apiConfig *APIConfig) ([]string, error) { | ||
| var region = "default" | ||
| if apiConfig != nil && apiConfig.Region != nil && *apiConfig.Region != "" { | ||
| region = *apiConfig.Region | ||
| } |
There was a problem hiding this comment.
ListModels can panic on nil apiConfig / API key.
Line 406 dereferences *apiConfig.ApiKey without nil checks.
Proposed fix
func (o *OrcaRouterModel) ListModels(apiConfig *APIConfig) ([]string, error) {
+ if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" {
+ return nil, fmt.Errorf("api key is nil or empty")
+ }
var region = "default"
if apiConfig != nil && apiConfig.Region != nil && *apiConfig.Region != "" {
region = *apiConfig.Region
}Also applies to: 405-407
🤖 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/orcarouter.go` around lines 385 - 389, ListModels
currently dereferences *apiConfig.ApiKey (and accesses apiConfig fields) without
nil checks, which can panic when apiConfig or apiConfig.ApiKey is nil; update
ListModels to safely handle nil by first checking if apiConfig != nil and
apiConfig.ApiKey != nil before using *apiConfig.ApiKey (and similarly guard any
other dereferences like apiConfig.Region), and return a clear error when the API
key is missing rather than allowing a panic; look for references to ListModels,
APIConfig, and ApiKey to apply the guards and the early error return.
| // Parse response | ||
| var result map[string]interface{} | ||
| if err = json.Unmarshal(body, &result); err != nil { | ||
| return nil, fmt.Errorf("failed to parse response: %w", err) | ||
| } | ||
|
|
||
| // convert result["data"] to []map[string]interface{} | ||
| models := make([]string, 0) | ||
| for _, model := range result["data"].([]interface{}) { | ||
| modelMap := model.(map[string]interface{}) | ||
| modelName := modelMap["id"].(string) | ||
| models = append(models, modelName) |
There was a problem hiding this comment.
Unsafe response assertions in ListModels can panic.
result["data"].([]interface{}), model.(map[string]interface{}), and modelMap["id"].(string) all assume perfect payload shape.
Proposed fix
// Parse response
var result map[string]interface{}
if err = json.Unmarshal(body, &result); err != nil {
return nil, fmt.Errorf("failed to parse response: %w", err)
}
- // convert result["data"] to []map[string]interface{}
+ data, ok := result["data"].([]interface{})
+ if !ok {
+ return nil, fmt.Errorf("invalid models payload: missing data array")
+ }
+
models := make([]string, 0)
- for _, model := range result["data"].([]interface{}) {
- modelMap := model.(map[string]interface{})
- modelName := modelMap["id"].(string)
- models = append(models, modelName)
+ for _, model := range data {
+ modelMap, ok := model.(map[string]interface{})
+ if !ok {
+ continue
+ }
+ modelName, ok := modelMap["id"].(string)
+ if !ok || modelName == "" {
+ continue
+ }
+ models = append(models, modelName)
}📝 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.
| // Parse response | |
| var result map[string]interface{} | |
| if err = json.Unmarshal(body, &result); err != nil { | |
| return nil, fmt.Errorf("failed to parse response: %w", err) | |
| } | |
| // convert result["data"] to []map[string]interface{} | |
| models := make([]string, 0) | |
| for _, model := range result["data"].([]interface{}) { | |
| modelMap := model.(map[string]interface{}) | |
| modelName := modelMap["id"].(string) | |
| models = append(models, modelName) | |
| // Parse response | |
| var result map[string]interface{} | |
| if err = json.Unmarshal(body, &result); err != nil { | |
| return nil, fmt.Errorf("failed to parse response: %w", err) | |
| } | |
| data, ok := result["data"].([]interface{}) | |
| if !ok { | |
| return nil, fmt.Errorf("invalid models payload: missing data array") | |
| } | |
| models := make([]string, 0) | |
| for _, model := range data { | |
| modelMap, ok := model.(map[string]interface{}) | |
| if !ok { | |
| continue | |
| } | |
| modelName, ok := modelMap["id"].(string) | |
| if !ok || modelName == "" { | |
| continue | |
| } | |
| models = append(models, modelName) | |
| } |
🤖 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/orcarouter.go` around lines 423 - 434, The JSON
parsing in ListModels uses unchecked type assertions on result["data"], each
model, and modelMap["id"] which can panic; update the code in the ListModels
function to safely validate types and presence using comma-ok checks (e.g.,
check result["data"] is []interface{}), iterate only when that succeeds, check
each model is map[string]interface{} before accessing "id", validate that
modelMap["id"] is a string, and return a descriptive error if any check fails
instead of panicking; reference the variables result, models, modelMap and the
place where you currently do
result["data"].([]interface{})/model.(map[string]interface{})/modelMap["id"].(string)
to locate and replace the unsafe assertions.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #15235 +/- ##
=======================================
Coverage 93.16% 93.16%
=======================================
Files 10 10
Lines 717 717
Branches 118 118
=======================================
Hits 668 668
Misses 29 29
Partials 20 20 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
What problem does this PR solve?
implement provider
OrcaRouterThe following functionalities are now supported:
Cohere:
Type of change