feat(shipping-notice): 新增库存确认来源接口及相关逻辑
This commit is contained in:
1 parent
a31735b689
commit
0a9bd6415d
69 files changed
+8442
-775
No files matched your search
@@ -102,7 +102,7 @@ SN-20260529-A1B2C3D4
|
||||
| 顺序 | 资料 | 格式 | 用途 |
|
||||
|------|------|------|------|
|
||||
| 1 | **发货通知 / 订单** | PDF、Word,或邮件/微信正文 | 解析客户、SKU、数量、Reservation ID、单价、金额等 |
|
||||
| 2 | **库存快照** | Excel(.xlsx / .xls / .csv) | 与订单行核对可用库存;大表请直接上传文件,不要整表粘贴到聊天框 |
|
||||
| 2 | **库存快照** | Excel(.xlsx / .csv) | 与订单行核对可用库存;大表请直接上传文件,不要整表粘贴到聊天框 |
|
||||
| 3 | (可选)**货代发货模板** | 由 IT 配置 | 生成最终上传货代用的 Excel |
|
||||
|
||||
**顺序要求**:须先完成发货通知解析并确认,再上传库存。若尚未有任务编号就上传库存 Excel,系统会提示您先提交发货通知。
|
||||
|
||||
@@ -1,36 +1,65 @@
|
||||
# 发货通知库存源路由与 RPA Driver 方案
|
||||
# 发货通知库存源路由方案(订单解析阶段判定)
|
||||
|
||||
## 背景
|
||||
|
||||
发货通知的库存导出不能再假设只有一个 RPA 地址或一种页面流程。不同目的地、出货港口或仓库线索可能对应不同货代系统入口,例如 DSV 360 的不同 Program,或 Crane FootPrint Portal 的不同 Warehouse。若选错库存源,后续库存匹配和出货 Excel 会建立在错误库存池上,风险高于“未查到库存”。
|
||||
发货通知的库存导出不能再假设只有一个 RPA 地址或一种页面流程。不同目的地、出货港口或仓库线索可能对应不同货代系统入口,例如 DSV 360 的不同 Program,或 Crane FootPrint Portal 的不同 Warehouse。若选错库存源,后续库存匹配和出货 Excel 会建立在错误库存池上,风险高于「未查到库存」。
|
||||
|
||||
历史上 `site_id`(如 `AWSBI`)与 `warehouse_id`(如 `BYAWSATL01`)曾被混用,导致发货 Excel 与 RPA 查询参数不一致。本方案将库存源判定前移到**订单解析阶段**,并以独立数据库字段持久化业务决策。
|
||||
|
||||
## 设计原则
|
||||
|
||||
- Dify LLM 负责从非结构化发货通知和用户对话中抽取候选字段,不负责最终 RPA URL 决策。
|
||||
- HXOPS 负责标准化、规则匹配、唯一性判断、权限校验、人工确认和审计。
|
||||
- Dify LLM 负责从非结构化发货通知和用户对话中抽取候选字段(目的地、港口、仓库线索等),**不负责**最终 RPA URL / source_code 决策。
|
||||
- HXOPS 在 `parse_notice` 阶段根据 YAML 配置做标准化、规则匹配、唯一性判断;唯一命中写入任务独立字段。
|
||||
- 用户人工确认时**只确认 `source_code`**;`site_id`、`warehouse_id` 由配置自动生成,禁止前端或 LLM 直写。
|
||||
- RPA、库存准备、匹配、Excel 生成、上传等后续步骤均通过 **inventory source gate** 硬阻断,未稳定(`resolved` / `user_confirmed`)不得继续。
|
||||
- RPA 只消费 HXOPS 给出的 `rpa_inventory_source` 参数,按 `source_type` 分发到不同 Driver。
|
||||
- 通用化范围是 trigger 协议、状态机、超时重试、回调、库存标准化;页面操作逻辑由不同 Driver 独立实现。
|
||||
- 用户对话可以做当前任务级临时覆盖;全局规则变更只能作为建议,需审核后生效。
|
||||
|
||||
## MVP 范围
|
||||
## 判定时机与状态机
|
||||
|
||||
第一版不改数据库结构,规则和源配置先放代码中,任务级上下文写入 `ShippingTask.step_payload_json`。
|
||||
### 何时判定
|
||||
|
||||
MVP 包含:
|
||||
1. **`parse_notice`**:解析结构化订单后,立即调用 `adjudicate_inventory_source()`。
|
||||
2. 唯一命中且配置完整 → `inventory_source_status=resolved`,任务可进入 `parsed`。
|
||||
3. 未命中 / 多命中 / 配置无效 / 路由指纹变更 → `pending_review`,`current_step=step2_parse_confirm`,`checkpoint=inventory_source_review`,`pending_human_reason=inventory_source_unresolved`。
|
||||
4. 用户在聊天区确认 `source_code` → `confirm_inventory_source` → `inventory_source_status=user_confirmed`,再推进至 `parsed` 并允许 RPA。
|
||||
|
||||
- 解析 Dify 输出的目的地候选字段,并保存到任务 step payload。
|
||||
- 根据目的地/港口/仓库线索解析库存源。
|
||||
- 唯一命中时自动写入 `rpa_inventory_source`。
|
||||
- 无法命中或多命中时进入 `inventory_source_review` 人工检查点。
|
||||
- RPA trigger JSON 增加 `rpa_inventory_source`。
|
||||
- 后端测试覆盖 DSV 与 Crane 的基本路由。
|
||||
### 稳定状态
|
||||
|
||||
暂不包含:
|
||||
| status | 含义 |
|
||||
| --- | --- |
|
||||
| `resolved` | 规则自动唯一命中 |
|
||||
| `user_confirmed` | 用户已确认 source_code |
|
||||
|
||||
- 后台规则管理页面。
|
||||
- 数据库规则表。
|
||||
- 批次级对话覆盖。
|
||||
- 全局规则建议审核流。
|
||||
其余状态(`unresolved`、`ambiguous`、`invalid`、`conflicted`)均视为不稳定,gate 阻断。
|
||||
|
||||
## 独立数据库字段(`shipping_tasks`)
|
||||
|
||||
| 字段 | 说明 |
|
||||
| --- | --- |
|
||||
| `destination_*` | 目的地候选(城市、国家、港口等),供规则匹配 |
|
||||
| `inventory_source_code` | 正式库存源编码,如 `DSV360_AWSDU` |
|
||||
| `inventory_source_type` | `dsv360` / `crane_footprint` |
|
||||
| `inventory_site_id` | DSV Program / Site(如 `AWSDU`) |
|
||||
| `inventory_warehouse_id` | 具体仓库(如需要) |
|
||||
| `inventory_source_status` | 判定状态 |
|
||||
| `inventory_source_method` | `auto` / `user` |
|
||||
| `inventory_source_config_version` | YAML 配置版本指纹 |
|
||||
| `inventory_routing_fingerprint` | 路由输入指纹,变更时要求重确认 |
|
||||
|
||||
`step_payload_json.inventory_source_resolution` 仅保留候选列表与诊断信息,**不作为** gate 真相源。
|
||||
|
||||
## 配置文件
|
||||
|
||||
路径:`backend/config/shipping_inventory_sources.yaml`(环境变量 `SHIPPING_NOTICE_INVENTORY_SOURCES_CONFIG`)
|
||||
|
||||
每个 source 含:
|
||||
|
||||
- `source_code`、`source_type`、`site_id`、`warehouse_id`
|
||||
- `navigation`、`parameters`(RPA 填表)
|
||||
- `match_aliases`:目的地/港口/仓库别名
|
||||
- `match`:加权评分规则(可选)
|
||||
|
||||
新增 Warehouse **只改 YAML**,不改 Python。
|
||||
|
||||
## Trigger 协议
|
||||
|
||||
@@ -69,19 +98,63 @@ Crane 示例:
|
||||
}
|
||||
```
|
||||
|
||||
## 初始路由规则
|
||||
## Gate 规则(硬阻断)
|
||||
|
||||
- `BISON`, `AWSBI` -> `DSV360_AWSBI`
|
||||
- `CINCINNATI`, `CVG`, `AWSCV` -> `DSV360_AWSCV`
|
||||
- `DUB`, `DUBLIN` -> `DSV360_AWSDU`
|
||||
- `MOERDIJK`, `AMS`, `AWSAMS` -> `DSV360_AWSAMS`
|
||||
- `USLCK`, `SALT LAKE CITY` -> `DSV360_AWSSL`
|
||||
- `SINGAPORE`, `SG` -> `DSV360_AWSSG`
|
||||
- `TZR`, `THAILAND` -> `CRANE_TZR`
|
||||
以下步骤执行前调用 `assert_inventory_source_gate(task)`:
|
||||
|
||||
- RPA 触发 / 重试
|
||||
- 库存快照 link / 映射 / 匹配
|
||||
- 发货 Excel 生成
|
||||
- 货代上传
|
||||
|
||||
库存快照 link 时额外做 **identity check**:`source_code`、`site_id`、`warehouse_id` 与任务字段一致,否则 `SHIPPING.INVENTORY_IDENTITY_MISMATCH`。
|
||||
|
||||
## 错误码(API)
|
||||
|
||||
| code | 场景 |
|
||||
| --- | --- |
|
||||
| `SHIPPING.INVENTORY_SOURCE_UNRESOLVED` | 无法判定 |
|
||||
| `SHIPPING.INVENTORY_SOURCE_AMBIGUOUS` | 多候选 |
|
||||
| `SHIPPING.INVENTORY_SOURCE_INVALID` | 配置无效 |
|
||||
| `SHIPPING.INVENTORY_SOURCE_CONFLICTED` | 判定冲突 |
|
||||
| `SHIPPING.INVENTORY_SOURCE_CONFIRMATION_REQUIRED` | 需人工确认 |
|
||||
| `SHIPPING.INVENTORY_SOURCE_CHANGED` | 路由变更需重确认 |
|
||||
| `SHIPPING.INVENTORY_IDENTITY_MISMATCH` | 快照与任务不一致 |
|
||||
| `SHIPPING.INVENTORY_SOURCE_GATE_BLOCKED` | 通用 gate 阻断 |
|
||||
|
||||
前端 i18n:`errors.codes.SHIPPING_INVENTORY_SOURCE_*`(点号转下划线)。
|
||||
|
||||
## 聊天区交互
|
||||
|
||||
- 检查点 `inventory_source_review` 展示候选按钮(携带 `source_code`)。
|
||||
- 用户可直接输入目的地线索(如 `Dublin`、`AWSDU`),前端匹配唯一候选后调用 `confirm_inventory_source`。
|
||||
- 动作:`confirm_inventory_source`、`retry_resolve_inventory_source`。
|
||||
|
||||
## 初始路由规则(示例)
|
||||
|
||||
- `BISON`, `AWSBI` → `DSV360_AWSBI`
|
||||
- `CINCINNATI`, `CVG`, `AWSCV` → `DSV360_AWSCV`
|
||||
- `DUB`, `DUBLIN` → `DSV360_AWSDU`
|
||||
- `MOERDIJK`, `AMS`, `AWSAMS` → `DSV360_AWSAMS`
|
||||
- `USLCK`, `SALT LAKE CITY` → `DSV360_AWSSL`
|
||||
- `SINGAPORE`, `SG` → `DSV360_AWSSG`
|
||||
- `TZR`, `THAILAND` → `CRANE_TZR`
|
||||
|
||||
完整列表以 YAML 为准。
|
||||
|
||||
## 相关代码
|
||||
|
||||
| 模块 | 路径 |
|
||||
| --- | --- |
|
||||
| 规则与指纹 | `services/shipping_notice/inventory_source_rules.py` |
|
||||
| Gate | `services/shipping_notice/inventory_source_gate.py` |
|
||||
| 解析集成 | `services/shipping_notice/task_workflow.py` |
|
||||
| 确认 Tool | `services/shipping_notice/inventory_rpa_workflow.py` |
|
||||
| 配置加载 | `services/shipping_notice/inventory_sources.py` |
|
||||
| 迁移 | `migrations/versions/b4c5d6e7f8a9_add_shipping_inventory_source_gate_fields.py` |
|
||||
|
||||
## 后续演进
|
||||
|
||||
- 将代码配置迁移到可维护规则表。
|
||||
- 增加管理员审核的规则变更建议。
|
||||
- RPA 侧实现 `dsv360` 与 `crane_footprint` Driver。
|
||||
- 导入库存时校验 source_code、warehouse/program 与任务期望是否一致。
|
||||
- 后台规则管理页面与审核流。
|
||||
- 批次级对话覆盖(当前仅任务级)。
|
||||
- 全局规则变更建议与审计。
|
||||
@@ -1440,7 +1440,7 @@ def main(normalized_query: str = "", intent_hint: str = "", sys_files=None):
|
||||
text = str(normalized_query or "").strip()
|
||||
if files:
|
||||
lower_names = " ".join(_file_name(f) for f in files)
|
||||
if any(ext in lower_names for ext in [".xlsx", ".xls", ".csv"]):
|
||||
if any(ext in lower_names for ext in [".xlsx", ".csv"]):
|
||||
return {"route_key": "inventory_path", "reason": "inventory_file"}
|
||||
return {"route_key": "direct_answer", "reason": "notice_files_managed_by_hxops"}
|
||||
if intent_hint in {
|
||||
@@ -2935,7 +2935,7 @@ Agent 状态输入标准化 -> 货代续办 Agent
|
||||
| 确认 / 驳回 / 重匹配直连 | `确认` / `驳回` / `重新匹配` / `rematch` | `submit_decision` | 是,直连决策 Tool | 当前任务或显式 `SN-...` | 按状态机推进;`rematch` 清理 shortage 并重新入队当前任务 | 通过:`PYTHONPATH=. pytest tests/test_shipping_notice_tools.py tests/test_shipping_notice_batches.py -q`;`test_contract_submit_decision_rematch_is_direct_recovery_entry` |
|
||||
| shortage 阻断 | shortage review 后输入 `继续` 或自动续跑 | `match_inventory` / `submit_decision` 返回门禁 | 是,命中状态机门禁后停止 | 当前任务 | `status=needs_human`;禁止 `generate_shipment_excel` / `upload_to_freight`;不生成 Excel | 通过:`PYTHONPATH=. pytest tests/test_shipping_notice_tools.py tests/test_shipping_notice_batches.py -q`;`test_shipping_notice_match_shortage_needs_human` |
|
||||
| 多任务只推进 focus task | 多票批次中输入 `继续处理` | `get_batch_status` + 当前 focus 的 `get_task_status` | 是,先读状态再决定 | `batch.current_focus_task_no` | 每轮只围绕一个 focus task;读批次状态不循环推进整批 | 通过:`PYTHONPATH=. pytest tests/test_shipping_notice_tools.py tests/test_shipping_notice_batches.py -q`;`test_contract_batch_status_keeps_single_focus_without_advancing_other_tasks` |
|
||||
| 库存上传链路 | 上传 `.xlsx/.xls/.csv` | `get_inventory_schema_preview` → 映射 LLM → `apply_inventory_mapping` | 否,进入库存专用分支,不走自由 Agent | 当前任务 / 所属批次库存快照 | 生成 schema preview;应用映射后进入 `align_review` 或后端续跑 | 待实测 |
|
||||
| 库存上传链路 | 上传 `.xlsx/.csv` | `get_inventory_schema_preview` → 映射 LLM → `apply_inventory_mapping` | 否,进入库存专用分支,不走自由 Agent | 当前任务 / 所属批次库存快照 | 生成 schema preview;应用映射后进入 `align_review` 或后端续跑 | 待实测 |
|
||||
| 字段对齐人工门 | `apply_inventory_mapping` 返回 `align_review` 后输入 `确认` | `submit_decision(decision=confirm_inventory_mapping)` | 是,直连决策 Tool | 当前任务 | 停住等待确认;确认后交由 hxops worker 续跑匹配 / 导出 / 上传 | 待实测 |
|
||||
| Tool 人工门收口 | 任一 Tool 返回 `needs_human` 或 `stop_agent_turn=true` | 工具结果标准化 | 是,硬停止本轮 | 当前任务 | 本轮不再继续调用后续 Tool | 待实测 |
|
||||
| forbidden_next_tools 门禁 | Tool 返回 `forbidden_next_tools` | 工具结果标准化 | 是,硬门禁 | 当前任务 | 画布不调用被禁止 Tool | 待实测 |
|
||||
@@ -3291,7 +3291,7 @@ API 层
|
||||
|
||||
### 5.2 快照复用规则
|
||||
|
||||
1. `ensure_inventory(mode=auto)` 在解析库存源后,优先 `find_latest_ready_snapshot(source_code, warehouse_id)`
|
||||
1. `ensure_inventory(mode=auto)` 在库存源**已稳定**后,优先 `find_latest_ready_snapshot(source_code, site_id, warehouse_id)`
|
||||
2. 未过期快照 → `link_snapshot_to_task(reason=reuse_latest)` → 映射/匹配
|
||||
3. 过期或缺失 → 触发 RPA(单 task trigger 或定时 batch trigger)
|
||||
4. `retry_rpa` **强制跳过**快照缓存
|
||||
@@ -3312,6 +3312,18 @@ API 层
|
||||
|
||||
### 5.4 审计字段
|
||||
|
||||
- `shipping_inventory_files`:`warehouse_id`、`source_code`、`exported_at`、`expires_at`、`rpa_run_no`、`schedule_run_id`
|
||||
- `shipping_task_inventory_links`:`link_reason`、`matched_warehouse_id`、`snapshot_age_seconds`、`audit_payload_json`
|
||||
- `shipping_inventory_files`:`warehouse_id`、`site_id`、`source_code`、`exported_at`、`expires_at`、`rpa_run_no`、`schedule_run_id`、`identity_status`
|
||||
- `shipping_task_inventory_links`:`link_reason`、`matched_source_code`、`matched_site_id`、`matched_warehouse_id`、`snapshot_age_seconds`、`audit_payload_json`、身份校验字段
|
||||
|
||||
### 5.5 订单解析阶段库存源判定(2026-07)
|
||||
|
||||
> 详细方案见 [`inventory_source_plan.md`](./inventory_source_plan.md)。
|
||||
|
||||
1. **`parse_notice` 内 adjudicate**:读取任务 `destination_*` 与 YAML `match`/`match_aliases`,产出 `InventorySourceAdjudication`。
|
||||
2. **稳定才放行**:`inventory_source_status ∈ {resolved, user_confirmed}` 时任务才可 `parsed` 并触发 RPA;否则 `checkpoint=inventory_source_review`,`process_status=pending_review`。
|
||||
3. **独立字段为真相源**:`inventory_source_code`、`inventory_site_id`、`inventory_warehouse_id` 等存于 `shipping_tasks` 列,gate 不读 `step_payload_json`。
|
||||
4. **用户只确认 source_code**:`confirm_inventory_source` 根据编码回填 site/warehouse;路由指纹变更时要求重确认(`SHIPPING.INVENTORY_SOURCE_CHANGED`)。
|
||||
5. **硬 Gate**:`inventory_rpa_workflow`、`inventory_workflow`、`fulfillment_workflow`、`progress_workflow` 入口均调用 `assert_inventory_source_gate`。
|
||||
6. **快照复用键**:`find_latest_ready_snapshot(source_code, site_id, warehouse_id)`;link 时做 identity check。
|
||||
7. **迁移**:`b4c5d6e7f8a9_add_shipping_inventory_source_gate_fields`。
|
||||
|
||||
Reference in new issue
Block a user