Repository navigation
Go: implement provider: MinerU_Local - #15051
Conversation
📝 WalkthroughWalkthroughThis PR introduces a new ChangesMinerU Local Model Driver Implementation
Sequence DiagramsequenceDiagram
participant User
participant ParseFile
participant HTTPClient
participant MinerUServer
participant ShowTask as ShowTask
User->>ParseFile: Call ParseFile with content, modelName, apiConfig
ParseFile->>ParseFile: Validate content, select region
ParseFile->>HTTPClient: Multipart POST to parse endpoint with PDF + backend field
alt Has ApiKey
ParseFile->>HTTPClient: Set Authorization Bearer header
end
HTTPClient->>MinerUServer: POST upload
MinerUServer-->>HTTPClient: Return task_id
HTTPClient-->>ParseFile: Unmarshall response (200/202)
ParseFile-->>User: Return ParseFileResponse with task_id
User->>ShowTask: Call ShowTask with taskID, apiConfig
ShowTask->>ShowTask: Validate taskID, select region
ShowTask->>HTTPClient: GET task result endpoint
alt Has ApiKey
ShowTask->>HTTPClient: Set Authorization Bearer header
end
HTTPClient->>MinerUServer: GET retrieve results
MinerUServer-->>HTTPClient: Return results with md_content
HTTPClient-->>ShowTask: Unmarshall response (200/202)
ShowTask->>ShowTask: Locate first md_content in results
ShowTask-->>User: Return TaskResponse with TaskSegment
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
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
🤖 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_local.go`:
- Around line 113-114: Check that the region-specific base URL exists and is
non-empty before building endpoint URLs: validate m.BaseURL[region] (used when
constructing apiURL with m.URLSuffix.DocumentParse and the other usage at the
section noted around lines 200-201), and if missing return a clear error (or
handle via fallback) instead of proceeding to fmt.Sprintf; update the methods
that construct apiURL to early-return an error when the base URL is absent and
include the region in the error message so callers can handle configuration
problems.
- Around line 221-257: The ShowTask handler in mineru_local.go currently treats
HTTP 202 like a finished response and proceeds to parse results, causing
misleading "missing results field"/"md_content not found" errors; update the
response handling so that if resp.StatusCode == 202 you short-circuit and return
a clear in-progress result (e.g., a dedicated error or status value like "task
in progress" with the response body/detail) instead of attempting JSON parse,
and only parse the body for status OK (200); adjust the logic around the
results/md_content checks (results variable and md_content lookup) to assume a
200-only code path so pending jobs produce a clear in-progress response from
ShowTask.
🪄 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: e00af4ad-6056-473d-96ca-dd25e17b0510
📒 Files selected for processing (6)
conf/models/mineru_local.jsoninternal/cli/lexer.gointernal/cli/types.gointernal/cli/user_parser.gointernal/entity/models/factory.gointernal/entity/models/mineru_local.go
| apiURL := fmt.Sprintf("%s/%s", m.BaseURL[region], m.URLSuffix.DocumentParse) | ||
|
|
There was a problem hiding this comment.
Validate region base URL before constructing endpoint URLs.
Both methods read m.BaseURL[region] without checking existence/emptiness, which can degrade into malformed URL errors when region config is missing.
Suggested fix
- apiURL := fmt.Sprintf("%s/%s", m.BaseURL[region], m.URLSuffix.DocumentParse)
+ baseURL, ok := m.BaseURL[region]
+ if !ok || baseURL == "" {
+ return nil, fmt.Errorf("no base URL configured for region %q", region)
+ }
+ apiURL := fmt.Sprintf("%s/%s", baseURL, m.URLSuffix.DocumentParse)- url := fmt.Sprintf("%s/%s/%s/result", m.BaseURL[region], m.URLSuffix.Task, taskID)
+ baseURL, ok := m.BaseURL[region]
+ if !ok || baseURL == "" {
+ return nil, fmt.Errorf("no base URL configured for region %q", region)
+ }
+ url := fmt.Sprintf("%s/%s/%s/result", baseURL, m.URLSuffix.Task, taskID)Also applies to: 200-201
🤖 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_local.go` around lines 113 - 114, Check that
the region-specific base URL exists and is non-empty before building endpoint
URLs: validate m.BaseURL[region] (used when constructing apiURL with
m.URLSuffix.DocumentParse and the other usage at the section noted around lines
200-201), and if missing return a clear error (or handle via fallback) instead
of proceeding to fmt.Sprintf; update the methods that construct apiURL to
early-return an error when the base URL is absent and include the region in the
error message so callers can handle configuration problems.
| if resp.StatusCode != http.StatusOK && resp.StatusCode != 202 { | ||
| return nil, fmt.Errorf("MinerU local status API failed with status %d: %s", resp.StatusCode, string(body)) | ||
| } | ||
|
|
||
| // parse JSON | ||
| var result map[string]interface{} | ||
|
|
||
| if err := json.Unmarshal(body, &result); err != nil { | ||
| return nil, fmt.Errorf("failed to parse JSON: %w", err) | ||
| } | ||
|
|
||
| content := "" | ||
|
|
||
| // results | ||
| results, ok := result["results"].(map[string]interface{}) | ||
| if !ok { | ||
| return nil, fmt.Errorf("missing results field") | ||
| } | ||
|
|
||
| // Get markdown | ||
| for _, fileObj := range results { | ||
|
|
||
| fileMap, ok := fileObj.(map[string]interface{}) | ||
| if !ok { | ||
| continue | ||
| } | ||
|
|
||
| md, ok := fileMap["md_content"].(string) | ||
| if ok { | ||
| content = md | ||
| break | ||
| } | ||
| } | ||
|
|
||
| if content == "" { | ||
| return nil, fmt.Errorf("md_content not found") | ||
| } |
There was a problem hiding this comment.
Handle 202 Accepted as an in-progress task, not a parse failure.
ShowTask allows 202 but immediately enforces results.md_content; pending jobs will be reported as "missing results field" / "md_content not found" instead of a clear “still processing” status.
Suggested fix
- if resp.StatusCode != http.StatusOK && resp.StatusCode != 202 {
+ if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusAccepted {
return nil, fmt.Errorf("MinerU local status API failed with status %d: %s", resp.StatusCode, string(body))
}
+
+ if resp.StatusCode == http.StatusAccepted {
+ return nil, fmt.Errorf("task %s is still processing", taskID)
+ }🤖 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_local.go` around lines 221 - 257, The ShowTask
handler in mineru_local.go currently treats HTTP 202 like a finished response
and proceeds to parse results, causing misleading "missing results
field"/"md_content not found" errors; update the response handling so that if
resp.StatusCode == 202 you short-circuit and return a clear in-progress result
(e.g., a dedicated error or status value like "task in progress" with the
response body/detail) instead of attempting JSON parse, and only parse the body
for status OK (200); adjust the logic around the results/md_content checks
(results variable and md_content lookup) to assume a 200-only code path so
pending jobs produce a clear in-progress response from ShowTask.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #15051 +/- ##
=======================================
Coverage 93.12% 93.12%
=======================================
Files 10 10
Lines 713 713
Branches 116 116
=======================================
Hits 664 664
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?
Verified from CLI
Type of change