Repository navigation
Go: implement provider: MinerU - #14990
Conversation
📝 WalkthroughWalkthroughThis PR integrates a new MinerU document parsing model provider, updates model configurations for naming consistency and URL structure, implements the MinerU model driver with document submission and task polling, adds URL detection to file input handling in the CLI, and fixes region validation in Gitee model methods. ChangesMinerU Model Provider Integration
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 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: 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/gitee.go (1)
988-991: 🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick winApply the same empty-string validation for consistency.
The
CheckConnectionmethod currently only checks ifapiConfig.Regionis non-nil, but doesn't validate against empty strings. This is now inconsistent with the four methods fixed in this PR (ListModels,Balance,ListTasks,ShowTask) and six other methods in this file that already perform both checks.🔧 Apply consistent validation
var region = "default" -if apiConfig.Region != nil { +if 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/gitee.go` around lines 988 - 991, The CheckConnection method currently sets region if apiConfig.Region is non-nil but doesn't guard against empty strings; update the region extraction in CheckConnection to only use apiConfig.Region when apiConfig.Region != nil && *apiConfig.Region != "" (otherwise keep "default"), mirroring the validation already applied in ListModels, Balance, ListTasks, and ShowTask; locate the logic around the region variable and apiConfig.Region in the CheckConnection function and apply the same non-nil AND non-empty check so behavior is consistent across methods.
🤖 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/mineru.go`:
- Around line 120-126: When building MinerU URLs, guard against apiConfig.Region
values that are non-empty but not present in the m.BaseURL map: check if
apiConfig.Region is nil or empty or if *apiConfig.Region is not a key in
m.BaseURL and fallback to the "default" region before using m.BaseURL[region];
update the URL construction sites (e.g., where apiURL is built using
m.BaseURL[region] and m.URLSuffix.DocumentParse and the analogous block used by
ShowTask and ParseFile) to perform this key-existence check so you never index
m.BaseURL with an unknown key.
- Around line 168-174: ParseFile currently treats any non-error code as success
even when taskResp.Data.TaskID is empty; update ParseFile to validate
taskResp.Data.TaskID (from the taskResp variable) after the code check and
return an error (e.g., fmt.Errorf with a clear message) if it's empty instead of
returning a ParseFileResponse with an empty TaskID so downstream "show task"
flows don't fail. Ensure the check happens before constructing and returning the
ParseFileResponse.
---
Outside diff comments:
In `@internal/entity/models/gitee.go`:
- Around line 988-991: The CheckConnection method currently sets region if
apiConfig.Region is non-nil but doesn't guard against empty strings; update the
region extraction in CheckConnection to only use apiConfig.Region when
apiConfig.Region != nil && *apiConfig.Region != "" (otherwise keep "default"),
mirroring the validation already applied in ListModels, Balance, ListTasks, and
ShowTask; locate the logic around the region variable and apiConfig.Region in
the CheckConnection function and apply the same non-nil AND non-empty check so
behavior is consistent across methods.
🪄 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: 0a913b6c-c882-48a1-878b-b468efad3151
📒 Files selected for processing (8)
conf/models/baidu.jsonconf/models/mineru.jsonconf/models/xunfei.jsoninternal/cli/user_command.gointernal/entity/models/factory.gointernal/entity/models/gitee.gointernal/entity/models/mineru.gointernal/entity/models/types.go
| var region = "default" | ||
| if apiConfig.Region != nil && *apiConfig.Region != "" { | ||
| region = *apiConfig.Region | ||
| } | ||
|
|
||
| apiURL := fmt.Sprintf("%s/api/%s", m.BaseURL[region], m.URLSuffix.DocumentParse) | ||
|
|
There was a problem hiding this comment.
Handle unknown region keys before building MinerU URLs.
If apiConfig.Region is non-empty but not present in m.BaseURL, URL construction uses an empty base and request creation fails with an invalid URL in both ParseFile and ShowTask.
🔧 Proposed fix
+func (m *MinerUModel) resolveBaseURL(apiConfig *APIConfig) (string, error) {
+ region := "default"
+ if apiConfig != nil && apiConfig.Region != nil && *apiConfig.Region != "" {
+ region = *apiConfig.Region
+ }
+ if base, ok := m.BaseURL[region]; ok && base != "" {
+ return base, nil
+ }
+ if base, ok := m.BaseURL["default"]; ok && base != "" {
+ return base, nil
+ }
+ return "", fmt.Errorf("invalid MinerU region: %s", region)
+}
+
func (m *MinerUModel) ParseFile(modelName *string, content []byte, documentURL *string, apiConfig *APIConfig, parseFileConfig *ParseFileConfig) (*ParseFileResponse, error) {
@@
- var region = "default"
- if apiConfig.Region != nil && *apiConfig.Region != "" {
- region = *apiConfig.Region
- }
-
- apiURL := fmt.Sprintf("%s/api/%s", m.BaseURL[region], m.URLSuffix.DocumentParse)
+ baseURL, err := m.resolveBaseURL(apiConfig)
+ if err != nil {
+ return nil, err
+ }
+ apiURL := fmt.Sprintf("%s/api/%s", baseURL, m.URLSuffix.DocumentParse)
@@
func (m *MinerUModel) ShowTask(taskID string, apiConfig *APIConfig) (*TaskResponse, error) {
@@
- var region = "default"
- if apiConfig.Region != nil && *apiConfig.Region != "" {
- region = *apiConfig.Region
- }
-
- // URL: https://mineru.net/api/v4/extract/task/{task_id}
- apiURL := fmt.Sprintf("%s/api/%s/%s", m.BaseURL[region], m.URLSuffix.DocumentParse, taskID)
+ baseURL, err := m.resolveBaseURL(apiConfig)
+ if err != nil {
+ return nil, err
+ }
+ // URL: https://mineru.net/api/v4/extract/task/{task_id}
+ apiURL := fmt.Sprintf("%s/api/%s/%s", baseURL, m.URLSuffix.DocumentParse, taskID)Also applies to: 201-207
🤖 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/mineru.go` around lines 120 - 126, When building
MinerU URLs, guard against apiConfig.Region values that are non-empty but not
present in the m.BaseURL map: check if apiConfig.Region is nil or empty or if
*apiConfig.Region is not a key in m.BaseURL and fallback to the "default" region
before using m.BaseURL[region]; update the URL construction sites (e.g., where
apiURL is built using m.BaseURL[region] and m.URLSuffix.DocumentParse and the
analogous block used by ShowTask and ParseFile) to perform this key-existence
check so you never index m.BaseURL with an unknown key.
| if taskResp.Code != 0 { | ||
| return nil, fmt.Errorf("MinerU task creation failed (code %d): %s", taskResp.Code, taskResp.Msg) | ||
| } | ||
|
|
||
| return &ParseFileResponse{ | ||
| TaskID: taskResp.Data.TaskID, | ||
| }, nil |
There was a problem hiding this comment.
Reject empty task_id in parse success responses.
ParseFile currently returns success even when task_id is empty, which will fail follow-up show task flows.
🔧 Proposed fix
if taskResp.Code != 0 {
return nil, fmt.Errorf("MinerU task creation failed (code %d): %s", taskResp.Code, taskResp.Msg)
}
+ if taskResp.Data.TaskID == "" {
+ return nil, fmt.Errorf("MinerU task creation succeeded but task_id is empty")
+ }
return &ParseFileResponse{
TaskID: taskResp.Data.TaskID,
}, 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 taskResp.Code != 0 { | |
| return nil, fmt.Errorf("MinerU task creation failed (code %d): %s", taskResp.Code, taskResp.Msg) | |
| } | |
| return &ParseFileResponse{ | |
| TaskID: taskResp.Data.TaskID, | |
| }, nil | |
| if taskResp.Code != 0 { | |
| return nil, fmt.Errorf("MinerU task creation failed (code %d): %s", taskResp.Code, taskResp.Msg) | |
| } | |
| if taskResp.Data.TaskID == "" { | |
| return nil, fmt.Errorf("MinerU task creation succeeded but task_id is empty") | |
| } | |
| return &ParseFileResponse{ | |
| TaskID: taskResp.Data.TaskID, | |
| }, 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/mineru.go` around lines 168 - 174, ParseFile currently
treats any non-error code as success even when taskResp.Data.TaskID is empty;
update ParseFile to validate taskResp.Data.TaskID (from the taskResp variable)
after the code check and return an error (e.g., fmt.Errorf with a clear message)
if it's empty instead of returning a ParseFileResponse with an empty TaskID so
downstream "show task" flows don't fail. Ensure the check happens before
constructing and returning the ParseFileResponse.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #14990 +/- ##
==========================================
- Coverage 95.59% 94.16% -1.43%
==========================================
Files 10 10
Lines 703 703
Branches 112 112
==========================================
- Hits 672 662 -10
- Misses 21 25 +4
- Partials 10 16 +6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
What problem does this PR solve?
Implement MinerU Provider
The following functionalities are now supported:
MinerU
List tasksVerified examples from the CLI:
Type of change