Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

Add Somark VerifyOCRConnection (#21927)

fenglingxiansheng79376b99f949
diff --git a/internal/entity/models/somark.go b/internal/entity/models/somark.go
index 2b4261e2cd..dc83ce5359 100644
--- a/internal/entity/models/somark.go
+++ b/internal/entity/models/somark.go
@@ -109,22 +109,69 @@ func (s *SoMarkModel) AudioSpeechWithSender(ctx context.Context, modelName *stri
}
type soMarkSubmitResponse struct {
- Code int `json:"code"`
- Message string `json:"message"`
- Data struct {
+ Code int `json:"code"`
+ Message string `json:"message"`
+ Data json.RawMessage `json:"data"`
+}
+
+func (r soMarkSubmitResponse) taskID() string {
+ var data struct {
TaskID string `json:"task_id"`
- } `json:"data"`
+ }
+ if json.Unmarshal(r.Data, &data) == nil && strings.TrimSpace(data.TaskID) != "" {
+ return strings.TrimSpace(data.TaskID)
+ }
+ var taskID string
+ if json.Unmarshal(r.Data, &taskID) == nil {
+ return strings.TrimSpace(taskID)
+ }
+ return ""
}
type soMarkPollResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
- Status string `json:"status"`
- Result map[string]any `json:"result"`
+ Status string `json:"status"`
+ Message string `json:"message"`
+ Error string `json:"error"`
+ ErrorMessage string `json:"error_message"`
+ Detail string `json:"detail"`
+ Reason string `json:"reason"`
+ Result map[string]any `json:"result"`
} `json:"data"`
}
+func (r soMarkPollResponse) taskFailureMessage() string {
+ for _, message := range []string{
+ r.Data.ErrorMessage,
+ r.Data.Error,
+ r.Data.Detail,
+ r.Data.Reason,
+ r.Data.Message,
+ r.Message,
+ } {
+ message = strings.TrimSpace(message)
+ if message != "" && !isSoMarkQueryStatusMessage(message) {
+ return message
+ }
+ }
+ return ""
+}
+
+func isSoMarkQueryStatusMessage(message string) bool {
+ switch strings.ToLower(strings.TrimSpace(message)) {
+ case "查询成功", "查询成功!", "query success", "query successful":
+ return true
+ default:
+ return false
+ }
+}
+
+func soMarkBusinessError(code int) error {
+ return fmt.Errorf("API request rejected (provider error code=%d)", code)
+}
+
// soMarkFormFileName picks the multipart filename the SoMark API uses for
// content-type detection. OCRFile has no filename parameter, so the driver
// sniffs the well-known magic prefixes; anything else defaults to PDF.
@@ -201,12 +248,13 @@ func (s *SoMarkModel) soMarkSubmit(ctx context.Context, submitURL string, conten
return "", fmt.Errorf("SoMark decode submit: %w", err)
}
if payload.Code != 0 {
- return "", fmt.Errorf("SoMark submit business error code=%d message=%s", payload.Code, payload.Message)
+ return "", soMarkBusinessError(payload.Code)
}
- if payload.Data.TaskID == "" {
+ taskID := payload.taskID()
+ if taskID == "" {
return "", fmt.Errorf("SoMark submit returned no task_id")
}
- return payload.Data.TaskID, nil
+ return taskID, nil
}
// soMarkPoll polls the async task until it reaches SUCCESS and returns the
@@ -244,7 +292,7 @@ func (s *SoMarkModel) soMarkPoll(ctx context.Context, pollURL, taskID, apiKey st
return nil, fmt.Errorf("SoMark decode poll: %w", err)
}
if payload.Code != 0 {
- return nil, fmt.Errorf("SoMark poll business error code=%d message=%s", payload.Code, payload.Message)
+ return nil, soMarkBusinessError(payload.Code)
}
switch strings.ToUpper(strings.TrimSpace(payload.Data.Status)) {
case "SUCCESS":
@@ -253,7 +301,10 @@ func (s *SoMarkModel) soMarkPoll(ctx context.Context, pollURL, taskID, apiKey st
}
return payload.Data.Result, nil
case "FAILED", "ERROR":
- return nil, fmt.Errorf("SoMark task %s failed with status=%s message=%s", taskID, payload.Data.Status, payload.Message)
+ if message := payload.taskFailureMessage(); message != "" {
+ return nil, fmt.Errorf("SoMark task %s failed with status=%s: %s", taskID, payload.Data.Status, message)
+ }
+ return nil, fmt.Errorf("SoMark task %s failed with status=%s; no task error details were returned", taskID, payload.Data.Status)
}
common.Debug("somark poll: in progress",
@@ -430,7 +481,42 @@ func (s *SoMarkModel) Balance(ctx context.Context, apiConfig *APIConfig) (map[st
}
func (s *SoMarkModel) CheckConnection(ctx context.Context, apiConfig *APIConfig) error {
- return fmt.Errorf("%s: no such method: CheckConnection", s.Name())
+ return s.VerifyOCRConnection(ctx, apiConfig)
+}
+
+// VerifyOCRConnection checks SoMark credentials and endpoint reachability by
+// submitting the same tiny probe image used by generic OCR verification. The
+// async service may reject parsing a 1x1 image after accepting the request, so
+// verification intentionally stops after a task is accepted instead of
+// waiting for that probe task to finish.
+func (s *SoMarkModel) VerifyOCRConnection(ctx context.Context, apiConfig *APIConfig) error {
+ if err := s.baseModel.APIConfigCheck(apiConfig); err != nil {
+ return err
+ }
+ submitURL, err := s.baseModel.EndpointURL(apiConfig, s.baseModel.URLSuffix.OCR)
+ if err != nil {
+ return err
+ }
+ apiKey := ""
+ if apiConfig != nil && apiConfig.ApiKey != nil {
+ apiKey = *apiConfig.ApiKey
+ }
+ ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
+ defer cancel()
+ _, err = s.soMarkSubmit(ctx, submitURL, soMarkVerificationPNG, apiKey)
+ return err
+}
+
+var soMarkVerificationPNG = []byte{
+ 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A,
+ 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52,
+ 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
+ 0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x77, 0x53,
+ 0xDE, 0x00, 0x00, 0x00, 0x0C, 0x49, 0x44, 0x41,
+ 0x54, 0x78, 0xDA, 0x63, 0xF8, 0xFF, 0xFF, 0x3F,
+ 0x00, 0x05, 0xFE, 0x02, 0xFE, 0x33, 0x12, 0x95,
+ 0x14, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E,
+ 0x44, 0xAE, 0x42, 0x60, 0x82,
}
func (s *SoMarkModel) ListTasks(ctx context.Context, apiConfig *APIConfig) ([]ListTaskStatus, error) {
diff --git a/internal/entity/models/somark_test.go b/internal/entity/models/somark_test.go
new file mode 100644
index 0000000000..0e4214fa45
--- /dev/null
+++ b/internal/entity/models/somark_test.go
@@ -0,0 +1,177 @@
+package models
+
+import (
+ "bytes"
+ "image/png"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+)
+
+func TestSoMarkVerifyOCRConnectionStopsAfterTaskAccepted(t *testing.T) {
+ withSSRFBypass(t)
+ var submitCalls, pollCalls int
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ switch r.URL.Path {
+ case "/api/v1/parse/async":
+ submitCalls++
+ reader, err := r.MultipartReader()
+ if err != nil {
+ t.Errorf("MultipartReader: %v", err)
+ return
+ }
+ var apiKey string
+ for {
+ part, err := reader.NextPart()
+ if err == io.EOF {
+ break
+ }
+ if err != nil {
+ t.Errorf("NextPart: %v", err)
+ return
+ }
+ value, _ := io.ReadAll(part)
+ if part.FormName() == "api_key" {
+ apiKey = string(value)
+ }
+ if part.FormName() == "file" {
+ probe, decodeErr := png.Decode(bytes.NewReader(value))
+ if decodeErr != nil {
+ t.Errorf("decode verification PNG: %v", decodeErr)
+ } else if bounds := probe.Bounds(); bounds.Dx() != 1 || bounds.Dy() != 1 {
+ t.Errorf("verification PNG bounds = %v, want 1x1", bounds)
+ }
+ }
+ }
+ if apiKey != "somark-test-key" {
+ t.Errorf("api_key = %q, want configured key", apiKey)
+ }
+ _, _ = w.Write([]byte(`{"code":0,"message":"accepted","data":{"task_id":"task-verify"}}`))
+ case "/api/v1/parse/async_check":
+ pollCalls++
+ // A 1x1 probe can legitimately fail parsing; model verification only
+ // checks that SoMark accepted the authenticated submission.
+ _, _ = w.Write([]byte(`{"code":0,"message":"查询成功","data":{"status":"FAILED"}}`))
+ default:
+ http.NotFound(w, r)
+ }
+ }))
+ defer server.Close()
+
+ driver := NewSoMarkModel(
+ map[string]string{"default": server.URL + "/api/v1"},
+ URLSuffix{OCR: "parse/async", Tasks: "parse/async_check"},
+ )
+ apiKey := "somark-test-key"
+ err := driver.VerifyOCRConnection(t.Context(), &APIConfig{ApiKey: &apiKey})
+ if err != nil {
+ t.Fatalf("VerifyOCRConnection: %v", err)
+ }
+ if submitCalls != 1 || pollCalls != 0 {
+ t.Fatalf("submit/poll calls = %d/%d, want 1/0", submitCalls, pollCalls)
+ }
+}
+
+func TestSoMarkVerifyOCRConnectionReturnsSubmitError(t *testing.T) {
+ withSSRFBypass(t)
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ _, _ = w.Write([]byte(`{"code":1107,"message":"无效的API密钥"}`))
+ }))
+ defer server.Close()
+
+ driver := NewSoMarkModel(
+ map[string]string{"default": server.URL + "/api/v1"},
+ URLSuffix{OCR: "parse/async", Tasks: "parse/async_check"},
+ )
+ apiKey := "bad-key"
+ err := driver.VerifyOCRConnection(t.Context(), &APIConfig{ApiKey: &apiKey})
+ if err == nil || err.Error() != "API request rejected (provider error code=1107)" {
+ t.Fatalf("VerifyOCRConnection error = %v, want normalized provider error", err)
+ }
+ if strings.Contains(err.Error(), "无效的API密钥") {
+ t.Fatalf("VerifyOCRConnection error = %q, must not expose provider-specific localized text", err)
+ }
+}
+
+func TestSoMarkVerifyOCRConnectionAcceptsStringTaskID(t *testing.T) {
+ withSSRFBypass(t)
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ _, _ = w.Write([]byte(`{"code":0,"message":"accepted","data":"task-verify"}`))
+ }))
+ defer server.Close()
+
+ driver := NewSoMarkModel(
+ map[string]string{"default": server.URL + "/api/v1"},
+ URLSuffix{OCR: "parse/async", Tasks: "parse/async_check"},
+ )
+ apiKey := "somark-test-key"
+ if err := driver.VerifyOCRConnection(t.Context(), &APIConfig{ApiKey: &apiKey}); err != nil {
+ t.Fatalf("VerifyOCRConnection with string task_id: %v", err)
+ }
+}
+
+func TestSoMarkPollFailureIgnoresGenericQuerySuccessMessage(t *testing.T) {
+ withSSRFBypass(t)
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ switch r.URL.Path {
+ case "/api/v1/parse/async":
+ _, _ = w.Write([]byte(`{"code":0,"data":{"task_id":"task-failed"}}`))
+ case "/api/v1/parse/async_check":
+ _, _ = w.Write([]byte(`{"code":0,"message":"查询成功","data":{"status":"FAILED"}}`))
+ default:
+ http.NotFound(w, r)
+ }
+ }))
+ defer server.Close()
+
+ driver := NewSoMarkModel(
+ map[string]string{"default": server.URL + "/api/v1"},
+ URLSuffix{OCR: "parse/async", Tasks: "parse/async_check"},
+ )
+ apiKey := "somark-test-key"
+ modelName := "SoMark-model"
+ _, err := driver.OCRFile(t.Context(), &modelName, []byte("%PDF-1.4"), nil, &APIConfig{ApiKey: &apiKey}, nil, nil)
+ if err == nil {
+ t.Fatal("OCRFile error = nil, want failed task error")
+ }
+ if !strings.Contains(err.Error(), "task-failed") || !strings.Contains(err.Error(), "status=FAILED") {
+ t.Fatalf("OCRFile error = %q, want task id and failed status", err)
+ }
+ if strings.Contains(err.Error(), "查询成功") {
+ t.Fatalf("OCRFile error = %q, must not report generic query success as task failure reason", err)
+ }
+ if !strings.Contains(err.Error(), "no task error details") {
+ t.Fatalf("OCRFile error = %q, want clear missing task-error details", err)
+ }
+}
+
+func TestSoMarkPollFailureReturnsTaskErrorDetails(t *testing.T) {
+ withSSRFBypass(t)
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ switch r.URL.Path {
+ case "/api/v1/parse/async":
+ _, _ = w.Write([]byte(`{"code":0,"data":{"task_id":"task-failed"}}`))
+ case "/api/v1/parse/async_check":
+ _, _ = w.Write([]byte(`{"code":0,"message":"查询成功","data":{"status":"FAILED","error_message":"invalid API key"}}`))
+ default:
+ http.NotFound(w, r)
+ }
+ }))
+ defer server.Close()
+
+ driver := NewSoMarkModel(
+ map[string]string{"default": server.URL + "/api/v1"},
+ URLSuffix{OCR: "parse/async", Tasks: "parse/async_check"},
+ )
+ apiKey := "somark-test-key"
+ modelName := "SoMark-model"
+ _, err := driver.OCRFile(t.Context(), &modelName, []byte("%PDF-1.4"), nil, &APIConfig{ApiKey: &apiKey}, nil, nil)
+ if err == nil || !strings.Contains(err.Error(), "invalid API key") {
+ t.Fatalf("OCRFile error = %v, want task error details", err)
+ }
+ if strings.Contains(err.Error(), "查询成功") {
+ t.Fatalf("OCRFile error = %q, must not report generic query success as task failure reason", err)
+ }
+}
diff --git a/internal/service/model_service.go b/internal/service/model_service.go
index 03863240f3..48f6ba6511 100644
--- a/internal/service/model_service.go
+++ b/internal/service/model_service.go
@@ -1718,6 +1718,11 @@ func verifyASRModel(ctx context.Context, driver modelModule.ModelDriver, modelNa
// providers return an error or empty text for such a trivial image;
// we accept any non-error response as a successful connectivity check.
func verifyOCRModel(ctx context.Context, driver modelModule.ModelDriver, modelName string, apiConfig *modelModule.APIConfig) error {
+ if verifier, ok := driver.(interface {
+ VerifyOCRConnection(context.Context, *modelModule.APIConfig) error
+ }); ok {
+ return verifier.VerifyOCRConnection(ctx, apiConfig)
+ }
// Send a minimal 1×1 white PNG through the OCR pipeline to verify
// connectivity. Most OCRModel drivers only check server reachability
// rather than performing full document parsing.
diff --git a/internal/service/model_service_test.go b/internal/service/model_service_test.go
index df2d99c40e..878e17a2bb 100644
--- a/internal/service/model_service_test.go
+++ b/internal/service/model_service_test.go
@@ -54,6 +54,32 @@ func (d *remoteModelProbeDriver) CheckConnection(_ context.Context, apiConfig *m
return d.checkErr
}
+type ocrVerificationProbeDriver struct {
+ *modelModule.DummyModel
+ verifyCalls int
+ ocrCalls int
+}
+
+func (d *ocrVerificationProbeDriver) VerifyOCRConnection(context.Context, *modelModule.APIConfig) error {
+ d.verifyCalls++
+ return nil
+}
+
+func (d *ocrVerificationProbeDriver) OCRFile(context.Context, *string, []byte, *string, *modelModule.APIConfig, *modelModule.OCRConfig, *common.ModelUsage) (*modelModule.OCRFileResponse, error) {
+ d.ocrCalls++
+ return nil, nil
+}
+
+func TestVerifyOCRModelUsesProviderConnectionVerifier(t *testing.T) {
+ driver := &ocrVerificationProbeDriver{DummyModel: modelModule.NewDummyModel(nil, modelModule.URLSuffix{})}
+ if err := verifyOCRModel(t.Context(), driver, "somark-model", &modelModule.APIConfig{}); err != nil {
+ t.Fatalf("verifyOCRModel: %v", err)
+ }
+ if driver.verifyCalls != 1 || driver.ocrCalls != 0 {
+ t.Fatalf("verification/OCR calls = %d/%d, want 1/0", driver.verifyCalls, driver.ocrCalls)
+ }
+}
+
func TestMonkeyOCREnvConfig(t *testing.T) {
t.Setenv(common.EnvMonkeyOCRAPIServer, "http://monkeyocr:7861")