RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix: implement CheckConnection and OCRFile for local MinerU driver (#20459)
Rivenae7730db4615
diff --git a/internal/entity/models/mineru_local.go b/internal/entity/models/mineru_local.goindex c3b9813c1d..4d3dbce4e3 100644--- a/internal/entity/models/mineru_local.go+++ b/internal/entity/models/mineru_local.go@@ -24,6 +24,7 @@ import ("io""mime/multipart""net/http"+ "net/url""ragflow/internal/common""strings")@@ -83,8 +84,14 @@ func (m *MinerULocalModel) AudioSpeechWithSender(ctx context.Context, modelNamereturn fmt.Errorf("%s no such method", m.Name())}+// OCRFile is exercised during model verification (verifyOCRModel). Local MinerU+// is a document-parsing API and has no image-OCR endpoint, so delegate to+// CheckConnection — same pattern as the hosted mineru.net driver (mineru.go).func (m *MinerULocalModel) OCRFile(ctx context.Context, modelName *string, content []byte, url *string, apiConfig *APIConfig, ocrConfig *OCRConfig, modelUsage *common.ModelUsage) (*OCRFileResponse, error) {- return nil, fmt.Errorf("%s no such method", m.Name())+ if err := m.CheckConnection(ctx, apiConfig); err != nil {+ return nil, err+ }+ return &OCRFileResponse{}, nil}func (m *MinerULocalModel) ListModels(ctx context.Context, apiConfig *APIConfig) ([]ListModelResponse, error) {@@ -96,7 +103,47 @@ func (m *MinerULocalModel) Balance(ctx context.Context, apiConfig *APIConfig) (m}func (m *MinerULocalModel) CheckConnection(ctx context.Context, apiConfig *APIConfig) error {- return fmt.Errorf("%s no such method", m.Name())+ if err := m.baseModel.APIConfigCheck(apiConfig); err != nil {+ return err+ }+ resolvedBaseURL, err := m.baseModel.GetBaseURL(apiConfig)+ if err != nil {+ return err+ }+ u, err := url.Parse(resolvedBaseURL)+ if err != nil {+ return fmt.Errorf("invalid base URL: %w", err)+ }+ // MinerU's API service exposes GET /health; keep scheme/host and replace+ // only the path so a base URL with extra path segments or query stays sane.+ u.Path = "/health"+ ctx, cancel := context.WithTimeout(ctx, nonStreamCallTimeout)+ defer cancel()+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)+ if err != nil {+ return fmt.Errorf("failed to create request: %w", err)+ }+ // Same token extraction as ParseFile/ShowTask: a plain api_key is a bearer+ // token, while MinerU provider JSON config carries no bearer unless it+ // embeds mineru_api_key/access_token — sending the raw payload would be an+ // invalid header.+ if apiConfig != nil && apiConfig.ApiKey != nil {+ if token := MinerUBearerTokenFromAPIKey(*apiConfig.ApiKey); token != "" {+ req.Header.Set("Authorization", "Bearer "+token)+ }+ }+ resp, err := m.baseModel.httpClient.Do(req)+ if err != nil {+ return fmt.Errorf("connection failed: %w", err)+ }+ defer resp.Body.Close()+ // Match the hosted mineru.net driver's semantics (mineru.go): 401/403 mean+ // the credentials are wrong, while any other HTTP answer (e.g. 404 on a+ // service without /health) still proves the endpoint is reachable.+ if resp.StatusCode == http.StatusUnauthorized || resp.StatusCode == http.StatusForbidden {+ return fmt.Errorf("authentication failed (HTTP %d)", resp.StatusCode)+ }+ return nil}func (m *MinerULocalModel) ParseFile(ctx context.Context, modelName *string, content []byte, documentURL *string, apiConfig *APIConfig, parseFileConfig *ParseFileConfig, modelUsage *common.ModelUsage) (*ParseFileResponse, error) {diff --git a/internal/entity/models/mineru_local_test.go b/internal/entity/models/mineru_local_test.goindex bab9ab95e9..1d07903b3f 100644--- a/internal/entity/models/mineru_local_test.go+++ b/internal/entity/models/mineru_local_test.go@@ -103,3 +103,108 @@ func TestMinerULocalParseFileMultipartFieldNames(t *testing.T) {t.Fatalf("ParseFile: %v", err)}}++func TestMinerULocalCheckConnectionPlainKey(t *testing.T) {+ var gotAuth string+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ if r.URL.Path != "/health" {+ http.Error(w, "unexpected path "+r.URL.Path, http.StatusNotFound)+ return+ }+ gotAuth = r.Header.Get("Authorization")+ w.WriteHeader(http.StatusOK)+ }))+ defer server.Close()++ baseURL := server.URL+ driver := NewMinerLocalUModel(map[string]string{"default": baseURL}, URLSuffix{DocumentParse: "file_parse", Task: "tasks"})+ apiKey := "plain-secret"+ if err := driver.CheckConnection(context.Background(), &APIConfig{BaseURL: &baseURL, ApiKey: &apiKey}); err != nil {+ t.Fatalf("CheckConnection: %v", err)+ }+ if gotAuth != "Bearer plain-secret" {+ t.Fatalf("Authorization = %q, want Bearer plain-secret", gotAuth)+ }+}++// Provider JSON config carries no bearer token unless mineru_api_key /+// access_token is present — the health probe must not send the raw payload.+func TestMinerULocalCheckConnectionJSONConfigNoBearer(t *testing.T) {+ var gotAuth string+ var authSeen bool+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ gotAuth = r.Header.Get("Authorization")+ _, authSeen = r.Header["Authorization"]+ w.WriteHeader(http.StatusOK)+ }))+ defer server.Close()++ baseURL := server.URL+ driver := NewMinerLocalUModel(map[string]string{"default": baseURL}, URLSuffix{DocumentParse: "file_parse", Task: "tasks"})+ apiKey := `{"mineru_backend":"vlm-http-client","mineru_server_url":"http://vllm:30000"}`+ if err := driver.CheckConnection(context.Background(), &APIConfig{BaseURL: &baseURL, ApiKey: &apiKey}); err != nil {+ t.Fatalf("CheckConnection: %v", err)+ }+ if authSeen && gotAuth != "" {+ t.Fatalf("unexpected Authorization header %q for JSON config without token", gotAuth)+ }+}++// Non-auth codes (e.g. 404 on a service without /health) still prove the+// endpoint is reachable.+func TestMinerULocalCheckConnectionNonAuthCodeIsReachable(t *testing.T) {+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ http.Error(w, "not found", http.StatusNotFound)+ }))+ defer server.Close()++ baseURL := server.URL+ driver := NewMinerLocalUModel(map[string]string{"default": baseURL}, URLSuffix{DocumentParse: "file_parse", Task: "tasks"})+ if err := driver.CheckConnection(context.Background(), &APIConfig{BaseURL: &baseURL}); err != nil {+ t.Fatalf("CheckConnection on 404: %v", err)+ }+}++func TestMinerULocalCheckConnectionAuthFailure(t *testing.T) {+ for _, code := range []int{http.StatusUnauthorized, http.StatusForbidden} {+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ http.Error(w, "denied", code)+ }))+ baseURL := server.URL+ driver := NewMinerLocalUModel(map[string]string{"default": baseURL}, URLSuffix{DocumentParse: "file_parse", Task: "tasks"})+ apiKey := "wrong-key"+ err := driver.CheckConnection(context.Background(), &APIConfig{BaseURL: &baseURL, ApiKey: &apiKey})+ server.Close()+ if err == nil {+ t.Fatalf("CheckConnection on HTTP %d: expected error", code)+ }+ }+}++func TestMinerULocalCheckConnectionUnreachable(t *testing.T) {+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))+ baseURL := server.URL+ server.Close()++ driver := NewMinerLocalUModel(map[string]string{"default": baseURL}, URLSuffix{DocumentParse: "file_parse", Task: "tasks"})+ if err := driver.CheckConnection(context.Background(), &APIConfig{BaseURL: &baseURL}); err == nil {+ t.Fatal("CheckConnection against a closed server: expected error")+ }+}++func TestMinerULocalOCRFileDelegatesToCheckConnection(t *testing.T) {+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ w.WriteHeader(http.StatusOK)+ }))+ defer server.Close()++ baseURL := server.URL+ driver := NewMinerLocalUModel(map[string]string{"default": baseURL}, URLSuffix{DocumentParse: "file_parse", Task: "tasks"})+ res, err := driver.OCRFile(context.Background(), nil, nil, nil, &APIConfig{BaseURL: &baseURL}, nil, nil)+ if err != nil {+ t.Fatalf("OCRFile: %v", err)+ }+ if res == nil {+ t.Fatal("OCRFile returned nil response")+ }+}