Files
syops/backend/app/services/shipping_notice/attachment_integrity.py
T
hugo 61ac6403c4 refactor(agent, shipping): 简化代理模型并完善订单抽取业务
- 移除 Agent 的 exposure_mode 与 internal_executor 相关逻辑,清理关联的过滤、序列化代码
- 删除废弃的 AgentRuntimeBinding 模型及其依赖
- 新增订单抽取上传批次、请求快照等数据库迁移与业务逻辑
- 修复附件完整性检测、订单抽取状态映射问题
- 更新多语言错误文案与前端单元测试配置
- 清理平台配置中不再使用的暴露模式配置
2026-07-28 20:25:48 +08:00

89 lines
3.0 KiB
Python

"""
services/shipping_notice/attachment_integrity.py
用途:判定 Intake 的 EML/附件解析是否完整,供建批门禁与重试资格复用。
依赖:仅标准库(与 structured_result_json / parse_summary 形状对齐)。
逻辑:由 batch_from_intakes、intake_service.retry_intake_parse 调用;
BLOCKING_WARNING_PREFIXES 与前端 shippingNoticeAttachmentIntegrity.ts 契约一致
(由 tests 校验两端前缀集合)。
"""
from __future__ import annotations
from typing import Any
# 顶层 / 附件行 warnings 前缀:出现任一即视为不完整(与前端门禁契约对齐)
BLOCKING_WARNING_PREFIXES: tuple[str, ...] = (
'eml_nesting_depth_exceeded:',
'eml_attachment_limit_exceeded:',
'eml_total_attachment_size_exceeded:',
'eml_nested_message_unreadable',
'eml_attachment_text_budget_exhausted:',
'eml_attachment_text_truncated:',
'notice_text_truncated:',
)
_INCOMPLETE_ATTACHMENT_STATUSES = frozenset({'failed', 'empty', 'skipped'})
def is_blocking_warning(text: str) -> bool:
"""
判定单条 warning 是否为完整性阻断项。
Args:
text: warning 原文。
Returns:
True 表示该警告会使 Intake 被视为附件解析不完整。
"""
normalized = str(text or '')
return any(normalized.startswith(prefix) for prefix in BLOCKING_WARNING_PREFIXES)
def is_attachment_parse_incomplete(structured: dict[str, Any] | None) -> bool:
"""
判定 structured_result_json / parse_summary 是否存在未完整解析的附件。
核心逻辑:阻断警告(深度/数量/总大小/不可读嵌套/文本预算或截断)、
attachment_count > 已登记数、或任一行 parse_status 为 failed/empty/skipped
均视为不完整;duplicate 仅去重提示,不阻断。
Args:
structured: Intake 的 structured_result_json 或 API 下发的 parse_summary。
Returns:
True 表示不可用该 Intake 直接建批,应拒绝或允许重试解析。
"""
if not isinstance(structured, dict):
return False
preview_status = str(structured.get('preview_status') or '').strip().lower()
if preview_status == 'directory_incomplete':
return True
if structured.get('attachment_directory_complete') is False:
return True
warnings = structured.get('warnings') or []
for item in warnings:
if is_blocking_warning(str(item or '')):
return True
rows = structured.get('attachments') or []
if not isinstance(rows, list):
rows = []
total = int(structured.get('attachment_count') or 0)
if total > len(rows):
return True
for row in rows:
if not isinstance(row, dict):
continue
status = str(row.get('parse_status') or '').strip().lower()
if status in _INCOMPLETE_ATTACHMENT_STATUSES:
return True
for item in row.get('warnings') or []:
if is_blocking_warning(str(item or '')):
return True
return False