feat(faq): 知识库相对应文档更新,标记关联的问题为待复审状态并暂停缓存命中
This commit is contained in:
1 parent
757231e3d5
commit
0d59b23e0c
16 files changed
+809
-185
No files matched your search
@@ -10,6 +10,7 @@ import re
|
||||
from pathlib import Path
|
||||
|
||||
import requests
|
||||
from flask import current_app
|
||||
|
||||
from app import db
|
||||
from app.models import KbDifySyncRecord, KbDocumentVersion, KbGroup
|
||||
@@ -312,6 +313,7 @@ def sync_document_version_to_dify(document_version_id: int, synced_by_id: int |
|
||||
.order_by(KbDifySyncRecord.updated_at.desc(), KbDifySyncRecord.id.desc())
|
||||
.first()
|
||||
)
|
||||
previous_sync_hash = existing_record.sync_hash if existing_record else None
|
||||
|
||||
remote_document_id = None
|
||||
remote_document_name = None
|
||||
@@ -401,6 +403,35 @@ def sync_document_version_to_dify(document_version_id: int, synced_by_id: int |
|
||||
|
||||
record.synced_at = datetime.utcnow()
|
||||
db.session.commit()
|
||||
|
||||
content_changed = previous_sync_hash != sync_hash
|
||||
if content_changed:
|
||||
try:
|
||||
from app.services.faq_kb_invalidation import (
|
||||
STALE_REASON_KB_UPDATED,
|
||||
invalidate_faqs_for_kb_document,
|
||||
)
|
||||
|
||||
invalidated = invalidate_faqs_for_kb_document(
|
||||
document.id,
|
||||
reason=STALE_REASON_KB_UPDATED,
|
||||
dataset_id=str(dataset_id),
|
||||
remote_document_id=str(remote_document_id),
|
||||
)
|
||||
if current_app and invalidated:
|
||||
current_app.logger.info(
|
||||
'[faq-kb-invalidation] kb_document_updated id=%s invalidated=%s',
|
||||
document.id,
|
||||
invalidated,
|
||||
)
|
||||
except Exception as exc:
|
||||
if current_app:
|
||||
current_app.logger.warning(
|
||||
'[faq-kb-invalidation] hook failed document_id=%s: %s',
|
||||
document.id,
|
||||
exc,
|
||||
)
|
||||
|
||||
return {
|
||||
'dataset_id': dataset_id,
|
||||
'dataset_name': dataset_name,
|
||||
@@ -445,4 +476,32 @@ def delete_document_from_dify(document_id: int) -> dict:
|
||||
deleted_count += 1
|
||||
|
||||
db.session.commit()
|
||||
|
||||
try:
|
||||
from app.services.faq_kb_invalidation import (
|
||||
STALE_REASON_KB_DELETED,
|
||||
invalidate_faqs_for_kb_document,
|
||||
)
|
||||
|
||||
latest = records[0] if records else None
|
||||
invalidated = invalidate_faqs_for_kb_document(
|
||||
int(document_id),
|
||||
reason=STALE_REASON_KB_DELETED,
|
||||
dataset_id=str(latest.dataset_id) if latest and latest.dataset_id else None,
|
||||
remote_document_id=str(latest.remote_document_id) if latest and latest.remote_document_id else None,
|
||||
)
|
||||
if current_app and invalidated:
|
||||
current_app.logger.info(
|
||||
'[faq-kb-invalidation] kb_document_deleted id=%s invalidated=%s',
|
||||
document_id,
|
||||
invalidated,
|
||||
)
|
||||
except Exception as exc:
|
||||
if current_app:
|
||||
current_app.logger.warning(
|
||||
'[faq-kb-invalidation] delete hook failed document_id=%s: %s',
|
||||
document_id,
|
||||
exc,
|
||||
)
|
||||
|
||||
return {'deleted': True, 'remote_document_count': deleted_count}
|
||||
Reference in new issue
Block a user