Skip to content

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

Apply datasets parser config to all documents (#21590)

Wang Qi1ab7dbf5a830
diff --git a/docs/references/http_api_reference.md b/docs/references/http_api_reference.md
index 02e8476eb5..04aedffa10 100644
--- a/docs/references/http_api_reference.md
+++ b/docs/references/http_api_reference.md
@@ -1123,6 +1123,45 @@ Failure:
---
+### Apply dataset parser settings to existing documents
+
+**POST** `/api/v1/datasets/{dataset_id}/documents/apply-parser-config`
+
+Applies the dataset's saved parser settings to all its existing documents. Save the dataset first with `PUT /api/v1/datasets/{dataset_id}`. This operation requires dataset update permission, takes no request body, and reads the current saved settings on the server.
+
+File-specific parser overrides are replaced. Parser selection and pipeline version are updated together with `parser_config`, using the same file-type resolution as uploads (including dedicated image, audio, presentation, and email parsers). A document assigned to a custom pipeline inherits the dataset's version pin; built-in parsers have no version pin.
+
+Existing chunks, indexes, metadata values, counters, and parsing progress are preserved. This endpoint does not start parsing or delete prior results. Manually re-parse the files to regenerate their content with the saved settings.
+
+All document changes are committed in one transaction. If any document has a queued, running, stopping, or unrecognized ingestion status, the operation fails with response body code `409` and no document changes are committed. Stop parsing or wait for completion before retrying. A database failure also rolls back all document updates. The dataset settings saved by the preceding PUT remain saved if this operation fails.
+
+Repeating this operation against unchanged dataset settings leaves already matching documents unchanged. An empty dataset succeeds with all counts set to zero.
+
+#### Request example
+
+```bash
+curl --request POST \
+ --url http://{address}/api/v1/datasets/{dataset_id}/documents/apply-parser-config \
+ --header 'Authorization: Bearer <YOUR_API_KEY>'
+```
+
+#### Response example
+
+```json
+{
+ "code": 0,
+ "message": "Parser settings applied. Re-parse files manually to update their content.",
+ "data": {
+ "dataset_id": "...",
+ "total_documents": 120,
+ "updated_documents": 115,
+ "unchanged_documents": 5
+ }
+}
+```
+
+---
+
### Update document
**PATCH** `/api/v1/datasets/{dataset_id}/documents/{document_id}`
diff --git a/internal/handler/document.go b/internal/handler/document.go
index 85274d36c8..8254fb92f6 100644
--- a/internal/handler/document.go
+++ b/internal/handler/document.go
@@ -74,6 +74,7 @@ type documentServiceIface interface {
UploadEmptyDocument(ctx context.Context, kb *entity.Knowledgebase, tenantID, name string) (map[string]interface{}, common.ErrorCode, error)
DownloadDocument(ctx context.Context, datasetID, docID string) (*document.DownloadDocumentResp, error)
UpdateDatasetDocument(ctx context.Context, userID, datasetID, documentID string, req *document.UpdateDatasetDocumentRequest, present map[string]bool) (*document.UpdateDatasetDocumentResponse, common.ErrorCode, error)
+ ApplyDatasetParserConfig(ctx context.Context, userID, datasetID string) (*document.ApplyDatasetParserConfigResponse, common.ErrorCode, error)
BatchUpdateDocumentMetadatas(ctx context.Context, datasetID string, selector *document.MetadataSelector, updates []document.MetadataUpdate, deletes []document.MetadataDelete) (*document.BatchUpdateMetadatasResponse, common.ErrorCode, error)
ListIngestionTasks(ctx context.Context, userID string, datasetID *string, page, pageSize int) ([]*entity.IngestionTask, error)
IngestDocuments(ctx context.Context, datasetID, userID string, docIDs []string) ([]*service.ParseDocumentResponse, error)
diff --git a/internal/handler/document_apply_parser_config.go b/internal/handler/document_apply_parser_config.go
new file mode 100644
index 0000000000..92ef42d441
--- /dev/null
+++ b/internal/handler/document_apply_parser_config.go
@@ -0,0 +1,29 @@
+package handler
+
+import (
+ "strings"
+
+ "ragflow/internal/common"
+
+ "github.com/gin-gonic/gin"
+)
+
+// ApplyDatasetParserConfig handles POST /api/v1/datasets/:dataset_id/documents/apply-parser-config.
+func (h *DocumentHandler) ApplyDatasetParserConfig(c *gin.Context) {
+ user, code, message := GetUser(c)
+ if code != common.CodeSuccess {
+ common.ErrorWithCode(c, code, message)
+ return
+ }
+ datasetID := strings.TrimSpace(c.Param("dataset_id"))
+ if datasetID == "" {
+ common.ResponseWithCodeData(c, common.CodeArgumentError, nil, "dataset_id is required")
+ return
+ }
+ result, code, err := h.documentService.ApplyDatasetParserConfig(c.Request.Context(), user.ID, datasetID)
+ if err != nil {
+ common.ErrorWithCode(c, code, err.Error())
+ return
+ }
+ common.SuccessWithData(c, result, "Parser settings applied. Re-parse files manually to update their content.")
+}
diff --git a/internal/handler/document_apply_parser_config_test.go b/internal/handler/document_apply_parser_config_test.go
new file mode 100644
index 0000000000..fe34f6e261
--- /dev/null
+++ b/internal/handler/document_apply_parser_config_test.go
@@ -0,0 +1,88 @@
+package handler
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "net/http"
+ "net/http/httptest"
+ "testing"
+
+ "ragflow/internal/common"
+ "ragflow/internal/entity"
+ "ragflow/internal/service/document"
+
+ "github.com/gin-gonic/gin"
+)
+
+func (f *fakeDocumentService) ApplyDatasetParserConfig(ctx context.Context, userID, datasetID string) (*document.ApplyDatasetParserConfigResponse, common.ErrorCode, error) {
+ return &document.ApplyDatasetParserConfigResponse{DatasetID: datasetID}, common.CodeSuccess, nil
+}
+
+type applyParserConfigService struct {
+ fakeDocumentService
+ called bool
+ code common.ErrorCode
+ err error
+ ctx context.Context
+}
+
+func (s *applyParserConfigService) ApplyDatasetParserConfig(ctx context.Context, userID, datasetID string) (*document.ApplyDatasetParserConfigResponse, common.ErrorCode, error) {
+ s.called = true
+ s.ctx = ctx
+ if userID != "user-1" || datasetID != "kb-1" {
+ return nil, common.CodeArgumentError, errors.New("wrong user or dataset")
+ }
+ return &document.ApplyDatasetParserConfigResponse{DatasetID: datasetID, TotalDocuments: 3, UpdatedDocuments: 2, UnchangedDocuments: 1}, s.code, s.err
+}
+
+func TestApplyDatasetParserConfigHandler(t *testing.T) {
+ gin.SetMode(gin.TestMode)
+ for _, tc := range []struct {
+ name string
+ auth bool
+ code common.ErrorCode
+ err error
+ }{
+ {"success without body", true, common.CodeSuccess, nil},
+ {"active ingestion", true, common.CodeConflict, errors.New("stop parsing before applying settings")},
+ {"unauthenticated", false, common.CodeSuccess, nil},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ svc := &applyParserConfigService{code: tc.code, err: tc.err}
+ h := NewDocumentHandler(svc, nil, nil)
+ r := gin.New()
+ r.POST("/api/v1/datasets/:dataset_id/documents/apply-parser-config", func(c *gin.Context) {
+ if tc.auth {
+ c.Set("user", &entity.User{ID: "user-1"})
+ }
+ h.ApplyDatasetParserConfig(c)
+ })
+ req := httptest.NewRequest(http.MethodPost, "/api/v1/datasets/kb-1/documents/apply-parser-config", nil)
+ rec := httptest.NewRecorder()
+ r.ServeHTTP(rec, req)
+ var body struct {
+ Code common.ErrorCode `json:"code"`
+ Data *document.ApplyDatasetParserConfigResponse `json:"data"`
+ }
+ if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
+ t.Fatal(err)
+ }
+ if svc.called != tc.auth {
+ t.Fatalf("called=%t auth=%t", svc.called, tc.auth)
+ }
+ if !tc.auth {
+ if body.Code == common.CodeSuccess {
+ t.Fatal("unauthenticated request succeeded")
+ }
+ return
+ }
+ if body.Code != tc.code || svc.ctx != req.Context() {
+ t.Fatalf("response=%s context propagated=%t", rec.Body.String(), svc.ctx == req.Context())
+ }
+ if tc.err == nil && (body.Data == nil || body.Data.UpdatedDocuments != 2 || body.Data.UnchangedDocuments != 1) {
+ t.Fatalf("response=%s", rec.Body.String())
+ }
+ })
+ }
+}
diff --git a/internal/router/router.go b/internal/router/router.go
index 070389eed0..50c3f725cc 100644
--- a/internal/router/router.go
+++ b/internal/router/router.go
@@ -344,6 +344,7 @@ func (r *Router) Setup(engine *gin.Engine) {
datasets.GET("/:dataset_id/graph", r.datasetsHandler.GetKnowledgeGraph)
datasets.POST("/:dataset_id/embedding/check", r.datasetsHandler.CheckEmbedding)
datasets.POST("/:dataset_id/documents/batch-update-status", r.documentHandler.BatchUpdateDocumentStatus)
+ datasets.POST("/:dataset_id/documents/apply-parser-config", r.documentHandler.ApplyDatasetParserConfig)
// Scheduler compile-status contract; replaces the retired
// RunIndex/TraceIndex/DeleteIndex /index routes.
datasets.GET("/:dataset_id/compilation/status", r.datasetsHandler.GetCompilationStatus)
diff --git a/internal/router/router_test.go b/internal/router/router_test.go
index 4682188ced..1e7baf0c3e 100644
--- a/internal/router/router_test.go
+++ b/internal/router/router_test.go
@@ -177,6 +177,22 @@ func TestRouterSetupRegistersUpdateDatasetRoute(t *testing.T) {
}
}
+func TestRouterSetupRegistersApplyDatasetParserConfigRoute(t *testing.T) {
+ gin.SetMode(gin.TestMode)
+ engine := gin.New()
+ r := &Router{
+ authHandler: handler.NewAuthHandler(),
+ datasetsHandler: handler.NewDatasetsHandler(nil, nil),
+ }
+ r.Setup(engine)
+ resp := httptest.NewRecorder()
+ req := httptest.NewRequest(http.MethodPost, "/api/v1/datasets/dataset-1/documents/apply-parser-config", nil)
+ engine.ServeHTTP(resp, req)
+ if resp.Code != http.StatusUnauthorized {
+ t.Fatalf("status=%d body=%s; want auth middleware to handle the registered route", resp.Code, resp.Body.String())
+ }
+}
+
func TestRouterSetupRegistersSearchbotMindMapRoute(t *testing.T) {
gin.SetMode(gin.TestMode)
diff --git a/internal/service/document/document_apply_parser_config.go b/internal/service/document/document_apply_parser_config.go
new file mode 100644
index 0000000000..1aa3725eeb
--- /dev/null
+++ b/internal/service/document/document_apply_parser_config.go
@@ -0,0 +1,175 @@
+package document
+
+import (
+ "bytes"
+ "context"
+ "encoding/json"
+ "fmt"
+ "strings"
+
+ "ragflow/internal/common"
+ "ragflow/internal/dao"
+ "ragflow/internal/entity"
+ componentpkg "ragflow/internal/ingestion/component"
+ "ragflow/internal/permission"
+ permissionresponse "ragflow/internal/permission/response"
+ "ragflow/internal/service"
+ "ragflow/internal/utility"
+
+ "gorm.io/gorm"
+ "gorm.io/gorm/clause"
+)
+
+type ApplyDatasetParserConfigResponse struct {
+ DatasetID string `json:"dataset_id"`
+ TotalDocuments int64 `json:"total_documents"`
+ UpdatedDocuments int64 `json:"updated_documents"`
+ UnchangedDocuments int64 `json:"unchanged_documents"`
+}
+
+// ApplyDatasetParserConfig resets document parser settings without changing
+// parsed content or scheduling ingestion. Dataset grants cover its documents.
+func (s *DocumentService) ApplyDatasetParserConfig(ctx context.Context, userID, datasetID string) (*ApplyDatasetParserConfigResponse, common.ErrorCode, error) {
+ datasetID = strings.TrimSpace(datasetID)
+ if datasetID == "" {
+ return nil, common.CodeArgumentError, fmt.Errorf("dataset_id is required")
+ }
+ result := &ApplyDatasetParserConfigResponse{DatasetID: datasetID}
+ code := common.CodeServerError
+ err := dao.DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
+ var kb entity.Knowledgebase
+ if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
+ Where("id = ? AND status = ?", datasetID, string(entity.StatusValid)).Take(&kb).Error; err != nil {
+ if dao.IsNotFoundErr(err) {
+ var normalized error
+ code, normalized = permissionresponse.NormalizeHidden(permission.ErrResourceNotFound)
+ return normalized
+ }
+ return err
+ }
+ checker := permission.NewDatabaseChecker(tx)
+ subject := permission.Subject{UserID: userID, TenantID: kb.TenantID}
+ ref := permission.ResourceRef{Kind: permission.ResourceKindDataset, ID: datasetID}
+ if err := checker.CheckResource(ctx, subject, ref, permission.OperationUpdate); err != nil {
+ var normalized error
+ code, normalized = permissionresponse.NormalizeHidden(err)
+ return normalized
+ }
+ if kb.PipelineID != nil && strings.TrimSpace(*kb.PipelineID) != "" {
+ if err := checker.CheckDependency(ctx, subject, ref,
+ permission.ResourceRef{Kind: permission.ResourceKindCanvas, ID: *kb.PipelineID},
+ permission.OperationUpdate, permission.OperationUse); err != nil {
+ var normalized error
+ code, normalized = permissionresponse.Normalize(err)
+ return normalized
+ }
+ }
+ selection := service.CurrentSelection(kb.ParserID, kb.PipelineID)
+ isPipeline, parserID, pipelineID := selection.Effective()
+ if _, err := service.LoadPipelineDSL(ctx, isPipeline, parserID, pipelineID, kb.PipelineVersionID); err != nil {
+ code = common.CodeDataError
+ return err
+ }
+
+ type parserSettings struct {
+ parserID string
+ pipelineID *string
+ versionID *string
+ config entity.JSONMap
+ encoded []byte
+ }
+ // Resolution depends on file family and its dedicated parser, not file
+ // identity. Keep one settings snapshot per family for the entire request.
+ settingsByFamily := make(map[string]*parserSettings)
+ lastID := ""
+ for {
+ var docs []entity.Document
+ if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
+ Where("kb_id = ? AND id > ?", datasetID, lastID).
+ Order("id").Limit(200).Find(&docs).Error; err != nil {
+ return err
+ }
+ if len(docs) == 0 {
+ break
+ }
+ ids := make([]string, len(docs))
+ for i := range docs {
+ ids[i] = docs[i].ID
+ }
+ // Lock in document -> task order, matching ingestion run creation.
+ // A new run cannot be published until its document lock is released.
+ var tasks []entity.IngestionTask
+ if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
+ Where("document_id IN ?", ids).Order("document_id").Find(&tasks).Error; err != nil {
+ return err
+ }
+ for _, task := range tasks {
+ if !common.IsTerminalTaskStatus(task.Status) {
+ code = common.CodeConflict
+ return fmt.Errorf("document %s has ingestion status %q; stop parsing or wait for it to finish before applying parser settings", task.DocumentID, task.Status)
+ }
+ }
+ changedByFamily := make(map[string][]string)
+ for _, doc := range docs {
+ name := "file." + doc.Suffix
+ if doc.Name != nil {
+ name = *doc.Name
+ }
+ filetype := utility.FileType(doc.Type)
+ family := componentpkg.ParserFileFamily(utility.GetFileExtension(name))
+ key := family + ":" + dedicatedDocumentParserID(filetype, name)
+ settings := settingsByFamily[key]
+ if settings == nil {
+ sel, config := resolveDocumentParser(ctx, &kb, name, filetype, cloneParserConfigForDocument(kb.ParserConfig))
+ parserID, pipelineID := parserSelectionColumns(sel)
+ var versionID *string
+ if sel.IsPipeline() {
+ versionID = kb.PipelineVersionID
+ }
+ if declared, found := configDeclaredFamily(config, family); family != "" && found && !declared {
+ code = common.CodeDataError
+ return fmt.Errorf("could not resolve parser settings for file type %q", family)
+ }
+ encoded, err := json.Marshal(config)
+ if err != nil {
+ return err
+ }
+ settings = &parserSettings{parserID, pipelineID, versionID, config, encoded}
+ settingsByFamily[key] = settings
+ }
+ encoded, err := json.Marshal(doc.ParserConfig)
+ if err != nil {
+ return err
+ }
+ result.TotalDocuments++
+ if doc.ParserID == settings.parserID && sameOptionalString(doc.PipelineID, settings.pipelineID) &&
+ sameOptionalString(doc.PipelineVersionID, settings.versionID) && bytes.Equal(encoded, settings.encoded) {
+ result.UnchangedDocuments++
+ continue
+ }
+ changedByFamily[key] = append(changedByFamily[key], doc.ID)
+ }
+ for key, ids := range changedByFamily {
+ settings := settingsByFamily[key]
+ if err := tx.Model(&entity.Document{}).Where("kb_id = ? AND id IN ?", datasetID, ids).
+ Updates(map[string]any{
+ "parser_id": settings.parserID, "pipeline_id": settings.pipelineID,
+ "pipeline_version_id": settings.versionID, "parser_config": settings.config,
+ }).Error; err != nil {
+ return err
+ }
+ result.UpdatedDocuments += int64(len(ids))
+ }
+ lastID = docs[len(docs)-1].ID
+ }
+ return nil
+ })
+ if err != nil {
+ return nil, code, err
+ }
+ return result, common.CodeSuccess, nil
+}
+
+func sameOptionalString(a, b *string) bool {
+ return a == nil && b == nil || a != nil && b != nil && *a == *b
+}
diff --git a/internal/service/document/document_apply_parser_config_test.go b/internal/service/document/document_apply_parser_config_test.go
new file mode 100644
index 0000000000..083c8849f2
--- /dev/null
+++ b/internal/service/document/document_apply_parser_config_test.go
@@ -0,0 +1,231 @@
+package document
+
+import (
+ "context"
+ "fmt"
+ "reflect"
+ "strings"
+ "testing"
+
+ "ragflow/internal/common"
+ "ragflow/internal/dao"
+ "ragflow/internal/entity"
+)
+
+func TestApplyDatasetParserConfigReplacesSettingsAndPreservesContent(t *testing.T) {
+ db := setupConcurrentServiceTestDB(t)
+ pushServiceDB(t, db)
+ insertTestKB(t, "kb-1", "tenant-1", 205, 42, 7)
+ insertTestKB(t, "kb-other", "tenant-1", 1, 0, 0)
+ config := generalParserConfig()
+ config["GeneralChunker:SixApplesFall"] = map[string]any{"chunk_token_size": 256}
+ if err := db.Model(&entity.Knowledgebase{}).Where("id = ?", "kb-1").Updates(map[string]any{
+ "parser_id": "general", "parser_config": config,
+ }).Error; err != nil {
+ t.Fatal(err)
+ }
+ for i := range 205 {
+ insertNamedTestDoc(t, fmt.Sprintf("doc-%03d", i), "kb-1", "report.txt", 42, 7)
+ }
+ insertNamedTestDoc(t, "other", "kb-other", "report.txt", 0, 0)
+ if err := db.Model(&entity.Document{}).Where("kb_id = ?", "kb-1").Updates(map[string]any{
+ "parser_config": entity.JSONMap{"FileOnly:Old": map[string]any{"value": "remove"}},
+ "pipeline_version_id": "old-version", "thumbnail": "keep-thumbnail",
+ "progress_msg": "keep-progress", "content_hash": "keep-hash",
+ }).Error; err != nil {
+ t.Fatal(err)
+ }
+ before, err := dao.NewDocumentDAO().GetByID(t.Context(), db, "doc-000")
+ if err != nil {
+ t.Fatal(err)
+ }
+ svc := testDocumentService(t)
+ result, code, err := svc.ApplyDatasetParserConfig(documentTestContext(t.Context()), "user-1", "kb-1")
+ if err != nil || code != common.CodeSuccess {
+ t.Fatalf("apply: code=%d err=%v", code, err)
+ }
+ if result.TotalDocuments != 205 || result.UpdatedDocuments != 205 || result.UnchangedDocuments != 0 {
+ t.Fatalf("unexpected counts: %+v", result)
+ }
+ var docs []entity.Document
+ if err := db.Where("kb_id = ?", "kb-1").Find(&docs).Error; err != nil {
+ t.Fatal(err)
+ }
+ kb, err := dao.NewKnowledgebaseDAO().GetByID(t.Context(), db, "kb-1")
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, doc := range docs {
+ if doc.ParserID != "general" || doc.PipelineID != nil || doc.PipelineVersionID != nil || !reflect.DeepEqual(doc.ParserConfig, kb.ParserConfig) {
+ t.Fatalf("document settings differ from dataset: %+v", doc)
+ }
+ if doc.ChunkNum != before.ChunkNum || doc.TokenNum != before.TokenNum || doc.Progress != before.Progress ||
+ *doc.Thumbnail != *before.Thumbnail || *doc.ProgressMsg != *before.ProgressMsg || *doc.ContentHash != *before.ContentHash {
+ t.Fatalf("parsed content changed: %+v", doc)
+ }
+ }
+ other, err := dao.NewDocumentDAO().GetByID(t.Context(), db, "other")
+ if err != nil || other.ParserID != "naive" || len(other.ParserConfig) != 0 {
+ t.Fatalf("other dataset changed: doc=%+v err=%v", other, err)
+ }
+ result, code, err = svc.ApplyDatasetParserConfig(documentTestContext(t.Context()), "user-1", "kb-1")
+ if err != nil || code != common.CodeSuccess || result.UpdatedDocuments != 0 || result.UnchangedDocuments != 205 {
+ t.Fatalf("repeat: result=%+v code=%d err=%v", result, code, err)
+ }
+ var taskCount int64
+ if err := db.Model(&entity.IngestionTask{}).Count(&taskCount).Error; err != nil || taskCount != 0 {
+ t.Fatalf("apply scheduled ingestion: count=%d err=%v", taskCount, err)
+ }
+}
+
+func TestApplyDatasetParserConfigRollsBackAllBatches(t *testing.T) {
+ for _, failure := range []string{common.CREATED, common.SCHEDULED, common.RUNNING, common.STOPPING, "unknown", "write failure"} {
+ t.Run(failure, func(t *testing.T) {
+ db := setupConcurrentServiceTestDB(t)
+ pushServiceDB(t, db)
+ insertTestKB(t, "kb-1", "tenant-1", 201, 0, 0)
+ if err := db.Model(&entity.Knowledgebase{}).Where("id = ?", "kb-1").Updates(map[string]any{
+ "parser_id": "general", "parser_config": generalParserConfig(),
+ }).Error; err != nil {
+ t.Fatal(err)
+ }
+ for i := range 201 {
+ insertNamedTestDoc(t, fmt.Sprintf("doc-%03d", i), "kb-1", "report.txt", 0, 0)
+ }
+ wantCode := common.CodeConflict
+ if failure == "write failure" {
+ wantCode = common.CodeServerError
+ if err := db.Exec(`CREATE TRIGGER reject_parser_update BEFORE UPDATE OF parser_config ON document
+ WHEN OLD.id = 'doc-200' BEGIN SELECT RAISE(ABORT, 'write failure'); END`).Error; err != nil {
+ t.Fatal(err)
+ }
+ } else {
+ insertTestIngestionTaskWithStatus(t, "task-1", "user-1", "doc-200", "kb-1", failure)
+ }
+ result, code, err := testDocumentService(t).ApplyDatasetParserConfig(documentTestContext(t.Context()), "user-1", "kb-1")
+ if err == nil || code != wantCode || result != nil {
+ t.Fatalf("result=%+v code=%d err=%v", result, code, err)
+ }
+ var changed int64
+ if err := db.Model(&entity.Document{}).Where("parser_id <> ?", "naive").Count(&changed).Error; err != nil || changed != 0 {
+ t.Fatalf("partial updates survived rollback: count=%d err=%v", changed, err)
+ }
+ })
+ }
+}
+
+func TestApplyDatasetParserConfigResolvesMixedFileTypes(t *testing.T) {
+ db := setupConcurrentServiceTestDB(t)
+ pushServiceDB(t, db)
+ insertTestKB(t, "kb-1", "tenant-1", 3, 0, 0)
+ if err := db.Model(&entity.Knowledgebase{}).Where("id = ?", "kb-1").Updates(map[string]any{
+ "parser_id": "manual", "parser_config": entity.JSONMap{
+ "Parser:HipSignsRhyme": map[string]any{"pdf": map[string]any{}, "text&code": map[string]any{}},
+ },
+ }).Error; err != nil {
+ t.Fatal(err)
+ }
+ files := []struct{ id, name, filetype, parserID string }{
+ {"text", "notes.txt", "doc", "manual"},
+ {"image", "photo.png", "visual", "picture"},
+ {"slides", "deck.pptx", "doc", "presentation"},
+ }
+ for _, f := range files {
+ insertNamedTestDoc(t, f.id, "kb-1", f.name, 0, 0)
+ if err := db.Model(&entity.Document{}).Where("id = ?", f.id).Update("type", f.filetype).Error; err != nil {
+ t.Fatal(err)
+ }
+ }
+ _, code, err := testDocumentService(t).ApplyDatasetParserConfig(documentTestContext(t.Context()), "user-1", "kb-1")
+ if err != nil || code != common.CodeSuccess {
+ t.Fatalf("apply: code=%d err=%v", code, err)
+ }
+ for _, f := range files {
+ doc, err := dao.NewDocumentDAO().GetByID(t.Context(), db, f.id)
+ if err != nil || doc.ParserID != f.parserID {
+ t.Fatalf("file=%s parser=%s err=%v", f.name, doc.ParserID, err)
+ }
+ }
+}
+
+func TestApplyDatasetParserConfigAuthorizationAndEmptyDataset(t *testing.T) {
+ db := setupConcurrentServiceTestDB(t)
+ pushServiceDB(t, db)
+ insertTestKB(t, "kb-1", "tenant-1", 0, 0, 0)
+ if err := db.Model(&entity.Knowledgebase{}).Where("id = ?", "kb-1").Updates(map[string]any{
+ "parser_id": "general", "parser_config": generalParserConfig(),
+ }).Error; err != nil {
+ t.Fatal(err)
+ }
+ svc := testDocumentService(t)
+ for _, id := range []string{"kb-1", "missing"} {
+ result, code, err := svc.ApplyDatasetParserConfig(documentTestActorContext(t.Context(), "outsider", "tenant-other"), "outsider", id)
+ if err == nil || code == common.CodeSuccess || result != nil {
+ t.Fatalf("unauthorized apply: result=%+v code=%d err=%v", result, code, err)
+ }
+ }
+ result, code, err := svc.ApplyDatasetParserConfig(documentTestContext(t.Context()), "user-1", "kb-1")
+ if err != nil || code != common.CodeSuccess || result.TotalDocuments != 0 {
+ t.Fatalf("empty dataset: result=%+v code=%d err=%v", result, code, err)
+ }
+ ctx, cancel := context.WithCancel(documentTestContext(t.Context()))
+ cancel()
+ if _, _, err := svc.ApplyDatasetParserConfig(ctx, "user-1", "kb-1"); err == nil {
+ t.Fatal("canceled request succeeded")
+ }
+ if _, _, err := svc.ApplyDatasetParserConfig(t.Context(), "user-1", " "); err == nil || !strings.Contains(err.Error(), "dataset_id") {
+ t.Fatalf("empty dataset ID: %v", err)
+ }
+}
+
+func TestApplyDatasetParserConfigCopiesPipelineVersion(t *testing.T) {
+ for _, missingVersion := range []bool{false, true} {
+ t.Run(fmt.Sprintf("missing=%t", missingVersion), func(t *testing.T) {
+ db := setupConcurrentServiceTestDB(t)
+ pushServiceDB(t, db)
+ if err := db.AutoMigrate(&entity.UserCanvasVersion{}); err != nil {
+ t.Fatal(err)
+ }
+ insertTestKB(t, "kb-1", "tenant-1", 1, 0, 0)
+ insertNamedTestDoc(t, "doc-1", "kb-1", "report.txt", 0, 0)
+ const canvasID = "1234567890abcdef1234567890abcdef"
+ insertTestCanvas(t, canvasID, "user-1")
+ if err := db.Model(&entity.UserCanvas{}).Where("id = ?", canvasID).Update("canvas_category", "dataflow_canvas").Error; err != nil {
+ t.Fatal(err)
+ }
+ config := entity.JSONMap{"Parser:Shared": map[string]any{"text&code": map[string]any{}, "value": "saved"}}
+ if !missingVersion {
+ if err := db.Create(&entity.UserCanvasVersion{
+ ID: "v-new", UserCanvasID: canvasID,
+ DSL: entity.JSONMap{"components": map[string]any{
+ "Parser:Shared": map[string]any{"obj": map[string]any{
+ "component_name": "Parser", "params": config["Parser:Shared"],
+ }},
+ }},
+ }).Error; err != nil {
+ t.Fatal(err)
+ }
+ }
+ if err := db.Model(&entity.Knowledgebase{}).Where("id = ?", "kb-1").Updates(map[string]any{
+ "parser_id": "", "pipeline_id": canvasID, "pipeline_version_id": "v-new", "parser_config": config,
+ }).Error; err != nil {
+ t.Fatal(err)
+ }
+ _, code, err := testDocumentService(t).ApplyDatasetParserConfig(documentTestContext(t.Context()), "user-1", "kb-1")
+ doc, readErr := dao.NewDocumentDAO().GetByID(t.Context(), db, "doc-1")
+ if readErr != nil {
+ t.Fatal(readErr)
+ }
+ if missingVersion {
+ if err == nil || code == common.CodeSuccess || doc.ParserID != "naive" || doc.PipelineID != nil {
+ t.Fatalf("invalid version changed document: doc=%+v code=%d err=%v", doc, code, err)
+ }
+ return
+ }
+ if err != nil || code != common.CodeSuccess || doc.ParserID != "" || doc.PipelineID == nil || *doc.PipelineID != canvasID ||
+ doc.PipelineVersionID == nil || *doc.PipelineVersionID != "v-new" || !reflect.DeepEqual(doc.ParserConfig, config) {
+ t.Fatalf("pipeline settings not copied: doc=%+v code=%d err=%v", doc, code, err)
+ }
+ })
+ }
+}
diff --git a/web/src/hooks/use-apply-dataset-parser-config.ts b/web/src/hooks/use-apply-dataset-parser-config.ts
new file mode 100644
index 0000000000..649334a167
--- /dev/null
+++ b/web/src/hooks/use-apply-dataset-parser-config.ts
@@ -0,0 +1,26 @@
+import { IApplyDatasetParserConfigResult } from '@/interfaces/database/document';
+import { ResponseType } from '@/interfaces/database/base';
+import { applyDatasetParserConfig } from '@/services/knowledge-service';
+import { useMutation, useQueryClient } from '@tanstack/react-query';
+import { DocumentKeys } from './document-query-keys';
+
+export function useApplyDatasetParserConfig(datasetId: string) {
+ const queryClient = useQueryClient();
+
+ return useMutation({
+ mutationFn: async () => {
+ const { data }: { data: ResponseType<IApplyDatasetParserConfigResult> } =
+ await applyDatasetParserConfig(datasetId);
+ if (data.code !== 0) {
+ throw new Error(data.message);
+ }
+ return data.data;
+ },
+ onSuccess: async () => {
+ await Promise.all([
+ queryClient.invalidateQueries({ queryKey: DocumentKeys.all() }),
+ queryClient.invalidateQueries({ queryKey: DocumentKeys.allFilters() }),
+ ]);
+ },
+ });
+}
diff --git a/web/src/hooks/use-knowledge-request.ts b/web/src/hooks/use-knowledge-request.ts
index 2d9bfcac8e..b937c0ccbc 100644
--- a/web/src/hooks/use-knowledge-request.ts
+++ b/web/src/hooks/use-knowledge-request.ts
@@ -339,6 +339,14 @@ export const useDeleteKnowledge = () => {
return { data, loading, deleteKnowledge: mutateAsync };
};
+export const DatasetPipelineConfigurationKeys = {
+ detail: (knowledgeBaseId: string | null | undefined) =>
+ [
+ KnowledgeApiAction.FetchDatasetPipelineConfiguration,
+ knowledgeBaseId,
+ ] as const,
+};
+
export const useUpdateKnowledge = (shouldFetchList = false) => {
const knowledgeBaseId = useKnowledgeBaseId();
const queryClient = useQueryClient();
@@ -392,6 +400,9 @@ export const useUpdateKnowledge = (shouldFetchList = false) => {
const { data = {} } = await updateKb(kbId, requestBody);
if (data.code === 0) {
message.success(i18n.t(`message.updated`));
+ queryClient.invalidateQueries({
+ queryKey: DatasetPipelineConfigurationKeys.detail(kbId),
+ });
if (shouldFetchList) {
queryClient.invalidateQueries({
queryKey: [KnowledgeApiAction.FetchKnowledgeListByPage],
@@ -429,14 +440,6 @@ export const useFetchKnowledgeBaseConfiguration = (props?: {
return { data, loading };
};
-export const DatasetPipelineConfigurationKeys = {
- detail: (knowledgeBaseId: string | null | undefined) =>
- [
- KnowledgeApiAction.FetchDatasetPipelineConfiguration,
- knowledgeBaseId,
- ] as const,
-};
-
export const useFetchDatasetPipelineConfiguration = (props?: {
isEdit?: boolean;
}) => {
diff --git a/web/src/interfaces/database/document.ts b/web/src/interfaces/database/document.ts
index 973dcef148..d77644a136 100644
--- a/web/src/interfaces/database/document.ts
+++ b/web/src/interfaces/database/document.ts
@@ -1,6 +1,13 @@
import { IngestionTaskStatus, RunningStatus } from '@/constants/knowledge';
import { IngestionEventItem } from './ingestion';
+export interface IApplyDatasetParserConfigResult {
+ dataset_id: string;
+ total_documents: number;
+ updated_documents: number;
+ unchanged_documents: number;
+}
+
export interface IDocumentInfo {
// chunk_num: number;
create_date: string;
diff --git a/web/src/locales/en.ts b/web/src/locales/en.ts
index ea47099a3e..a068e95518 100644
--- a/web/src/locales/en.ts
+++ b/web/src/locales/en.ts
@@ -772,8 +772,13 @@ Example: A 1 KB message with 1024-dim embedding uses ~9 KB. The 5 MB default lim
parsingMode: 'Parsing mode',
manualSetup: 'Custom ingestion pipeline',
builtIn: 'Built-in parsing template',
+ applyParserSettingsTitle: 'Apply parser settings to existing files?',
parserSettingsChangeWarning:
- 'Updated parser settings apply only to newly uploaded files. To apply these settings to existing files, update their parser settings and re-parse them.',
+ 'Updated parser settings apply only to newly uploaded files by default. Choose “Yes” to also apply them to all existing files in this dataset, replacing any file-specific parser settings. You’ll need to manually re-parse those files for the changes to affect their content. Choose “No” to keep existing files’ settings unchanged.',
+ parserSettingsApplied:
+ 'Parser settings applied. Re-parse files manually to update their content.',
+ parserSettingsApplyFailed:
+ 'Dataset settings saved, but existing files were not updated. Choose “Yes” to retry.',
noConfigChunkerHint:
'The built-in {{name}} parser handles chunking automatically, so no additional configuration is required.',
titleDescription:
diff --git a/web/src/locales/zh.ts b/web/src/locales/zh.ts
index a916fa9eb3..51c88e4b06 100644
--- a/web/src/locales/zh.ts
+++ b/web/src/locales/zh.ts
@@ -708,8 +708,12 @@ export default {
parsingMode: '解析方式',
manualSetup: '自定义Ingestion pipeline',
builtIn: '内置解析模板',
+ applyParserSettingsTitle: '将解析设置应用到现有文件?',
parserSettingsChangeWarning:
- '更新后的解析设置仅对新上传的文件生效。如需对现有文件生效,请修改这些文件的解析设置并重新解析。',
+ '默认情况下,更新后的解析设置仅应用于新上传的文件。选择“是”可将设置同时应用于此知识库中的所有现有文件,并替换各文件的自定义解析设置。您仍需手动重新解析这些文件,设置才会对其内容生效。选择“否”可保留现有文件的解析设置。',
+ parserSettingsApplied: '解析设置已应用。请手动重新解析文件以更新其内容。',
+ parserSettingsApplyFailed:
+ '知识库设置已保存,但现有文件尚未更新。选择“是”可重试。',
noConfigChunkerHint:
'内置的 {{name}} 解析器会自动处理分块,因此无需额外配置。',
titleDescription: '在这里更新知识库配置,特别是大语言模型和提示词。',
diff --git a/web/src/pages/dataset/setting/apply-parser-config-dialog.tsx b/web/src/pages/dataset/setting/apply-parser-config-dialog.tsx
new file mode 100644
index 0000000000..4176441f23
--- /dev/null
+++ b/web/src/pages/dataset/setting/apply-parser-config-dialog.tsx
@@ -0,0 +1,83 @@
+import {
+ AlertDialog,
+ AlertDialogCancel,
+ AlertDialogContent,
+ AlertDialogDescription,
+ AlertDialogFooter,
+ AlertDialogHeader,
+ AlertDialogTitle,
+} from '@/components/ui/alert-dialog';
+import { ButtonLoading, buttonVariants } from '@/components/ui/button';
+import message from '@/components/ui/message';
+import { useApplyDatasetParserConfig } from '@/hooks/use-apply-dataset-parser-config';
+import { useTranslation } from 'react-i18next';
+
+type ApplyParserConfigDialogProps = {
+ datasetId: string;
+ open: boolean;
+ onOpenChange: (open: boolean) => void;
+};
+
+export function ApplyParserConfigDialog({
+ datasetId,
+ open,
+ onOpenChange,
+}: ApplyParserConfigDialogProps) {
+ const { t } = useTranslation();
+ const { mutateAsync, isPending, error, reset } =
+ useApplyDatasetParserConfig(datasetId);
+
+ const handleOpenChange = (nextOpen: boolean) => {
+ if (isPending) return;
+ reset();
+ onOpenChange(nextOpen);
+ };
+
+ const handleApply = async () => {
+ try {
+ await mutateAsync();
+ message.success(t('knowledgeConfiguration.parserSettingsApplied'));
+ onOpenChange(false);
+ } catch {
+ // Keep the saved-settings dialog open so Yes can retry the operation.
+ }
+ };
+
+ return (
+ <AlertDialog open={open} onOpenChange={handleOpenChange}>
+ <AlertDialogContent className="bg-bg-base border-border-default">
+ <AlertDialogHeader>
+ <AlertDialogTitle>
+ {t('knowledgeConfiguration.applyParserSettingsTitle')}
+ </AlertDialogTitle>
+ <AlertDialogDescription>
+ {t('knowledgeConfiguration.parserSettingsChangeWarning')}
+ </AlertDialogDescription>
+ </AlertDialogHeader>
+ {error && (
+ <p role="alert" className="text-state-error text-sm">
+ {t('knowledgeConfiguration.parserSettingsApplyFailed')}{' '}
+ {error.message}
+ </p>
+ )}
+ <AlertDialogFooter>
+ <ButtonLoading
+ type="button"
+ variant="outline"
+ loading={isPending}
+ disabled={isPending}
+ onClick={handleApply}
+ >
+ {t('common.yes')}
+ </ButtonLoading>
+ <AlertDialogCancel
+ className={buttonVariants({ variant: 'default' })}
+ disabled={isPending}
+ >
+ {t('common.no')}
+ </AlertDialogCancel>
+ </AlertDialogFooter>
+ </AlertDialogContent>
+ </AlertDialog>
+ );
+}
diff --git a/web/src/pages/dataset/setting/index.test.tsx b/web/src/pages/dataset/setting/index.test.tsx
new file mode 100644
index 0000000000..db71da655b
--- /dev/null
+++ b/web/src/pages/dataset/setting/index.test.tsx
@@ -0,0 +1,310 @@
+import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
+import {
+ act,
+ fireEvent,
+ render,
+ screen,
+ waitFor,
+} from '@testing-library/react';
+import { useEffect as mockUseEffect } from 'react';
+import { useFormContext as mockUseFormContext } from 'react-hook-form';
+import { AxiosHeaders } from 'axios';
+import { applyDatasetParserConfig } from '@/services/knowledge-service';
+import DatasetSetting from './index';
+
+const mockSave = jest.fn();
+const mockTranslate = (key: string) => key;
+const mockInitialValues = {
+ name: 'Dataset',
+ parse_type: 1,
+ parser_id: 'general',
+ pipeline_id: '',
+ pipeline_version_id: '',
+ parser_config: { 'Parser:One': { value: 1 } },
+};
+let mockDataset = { id: 'kb-1', document_count: 2, chunk_count: 0 };
+const mockSourceData: unknown[] = [];
+
+jest.mock('react-i18next', () => ({
+ useTranslation: () => ({ t: mockTranslate }),
+}));
+jest.mock('@/services/knowledge-service', () => ({
+ applyDatasetParserConfig: jest.fn(),
+}));
+jest.mock('@/components/ui/message', () => ({
+ __esModule: true,
+ default: { success: jest.fn() },
+}));
+jest.mock('./form-schema', () => {
+ const { z } = require('zod');
+ return { formSchema: z.object({}).passthrough() };
+});
+jest.mock('./hooks', () => ({
+ useFetchDatasetSettingOnMount: (form: {
+ reset: (values: unknown) => void;
+ }) => {
+ mockUseEffect(() => form.reset(mockInitialValues), [form]);
+ return {
+ knowledgeDetails: mockDataset,
+ loading: false,
+ sourceData: mockSourceData,
+ };
+ },
+ useSaveDatasetSetting: () => ({ handleSave: mockSave, loading: false }),
+ usePipelineDataList: () => [],
+ useConnectorHandlers: () => ({}),
+}));
+jest.mock('./general-form', () => ({
+ GeneralForm: () => {
+ const form = mockUseFormContext();
+ return (
+ <>
+ <input aria-label="name" {...form.register('name')} />
+ <input
+ aria-label="parser value"
+ {...form.register('parser_config.Parser:One.value', {
+ valueAsNumber: true,
+ })}
+ />
+ <input
+ aria-label="pipeline version"
+ {...form.register('pipeline_version_id')}
+ />
+ </>
+ );
+ },
+}));
+jest.mock('@/components/parser-select', () => ({ ParserSelect: () => null }));
+jest.mock('@/components/pipeline-version-select', () => ({
+ PipelineVersionSelect: () => null,
+}));
+jest.mock('@/components/pipeline-operator-tabs', () => ({
+ __esModule: true,
+ default: () => null,
+}));
+jest.mock('./chunk-method-learn-more', () => ({
+ __esModule: true,
+ default: () => null,
+}));
+jest.mock('./components/link-data-source', () => ({
+ __esModule: true,
+ default: () => null,
+}));
+jest.mock('@/hooks/use-parser-select-handler', () => ({
+ useParserSelectHandler: () => jest.fn(),
+}));
+jest.mock('@/hooks/use-parser-options', () => ({
+ buildParserSelectValue: () => '',
+}));
+jest.mock('@/hooks/use-pipeline-operator', () => ({
+ useActiveTab: () => ({ activeTab: '', setActiveTab: jest.fn() }),
+ usePipelineOperatorNodes: () => ({ operatorNodes: [], loading: false }),
+ useResetParserConfigOnPipelineChange: jest.fn(),
+}));
+jest.mock('@/utils/pipeline-operator', () => ({
+ buildParserConfigFromNodes: () => ({}),
+}));
+jest.mock('./parser-config', () => ({
+ normalizeParserConfigForComparison: (config: unknown) => config,
+}));
+jest.mock('@/hooks/use-agent-request', () => ({
+ useFetchCanvasVersionList: () => ({ data: [] }),
+}));
+jest.mock('./use-reveal-submit-errors', () => ({
+ useRevealSubmitErrors: () => ({}),
+}));
+
+const mockApply = jest.mocked(applyDatasetParserConfig);
+
+function response(data: Record<string, unknown>) {
+ return {
+ data,
+ status: 200,
+ statusText: 'OK',
+ headers: {},
+ config: { headers: new AxiosHeaders() },
+ };
+}
+
+function setup() {
+ const queryClient = new QueryClient({
+ defaultOptions: { mutations: { retry: false } },
+ });
+ render(
+ <QueryClientProvider client={queryClient}>
+ <DatasetSetting />
+ </QueryClientProvider>,
+ );
+}
+
+function saveParserChanges() {
+ fireEvent.change(screen.getByLabelText('parser value'), {
+ target: { value: '2' },
+ });
+ fireEvent.click(
+ screen.getByRole('button', { name: 'knowledgeConfiguration.save' }),
+ );
+}
+
+beforeEach(() => {
+ mockSave.mockReset().mockResolvedValue({ code: 0 });
+ mockApply.mockReset().mockResolvedValue(
+ response({
+ code: 0,
+ data: {
+ dataset_id: 'kb-1',
+ total_documents: 2,
+ updated_documents: 2,
+ unchanged_documents: 0,
+ },
+ }),
+ );
+ mockDataset = { id: 'kb-1', document_count: 2, chunk_count: 0 };
+});
+
+it('saves before prompting, including existing files without chunks, and defaults to No', async () => {
+ let finishSave!: (value: { code: number }) => void;
+ mockSave.mockReturnValue(
+ new Promise((resolve) => {
+ finishSave = resolve;
+ }),
+ );
+ setup();
+ saveParserChanges();
+ await waitFor(() => expect(mockSave).toHaveBeenCalledTimes(1));
+ expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument();
+ expect(mockApply).not.toHaveBeenCalled();
+
+ await act(async () => finishSave({ code: 0 }));
+ await screen.findByRole('alertdialog');
+ expect(
+ Array.from(screen.getByRole('alertdialog').querySelectorAll('button')),
+ ).toEqual([
+ screen.getByRole('button', { name: 'common.yes' }),
+ screen.getByRole('button', { name: 'common.no' }),
+ ]);
+ expect(screen.getByRole('button', { name: 'common.no' })).toHaveFocus();
+ expect(screen.getByRole('button', { name: 'common.no' })).toHaveClass(
+ 'bg-text-primary',
+ 'text-bg-base',
+ );
+ expect(screen.getByRole('button', { name: 'common.yes' })).toHaveClass(
+ 'bg-bg-input',
+ 'text-text-secondary',
+ );
+ fireEvent.click(screen.getByRole('button', { name: 'common.no' }));
+ await waitFor(() =>
+ expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument(),
+ );
+ expect(mockApply).not.toHaveBeenCalled();
+ expect(mockSave).toHaveBeenCalledTimes(1);
+});
+
+it('applies the saved settings only after Yes', async () => {
+ setup();
+ saveParserChanges();
+ await screen.findByRole('alertdialog');
+ expect(mockApply).not.toHaveBeenCalled();
+ fireEvent.click(screen.getByRole('button', { name: 'common.yes' }));
+ await waitFor(() => expect(mockApply).toHaveBeenCalledWith('kb-1'));
+ await waitFor(() =>
+ expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument(),
+ );
+ expect(mockSave).toHaveBeenCalledTimes(1);
+});
+
+it('does not prompt when saving fails', async () => {
+ mockSave.mockResolvedValue({ code: 101 });
+ setup();
+ saveParserChanges();
+ await waitFor(() => expect(mockSave).toHaveBeenCalledTimes(1));
+ expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument();
+ expect(mockApply).not.toHaveBeenCalled();
+});
+
+it.each(['unchanged parser settings', 'empty dataset'])(
+ 'does not prompt for %s',
+ async (scenario) => {
+ if (scenario === 'empty dataset') mockDataset.document_count = 0;
+ setup();
+ if (scenario === 'unchanged parser settings') {
+ fireEvent.change(screen.getByLabelText('name'), {
+ target: { value: 'Renamed' },
+ });
+ fireEvent.click(
+ screen.getByRole('button', { name: 'knowledgeConfiguration.save' }),
+ );
+ } else {
+ saveParserChanges();
+ }
+ await waitFor(() => expect(mockSave).toHaveBeenCalledTimes(1));
+ expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument();
+ },
+);
+
+it('prompts for a pipeline version change', async () => {
+ setup();
+ fireEvent.change(screen.getByLabelText('pipeline version'), {
+ target: { value: 'version-2' },
+ });
+ fireEvent.click(
+ screen.getByRole('button', { name: 'knowledgeConfiguration.save' }),
+ );
+ await screen.findByRole('alertdialog');
+ expect(mockApply).not.toHaveBeenCalled();
+});
+
+it('keeps the dialog open after failure and retries without saving again', async () => {
+ mockApply.mockResolvedValueOnce(
+ response({ code: 409, message: 'Stop parsing first' }),
+ );
+ setup();
+ saveParserChanges();
+ await screen.findByRole('alertdialog');
+ fireEvent.click(screen.getByRole('button', { name: 'common.yes' }));
+ expect(await screen.findByRole('alert')).toHaveTextContent(
+ 'knowledgeConfiguration.parserSettingsApplyFailed',
+ );
+ expect(screen.getByRole('alert')).toHaveTextContent('Stop parsing first');
+ fireEvent.click(screen.getByRole('button', { name: 'common.yes' }));
+ await waitFor(() =>
+ expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument(),
+ );
+ expect(mockApply).toHaveBeenCalledTimes(2);
+ expect(mockSave).toHaveBeenCalledTimes(1);
+});
+
+it('treats Escape as No', async () => {
+ setup();
+ saveParserChanges();
+ const dialog = await screen.findByRole('alertdialog');
+ fireEvent.keyDown(dialog, { key: 'Escape', code: 'Escape' });
+ await waitFor(() =>
+ expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument(),
+ );
+ expect(mockApply).not.toHaveBeenCalled();
+});
+
+it('disables both choices while applying settings', async () => {
+ let finishApply!: (value: ReturnType<typeof response>) => void;
+ mockApply.mockReturnValue(
+ new Promise((resolve) => {
+ finishApply = resolve;
+ }),
+ );
+ setup();
+ saveParserChanges();
+ const dialog = await screen.findByRole('alertdialog');
+ fireEvent.click(screen.getByRole('button', { name: 'common.yes' }));
+ await waitFor(() => {
+ expect(screen.getByRole('button', { name: 'common.yes' })).toBeDisabled();
+ expect(screen.getByRole('button', { name: 'common.no' })).toBeDisabled();
+ });
+ fireEvent.keyDown(dialog, { key: 'Escape', code: 'Escape' });
+ expect(screen.getByRole('alertdialog')).toBeInTheDocument();
+ await act(async () => finishApply(response({ code: 0, data: {} })));
+ await waitFor(() =>
+ expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument(),
+ );
+ expect(mockApply).toHaveBeenCalledTimes(1);
+});
diff --git a/web/src/pages/dataset/setting/index.tsx b/web/src/pages/dataset/setting/index.tsx
index ac0c02fa1e..36ec57523d 100644
--- a/web/src/pages/dataset/setting/index.tsx
+++ b/web/src/pages/dataset/setting/index.tsx
@@ -9,7 +9,6 @@ import {
CardTitle,
} from '@/components/ui/card';
import Divider from '@/components/ui/divider';
-import { Modal } from '@/components/ui/modal/modal';
import {
Form,
FormField,
@@ -49,6 +48,7 @@ import { PipelineVersionSelect } from '@/components/pipeline-version-select';
import { RAGFlowTooltip } from '@/components/ui/tooltip';
import { useFetchCanvasVersionList } from '@/hooks/use-agent-request';
import { useRevealSubmitErrors } from './use-reveal-submit-errors';
+import { ApplyParserConfigDialog } from './apply-parser-config-dialog';
export default function DatasetSetting() {
const { t } = useTranslation();
@@ -79,6 +79,7 @@ export default function DatasetSetting() {
sourceData,
} = useFetchDatasetSettingOnMount(form);
const { handleSave, loading: saveLoading } = useSaveDatasetSetting();
+ const [applyParserConfigOpen, setApplyParserConfigOpen] = useState(false);
const [sourceDataState, setSourceDataState] =
useState<IDataSourceNodeProps[]>();
@@ -207,27 +208,19 @@ export default function DatasetSetting() {
data.parse_type !== savedValues?.parse_type ||
(data.parser_id || '') !== (savedValues?.parser_id || '') ||
(data.pipeline_id || '') !== (savedValues?.pipeline_id || '') ||
+ (data.pipeline_version_id || '') !==
+ (savedValues?.pipeline_version_id || '') ||
!isEqual(parserConfig, savedParserConfig);
- if (knowledgeDetails.chunk_count > 0 && parserSettingsChanged) {
- await new Promise<void>((resolve) => {
- Modal.warning({
- title: t('common.warn'),
- content: t('knowledgeConfiguration.parserSettingsChangeWarning'),
- okText: t('common.ok'),
- closable: false,
- showCancel: false,
- onOk: resolve,
- });
- });
- }
-
const result = await handleSave(data);
if (result.code === 0) {
form.reset(data);
+ if (knowledgeDetails.document_count > 0 && parserSettingsChanged) {
+ setApplyParserConfigOpen(true);
+ }
}
},
- [form, handleSave, knowledgeDetails.chunk_count, operatorNodes, t],
+ [form, handleSave, knowledgeDetails.document_count, operatorNodes, t],
);
const { errors } = useFormState({
@@ -269,6 +262,11 @@ export default function DatasetSetting() {
return (
<div className="pr-5 pb-5">
+ <ApplyParserConfigDialog
+ datasetId={knowledgeDetails.id}
+ open={applyParserConfigOpen}
+ onOpenChange={setApplyParserConfigOpen}
+ />
<Card className="p-0 h-full flex flex-col bg-transparent shadow-none">
<CardHeader className="p-5 border-b-0.5 border-border-button">
<header>
diff --git a/web/src/services/knowledge-parser-config.test.ts b/web/src/services/knowledge-parser-config.test.ts
new file mode 100644
index 0000000000..9656522c53
--- /dev/null
+++ b/web/src/services/knowledge-parser-config.test.ts
@@ -0,0 +1,27 @@
+import nextRequest from '@/utils/next-request';
+import { applyDatasetParserConfig } from './knowledge-service';
+
+jest.mock('@/utils/next-request', () => ({
+ __esModule: true,
+ default: jest.fn(),
+}));
+jest.mock('@/utils/request', () => ({
+ __esModule: true,
+ default: {},
+}));
+jest.mock('@/utils/api', () => ({
+ __esModule: true,
+ default: {},
+}));
+
+it('applies saved dataset parser settings with a bodyless POST', async () => {
+ await applyDatasetParserConfig('dataset-1');
+
+ expect(nextRequest).toHaveBeenCalledWith({
+ url: '/api/v1/datasets/dataset-1/documents/apply-parser-config',
+ method: 'post',
+ datasetId: 'dataset-1',
+ skipGlobalErrorNotification: true,
+ });
+ expect(jest.mocked(nextRequest).mock.calls[0][0]).not.toHaveProperty('data');
+});
diff --git a/web/src/services/knowledge-service.ts b/web/src/services/knowledge-service.ts
index 5da36c8d58..4f241359c7 100644
--- a/web/src/services/knowledge-service.ts
+++ b/web/src/services/knowledge-service.ts
@@ -393,6 +393,20 @@ export const getDatasetCompilationStatus = (datasetId: string, kind: string) =>
true,
);
+const datasetParserConfigProxy = registerNextServer({
+ applyDatasetParserConfig: {
+ url: ({ datasetId }: { datasetId: string }) =>
+ `/api/v1/datasets/${datasetId}/documents/apply-parser-config`,
+ method: 'post',
+ },
+});
+
+export const applyDatasetParserConfig = (datasetId: string) =>
+ datasetParserConfigProxy.applyDatasetParserConfig(
+ { datasetId, skipGlobalErrorNotification: true },
+ true,
+ );
+
// Using RESTful API: GET /api/v1/datasets/{dataset_id}/documents
export const listDocument = (
params?: IFetchKnowledgeListRequestParams,