feat(chat): allow knowledge chat before upload

This commit is contained in:
2026-06-04 22:16:54 +08:00
parent 1d8a526770
commit efb06519d8
6 changed files with 158 additions and 27 deletions

View File

@@ -10,6 +10,7 @@ from apps.audit.models import NotificationRecord
from apps.chat.models import Conversation
from apps.chat.services import (
create_conversation_for_batch,
create_knowledge_conversation,
execute_conversation_agent,
execute_conversation_export,
)
@@ -79,6 +80,47 @@ def test_chat_rejects_empty_message(client, db):
assert "请输入要咨询的问题" in response.content.decode("utf-8")
def test_chat_index_allows_question_before_upload_and_shows_upload_control(client, db):
response = client.get(reverse("chat:index"))
content = response.content.decode("utf-8")
assert response.status_code == 200
assert "发送问题" in content
assert "导入资料包" in content
assert "未选择文档时Agent 会按问题检索当前知识库" in content
assert "暂无绑定资料包,仍可先通过中间对话区查询知识库" in content
def test_chat_index_post_creates_knowledge_conversation_and_runs_agent(client, db, monkeypatch):
captured = {}
def fake_run_agent(scenario_config, user_input, options=None):
captured["scenario_id"] = scenario_config["id"]
captured["user_input"] = user_input
captured["options"] = options or {}
return AgentResult(answer="知识库命中:注册资料目录要求", status="success")
monkeypatch.setattr("apps.chat.services.run_agent", fake_run_agent)
response = client.post(
reverse("chat:index"),
{"message": "注册资料目录有哪些要求?"},
)
content = response.content.decode("utf-8")
conversation = Conversation.objects.get()
assert response.status_code == 200
assert conversation.title == "知识库问答会话"
assert conversation.batch_id == ""
assert captured["scenario_id"] == "document_review"
assert captured["user_input"] == "注册资料目录有哪些要求?"
assert captured["options"]["conversation_id"] == conversation.conversation_id
assert captured["options"]["batch_id"] == ""
assert captured["options"]["document_ids"] == []
assert "知识库命中:注册资料目录要求" in content
assert AgentAuditLog.objects.filter(conversation_id=conversation.conversation_id).count() == 1
def test_chat_passes_selected_document_ids_to_agent_core(client, db, monkeypatch):
batch, conversation = _create_conversation_with_batch()
selected = UploadedDocument.objects.create(