feat(queue): 添加旧知识库任务入队兼容入口
- 增加 enqueue_kb_job 和 enqueue_clean_job 函数以支持旧 KB 清洗任务入队 - 在 enqueue 函数中处理 KB_CLEAN 和 KB_DIFY_SYNC 类型的任务 - 引入 ValidationError 以处理 job_id 缺失的情况 feat(shipping_notice): 更新库存 RPA 工作流逻辑 - 修改 ensure_inventory_via_rpa 函数以支持手动重试逻辑 - 优化库存导出触发和状态更新的消息提示 fix(i18n): 更新任务未找到的提示信息 - 修改中文和英文提示,明确用户操作要求 feat(shipping_notice): 增强任务解析逻辑 - 添加缺失 Reservation ID 的检查,阻止任务进入确认阶段 - 更新 get_task_status 函数以处理多票会话的任务解析
This commit is contained in:
1 parent
eb0466245b
commit
4cd987eef6
45 files changed
+2748
-1406
No files matched your search
@@ -6,6 +6,7 @@ from app.services.shipping_notice.match import match_notice_lines
|
||||
|
||||
|
||||
def _tool_headers(app):
|
||||
app.config["SHIPPING_NOTICE_TOOL_API_KEY"] = ""
|
||||
app.config["INTEGRATION_API_KEY"] = "test-integration-key"
|
||||
return {
|
||||
"Content-Type": "application/json",
|
||||
@@ -13,6 +14,51 @@ def _tool_headers(app):
|
||||
}
|
||||
|
||||
|
||||
def test_shipping_notice_tool_auth_accepts_dedicated_key(app, client):
|
||||
with app.app_context():
|
||||
app.config["SHIPPING_NOTICE_TOOL_API_KEY"] = "shipping-secret"
|
||||
app.config["INTEGRATION_API_KEY"] = ""
|
||||
|
||||
res = client.post(
|
||||
"/api/shipping-notice/tools/create_task",
|
||||
headers={"Content-Type": "application/json", "X-Integration-Key": "shipping-secret"},
|
||||
json={"source_type": "manual"},
|
||||
)
|
||||
|
||||
assert res.status_code == 200
|
||||
assert res.get_json()["status"] == "ok"
|
||||
|
||||
|
||||
def test_shipping_notice_tool_auth_falls_back_to_integration_key(app, client):
|
||||
with app.app_context():
|
||||
app.config["SHIPPING_NOTICE_TOOL_API_KEY"] = ""
|
||||
app.config["INTEGRATION_API_KEY"] = "integration-secret"
|
||||
|
||||
res = client.post(
|
||||
"/api/shipping-notice/tools/create_task",
|
||||
headers={"Content-Type": "application/json", "X-Integration-Key": "integration-secret"},
|
||||
json={"source_type": "manual"},
|
||||
)
|
||||
|
||||
assert res.status_code == 200
|
||||
assert res.get_json()["status"] == "ok"
|
||||
|
||||
|
||||
def test_shipping_notice_tool_auth_reports_missing_config(app, client):
|
||||
with app.app_context():
|
||||
app.config["SHIPPING_NOTICE_TOOL_API_KEY"] = ""
|
||||
app.config["INTEGRATION_API_KEY"] = ""
|
||||
|
||||
res = client.post(
|
||||
"/api/shipping-notice/tools/create_task",
|
||||
headers={"Content-Type": "application/json", "X-Integration-Key": "any"},
|
||||
json={"source_type": "manual"},
|
||||
)
|
||||
|
||||
assert res.status_code == 503
|
||||
assert res.get_json()["error_code"] == "missing_auth"
|
||||
|
||||
|
||||
def test_match_notice_lines_reports_progress():
|
||||
seen: list[tuple[int, int]] = []
|
||||
|
||||
@@ -116,6 +162,31 @@ def test_shipping_notice_tool_flow(app, client):
|
||||
assert status_res.get_json()["status"] == "ok"
|
||||
|
||||
|
||||
def test_contract_get_task_status_accepts_task_no_anchor(app, client):
|
||||
"""契约:用户输入 SN-... 时,Dify 应直连 get_task_status 并锚定对应任务。"""
|
||||
headers = _tool_headers(app)
|
||||
|
||||
create_res = client.post(
|
||||
"/api/shipping-notice/tools/create_task",
|
||||
headers=headers,
|
||||
json={"source_type": "manual", "raw_content": "客户:ACME 料号A001 x 10"},
|
||||
)
|
||||
task_id = create_res.get_json()["task_id"]
|
||||
|
||||
status_res = client.post(
|
||||
"/api/shipping-notice/tools/get_task_status",
|
||||
headers=headers,
|
||||
json={"task_no": task_id},
|
||||
)
|
||||
data = status_res.get_json()
|
||||
|
||||
assert status_res.status_code == 200
|
||||
assert data["status"] == "ok"
|
||||
assert data["task_id"] == task_id
|
||||
assert data["data"]["task_no"] == task_id
|
||||
assert "当前进度" in data["message"]
|
||||
|
||||
|
||||
def test_shipping_notice_match_shortage_needs_human(app, client):
|
||||
headers = _tool_headers(app)
|
||||
|
||||
@@ -157,6 +228,8 @@ def test_shipping_notice_match_shortage_needs_human(app, client):
|
||||
assert "confirm" not in options
|
||||
assert "reparse_inventory" in options
|
||||
assert "rematch" in options
|
||||
assert "generate_shipment_excel" in (data.get("forbidden_next_tools") or [])
|
||||
assert "upload_to_freight" in (data.get("forbidden_next_tools") or [])
|
||||
|
||||
confirm_res = client.post(
|
||||
"/api/shipping-notice/tools/submit_decision",
|
||||
@@ -227,6 +300,62 @@ def test_submit_decision_rematch_reopens_shortage_for_worker(app, client, monkey
|
||||
assert task.line_results_json is None
|
||||
|
||||
|
||||
def test_contract_submit_decision_rematch_is_direct_recovery_entry(app, client, monkeypatch):
|
||||
"""契约:重新匹配必须通过 submit_decision(rematch) 恢复当前任务,不经 Agent 自由续写。"""
|
||||
headers = _tool_headers(app)
|
||||
started: list[str] = []
|
||||
|
||||
from app.services.shipping_notice import decision_workflow
|
||||
|
||||
monkeypatch.setattr(
|
||||
decision_workflow,
|
||||
"start_task_orchestrator",
|
||||
lambda task_no: started.append(str(task_no)) or True,
|
||||
)
|
||||
|
||||
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": "ACME",
|
||||
"lines": [{"sku": "A001", "reservation_id": "R-1", "qty": 20, "uom": "EA"}],
|
||||
"confidence": 0.95,
|
||||
},
|
||||
},
|
||||
)
|
||||
client.post(
|
||||
"/api/shipping-notice/tools/ensure_inventory",
|
||||
headers=headers,
|
||||
json={"task_id": task_id, "inventory_lines": [{"sku": "A001", "reservation_id": "R-1", "available_qty": 1}]},
|
||||
)
|
||||
client.post(
|
||||
"/api/shipping-notice/tools/match_inventory",
|
||||
headers=headers,
|
||||
json={"task_id": task_id},
|
||||
)
|
||||
|
||||
rematch_res = client.post(
|
||||
"/api/shipping-notice/tools/submit_decision",
|
||||
headers=headers,
|
||||
json={"task_id": task_id, "decision": "rematch", "comment": "contract direct rematch"},
|
||||
)
|
||||
data = rematch_res.get_json()
|
||||
|
||||
assert rematch_res.status_code == 200
|
||||
assert data["status"] == "running"
|
||||
assert data["task_id"] == task_id
|
||||
assert started == [task_id]
|
||||
|
||||
|
||||
def test_openapi_spec_public(app, client):
|
||||
app.config["HXOPS_TOOLS_API_BASE"] = "http://tool-api.example.com"
|
||||
res = client.get("/api/shipping-notice/tools/openapi.json")
|
||||
@@ -245,6 +374,13 @@ def test_openapi_spec_public(app, client):
|
||||
assert "/tools/switch_focus_task" not in spec["paths"]
|
||||
assert "/tools/get_inventory_schema_preview" in spec["paths"]
|
||||
assert "/tools/apply_inventory_mapping" in spec["paths"]
|
||||
# 锁定 Dify Tool 契约:OpenAPI 暴露字符串,避免 Dify 画布无法选择上游 map_json 变量。
|
||||
apply_mapping = spec["paths"]["/tools/apply_inventory_mapping"]["post"]["requestBody"]["content"][
|
||||
"application/json"
|
||||
]["schema"]
|
||||
map_json_schema = apply_mapping["properties"]["map_json"]
|
||||
assert map_json_schema["type"] == "string"
|
||||
assert "JSON 字符串" in map_json_schema["description"]
|
||||
submit_decision = spec["paths"]["/tools/submit_decision"]["post"]["requestBody"]["content"]["application/json"]["schema"]
|
||||
assert "retry_rpa" in submit_decision["properties"]["decision"]["enum"]
|
||||
|
||||
@@ -734,7 +870,7 @@ def test_upload_inventory_file_parses_server_side(app, client, tmp_path):
|
||||
headers=headers,
|
||||
json={
|
||||
"task_id": task_id,
|
||||
"column_mapping_json": {
|
||||
"map_json": {
|
||||
**preview["suggested_mapping"],
|
||||
"match_rule": "LLM:Reservation Order 对齐订单 reservation_id,汇总 Qty Available。",
|
||||
},
|
||||
@@ -990,6 +1126,57 @@ def test_confirm_parse_does_not_skip_inventory_match(app, client):
|
||||
assert task.match_status is None
|
||||
|
||||
|
||||
@pytest.mark.auto_rpa
|
||||
def test_parse_notice_blocks_when_reservation_id_missing(app, client, monkeypatch):
|
||||
import app.services.shipping_notice.task_workflow as task_workflow
|
||||
|
||||
headers = _tool_headers(app)
|
||||
called: dict[str, str] = {}
|
||||
|
||||
def fake_start_task_orchestrator(task_no: str | None) -> bool:
|
||||
called["task_no"] = str(task_no or "")
|
||||
return True
|
||||
|
||||
monkeypatch.setattr(task_workflow, "start_task_orchestrator", fake_start_task_orchestrator)
|
||||
|
||||
create_res = client.post(
|
||||
"/api/shipping-notice/tools/create_task",
|
||||
headers=headers,
|
||||
json={"source_type": "manual"},
|
||||
)
|
||||
task_id = create_res.get_json()["task_id"]
|
||||
|
||||
parse_res = client.post(
|
||||
"/api/shipping-notice/tools/parse_notice",
|
||||
headers=headers,
|
||||
json={
|
||||
"task_id": task_id,
|
||||
"structured": {
|
||||
"customer_name": "ACME",
|
||||
"lines": [{"sku": "A001", "qty": 10, "uom": "EA"}],
|
||||
"confidence": 0.95,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
parse_data = parse_res.get_json()
|
||||
assert parse_data["status"] == "needs_human"
|
||||
assert parse_data["checkpoint"] == "rsv_req"
|
||||
assert "解析失败" in parse_data["message"]
|
||||
assert "Reservation ID / COM ID" in parse_data["message"]
|
||||
assert "confirm" not in parse_data.get("human_options", [])
|
||||
assert called == {}
|
||||
|
||||
with app.app_context():
|
||||
task = ShippingTask.query.filter_by(task_no=task_id).first()
|
||||
assert task is not None
|
||||
assert task.process_status == "pending_review"
|
||||
assert task.review_status == "rsv_req"
|
||||
assert task.pending_human_reason == "rsv_req"
|
||||
assert task.error_message and "解析失败" in task.error_message
|
||||
assert task.match_status is None
|
||||
|
||||
|
||||
@pytest.mark.auto_rpa
|
||||
def test_parse_notice_auto_continue_hides_parse_confirmation_hint(app, client, monkeypatch):
|
||||
import app.services.shipping_notice.task_workflow as task_workflow
|
||||
@@ -1184,6 +1371,15 @@ def test_rpa_pending_timeout_recheck_triggers_next_inventory_export(app, client,
|
||||
assert task is not None
|
||||
task.process_status = "waiting_inventory"
|
||||
task.pending_human_reason = PH_RPA_PENDING
|
||||
task.step_payload_json = {
|
||||
"rpa_inventory_source": {
|
||||
"source_code": "DSV360_AWSDU",
|
||||
"source_type": "dsv360",
|
||||
"display_name": "DSV 360 / Dublin / AWSDU - Prod (JDA)",
|
||||
"entry_url": "https://s360.dbschenker.com/",
|
||||
"query": {"program_label": "AWSDU - Prod (JDA)", "owner_code": "CADU-0000000100"},
|
||||
}
|
||||
}
|
||||
db.session.add(
|
||||
ShippingTaskEvent(
|
||||
task_id=task.id,
|
||||
@@ -1229,6 +1425,15 @@ def test_retry_rpa_after_exhausted_starts_new_attempt_window(app, client, tmp_pa
|
||||
assert task is not None
|
||||
task.process_status = "waiting_inventory"
|
||||
task.pending_human_reason = PH_RPA_EXHAUSTED
|
||||
task.step_payload_json = {
|
||||
"rpa_inventory_source": {
|
||||
"source_code": "DSV360_AWSDU",
|
||||
"source_type": "dsv360",
|
||||
"display_name": "DSV 360 / Dublin / AWSDU - Prod (JDA)",
|
||||
"entry_url": "https://s360.dbschenker.com/",
|
||||
"query": {"program_label": "AWSDU - Prod (JDA)", "owner_code": "CADU-0000000100"},
|
||||
}
|
||||
}
|
||||
base_time = datetime.utcnow() - timedelta(minutes=5)
|
||||
for idx in range(3):
|
||||
db.session.add(
|
||||
@@ -1248,7 +1453,7 @@ def test_retry_rpa_after_exhausted_starts_new_attempt_window(app, client, tmp_pa
|
||||
)
|
||||
retry_data = retry_res.get_json()
|
||||
assert retry_data["status"] == "running"
|
||||
assert "新一轮第 1/3 次" in retry_data["message"]
|
||||
assert retry_data.get("data", {}).get("rpa", {}).get("attempts") == 1
|
||||
|
||||
with app.app_context():
|
||||
task = ShippingTask.query.filter_by(task_no=task_id).first()
|
||||
@@ -1257,8 +1462,8 @@ def test_retry_rpa_after_exhausted_starts_new_attempt_window(app, client, tmp_pa
|
||||
assert task.pending_human_reason == "rpa_pending"
|
||||
assert task.error_message is None
|
||||
progress = (task.step_payload_json or {}).get("current_step_progress") or {}
|
||||
assert progress.get("message") == "已手动重新触发 RPA 库存导出,等待结果"
|
||||
assert progress.get("detail") == "新一轮第 1/3 次尝试"
|
||||
assert progress.get("current") == 1
|
||||
assert progress.get("extra", {}).get("manual_retry_round") is True
|
||||
assert ShippingTaskEvent.query.filter_by(task_id=task.id, event_type="decision_retry_rpa").count() == 1
|
||||
assert ShippingTaskEvent.query.filter_by(task_id=task.id, event_type="inventory_rpa_triggered").count() == 4
|
||||
assert len(list(trigger.glob("inventory_export_*.json"))) == 1
|
||||
@@ -1299,6 +1504,15 @@ def test_batch_retry_rechecks_timed_out_rpa_pending_immediately(app, client, tmp
|
||||
assert task is not None
|
||||
task.process_status = "waiting_inventory"
|
||||
task.pending_human_reason = PH_RPA_PENDING
|
||||
task.step_payload_json = {
|
||||
"rpa_inventory_source": {
|
||||
"source_code": "DSV360_AWSDU",
|
||||
"source_type": "dsv360",
|
||||
"display_name": "DSV 360 / Dublin / AWSDU - Prod (JDA)",
|
||||
"entry_url": "https://s360.dbschenker.com/",
|
||||
"query": {"program_label": "AWSDU - Prod (JDA)", "owner_code": "CADU-0000000100"},
|
||||
}
|
||||
}
|
||||
db.session.add(
|
||||
ShippingTaskEvent(
|
||||
task_id=task.id,
|
||||
@@ -1340,7 +1554,7 @@ def test_submit_decision_by_conversation_id(app, client):
|
||||
"conversation_id": "conv-test-001",
|
||||
"structured": {
|
||||
"customer_name": "Wistron",
|
||||
"lines": [{"sku": "504-010213-001", "qty": 9, "uom": "EA"}],
|
||||
"lines": [{"sku": "504-010213-001", "reservation_id": "1000140351", "qty": 9, "uom": "EA"}],
|
||||
"confidence": 0.95,
|
||||
},
|
||||
},
|
||||
@@ -1365,6 +1579,60 @@ def test_submit_decision_by_conversation_id(app, client):
|
||||
assert dup_data.get("checkpoint") == "task_exists"
|
||||
|
||||
|
||||
def test_submit_decision_requires_task_id_when_conversation_has_multiple_tasks(app, client):
|
||||
headers = _tool_headers(app)
|
||||
|
||||
first = ShippingTask(
|
||||
task_no="SN-MULTI-CONV-001",
|
||||
conversation_id="conv-test-multi",
|
||||
process_status="pending_review",
|
||||
review_status="parse_review",
|
||||
pending_human_reason="low_confidence",
|
||||
source_type="manual",
|
||||
lines_json=[{"sku": "A001", "reservation_id": "1000140351", "qty": 1}],
|
||||
)
|
||||
second = ShippingTask(
|
||||
task_no="SN-MULTI-CONV-002",
|
||||
conversation_id="conv-test-multi",
|
||||
process_status="pending_review",
|
||||
review_status="parse_review",
|
||||
pending_human_reason="low_confidence",
|
||||
source_type="manual",
|
||||
lines_json=[{"sku": "A002", "reservation_id": "1000140352", "qty": 1}],
|
||||
)
|
||||
with app.app_context():
|
||||
db.session.add_all([first, second])
|
||||
db.session.commit()
|
||||
|
||||
confirm_res = client.post(
|
||||
"/api/shipping-notice/tools/submit_decision",
|
||||
headers=headers,
|
||||
json={"conversation_id": "conv-test-multi", "decision": "confirm"},
|
||||
)
|
||||
confirm_data = confirm_res.get_json()
|
||||
assert confirm_data["status"] == "failed"
|
||||
assert "任务编号" in confirm_data["message"] or "task ID" in confirm_data["message"]
|
||||
|
||||
status_res = client.post(
|
||||
"/api/shipping-notice/tools/get_task_status",
|
||||
headers=headers,
|
||||
json={"conversation_id": "conv-test-multi"},
|
||||
)
|
||||
status_data = status_res.get_json()
|
||||
assert status_data["status"] == "failed"
|
||||
assert "任务编号" in status_data["message"]
|
||||
|
||||
with app.app_context():
|
||||
rows = {
|
||||
task.task_no: task
|
||||
for task in ShippingTask.query.filter(ShippingTask.task_no.in_(["SN-MULTI-CONV-001", "SN-MULTI-CONV-002"]))
|
||||
.order_by(ShippingTask.task_no)
|
||||
.all()
|
||||
}
|
||||
assert rows["SN-MULTI-CONV-001"].process_status == "pending_review"
|
||||
assert rows["SN-MULTI-CONV-002"].process_status == "pending_review"
|
||||
|
||||
|
||||
def test_submit_decision_retry_rpa_routes_to_inventory_workflow(app, client, monkeypatch):
|
||||
headers = _tool_headers(app)
|
||||
|
||||
|
||||
Reference in new issue
Block a user