fix: improve pipeline compilation, template updates, and document resets (#16815)

### What problem does this PR solve?

- Clear stale pipeline IDs and generated data when updating documents
without `pipeline_id`.
- Support tree compilation results in pipeline workflows.
- Update compilation templates in place while preserving existing
template IDs.
- Improve duplicate-template validation messages.

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
- [x] New Feature (non-breaking change which adds functionality)

Co-authored-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
buua436
2026-07-10 21:17:19 +08:00
committed by GitHub
parent 30739b7f8e
commit d291e4641d
7 changed files with 266 additions and 39 deletions

View File

@@ -272,19 +272,25 @@ async def update_document(tenant_id, dataset_id, document_id):
parser_config_template_group_changed = parser_config_template_group_touched and _compilation_template_group_id_changed(old_parser_config, req["parser_config"])
DocumentService.update_parser_config(doc.id, req["parser_config"])
# pipeline_id provided - reset document for reparse
if update_doc_req.pipeline_id:
if error := reset_document_for_reparse(doc, tenant_id, pipeline_id=update_doc_req.pipeline_id):
# A non-empty pipeline_id selects pipeline parsing; an explicitly empty
# value clears it and switches back to the direct parser path.
if "pipeline_id" in req:
if error := reset_document_for_reparse(doc, tenant_id, pipeline_id=update_doc_req.pipeline_id or ""):
return error
# chunk method provided - the update method will check if it's different with existing one
elif update_doc_req.chunk_method:
if error := update_chunk_method(req, doc, tenant_id):
return error
if parser_config_template_group_changed and doc.parser_id.lower() == req["chunk_method"].lower():
if error := reset_document_for_reparse(doc, tenant_id):
if error := reset_document_for_reparse(doc, tenant_id, pipeline_id=""):
return error
elif parser_config_template_group_changed:
if error := reset_document_for_reparse(doc, tenant_id):
if error := reset_document_for_reparse(doc, tenant_id, pipeline_id=""):
return error
else:
# Direct-parser updates do not carry a pipeline_id. Clear any stale
# pipeline selection and its generated document-store data.
if error := reset_document_for_reparse(doc, tenant_id, pipeline_id=""):
return error
if "enabled" in req: # already checked in UpdateDocumentReq - it's int if present

View File

@@ -80,7 +80,13 @@ def update_chunk_method(req, doc, tenant_id):
"""
if doc.parser_id.lower() != req["chunk_method"].lower():
# if chunk method changed, reset document for reparse
result = reset_document_for_reparse(doc, tenant_id, parser_id=req["chunk_method"])
result = reset_document_for_reparse(doc, tenant_id, parser_id=req["chunk_method"], pipeline_id="")
if result:
return result
elif doc.pipeline_id:
# An explicit chunk method selects the direct parser path. Clear the
# previous pipeline even when the parser method itself is unchanged.
result = reset_document_for_reparse(doc, tenant_id, pipeline_id="")
if result:
return result
if not req.get("parser_config"):
@@ -122,7 +128,9 @@ def reset_document_for_reparse(doc, tenant_id, parser_id=None, pipeline_id=None)
if not e:
return get_error_data_result(message="Document not found!")
# Delete chunks from document store
# Update document statistics before deleting all document rows. Pipeline
# compilation rows may exist even when token_num is zero, so the doc-store
# cleanup must not be gated by the document counters.
if doc.token_num > 0:
try:
e = DocumentService.increment_chunk_num(
@@ -136,7 +144,7 @@ def reset_document_for_reparse(doc, tenant_id, parser_id=None, pipeline_id=None)
return get_error_data_result(message="Document not found!")
if not e:
return get_error_data_result(message="Document not found!")
settings.docStoreConn.delete({"doc_id": doc.id}, search.index_name(tenant_id), doc.kb_id)
settings.docStoreConn.delete({"doc_id": doc.id}, search.index_name(tenant_id), doc.kb_id)
# Delete chunk images
try: