主要变更: 1. 移除废弃的发货通知字段抽取独立Workflow Agent相关代码 2. 重构Dify服务调用,新增inputs参数支持注入环境变量配置的工具API地址与密钥 3. 新增auto_progress工具API与checkpoint catalog接口,支持流程自动推进 4. 统一字段命名,将column_mapping_json改为map_json并兼容旧有调用 5. 重构库存匹配、解析结果的展示格式,改为标准化多语言Markdown表格 6. 新增ODM ID库存字段支持,优化库存解析与映射逻辑 7. 新增LibreOffice转换隔离Profile配置,避免多实例运行冲突 8. 新增完整环境变量配置、数据库迁移脚本与测试用例 9. 适配前端代码,更新字段命名与聊天跳转逻辑 10. 更新OpenAPI规范适配Dify工具调用,删除已归档的旧部署文档
122 lines
4.1 KiB
Python
122 lines
4.1 KiB
Python
from __future__ import annotations
|
||
|
||
import re
|
||
from typing import Any
|
||
|
||
from app.services.shipping_notice.po_number import enrich_notice_with_po_number
|
||
from app.services.shipping_notice.reservation_id import find_reservation_id_in_document
|
||
from app.services.shipping_notice.notice_lines import (
|
||
enrich_notice_with_document_currency,
|
||
enrich_notice_with_document_required_date,
|
||
enrich_notice_with_document_reservation,
|
||
normalize_notice_lines,
|
||
parse_notice_table_text,
|
||
validate_notice_lines,
|
||
)
|
||
|
||
|
||
def _finalize_notice_parse(
|
||
parsed: dict[str, Any],
|
||
*,
|
||
base_confidence: float,
|
||
raw_text: str = "",
|
||
) -> tuple[dict[str, Any], float, list[str]]:
|
||
lines = normalize_notice_lines(parsed.get("lines") if isinstance(parsed.get("lines"), list) else [])
|
||
parsed = enrich_notice_with_document_reservation({**parsed, "lines": lines}, raw_text=raw_text)
|
||
parsed = enrich_notice_with_document_required_date(parsed)
|
||
parsed = enrich_notice_with_document_currency(parsed)
|
||
parsed = enrich_notice_with_po_number(parsed, raw_text=raw_text)
|
||
lines = parsed.get("lines") if isinstance(parsed.get("lines"), list) else []
|
||
warnings = validate_notice_lines(lines)
|
||
from app.services.shipping_notice.notice_lines import notice_parse_completeness
|
||
|
||
completeness = notice_parse_completeness(lines)
|
||
confidence = min(base_confidence, 0.95) * (0.5 + 0.5 * completeness) if lines else base_confidence * 0.5
|
||
return parsed, round(confidence, 4), warnings
|
||
|
||
|
||
def extract_notice_fields(
|
||
*,
|
||
raw_text: str,
|
||
source_type: str = "manual",
|
||
structured: dict[str, Any] | None = None,
|
||
notice_files: list[dict[str, Any]] | None = None,
|
||
) -> tuple[dict[str, Any], float, list[str]]:
|
||
warnings: list[str] = []
|
||
|
||
if structured and isinstance(structured, dict):
|
||
return _finalize_notice_parse(
|
||
structured,
|
||
base_confidence=float(structured.get("confidence") or 0.95),
|
||
raw_text=str(structured.get("raw_text") or ""),
|
||
)
|
||
|
||
heuristic, confidence, heuristic_warnings = _heuristic_extract(raw_text)
|
||
warnings.extend(heuristic_warnings)
|
||
return _finalize_notice_parse(heuristic, base_confidence=confidence, raw_text=raw_text)
|
||
|
||
|
||
def _heuristic_extract(raw_text: str) -> tuple[dict[str, Any], float, list[str]]:
|
||
text = str(raw_text or "").strip()
|
||
warnings: list[str] = []
|
||
customer_name = ""
|
||
m = re.search(r"客户[::]\s*(\S+)", text)
|
||
if m:
|
||
customer_name = m.group(1)
|
||
|
||
lines = parse_notice_table_text(text)
|
||
if lines:
|
||
doc_rid = find_reservation_id_in_document(text)
|
||
if doc_rid:
|
||
for line in lines:
|
||
if not line.get("reservation_id"):
|
||
line["reservation_id"] = doc_rid
|
||
return (
|
||
{
|
||
"customer_name": customer_name,
|
||
"customer_code": "",
|
||
"ship_to": "",
|
||
"required_date": "",
|
||
"lines": lines,
|
||
"confidence": 0.72,
|
||
},
|
||
0.72,
|
||
warnings,
|
||
)
|
||
|
||
lines = []
|
||
for m in re.finditer(r"([A-Za-z0-9\-_]{3,})\s*[xX×*]\s*(\d+(?:\.\d+)?)", text):
|
||
lines.append({"sku": m.group(1), "qty": float(m.group(2)), "uom": "EA"})
|
||
|
||
# 尝试纯数字 Reservation ID + SKU + Qty 的自由文本模式(如 1000140351 10-40594-001 2)
|
||
if not lines:
|
||
for m in re.finditer(
|
||
r"(?P<resv>\d{8,12})\s+([A-Za-z0-9\-_]{3,})\s+(\d+(?:\.\d+)?)",
|
||
text,
|
||
):
|
||
lines.append(
|
||
{
|
||
"reservation_id": m.group("resv"),
|
||
"sku": m.group(2),
|
||
"qty": float(m.group(3)),
|
||
"uom": "EA",
|
||
}
|
||
)
|
||
|
||
if not lines:
|
||
warnings.append("heuristic_no_lines_detected")
|
||
|
||
confidence = 0.5 if lines else 0.3
|
||
return (
|
||
{
|
||
"customer_name": customer_name,
|
||
"customer_code": "",
|
||
"ship_to": "",
|
||
"required_date": "",
|
||
"lines": lines,
|
||
"confidence": confidence,
|
||
},
|
||
confidence,
|
||
warnings,
|
||
)
|