feat(shipping_notice): 新增货代订单全流程业务模块及配套基建
本次提交新增货代订单处理全流程的完整业务能力、数据库迁移与测试用例: 1. 统一追加式审计事件体系,支持全链路不可篡改的业务追溯 2. 货代任务撤销、版本替代管控逻辑,规范已上传订单的合规版本更替 3. 订单抽取路由审计、模板自动冻结资格、路由计数等核心业务基建 4. EML订单补充表解析契约与合并逻辑,完善订单补全能力 5. Chatflow只读能力令牌校验机制,保障工具调用安全 6. 人工复核证据校验服务,规范人工审核的证据留存 所有变更均配套完整的单元测试与迁移契约测试,支持受控回退
This commit is contained in:
1 parent
90f77634d6
commit
1f3ecb87b7
128 files changed
+3635
-2074
No files matched your search
+32
-52
@@ -82,9 +82,6 @@ def _normalize_user_attachments(value) -> list[dict]:
|
||||
'type': attachment_type,
|
||||
'source': source,
|
||||
}
|
||||
attachment_id = re.sub(r'[\x00-\x1f\x7f]', '', str(item.get('id') or '')).strip()[:128]
|
||||
if attachment_id:
|
||||
snapshot['id'] = attachment_id
|
||||
extension = str(item.get('extension') or '').strip().lower()
|
||||
if re.fullmatch(r'[a-z0-9]{1,16}', extension):
|
||||
snapshot['extension'] = extension
|
||||
@@ -141,8 +138,7 @@ def _build_chat_adapter_hints(
|
||||
*,
|
||||
shipping_task_id: str | None = None,
|
||||
shipping_batch_no: str | None = None,
|
||||
shipping_temporary_inventory_asset_no: str | None = None,
|
||||
shipping_temporary_inventory_session_no: str | None = None,
|
||||
shipping_intake_nos: list[str] | None = None,
|
||||
) -> dict | None:
|
||||
"""
|
||||
构建聊天入口分发给领域 adapter 的提示字典。
|
||||
@@ -152,17 +148,16 @@ def _build_chat_adapter_hints(
|
||||
"""
|
||||
task_id = str(shipping_task_id or '').strip()
|
||||
batch_no = str(shipping_batch_no or '').strip()
|
||||
temporary_asset_no = str(shipping_temporary_inventory_asset_no or '').strip().upper()
|
||||
temporary_session_no = str(shipping_temporary_inventory_session_no or '').strip().upper()
|
||||
if not task_id and not batch_no and not temporary_asset_no and not temporary_session_no:
|
||||
intake_nos = sorted({str(item or '').strip().upper() for item in (shipping_intake_nos or []) if str(item or '').strip()})
|
||||
if not task_id and not batch_no and not intake_nos:
|
||||
return None
|
||||
return {
|
||||
'shipping_notice': {
|
||||
'task_id': task_id or None,
|
||||
'batch_no': batch_no or None,
|
||||
'capability': 'temporary_inventory' if temporary_asset_no else None,
|
||||
'temporary_inventory_asset_no': temporary_asset_no or None,
|
||||
'temporary_inventory_session_no': temporary_session_no or None,
|
||||
'intake_nos': intake_nos,
|
||||
'capability': 'order_extraction' if intake_nos else None,
|
||||
'contract_version': 'shipping-order-extraction-v1' if intake_nos else None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -204,18 +199,17 @@ def _user_facing_chat_query(text: str, *, agent_code: str | None = None) -> str:
|
||||
|
||||
task_match = re.search(r'(SN-\d{8}-[A-Z0-9]+)', value, re.I)
|
||||
task_no = task_match.group(1).upper() if task_match else ''
|
||||
if 'create_task' in value or 'get_task_status' in value:
|
||||
if 'create_task' in value or 'get_task_status' in value or 'parse_notice' in value:
|
||||
return f'继续处理当前发货任务 {task_no}'.strip()
|
||||
return value
|
||||
|
||||
|
||||
def _serialize_session(
|
||||
session: ChatSession,
|
||||
*,
|
||||
unseen_order_extraction_count: int = 0,
|
||||
*, unseen_order_extraction_count: int = 0,
|
||||
unseen_order_extraction_severity: str | None = None,
|
||||
) -> dict:
|
||||
"""输出会话摘要及订单识别待查看聚合,不返回 Run 内容或错误正文。"""
|
||||
"""输出会话摘要及订单抽取待查看聚合,不返回 Run 内容或错误正文。"""
|
||||
last_active = session.last_active_at or session.created_at
|
||||
return {
|
||||
'id': session.id,
|
||||
@@ -228,8 +222,10 @@ def _serialize_session(
|
||||
}
|
||||
|
||||
|
||||
def _order_extraction_attention_by_conversation(*, user_id: int, agent_id: int, conversation_ids: set[str]) -> dict[str, tuple[int, str]]:
|
||||
"""聚合阶段4之后生成、且当前用户尚未确认的终态订单识别提醒。"""
|
||||
def _order_extraction_attention_by_conversation(
|
||||
*, user_id: int, agent_id: int, conversation_ids: set[str],
|
||||
) -> dict[str, tuple[int, str]]:
|
||||
"""聚合同用户同 Agent 尚未确认的终态订单抽取提醒,防止跨会话泄露。"""
|
||||
if not conversation_ids:
|
||||
return {}
|
||||
severity_rank = {'info': 1, 'warning': 2, 'error': 3}
|
||||
@@ -247,10 +243,10 @@ def _order_extraction_attention_by_conversation(*, user_id: int, agent_id: int,
|
||||
aggregate: dict[str, tuple[int, str]] = {}
|
||||
for conversation_id, status in rows:
|
||||
severity = status_severity.get(str(status or ''), 'info')
|
||||
current_count, current_severity = aggregate.get(conversation_id, (0, severity))
|
||||
count, existing = aggregate.get(conversation_id, (0, severity))
|
||||
aggregate[conversation_id] = (
|
||||
current_count + 1,
|
||||
severity if severity_rank[severity] >= severity_rank[current_severity] else current_severity,
|
||||
count + 1,
|
||||
severity if severity_rank[severity] >= severity_rank[existing] else existing,
|
||||
)
|
||||
return aggregate
|
||||
|
||||
@@ -1162,9 +1158,9 @@ def get_chat_messages():
|
||||
ChatLog.agent_id == g.current_agent.id,
|
||||
ChatLog.conversation_id == conversation_id,
|
||||
).order_by(ChatLog.created_at.asc()).all()
|
||||
|
||||
|
||||
def _is_order_extraction_state_card(payload):
|
||||
"""仅识别正式 Run 状态卡,避免用户上传记录在历史回放中被投影成重复助手卡。"""
|
||||
"""只识别正式 Run 状态卡;用户上传锚点不得回放为重复的助手卡。"""
|
||||
if not isinstance(payload, dict):
|
||||
return False
|
||||
run_no = str(payload.get('order_extraction_run_no') or '').strip()
|
||||
@@ -1172,8 +1168,7 @@ def get_chat_messages():
|
||||
|
||||
run_nos = {
|
||||
str(log.payload.get('order_extraction_run_no') or '').strip()
|
||||
for log in logs
|
||||
if _is_order_extraction_state_card(log.payload)
|
||||
for log in logs if _is_order_extraction_state_card(log.payload)
|
||||
}
|
||||
order_extraction_runs = {}
|
||||
if run_nos:
|
||||
@@ -1186,6 +1181,7 @@ def get_chat_messages():
|
||||
ShippingOrderExtractionRun.run_no.in_(run_nos),
|
||||
).all()
|
||||
}
|
||||
|
||||
messages = []
|
||||
agent_code = str(getattr(g.current_agent, 'agent_code', '') or '').strip() or 'generic'
|
||||
for log in logs:
|
||||
@@ -1197,8 +1193,7 @@ def get_chat_messages():
|
||||
if key != 'user_attachments'
|
||||
}
|
||||
run_no = str(assistant_payload.get('order_extraction_run_no') or '').strip()
|
||||
is_order_extraction_state_card = _is_order_extraction_state_card(raw_payload)
|
||||
if is_order_extraction_state_card and run_no in order_extraction_runs:
|
||||
if _is_order_extraction_state_card(raw_payload) and run_no in order_extraction_runs:
|
||||
from app.services.shipping_notice.order_extraction_run_service import serialize_order_extraction_message_state
|
||||
|
||||
assistant_payload['shipping_order_extraction_state'] = serialize_order_extraction_message_state(
|
||||
@@ -1209,11 +1204,7 @@ def get_chat_messages():
|
||||
if user_attachments:
|
||||
user_msg['attachments'] = user_attachments
|
||||
messages.append(user_msg)
|
||||
if (
|
||||
str(raw_payload.get('formId') or '').strip()
|
||||
== f'shipping-order-extraction-user:{run_no}'
|
||||
):
|
||||
# 用户上传记录携带 Run 锚点只用于关联,不能再回放为一张助手状态卡。
|
||||
if str(raw_payload.get('formId') or '').strip() == f'shipping-order-extraction-user:{run_no}':
|
||||
continue
|
||||
if (
|
||||
not log.answer
|
||||
@@ -1469,8 +1460,14 @@ def chat_completions():
|
||||
conversation_id = data.get('conversation_id')
|
||||
shipping_task_id = str(data.get('task_id') or '').strip()
|
||||
shipping_batch_no = str(data.get('batch_no') or '').strip()
|
||||
shipping_temporary_inventory_asset_no = str(data.get('shipping_temporary_inventory_asset_no') or '').strip().upper()
|
||||
shipping_temporary_inventory_session_no = str(data.get('shipping_temporary_inventory_session_no') or '').strip().upper()
|
||||
raw_shipping_intakes = data.get('shipping_intake_nos')
|
||||
if raw_shipping_intakes is not None and not isinstance(raw_shipping_intakes, list):
|
||||
raise ValidationError('shipping_intake_nos must be a list')
|
||||
shipping_intake_nos = sorted({
|
||||
str(item or '').strip().upper()
|
||||
for item in (raw_shipping_intakes or [])
|
||||
if str(item or '').strip()
|
||||
})
|
||||
kb_type = data.get('kb_type', '企业知识库')
|
||||
user_attachments = _normalize_user_attachments(data.get('user_attachments'))
|
||||
session_title = display_query or (user_attachments[0]['name'] if user_attachments else str(query or '').strip())
|
||||
@@ -1609,8 +1606,7 @@ def chat_completions():
|
||||
adapter_hints=_build_chat_adapter_hints(
|
||||
shipping_task_id=shipping_task_id,
|
||||
shipping_batch_no=shipping_batch_no,
|
||||
shipping_temporary_inventory_asset_no=shipping_temporary_inventory_asset_no,
|
||||
shipping_temporary_inventory_session_no=shipping_temporary_inventory_session_no,
|
||||
shipping_intake_nos=shipping_intake_nos,
|
||||
),
|
||||
shipping_task_id=shipping_task_id,
|
||||
shipping_batch_no=shipping_batch_no,
|
||||
@@ -1653,8 +1649,7 @@ def chat_completions():
|
||||
adapter_hints=_build_chat_adapter_hints(
|
||||
shipping_task_id=shipping_task_id,
|
||||
shipping_batch_no=shipping_batch_no,
|
||||
shipping_temporary_inventory_asset_no=shipping_temporary_inventory_asset_no,
|
||||
shipping_temporary_inventory_session_no=shipping_temporary_inventory_session_no,
|
||||
shipping_intake_nos=shipping_intake_nos,
|
||||
),
|
||||
),
|
||||
)
|
||||
@@ -1683,7 +1678,6 @@ def chat_completions():
|
||||
from app.services.chat.stream_relay import (
|
||||
iter_handler_preface_sse,
|
||||
iter_handler_terminal_sse,
|
||||
should_recover_upstream_error,
|
||||
)
|
||||
|
||||
for preface_line in iter_handler_preface_sse(relay_stream_ctx, emitted_keys=emitted_handler_state_keys):
|
||||
@@ -1707,20 +1701,6 @@ def chat_completions():
|
||||
|
||||
provider_conversation_id = str(event_data.get('conversation_id') or known_provider_conversation_id or '').strip() or None
|
||||
event_name = event_data.get('event')
|
||||
if event_name == 'error' and should_recover_upstream_error(relay_stream_ctx, event_data):
|
||||
current_app.logger.warning(
|
||||
'[chat-api] recover upstream error from persisted shipping extraction request_id=%s session_id=%s',
|
||||
request_id,
|
||||
local_session_id,
|
||||
)
|
||||
event_data = {
|
||||
**event_data,
|
||||
'event': 'message_end',
|
||||
'answer': '',
|
||||
'replace_answer': False,
|
||||
'recovered_upstream_error': True,
|
||||
}
|
||||
event_name = 'message_end'
|
||||
if event_name in ('error', 'message_end', 'workflow_finished') and user_attachments:
|
||||
try:
|
||||
attachment_chat_log_id = int(event_data.get('chat_log_id') or 0)
|
||||
|
||||
Reference in new issue
Block a user