fix: async document processing in background threads, improve chat UX

- Fix knowledge_base_service calling async process_document synchronously
  in background threads — use asyncio.new_event_loop().run_until_complete()
  instead of bare call that returned unawaited coroutine
- Same fix for _update_document and reindex_document
- Replace ModeSelector with auto-detect: RAG mode when knowledge bases selected
- Convert regenerateMessage to streaming (was synchronous API call)
- Show "知识库检索模式" indicator above input when KB selected
- Remove loadSessions side-effect from streaming onComplete
- Fix chat page loading state to avoid flash
- Improve forum page spacing and sizing

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-27 11:33:32 +08:00
parent 50faf7ad4e
commit 62da8b3c58
5 changed files with 250 additions and 157 deletions
+14 -6
View File
@@ -161,14 +161,16 @@ class KnowledgeBaseService:
# 处理文档(向量化)- 在后台异步处理,不阻塞主流程
try:
# 使用同步方法,但不等待完成(在后台处理)
import threading
import asyncio as _asyncio
def process_in_background():
try:
self.document_service.process_document(document.id)
loop = _asyncio.new_event_loop()
loop.run_until_complete(self.document_service.process_document(document.id))
loop.close()
except Exception as e:
print(f"后台处理文档 {document.id} 失败: {e}")
thread = threading.Thread(target=process_in_background, daemon=True)
thread.start()
except Exception as e:
@@ -198,12 +200,15 @@ class KnowledgeBaseService:
# 重新处理文档 - 在后台异步处理
try:
import threading
import asyncio as _asyncio
def process_in_background():
try:
self.document_service.process_document(document.id)
loop = _asyncio.new_event_loop()
loop.run_until_complete(self.document_service.process_document(document.id))
loop.close()
except Exception as e:
print(f"后台处理文档 {document.id} 失败: {e}")
thread = threading.Thread(target=process_in_background, daemon=True)
thread.start()
except Exception as e:
@@ -340,7 +345,10 @@ class KnowledgeBaseService:
self.db.query(DocumentChunk).filter(DocumentChunk.document_id == document_id).delete()
# 重新处理文档
success = self.document_service.process_document(document_id)
import asyncio as _asyncio
success = _asyncio.get_event_loop().run_until_complete(
self.document_service.process_document(document_id)
)
if success:
return {"success": True, "message": "文档重新索引成功"}