Repository navigation
Go: refactor model provider - #15568
Conversation
Signed-off-by: Jin Hai <[email protected]>
📝 WalkthroughWalkthroughThis PR standardizes Apache 2.0 license headers across 22 model provider files and introduces a BaseModel abstraction for shared API configuration, validation, and HTTP client management. ZhipuAIModel is refactored to embed BaseModel, consolidating region-to-base-URL resolution and API key validation logic across all its methods. ChangesLicense Header Standardization
BaseModel Abstraction and ZhipuAIModel Refactoring
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 inconclusive)
✅ 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 |
Signed-off-by: Jin Hai <[email protected]>
Signed-off-by: Jin Hai <[email protected]>
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 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/base_model.go`:
- Around line 47-61: The mapped base URL returned from b.BaseURL[region] isn't
normalized like apiConfig.BaseURL (which trims trailing '/'), causing double
slashes; update the code that reads baseURL from b.BaseURL (the branch that uses
baseURL, ok := b.BaseURL[region]) to apply the same normalization (e.g.,
strings.TrimSuffix(baseURL, "/")) before returning so callers using
fmt.Sprintf("%s/%s", ...) get a canonical base URL; change the return from
returning raw baseURL to the trimmed value and keep the same error behavior for
missing entries.
🪄 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: 95f2a9c3-a71b-444d-b9ba-c71fbecc558f
📒 Files selected for processing (24)
internal/entity/models/302ai.gointernal/entity/models/baichuan.gointernal/entity/models/baidu.gointernal/entity/models/base_model.gointernal/entity/models/cohere.gointernal/entity/models/deepinfra.gointernal/entity/models/fishaudio.gointernal/entity/models/huaweicloud.gointernal/entity/models/huggingface.gointernal/entity/models/jiekouai.gointernal/entity/models/jina.gointernal/entity/models/lmstudio.gointernal/entity/models/mineru.gointernal/entity/models/mineru_local.gointernal/entity/models/nvidia.gointernal/entity/models/ollama.gointernal/entity/models/openrouter.gointernal/entity/models/orcarouter.gointernal/entity/models/paddleocr.gointernal/entity/models/paddleocr_local.gointernal/entity/models/qiniu.gointernal/entity/models/tokenhub.gointernal/entity/models/xunfei.gointernal/entity/models/zhipu-ai.go
| if apiConfig.BaseURL != nil && *apiConfig.BaseURL != "" { | ||
| return strings.TrimSuffix(*apiConfig.BaseURL, "/"), nil | ||
| } | ||
|
|
||
| region := "default" | ||
| if apiConfig.Region != nil { | ||
| region = *apiConfig.Region | ||
| } | ||
|
|
||
| baseURL, ok := b.BaseURL[region] | ||
| if !ok || baseURL == "" { | ||
| return "", fmt.Errorf("no base URL configured for region %q", region) | ||
| } | ||
|
|
||
| return baseURL, nil |
There was a problem hiding this comment.
Normalize mapped base URLs the same way as explicit ones.
The explicit apiConfig.BaseURL branch strips a trailing /, but the b.BaseURL[region] branch returns the raw string. A mapped value like https://host/ will produce https://host//... in every caller that does fmt.Sprintf("%s/%s", ...).
💡 Suggested fix
func (b *BaseModel) GetBaseURL(apiConfig *APIConfig) (string, error) {
if apiConfig.BaseURL != nil && *apiConfig.BaseURL != "" {
return strings.TrimSuffix(*apiConfig.BaseURL, "/"), nil
}
@@
baseURL, ok := b.BaseURL[region]
if !ok || baseURL == "" {
return "", fmt.Errorf("no base URL configured for region %q", region)
}
- return baseURL, nil
+ return strings.TrimSuffix(baseURL, "/"), nil
}📝 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.
| if apiConfig.BaseURL != nil && *apiConfig.BaseURL != "" { | |
| return strings.TrimSuffix(*apiConfig.BaseURL, "/"), nil | |
| } | |
| region := "default" | |
| if apiConfig.Region != nil { | |
| region = *apiConfig.Region | |
| } | |
| baseURL, ok := b.BaseURL[region] | |
| if !ok || baseURL == "" { | |
| return "", fmt.Errorf("no base URL configured for region %q", region) | |
| } | |
| return baseURL, nil | |
| func (b *BaseModel) GetBaseURL(apiConfig *APIConfig) (string, error) { | |
| if apiConfig.BaseURL != nil && *apiConfig.BaseURL != "" { | |
| return strings.TrimSuffix(*apiConfig.BaseURL, "/"), nil | |
| } | |
| region := "default" | |
| if apiConfig.Region != nil { | |
| region = *apiConfig.Region | |
| } | |
| baseURL, ok := b.BaseURL[region] | |
| if !ok || baseURL == "" { | |
| return "", fmt.Errorf("no base URL configured for region %q", region) | |
| } | |
| return strings.TrimSuffix(baseURL, "/"), nil | |
| } |
🤖 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/base_model.go` around lines 47 - 61, The mapped base
URL returned from b.BaseURL[region] isn't normalized like apiConfig.BaseURL
(which trims trailing '/'), causing double slashes; update the code that reads
baseURL from b.BaseURL (the branch that uses baseURL, ok := b.BaseURL[region])
to apply the same normalization (e.g., strings.TrimSuffix(baseURL, "/")) before
returning so callers using fmt.Sprintf("%s/%s", ...) get a canonical base URL;
change the return from returning raw baseURL to the trimmed value and keep the
same error behavior for missing entries.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #15568 +/- ##
=======================================
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?
Type of change