Skip to content

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

fix: SoMark web page is missing (#21710)

fenglingxiansheng14336bba2dd6
diff --git a/conf/models/somark.json b/conf/models/somark.json
new file mode 100644
index 0000000000..3642723b19
--- /dev/null
+++ b/conf/models/somark.json
@@ -0,0 +1,20 @@
+{
+ "name": "SoMark",
+ "url": {
+ "default": "https://somark.cn/api/v1",
+ "intl": "https://somark.ai/api/v1"
+ },
+ "url_suffix": {
+ "ocr": "parse/async",
+ "tasks": "parse/async_check"
+ },
+ "class": "somark",
+ "models": [
+ {
+ "name": "SoMark-model",
+ "model_types": [
+ "ocr"
+ ]
+ }
+ ]
+}
diff --git a/internal/entity/models/factory.go b/internal/entity/models/factory.go
index 0bb580ab2b..30f9fb066b 100644
--- a/internal/entity/models/factory.go
+++ b/internal/entity/models/factory.go
@@ -146,6 +146,8 @@ func (f *ModelFactory) CreateModelDriver(providerName string, baseURL map[string
return NewVoyageModel(baseURL, urlSuffix), nil
case "paddleocr":
return NewPaddleOCRModel(baseURL, urlSuffix), nil
+ case "somark":
+ return NewSoMarkModel(baseURL, urlSuffix), nil
case "xunfei spark":
return NewXunFeiModel(baseURL, urlSuffix), nil
case "deepinfra":
diff --git a/internal/entity/models/somark.go b/internal/entity/models/somark.go
new file mode 100644
index 0000000000..d73e40ca0b
--- /dev/null
+++ b/internal/entity/models/somark.go
@@ -0,0 +1,442 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package models
+
+import (
+ "bytes"
+ "context"
+ "encoding/json"
+ "fmt"
+ "io"
+ "mime/multipart"
+ "net/http"
+ "net/url"
+ "strings"
+ "time"
+
+ "ragflow/internal/common"
+
+ "go.uber.org/zap"
+)
+
+// SoMarkModel drives the SoMark document-understanding service
+// (https://somark.cn / https://somark.ai). The provider exposes an
+// asynchronous parse protocol: POST {base}/parse/async uploads the document
+// and returns a task_id, POST {base}/parse/async_check polls the task until
+// it reaches SUCCESS and carries the parsed block structure in
+// data.result.outputs.json. Authentication is a form field (api_key), not an
+// Authorization header, and self-hosted deployments may run without a key -
+// hence AllowEmptyAPIKey. The same protocol is implemented by the DeepDoc
+// parser path (internal/parser/parser/pdf_parser_somark.go); this driver
+// serves the model-provider surface (connection verification and the
+// /v1/file/ocr route).
+type SoMarkModel struct {
+ baseModel BaseModel
+}
+
+func NewSoMarkModel(baseURL map[string]string, urlSuffix URLSuffix) *SoMarkModel {
+ return &SoMarkModel{
+ baseModel: BaseModel{
+ BaseURL: baseURL,
+ URLSuffix: urlSuffix,
+ AllowEmptyAPIKey: true,
+ httpClient: common.GetSSRFHTTPClient(),
+ },
+ }
+}
+
+func (s *SoMarkModel) NewInstance(baseURL map[string]string) ModelDriver {
+ return NewSoMarkModel(baseURL, s.baseModel.URLSuffix)
+}
+
+func (s *SoMarkModel) Name() string {
+ return "somark"
+}
+
+// GetBaseURL resolves the configured region when an instance has no custom URL.
+func (s *SoMarkModel) GetBaseURL(apiConfig *APIConfig) (string, error) {
+ return s.baseModel.GetBaseURL(apiConfig)
+}
+
+func (s *SoMarkModel) ChatWithMessages(ctx context.Context, modelName string, messages []Message, apiConfig *APIConfig, chatModelConfig *ChatConfig, modelUsage *common.ModelUsage) (*ChatResponse, error) {
+ return nil, fmt.Errorf("%s, no such method", s.Name())
+}
+
+func (s *SoMarkModel) ChatStreamlyWithSender(ctx context.Context, modelName string, messages []Message, apiConfig *APIConfig, modelConfig *ChatConfig, modelUsage *common.ModelUsage, sender func(*string, *string) error) error {
+ return fmt.Errorf("%s, no such method", s.Name())
+}
+
+func (s *SoMarkModel) Embed(ctx context.Context, modelName *string, request EmbedRequest, apiConfig *APIConfig, embeddingConfig *EmbeddingConfig, modelUsage *common.ModelUsage) ([]EmbeddingData, error) {
+ return nil, fmt.Errorf("%s, no such method", s.Name())
+}
+
+func (s *SoMarkModel) Rerank(ctx context.Context, modelName *string, request RerankRequest, apiConfig *APIConfig, rerankConfig *RerankConfig, modelUsage *common.ModelUsage) (*RerankResponse, error) {
+ return nil, fmt.Errorf("%s, no such method", s.Name())
+}
+
+func (s *SoMarkModel) Decide(ctx context.Context, modelName *string, request *DecideRequest, apiConfig *APIConfig, modelUsage *common.ModelUsage) (*DecideResponse, error) {
+ return nil, fmt.Errorf("%s, no such method", s.Name())
+}
+
+func (s *SoMarkModel) TranscribeAudio(ctx context.Context, modelName *string, file *string, apiConfig *APIConfig, asrConfig *ASRConfig, modelUsage *common.ModelUsage) (*ASRResponse, error) {
+ return nil, fmt.Errorf("%s, no such method", s.Name())
+}
+
+func (s *SoMarkModel) TranscribeAudioWithSender(ctx context.Context, modelName *string, file *string, apiConfig *APIConfig, asrConfig *ASRConfig, modelUsage *common.ModelUsage, sender func(*string, *string) error) error {
+ return fmt.Errorf("%s, no such method", s.Name())
+}
+
+func (s *SoMarkModel) AudioSpeech(ctx context.Context, modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig, modelUsage *common.ModelUsage) (*TTSResponse, error) {
+ return nil, fmt.Errorf("%s, no such method", s.Name())
+}
+
+func (s *SoMarkModel) AudioSpeechWithSender(ctx context.Context, modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig, modelUsage *common.ModelUsage, sender func(*string, *string) error) error {
+ return fmt.Errorf("%s, no such method", s.Name())
+}
+
+type soMarkSubmitResponse struct {
+ Code int `json:"code"`
+ Message string `json:"message"`
+ Data struct {
+ TaskID string `json:"task_id"`
+ } `json:"data"`
+}
+
+type soMarkPollResponse struct {
+ Code int `json:"code"`
+ Message string `json:"message"`
+ Data struct {
+ Status string `json:"status"`
+ Result map[string]any `json:"result"`
+ } `json:"data"`
+}
+
+// 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.
+func soMarkFormFileName(content []byte) string {
+ switch {
+ case len(content) >= 4 && string(content[:4]) == "%PDF":
+ return "document.pdf"
+ case len(content) >= 8 && content[0] == 0x89 && content[1] == 'P' && content[2] == 'N' && content[3] == 'G':
+ return "image.png"
+ case len(content) >= 2 && content[0] == 0xFF && content[1] == 0xD8:
+ return "image.jpg"
+ default:
+ return "document.pdf"
+ }
+}
+
+// soMarkSubmit uploads the document and returns the async task id. The form
+// mirrors parser.SoMarkSubmit's wire format with the parser defaults for the
+// element/feature switches; the configurable variants stay on the parser
+// path, which reads the knowledge-base setup.
+func (s *SoMarkModel) soMarkSubmit(ctx context.Context, submitURL string, content []byte, apiKey string) (string, error) {
+ var body bytes.Buffer
+ writer := multipart.NewWriter(&body)
+ part, err := writer.CreateFormFile("file", soMarkFormFileName(content))
+ if err != nil {
+ return "", fmt.Errorf("SoMark create file: %w", err)
+ }
+ if _, err := part.Write(content); err != nil {
+ return "", fmt.Errorf("SoMark write file: %w", err)
+ }
+ _ = writer.WriteField("output_formats", "json")
+ elementFormats, _ := json.Marshal(map[string]any{
+ "image": "url",
+ "formula": "latex",
+ "table": "html",
+ "cs": "image",
+ })
+ featureConfig, _ := json.Marshal(map[string]any{
+ "enable_text_cross_page": false,
+ "enable_table_cross_page": false,
+ "enable_title_level_recognition": true,
+ "enable_inline_image": true,
+ "enable_table_image": true,
+ "enable_image_understanding": true,
+ "keep_header_footer": false,
+ })
+ _ = writer.WriteField("element_formats", string(elementFormats))
+ _ = writer.WriteField("feature_config", string(featureConfig))
+ if strings.TrimSpace(apiKey) != "" {
+ _ = writer.WriteField("api_key", apiKey)
+ }
+ if err = writer.Close(); err != nil {
+ return "", fmt.Errorf("SoMark finalize form: %w", err)
+ }
+ req, err := http.NewRequestWithContext(ctx, http.MethodPost, submitURL, &body)
+ if err != nil {
+ return "", fmt.Errorf("SoMark request: %w", err)
+ }
+ req.Header.Set("Content-Type", writer.FormDataContentType())
+ resp, err := s.baseModel.httpClient.Do(req)
+ if err != nil {
+ return "", fmt.Errorf("SoMark submit: %w", err)
+ }
+ defer resp.Body.Close()
+ raw, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return "", fmt.Errorf("SoMark read submit: %w", err)
+ }
+ if resp.StatusCode != http.StatusOK {
+ return "", fmt.Errorf("SoMark submit failed with status %d: %s", resp.StatusCode, soMarkErrorBody(raw))
+ }
+ var payload soMarkSubmitResponse
+ if err = json.Unmarshal(raw, &payload); err != nil {
+ 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)
+ }
+ if payload.Data.TaskID == "" {
+ return "", fmt.Errorf("SoMark submit returned no task_id")
+ }
+ return payload.Data.TaskID, nil
+}
+
+// soMarkPoll polls the async task until it reaches SUCCESS and returns the
+// result payload. Non-SUCCESS terminal statuses fail the task; anything else
+// keeps polling until the caller's deadline.
+func (s *SoMarkModel) soMarkPoll(ctx context.Context, pollURL, taskID, apiKey string) (map[string]any, error) {
+ pollInterval := 2 * time.Second
+ const pollMultiplier = 1.5
+ maxPollInterval := 15 * time.Second
+
+ for attempt := 1; ; attempt++ {
+ form := url.Values{"task_id": {taskID}}
+ if strings.TrimSpace(apiKey) != "" {
+ form.Set("api_key", apiKey)
+ }
+ req, err := http.NewRequestWithContext(ctx, http.MethodPost, pollURL, strings.NewReader(form.Encode()))
+ if err != nil {
+ return nil, fmt.Errorf("SoMark poll request: %w", err)
+ }
+ req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
+ resp, err := s.baseModel.httpClient.Do(req)
+ if err != nil {
+ return nil, fmt.Errorf("SoMark poll: %w", err)
+ }
+ raw, readErr := io.ReadAll(resp.Body)
+ resp.Body.Close()
+ if readErr != nil {
+ return nil, fmt.Errorf("SoMark read poll: %w", readErr)
+ }
+ if resp.StatusCode != http.StatusOK {
+ return nil, fmt.Errorf("SoMark poll failed with status %d: %s", resp.StatusCode, soMarkErrorBody(raw))
+ }
+ var payload soMarkPollResponse
+ if err = json.Unmarshal(raw, &payload); err != nil {
+ 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)
+ }
+ switch strings.ToUpper(strings.TrimSpace(payload.Data.Status)) {
+ case "SUCCESS":
+ if payload.Data.Result == nil {
+ return nil, fmt.Errorf("SoMark task %s SUCCESS without result", taskID)
+ }
+ 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)
+ }
+
+ common.Debug("somark poll: in progress",
+ zap.String("task_id", taskID),
+ zap.Int("attempt", attempt),
+ zap.String("status", payload.Data.Status),
+ zap.Duration("next_poll_in", pollInterval))
+
+ select {
+ case <-time.After(pollInterval):
+ case <-ctx.Done():
+ common.Error("somark poll: context done while waiting",
+ ctx.Err(),
+ zap.String("task_id", taskID),
+ zap.Int("attempt", attempt),
+ zap.String("last_status", payload.Data.Status))
+ return nil, ctx.Err()
+ }
+ pollInterval = min(time.Duration(float64(pollInterval)*pollMultiplier), maxPollInterval)
+ }
+}
+
+func soMarkErrorBody(raw []byte) string {
+ const maxErrorBodyBytes = 1024
+ if len(raw) > maxErrorBodyBytes {
+ return string(raw[:maxErrorBodyBytes]) + "..."
+ }
+ return string(raw)
+}
+
+// soMarkMarkdown flattens the parsed block structure into markdown: titles
+// become ATX headings at their recognized level, tables keep their HTML,
+// figures fall back to a placeholder caption, and everything else passes its
+// content through. Mirrors parser.soMarkBlockToItem's text mapping.
+func soMarkMarkdown(result map[string]any) string {
+ outputs, _ := result["outputs"].(map[string]any)
+ jsonPayload, _ := outputs["json"].(map[string]any)
+ pages, _ := jsonPayload["pages"].([]any)
+ var markdown strings.Builder
+ for _, pageRaw := range pages {
+ page, ok := pageRaw.(map[string]any)
+ if !ok {
+ continue
+ }
+ blocks, _ := page["blocks"].([]any)
+ for _, blockRaw := range blocks {
+ block, ok := blockRaw.(map[string]any)
+ if !ok {
+ continue
+ }
+ blockType := strings.ToLower(strings.TrimSpace(soMarkStringValue(block["type"])))
+ switch blockType {
+ case "cate", "cate_item", "blank":
+ continue
+ }
+ content := strings.TrimSpace(soMarkStringValue(block["content"]))
+ switch blockType {
+ case "title":
+ if content == "" {
+ continue
+ }
+ level := int(soMarkNumberValue(block["title_level"]))
+ if level < 1 || level > 6 {
+ level = 1
+ }
+ markdown.WriteString(strings.Repeat("#", level) + " " + content)
+ case "figure", "cs", "qrcode", "stamp":
+ if content == "" {
+ content = "[Image]"
+ }
+ markdown.WriteString(content)
+ default:
+ if content == "" {
+ continue
+ }
+ markdown.WriteString(content)
+ }
+ markdown.WriteString("\n\n")
+ }
+ }
+ return strings.TrimSpace(markdown.String())
+}
+
+func soMarkStringValue(value any) string {
+ text, _ := value.(string)
+ return text
+}
+
+func soMarkNumberValue(value any) float64 {
+ switch v := value.(type) {
+ case float64:
+ return v
+ case int:
+ return float64(v)
+ default:
+ return 0
+ }
+}
+
+func (s *SoMarkModel) OCRFile(ctx context.Context, modelName *string, content []byte, fileURL *string, apiConfig *APIConfig, ocrConfig *OCRConfig, modelUsage *common.ModelUsage) (*OCRFileResponse, error) {
+ if err := s.baseModel.APIConfigCheck(apiConfig); err != nil {
+ return nil, err
+ }
+ if len(content) == 0 {
+ // The SoMark API has no fetch-by-URL mode; the parser path uploads
+ // the file bytes directly.
+ return nil, fmt.Errorf("SoMark requires file content")
+ }
+
+ apiKey := ""
+ if apiConfig != nil && apiConfig.ApiKey != nil {
+ apiKey = *apiConfig.ApiKey
+ }
+
+ // One generous deadline bounds the whole operation (submit + poll), so
+ // the poll loop below cannot spin forever.
+ ctx, cancel := context.WithTimeout(ctx, longOpCallTimeout)
+ defer cancel()
+
+ submitURL, err := s.baseModel.EndpointURL(apiConfig, s.baseModel.URLSuffix.OCR)
+ if err != nil {
+ return nil, err
+ }
+ pollURL, err := s.baseModel.EndpointURL(apiConfig, s.baseModel.URLSuffix.Tasks)
+ if err != nil {
+ return nil, err
+ }
+
+ modelNameValue := ""
+ if modelName != nil {
+ modelNameValue = *modelName
+ }
+ common.Info("somark submit: sending task",
+ zap.String("driver", s.Name()),
+ zap.String("model", modelNameValue),
+ zap.Int("content_bytes", len(content)))
+
+ taskID, err := s.soMarkSubmit(ctx, submitURL, content, apiKey)
+ if err != nil {
+ common.Error("somark submit: request failed", err, zap.String("driver", s.Name()))
+ return nil, err
+ }
+ common.Info("somark submit: ok",
+ zap.String("driver", s.Name()),
+ zap.String("task_id", taskID))
+
+ result, err := s.soMarkPoll(ctx, pollURL, taskID, apiKey)
+ if err != nil {
+ common.Error("somark poll: failed", err, zap.String("task_id", taskID))
+ return nil, err
+ }
+
+ // A SUCCESS task with no usable blocks still proves connectivity, which
+ // is what the provider connection check keys on (verifyOCRModel accepts
+ // any non-error response), so an empty extraction is returned as-is
+ // rather than as an error.
+ text := soMarkMarkdown(result)
+ common.Info("somark result: parsed",
+ zap.String("task_id", taskID),
+ zap.Int("text_len", len(text)))
+ return &OCRFileResponse{Text: &text}, nil
+}
+
+func (s *SoMarkModel) ParseFile(ctx context.Context, modelName *string, content []byte, url *string, apiConfig *APIConfig, parseFileConfig *ParseFileConfig, modelUsage *common.ModelUsage) (*ParseFileResponse, error) {
+ return nil, fmt.Errorf("%s, no such method", s.Name())
+}
+
+func (s *SoMarkModel) ListModels(ctx context.Context, apiConfig *APIConfig) ([]ListModelResponse, error) {
+ return nil, fmt.Errorf("%s, no such method", s.Name())
+}
+
+func (s *SoMarkModel) Balance(ctx context.Context, apiConfig *APIConfig) (map[string]interface{}, error) {
+ return nil, fmt.Errorf("%s, no such method", s.Name())
+}
+
+func (s *SoMarkModel) CheckConnection(ctx context.Context, apiConfig *APIConfig) error {
+ return fmt.Errorf("%s, no such method", s.Name())
+}
+
+func (s *SoMarkModel) ListTasks(ctx context.Context, apiConfig *APIConfig) ([]ListTaskStatus, error) {
+ return nil, fmt.Errorf("%s, no such method", s.Name())
+}
+
+func (s *SoMarkModel) ShowTask(ctx context.Context, taskID string, apiConfig *APIConfig) (*TaskResponse, error) {
+ return nil, fmt.Errorf("%s, no such method", s.Name())
+}
diff --git a/internal/ingestion/component/pdf_vision_dispatch.go b/internal/ingestion/component/pdf_vision_dispatch.go
index 2e7d00bd9b..b9e48f17b8 100644
--- a/internal/ingestion/component/pdf_vision_dispatch.go
+++ b/internal/ingestion/component/pdf_vision_dispatch.go
@@ -177,7 +177,7 @@ func dispatchPDFBackend(ctx context.Context, db *gorm.DB, filename string, binar
// provider/selectors name PaddleOCR, or a bare tenant model UUID (in
// either parse_method or layout_recognizer) that resolves to a PaddleOCR
// OCR model. A bare UUID carries no provider spelling in the string, so it
- // is resolved first — mirroring Python's get_composite_model_name_by_id +
+ // is resolved first - mirroring Python's get_composite_model_name_by_id +
// normalize_layout_recognizer chain, which converts the raw model UUID
// into model@instance@provider before choosing the dispatch path.
isPaddleOCRMatch := strings.EqualFold(strings.TrimSpace(method), "paddleocr") ||
@@ -217,6 +217,40 @@ func dispatchPDFBackend(ctx context.Context, db *gorm.DB, filename string, binar
return res, true, nil
}
+ // SoMark dispatch: parse_method "somark", a layout_recognizer naming
+ // SoMark, or a bare tenant model UUID (in either parse_method or
+ // layout_recognizer) that resolves to a SoMark OCR model. A resolved
+ // tenant instance overrides the parser's setup/env SoMark configuration
+ // with the instance's base URL and API key and runs the same SoMark
+ // parser path; when the tenant has no SoMark instance the branch falls
+ // through so the setup/env-driven parser path (SOMARK_BASE_URL et al.)
+ // keeps working unchanged.
+ isSoMarkMatch := strings.EqualFold(strings.TrimSpace(method), "somark") ||
+ strings.HasPrefix(layoutLower, "somark") ||
+ strings.Contains(layoutLower, "@somark")
+ soMarkSelector := layout
+ if strings.TrimSpace(soMarkSelector) == "" {
+ soMarkSelector = method
+ }
+ isSoMarkByUUID := false
+ if !isSoMarkMatch && tenantID != "" && strings.TrimSpace(soMarkSelector) != "" &&
+ !parser.IsPDFParseMethod(soMarkSelector) {
+ isSoMarkByUUID = isSoMarkLayoutModelID(ctx, db, tenantID, soMarkSelector)
+ }
+ if isSoMarkMatch || isSoMarkByUUID {
+ if tenantID != "" {
+ dispatchModelID := ""
+ if isSoMarkByUUID || strings.Contains(soMarkSelector, "@") {
+ dispatchModelID = soMarkSelector
+ }
+ if res, handled, err := dispatchSoMarkPDF(ctx, db, filename, binary, tenantID, setup, dispatchModelID); handled {
+ return res, true, err
+ }
+ }
+ // No tenant SoMark instance: fall through to the setup/env-driven
+ // parser path.
+ }
+
modelID, useVision := resolvePDFVisionModelID(setup)
if !useVision {
return parser.ParseResult{}, false, nil
@@ -554,7 +588,7 @@ func dispatchMinerUPDF(
parseMethod := mineruAPIParseMethod(getStringOr(setup, "mineru_parse_method", ""))
// MinerU's language is an OCR engine parameter, not the caption language:
// mineru_lang wins, then the family lang, then the engine's own default.
- // That default used to arrive implicitly through defaultSetups() — which
+ // That default used to arrive implicitly through defaultSetups() - which
// also fed captions, so a knowledge base that never chose a language got
// Chinese captions (issue #20727). Keeping it here confines it to the engine.
lang := firstNonEmpty(getStringOr(setup, "mineru_lang", ""), getStringOr(setup, "lang", ""))
@@ -666,7 +700,7 @@ func appendMineruGeometryWarning(res *parser.ParseResult, err error) {
// dispatch. modelID is the raw selector value: a bare tenant model UUID (no
// "@") selects that exact model, a composite model@instance@provider name
// resolves through the provider-instance chain, and an empty value falls back
-// to the tenant's first MinerU OCR model — mirroring Python's by_mineru,
+// to the tenant's first MinerU OCR model - mirroring Python's by_mineru,
// which falls back to get_first_provider_model_name(tenant_id, "MinerU",
// LLMType.OCR) for the named "mineru" selector.
var resolveMinerUModelForDispatch = defaultResolveMinerUModelForDispatch
@@ -698,8 +732,8 @@ func defaultResolveMinerUModelForDispatch(ctx context.Context, db *gorm.DB, tena
// Known limitation: composite names such as "some-model@[email protected]"
// (an explicit local selection) are not recognized here. Any value containing
// "@" falls through to resolveTenantOCRModelByProvider("PaddleOCR"), which
-// returns the tenant's first active PaddleOCR OCR model — potentially the
-// local one — when both the local ("PaddleOCR.local") and the cloud
+// returns the tenant's first active PaddleOCR OCR model - potentially the
+// local one - when both the local ("PaddleOCR.local") and the cloud
// ("PaddleOCR") providers are configured. The Python path behaves the same
// way; if exact selection is required, pass the model's tenant-model UUID
// instead.
@@ -1609,8 +1643,8 @@ func isMinerUDriver(driver modelModule.ModelDriver) bool {
return false
}
-// isMinerULayoutModelID reports whether selector — a bare tenant model UUID
-// with no "model@instance@provider" composite hint — resolves to an active
+// isMinerULayoutModelID reports whether selector - a bare tenant model UUID
+// with no "model@instance@provider" composite hint - resolves to an active
// OCR model driven by a MinerU provider (remote mineru.net or local mineru).
// The raw UUID carries no provider spelling, so it must be resolved before
// the dispatch path is chosen instead of falling through to the image2text
@@ -1642,8 +1676,8 @@ func isPaddleOCRDriver(driver modelModule.ModelDriver) bool {
return false
}
-// isPaddleOCRLayoutModelID reports whether layout — a bare tenant model UUID
-// with no "model@instance@provider" composite hint — resolves to an active
+// isPaddleOCRLayoutModelID reports whether layout - a bare tenant model UUID
+// with no "model@instance@provider" composite hint - resolves to an active
// OCR model driven by a PaddleOCR provider (cloud "PaddleOCR" or local
// "PaddleOCR.local"). The web UI stores the tenant model UUID directly in
// layout_recognizer, so the raw value carries no provider spelling; this
@@ -1665,3 +1699,102 @@ func defaultIsPaddleOCRLayoutModelID(ctx context.Context, db *gorm.DB, tenantID,
}
return isPaddleOCRDriver(driver)
}
+
+// resolveSoMarkModelForDispatch resolves the OCR model used by the SoMark PDF
+// dispatch. modelID is the raw selector value: a bare tenant model UUID (no
+// "@") selects that exact model, a composite model@instance@provider name
+// resolves through the provider-instance chain, and an empty value falls back
+// to the tenant's first SoMark OCR model.
+var resolveSoMarkModelForDispatch = defaultResolveSoMarkModelForDispatch
+
+func defaultResolveSoMarkModelForDispatch(ctx context.Context, db *gorm.DB, tenantID, modelID string) (modelModule.ModelDriver, string, *modelModule.APIConfig, error) {
+ modelID = strings.TrimSpace(modelID)
+ if modelID != "" && !strings.Contains(modelID, "@") {
+ resolved, err := resolveModelConfigByID(ctx, db, tenantID, entity.ModelTypeOCR, modelID)
+ if err != nil {
+ return nil, "", nil, err
+ }
+ return resolved.driver, resolved.model.ModelName, resolved.apiConfig, nil
+ }
+ if modelID != "" {
+ driver, modelName, apiConfig, _, err := resolveModelConfig(ctx, db, tenantID, entity.ModelTypeOCR, modelID)
+ return driver, modelName, apiConfig, err
+ }
+ driver, modelName, apiConfig, _, err := resolveTenantOCRModelByProvider(ctx, db, tenantID, "SoMark")
+ return driver, modelName, apiConfig, err
+}
+
+// isSoMarkLayoutModelID reports whether selector - a bare tenant model UUID
+// with no "model@instance@provider" composite hint - resolves to an active
+// OCR model driven by the SoMark provider. Mirrors the PaddleOCR/MinerU
+// probes: the raw UUID carries no provider spelling, so it must be resolved
+// before the dispatch path is chosen instead of falling through to the
+// image2text VLM path.
+var isSoMarkLayoutModelID = defaultIsSoMarkLayoutModelID
+
+func defaultIsSoMarkLayoutModelID(ctx context.Context, db *gorm.DB, tenantID, selector string) bool {
+ selector = strings.TrimSpace(selector)
+ if db == nil {
+ return false
+ }
+ if selector == "" || strings.Contains(selector, "@") {
+ return false
+ }
+ driver, _, _, err := defaultResolveSoMarkModelForDispatch(ctx, db, tenantID, selector)
+ if err != nil {
+ return false
+ }
+ return strings.EqualFold(driver.Name(), "somark")
+}
+
+// dispatchSoMarkPDF runs the SoMark parser path with the tenant model
+// provider instance's endpoint and key overriding the parser setup/env
+// configuration, so instance-driven parsing yields the same structured
+// result (titles, tables, figures with layout) as the env-driven path.
+// handled reports whether the dispatch consumed the request: resolution
+// failures for an explicit model selector are handled errors; when no tenant
+// model was selected, the caller falls through to the setup/env-driven path.
+func dispatchSoMarkPDF(
+ ctx context.Context,
+ db *gorm.DB,
+ filename string,
+ binary []byte,
+ tenantID string,
+ setup schema.ParserSetup,
+ modelID string,
+) (parser.ParseResult, bool, error) {
+ driver, _, apiConfig, err := resolveSoMarkModelForDispatch(ctx, db, tenantID, modelID)
+ if err != nil {
+ if strings.TrimSpace(modelID) != "" {
+ return parser.ParseResult{}, true, fmt.Errorf("parser: resolve selected SoMark model %q: %w", modelID, err)
+ }
+ // No tenant SoMark instance (or unresolvable selector): let the
+ // setup/env-driven parser path handle the request.
+ return parser.ParseResult{}, false, nil
+ }
+ if !strings.EqualFold(driver.Name(), "somark") {
+ return parser.ParseResult{}, true, fmt.Errorf(
+ "parser: SoMark requires a SoMark OCR model; found %q. Please add a SoMark OCR model to your tenant", driver.Name())
+ }
+
+ p := parser.NewPDFParser()
+ p.ConfigureFromSetup(setup)
+ p.ParseMethod = "somark"
+ if somarkDriver, ok := driver.(interface {
+ GetBaseURL(*modelModule.APIConfig) (string, error)
+ }); ok {
+ if baseURL, err := somarkDriver.GetBaseURL(apiConfig); err == nil && strings.TrimSpace(baseURL) != "" {
+ p.SoMarkBaseURL = strings.TrimRight(strings.TrimSpace(baseURL), "/")
+ }
+ }
+ if apiConfig != nil {
+ if apiConfig.ApiKey != nil && strings.TrimSpace(*apiConfig.ApiKey) != "" {
+ p.SoMarkAPIKey = strings.TrimSpace(*apiConfig.ApiKey)
+ }
+ }
+ res := p.ParseWithResult(ctx, filename, binary)
+ if res.Err != nil {
+ return parser.ParseResult{}, true, res.Err
+ }
+ return res, true, nil
+}
diff --git a/internal/parser/parser/pdf_parser_somark.go b/internal/parser/parser/pdf_parser_somark.go
index 7c09323685..b64edf80b1 100644
--- a/internal/parser/parser/pdf_parser_somark.go
+++ b/internal/parser/parser/pdf_parser_somark.go
@@ -11,6 +11,7 @@ import (
"net/http"
"net/url"
"strings"
+ "time"
pdfreader "github.com/ledongthuc/pdf"
@@ -18,6 +19,9 @@ import (
"ragflow/internal/parser/media"
)
+const soMarkPollTimeout = 30 * time.Minute
+const soMarkPollInterval = 2 * time.Second
+
// parsePDFWithSoMark submits a PDF and converts the task result into parser
// output. Explicit tenant configuration never borrows a global endpoint or key.
func parsePDFWithSoMark(ctx context.Context, filename string, data []byte, parser *PDFParser) ParseResult {
@@ -125,44 +129,61 @@ func soMarkSubmit(ctx context.Context, baseURL, filename string, data []byte, pa
}
func soMarkPoll(ctx context.Context, baseURL, taskID, apiKey string) (map[string]any, error) {
- form := url.Values{"task_id": {taskID}}
- if apiKey != "" {
- form.Set("api_key", apiKey)
- }
- req, err := http.NewRequestWithContext(ctx, http.MethodPost, baseURL+"/parse/async_check", strings.NewReader(form.Encode()))
- if err != nil {
- return nil, fmt.Errorf("parser: SoMark poll request: %w", err)
- }
- req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
- resp, err := common.GetSSRFHTTPClient().Do(req)
- if err != nil {
- return nil, fmt.Errorf("parser: SoMark poll: %w", err)
- }
- defer resp.Body.Close()
- raw, err := io.ReadAll(resp.Body)
- if err != nil {
- return nil, fmt.Errorf("parser: SoMark read poll: %w", err)
- }
- var payload struct {
- Code int `json:"code"`
- Message string `json:"message"`
- Data map[string]any `json:"data"`
- }
- if err = json.Unmarshal(raw, &payload); err != nil {
- return nil, fmt.Errorf("parser: SoMark decode poll: %w", err)
- }
- if payload.Code != 0 {
- return nil, fmt.Errorf("parser: SoMark poll business error code=%d message=%s", payload.Code, payload.Message)
- }
- status, _ := payload.Data["status"].(string)
- if status != "SUCCESS" {
- return nil, fmt.Errorf("parser: SoMark task %s status=%s", taskID, status)
- }
- result, _ := payload.Data["result"].(map[string]any)
- if result == nil {
- return nil, fmt.Errorf("parser: SoMark SUCCESS without result")
+ pollCtx, cancel := context.WithTimeout(ctx, soMarkPollTimeout)
+ defer cancel()
+ ticker := time.NewTicker(soMarkPollInterval)
+ defer ticker.Stop()
+ for {
+ form := url.Values{"task_id": {taskID}}
+ if apiKey != "" {
+ form.Set("api_key", apiKey)
+ }
+ req, err := http.NewRequestWithContext(pollCtx, http.MethodPost, baseURL+"/parse/async_check", strings.NewReader(form.Encode()))
+ if err != nil {
+ return nil, fmt.Errorf("parser: SoMark poll request: %w", err)
+ }
+ req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
+ resp, err := common.GetSSRFHTTPClient().Do(req)
+ if err != nil {
+ if pollCtx.Err() != nil {
+ return nil, fmt.Errorf("parser: timed out waiting for SoMark task %s: %w", taskID, pollCtx.Err())
+ }
+ return nil, fmt.Errorf("parser: SoMark poll: %w", err)
+ }
+ raw, readErr := io.ReadAll(resp.Body)
+ _ = resp.Body.Close()
+ if readErr != nil {
+ return nil, fmt.Errorf("parser: SoMark read poll: %w", readErr)
+ }
+ var payload struct {
+ Code int `json:"code"`
+ Message string `json:"message"`
+ Data map[string]any `json:"data"`
+ }
+ if err = json.Unmarshal(raw, &payload); err != nil {
+ return nil, fmt.Errorf("parser: SoMark decode poll: %w", err)
+ }
+ if payload.Code != 0 {
+ return nil, fmt.Errorf("parser: SoMark poll business error code=%d message=%s", payload.Code, payload.Message)
+ }
+ status, _ := payload.Data["status"].(string)
+ switch strings.ToUpper(status) {
+ case "SUCCESS":
+ result, _ := payload.Data["result"].(map[string]any)
+ if result == nil {
+ return nil, fmt.Errorf("parser: SoMark SUCCESS without result")
+ }
+ return result, nil
+ case "QUEUING", "PENDING", "RUNNING", "PROCESSING":
+ select {
+ case <-pollCtx.Done():
+ return nil, fmt.Errorf("parser: timed out waiting for SoMark task %s; last status=%s: %w", taskID, status, pollCtx.Err())
+ case <-ticker.C:
+ }
+ default:
+ return nil, fmt.Errorf("parser: SoMark task %s status=%s", taskID, status)
+ }
}
- return result, nil
}
func soMarkItems(ctx context.Context, result map[string]any, keepHeaderFooter bool, pageSizes [][2]float64) ([]map[string]any, int, []string) {
diff --git a/internal/parser/parser/pdf_parser_somark_test.go b/internal/parser/parser/pdf_parser_somark_test.go
index 7588e88f45..5c96601e40 100644
--- a/internal/parser/parser/pdf_parser_somark_test.go
+++ b/internal/parser/parser/pdf_parser_somark_test.go
@@ -22,6 +22,7 @@ func TestPDFParser_ParseWithResult_SoMarkJSONIntegration(t *testing.T) {
withSSRFBypass(t)
var submitSeen bool
var pollSeen bool
+ pollCount := 0
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/parse/async":
@@ -55,11 +56,16 @@ func TestPDFParser_ParseWithResult_SoMarkJSONIntegration(t *testing.T) {
_, _ = w.Write([]byte(`{"code":0,"data":{"task_id":"task-1"}}`))
case "/parse/async_check":
pollSeen = true
+ pollCount++
body, _ := io.ReadAll(r.Body)
if !strings.Contains(string(body), "task_id=task-1") {
t.Errorf("poll body = %q, want task_id", string(body))
return
}
+ if pollCount == 1 {
+ _, _ = w.Write([]byte(`{"code":0,"data":{"status":"QUEUING"}}`))
+ return
+ }
_, _ = w.Write([]byte(`{"code":0,"data":{"status":"SUCCESS","result":{"outputs":{"json":{"pages":[{"blocks":[{"type":"title","content":"SoMark Title","title_level":2},{"type":"figure","content":"Figure caption"},{"type":"table","content":"<table><tr><td>x</td></tr></table>"}]}]}}}}}`))
default:
http.NotFound(w, r)
@@ -78,8 +84,8 @@ func TestPDFParser_ParseWithResult_SoMarkJSONIntegration(t *testing.T) {
if res.Err != nil {
t.Fatalf("ParseWithResult: %v", res.Err)
}
- if !submitSeen || !pollSeen {
- t.Fatalf("submit/poll seen = %v/%v, want true/true", submitSeen, pollSeen)
+ if !submitSeen || !pollSeen || pollCount < 2 {
+ t.Fatalf("submit/poll seen = %v/%v (%d checks), want submit and at least two poll checks", submitSeen, pollSeen, pollCount)
}
if len(res.JSON) < 3 {
t.Fatalf("JSON len = %d, want >=3", len(res.JSON))