feat(schedule): 新增平台任务参数契约与动态选项支持

This commit is contained in:
hugo committed 2026-07-09 13:19:36 +08:00
1 parent 2ce1aca023
commit 8b5b0b49bf
76 files changed
+7103 -1203

No files matched your search

@@ -41,7 +41,7 @@ def create_shipping_batch_from_notice_items(
from app.core.errors import ValidationError
raise ValidationError(
"all uploaded files failed extraction or contained no parseable text",
"未能从发货通知附件中抽取到可解析文本,请上传清晰版 PDF/文本后重试",
details={"file_summary": file_summary or {}},
)
@@ -660,7 +660,8 @@ def rpa_report_inventory_batch(payload: dict[str, Any]) -> dict[str, Any]:
RPA 批量库存导出回调:为每个 Warehouse 创建库存快照,可选关联发货任务。
【AI 触发时机】:freight_rpa_worker 完成 inventory_export_batch 后 POST 本接口。
核心逻辑:遍历 results;定时任务只入库;带 task_no 的成功项关联对应任务。
核心逻辑:遍历 results;定时任务只入库;带 task_no 的成功项关联对应任务;
含 schedule_run_id 时委托 finalize 终态化排程 run。
"""
from app.services.shipping_notice.inventory_mapping_workflow import inventory_loaded_response
@@ -748,13 +749,32 @@ def rpa_report_inventory_batch(payload: dict[str, Any]) -> dict[str, Any]:
})
db.session.commit()
finalize_result: dict[str, Any] = {"ok": False, "schedule_finalize_status": "skipped"}
if schedule_run_id is not None:
from app.services.schedule.shipping_inventory_rpa_finalize import finalize_shipping_inventory_schedule_run
finalize_result = finalize_shipping_inventory_schedule_run(
schedule_run_id,
created=created,
failed=failed,
payload=payload,
)
response_data = {
"run_no": run_no,
"created": created,
"failed": failed,
"rpa_mode": payload.get("rpa_mode"),
"schedule_finalize_status": finalize_result.get("schedule_finalize_status"),
}
if finalize_result.get("error"):
response_data["schedule_finalize_error"] = finalize_result.get("error")
if finalize_result.get("manual_retryable"):
response_data["schedule_finalize_manual_retryable"] = True
return tool_response(
status="ok" if created else "failed",
message=f"批量库存回调完成:成功 {len(created)},失败 {len(failed)}",
data={
"run_no": run_no,
"created": created,
"failed": failed,
"rpa_mode": payload.get("rpa_mode"),
},
data=response_data,
)
@@ -1,9 +1,9 @@
"""
services/shipping_notice/inventory_sources.py
用途:从 JSON 配置加载发货通知库存 RPA 源,并构造写入 trigger 的标准 rpa_inventory_source。
依赖:Flask current_app、pathlib/json;inventory_source_rules / inventory_rpa_workflow 调用本模块。
逻辑:新增 Warehouse 只改 backend/config/shipping_inventory_sources.json,不改 Python。
用途:从 YAML/JSON 配置加载发货通知库存 RPA 源,并构造写入 trigger 的标准 rpa_inventory_source。
依赖:Flask current_app、pathlib、PyYAML/json;inventory_source_rules / inventory_rpa_workflow 调用本模块。
逻辑:新增 Warehouse 只改 backend/config/shipping_inventory_sources.yaml,不改 Python。
"""
from __future__ import annotations
@@ -13,6 +13,7 @@ from copy import deepcopy
from pathlib import Path
from typing import Any
import yaml
from flask import current_app
_SITE_TO_SOURCE_TYPE = {
@@ -24,7 +25,7 @@ _CONFIG_CACHE: dict[str, Any] | None = None
class InventorySourceConfigError(ValueError):
"""库存源 JSON 配置校验失败。"""
"""库存源配置校验失败。"""
def _config_path() -> Path:
@@ -39,10 +40,34 @@ def _config_path() -> Path:
return path
from app.path_utils import resolve_backend_relative_path
default = Path(__file__).resolve().parents[3] / "config" / "shipping_inventory_sources.json"
default = Path(__file__).resolve().parents[3] / "config" / "shipping_inventory_sources.yaml"
return Path(resolve_backend_relative_path(None, default))
def _parse_config_payload(path: Path) -> dict[str, Any]:
"""
按文件后缀解析库存源配置(默认 YAML;测试夹具仍可用 .json)。
Raises:
InventorySourceConfigError: 后缀不支持或解析失败
"""
text = path.read_text(encoding="utf-8")
suffix = path.suffix.lower()
try:
if suffix in {".yaml", ".yml"}:
payload = yaml.safe_load(text)
elif suffix == ".json":
payload = json.loads(text)
else:
raise InventorySourceConfigError(
f"unsupported inventory sources config format: {path.suffix} (use .yaml, .yml, or .json)"
)
except (yaml.YAMLError, json.JSONDecodeError) as exc:
raise InventorySourceConfigError(f"failed to parse inventory sources config: {path}: {exc}") from exc
if not isinstance(payload, dict):
raise InventorySourceConfigError("inventory sources config root must be a mapping")
return payload
def _join_parameter_values(parameter: dict[str, Any]) -> str | None:
"""将 multi_text 参数值按 joiner 拼接为单字符串。"""
values = [str(item).strip() for item in (parameter.get("values") or []) if str(item).strip()]
@@ -160,7 +185,7 @@ def _validate_source(raw: dict[str, Any], *, seen_codes: set[str]) -> dict[str,
def _load_config(*, force_reload: bool = False) -> dict[str, Any]:
"""加载并缓存 JSON 配置;force_reload 用于测试。"""
"""加载并缓存 YAML/JSON 配置;force_reload 用于测试。"""
global _CONFIG_CACHE
if _CONFIG_CACHE is not None and not force_reload:
return _CONFIG_CACHE
@@ -169,7 +194,7 @@ def _load_config(*, force_reload: bool = False) -> dict[str, Any]:
if not path.is_file():
raise InventorySourceConfigError(f"inventory sources config not found: {path}")
payload = json.loads(path.read_text(encoding="utf-8"))
payload = _parse_config_payload(path)
sources_raw = payload.get("sources")
if not isinstance(sources_raw, list):
raise InventorySourceConfigError("sources must be a list")