把 shipping 侧 inventory/receipt 词根迁到 onhand/inbound,29 张表与 枚举/权限/前端/i18n 同步改名;在手与入站报表配置及文件改由 freight_rpa 持有,发货与调拨仅按 source_code 消费;ORM 类加 FreightRpa 前缀, 上传动作改为 freight_rpa_upload。含类名与上传动作改名规格文档。 Co-authored-by: Cursor <cursoragent@cursor.com>
220 lines
10 KiB
Python
220 lines
10 KiB
Python
"""
|
||
backend/app/api/freight_rpa.py
|
||
|
||
用途:货代 RPA 回调蓝图(/api/freight-rpa/*)——Windows Worker 向后端上报心跳、
|
||
库存(onhand)/收料(inbound)导出结果与货代上传结果的唯一回调入口。
|
||
批次 R 解耦后取代旧 shipping-notice rpa 回调前缀(无双路由、无兼容层)。
|
||
依赖:app.core.tool_auth(集成密钥鉴权)、freight_rpa.common.callback_sinks(sink 分发)、
|
||
freight_rpa.common.worker_heartbeat(心跳)、shipping_tools、
|
||
shipping_notice.rpa_sinks(api 层为组合根,允许同时 import 双侧)。
|
||
逻辑:before_request 统一 Bearer / X-Integration-Key 鉴权(与 Tool 蓝图同一把 key)。
|
||
分发策略按「处理函数当前住在哪个包」决定:
|
||
- /heartbeat 直连 freight_rpa.common.worker_heartbeat;
|
||
- /upload-complete 直连现有业务处理器(shipping_notice.tools);
|
||
- onhand-* 与 inbound-* 全部经 callback_sinks.dispatch_sink 分发,真实业务 sink 由
|
||
组合根(create_app → shipping_notice.rpa_sinks.register_onhand_sinks /
|
||
register_inbound_sinks)注册;未注册的 kind 显式 404(FREIGHT_RPA.SINK_NOT_REGISTERED)。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from flask import Blueprint, jsonify, request
|
||
|
||
from app.core.errors import ValidationError
|
||
from app.core.tool_auth import require_tool_auth
|
||
from app.services.freight_rpa.common.callback_sinks import dispatch_sink
|
||
from app.services.freight_rpa.common.worker_heartbeat import record_rpa_worker_heartbeat
|
||
|
||
freight_rpa_bp = Blueprint('freight_rpa', __name__)
|
||
|
||
|
||
def _payload() -> dict:
|
||
"""
|
||
读取回调 JSON 请求体,并剥离不可信的操作人身份字段。
|
||
|
||
核心逻辑:与 shipping_notice Tool 蓝图同口径——集成密钥调用方不得通过 body
|
||
伪造 `_trusted_user_id` / `operator_id` / `actor_id`。
|
||
"""
|
||
raw = request.get_json(silent=True) or {}
|
||
if not isinstance(raw, dict):
|
||
return {}
|
||
payload = dict(raw)
|
||
for key in ('_trusted_user_id', 'operator_id', 'actor_id'):
|
||
payload.pop(key, None)
|
||
return payload
|
||
|
||
|
||
@freight_rpa_bp.before_request
|
||
def _verify_worker_access():
|
||
"""RPA Worker 回调统一 Header 鉴权;与 Tool API 共用同一集成密钥配置。"""
|
||
require_tool_auth()
|
||
return None
|
||
|
||
|
||
@freight_rpa_bp.route('/heartbeat', methods=['POST'])
|
||
def heartbeat():
|
||
"""
|
||
RPA Worker 心跳上报。
|
||
【AI 触发时机】:非 Agent 入口;Windows Worker(库存/收料)由独立守护线程周期调用。
|
||
|
||
核心逻辑:校验 payload 后写入 Redis/内存;后端在写各类 trigger 前以此做在线就绪检查。
|
||
"""
|
||
payload = _payload()
|
||
try:
|
||
worker = record_rpa_worker_heartbeat(payload)
|
||
except ValueError as exc:
|
||
raise ValidationError(str(exc))
|
||
# 心跳存储不可用(ServiceUnavailableError)原样上抛,由全局 AppError handler 出标准错误 envelope。
|
||
return jsonify({'status': 'ok', 'message': 'heartbeat recorded', 'data': worker})
|
||
|
||
|
||
@freight_rpa_bp.route('/onhand-ready', methods=['POST'])
|
||
def onhand_ready():
|
||
"""
|
||
onhand 单任务导出回调(旧 inventory-ready 端点随批次 R 迁入本蓝图)。
|
||
【AI 触发时机】:非 Agent 入口;库存 Worker 完成单任务导出后调用。
|
||
|
||
核心逻辑:dispatch_sink('onhand-ready') 分发到组合根注册的业务 sink
|
||
(onhand_rpa_workflow.rpa_report_inventory:快照落盘 + 任务状态回写),
|
||
envelope = 裸业务 tool_response dict(与旧 /rpa/inventory-ready 逐字一致)。
|
||
"""
|
||
return jsonify(dispatch_sink('onhand-ready', **_payload()))
|
||
|
||
|
||
@freight_rpa_bp.route('/onhand-batch-ready', methods=['POST'])
|
||
def onhand_batch_ready():
|
||
"""
|
||
onhand 批量导出终态回调(旧 /rpa/inventory-batch-ready)。
|
||
【AI 触发时机】:非 Agent 入口;库存 Worker 按排程批次完成全部站点导出后调用。
|
||
|
||
核心逻辑:dispatch_sink('onhand-batch-ready') 分发;按 Warehouse 写入库存快照并
|
||
finalize 排程 run(rpa_report_inventory_batch),envelope = 裸业务 dict。
|
||
"""
|
||
return jsonify(dispatch_sink('onhand-batch-ready', **_payload()))
|
||
|
||
|
||
@freight_rpa_bp.route('/onhand-batch-claimed', methods=['POST'])
|
||
def onhand_batch_claimed():
|
||
"""
|
||
onhand 批量领取回调(旧 /rpa/inventory-batch-claimed)。
|
||
【AI 触发时机】:非 Agent 入口;v2 Worker 领取批次 trigger 后立即调用。
|
||
|
||
核心逻辑:dispatch_sink('onhand-batch-claimed') 分发;仅记录 Attempt 执行阶段
|
||
(per_source_rpa_service.claim_inventory_batch),envelope = {'success':True,'data':...}。
|
||
"""
|
||
return jsonify(dispatch_sink('onhand-batch-claimed', **_payload()))
|
||
|
||
|
||
@freight_rpa_bp.route('/onhand-batch-progress', methods=['POST'])
|
||
def onhand_batch_progress():
|
||
"""
|
||
onhand 批量导出进度回调(旧 /rpa/inventory-batch-progress)。
|
||
【AI 触发时机】:非 Agent 入口;Worker 每完成一个站点导出后调用(非终态,以 batch-ready 为准)。
|
||
|
||
核心逻辑:dispatch_sink('onhand-batch-progress') 分发;实时回写排程 run 计数
|
||
(onhand_rpa_workflow.report_inventory_batch_progress),envelope = 裸业务 dict。
|
||
"""
|
||
return jsonify(dispatch_sink('onhand-batch-progress', **_payload()))
|
||
|
||
|
||
@freight_rpa_bp.route('/onhand-configuration-test-start', methods=['POST'])
|
||
def onhand_configuration_test_start():
|
||
"""
|
||
onhand 配置测试开始回调(旧 /rpa/inventory-configuration-test-batch-progress)。
|
||
【AI 触发时机】:非 Agent 入口;Worker 领取批量验证中的单个站点后立即调用。
|
||
|
||
核心逻辑:dispatch_sink('onhand-configuration-test-start') 分发;把该站点测试运行写为
|
||
processing / rpa_exporting,envelope = {'status':'ok','data':...}(旧 batch-progress 同款)。
|
||
"""
|
||
return jsonify(dispatch_sink('onhand-configuration-test-start', **_payload()))
|
||
|
||
|
||
@freight_rpa_bp.route('/onhand-configuration-test-analyze', methods=['POST'])
|
||
def onhand_configuration_test_analyze():
|
||
"""
|
||
onhand 配置测试结构分析回调(旧 /rpa/inventory-configuration-test-ready)。
|
||
【AI 触发时机】:非 Agent 入口;Worker 完成单站点配置测试导出后调用。
|
||
|
||
核心逻辑:dispatch_sink('onhand-configuration-test-analyze') 分发;仅分析文件结构,
|
||
不创建生产库存快照或任务关联,envelope = {'status':'ok','data':...}(旧 ready 同款)。
|
||
"""
|
||
return jsonify(dispatch_sink('onhand-configuration-test-analyze', **_payload()))
|
||
|
||
|
||
@freight_rpa_bp.route('/onhand-configuration-test-complete', methods=['POST'])
|
||
def onhand_configuration_test_complete():
|
||
"""
|
||
onhand 配置测试批次终态回调(旧 /rpa/inventory-configuration-test-batch-ready)。
|
||
【AI 触发时机】:非 Agent 入口;Worker 完成批量配置测试后按 validation_run_no 回传 results。
|
||
|
||
核心逻辑:dispatch_sink('onhand-configuration-test-complete') 分发;独立回写每个库存站点
|
||
的测试结果,envelope = {'status':'ok','data':...}(旧 batch-ready 同款)。
|
||
"""
|
||
return jsonify(dispatch_sink('onhand-configuration-test-complete', **_payload()))
|
||
|
||
|
||
@freight_rpa_bp.route('/onhand-repair', methods=['POST'])
|
||
def onhand_repair():
|
||
"""
|
||
onhand 受控库存回调修复(旧 /rpa/inventory-repair)。
|
||
【AI 触发时机】:非 Agent 入口;人工/运维补偿链路调用,复用正式 v2 ingest 流程。
|
||
|
||
核心逻辑:dispatch_sink('onhand-repair') 分发;repair 语义由 kind 承载——sink 等价于
|
||
旧实现 `payload['repair'] = True` 后走 batch-ready ingest,envelope = 裸业务 dict。
|
||
"""
|
||
return jsonify(dispatch_sink('onhand-repair', **_payload()))
|
||
|
||
|
||
@freight_rpa_bp.route('/inbound-batch-ready', methods=['POST'])
|
||
def inbound_batch_ready():
|
||
"""
|
||
inbound(收料)批量导出终态回调(旧 /rpa/receipt-batch-ready)。
|
||
【AI 触发时机】:非 Agent 入口;收料 Worker 完成报表导出并落 drop 目录后调用。
|
||
|
||
核心逻辑:dispatch_sink('inbound-batch-ready') 分发到组合根注册的业务 sink
|
||
(inbound_report_storage.ingest_receipt_callback:委托 freight_rpa.inbound.report_archive
|
||
原样归档,终态 commit 后由域内业务桥推进在途调拨并归集排程终态)——独立归档文件,
|
||
绝不进入库存回调或库存匹配链路;envelope = {'success': True, 'data': ...}(旧端点逐字一致)。
|
||
"""
|
||
return jsonify(dispatch_sink('inbound-batch-ready', **_payload()))
|
||
|
||
|
||
@freight_rpa_bp.route('/inbound-unit-progress', methods=['POST'])
|
||
def inbound_unit_progress():
|
||
"""
|
||
inbound 单元领取/导出中回调(旧 /rpa/receipt-unit-progress)。
|
||
【AI 触发时机】:非 Agent 入口;收料 Worker 领取单元后调用(awaiting_rpa → processing)。
|
||
|
||
核心逻辑:dispatch_sink('inbound-unit-progress') 分发到 mark_receipt_unit_processing
|
||
(Task 11 已随排程壳平移改名,现住 schedule.freight_rpa_inbound_finalize);本路由同时是收料
|
||
超时对账的活动锚数据源(mark 落单元 started_at)。曾因重构分叉合并丢失(2026-10-08),
|
||
Worker 上报全程 404,勿再从任何路由文件删除。
|
||
"""
|
||
return jsonify(dispatch_sink('inbound-unit-progress', **_payload()))
|
||
|
||
|
||
@freight_rpa_bp.route('/inbound-batch-progress', methods=['POST'])
|
||
def inbound_batch_progress():
|
||
"""
|
||
inbound 批量导出累计进度回调(旧 /rpa/receipt-batch-progress)。
|
||
【AI 触发时机】:非 Agent 入口;收料 Worker 每完成一笔导出后调用(非终态)。
|
||
|
||
核心逻辑:dispatch_sink('inbound-batch-progress') 分发到 report_receipt_batch_progress
|
||
(Task 11 已随排程壳平移改名,现住 schedule.freight_rpa_inbound_finalize),回写排程 run 计数
|
||
(对齐 onhand-batch-progress 语义)。
|
||
"""
|
||
return jsonify(dispatch_sink('inbound-batch-progress', **_payload()))
|
||
|
||
|
||
@freight_rpa_bp.route('/upload-complete', methods=['POST'])
|
||
def upload_complete():
|
||
"""
|
||
货代上传结果回调(旧 /rpa/upload-complete,URL 词根不变)。
|
||
【AI 触发时机】:非 Agent 入口;Worker 完成出货 Excel 上传(成功或失败)后调用。
|
||
|
||
核心逻辑:直连 shipping_notice.tools.rpa_report_upload 回写任务状态。
|
||
"""
|
||
from app.services.shipping_notice import tools as shipping_tools
|
||
|
||
return jsonify(shipping_tools.rpa_report_upload(_payload()))
|