-
Notifications
You must be signed in to change notification settings - Fork 11.8k
feat(mineru): add MinerU /v1/file_parse relay endpoint (channel type 64) #7588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
2388832
wants to merge
4
commits into
QuantumNous:main
Choose a base branch
from
2388832:feat/mineru-relay
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+395
−1
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
eb7a062
feat(mineru): add MinerU /v1/file_parse relay endpoint
invalid-email-address 2523a2f
fix(mineru): address code review findings
invalid-email-address 47d1872
fix(mineru): close credential-transport bypasses found in review
invalid-email-address 736cc30
fix(mineru): pin validated address to close DNS rebinding window
invalid-email-address File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| package mineru | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "net" | ||
| "net/http" | ||
| "net/url" | ||
| "strings" | ||
|
|
||
| "github.com/QuantumNous/new-api/relay/channel" | ||
| relaycommon "github.com/QuantumNous/new-api/relay/common" | ||
| "github.com/QuantumNous/new-api/relaykit/dto" | ||
| "github.com/QuantumNous/new-api/relaykit/types" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| const ChannelName = "mineru" | ||
|
|
||
| var ModelList = []string{"mineru"} | ||
|
|
||
| type Adaptor struct { | ||
| } | ||
|
|
||
| func (a *Adaptor) Init(info *relaycommon.RelayInfo) { | ||
| } | ||
|
|
||
| // GetRequestURL returns channel base_url + /file_parse. | ||
| // Local MinerU: base_url = http://mineru-api:8000 | ||
| // Upstream API: base_url = https://gateway.example.com/v1 | ||
| func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) { | ||
| baseUrl := strings.TrimRight(info.ChannelBaseUrl, "/") | ||
| if baseUrl == "" { | ||
| return "", errors.New("mineru channel base_url is empty") | ||
| } | ||
| return fmt.Sprintf("%s/file_parse", baseUrl), nil | ||
| } | ||
|
|
||
| func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error { | ||
| // Multipart passthrough: Content-Type (with boundary) is set by | ||
| // DoFormRequest from the incoming request. | ||
| if info.ApiKey != "" { | ||
| if err := ensureSecureCredentialTransport(info.ChannelBaseUrl); err != nil { | ||
| return err | ||
| } | ||
| req.Set("Authorization", fmt.Sprintf("Bearer %s", info.ApiKey)) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // ensureSecureCredentialTransport refuses to send a Bearer credential over | ||
| // cleartext http:// to a non-private target (CWE-319). Documented local | ||
| // MinerU deployments on loopback / RFC1918 private networks / single-label | ||
| // container hostnames (e.g. http://mineru-api:8000) remain supported; | ||
| // any other target must use https. | ||
| func ensureSecureCredentialTransport(baseURL string) error { | ||
| u, err := url.Parse(baseURL) | ||
| if err != nil || u.Host == "" { | ||
| return fmt.Errorf("invalid mineru channel base_url: %q", baseURL) | ||
| } | ||
| if strings.EqualFold(u.Scheme, "https") { | ||
| return nil | ||
| } | ||
| if isPrivateOrLocalHost(u.Hostname()) { | ||
| return nil | ||
| } | ||
| return fmt.Errorf("refusing to send Authorization over insecure %s channel base_url %q: use https or a private-network address", u.Scheme, baseURL) | ||
| } | ||
|
|
||
| // isPrivateOrLocalHost reports whether host points at a trusted local | ||
| // target: "localhost", loopback, RFC1918/RFC4193 private or link-local | ||
| // address, or a single-label hostname (container / intranet DNS name). | ||
| func isPrivateOrLocalHost(host string) bool { | ||
| h := strings.ToLower(strings.TrimSpace(host)) | ||
| if h == "" { | ||
| return false | ||
| } | ||
| if h == "localhost" { | ||
| return true | ||
| } | ||
| if ip := net.ParseIP(h); ip != nil { | ||
| return ip.IsLoopback() || ip.IsPrivate() || ip.IsLinkLocalUnicast() | ||
| } | ||
| // Non-IP literal: single-label hostnames (no dot) are treated as | ||
| // container/intranet names, e.g. "mineru-api". | ||
| return !strings.Contains(h, ".") | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) { | ||
| return channel.DoFormRequest(a, c, info, requestBody) | ||
| } | ||
|
|
||
| func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) { | ||
| // The response body is streamed back verbatim by MinerUHelper; here we | ||
| // only return zero usage (per-call billing). | ||
| return &dto.Usage{}, nil | ||
| } | ||
|
|
||
| func (a *Adaptor) GetModelList() []string { | ||
| return ModelList | ||
| } | ||
|
|
||
| func (a *Adaptor) GetChannelName() string { | ||
| return ChannelName | ||
| } | ||
|
|
||
| // Stubs required by the channel.Adaptor interface (not used by MinerU relay). | ||
|
|
||
| func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) { | ||
| return nil, errors.New("not implemented") | ||
| } | ||
|
|
||
| func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) { | ||
| return nil, errors.New("not implemented") | ||
| } | ||
|
|
||
| func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) { | ||
| return nil, errors.New("not implemented") | ||
| } | ||
|
|
||
| func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) { | ||
| return nil, errors.New("not implemented") | ||
| } | ||
|
|
||
| func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) { | ||
| return nil, errors.New("not implemented") | ||
| } | ||
|
|
||
| func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) { | ||
| return nil, errors.New("not implemented") | ||
| } | ||
|
|
||
| func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.ClaudeRequest) (any, error) { | ||
| return nil, errors.New("not implemented") | ||
| } | ||
|
|
||
| func (a *Adaptor) ConvertGeminiRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeminiChatRequest) (any, error) { | ||
| return nil, errors.New("not implemented") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package relay | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
|
|
||
| relaycommon "github.com/QuantumNous/new-api/relay/common" | ||
| "github.com/QuantumNous/new-api/relaykit/dto" | ||
| "github.com/QuantumNous/new-api/relaykit/types" | ||
| "github.com/QuantumNous/new-api/service" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| // MinerUHelper forwards /v1/file_parse (multipart/form-data) requests to | ||
| // channel base_url + /file_parse and streams the upstream response back to | ||
| // the client verbatim. Channel conventions: | ||
| // - Local MinerU: base_url = http://mineru-api:8000 | ||
| // - Upstream API: base_url = https://gateway.example.com/v1 | ||
| func MinerUHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *types.NewAPIError) { | ||
| info.InitChannelMeta(c) | ||
|
|
||
| if _, ok := info.Request.(*dto.MinerURequest); !ok { | ||
| return types.NewError(errors.New("invalid request type"), types.ErrorCodeInvalidRequest, types.ErrOptionWithSkipRetry()) | ||
| } | ||
|
|
||
| adaptor := GetAdaptor(info.ApiType) | ||
| if adaptor == nil { | ||
| return types.NewError(fmt.Errorf("invalid api type: %d", info.ApiType), types.ErrorCodeInvalidApiType, types.ErrOptionWithSkipRetry()) | ||
| } | ||
| adaptor.Init(info) | ||
|
|
||
| // The multipart body has already been made replayable (BodyStorage) by | ||
| // controller.Relay; forward it as-is. | ||
| resp, err := adaptor.DoRequest(c, info, c.Request.Body) | ||
| if err != nil { | ||
| return types.NewOpenAIError(err, types.ErrorCodeDoRequestFailed, http.StatusInternalServerError) | ||
| } | ||
| httpResp, ok := resp.(*http.Response) | ||
| if !ok || httpResp == nil { | ||
| return types.NewError(errors.New("invalid upstream response"), types.ErrorCodeDoRequestFailed, types.ErrOptionWithSkipRetry()) | ||
| } | ||
| defer httpResp.Body.Close() | ||
|
|
||
| statusCodeMappingStr := c.GetString("status_code_mapping") | ||
| if httpResp.StatusCode < 200 || httpResp.StatusCode > 299 { | ||
| newAPIError = service.RelayErrorHandler(c.Request.Context(), httpResp, false) | ||
| service.ResetStatusCode(newAPIError, statusCodeMappingStr) | ||
| return newAPIError | ||
| } | ||
|
|
||
| // Stream the successful response back verbatim (JSON / ZIP alike), | ||
| // preserving the upstream status code. | ||
| if contentType := httpResp.Header.Get("Content-Type"); contentType != "" { | ||
| c.Writer.Header().Set("Content-Type", contentType) | ||
| } | ||
| if cd := httpResp.Header.Get("Content-Disposition"); cd != "" { | ||
| c.Writer.Header().Set("Content-Disposition", cd) | ||
| } | ||
| c.Status(httpResp.StatusCode) | ||
| if _, err := io.Copy(c.Writer, httpResp.Body); err != nil { | ||
| // The 2xx status line is already committed and cannot be replaced; | ||
| // return a non-retryable error so the request is not marked | ||
| // successful and the reserved charge gets refunded on the existing | ||
| // failure path. | ||
| return types.NewError(err, types.ErrorCodeReadResponseBodyFailed, types.ErrOptionWithSkipRetry()) | ||
| } | ||
|
|
||
| // Per-call billing (mineru is quota_type=1, priced per call; zero usage). | ||
| service.PostTextConsumeQuota(c, info, &dto.Usage{}, nil) | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package dto | ||
|
|
||
| // MinerURequest 表示 /v1/file_parse 的 multipart 表单请求。 | ||
| // 表单内容(files 及解析参数)原样透传给上游 MinerU 服务, | ||
| // 这里只提取渠道选择所需的元信息(model,可省略,默认 mineru)。 | ||
| type MinerURequest struct { | ||
| BaseRequest | ||
| Model string `json:"model" form:"model"` | ||
| } | ||
|
|
||
| func (r *MinerURequest) SetModelName(modelName string) { | ||
| if modelName != "" { | ||
| r.Model = modelName | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.