feat(freight-rpa): 批次R解耦编排层并切换 onhand/inbound 契约

将 RPA 编排收编至 freight_rpa,回调迁 /api/freight-rpa/*。
共享目录与 Worker 契约一次切换至 onhand/inbound/upload,
并补 outbound 骨架与 tt16 排程 job key 迁移。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
hugoandCursor committed 2026-10-17 23:18:48 +08:00
1 parent bf3154033b
commit ffc0d4a430
147 files changed
+4221 -2678

No files matched your search

@@ -22,8 +22,8 @@ from pathlib import Path
def _load_worker():
repo_root = next((_p for _p in Path(__file__).resolve().parents if (_p / "backend").is_dir() and (_p / "frontend").is_dir()), Path(__file__).resolve().parents[2])
module_path = repo_root / "scripts" / "freight_rpa" / "inventory" / "inventory_rpa_worker.py"
spec = importlib.util.spec_from_file_location("inventory_rpa_worker_crane_smoke", module_path)
module_path = repo_root / "scripts" / "freight_rpa" / "onhand" / "onhand_rpa_worker.py"
spec = importlib.util.spec_from_file_location("onhand_rpa_worker_crane_smoke", module_path)
assert spec and spec.loader
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
@@ -2,7 +2,7 @@
scripts/freight_rpa/crane_report_export.py
用途:Crane On Hand Details Query 异步报表导出(Run Report → Schedule Report → Scheduled Reports 下载)。
依赖:Playwright Page、download_artifact_fallback;由 inventory_rpa_worker._export_crane_inventory_source 委托调用。
依赖:Playwright Page、download_artifact_fallback;由 onhand_rpa_worker._export_crane_onhand_source 委托调用。
逻辑:不填写页面查询条件;必须先 Run Report 产出查询结果,再 Schedule 异步导出;
轮询仅在 scheduled-reports 路由进行;expect_download 超时时按下载目录新文件做短窗口兜底。
"""
@@ -18,13 +18,13 @@ _RPA_ROOT = next(
for _c in _Path(__file__).resolve().parents
if (_c / "connectors").is_dir()
and (_c / "common").is_dir()
and (_c / "inventory").is_dir()
and (_c / "receipt").is_dir()
and (_c / "onhand").is_dir()
and (_c / "inbound").is_dir()
),
None,
)
if _RPA_ROOT is not None:
for _rel in ("", "common", "connectors", "connectors/dsv", "connectors/crane", "inventory", "receipt"):
for _rel in ("", "common", "connectors", "connectors/dsv", "connectors/crane", "onhand", "inbound"):
_dir = str(_RPA_ROOT) if not _rel else str(_RPA_ROOT / _rel)
if _dir not in _sys.path:
_sys.path.insert(0, _dir)
@@ -1167,7 +1167,7 @@ def export_crane_inventory_report(
browser_timeout_ms: 浏览器操作超时。
select_or_fill: worker 提供的下拉/输入填充函数。
click_text: worker 提供的文本点击函数(file_download 直出模式备用)。
trigger: inventory_export / batch trigger,用于生成唯一 Description。
trigger: onhand_export / batch trigger,用于生成唯一 Description。
Returns:
Playwright Download 或 FilesystemDownloadArtifact。
@@ -17,11 +17,11 @@ from pathlib import Path as _Path
_RPA_ROOT = next(
(_c for _c in _Path(__file__).resolve().parents
if (_c / "connectors").is_dir() and (_c / "common").is_dir()
and (_c / "inventory").is_dir() and (_c / "receipt").is_dir()),
and (_c / "onhand").is_dir() and (_c / "inbound").is_dir()),
None,
)
if _RPA_ROOT is not None:
for _rel in ("", "common", "connectors", "connectors/dsv", "connectors/crane", "inventory", "receipt"):
for _rel in ("", "common", "connectors", "connectors/dsv", "connectors/crane", "onhand", "inbound"):
_dir = str(_RPA_ROOT) if not _rel else str(_RPA_ROOT / _rel)
if _dir not in _sys.path:
_sys.path.insert(0, _dir)
@@ -18,11 +18,11 @@ from pathlib import Path as _Path
_RPA_ROOT = next(
(_c for _c in _Path(__file__).resolve().parents
if (_c / "connectors").is_dir() and (_c / "common").is_dir()
and (_c / "inventory").is_dir() and (_c / "receipt").is_dir()),
and (_c / "onhand").is_dir() and (_c / "inbound").is_dir()),
None,
)
if _RPA_ROOT is not None:
for _rel in ("", "common", "connectors", "connectors/dsv", "connectors/crane", "inventory", "receipt"):
for _rel in ("", "common", "connectors", "connectors/dsv", "connectors/crane", "onhand", "inbound"):
_dir = str(_RPA_ROOT) if not _rel else str(_RPA_ROOT / _rel)
if _dir not in _sys.path:
_sys.path.insert(0, _dir)
@@ -14,11 +14,11 @@ from pathlib import Path as _Path
_RPA_ROOT = next(
(_c for _c in _Path(__file__).resolve().parents
if (_c / "connectors").is_dir() and (_c / "common").is_dir()
and (_c / "inventory").is_dir() and (_c / "receipt").is_dir()),
and (_c / "onhand").is_dir() and (_c / "inbound").is_dir()),
None,
)
if _RPA_ROOT is not None:
for _rel in ("", "common", "connectors", "connectors/dsv", "connectors/crane", "inventory", "receipt"):
for _rel in ("", "common", "connectors", "connectors/dsv", "connectors/crane", "onhand", "inbound"):
_dir = str(_RPA_ROOT) if not _rel else str(_RPA_ROOT / _rel)
if _dir not in _sys.path:
_sys.path.insert(0, _dir)
@@ -14,11 +14,11 @@ from pathlib import Path as _Path
_RPA_ROOT = next(
(_c for _c in _Path(__file__).resolve().parents
if (_c / "connectors").is_dir() and (_c / "common").is_dir()
and (_c / "inventory").is_dir() and (_c / "receipt").is_dir()),
and (_c / "onhand").is_dir() and (_c / "inbound").is_dir()),
None,
)
if _RPA_ROOT is not None:
for _rel in ("", "common", "connectors", "connectors/dsv", "connectors/crane", "inventory", "receipt"):
for _rel in ("", "common", "connectors", "connectors/dsv", "connectors/crane", "onhand", "inbound"):
_dir = str(_RPA_ROOT) if not _rel else str(_RPA_ROOT / _rel)
if _dir not in _sys.path:
_sys.path.insert(0, _dir)
@@ -22,8 +22,8 @@ from pathlib import Path
def _load_worker():
repo_root = next((_p for _p in Path(__file__).resolve().parents if (_p / "backend").is_dir() and (_p / "frontend").is_dir()), Path(__file__).resolve().parents[2])
module_path = repo_root / "scripts" / "freight_rpa" / "inventory" / "inventory_rpa_worker.py"
spec = importlib.util.spec_from_file_location("inventory_rpa_worker_smoke", module_path)
module_path = repo_root / "scripts" / "freight_rpa" / "onhand" / "onhand_rpa_worker.py"
spec = importlib.util.spec_from_file_location("onhand_rpa_worker_smoke", module_path)
assert spec and spec.loader
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
@@ -14,11 +14,11 @@ from pathlib import Path as _Path
_RPA_ROOT = next(
(_c for _c in _Path(__file__).resolve().parents
if (_c / "connectors").is_dir() and (_c / "common").is_dir()
and (_c / "inventory").is_dir() and (_c / "receipt").is_dir()),
and (_c / "onhand").is_dir() and (_c / "inbound").is_dir()),
None,
)
if _RPA_ROOT is not None:
for _rel in ("", "common", "connectors", "connectors/dsv", "connectors/crane", "inventory", "receipt"):
for _rel in ("", "common", "connectors", "connectors/dsv", "connectors/crane", "onhand", "inbound"):
_dir = str(_RPA_ROOT) if not _rel else str(_RPA_ROOT / _rel)
if _dir not in _sys.path:
_sys.path.insert(0, _dir)
@@ -16,11 +16,11 @@ from pathlib import Path as _Path
_RPA_ROOT = next(
(_c for _c in _Path(__file__).resolve().parents
if (_c / "connectors").is_dir() and (_c / "common").is_dir()
and (_c / "inventory").is_dir() and (_c / "receipt").is_dir()),
and (_c / "onhand").is_dir() and (_c / "inbound").is_dir()),
None,
)
if _RPA_ROOT is not None:
for _rel in ("", "common", "connectors", "connectors/dsv", "connectors/crane", "inventory", "receipt"):
for _rel in ("", "common", "connectors", "connectors/dsv", "connectors/crane", "onhand", "inbound"):
_dir = str(_RPA_ROOT) if not _rel else str(_RPA_ROOT / _rel)
if _dir not in _sys.path:
_sys.path.insert(0, _dir)
@@ -100,7 +100,7 @@ _DSV_INVENTORY_BODY_MARKERS: tuple[str, ...] = (
)
# Receipt Detail 专用正文标记(AWSRTM 实机列头,2026-09-28);可与库存共享 UI 词,不含库存专用语义。
_DSV_RECEIPT_BODY_MARKERS: tuple[str, ...] = (
_DSV_INBOUND_BODY_MARKERS: tuple[str, ...] = (
"receipt detail",
"external asn",
"wms receipt",
@@ -117,26 +117,26 @@ _DSV_RECEIPT_BODY_MARKERS: tuple[str, ...] = (
def _dsv_export_kind(source: dict[str, Any] | None = None, *, kind: str | None = None) -> str:
"""
解析导出语义:inventory | receipt。
解析导出语义:inventory(onhand)| inbound(原 receipt)。
核心逻辑:显式 kind / source.__export_kind__ 优先;否则按报表名含 receipt detail 推断。
"""
explicit = str(kind or "").strip().lower()
if explicit in {"inventory", "receipt"}:
if explicit in {"inventory", "inbound"}:
return explicit
if isinstance(source, dict):
flagged = str(source.get("__export_kind__") or "").strip().lower()
if flagged in {"inventory", "receipt"}:
if flagged in {"inventory", "inbound"}:
return flagged
report = _dsv_report_name(source).casefold()
if "receipt detail" in report:
return "receipt"
return "inbound"
return "inventory"
def _dsv_body_markers_for_kind(kind: str) -> tuple[str, ...]:
"""按 export_kind 返回结果正文标记;收料与库存字段列表分离。"""
return _DSV_RECEIPT_BODY_MARKERS if kind == "receipt" else _DSV_INVENTORY_BODY_MARKERS
return _DSV_INBOUND_BODY_MARKERS if kind == "inbound" else _DSV_INVENTORY_BODY_MARKERS
def _click_dsv_export_excel(page: Any, *, kind: str = "inventory") -> None:
@@ -746,7 +746,7 @@ def _wait_for_dsv_report_result_ready(page: Any, timeout_ms: int, *, kind: str =
f"frameIndex={frame_index}"
)
ready = bool(state["bodyReady"] and not state["asyncWait"])
if export_kind == "receipt":
if export_kind == "inbound":
# 收料:Export 工具栏或 Receipt Detail 正文均可作为就绪信号。
ready = (not state["asyncWait"]) and (state["bodyReady"] or state["exportReady"])
if ready:
@@ -1003,7 +1003,7 @@ def _ensure_dsv_inventory_rows_or_export_fallback(page: Any, *, report_name: str
# Receipt Detail 明细行探针:表头看 External ASN / WMS Receipt / Qty Expected|Received,不用库存 Qty Available。
_DSV_RECEIPT_DATA_ROWS_PROBE_JS = """
_DSV_INBOUND_DATA_ROWS_PROBE_JS = """
() => {
const normalize = (text) => (text || '').replace(/\\s+/g, ' ').trim();
const PARAMETER_ROOT_SELECTORS = [
@@ -1102,56 +1102,56 @@ _DSV_RECEIPT_DATA_ROWS_PROBE_JS = """
"""
def _dsv_report_has_receipt_data_rows(page: Any) -> bool:
def _dsv_report_has_inbound_data_rows(page: Any) -> bool:
"""
判断 Receipt Detail 结果页是否已出现收料明细行。
判断 Receipt Detail 结果页是否已出现入站明细行。
核心逻辑:结构对齐库存行探针,但表头/明细字段用 External ASN、WMS Receipt、
Qty Expected/Received 等收料列,禁止复用 Qty Available 库存判定。
"""
selected = _find_dsv_report_result_frame(page, kind="receipt")
selected = _find_dsv_report_result_frame(page, kind="inbound")
if selected is None:
return False
_frame_index, frame, _state = selected
try:
return bool(frame.evaluate(_DSV_RECEIPT_DATA_ROWS_PROBE_JS))
return bool(frame.evaluate(_DSV_INBOUND_DATA_ROWS_PROBE_JS))
except Exception as exc:
print(f"[dsv-report] receipt data row probe failed: {exc}", file=sys.stderr)
print(f"[dsv-report] inbound data row probe failed: {exc}", file=sys.stderr)
return False
def _wait_dsv_report_receipt_rows(page: Any, *, timeout_ms: int) -> bool:
def _wait_dsv_report_inbound_rows(page: Any, *, timeout_ms: int) -> bool:
"""条件轮询等待 Receipt Detail 明细行;超时返回 False,交由导出+Excel 判空。"""
deadline = time.monotonic() + max(1.0, timeout_ms / 1000)
while time.monotonic() < deadline:
if _dsv_report_has_receipt_data_rows(page):
if _dsv_report_has_inbound_data_rows(page):
return True
time.sleep(1.0)
return False
def _ensure_dsv_receipt_rows_or_export_fallback(page: Any, *, report_name: str) -> None:
def _ensure_dsv_inbound_rows_or_export_fallback(page: Any, *, report_name: str) -> None:
"""
收料 DOM 有行则继续;无行重试一次 View Report,仍无行也不抛空——交给 Excel 判空。
核心逻辑:与库存兜底同构,但就绪等待与行探针均走 receipt kind。
核心逻辑:与库存兜底同构,但就绪等待与行探针均走 inbound kind。
"""
row_wait_ms = min(_browser_timeout_ms(), 45_000)
if _wait_dsv_report_receipt_rows(page, timeout_ms=row_wait_ms):
if _wait_dsv_report_inbound_rows(page, timeout_ms=row_wait_ms):
return
print(
f"[dsv-report] receipt rows not visible after ready reportName={report_name!r}; "
f"[dsv-report] inbound rows not visible after ready reportName={report_name!r}; "
"retry View Report once",
file=sys.stderr,
)
_click_dsv_view_report(page)
_wait_dsv_report_ready(page, timeout_ms=_browser_timeout_ms(), kind="receipt")
if _wait_dsv_report_receipt_rows(page, timeout_ms=row_wait_ms):
_wait_dsv_report_ready(page, timeout_ms=_browser_timeout_ms(), kind="inbound")
if _wait_dsv_report_inbound_rows(page, timeout_ms=row_wait_ms):
return
print(
f"[dsv-report] receipt rows still missing after View Report retry "
f"[dsv-report] inbound rows still missing after View Report retry "
f"reportName={report_name!r}; proceed to Excel export fallback",
file=sys.stderr,
)
@@ -1728,8 +1728,8 @@ def _run_dsv_inventory_export_from_form(page: Any, source: dict[str, Any]) -> An
_wait_dsv_report_ready(page, timeout_ms=_browser_timeout_ms(), kind=export_kind)
# [STEP 2] DOM 明细行仅作加速;无行时重试 View Report,仍无则继续导出,
# 真空由落盘 Excel 判定(避免慢渲染假空 skip)。收料用 Receipt Detail 行探针。
if export_kind == "receipt":
_ensure_dsv_receipt_rows_or_export_fallback(page, report_name=report_name)
if export_kind == "inbound":
_ensure_dsv_inbound_rows_or_export_fallback(page, report_name=report_name)
else:
_ensure_dsv_inventory_rows_or_export_fallback(page, report_name=report_name)
except Exception as exc:
@@ -1900,9 +1900,9 @@ wait_dsv_report_ready = _wait_dsv_report_ready
dsv_report_has_inventory_data_rows = _dsv_report_has_inventory_data_rows
wait_dsv_report_inventory_rows = _wait_dsv_report_inventory_rows
ensure_dsv_inventory_rows_or_export_fallback = _ensure_dsv_inventory_rows_or_export_fallback
dsv_report_has_receipt_data_rows = _dsv_report_has_receipt_data_rows
wait_dsv_report_receipt_rows = _wait_dsv_report_receipt_rows
ensure_dsv_receipt_rows_or_export_fallback = _ensure_dsv_receipt_rows_or_export_fallback
dsv_report_has_inbound_data_rows = _dsv_report_has_inbound_data_rows
wait_dsv_report_inbound_rows = _wait_dsv_report_inbound_rows
ensure_dsv_inbound_rows_or_export_fallback = _ensure_dsv_inbound_rows_or_export_fallback
dsv_export_kind = _dsv_export_kind
dsv_body_markers_for_kind = _dsv_body_markers_for_kind
fill_dsv_filter_if_needed = _fill_dsv_filter_if_needed
@@ -16,11 +16,11 @@ from pathlib import Path as _Path
_RPA_ROOT = next(
(_c for _c in _Path(__file__).resolve().parents
if (_c / "connectors").is_dir() and (_c / "common").is_dir()
and (_c / "inventory").is_dir() and (_c / "receipt").is_dir()),
and (_c / "onhand").is_dir() and (_c / "inbound").is_dir()),
None,
)
if _RPA_ROOT is not None:
for _rel in ("", "common", "connectors", "connectors/dsv", "connectors/crane", "inventory", "receipt"):
for _rel in ("", "common", "connectors", "connectors/dsv", "connectors/crane", "onhand", "inbound"):
_dir = str(_RPA_ROOT) if not _rel else str(_RPA_ROOT / _rel)
if _dir not in _sys.path:
_sys.path.insert(0, _dir)