Skip to content

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

feat(go): complete the Mistral /v1/ocr client and list mistral-ocr-latest as an ocr model (#20531)

Xavierandoea0d7f21ae44
diff --git a/conf/models/mistral.json b/conf/models/mistral.json
index 2119046046..522b011d6c 100644
--- a/conf/models/mistral.json
+++ b/conf/models/mistral.json
@@ -9,7 +9,8 @@
"embedding": "v1/embeddings",
"asr": "v1/audio/transcriptions",
"tts": "v1/audio/speech",
- "ocr": "v1/ocr"
+ "ocr": "v1/ocr",
+ "files": "v1/files"
},
"class": "mistral",
"models": [
@@ -188,6 +189,13 @@
"model_types": [
"ocr"
]
+ },
+ {
+ "name": "mistral-ocr-latest",
+ "max_tokens": 8192,
+ "model_types": [
+ "ocr"
+ ]
}
]
}
diff --git a/internal/dao/model_context.go b/internal/dao/model_context.go
index 10eb09c075..45be63c6dc 100644
--- a/internal/dao/model_context.go
+++ b/internal/dao/model_context.go
@@ -25,6 +25,7 @@ import (
"gorm.io/gorm"
"ragflow/internal/entity"
+ "ragflow/internal/entity/models"
)
// ResolveModelContentLength returns the chat model's effective context window
@@ -66,21 +67,21 @@ func ResolveModelContentLength(ctx context.Context, db *gorm.DB, tenantID, model
// 2a. Active tenant row → its provider row → catalog context_length.
if obj != nil && obj.Status == "active" {
if provider, err := NewTenantModelProviderDAO().GetByID(ctx, db, obj.ProviderID); err == nil && provider != nil {
- if mdl, err := GetModelProviderManager().GetModelByName(provider.ProviderName, obj.ModelName); err == nil && mdl.ContextLength != nil {
+ if mdl, err := GetModelProviderManager().GetModelByName(models.CatalogProviderName(provider.ProviderName), obj.ModelName); err == nil && mdl.ContextLength != nil {
return *mdl.ContextLength
}
}
}
// 2b. Composite reference with no tenant row → catalog by reference parts.
if composite {
- if mdl, err := GetModelProviderManager().GetModelByName(providerName, pureName); err == nil && mdl.ContextLength != nil {
+ if mdl, err := GetModelProviderManager().GetModelByName(models.CatalogProviderName(providerName), pureName); err == nil && mdl.ContextLength != nil {
return *mdl.ContextLength
}
}
// 2c. Resolved driver + bare model name: fallback when modelRef is a
// tenant id that could not be resolved without a database.
if driver != "" && modelName != "" {
- if mdl, err := GetModelProviderManager().GetModelByName(driver, modelName); err == nil && mdl.ContextLength != nil {
+ if mdl, err := GetModelProviderManager().GetModelByName(models.CatalogProviderName(driver), modelName); err == nil && mdl.ContextLength != nil {
return *mdl.ContextLength
}
}
diff --git a/internal/dao/model_context_test.go b/internal/dao/model_context_test.go
index 00ca628d81..06904e9aa7 100644
--- a/internal/dao/model_context_test.go
+++ b/internal/dao/model_context_test.go
@@ -327,3 +327,25 @@ func openModelContextTestDB(t *testing.T) *gorm.DB {
}
return db
}
+
+// TestResolveModelContentLength_StoredMistralOCRProvider reads the Mistral
+// catalog for models stored under the "Mistral OCR" provider name: through the
+// tenant row, through a composite reference without a row, and through the
+// driver fallback.
+func TestResolveModelContentLength_StoredMistralOCRProvider(t *testing.T) {
+ const want = 262144 // mistral-large-latest context_length in conf/models/mistral.json
+
+ if got := ResolveModelContentLength(t.Context(), nil, "", "mistral-large-latest@default@Mistral OCR", "", ""); got != want {
+ t.Fatalf("composite without tenant row = %d, want %d", got, want)
+ }
+ if got := ResolveModelContentLength(t.Context(), nil, "", "", "Mistral OCR", "mistral-large-latest"); got != want {
+ t.Fatalf("driver fallback = %d, want %d", got, want)
+ }
+
+ db := openModelContextTestDB(t)
+ pushDB(t, db)
+ seedChatModel(t, db, "provider-mistral-ocr", "Mistral OCR", "mistral-large-latest", "")
+ if got := ResolveModelContentLength(t.Context(), db, "tenant-1", "0123456789abcdef0123456789abcdef", "", ""); got != want {
+ t.Fatalf("tenant model id = %d, want %d", got, want)
+ }
+}
diff --git a/internal/entity/models/mistral.go b/internal/entity/models/mistral.go
index 2ba907c62e..7a2724f06c 100644
--- a/internal/entity/models/mistral.go
+++ b/internal/entity/models/mistral.go
@@ -30,7 +30,6 @@ import (
"ragflow/internal/common"
"strconv"
"strings"
- "time"
)
// normalizeMistralStructuredContent rewrites a Mistral magistral response
@@ -572,93 +571,6 @@ func (m *MistralModel) AudioSpeechWithSender(ctx context.Context, modelName *str
return fmt.Errorf("%s, no such method", m.Name())
}
-// OCRFile OCR file
-func (m *MistralModel) OCRFile(ctx context.Context, modelName *string, content []byte, urls *string, apiConfig *APIConfig, ocrConfig *OCRConfig, modelUsage *common.ModelUsage) (*OCRFileResponse, error) {
- if err := m.baseModel.APIConfigCheck(apiConfig); err != nil {
- return nil, err
- }
-
- if (urls == nil || *urls == "") && (content == nil || len(content) == 0) {
- return nil, fmt.Errorf("file url or content is required")
- }
-
- url, err := m.baseModel.EndpointURL(apiConfig, m.baseModel.URLSuffix.OCR)
- if err != nil {
- return nil, err
- }
-
- var docURL string
- if urls != nil && *urls != "" {
- docURL = *urls
- } else {
- mimeType := http.DetectContentType(content)
- base64Str := base64.StdEncoding.EncodeToString(content)
- docURL = fmt.Sprintf("data:%s;base64,%s", mimeType, base64Str)
- }
-
- reqData := map[string]interface{}{
- "model": *modelName,
- "document": map[string]interface{}{
- "type": "document_url",
- "document_url": docURL,
- },
- }
-
- jsonData, err := json.Marshal(reqData)
- if err != nil {
- return nil, fmt.Errorf("failed to marshal json payload: %w", err)
- }
-
- ctx, cancel := context.WithTimeout(ctx, 120*time.Second)
- defer cancel()
-
- req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))
- if err != nil {
- return nil, fmt.Errorf("failed to create request: %w", err)
- }
-
- req.Header.Set("Content-Type", "application/json")
- req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *apiConfig.ApiKey))
-
- resp, err := m.baseModel.httpClient.Do(req)
- if err != nil {
- return nil, fmt.Errorf("failed to send request: %w", err)
- }
- defer resp.Body.Close()
-
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- return nil, fmt.Errorf("failed to read response: %w", err)
- }
-
- if resp.StatusCode != http.StatusOK {
- return nil, fmt.Errorf("Mistral OCR API error: %s, body: %s", resp.Status, string(body))
- }
-
- var mistralResp struct {
- Pages []struct {
- Index int `json:"index"`
- Markdown string `json:"markdown"`
- } `json:"pages"`
- }
-
- if err = json.Unmarshal(body, &mistralResp); err != nil {
- return nil, fmt.Errorf("failed to parse response json: %w", err)
- }
-
- var fullMarkdown strings.Builder
- for _, page := range mistralResp.Pages {
- fullMarkdown.WriteString(page.Markdown)
- fullMarkdown.WriteString("\n\n")
- }
-
- resultText := strings.TrimSpace(fullMarkdown.String())
-
- return &OCRFileResponse{
- Text: &resultText,
- }, nil
-}
-
func (m *MistralModel) 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", m.Name())
}
diff --git a/internal/entity/models/mistral_ocr.go b/internal/entity/models/mistral_ocr.go
new file mode 100644
index 0000000000..866f91cb44
--- /dev/null
+++ b/internal/entity/models/mistral_ocr.go
@@ -0,0 +1,425 @@
+//
+// 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/base64"
+ "encoding/json"
+ "fmt"
+ "mime"
+ "mime/multipart"
+ "net/http"
+ neturl "net/url"
+ "path"
+ "regexp"
+ "sort"
+ "strings"
+ "time"
+
+ "ragflow/internal/common"
+
+ "go.uber.org/zap"
+)
+
+// Mistral OCR is the dedicated POST /v1/ocr document endpoint, not a vision
+// chat model. The "Mistral" provider's ocr-typed models (mistral-ocr-*) reach
+// it through MistralModel.OCRFile.
+const (
+ // mistralOCRInlineMaxBytes is the largest document sent inline as a
+ // base64 data URL; larger documents go through the Files API.
+ mistralOCRInlineMaxBytes = 20 << 20
+ // mistralOCRMaxPages is the API's per-document page limit.
+ mistralOCRMaxPages = 1000
+ // mistralOCRMaxResponseBytes bounds a full-document response. Requests
+ // set include_image_base64=false, so a page carries only its Markdown,
+ // tables and block layout: about 8 KB per text page on the live API,
+ // which leaves 8x headroom at the 1000-page limit.
+ mistralOCRMaxResponseBytes int64 = 64 << 20
+ // mistralOCRSignedURLExpiryHours is how long the uploaded document's
+ // signed URL stays valid.
+ mistralOCRSignedURLExpiryHours = 24
+ mistralOCRCleanupTimeout = 30 * time.Second
+ // mistralOCRTableFormat makes the API return extracted tables as HTML,
+ // which keeps merged cells that Markdown tables cannot express.
+ mistralOCRTableFormat = "html"
+)
+
+// mistralOCRLinkPattern matches the Markdown links Mistral OCR leaves in a
+// page's Markdown where it extracted a table ([tbl-0.html](tbl-0.html)) or
+// an image (![img-0.jpeg](img-0.jpeg)).
+var mistralOCRLinkPattern = regexp.MustCompile(`(!?)\[[^\]]*\]\(([^)\s]+)\)`)
+
+var mistralOCRBlankLines = regexp.MustCompile(`\n{3,}`)
+
+// mistralOCRPageIndexes converts 1-indexed inclusive page ranges into the
+// zero-based page selector /v1/ocr expects. The selector is bounded by the
+// API's page limit; a selection lying entirely past it is an error rather
+// than a silent whole-document request. Callers that know the document's
+// page count clamp the ranges to it (and omit a whole-document selection),
+// since the API silently skips indexes past the last page.
+func mistralOCRPageIndexes(ocrConfig *OCRConfig) ([]int, error) {
+ if ocrConfig == nil || len(ocrConfig.Pages) == 0 {
+ return nil, nil
+ }
+ seen := make(map[int]struct{})
+ indexes := make([]int, 0)
+ for _, pageRange := range ocrConfig.Pages {
+ if len(pageRange) != 2 {
+ continue
+ }
+ from, to := max(pageRange[0], 1), min(pageRange[1], mistralOCRMaxPages)
+ for page := from; page <= to; page++ {
+ if _, ok := seen[page-1]; ok {
+ continue
+ }
+ seen[page-1] = struct{}{}
+ indexes = append(indexes, page-1)
+ }
+ }
+ if len(indexes) == 0 {
+ return nil, fmt.Errorf("Mistral OCR: page selection %v is outside the first %d pages", ocrConfig.Pages, mistralOCRMaxPages)
+ }
+ sort.Ints(indexes)
+ return indexes, nil
+}
+
+// OCRFile runs Mistral OCR on content (a PDF or image) or, when content is
+// empty, on the document at the http(s) URL. It returns the document as
+// Markdown: header/footer blocks are dropped, extracted tables are inlined as
+// HTML, and image placeholders are removed.
+func (m *MistralModel) OCRFile(ctx context.Context, modelName *string, content []byte, url *string, apiConfig *APIConfig, ocrConfig *OCRConfig, modelUsage *common.ModelUsage) (*OCRFileResponse, error) {
+ apiConfig = mistralOCRCredentials(apiConfig)
+ if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" {
+ return nil, fmt.Errorf("api key is required")
+ }
+ if modelName == nil || strings.TrimSpace(*modelName) == "" {
+ return nil, fmt.Errorf("model name is required")
+ }
+ remoteURL := ""
+ if url != nil {
+ remoteURL = strings.TrimSpace(*url)
+ }
+ if len(content) == 0 && !strings.HasPrefix(remoteURL, "http://") && !strings.HasPrefix(remoteURL, "https://") {
+ return nil, fmt.Errorf("file url or content is required")
+ }
+ pages, err := mistralOCRPageIndexes(ocrConfig)
+ if err != nil {
+ return nil, err
+ }
+
+ baseURL, err := m.baseModel.GetBaseURL(apiConfig)
+ if err != nil {
+ return nil, err
+ }
+ auth := &APIConfig{ApiKey: apiConfig.ApiKey}
+
+ ctx, cancel := context.WithTimeout(ctx, longOpCallTimeout)
+ defer cancel()
+
+ var document map[string]any
+ switch {
+ case len(content) == 0 && mistralOCRIsImageURL(remoteURL):
+ document = map[string]any{"type": "image_url", "image_url": remoteURL}
+ case len(content) == 0:
+ document = map[string]any{"type": "document_url", "document_url": remoteURL}
+ case len(content) > mistralOCRInlineMaxBytes:
+ signedURL, cleanup, err := m.mistralOCRUpload(ctx, baseURL, auth, content)
+ if err != nil {
+ return nil, err
+ }
+ defer cleanup()
+ document = map[string]any{"type": "document_url", "document_url": signedURL}
+ default:
+ mimeType := mistralOCRMimeType(content)
+ dataURL := fmt.Sprintf("data:%s;base64,%s", mimeType, base64.StdEncoding.EncodeToString(content))
+ if strings.HasPrefix(mimeType, "image/") {
+ document = map[string]any{"type": "image_url", "image_url": dataURL}
+ } else {
+ document = map[string]any{"type": "document_url", "document_url": dataURL}
+ }
+ }
+
+ payload := map[string]any{
+ "model": strings.TrimSpace(*modelName),
+ "document": document,
+ "include_blocks": true,
+ "include_image_base64": false,
+ "table_format": mistralOCRTableFormat,
+ }
+ if len(pages) > 0 {
+ payload["pages"] = pages
+ }
+
+ req, err := m.baseModel.newJSONPostRequest(ctx, joinURL(baseURL, m.baseModel.URLSuffix.OCR), auth, payload)
+ if err != nil {
+ return nil, err
+ }
+ body, err := m.mistralOCRSend(req, mistralOCRMaxResponseBytes)
+ if err != nil {
+ return nil, fmt.Errorf("Mistral OCR: %w", err)
+ }
+
+ var result mistralOCRResponse
+ if err = json.Unmarshal(body, &result); err != nil {
+ return nil, fmt.Errorf("Mistral OCR: failed to parse response: %w", err)
+ }
+ text := result.markdown()
+ return &OCRFileResponse{Text: &text}, nil
+}
+
+// mistralOCRCredentials returns apiConfig with the bare Mistral key. Instances
+// created by Python's "Mistral OCR" factory store a JSON api_key:
+// {"MISTRAL_OCR_API_KEY", "MISTRAL_OCR_BASE_URL", ...} when provisioned from
+// the environment, or {"api_key", "mistral_ocr_base_url", ...} from its form.
+// The key is read from it, and its base URL is used when the instance has none.
+func mistralOCRCredentials(apiConfig *APIConfig) *APIConfig {
+ if apiConfig == nil || apiConfig.ApiKey == nil {
+ return apiConfig
+ }
+ resolved := *apiConfig
+ raw := strings.TrimSpace(*apiConfig.ApiKey)
+ if providerJSONConfigMap(raw) == nil {
+ resolved.ApiKey = &raw
+ return &resolved
+ }
+ apiKey := ProviderJSONConfigValue(raw, "api_key", "MISTRAL_OCR_API_KEY")
+ resolved.ApiKey = &apiKey
+ if resolved.BaseURL == nil || strings.TrimSpace(*resolved.BaseURL) == "" {
+ if baseURL := ProviderJSONConfigValue(raw, "mistral_ocr_base_url", "MISTRAL_OCR_BASE_URL"); baseURL != "" {
+ resolved.BaseURL = &baseURL
+ }
+ }
+ return &resolved
+}
+
+// mistralOCRPDFHeaderWindow is how far into the file a PDF header may start;
+// PDF readers accept leading bytes before %PDF- within the first kilobyte.
+const mistralOCRPDFHeaderWindow = 1024
+
+// mistralOCRMimeType returns the bare media type of content for the OCR data
+// URL. Image signatures are trusted first; otherwise PDFs are recognised even
+// when bytes precede the header, which content sniffing alone reports as
+// text/plain.
+func mistralOCRMimeType(content []byte) string {
+ mediaType, _, err := mime.ParseMediaType(http.DetectContentType(content))
+ if err == nil && strings.HasPrefix(mediaType, "image/") {
+ return mediaType
+ }
+ if bytes.Contains(content[:min(len(content), mistralOCRPDFHeaderWindow)], []byte("%PDF-")) {
+ return "application/pdf"
+ }
+ if err != nil {
+ return "application/octet-stream"
+ }
+ return mediaType
+}
+
+// mistralOCRImageExtensions are common image formats, sent to /v1/ocr as
+// image_url; anything else is sent as document_url.
+var mistralOCRImageExtensions = map[string]bool{
+ ".png": true, ".jpg": true, ".jpeg": true, ".avif": true, ".webp": true,
+ ".gif": true, ".bmp": true, ".tif": true, ".tiff": true,
+}
+
+// mistralOCRIsImageURL reports whether the remote file's path names an image.
+func mistralOCRIsImageURL(remoteURL string) bool {
+ parsed, err := neturl.Parse(remoteURL)
+ if err != nil {
+ return false
+ }
+ return mistralOCRImageExtensions[strings.ToLower(path.Ext(parsed.Path))]
+}
+
+// mistralOCRSend performs req and returns the response body of a 2xx reply.
+func (m *MistralModel) mistralOCRSend(req *http.Request, maxBytes int64) ([]byte, error) {
+ resp, err := m.baseModel.httpClient.Do(req)
+ if err != nil {
+ return nil, fmt.Errorf("failed to send request: %w", err)
+ }
+ defer resp.Body.Close()
+ if resp.StatusCode < 200 || resp.StatusCode > 299 {
+ body, err := readModelErrorBody(resp.Body)
+ if err != nil {
+ return nil, fmt.Errorf("API request failed with status %d; failed to read error response: %w", resp.StatusCode, err)
+ }
+ return nil, &APIStatusError{Status: resp.StatusCode, Body: string(body)}
+ }
+ body, err := readModelResponseBodyLimited(resp.Body, maxBytes)
+ if err != nil {
+ return nil, fmt.Errorf("failed to read response: %w", err)
+ }
+ return body, nil
+}
+
+// mistralOCRUpload stores content through the Files API (purpose "ocr") and
+// returns a signed URL for it plus a cleanup that deletes the upload. The
+// cleanup runs even after ctx is done so no document is left on Mistral.
+func (m *MistralModel) mistralOCRUpload(ctx context.Context, baseURL string, auth *APIConfig, content []byte) (string, func(), error) {
+ if m.baseModel.URLSuffix.Files == "" {
+ return "", nil, fmt.Errorf("Mistral OCR: document exceeds %d bytes and the provider has no files endpoint", mistralOCRInlineMaxBytes)
+ }
+ filesURL := joinURL(baseURL, m.baseModel.URLSuffix.Files)
+
+ filename := "document"
+ if extensions, _ := mime.ExtensionsByType(mistralOCRMimeType(content)); len(extensions) > 0 {
+ filename += extensions[0]
+ }
+ var form bytes.Buffer
+ writer := multipart.NewWriter(&form)
+ _ = writer.WriteField("purpose", "ocr")
+ part, err := writer.CreateFormFile("file", filename)
+ if err != nil {
+ return "", nil, fmt.Errorf("Mistral OCR upload: %w", err)
+ }
+ if _, err = part.Write(content); err != nil {
+ return "", nil, fmt.Errorf("Mistral OCR upload: %w", err)
+ }
+ if err = writer.Close(); err != nil {
+ return "", nil, fmt.Errorf("Mistral OCR upload: %w", err)
+ }
+ req, err := http.NewRequestWithContext(ctx, http.MethodPost, filesURL, &form)
+ if err != nil {
+ return "", nil, fmt.Errorf("Mistral OCR upload: %w", err)
+ }
+ req.Header.Set("Content-Type", writer.FormDataContentType())
+ req.Header.Set("Authorization", BearerAuth(auth))
+ body, err := m.mistralOCRSend(req, maxModelResponseBodyBytes)
+ if err != nil {
+ return "", nil, fmt.Errorf("Mistral OCR upload: %w", err)
+ }
+ var uploaded struct {
+ ID string `json:"id"`
+ }
+ if err = json.Unmarshal(body, &uploaded); err != nil || uploaded.ID == "" {
+ return "", nil, fmt.Errorf("Mistral OCR upload: response carries no file id: %s", string(body))
+ }
+
+ fileURL := filesURL + "/" + uploaded.ID
+ cleanup := func() {
+ deleteCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), mistralOCRCleanupTimeout)
+ defer cancel()
+ req, err := http.NewRequestWithContext(deleteCtx, http.MethodDelete, fileURL, nil)
+ if err == nil {
+ req.Header.Set("Authorization", BearerAuth(auth))
+ _, err = m.mistralOCRSend(req, maxModelResponseBodyBytes)
+ }
+ if err != nil {
+ common.Warn("Mistral OCR: failed to delete uploaded document", zap.String("file_id", uploaded.ID), zap.Error(err))
+ }
+ }
+
+ req, err = http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("%s/url?expiry=%d", fileURL, mistralOCRSignedURLExpiryHours), nil)
+ if err == nil {
+ req.Header.Set("Authorization", BearerAuth(auth))
+ body, err = m.mistralOCRSend(req, maxModelResponseBodyBytes)
+ }
+ var signed struct {
+ URL string `json:"url"`
+ }
+ if err == nil {
+ if err = json.Unmarshal(body, &signed); err == nil && signed.URL == "" {
+ err = fmt.Errorf("response carries no url: %s", string(body))
+ }
+ }
+ if err != nil {
+ cleanup()
+ return "", nil, fmt.Errorf("Mistral OCR signed url: %w", err)
+ }
+ return signed.URL, cleanup, nil
+}
+
+type mistralOCRResponse struct {
+ Pages []mistralOCRPage `json:"pages"`
+}
+
+type mistralOCRPage struct {
+ Index int `json:"index"`
+ Markdown string `json:"markdown"`
+ Images []struct {
+ ID string `json:"id"`
+ } `json:"images"`
+ Tables []struct {
+ ID string `json:"id"`
+ Content string `json:"content"`
+ } `json:"tables"`
+ Blocks []struct {
+ Type string `json:"type"`
+ Content string `json:"content"`
+ TableID string `json:"table_id"`
+ } `json:"blocks"`
+}
+
+// markdown joins the pages in order. A page with layout blocks is rebuilt
+// from them, which drops header/footer blocks; a page without
+// blocks falls back to its Markdown with table and image links resolved.
+// Title blocks stay Markdown headings either way: the API sends their content
+// with its heading marker ("## Section"), and a bare title becomes "# ".
+func (r mistralOCRResponse) markdown() string {
+ parts := make([]string, 0, len(r.Pages))
+ for _, page := range r.Pages {
+ tables := make(map[string]string, len(page.Tables))
+ for _, table := range page.Tables {
+ tables[table.ID] = strings.TrimSpace(table.Content)
+ }
+ if len(page.Blocks) == 0 {
+ if md := page.resolvedMarkdown(tables); md != "" {
+ parts = append(parts, md)
+ }
+ continue
+ }
+ for _, block := range page.Blocks {
+ text := strings.TrimSpace(block.Content)
+ switch block.Type {
+ case "header", "footer", "image":
+ continue
+ case "title":
+ if text != "" && !strings.HasPrefix(text, "#") {
+ text = "# " + text
+ }
+ case "table":
+ if text == "" {
+ text = tables[block.TableID]
+ }
+ }
+ if text != "" {
+ parts = append(parts, text)
+ }
+ }
+ }
+ return strings.Join(parts, "\n\n")
+}
+
+func (p mistralOCRPage) resolvedMarkdown(tables map[string]string) string {
+ images := make(map[string]struct{}, len(p.Images))
+ for _, image := range p.Images {
+ images[image.ID] = struct{}{}
+ }
+ md := mistralOCRLinkPattern.ReplaceAllStringFunc(p.Markdown, func(link string) string {
+ match := mistralOCRLinkPattern.FindStringSubmatch(link)
+ target := match[2]
+ if table, ok := tables[target]; ok && match[1] == "" {
+ return table
+ }
+ if _, ok := images[target]; ok && match[1] == "!" {
+ return ""
+ }
+ return link
+ })
+ return strings.TrimSpace(mistralOCRBlankLines.ReplaceAllString(md, "\n\n"))
+}
diff --git a/internal/entity/models/mistral_ocr_test.go b/internal/entity/models/mistral_ocr_test.go
new file mode 100644
index 0000000000..06cb19c40a
--- /dev/null
+++ b/internal/entity/models/mistral_ocr_test.go
@@ -0,0 +1,618 @@
+//
+// 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"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "reflect"
+ "strings"
+ "sync"
+ "testing"
+)
+
+// mistralOCRResponseFixture mirrors a live /v1/ocr response with
+// include_blocks=true: page 0 carries blocks (a header to drop, a title whose
+// content already holds its Markdown heading marker, as the live API sends
+// it, a text paragraph, an image and an HTML table); page 1 carries no
+// blocks, so its Markdown is used with the table placeholder inlined and the
+// image placeholder removed.
+const mistralOCRResponseFixture = `{
+ "model": "mistral-ocr-latest",
+ "usage_info": {"pages_processed": 2},
+ "pages": [
+ {
+ "index": 0,
+ "dimensions": {"dpi": 87, "width": 720, "height": 1018},
+ "markdown": "# Title\n\nhello world",
+ "images": [{"id": "img-0.jpeg"}],
+ "tables": [{"id": "tbl-0.html", "content": "<table><tr><td>a</td></tr></table>", "format": "html"}],
+ "blocks": [
+ {"type": "header", "content": "Co-funded by the EU"},
+ {"type": "title", "content": "# Title"},
+ {"type": "text", "content": " hello world "},
+ {"type": "image", "content": ""},
+ {"type": "table", "content": "", "table_id": "tbl-0.html"},
+ {"type": "footer", "content": "Page 1"}
+ ]
+ },
+ {
+ "index": 1,
+ "dimensions": {"dpi": 144, "width": 1021, "height": 681},
+ "markdown": "## Second\n\n![img-1.jpeg](img-1.jpeg)\n\n[tbl-1.html](tbl-1.html)\n\nclosing",
+ "images": [{"id": "img-1.jpeg"}],
+ "tables": [{"id": "tbl-1.html", "content": "<table><tr><td>b</td></tr></table>", "format": "html"}]
+ }
+ ]
+}`
+
+type mistralOCRRecorder struct {
+ mu sync.Mutex
+ paths []string
+ ocrBody map[string]any
+ auth string
+ deleted []string
+ upload map[string]string
+ uploadSz int
+ // signedURLStatus, when set, is the status of the signed-URL request.
+ signedURLStatus int
+}
+
+func (r *mistralOCRRecorder) record(path string) {
+ r.mu.Lock()
+ defer r.mu.Unlock()
+ r.paths = append(r.paths, path)
+}
+
+// newMistralOCRServer serves the Mistral OCR and Files endpoints under
+// /v1, recording what the driver sent.
+func newMistralOCRServer(t *testing.T, rec *mistralOCRRecorder, ocrStatus int, ocrResponse string) *httptest.Server {
+ t.Helper()
+ var srv *httptest.Server
+ srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ rec.record(r.Method + " " + r.URL.RequestURI())
+ w.Header().Set("Content-Type", "application/json")
+ switch {
+ case r.Method == http.MethodPost && r.URL.Path == "/v1/ocr":
+ rec.mu.Lock()
+ rec.auth = r.Header.Get("Authorization")
+ raw, _ := io.ReadAll(r.Body)
+ if err := json.Unmarshal(raw, &rec.ocrBody); err != nil {
+ t.Errorf("invalid OCR JSON body: %v", err)
+ }
+ rec.mu.Unlock()
+ w.WriteHeader(ocrStatus)
+ _, _ = w.Write([]byte(ocrResponse))
+ case r.Method == http.MethodPost && r.URL.Path == "/v1/files":
+ if err := r.ParseMultipartForm(64 << 20); err != nil {
+ t.Errorf("parse upload: %v", err)
+ }
+ file, header, err := r.FormFile("file")
+ if err != nil {
+ t.Errorf("upload file part: %v", err)
+ return
+ }
+ data, _ := io.ReadAll(file)
+ rec.mu.Lock()
+ rec.upload = map[string]string{"purpose": r.FormValue("purpose"), "filename": header.Filename}
+ rec.uploadSz = len(data)
+ rec.mu.Unlock()
+ _, _ = w.Write([]byte(`{"id":"file-1","purpose":"ocr"}`))
+ case r.Method == http.MethodGet && r.URL.Path == "/v1/files/file-1/url":
+ if rec.signedURLStatus != 0 {
+ w.WriteHeader(rec.signedURLStatus)
+ _, _ = w.Write([]byte(`{"message":"signed url unavailable"}`))
+ return
+ }
+ _, _ = w.Write([]byte(`{"url":"https://signed.example/doc.pdf"}`))
+ case r.Method == http.MethodDelete && r.URL.Path == "/v1/files/file-1":
+ rec.mu.Lock()
+ rec.deleted = append(rec.deleted, "file-1")
+ rec.mu.Unlock()
+ _, _ = w.Write([]byte(`{"id":"file-1","deleted":true}`))
+ default:
+ http.NotFound(w, r)
+ }
+ }))
+ return srv
+}
+
+func newMistralOCRDriverForTest() *MistralModel {
+ return NewMistralModel(
+ map[string]string{"default": "https://api.mistral.ai"},
+ URLSuffix{OCR: "v1/ocr", Files: "v1/files"},
+ )
+}
+
+func mistralOCRAPIConfig(apiKey, baseURL string) *APIConfig {
+ return &APIConfig{ApiKey: &apiKey, BaseURL: &baseURL}
+}
+
+func TestMistralOCRFileSendsInlinePDFRequest(t *testing.T) {
+ withSSRFBypass(t)
+ rec := &mistralOCRRecorder{}
+ srv := newMistralOCRServer(t, rec, http.StatusOK, mistralOCRResponseFixture)
+ defer srv.Close()
+
+ model := "mistral-ocr-latest"
+ pdf := []byte("%PDF-1.4 tiny")
+ // A base URL that already ends in /v1 (the documented form) must not yield /v1/v1/ocr.
+ resp, err := newMistralOCRDriverForTest().OCRFile(context.Background(), &model, pdf, nil,
+ mistralOCRAPIConfig("sk-test", srv.URL+"/v1/"), nil, nil)
+ if err != nil {
+ t.Fatalf("OCRFile: %v", err)
+ }
+ if resp == nil || resp.Text == nil {
+ t.Fatal("OCRFile returned no text")
+ }
+ if rec.auth != "Bearer sk-test" {
+ t.Errorf("Authorization = %q, want Bearer sk-test", rec.auth)
+ }
+ if got, want := rec.paths, []string{"POST /v1/ocr"}; !reflect.DeepEqual(got, want) {
+ t.Fatalf("requests = %v, want %v", got, want)
+ }
+ body := rec.ocrBody
+ if body["model"] != model {
+ t.Errorf("model = %v, want %s", body["model"], model)
+ }
+ if body["table_format"] != "html" {
+ t.Errorf("table_format = %v, want html", body["table_format"])
+ }
+ if body["include_blocks"] != true {
+ t.Errorf("include_blocks = %v, want true", body["include_blocks"])
+ }
+ if body["include_image_base64"] != false {
+ t.Errorf("include_image_base64 = %v, want false", body["include_image_base64"])
+ }
+ if _, ok := body["pages"]; ok {
+ t.Errorf("pages = %v, want absent without a page selection", body["pages"])
+ }
+ doc, _ := body["document"].(map[string]any)
+ if doc["type"] != "document_url" {
+ t.Errorf("document.type = %v, want document_url", doc["type"])
+ }
+ if url, _ := doc["document_url"].(string); !strings.HasPrefix(url, "data:application/pdf;base64,") {
+ t.Errorf("document_url = %.60q, want a PDF data URL", url)
+ }
+}
+
+func TestMistralOCRFileDetectsPDFWithLeadingBytes(t *testing.T) {
+ withSSRFBypass(t)
+ rec := &mistralOCRRecorder{}
+ srv := newMistralOCRServer(t, rec, http.StatusOK, mistralOCRResponseFixture)
+ defer srv.Close()
+
+ model := "mistral-ocr-latest"
+ // Real-world PDFs often carry bytes before the header; content sniffing
+ // alone labels them text/plain, which the OCR API rejects with 422.
+ pdf := []byte("\r\n%PDF-1.4\r\n%\xfb\xfc\r\n1 0 obj\r\n<<>>\r\nendobj")
+ if _, err := newMistralOCRDriverForTest().OCRFile(context.Background(), &model, pdf, nil,
+ mistralOCRAPIConfig("sk-test", srv.URL), nil, nil); err != nil {
+ t.Fatalf("OCRFile: %v", err)
+ }
+ doc, _ := rec.ocrBody["document"].(map[string]any)
+ if url, _ := doc["document_url"].(string); !strings.HasPrefix(url, "data:application/pdf;base64,") {
+ t.Errorf("document_url = %.60q, want a PDF data URL", url)
+ }
+}
+
+func TestMistralOCRMimeTypeDropsParameters(t *testing.T) {
+ if got := mistralOCRMimeType([]byte("plain text, not a document")); got != "text/plain" {
+ t.Errorf("mistralOCRMimeType(text) = %q, want text/plain", got)
+ }
+}
+
+// An image whose first kilobyte happens to contain "%PDF-" (metadata, an
+// embedded thumbnail) is still an image: its signature wins over the PDF
+// header window.
+func TestMistralOCRMimeTypePrefersImageSignature(t *testing.T) {
+ png := append([]byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0, 0, 0, 0x0D, 0x49, 0x48, 0x44, 0x52}, []byte("tEXt%PDF-1.4")...)
+ if got := mistralOCRMimeType(png); got != "image/png" {
+ t.Errorf("mistralOCRMimeType(png with %%PDF-) = %q, want image/png", got)
+ }
+ jpeg := append([]byte{0xFF, 0xD8, 0xFF, 0xE0}, []byte("JFIF %PDF-1.7")...)
+ if got := mistralOCRMimeType(jpeg); got != "image/jpeg" {
+ t.Errorf("mistralOCRMimeType(jpeg with %%PDF-) = %q, want image/jpeg", got)
+ }
+}
+
+// Title blocks render as Markdown headings so block-built pages match pages
+// that fall back to the API Markdown. The live API already prefixes the
+// content with its heading marker ("## Section"); a bare title gets "# ".
+func TestMistralOCRMarkdownRendersTitleBlocksAsHeadings(t *testing.T) {
+ var result mistralOCRResponse
+ if err := json.Unmarshal([]byte(`{"pages":[{"index":0,"blocks":[
+ {"type":"title","content":"# Annual Report"},
+ {"type":"title","content":"## Summary"},
+ {"type":"title","content":"Plain Title"},
+ {"type":"text","content":"Body text."}
+ ]}]}`), &result); err != nil {
+ t.Fatalf("unmarshal: %v", err)
+ }
+ want := "# Annual Report\n\n## Summary\n\n# Plain Title\n\nBody text."
+ if got := result.markdown(); got != want {
+ t.Fatalf("markdown =\n%q\nwant\n%q", got, want)
+ }
+}
+
+func TestMistralOCRSendRejectsOversizedResponse(t *testing.T) {
+ withSSRFBypass(t)
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ _, _ = w.Write(bytes.Repeat([]byte("x"), 2048))
+ }))
+ defer srv.Close()
+
+ req, err := http.NewRequest(http.MethodGet, srv.URL, nil)
+ if err != nil {
+ t.Fatalf("NewRequest: %v", err)
+ }
+ _, err = newMistralOCRDriverForTest().mistralOCRSend(req, 1024)
+ if err == nil || !strings.Contains(err.Error(), "exceeds 1024 bytes") {
+ t.Fatalf("error = %v, want the response size limit", err)
+ }
+}
+
+func TestMistralOCRFileJoinsPagesAndInlinesTables(t *testing.T) {
+ withSSRFBypass(t)
+ rec := &mistralOCRRecorder{}
+ srv := newMistralOCRServer(t, rec, http.StatusOK, mistralOCRResponseFixture)
+ defer srv.Close()
+
+ model := "mistral-ocr-latest"
+ resp, err := newMistralOCRDriverForTest().OCRFile(context.Background(), &model, []byte("%PDF-1.4"), nil,
+ mistralOCRAPIConfig("sk-test", srv.URL), nil, nil)
+ if err != nil {
+ t.Fatalf("OCRFile: %v", err)
+ }
+ want := strings.Join([]string{
+ "# Title",
+ "hello world",
+ "<table><tr><td>a</td></tr></table>",
+ "## Second",
+ "<table><tr><td>b</td></tr></table>",
+ "closing",
+ }, "\n\n")
+ if got := *resp.Text; got != want {
+ t.Fatalf("text =\n%s\nwant\n%s", got, want)
+ }
+}
+
+func TestMistralOCRFileForwardsPageSelection(t *testing.T) {
+ withSSRFBypass(t)
+ cases := []struct {
+ name string
+ ranges [][]int
+ want []any
+ }{
+ {"one-indexed ranges become zero-based indexes", [][]int{{2, 3}, {5, 5}}, []any{1.0, 2.0, 4.0}},
+ {"selection is bounded by the 1000-page API limit", [][]int{{999, 100000}}, []any{998.0, 999.0}},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ rec := &mistralOCRRecorder{}
+ srv := newMistralOCRServer(t, rec, http.StatusOK, mistralOCRResponseFixture)
+ defer srv.Close()
+
+ model := "mistral-ocr-latest"
+ if _, err := newMistralOCRDriverForTest().OCRFile(context.Background(), &model, []byte("%PDF-1.4"), nil,
+ mistralOCRAPIConfig("sk-test", srv.URL), &OCRConfig{Pages: tc.ranges}, nil); err != nil {
+ t.Fatalf("OCRFile: %v", err)
+ }
+ if got := rec.ocrBody["pages"]; !reflect.DeepEqual(got, tc.want) {
+ t.Fatalf("pages = %v, want %v", got, tc.want)
+ }
+ })
+ }
+}
+
+func TestMistralOCRFileSendsImagesAsImageURL(t *testing.T) {
+ withSSRFBypass(t)
+ rec := &mistralOCRRecorder{}
+ srv := newMistralOCRServer(t, rec, http.StatusOK, `{"pages":[]}`)
+ defer srv.Close()
+
+ model := "mistral-ocr-latest"
+ png := []byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0, 0, 0, 0x0D, 0x49, 0x48, 0x44, 0x52}
+ if _, err := newMistralOCRDriverForTest().OCRFile(context.Background(), &model, png, nil,
+ mistralOCRAPIConfig("sk-test", srv.URL), nil, nil); err != nil {
+ t.Fatalf("OCRFile: %v", err)
+ }
+ doc, _ := rec.ocrBody["document"].(map[string]any)
+ if doc["type"] != "image_url" {
+ t.Fatalf("document.type = %v, want image_url", doc["type"])
+ }
+ if url, _ := doc["image_url"].(string); !strings.HasPrefix(url, "data:image/png;base64,") {
+ t.Fatalf("image_url = %.40q, want a PNG data URL", url)
+ }
+}
+
+// A remote URL whose path ends in an image extension is sent as image_url;
+// the query string and the extension case do not matter.
+func TestMistralOCRFileSendsRemoteImagesAsImageURL(t *testing.T) {
+ withSSRFBypass(t)
+ rec := &mistralOCRRecorder{}
+ srv := newMistralOCRServer(t, rec, http.StatusOK, `{"pages":[]}`)
+ defer srv.Close()
+
+ model := "mistral-ocr-latest"
+ for _, tc := range []struct {
+ remote string
+ wantType string
+ }{
+ {remote: "https://example.com/scans/Page-1.JPG?sig=abc", wantType: "image_url"},
+ {remote: "https://example.com/scans/page-2.webp", wantType: "image_url"},
+ {remote: "https://example.com/report.pdf?name=scan.png", wantType: "document_url"},
+ {remote: "https://example.com/slides.pptx", wantType: "document_url"},
+ {remote: "https://example.com/download", wantType: "document_url"},
+ } {
+ t.Run(tc.remote, func(t *testing.T) {
+ remote := tc.remote
+ if _, err := newMistralOCRDriverForTest().OCRFile(context.Background(), &model, nil, &remote,
+ mistralOCRAPIConfig("sk-test", srv.URL), nil, nil); err != nil {
+ t.Fatalf("OCRFile: %v", err)
+ }
+ doc, _ := rec.ocrBody["document"].(map[string]any)
+ if doc["type"] != tc.wantType || doc[tc.wantType] != remote {
+ t.Fatalf("document = %v, want %s %s", doc, tc.wantType, remote)
+ }
+ })
+ }
+}
+
+func TestMistralOCRFileSendsRemoteDocumentURL(t *testing.T) {
+ withSSRFBypass(t)
+ rec := &mistralOCRRecorder{}
+ srv := newMistralOCRServer(t, rec, http.StatusOK, `{"pages":[]}`)
+ defer srv.Close()
+
+ model := "mistral-ocr-latest"
+ remote := "https://example.com/report.pdf"
+ if _, err := newMistralOCRDriverForTest().OCRFile(context.Background(), &model, nil, &remote,
+ mistralOCRAPIConfig("sk-test", srv.URL), nil, nil); err != nil {
+ t.Fatalf("OCRFile: %v", err)
+ }
+ doc, _ := rec.ocrBody["document"].(map[string]any)
+ if doc["type"] != "document_url" || doc["document_url"] != remote {
+ t.Fatalf("document = %v, want document_url %s", doc, remote)
+ }
+}
+
+// Instances created by Python's "Mistral OCR" factory store the key inside a
+// JSON api_key; the bare key is sent and the payload base URL is used when the
+// instance has none.
+func TestMistralOCRFileReadsStoredJSONKey(t *testing.T) {
+ withSSRFBypass(t)
+ rec := &mistralOCRRecorder{}
+ srv := newMistralOCRServer(t, rec, http.StatusOK, `{"pages":[]}`)
+ defer srv.Close()
+
+ model := "mistral-ocr-latest"
+ cases := []struct {
+ name string
+ config *APIConfig
+ want string
+ }{
+ {
+ name: "environment payload",
+ config: &APIConfig{ApiKey: strPtr(`{"MISTRAL_OCR_BASE_URL": "` + srv.URL + `/v1", "MISTRAL_OCR_API_KEY": "sk-env",` +
+ ` "MISTRAL_OCR_TABLE_FORMAT": "html", "MISTRAL_OCR_KEEP_HEADER_FOOTER": 0}`)},
+ want: "Bearer sk-env",
+ },
+ {
+ name: "form payload",
+ config: &APIConfig{ApiKey: strPtr(`{"api_key": " sk-form ", "mistral_ocr_base_url": "` + srv.URL + `"}`)},
+ want: "Bearer sk-form",
+ },
+ {
+ name: "nested form payload",
+ config: &APIConfig{ApiKey: strPtr(`{"api_key": {"api_key": "sk-nested", "mistral_ocr_base_url": "` + srv.URL + `/v1"}}`)},
+ want: "Bearer sk-nested",
+ },
+ {
+ name: "instance base URL wins over the payload",
+ config: mistralOCRAPIConfig(`{"api_key": "sk-form", "mistral_ocr_base_url": "https://unused.example/v1"}`, srv.URL),
+ want: "Bearer sk-form",
+ },
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ rec.paths = nil
+ if _, err := newMistralOCRDriverForTest().OCRFile(context.Background(), &model, []byte("%PDF-1.4"), nil,
+ tc.config, nil, nil); err != nil {
+ t.Fatalf("OCRFile: %v", err)
+ }
+ if rec.auth != tc.want {
+ t.Errorf("Authorization = %q, want %q", rec.auth, tc.want)
+ }
+ if got, want := rec.paths, []string{"POST /v1/ocr"}; !reflect.DeepEqual(got, want) {
+ t.Errorf("requests = %v, want %v", got, want)
+ }
+ })
+ }
+}
+
+// A JSON api_key without a key is rejected before any request is made.
+func TestMistralOCRFileRejectsStoredJSONWithoutKey(t *testing.T) {
+ model := "mistral-ocr-latest"
+ _, err := newMistralOCRDriverForTest().OCRFile(context.Background(), &model, []byte("%PDF-1.4"), nil,
+ &APIConfig{ApiKey: strPtr(`{"MISTRAL_OCR_BASE_URL": "https://api.mistral.ai/v1"}`)}, nil, nil)
+ if err == nil || !strings.Contains(err.Error(), "api key is required") {
+ t.Fatalf("error = %v, want api key is required", err)
+ }
+}
+
+func TestMistralOCRFileUploadsLargeDocuments(t *testing.T) {
+ withSSRFBypass(t)
+ rec := &mistralOCRRecorder{}
+ srv := newMistralOCRServer(t, rec, http.StatusOK, mistralOCRResponseFixture)
+ defer srv.Close()
+
+ model := "mistral-ocr-latest"
+ large := append([]byte("%PDF-1.4\n"), bytes.Repeat([]byte("x"), mistralOCRInlineMaxBytes)...)
+ if _, err := newMistralOCRDriverForTest().OCRFile(context.Background(), &model, large, nil,
+ mistralOCRAPIConfig("sk-test", srv.URL), nil, nil); err != nil {
+ t.Fatalf("OCRFile: %v", err)
+ }
+ want := []string{
+ "POST /v1/files",
+ "GET /v1/files/file-1/url?expiry=24",
+ "POST /v1/ocr",
+ "DELETE /v1/files/file-1",
+ }
+ if !reflect.DeepEqual(rec.paths, want) {
+ t.Fatalf("requests = %v, want %v", rec.paths, want)
+ }
+ if rec.upload["purpose"] != "ocr" || rec.upload["filename"] != "document.pdf" || rec.uploadSz != len(large) {
+ t.Errorf("upload = %v (%d bytes), want purpose=ocr document.pdf %d bytes", rec.upload, rec.uploadSz, len(large))
+ }
+ doc, _ := rec.ocrBody["document"].(map[string]any)
+ if doc["document_url"] != "https://signed.example/doc.pdf" {
+ t.Errorf("document_url = %v, want the signed URL", doc["document_url"])
+ }
+}
+
+func TestMistralOCRFileDeletesUploadWhenOCRFails(t *testing.T) {
+ withSSRFBypass(t)
+ rec := &mistralOCRRecorder{}
+ srv := newMistralOCRServer(t, rec, http.StatusInternalServerError, `{"message":"boom"}`)
+ defer srv.Close()
+
+ model := "mistral-ocr-latest"
+ large := append([]byte("%PDF-1.4\n"), bytes.Repeat([]byte("x"), mistralOCRInlineMaxBytes)...)
+ if _, err := newMistralOCRDriverForTest().OCRFile(context.Background(), &model, large, nil,
+ mistralOCRAPIConfig("sk-test", srv.URL), nil, nil); err == nil {
+ t.Fatal("OCRFile: want error, got nil")
+ }
+ if !reflect.DeepEqual(rec.deleted, []string{"file-1"}) {
+ t.Fatalf("deleted = %v, want the uploaded file removed", rec.deleted)
+ }
+}
+
+func TestMistralOCRFileDeletesUploadWhenSignedURLFails(t *testing.T) {
+ withSSRFBypass(t)
+ rec := &mistralOCRRecorder{signedURLStatus: http.StatusInternalServerError}
+ srv := newMistralOCRServer(t, rec, http.StatusOK, mistralOCRResponseFixture)
+ defer srv.Close()
+
+ model := "mistral-ocr-latest"
+ large := append([]byte("%PDF-1.4\n"), bytes.Repeat([]byte("x"), mistralOCRInlineMaxBytes)...)
+ _, err := newMistralOCRDriverForTest().OCRFile(context.Background(), &model, large, nil,
+ mistralOCRAPIConfig("sk-test", srv.URL), nil, nil)
+ if err == nil || !strings.Contains(err.Error(), "signed url") {
+ t.Fatalf("OCRFile error = %v, want the signed url failure", err)
+ }
+ want := []string{
+ "POST /v1/files",
+ "GET /v1/files/file-1/url?expiry=24",
+ "DELETE /v1/files/file-1",
+ }
+ if !reflect.DeepEqual(rec.paths, want) {
+ t.Fatalf("requests = %v, want %v (no OCR call, upload deleted)", rec.paths, want)
+ }
+}
+
+func TestMistralOCRFileReportsHTTPErrors(t *testing.T) {
+ withSSRFBypass(t)
+ rec := &mistralOCRRecorder{}
+ srv := newMistralOCRServer(t, rec, http.StatusUnprocessableEntity, `{"detail":"invalid document"}`)
+ defer srv.Close()
+
+ model := "mistral-ocr-latest"
+ _, err := newMistralOCRDriverForTest().OCRFile(context.Background(), &model, []byte("%PDF-1.4"), nil,
+ mistralOCRAPIConfig("sk-test", srv.URL), nil, nil)
+ if err == nil {
+ t.Fatal("OCRFile: want error, got nil")
+ }
+ if !strings.Contains(err.Error(), "422") || !strings.Contains(err.Error(), "invalid document") {
+ t.Fatalf("error = %v, want status and body", err)
+ }
+}
+
+func TestMistralOCRFileRequiresAPIKey(t *testing.T) {
+ model := "mistral-ocr-latest"
+ _, err := newMistralOCRDriverForTest().OCRFile(context.Background(), &model, []byte("%PDF-1.4"), nil,
+ mistralOCRAPIConfig(" ", ""), nil, nil)
+ if err == nil || !strings.Contains(err.Error(), "api key is required") {
+ t.Fatalf("error = %v, want api key is required", err)
+ }
+}
+
+// mistral-ocr-latest is listed with the "ocr" type under the Mistral
+// provider, so a tenant can add it to a Mistral instance from the model
+// settings and pick it as a PDF layout recognizer.
+func TestMistralProviderListsOCRModels(t *testing.T) {
+ dir, restore := setupProviderTestDir(t, "mistral.json")
+ defer restore()
+
+ if err := InitProviderManager(dir); err != nil {
+ t.Fatalf("InitProviderManager: %v", err)
+ }
+ provider := GetProviderManager().FindProvider("Mistral")
+ if provider == nil {
+ t.Fatal("Mistral provider not found")
+ }
+ if provider.URLSuffix.OCR != "v1/ocr" || provider.URLSuffix.Files != "v1/files" {
+ t.Fatalf("URLSuffix = %+v, want ocr=v1/ocr files=v1/files", provider.URLSuffix)
+ }
+ for _, name := range []string{"mistral-ocr-latest", "mistral-ocr-2512"} {
+ model, err := GetProviderManager().GetModelByName("Mistral", name)
+ if err != nil {
+ t.Fatalf("GetModelByName(%q): %v", name, err)
+ }
+ if !model.ModelTypeMap["ocr"] {
+ t.Fatalf("%s model types = %v, want ocr", name, model.ModelTypes)
+ }
+ }
+}
+
+// Stored "Mistral OCR" provider names resolve to the Mistral catalog entry
+// through CatalogProviderName, while FindProvider keeps the stored name
+// unknown.
+func TestCatalogProviderNameMapsStoredMistralOCR(t *testing.T) {
+ dir, restore := setupProviderTestDir(t, "mistral.json")
+ defer restore()
+
+ if err := InitProviderManager(dir); err != nil {
+ t.Fatalf("InitProviderManager: %v", err)
+ }
+ for _, stored := range []string{"Mistral OCR", "mistral ocr", " Mistral OCR "} {
+ if got := CatalogProviderName(stored); got != "Mistral" {
+ t.Errorf("CatalogProviderName(%q) = %q, want Mistral", stored, got)
+ }
+ }
+ for _, stored := range []string{"Mistral", "OpenAI", "MistralOCR", ""} {
+ if got := CatalogProviderName(stored); got != stored {
+ t.Errorf("CatalogProviderName(%q) = %q, want it unchanged", stored, got)
+ }
+ }
+
+ pm := GetProviderManager()
+ if provider := pm.FindProvider("Mistral OCR"); provider != nil {
+ t.Fatalf("FindProvider(%q) = %s, want nil", "Mistral OCR", provider.Name)
+ }
+ model, err := pm.GetModelByName(CatalogProviderName("Mistral OCR"), "mistral-ocr-latest")
+ if err != nil {
+ t.Fatalf("GetModelByName: %v", err)
+ }
+ if !model.ModelTypeMap["ocr"] {
+ t.Fatalf("model types = %v, want ocr", model.ModelTypes)
+ }
+}
diff --git a/internal/entity/models/model.go b/internal/entity/models/model.go
index 240c611034..fb51512d21 100644
--- a/internal/entity/models/model.go
+++ b/internal/entity/models/model.go
@@ -875,6 +875,27 @@ func findRepoRoot() string {
return "."
}
+// storedCatalogProviders maps provider names found in
+// tenant_model_provider.provider_name and in model references onto the
+// catalog provider that serves those models. Rows created by Python's
+// "Mistral OCR" factory carry that name; their models are the Mistral
+// provider's ocr models.
+var storedCatalogProviders = map[string]string{
+ "mistral ocr": "Mistral",
+}
+
+// CatalogProviderName returns the catalog provider name for a stored provider
+// name, for looking up a stored model in the catalog (FindProvider,
+// GetModelByName). Tenant rows are still looked up by the stored name, and
+// FindProvider itself does not apply this mapping, so provider management
+// keeps treating the stored name as an unknown provider.
+func CatalogProviderName(storedName string) string {
+ if name, ok := storedCatalogProviders[strings.ToLower(strings.TrimSpace(storedName))]; ok {
+ return name
+ }
+ return storedName
+}
+
func (pm *ProviderManager) FindProvider(name string) *Provider {
for i := range pm.Providers {
if strings.EqualFold(pm.Providers[i].Name, name) {
diff --git a/internal/entity/models/types.go b/internal/entity/models/types.go
index a7df795561..add68abc8d 100644
--- a/internal/entity/models/types.go
+++ b/internal/entity/models/types.go
@@ -258,6 +258,10 @@ type TTSConfig struct {
type OCRConfig struct {
Algorithm string
+ // Pages restricts OCR to these 1-indexed inclusive [from, to] ranges, the
+ // parser setup's "pages" shape. Empty means the whole document. Drivers
+ // whose backend cannot select pages ignore it.
+ Pages [][]int
}
type ParseFileConfig struct {
diff --git a/internal/ingestion/component/dispatch_model.go b/internal/ingestion/component/dispatch_model.go
index 24647d634a..52fc024be0 100644
--- a/internal/ingestion/component/dispatch_model.go
+++ b/internal/ingestion/component/dispatch_model.go
@@ -231,7 +231,8 @@ func resolveModelConfigByID(ctx context.Context, db *gorm.DB, tenantID string, m
region := extra["region"]
baseURL := extra["base_url"]
- providerInfo := dao.GetModelProviderManager().FindProvider(provider.ProviderName)
+ catalogName := modelModule.CatalogProviderName(provider.ProviderName)
+ providerInfo := dao.GetModelProviderManager().FindProvider(catalogName)
if providerInfo == nil {
return nil, "", nil, 0, fmt.Errorf("provider %q driver not found", provider.ProviderName)
}
@@ -240,7 +241,7 @@ func resolveModelConfigByID(ctx context.Context, db *gorm.DB, tenantID string, m
return nil, "", nil, 0, err
}
contextLength := 0
- if mi, _ := dao.GetModelProviderManager().GetModelByName(provider.ProviderName, modelObj.ModelName); mi != nil {
+ if mi, _ := dao.GetModelProviderManager().GetModelByName(catalogName, modelObj.ModelName); mi != nil {
if mi.ContextLength != nil {
contextLength = *mi.ContextLength
} else if mi.MaxTokens != nil {
@@ -265,6 +266,7 @@ func resolveModelConfigFromProviderInstance(ctx context.Context, db *gorm.DB, te
if err != nil {
return nil, "", nil, 0, err
}
+ catalogName := modelModule.CatalogProviderName(providerName)
providerDAO := dao.NewTenantModelProviderDAO()
instanceDAO := dao.NewTenantModelInstanceDAO()
@@ -293,7 +295,7 @@ func resolveModelConfigFromProviderInstance(ctx context.Context, db *gorm.DB, te
if modelObj.Status == "inactive" {
return nil, "", nil, 0, fmt.Errorf("model %q is disabled", modelName)
}
- providerInfo := dao.GetModelProviderManager().FindProvider(providerName)
+ providerInfo := dao.GetModelProviderManager().FindProvider(catalogName)
if providerInfo == nil {
return nil, "", nil, 0, fmt.Errorf("provider %q driver not found", providerName)
}
@@ -302,7 +304,7 @@ func resolveModelConfigFromProviderInstance(ctx context.Context, db *gorm.DB, te
return nil, "", nil, 0, err
}
contextLength := 0
- if mi, _ := dao.GetModelProviderManager().GetModelByName(providerName, pureModelName); mi != nil {
+ if mi, _ := dao.GetModelProviderManager().GetModelByName(catalogName, pureModelName); mi != nil {
if mi.ContextLength != nil {
contextLength = *mi.ContextLength
} else if mi.MaxTokens != nil {
@@ -324,7 +326,7 @@ func resolveModelConfigFromProviderInstance(ctx context.Context, db *gorm.DB, te
return nil, "", nil, 0, fmt.Errorf("model %q lookup failed: %w", modelName, modelErr)
}
- targetProvider := dao.GetModelProviderManager().FindProvider(providerName)
+ targetProvider := dao.GetModelProviderManager().FindProvider(catalogName)
if targetProvider == nil {
return nil, "", nil, 0, fmt.Errorf("model provider config not found: %s", providerName)
}
diff --git a/internal/ingestion/component/dispatch_model_test.go b/internal/ingestion/component/dispatch_model_test.go
index 18b40a264f..e718b4e3ec 100644
--- a/internal/ingestion/component/dispatch_model_test.go
+++ b/internal/ingestion/component/dispatch_model_test.go
@@ -18,6 +18,10 @@ package component
import (
"testing"
+ "github.com/glebarez/sqlite"
+ "gorm.io/gorm"
+
+ "ragflow/internal/entity"
"ragflow/internal/ingestion/component/schema"
)
@@ -47,3 +51,57 @@ func TestConfiguredAudioModelID(t *testing.T) {
})
}
}
+
+// Models stored under Python's "Mistral OCR" provider name resolve to the
+// Mistral driver and catalog entry, by tenant model id and by composite
+// reference, enrolled or catalog-only. The instance key is passed through as
+// stored; OCRFile reads the bare key from it.
+func TestResolveModelConfigForStoredMistralOCRProvider(t *testing.T) {
+ db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{TranslateError: true})
+ if err != nil {
+ t.Fatalf("open sqlite: %v", err)
+ }
+ if err := db.AutoMigrate(&entity.TenantModelProvider{}, &entity.TenantModelInstance{}, &entity.TenantModel{}); err != nil {
+ t.Fatalf("migrate: %v", err)
+ }
+ const storedKey = `{"MISTRAL_OCR_API_KEY": "sk-env"}`
+ for _, row := range []interface{}{
+ &entity.TenantModelProvider{ID: "provider-mistral-ocr", TenantID: "tenant-1", ProviderName: "Mistral OCR"},
+ &entity.TenantModelInstance{ID: "instance-mistral-ocr", ProviderID: "provider-mistral-ocr", InstanceName: "default", APIKey: storedKey, Status: "active", Extra: "{}"},
+ &entity.TenantModel{ID: "model-mistral-ocr", ProviderID: "provider-mistral-ocr", InstanceID: "instance-mistral-ocr", ModelName: "mistral-ocr-latest", ModelType: int(entity.ModelTypeOCR), Status: "active"},
+ } {
+ if err := db.Create(row).Error; err != nil {
+ t.Fatalf("seed %T: %v", row, err)
+ }
+ }
+
+ for _, tc := range []struct {
+ name string
+ modelRef string
+ wantModel string
+ }{
+ {name: "tenant model id", modelRef: "model-mistral-ocr", wantModel: "mistral-ocr-latest"},
+ {name: "enrolled composite", modelRef: "mistral-ocr-latest@default@Mistral OCR", wantModel: "mistral-ocr-latest"},
+ {name: "catalog composite", modelRef: "mistral-ocr-2512@default@Mistral OCR", wantModel: "mistral-ocr-2512"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ driver, modelName, apiConfig, contextLength, err := defaultResolveModelConfig(t.Context(), db, "tenant-1", entity.ModelTypeOCR, tc.modelRef)
+ if err != nil {
+ t.Fatalf("resolve %q: %v", tc.modelRef, err)
+ }
+ if driver == nil || driver.Name() != "mistral" {
+ t.Fatalf("driver = %v, want the mistral driver", driver)
+ }
+ if modelName != tc.wantModel {
+ t.Fatalf("model name = %q, want %q", modelName, tc.wantModel)
+ }
+ // 8192 is the catalog max_tokens of the Mistral OCR models.
+ if contextLength != 8192 {
+ t.Fatalf("context length = %d, want 8192 from the Mistral catalog", contextLength)
+ }
+ if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey != storedKey {
+ t.Fatalf("api config = %+v, want the stored key", apiConfig)
+ }
+ })
+ }
+}
diff --git a/internal/service/model_factory.go b/internal/service/model_factory.go
index 325b87faf1..0aec4078f4 100644
--- a/internal/service/model_factory.go
+++ b/internal/service/model_factory.go
@@ -607,7 +607,7 @@ func extraToolSupport(extra string) (bool, bool) {
func catalogToolSupport(providerName, modelName string) bool {
providerManager := dao.GetModelProviderManager()
- provider := providerManager.FindProvider(providerName)
+ provider := providerManager.FindProvider(modelModule.CatalogProviderName(providerName))
if provider == nil {
return false
}
@@ -733,12 +733,13 @@ func (s *ModelFactory) resolveTenantModel(ctx context.Context, modelType entity.
providerEntity := identity.providerEntity
instanceEntity := identity.instanceEntity
- providerInfo := dao.GetModelProviderManager().FindProvider(providerEntity.ProviderName)
+ catalogName := modelModule.CatalogProviderName(providerEntity.ProviderName)
+ providerInfo := dao.GetModelProviderManager().FindProvider(catalogName)
if providerInfo == nil {
return nil, fmt.Errorf("%w: provider %q driver not found", errModelConfigUnavailable, providerEntity.ProviderName)
}
- modelInfo, _ := dao.GetModelProviderManager().GetModelByName(providerEntity.ProviderName, modelEntity.ModelName)
+ modelInfo, _ := dao.GetModelProviderManager().GetModelByName(catalogName, modelEntity.ModelName)
modelInfo, err := modelInfoWithTenantExtra(modelInfo, modelEntity)
if err != nil {
return nil, fmt.Errorf("%w: read model metadata: %v", errModelConfigUnavailable, err)
@@ -888,7 +889,8 @@ func (s *ModelFactory) resolveProviderInstanceModel(ctx context.Context, access
return nil, fmt.Errorf("%w: model %q is disabled", errModelConfigUnavailable, modelRef)
}
- providerInfo := dao.GetModelProviderManager().FindProvider(providerName)
+ catalogName := modelModule.CatalogProviderName(providerName)
+ providerInfo := dao.GetModelProviderManager().FindProvider(catalogName)
if availableTypes != 0 {
if access.UserID != "" {
if err = checkModelPermission(ctx, access.UserID, permission.ResourceKindModel, modelEntity.ID, permission.OperationUse); err != nil {
@@ -902,7 +904,7 @@ func (s *ModelFactory) resolveProviderInstanceModel(ctx context.Context, access
if driverErr != nil {
return nil, fmt.Errorf("%w: create model driver: %v", errModelConfigUnavailable, driverErr)
}
- modelInfo, _ := dao.GetModelProviderManager().GetModelByName(providerName, pureModelName)
+ modelInfo, _ := dao.GetModelProviderManager().GetModelByName(catalogName, pureModelName)
modelInfo, err = modelInfoWithTenantExtra(modelInfo, modelEntity)
if err != nil {
return nil, fmt.Errorf("%w: read model metadata: %v", errModelConfigUnavailable, err)
@@ -926,7 +928,7 @@ func (s *ModelFactory) resolveProviderInstanceModel(ctx context.Context, access
if providerInfo == nil {
return nil, fmt.Errorf("%w: model provider config not found: %s", errModelConfigUnavailable, providerName)
}
- targetProvider := dao.GetModelProviderManager().FindProvider(providerName)
+ targetProvider := dao.GetModelProviderManager().FindProvider(catalogName)
if targetProvider == nil {
return nil, fmt.Errorf("%w: model provider config not found: %s", errModelConfigUnavailable, providerName)
}
@@ -1058,7 +1060,7 @@ func (s *ModelFactory) resolveCompositeModelType(ctx context.Context, access Mod
return activeTypes, nil
}
- providerInfo := dao.GetModelProviderManager().FindProvider(providerName)
+ providerInfo := dao.GetModelProviderManager().FindProvider(modelModule.CatalogProviderName(providerName))
if providerInfo == nil {
return 0, fmt.Errorf("%w: model provider config not found: %s", errModelConfigUnavailable, providerName)
}
diff --git a/internal/service/model_service.go b/internal/service/model_service.go
index 0dda834656..8689012e6f 100644
--- a/internal/service/model_service.go
+++ b/internal/service/model_service.go
@@ -2482,12 +2482,13 @@ func (m *ModelProviderService) getModelInstanceAndProviderByName(ctx context.Con
return nil, permission.ErrResourceNotFound
}
- providerInfo := dao.GetModelProviderManager().FindProvider(providerEntity.ProviderName)
+ catalogName := modelModule.CatalogProviderName(providerEntity.ProviderName)
+ providerInfo := dao.GetModelProviderManager().FindProvider(catalogName)
if providerInfo == nil {
return nil, errors.New("provider not found")
}
- modelInfo, err := dao.GetModelProviderManager().GetModelByName(providerEntity.ProviderName, *modelName)
+ modelInfo, err := dao.GetModelProviderManager().GetModelByName(catalogName, *modelName)
if err != nil {
return nil, fmt.Errorf("provider %s model %s not found", providerEntity.ProviderName, *modelName)
}
@@ -2550,12 +2551,13 @@ func (m *ModelProviderService) getModelInstanceAndProviderByID(ctx context.Conte
return nil, err
}
- providerInfo := dao.GetModelProviderManager().FindProvider(providerEntity.ProviderName)
+ catalogName := modelModule.CatalogProviderName(providerEntity.ProviderName)
+ providerInfo := dao.GetModelProviderManager().FindProvider(catalogName)
if providerInfo == nil {
return nil, errors.New("provider not found")
}
- modelInfo, err := dao.GetModelProviderManager().GetModelByName(providerEntity.ProviderName, modelEntity.ModelName)
+ modelInfo, err := dao.GetModelProviderManager().GetModelByName(catalogName, modelEntity.ModelName)
if err != nil {
return nil, errors.New(fmt.Sprintf("provider %s model %s not found", providerEntity.ProviderName, modelEntity.ModelName))
}
diff --git a/internal/service/model_stored_provider_name_test.go b/internal/service/model_stored_provider_name_test.go
new file mode 100644
index 0000000000..52c45fa7b7
--- /dev/null
+++ b/internal/service/model_stored_provider_name_test.go
@@ -0,0 +1,166 @@
+//
+// 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 service
+
+import (
+ "testing"
+
+ "gorm.io/gorm"
+
+ "ragflow/internal/common"
+ "ragflow/internal/entity"
+)
+
+// mistralOCRStoredKey is the api_key Python's "Mistral OCR" factory stored when
+// provisioned from the environment; the model migration copies it verbatim.
+const mistralOCRStoredKey = `{"MISTRAL_OCR_API_KEY": "sk-env", "MISTRAL_OCR_BASE_URL": "https://api.mistral.ai/v1"}`
+
+// seedStoredMistralOCRProvider seeds a tenant whose provider row carries the
+// "Mistral OCR" name, with mistral-ocr-latest enrolled as an ocr model.
+// mistral-ocr-2512 is left unenrolled so composite references to it resolve
+// through the catalog.
+func seedStoredMistralOCRProvider(t *testing.T, db *gorm.DB) {
+ t.Helper()
+ activeStatus := "1"
+ rows := []interface{}{
+ &entity.UserTenant{ID: "user-tenant-1", UserID: "user-1", TenantID: "tenant-1", Role: "owner", InvitedBy: "user-1", Status: &activeStatus},
+ &entity.TenantModelProvider{ID: "provider-mistral-ocr", TenantID: "tenant-1", ProviderName: "Mistral OCR"},
+ &entity.TenantModelInstance{ID: "instance-mistral-ocr", ProviderID: "provider-mistral-ocr", InstanceName: "default", APIKey: mistralOCRStoredKey, Status: "active", Extra: "{}"},
+ &entity.TenantModel{ID: "model-mistral-ocr", ProviderID: "provider-mistral-ocr", InstanceID: "instance-mistral-ocr", ModelName: "mistral-ocr-latest", ModelType: int(entity.ModelTypeOCR), Status: "active"},
+ }
+ for _, row := range rows {
+ if err := db.Create(row).Error; err != nil {
+ t.Fatalf("failed to seed %T: %v", row, err)
+ }
+ }
+}
+
+// Model references stored under the "Mistral OCR" provider name resolve to the
+// Mistral driver and catalog entry, whether they name the tenant model by id
+// or by composite, enrolled or catalog-only. The target keeps the stored
+// provider name and the stored key.
+func TestModelFactoryResolvesStoredMistralOCRProvider(t *testing.T) {
+ db := setupModelProviderServiceTestDB(t)
+ useModelProviderServiceTestDB(t, db)
+ seedStoredMistralOCRProvider(t, db)
+
+ access := ModelAccess{TenantID: "tenant-1", UserID: "user-1"}
+ for _, tc := range []struct {
+ name string
+ modelRef string
+ wantModel string
+ }{
+ {name: "tenant model id", modelRef: "model-mistral-ocr", wantModel: "mistral-ocr-latest"},
+ {name: "enrolled composite", modelRef: "mistral-ocr-latest@default@Mistral OCR", wantModel: "mistral-ocr-latest"},
+ {name: "catalog composite", modelRef: "mistral-ocr-2512@default@Mistral OCR", wantModel: "mistral-ocr-2512"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ target, err := NewModelFactory().resolveConfig(t.Context(), access, entity.ModelTypeOCR, tc.modelRef)
+ if err != nil {
+ t.Fatalf("resolveConfig(%q): %v", tc.modelRef, err)
+ }
+ if target.Driver == nil || target.Driver.Name() != "mistral" {
+ t.Fatalf("driver = %v, want the mistral driver", target.Driver)
+ }
+ if target.ModelName != tc.wantModel || target.ModelInfo == nil || !target.ModelInfo.ModelTypeMap["ocr"] {
+ t.Fatalf("model = %q / %+v, want %s from the catalog", target.ModelName, target.ModelInfo, tc.wantModel)
+ }
+ if target.ProviderName != "Mistral OCR" {
+ t.Fatalf("provider name = %q, want the stored name", target.ProviderName)
+ }
+ if target.APIConfig == nil || target.APIConfig.ApiKey == nil || *target.APIConfig.ApiKey != mistralOCRStoredKey {
+ t.Fatalf("api key = %v, want the stored key", target.APIConfig)
+ }
+ })
+ }
+}
+
+// ResolveModelType reads the catalog types of a composite reference that is
+// not enrolled under the "Mistral OCR" provider row.
+func TestModelFactoryResolveModelTypeForStoredMistralOCRProvider(t *testing.T) {
+ db := setupModelProviderServiceTestDB(t)
+ useModelProviderServiceTestDB(t, db)
+ seedStoredMistralOCRProvider(t, db)
+
+ types, err := NewModelFactory().ResolveModelType(t.Context(), ModelAccess{TenantID: "tenant-1", UserID: "user-1"}, "mistral-ocr-2512@default@Mistral OCR")
+ if err != nil {
+ t.Fatalf("ResolveModelType: %v", err)
+ }
+ if len(types) != 1 || types[0] != entity.ModelTypeOCR {
+ t.Fatalf("types = %v, want [ocr]", types)
+ }
+}
+
+// Tool support falls back to the catalog entry of the mapped provider.
+func TestCatalogToolSupportForStoredMistralOCRProvider(t *testing.T) {
+ if !catalogToolSupport("Mistral", "mistral-large-latest") {
+ t.Skip("Mistral catalog no longer lists tool support for mistral-large-latest")
+ }
+ if !catalogToolSupport("Mistral OCR", "mistral-large-latest") {
+ t.Fatal(`catalogToolSupport("Mistral OCR", "mistral-large-latest") = false, want the Mistral catalog value`)
+ }
+}
+
+// The model call service resolves a "Mistral OCR" model by tenant model id and
+// by composite reference.
+func TestModelCallServiceResolvesStoredMistralOCRProvider(t *testing.T) {
+ db := setupModelProviderServiceTestDB(t)
+ useModelProviderServiceTestDB(t, db)
+ seedStoredMistralOCRProvider(t, db)
+
+ svc := NewModelCallService()
+ for _, modelRef := range []string{"model-mistral-ocr", "mistral-ocr-latest@default@Mistral OCR"} {
+ t.Run(modelRef, func(t *testing.T) {
+ info, err := svc.resolveModelInfo(t.Context(), modelRef, "user-1")
+ if err != nil {
+ t.Fatalf("resolveModelInfo(%q): %v", modelRef, err)
+ }
+ if info.ProviderInfo == nil || info.ProviderInfo.Name != "Mistral" {
+ t.Fatalf("provider info = %+v, want the Mistral catalog entry", info.ProviderInfo)
+ }
+ if info.ModelInfo == nil || info.ModelInfo.Name != "mistral-ocr-latest" {
+ t.Fatalf("model info = %+v, want mistral-ocr-latest", info.ModelInfo)
+ }
+ if info.ProviderEntity == nil || info.ProviderEntity.ProviderName != "Mistral OCR" {
+ t.Fatalf("provider entity = %+v, want the stored row", info.ProviderEntity)
+ }
+ })
+ }
+}
+
+// The mapping is limited to model resolution: the provider list still skips a
+// "Mistral OCR" row, as it does for any name the catalog does not serve.
+func TestListProvidersOfTenantSkipsStoredMistralOCRProvider(t *testing.T) {
+ db := setupModelProviderServiceTestDB(t)
+ useModelProviderServiceTestDB(t, db)
+ seedStoredMistralOCRProvider(t, db)
+ if err := db.Create(&entity.TenantModelProvider{ID: "provider-openai", TenantID: "tenant-1", ProviderName: "OpenAI"}).Error; err != nil {
+ t.Fatalf("create provider: %v", err)
+ }
+
+ providers, code, err := NewModelProviderService().ListProvidersOfTenant(t.Context(), "user-1")
+ if err != nil || code != common.CodeSuccess {
+ t.Fatalf("ListProvidersOfTenant = (%v, %v)", code, err)
+ }
+ var names []string
+ for _, provider := range providers {
+ names = append(names, provider["name"].(string))
+ }
+ if len(names) != 1 || names[0] != "OpenAI" {
+ t.Fatalf("providers = %v, want only OpenAI", names)
+ }
+}