refactor: 下沉会话执行编排到 chat 服务层
This commit is contained in:
@@ -8,7 +8,7 @@ from agent_core.results import AgentResult
|
||||
from apps.audit.models import AgentAuditLog
|
||||
from apps.audit.models import NotificationRecord
|
||||
from apps.chat.models import Conversation
|
||||
from apps.chat.services import create_conversation_for_batch
|
||||
from apps.chat.services import create_conversation_for_batch, execute_conversation_agent
|
||||
from apps.documents.models import ExportedDocument, SubmissionBatch, UploadedDocument
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ def test_chat_post_returns_agent_result_and_audit_log(client, db, monkeypatch):
|
||||
)
|
||||
|
||||
monkeypatch.setattr(
|
||||
"apps.chat.views.run_agent",
|
||||
"apps.chat.services.run_agent",
|
||||
lambda *args, **kwargs: AgentResult(answer="模拟回答", status="success"),
|
||||
)
|
||||
|
||||
@@ -99,7 +99,7 @@ def test_chat_passes_selected_document_ids_to_agent_core(client, db, monkeypatch
|
||||
captured["options"] = options or {}
|
||||
return AgentResult(answer="ok", status="success")
|
||||
|
||||
monkeypatch.setattr("apps.chat.views.run_agent", fake_run_agent)
|
||||
monkeypatch.setattr("apps.chat.services.run_agent", fake_run_agent)
|
||||
|
||||
response = client.post(
|
||||
reverse("chat:detail", args=[conversation.conversation_id]),
|
||||
@@ -169,7 +169,7 @@ def test_chat_execution_creates_notification_record_from_agent_result(client, db
|
||||
)
|
||||
|
||||
monkeypatch.setattr(
|
||||
"apps.chat.views.run_agent",
|
||||
"apps.chat.services.run_agent",
|
||||
lambda *args, **kwargs: AgentResult(
|
||||
answer="执行完成",
|
||||
status="success",
|
||||
@@ -212,7 +212,7 @@ def test_chat_execution_uses_notification_payload_message_status_and_receipt(cli
|
||||
)
|
||||
|
||||
monkeypatch.setattr(
|
||||
"apps.chat.views.run_agent",
|
||||
"apps.chat.services.run_agent",
|
||||
lambda *args, **kwargs: AgentResult(
|
||||
answer="通知已发送",
|
||||
status="success",
|
||||
@@ -257,7 +257,7 @@ def test_chat_execution_creates_failed_notification_record_and_updates_conversat
|
||||
)
|
||||
|
||||
monkeypatch.setattr(
|
||||
"apps.chat.views.run_agent",
|
||||
"apps.chat.services.run_agent",
|
||||
lambda *args, **kwargs: AgentResult(
|
||||
answer="执行失败",
|
||||
status="failed",
|
||||
@@ -310,7 +310,7 @@ def test_chat_execution_persists_agent_node_results_to_conversation(client, db,
|
||||
)
|
||||
|
||||
monkeypatch.setattr(
|
||||
"apps.chat.views.run_agent",
|
||||
"apps.chat.services.run_agent",
|
||||
lambda *args, **kwargs: AgentResult(
|
||||
answer="已生成风险结论",
|
||||
status="success",
|
||||
@@ -366,6 +366,52 @@ def test_create_conversation_for_batch_initializes_eight_workflow_nodes(db):
|
||||
]
|
||||
|
||||
|
||||
def test_execute_conversation_agent_runs_in_service_layer_and_persists_audit_and_notifications(db, monkeypatch):
|
||||
batch, conversation = _create_conversation_with_batch()
|
||||
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",
|
||||
notification_payload={
|
||||
"batch_id": batch.batch_id,
|
||||
"conversation_id": conversation.conversation_id,
|
||||
"product_name": batch.product_name,
|
||||
"notify_reason": "task_completed",
|
||||
"owners": [
|
||||
{
|
||||
"owner_role": "注册资料负责人",
|
||||
"feishu_user_id": "ou_service_1",
|
||||
}
|
||||
],
|
||||
},
|
||||
)
|
||||
|
||||
monkeypatch.setattr("apps.chat.services.run_agent", fake_run_agent)
|
||||
|
||||
result, audit_log = execute_conversation_agent(
|
||||
conversation=conversation,
|
||||
message="服务层执行审核",
|
||||
document_ids=[101, 102],
|
||||
detail_url_builder=lambda log_id: f"/audit/{log_id}/",
|
||||
)
|
||||
|
||||
conversation.refresh_from_db()
|
||||
assert result.answer == "服务层执行完成"
|
||||
assert audit_log.batch_id == batch.batch_id
|
||||
assert captured["scenario_id"] == "document_review"
|
||||
assert captured["user_input"] == "服务层执行审核"
|
||||
assert captured["options"]["document_ids"] == [101, 102]
|
||||
assert conversation.latest_summary["answer"] == "服务层执行完成"
|
||||
record = NotificationRecord.objects.get()
|
||||
assert record.notify_reason == "task_completed"
|
||||
assert record.web_detail_url == f"/audit/{audit_log.id}/"
|
||||
|
||||
|
||||
def test_chat_page_shows_upload_entry_and_dynamic_context_cards(client, db):
|
||||
batch, conversation = _create_conversation_with_batch()
|
||||
conversation.node_results = [
|
||||
|
||||
Reference in New Issue
Block a user