feat(shipping_notice): 完成货代发货通知全流程功能迭代与重构

主要变更:
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工具调用,删除已归档的旧部署文档
This commit is contained in:
hugo committed 2026-06-08 16:07:20 +08:00
1 parent 2ce5256483
commit c18c0394d0
50 files changed
+6088 -2407

No files matched your search

@@ -0,0 +1,56 @@
from app.services.shipping_notice.field_alignment_summary import format_field_alignment_summary
def test_format_field_alignment_summary_single_block():
field_alignment = {
"match_rule": "按 AWS Sku + Reservation Order 汇总 Qty Available 与订单 qty 比对。",
"match_rule_source": "llm",
"inventory_columns": {
"mapping": {
"sku": {"source_column": "AWS Sku"},
"reservation_id": {"source_column": "Reservation Order"},
"available_qty": {"source_column": "Qty Available"},
}
},
"sample_notice_lines": [
{"sku": "504-010213-001", "reservation_id": "1000140351", "qty": 9},
],
"sample_inventory_lines": [
{"sku": "504-010213-001", "reservation_id": "1000140351", "available_qty": 12},
],
"inventory_row_count": 120,
"notice_line_count": 1,
"warnings": [],
}
text = format_field_alignment_summary(
task_no="SN-20260602-AB12CD34",
field_alignment=field_alignment,
)
assert "【库存字段对齐确认" in text
assert "SN-20260602-AB12CD34" in text
assert "| 预订单号 | `Reservation Order` | 预订单号 |" in text
assert "| 料号 | `AWS Sku` | 料号 |" in text
rid_idx = text.index("| 预订单号 |")
sku_idx = text.index("| 料号 | `AWS Sku`")
assert rid_idx < sku_idx
assert "(LLM 推断)" in text
assert "订单样例:" in text
assert "预订单号 1000140351" in text
assert "料号 504-010213-001" in text
assert "回复「确认」进入匹配" in text
def test_format_field_alignment_summary_includes_warnings():
text = format_field_alignment_summary(
task_no="SN-1",
field_alignment={
"match_rule": "test",
"inventory_columns": {"mapping": {}},
"warnings": ["库存未识别 Reservation ID 相关列"],
},
warnings=["额外提示"],
)
assert "库存未识别 Reservation ID 相关列" in text
assert "额外提示" in text
@@ -51,18 +51,36 @@ def test_read_inventory_schema_preview_skips_title_row(app, tmp_path):
def test_parse_aws_inventory_headers(app):
with app.app_context():
headers = ["AWS Sku", "COM ID", "Qty Available", "Qty"]
headers = ["AWS Sku", "COM ID", "Qty Available", "Qty", "ODM ID"]
data = [
["10-40594-001", "AWS-311", 2, 1],
["10-40594-002", "ORD-3002", 0, 5],
["10-40594-001", "AWS-311", 2, 1, "16754796"],
["10-40594-002", "ORD-3002", 0, 5, "16754797"],
]
lines = parse_inventory_rows(headers, data)
assert len(lines) == 2
assert lines[0]["sku"] == "10-40594-001"
assert lines[0]["reservation_id"] == "AWS-311"
assert lines[0]["available_qty"] == 2.0
assert lines[0]["odm_id"] == "16754796"
assert lines[1]["reservation_id"] == "ORD-3002"
assert lines[1]["available_qty"] == 0.0
assert lines[1]["odm_id"] == "16754797"
def test_parse_inventory_rows_with_explicit_mapping_keeps_odm_id(app):
with app.app_context():
headers = ["AWS Sku", "COM ID", "Qty Available", "ODM ID"]
data = [["10-40594-001", "AWS-311", 2, "16754796"]]
lines = parse_inventory_rows(
headers,
data,
field_mapping={
"sku": "AWS Sku",
"reservation_id": "COM ID",
"available_qty": "Qty Available",
},
)
assert lines[0]["odm_id"] == "16754796"
def test_match_aggregates_same_sku_reservation():
@@ -15,9 +15,10 @@ def test_format_match_summary_fulfillable():
}
],
)
assert "匹配明细" in msg
assert "订单 2 / 库存 5" in msg
assert "充足" in msg
assert "【库存匹配结果】" in msg or "匹配明细" in msg
assert "| # | 预订单号 | 料号 |" in msg
assert "| 1 | AWS-311 | 10-40594-001 | 2 | 5 | - |" in msg
assert "✅ 充足" in msg
assert "可生成发货 Excel" in msg
@@ -35,6 +36,6 @@ def test_format_match_summary_shortage():
}
],
)
assert "订单 100 / 库存 5" in msg
assert "缺口 95" in msg
assert "| R-1 | A001 | 100 | 5 | 95 |" in msg
assert "⚠️ 部分满足" in msg
assert "部分行库存不足" in msg
@@ -20,9 +20,13 @@ def test_format_parse_summary_single_line():
],
)
assert "【发货通知解析确认】任务编号:SN-20260604-7249E4D7" in msg
assert "| PO | 16754796 |" in msg
assert "| SKU | 504-010213-001 |" in msg
assert "| Reservation ID | 1000140351 |" in msg
assert "| 采购单号 | 16754796 |" in msg
assert "| 预订单号 | 1000140351 |" in msg
assert "| 料号 | 504-010213-001 |" in msg
po_idx = msg.index("| 采购单号 |")
rid_idx = msg.index("| 预订单号 |")
sku_idx = msg.index("| 料号 |")
assert po_idx < rid_idx < sku_idx
assert "| 数量 | 9 PCS |" in msg
assert "| 单价 | $1,607.72 |" in msg
assert "| 金额 | $14,469.48 |" in msg
@@ -43,7 +47,7 @@ def test_format_parse_summary_multiple_lines():
)
assert "【明细 1】" in msg
assert "【明细 2】" in msg
assert "PO:PO-1" in msg
assert "采购单号:PO-1" in msg
def test_format_parse_summary_includes_warnings():
+75 -1
View File
@@ -37,7 +37,7 @@ def test_shipping_notice_tool_flow(app, client):
parse_body = parse_res.get_json()
assert parse_body["status"] in {"ok", "needs_human"}
assert "【发货通知解析确认】" in parse_body["message"]
assert "| SKU | A001 |" in parse_body["message"]
assert "| 料号 | A001 |" in parse_body["message"]
inv_res = client.post(
"/api/shipping-notice/tools/ensure_inventory",
@@ -491,6 +491,80 @@ def test_rpa_upload_callback(app, client, tmp_path):
assert cb_res.get_json()["data"]["freight_ref_no"] == "FR-001"
def test_generate_shipment_excel_writes_vendor_part_and_odm_id(app, client):
headers = _tool_headers(app)
create_res = client.post(
"/api/shipping-notice/tools/create_task",
headers=headers,
json={"source_type": "manual"},
)
task_id = create_res.get_json()["task_id"]
client.post(
"/api/shipping-notice/tools/parse_notice",
headers=headers,
json={
"task_id": task_id,
"structured": {
"customer_name": "Wistron",
"po_number": "SN-20260608-7FCBFA4B",
"lines": [
{
"sku": "504-010213-001",
"vendor_part_number": "504-010213-001",
"reservation_id": "1000140351",
"qty": 9,
"uom": "EA",
}
],
"confidence": 0.95,
},
},
)
client.post(
"/api/shipping-notice/tools/ensure_inventory",
headers=headers,
json={
"task_id": task_id,
"inventory_lines": [
{
"sku": "504-010213-001",
"reservation_id": "1000140351",
"available_qty": 9,
"odm_id": "16754796",
}
],
},
)
client.post(
"/api/shipping-notice/tools/match_inventory",
headers=headers,
json={"task_id": task_id},
)
excel_res = client.post(
"/api/shipping-notice/tools/generate_shipment_excel",
headers=headers,
json={"task_id": task_id},
)
assert excel_res.status_code == 200
assert excel_res.get_json()["status"] == "ok"
from openpyxl import load_workbook
with app.app_context():
task = ShippingTask.query.filter_by(task_no=task_id).first()
assert task is not None
assert task.export_file_path
wb = load_workbook(task.export_file_path, read_only=True)
header_ws = wb["PVMI Pull Request Header"]
detail_ws = wb["PVMI Pull Request Detail"]
assert header_ws.cell(4, 6).value == "16754796"
assert detail_ws.cell(4, 4).value == "504-010213-001"
wb.close()
def test_upload_inventory_file_parses_server_side(app, client, tmp_path):
headers = _tool_headers(app)
app.config["SHIPPING_NOTICE_STORAGE_ROOT"] = str(tmp_path / "shipping")
@@ -0,0 +1,78 @@
from pathlib import Path
import subprocess
from edkb_system.skills import soffice_util
def test_convert_with_soffice_uses_isolated_profile_and_cleans(monkeypatch, tmp_path):
profile_dir = tmp_path / "lo-profile-fixed"
def fake_mkdtemp(prefix=""):
profile_dir.mkdir(parents=True, exist_ok=True)
return str(profile_dir)
monkeypatch.setenv("EDKB_SOFFICE_ISOLATED_PROFILE", "1")
monkeypatch.setattr(soffice_util.tempfile, "mkdtemp", fake_mkdtemp)
captured = {}
def fake_run(cmd):
captured["cmd"] = cmd
assert profile_dir.exists()
return subprocess.CompletedProcess(cmd, 0, stdout="", stderr="")
monkeypatch.setattr(soffice_util, "run_soffice", fake_run)
source_path = tmp_path / "sample.doc"
source_path.write_text("dummy", encoding="utf-8")
out_dir = tmp_path / "out"
proc = soffice_util.convert_with_soffice(
"soffice",
convert_to="docx",
source_path=source_path,
out_dir=out_dir,
)
assert proc.returncode == 0
assert any(
str(item).startswith("-env:UserInstallation=") for item in captured["cmd"]
)
assert not profile_dir.exists()
def test_convert_with_soffice_can_use_shared_profile(monkeypatch, tmp_path):
called = {"mkdtemp": 0}
def fake_mkdtemp(prefix=""):
called["mkdtemp"] += 1
return str(tmp_path / "unused-profile")
monkeypatch.setenv("EDKB_SOFFICE_ISOLATED_PROFILE", "0")
monkeypatch.setattr(soffice_util.tempfile, "mkdtemp", fake_mkdtemp)
captured = {}
def fake_run(cmd):
captured["cmd"] = cmd
return subprocess.CompletedProcess(cmd, 0, stdout="", stderr="")
monkeypatch.setattr(soffice_util, "run_soffice", fake_run)
source_path = tmp_path / "sample.doc"
source_path.write_text("dummy", encoding="utf-8")
out_dir = tmp_path / "out"
proc = soffice_util.convert_with_soffice(
"soffice",
convert_to="docx",
source_path=source_path,
out_dir=out_dir,
)
assert proc.returncode == 0
assert called["mkdtemp"] == 0
assert not any(
str(item).startswith("-env:UserInstallation=") for item in captured["cmd"]
)