feat(shipping-notice): 完成货代发货通知全流程开发与代码重构
- 新增PO号提取、预约ID解析(支持`PR@xxx`格式)工具,完善库存匹配逻辑 - 新增库存映射缓存、匹配/解析结果格式化摘要,优化用户提示 - 修复数据库迁移脚本未检查表存在的问题,避免全新部署失败 - 新增路径工具类统一管理相对路径,优化配置文件处理逻辑 - 归档旧文档,将部署、Dify配置合并到`shipping_notice.md` - 新增完整单元测试与集成测试覆盖核心流程 - 更新前端国际化与会话管理逻辑,优化界面交互
This commit is contained in:
1 parent
b62082758b
commit
8deae3146b
65 files changed
+6477
-1937
No files matched your search
@@ -1,12 +1,18 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
from app.services.dify import DifyService
|
||||
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.agent_resolver import resolve_extract_agent
|
||||
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,
|
||||
@@ -35,9 +41,18 @@ def _parse_workflow_outputs(result: dict[str, Any]) -> dict[str, Any] | None:
|
||||
return outputs if outputs else None
|
||||
|
||||
|
||||
def _finalize_notice_parse(parsed: dict[str, Any], *, base_confidence: float) -> tuple[dict[str, Any], float, list[str]]:
|
||||
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 = {**parsed, "lines": lines}
|
||||
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
|
||||
|
||||
@@ -46,23 +61,63 @@ def _finalize_notice_parse(parsed: dict[str, Any], *, base_confidence: float) ->
|
||||
return parsed, round(confidence, 4), warnings
|
||||
|
||||
|
||||
def _resolve_workflow_file_input_key(agent) -> str | None:
|
||||
forced = str(os.getenv("SHIPPING_NOTICE_EXTRACT_DIFY_FILE_INPUT_KEY") or "").strip()
|
||||
if forced:
|
||||
return forced
|
||||
|
||||
params = DifyService.get_app_parameters(agent) or {}
|
||||
|
||||
def _walk(v: Any) -> str | None:
|
||||
if isinstance(v, dict):
|
||||
var = str(v.get("variable") or v.get("name") or "").strip()
|
||||
t = str(v.get("type") or v.get("value_type") or v.get("valueType") or "").strip().lower()
|
||||
has_file_cfg = isinstance(v.get("file"), dict) or isinstance(v.get("file_upload"), dict) or isinstance(v.get("fileUpload"), dict)
|
||||
if var and (has_file_cfg or t in {"file", "files", "file_array", "filearray", "file-list", "file_list"}):
|
||||
return var
|
||||
for vv in v.values():
|
||||
hit = _walk(vv)
|
||||
if hit:
|
||||
return hit
|
||||
elif isinstance(v, list):
|
||||
for item in v:
|
||||
hit = _walk(item)
|
||||
if hit:
|
||||
return hit
|
||||
return None
|
||||
|
||||
return _walk(params)
|
||||
|
||||
|
||||
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))
|
||||
return _finalize_notice_parse(
|
||||
structured,
|
||||
base_confidence=float(structured.get("confidence") or 0.95),
|
||||
raw_text=str(structured.get("raw_text") or ""),
|
||||
)
|
||||
|
||||
agent = resolve_extract_agent()
|
||||
if agent and raw_text.strip():
|
||||
if agent and (raw_text.strip() or notice_files):
|
||||
try:
|
||||
inputs: dict[str, Any] = {"raw_text": raw_text, "source_type": source_type}
|
||||
if notice_files:
|
||||
key = _resolve_workflow_file_input_key(agent)
|
||||
if key:
|
||||
inputs[key] = notice_files
|
||||
else:
|
||||
warnings.append("workflow_file_input_key_missing")
|
||||
result = DifyService.run_workflow_streaming(
|
||||
agent=agent,
|
||||
inputs={"raw_text": raw_text, "source_type": source_type},
|
||||
inputs=inputs,
|
||||
user="shipping_notice_tool",
|
||||
)
|
||||
parsed = _parse_workflow_outputs(result)
|
||||
@@ -70,6 +125,7 @@ def extract_notice_fields(
|
||||
finalized, confidence, line_warnings = _finalize_notice_parse(
|
||||
parsed,
|
||||
base_confidence=float(parsed.get("confidence") or 0.85),
|
||||
raw_text=raw_text,
|
||||
)
|
||||
warnings.extend(line_warnings)
|
||||
return finalized, confidence, warnings
|
||||
@@ -79,7 +135,7 @@ def extract_notice_fields(
|
||||
|
||||
heuristic, confidence, heuristic_warnings = _heuristic_extract(raw_text)
|
||||
warnings.extend(heuristic_warnings)
|
||||
return _finalize_notice_parse(heuristic, base_confidence=confidence)
|
||||
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]]:
|
||||
@@ -92,6 +148,11 @@ def _heuristic_extract(raw_text: str) -> tuple[dict[str, Any], float, list[str]]
|
||||
|
||||
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,
|
||||
|
||||
Reference in new issue
Block a user