Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 79 additions & 5 deletions apps/chat/serializers/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,19 @@ def is_valid_chat(self):
):
raise ChatException(500, _("Conversation does not exist"))

def is_valid_chat_record(self, chat_record_id):
"""
带 chat_record_id(重答 / 表单续跑)时,该记录必须存在且属于当前会话。
后续按记录 id 读取 workflow_context、回填 messages/details,都不再带 chat_id 条件,
所以在这里拦住:否则可拿自己的 chat_id 读写别人会话的记录,或对不存在的记录空跑不落库。
"""
if chat_record_id is None:
return
if not QuerySet(ChatRecord).filter(id=chat_record_id, chat_id=self.data.get("chat_id")).exists():
raise ChatException(500, _("Conversation record does not exist"))

def is_valid_intraday_access_num(self):
if not self.data.get("debug") and [
if [
ChatUserType.ANONYMOUS_USER.value,
ChatUserType.CHAT_USER.value,
].__contains__(self.data.get("chat_user_type")):
Expand Down Expand Up @@ -460,13 +471,76 @@ def generate():
usage = self._usage_from_details(work_flow_manage.get_details())
return base_to_response.to_block(chat_id, chat_record_id_str, aggregation.get_contents(), usage)

def chat(self, instance: dict, base_to_response: BaseToResponse = SystemToResponse()):
self.is_valid(raise_exception=True)
ChatMessageSerializers(data=instance).is_valid(raise_exception=True)
# ---------- 前置校验 ----------
def valid_access(self, instance: dict):
"""
准入校验:会话归属 + 记录归属 + 应用可用,返回 application。
失败时没有可写入的合法会话(越权 / 记录不属于该会话 / 应用不存在或未发布),直接抛出、不落库。
"""
self.is_valid_chat()
application = self.get_application()
self.is_valid_chat_record(instance.get("chat_record_id"))
return self.get_application()

def valid_limit(self):
"""额度校验:当日访问次数 + token 配额。失败由 chat 落库并按对话格式返回。"""
self.is_valid_intraday_access_num()
ChatUserTokenQuota.check_available(self.data.get("chat_user_id"))

def save_failure_record(self, instance: dict, chat_record_id, block: dict):
"""校验失败落库:惰性建 Chat 行,并写入一条以 FAILURE 块作为回答的 ChatRecord。"""
chat_id = self.data.get("chat_id")
message_dict = instance.get("message")
question = message_dict.get("content", "") if isinstance(message_dict, dict) else message_dict
form_data = instance.get("form_data") or {}
chat_user = resolve_chat_user(
self.data.get("chat_user_id"), self.data.get("chat_user_type"), asker=form_data.get("asker")
)
self.ensure_chat_row(question, chat_user)
record = ChatRecord(id=chat_record_id, **{**self.get_defaults_record(message_dict), "messages": [block]})
record.save(force_insert=True)
QuerySet(Chat).filter(id=chat_id).update(update_time=timezone.now())
ChatCountSerializer(data={"chat_id": chat_id}).update_chat()
record.create_time = record.create_time or timezone.now()
ChatHistory(chat_id).append(record)

def chat_valid_failure(self, error: AppApiException, instance: dict, base_to_response: BaseToResponse):
"""
校验失败的收口:落库 + 返回失败响应。
- 落库:仅新提问(未带 chat_record_id)时写入;重答/表单续跑不覆盖已有记录。
- 响应:与工作流运行出错一致,流式下发 FAILURE 帧后 [DONE],非流式抛出原异常。
"""
chat_id = self.data.get("chat_id")
chat_record_id = instance.get("chat_record_id")
chat_record_id_str = str(uuid.uuid7()) if chat_record_id is None else str(chat_record_id)
# extra.code 标识是哪种校验失败(如 1002 访问次数超限),前端 FAILURE 聚合器会原样保留 extra
block = FailureContent(
str(uuid_utils.uuid7()), str(error.message), Status.SUCCESS, None, None, extra={"code": error.code}
).to_dict()
if chat_record_id is None:
try:
self.save_failure_record(instance, chat_record_id_str, block)
except Exception as e:
# 落库失败不能盖掉校验失败本身,仍按校验失败返回
maxkb_logger.error(f"save chat valid failure record error [{chat_id}]: {e}")
if not instance.get("stream", True):
raise error

def generate():
frame = base_to_response.to_stream(chat_id, chat_record_id_str, block)
if frame is not None:
yield "data: " + frame + "\n\n"
yield "data: [DONE]\n\n"

return to_stream_response_simple(generate())

def chat(self, instance: dict, base_to_response: BaseToResponse = SystemToResponse()):
self.is_valid(raise_exception=True)
ChatMessageSerializers(data=instance).is_valid(raise_exception=True)
application = self.valid_access(instance)
try:
self.valid_limit()
except AppApiException as e:
return self.chat_valid_failure(e, instance, base_to_response)
return self.chat_work_flow(application, instance, base_to_response)


Expand Down
2 changes: 2 additions & 0 deletions apps/chat/tests/test_chat_user_quota.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ def test_exhausted_periodic_quota_is_rejected_before_workflow_runs(self):
patch.object(ChatSerializers, "get_application", return_value=object()),
patch.object(ChatSerializers, "is_valid_chat"),
patch.object(ChatSerializers, "is_valid_intraday_access_num"),
patch.object(ChatSerializers, "save_failure_record") as save_failure_record,
patch.object(ChatSerializers, "chat_work_flow") as workflow,
):
serializer = ChatSerializers(data=data)
with self.assertRaises(AppApiException):
serializer.chat(instance)
workflow.assert_not_called()
save_failure_record.assert_called_once()

def test_available_quota_lets_workflow_run(self):
"""配额未用尽时,前置校验不拦截,正常进入工作流。"""

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The patch is syntactically fine and the new mock/assertion will work. Main review points:

  • Weak assertion: save_failure_record.assert_called_once() only checks that it was called once. It does not verify the failure details. Prefer assert_called_once_with(...) or inspect call_args to confirm the expected exception/context is passed.
  • Test name/scope: The test name only mentions “rejected before workflow runs,” but the test now also verifies failure-record persistence. Consider renaming it or splitting the assertion into a separate test.
  • Missing negative coverage: In test_available_quota_lets_workflow_run, consider patching save_failure_record and asserting assert_not_called() so a success path cannot accidentally save a failure record.
  • Alias naming: save_failure_record shadows the patched method name. A clearer alias such as save_failure_record_mock would improve readability.
  • Line length: The added line is about 87 characters. If the project enforces 79-character lines, wrap it or use a shorter alias.
  • Signature validation: Consider patch.object(..., autospec=True) if you want the mock to catch signature mismatches.
  • Persistence not verified: If save_failure_record writes to a DB or external system, this unit test only verifies the call, not that the record is actually persisted. Add an integration test if that behavior matters.

No blocking issue with patch ordering or context-manager usage.

Expand Down
160 changes: 160 additions & 0 deletions apps/chat/tests/test_chat_valid_failure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# coding=utf-8
import json
from contextlib import contextmanager
from unittest.mock import patch

from django.test import SimpleTestCase

from chat.serializers.chat import ChatSerializers
from common.exception.app_exception import AppChatNumOutOfBoundsFailed, ChatException
from common.handle.impl.response.openai_to_response import OpenaiToResponse

CHAT_ID = "0b1d5a02-6f5b-4d1e-9a4e-6a3c1a1a1a1a"
DATA = {
"chat_id": CHAT_ID,
"chat_user_id": "user-1",
"chat_user_type": "CHAT_USER",
"application_id": "0b1d5a02-6f5b-4d1e-9a4e-6a3c1a1a1a1b",
"debug": False,
"ip_address": "127.0.0.1",
"source": {"type": "ONLINE"},
}
LIMIT_ERROR = AppChatNumOutOfBoundsFailed(1002, "The number of visits exceeds today's visits")


@contextmanager
def patched_chat(limit_error=LIMIT_ERROR, access_error=None, save_error=None, record_error=None):
"""隔离 DB:准入/额度校验、落库、工作流全部打桩,只测 chat 的编排。"""
with (
patch.object(ChatSerializers, "is_valid_chat", side_effect=access_error),
patch.object(ChatSerializers, "is_valid_chat_record", side_effect=record_error) as is_valid_chat_record,
patch.object(ChatSerializers, "get_application", return_value=object()),
patch.object(ChatSerializers, "valid_limit", side_effect=limit_error),
patch.object(ChatSerializers, "save_failure_record", side_effect=save_error) as save_failure_record,
patch.object(ChatSerializers, "chat_work_flow", return_value="ok") as workflow,
):
save_failure_record.is_valid_chat_record = is_valid_chat_record
yield save_failure_record, workflow


def read_frames(response):
content = b"".join(
chunk if isinstance(chunk, bytes) else chunk.encode("utf-8") for chunk in response.streaming_content
).decode("utf-8")
return [frame[len("data: ") :] for frame in content.split("\n\n") if frame]


class ChatValidFailureTests(SimpleTestCase):
def test_stream_limit_failure_is_saved_and_returned_as_failure_frame(self):
"""流式:额度校验失败时落库,并以 FAILURE 帧 + [DONE] 返回,不进入工作流。"""
with patched_chat() as (save_failure_record, workflow):
response = ChatSerializers(data=DATA).chat({"message": {"content": "hi"}, "stream": True})
frames = read_frames(response)

workflow.assert_not_called()
save_failure_record.assert_called_once()
_instance, chat_record_id, block = save_failure_record.call_args.args
self.assertEqual(block["type"], "FAILURE")
self.assertEqual(block["content"], LIMIT_ERROR.message)
# 校验失败的错误码随块落库,并放在 extra 里下发
self.assertEqual(block["extra"], {"code": LIMIT_ERROR.code})

self.assertEqual(frames[-1], "[DONE]")
payload = json.loads(frames[0])
self.assertEqual(payload["chat_id"], CHAT_ID)
# 返回的记录 id 与落库的记录 id 一致,前端可据此定位这条失败记录
self.assertEqual(payload["chat_record_id"], chat_record_id)
self.assertEqual(payload["content"][0]["type"], "FAILURE")
self.assertEqual(payload["content"][0]["content"], LIMIT_ERROR.message)
self.assertEqual(payload["content"][0]["extra"], {"code": LIMIT_ERROR.code})

def test_block_limit_failure_is_saved_and_raised(self):
"""非流式:额度校验失败时落库,并抛出原异常(保留原状态码)。"""
with patched_chat() as (save_failure_record, workflow):
with self.assertRaises(AppChatNumOutOfBoundsFailed):
ChatSerializers(data=DATA).chat({"message": {"content": "hi"}, "stream": False})

workflow.assert_not_called()
save_failure_record.assert_called_once()

def test_existing_record_is_not_overwritten(self):
"""带 chat_record_id(重答/表单续跑)时不落库,避免覆盖已有记录,但仍返回失败。"""
record_id = "0b1d5a02-6f5b-4d1e-9a4e-6a3c1a1a1a1c"
with patched_chat() as (save_failure_record, _workflow):
response = ChatSerializers(data=DATA).chat(
{"message": {"content": "hi"}, "stream": True, "chat_record_id": record_id}
)
frames = read_frames(response)

save_failure_record.assert_not_called()
self.assertEqual(json.loads(frames[0])["chat_record_id"], record_id)

def test_access_failure_is_raised_without_saving(self):
"""准入校验(会话归属/应用)失败时直接抛出,不落库。"""
error = ChatException(500, "Conversation does not exist")
with patched_chat(limit_error=None, access_error=error) as (save_failure_record, workflow):
with self.assertRaises(ChatException):
ChatSerializers(data=DATA).chat({"message": {"content": "hi"}, "stream": True})

save_failure_record.assert_not_called()
workflow.assert_not_called()

def test_record_not_in_chat_is_raised_before_limit_check_and_workflow(self):
"""chat_record_id 不属于当前会话(或不存在)时直接抛出:不落库、不进工作流。"""
record_id = "0b1d5a02-6f5b-4d1e-9a4e-6a3c1a1a1a1c"
error = ChatException(500, "Conversation record does not exist")
with patched_chat(limit_error=None, record_error=error) as (save_failure_record, workflow):
with self.assertRaises(ChatException):
ChatSerializers(data=DATA).chat(
{"message": {"content": "hi"}, "stream": True, "chat_record_id": record_id, "position": {"id": "n"}}
)

save_failure_record.is_valid_chat_record.assert_called_once_with(record_id)
save_failure_record.assert_not_called()
workflow.assert_not_called()

def test_chat_record_must_belong_to_current_chat(self):
"""记录归属校验按 (id, chat_id) 查询:查不到即拒绝;未带 chat_record_id 时不查库。"""
serializer = ChatSerializers(data=DATA)
serializer.is_valid(raise_exception=True)
record_id = "0b1d5a02-6f5b-4d1e-9a4e-6a3c1a1a1a1c"

with patch("chat.serializers.chat.QuerySet") as query_set:
serializer.is_valid_chat_record(None)
query_set.assert_not_called()

query_set.return_value.filter.return_value.exists.return_value = False
with self.assertRaises(ChatException):
serializer.is_valid_chat_record(record_id)
self.assertEqual(query_set.return_value.filter.call_args.kwargs["id"], record_id)
self.assertEqual(str(query_set.return_value.filter.call_args.kwargs["chat_id"]), CHAT_ID)

query_set.return_value.filter.return_value.exists.return_value = True
serializer.is_valid_chat_record(record_id)

def test_save_error_does_not_mask_validation_failure(self):
"""落库出错时仍按校验失败返回,不把落库异常抛给调用方。"""
with patched_chat(save_error=RuntimeError("db down")):
response = ChatSerializers(data=DATA).chat({"message": {"content": "hi"}, "stream": True})
frames = read_frames(response)

self.assertEqual(json.loads(frames[0])["content"][0]["content"], LIMIT_ERROR.message)

def test_openai_stream_skips_failure_frame(self):
"""OpenAI 流式格式不表达 FAILURE 块,只以 [DONE] 收尾(与工作流运行出错的行为一致)。"""
with patched_chat():
response = ChatSerializers(data=DATA).chat(
{"message": {"content": "hi"}, "stream": True}, base_to_response=OpenaiToResponse()
)
frames = read_frames(response)

self.assertEqual(frames, ["[DONE]"])

def test_passing_validation_runs_workflow(self):
"""校验全部通过时正常进入工作流,不落失败记录。"""
with patched_chat(limit_error=None) as (save_failure_record, workflow):
result = ChatSerializers(data=DATA).chat({"message": {"content": "hi"}, "stream": True})

self.assertEqual(result, "ok")
workflow.assert_called_once()
save_failure_record.assert_not_called()
Loading