feat(shipping-notice): 新增货代客户管理功能,支持客户导入、识别证据管理与客户确认

This commit is contained in:
hugo committed 2026-07-16 08:16:19 +08:00
1 parent b595e157e9
commit 4fbaed2b4c
176 files changed
+14011 -994

No files matched your search

+60 -2
View File
@@ -1,9 +1,9 @@
"""
backend/app/services/chat/log_service.py
用途:Chat Turn 与对话历史的 ChatLog 统一写入。
用途:Chat Turn 与对话历史的 ChatLog 统一写入及结构化 payload 合并。
依赖:models.ChatLog、SQLAlchemy。
逻辑:turn_runtime 在 run.completed 时调用 save_turn_chat_log;api/chat 历史路由可逐步迁入。
逻辑:turn_runtime 在 run.completed 时调用 save_turn_chat_log;领域 Handler 可在上游终态后安全合并回放 payload。
"""
from __future__ import annotations
@@ -72,3 +72,61 @@ def save_turn_chat_log(
current_app.logger.exception("[chat-turn] failed to save chat log: %s", exc)
db.session.rollback()
return None
def merge_chat_log_payload(
*,
chat_log_id: int,
user_id: int,
agent_id: int,
session_id: str,
payload_patch: dict[str, Any],
) -> bool:
"""
在归属校验通过后幂等合并 ChatLog 结构化 payload。
核心逻辑:锁定指定消息并同时校验用户、Agent、本地会话,随后以顶层键覆盖方式合并领域终态;
该函数不修改正文,适合 SSE terminal 之后补写可回放的状态卡片数据。
Args:
chat_log_id: 已持久化的 ChatLog 主键,必须为正整数。
user_id: 当前请求用户主键。
agent_id: 当前请求 Agent 主键。
session_id: 当前 HXOPS 本地会话号。
payload_patch: 要合并的顶层结构化字段,不得为空。
Returns:
找到且成功写入时返回 True;身份不匹配、参数为空或写入失败时返回 False。
"""
if chat_log_id <= 0 or user_id <= 0 or agent_id <= 0 or not str(session_id or '').strip() or not payload_patch:
return False
try:
chat_log = (
db.session.query(ChatLog)
.filter(
ChatLog.id == int(chat_log_id),
ChatLog.user_id == int(user_id),
ChatLog.agent_id == int(agent_id),
ChatLog.session_id == str(session_id).strip(),
)
.with_for_update()
.one_or_none()
)
if chat_log is None:
db.session.rollback()
return False
chat_log.payload = {
**(chat_log.payload if isinstance(chat_log.payload, dict) else {}),
**payload_patch,
}
db.session.commit()
return True
except Exception as exc:
if current_app:
current_app.logger.exception(
"[chat-log] failed to merge payload chat_log_id=%s: %s",
chat_log_id,
exc,
)
db.session.rollback()
return False