933 lines
35 KiB
Markdown
933 lines
35 KiB
Markdown
# 货代发货通知 — 完整手册
|
||
|
||
> **唯一技术文档**:货代发货通知相关的部署、Dify 配置、OpenAPI、Agent 指令与运维说明 **仅维护本文件**(`docs/shipping_notice.md`),请勿另建分散 md。
|
||
> **业务操作说明**:[`shipping_notice_business.md`](./shipping_notice_business.md)(面向货代/跟单等业务人员,无程序与 Dify 内容)。
|
||
> **同步维护**:流程、字段规则、人工确认节点等变更时,**须同时更新本文件与 `shipping_notice_business.md`**。
|
||
|
||
Hxops **货代发货通知 Agent** 的部署、Dify 配置、OpenAPI、大库存 HTTP 直传与 Agent 指令合集。
|
||
|
||
**Tool API 基路径**:`/api/shipping-notice`
|
||
**OpenAPI**:`GET /api/shipping-notice/tools/openapi.json`
|
||
**鉴权 Header**:`X-Shipping-Tool-Key`
|
||
|
||
---
|
||
|
||
## 目录
|
||
|
||
0. [业务操作说明(另册)](#业务操作说明另册)
|
||
1. [部署 Runbook](#1-部署-runbook)
|
||
2. [Dify OpenAPI 配置](#2-dify-openapi-配置)
|
||
3. [OpenAPI 变更说明](#3-openapi-变更说明)
|
||
4. [大库存 Excel — HTTP 直传](#4-大库存-excel--http-直传)
|
||
5. [Agent 系统指令(粘贴用)](#5-agent-系统指令粘贴用)
|
||
6. [Chatflow 画布参考](#6-chatflow-画布参考)
|
||
- [6.0 总览](#60-总览)
|
||
- [6.1 前置](#61-前置)
|
||
- [6.2 开始节点](#62-开始节点)
|
||
- [6.3 附件分类](#63-附件分类)
|
||
- [6.4 PDF 分支 → 文档提取器](#64-pdf-分支--文档提取器)
|
||
- [6.5 xlsx 分支 → HTTP 上传](#65-xlsx-分支--http-上传)
|
||
- [6.6 合并 Agent 输入(必做)](#66-合并-agent-输入必做)
|
||
- [6.7 Agent 节点](#67-agent-节点)
|
||
- [6.8 提取 case_id](#68-提取-case_id)
|
||
- [6.9 直接回复](#69-直接回复)
|
||
- [6.10 各轮对照 · 验收 · 速查](#610-各轮对照--验收--速查)
|
||
7. [相关代码与文件](#7-相关代码与文件)
|
||
|
||
---
|
||
|
||
## 业务操作说明(另册)
|
||
|
||
面向业务人员的流程、字段、确认节点与 FAQ 见 **[`shipping_notice_business.md`](./shipping_notice_business.md)**。
|
||
IT / 开发改流程时,请同步更新该文件,避免聊天助手行为与业务培训材料不一致。
|
||
|
||
---
|
||
|
||
## 1. 部署 Runbook
|
||
|
||
### 1.1 数据库迁移
|
||
|
||
```bash
|
||
cd backend
|
||
flask db upgrade
|
||
```
|
||
|
||
### 1.2 环境变量(`.env.dev` 示例)
|
||
|
||
```env
|
||
# Tool API(Dify / RPA 回调共用,可与 INTEGRATION_API_KEY 相同)
|
||
SHIPPING_NOTICE_TOOL_API_KEY=your-tool-key
|
||
SHIPPING_NOTICE_TOOL_API_BASE=http://192.168.36.114:5052/api/shipping-notice
|
||
|
||
# 文件存储(可选,默认 instance/shipping_notice)
|
||
SHIPPING_NOTICE_STORAGE_ROOT=backend/data/shipping_notice
|
||
|
||
# 库存文本上限(超过则必须 HTTP 上传)
|
||
SHIPPING_NOTICE_INVENTORY_TEXT_MAX_CHARS=80000
|
||
|
||
# RPA 目录(开发机本地路径,需 RPA worker 与 backend 一致)
|
||
FREIGHT_INVENTORY_DROP_DIR=backend/data/freight_rpa/inventory_drop
|
||
FREIGHT_RPA_INVENTORY_TRIGGER_DIR=backend/data/freight_rpa/inventory_triggers
|
||
FREIGHT_UPLOAD_QUEUE_DIR=backend/data/freight_rpa/upload_queue
|
||
FREIGHT_RPA_UPLOAD_TRIGGER_DIR=backend/data/freight_rpa/upload_triggers
|
||
|
||
# 发货 Excel 模板(可选)
|
||
FREIGHT_SHIPMENT_TEMPLATE_PATH=backend/templates/freight_shipment_upload.xlsx
|
||
|
||
# AWS 库存列映射(可选)
|
||
# FREIGHT_INVENTORY_COLUMN_MAP={"AWS Sku":"sku","COM ID":"reservation_id","Qty Available":"available_qty"}
|
||
|
||
# 字段对齐确认(生产建议 true)
|
||
# SHIPPING_NOTICE_INVENTORY_MAPPING_REVIEW=true
|
||
|
||
# Dify Chatflow / Workflow(seed 脚本用)
|
||
SHIPPING_NOTICE_CHAT_DIFY_APP_ID=
|
||
SHIPPING_NOTICE_CHAT_DIFY_API_KEY=
|
||
SHIPPING_NOTICE_CHAT_DIFY_BASE=http://192.168.100.192:8882/v1
|
||
SHIPPING_NOTICE_EXTRACT_DIFY_APP_ID=
|
||
SHIPPING_NOTICE_EXTRACT_DIFY_API_KEY=
|
||
```
|
||
|
||
生成模板(若尚未存在):
|
||
|
||
```bash
|
||
python backend/scripts/generate_freight_shipment_template.py
|
||
```
|
||
|
||
### 1.3 注册 Agent 记录
|
||
|
||
```bash
|
||
cd backend
|
||
python seed_shipping_notice_agents.py
|
||
```
|
||
|
||
在 Dify 创建同名应用后,把 `dify_app_id` / API Key 写入 `.env` 并重新 seed。
|
||
|
||
### 1.4 Mock RPA(开发)
|
||
|
||
```powershell
|
||
$env:HXOPS_API_BASE = "http://127.0.0.1:5052"
|
||
$env:SHIPPING_NOTICE_TOOL_API_KEY = "your-tool-key"
|
||
$env:FREIGHT_RPA_MOCK = "true"
|
||
python scripts/rpa/freight_rpa_worker.py
|
||
```
|
||
|
||
Worker 脚本:`scripts/rpa/freight_rpa_worker.py`(监听 `FREIGHT_RPA_*` 目录,Mock 模式设 `FREIGHT_RPA_MOCK=true`)。
|
||
|
||
### 1.5 前端入口
|
||
|
||
- 工作台侧边栏 **货代发货**(只读 case 列表)
|
||
- 独立路由 `/shipping-notice`
|
||
- **在 Chat 中继续** → `/chat/{agentId}?case_id=SN-...`
|
||
|
||
### 1.6 端到端验证
|
||
|
||
```bash
|
||
cd backend
|
||
pytest tests/test_shipping_notice_tools.py -q
|
||
```
|
||
|
||
Tool 流程:`create_case` → `parse_notice` → HTTP 上传 / `get_inventory_schema_preview` → `apply_inventory_mapping` → 字段对齐确认 → `match_inventory` → `generate_shipment_excel` → `upload_to_freight`。
|
||
|
||
### 1.7 待业务提供
|
||
|
||
- 货代系统库存导出 SOP + 样例 Excel 列名
|
||
- **AWS 库存模板**(默认映射):`AWS Sku` → sku,`COM ID` / `Reservation Order` → reservation_id,`Qty Available` → 可用数量;匹配键 **SKU + Reservation ID**
|
||
- 货代上传 SOP + 正式上传模板列映射
|
||
- 生产 Windows RPA 脚本
|
||
|
||
---
|
||
|
||
## 2. Dify OpenAPI 配置
|
||
|
||
### 2.1 前置条件
|
||
|
||
|
||
| 项 | 说明 |
|
||
| --------- | ----------------------------------------- |
|
||
| hxops 可访问 | Dify 必须能 HTTP 访问 **hxops 后端**(不是 Dify 自己) |
|
||
| Tool Key | `.env` 中 `SHIPPING_NOTICE_TOOL_API_KEY` |
|
||
| Dify 应用 | Chatflow,名称 **货代发货通知 Agent** |
|
||
|
||
|
||
### 2.2 端口说明(易混)
|
||
|
||
|
||
| 端口 | 服务 | Tool 是否用这个 |
|
||
| -------- | -------------------- | ---------------------------- |
|
||
| **8882** | Dify | ❌ 不要写进 OpenAPI `servers.url` |
|
||
| **8890** | hxops 前端 + `/api` 反代 | ✅ 部署联调 |
|
||
| **5052** | hxops backend 直连 | ✅ 本机 / Dify 访问宿主机 |
|
||
|
||
|
||
OpenAPI `servers[0].url` 示例:
|
||
|
||
- `http://192.168.100.192:8890/api/shipping-notice`
|
||
- `http://192.168.36.114:5052/api/shipping-notice`
|
||
|
||
验证:
|
||
|
||
```bash
|
||
curl -s http://192.168.100.192:8890/api/shipping-notice/tools/openapi.json | head
|
||
```
|
||
|
||
导出静态 OpenAPI:
|
||
|
||
```bash
|
||
cd backend
|
||
set SHIPPING_NOTICE_TOOL_API_BASE=http://192.168.100.192:8890
|
||
python scripts/export_shipping_notice_openapi.py
|
||
# → docs/openapi/shipping_notice_tools.dify.json
|
||
```
|
||
|
||
### 2.3 步骤 1 — 创建 Chatflow 应用
|
||
|
||
1. Dify → **工作室** → **创建应用** → **Chatflow**
|
||
2. 名称:**货代发货通知 Agent**
|
||
3. 写入 `.env.dev` 后 `python seed_shipping_notice_agents.py`
|
||
|
||
### 2.4 步骤 2 — 导入自定义工具
|
||
|
||
**方式 A — URL 导入(推荐)**
|
||
|
||
```
|
||
http://192.168.100.192:8890/api/shipping-notice/tools/openapi.json
|
||
```
|
||
|
||
鉴权:Header `X-Shipping-Tool-Key` = `SHIPPING_NOTICE_TOOL_API_KEY`
|
||
|
||
确认 **11** 个 operation:`create_case`、`parse_notice`、`parse_inventory`、`get_inventory_schema_preview`、`apply_inventory_mapping`、`ensure_inventory`、`match_inventory`、`submit_decision`、`generate_shipment_excel`、`upload_to_freight`、`get_case_status`。
|
||
|
||
**方式 B — 粘贴** `docs/openapi/shipping_notice_tools.dify.json`
|
||
|
||
另有大库存专用 HTTP(**不在 OpenAPI 里**,画布 HTTP 节点配置):
|
||
|
||
```
|
||
POST /api/shipping-notice/tools/upload_inventory_file
|
||
```
|
||
|
||
### 2.5 步骤 3 — Agent + 工具
|
||
|
||
1. Agent 节点 → 工具 → 勾选全部 11 个 operation
|
||
2. **最大迭代次数** ≥ 10;策略 **Function Calling**
|
||
3. **指令**:粘贴 [第 5 节 Agent 系统指令](#5-agent-系统指令粘贴用) 全文
|
||
4. **画布**:按 [§6 Chatflow 画布参考](#6-chatflow-画布参考) 搭建(**必做「合并 Agent 输入」Code**)
|
||
|
||
### 2.6 步骤 4 — 发布验证
|
||
|
||
1. 发布 Chatflow
|
||
2. 第 1 轮:发货通知 → 获得 `case_id`
|
||
3. 第 2 轮:上传库存 `.xlsx` → HTTP 直传 → `apply_inventory_mapping` → 字段对齐确认 → 匹配
|
||
|
||
### 2.7 常见问题
|
||
|
||
|
||
| 问题 | 处理 |
|
||
| ---------------------- | --------------------------------------------------- |
|
||
| OpenAPI invalid schema | 用 `/tools/openapi.json`;POST 含 `parameters: []` |
|
||
| Tool 401 | 检查 `X-Shipping-Tool-Key` |
|
||
| Tool 连接失败 | `servers.url` 勿用 `localhost`;Dify SSRF 需放行 hxops IP |
|
||
| Agent 不调工具 | 已发布、已绑工具、已粘贴第 5 节指令 |
|
||
| token 超限 | 库存 Excel 勿进文档提取器 → 第 4 节 HTTP |
|
||
| Agent 跳过字段对齐 | 更新第 5 节指令;重导 OpenAPI |
|
||
|
||
|
||
字段抽取 Workflow **「发货通知字段抽取」** 由 `parse_notice` 内部调用,Chatflow 无需再绑。
|
||
|
||
**订单行必抽字段**(`lines[]` 每行):
|
||
|
||
| 系统字段 | 含义 | 常见表头 |
|
||
|---------|------|---------|
|
||
| `sku` | SKU / 料号 | AWS Sku、料号、Part Number |
|
||
| `qty` | 需求数量 | Qty、Order Qty、数量、发货数量 |
|
||
| `unit_price` | 单价 | Unit Price、单价、Price |
|
||
| `amount` | 金额 | Amount、Line Amount、金额、小计 |
|
||
| `reservation_id` | Reservation ID | Reservation ID、COM ID;**常见为 8~12 位纯数字**(如 `1000140351`) |
|
||
|
||
Workflow / 规则解析输出示例:
|
||
|
||
```json
|
||
{
|
||
"customer_name": "ACME",
|
||
"lines": [
|
||
{
|
||
"sku": "10-40594-001",
|
||
"qty": 2,
|
||
"unit_price": 15.5,
|
||
"amount": 31.0,
|
||
"reservation_id": "1000140351",
|
||
"uom": "EA"
|
||
}
|
||
],
|
||
"confidence": 0.9
|
||
}
|
||
```
|
||
|
||
**库存匹配**(后端 `match_inventory`):按 **SKU + Reservation ID** 汇总库存可用量,与订单 `qty` 比对;单价/金额仅用于展示与导出,不参与匹配。
|
||
|
||
---
|
||
|
||
## 3. OpenAPI 变更说明
|
||
|
||
### 3.1 变更摘要
|
||
|
||
|
||
| 类别 | 变更 | Dify 是否要改 |
|
||
| ----------------- | ------------------------------------------------------------------- | ------------ |
|
||
| Tool 数量 | **11** 个(+`get_inventory_schema_preview`、`apply_inventory_mapping`) | 重新导入 |
|
||
| Tool 响应 | 新增 `checkpoint`、`stop_agent_turn`、`forbidden_next_tools` | 更新 Agent 指令 |
|
||
| `submit_decision` | +`confirm_inventory_mapping`、`reparse_inventory` | 重新导入 OpenAPI |
|
||
| `parse_inventory` | +`use_uploaded_file`、`column_mapping_json`;大文本拒绝 | 重导 + 更新指令 |
|
||
| 大库存 | HTTP 上传 → `schema_preview` → `apply_inventory_mapping` | 画布 + 指令 |
|
||
| 列映射 | LLM 只推断 **列名映射**;完整解析在后端 | 更新 Agent 指令 |
|
||
|
||
|
||
**Breaking change**:不能 `parse_inventory` 同轮连调 `match_inventory`;须 `confirm_inventory_mapping` 后再匹配。
|
||
|
||
### 3.2 标准流程
|
||
|
||
```text
|
||
create_case → parse_notice → HTTP 上传(或 RPA 落盘 / 小表 parse_inventory)
|
||
→ get_inventory_schema_preview(若 HTTP 已返回 schema_preview 可跳过)
|
||
→ apply_inventory_mapping(column_mapping_json=...)
|
||
→ 展示 field_alignment → submit_decision(confirm_inventory_mapping)
|
||
→ match_inventory → generate_shipment_excel → upload_to_freight
|
||
```
|
||
|
||
### 3.3 `submit_decision` decision 枚举
|
||
|
||
```json
|
||
["confirm", "confirm_parse", "reject", "rematch", "reparse", "confirm_inventory_mapping", "correct_inventory_mapping", "reparse_inventory"]
|
||
```
|
||
|
||
|
||
| decision | 何时用 |
|
||
| -------------------------------- | --------------------------------------- |
|
||
| `confirm` / `confirm_parse` | **发货通知解析**确认(match **之前**;不会设为 matched) |
|
||
| `confirm_inventory_mapping` | 字段对齐确认(match **之前**) |
|
||
| `correct_inventory_mapping` | 更正列映射(回到 `inventory_mapping_inference`) |
|
||
| `reparse_inventory` | 重新上传库存 |
|
||
| `confirm` | 库存不足时确认部分发货(match **之后**,此时才会 matched) |
|
||
| `reject` / `rematch` / `reparse` | 驳回 / 重匹配 / 重解析发货通知 |
|
||
|
||
|
||
### 3.4 库存列映射(LLM 推断 + 后端解析)
|
||
|
||
|
||
| 阶段 | checkpoint | 说明 |
|
||
| ---------------------------- | -------------------------------- | ------------------------------------------------------------------- |
|
||
| HTTP 上传成功 | `inventory_mapping_inference` | 返回 `data.schema_preview`(headers + sample_rows + suggested_mapping) |
|
||
| `apply_inventory_mapping` 成功 | `inventory_field_mapping_review` | 返回 `data.field_alignment`,`stop_agent_turn=true` |
|
||
| 用户确认后 | `inventory_mapping_confirmed` | 可调用 `match_inventory` |
|
||
|
||
|
||
`column_mapping_json` 示例:
|
||
|
||
```json
|
||
{"sku":"AWS Sku","reservation_id":"COM ID","available_qty":"Qty Available"}
|
||
```
|
||
|
||
### 3.5 `apply_inventory_mapping` 成功响应(字段对齐开启时)
|
||
|
||
|
||
| 字段 | 值 |
|
||
| ---------------------- | ----------------------------------------------------------------- |
|
||
| `status` | `needs_human` |
|
||
| `checkpoint` | `inventory_field_mapping_review` |
|
||
| `stop_agent_turn` | `true` |
|
||
| `forbidden_next_tools` | `match_inventory`, `generate_shipment_excel`, `upload_to_freight` |
|
||
|
||
|
||
### 3.6 `field_alignment` 结构(节选)
|
||
|
||
```json
|
||
{
|
||
"match_rule": "按 AWS Sku + Reservation ID 汇总可用数量,与订单 qty 比对",
|
||
"inventory_columns": { "mapping": { "sku": {}, "reservation_id": {}, "available_qty": {} } },
|
||
"sample_notice_lines": [],
|
||
"sample_inventory_lines": [],
|
||
"inventory_row_count": 120,
|
||
"notice_line_count": 3,
|
||
"warnings": []
|
||
}
|
||
```
|
||
|
||
### 3.7 可选后端配置
|
||
|
||
```env
|
||
SHIPPING_NOTICE_INVENTORY_MAPPING_REVIEW=false # 测试可关字段对齐
|
||
SHIPPING_NOTICE_INVENTORY_TEXT_MAX_CHARS=80000
|
||
FREIGHT_INVENTORY_COLUMN_MAP={"AWS Sku":"sku","COM ID":"reservation_id","Qty Available":"available_qty"}
|
||
```
|
||
|
||
---
|
||
|
||
## 4. 大库存 Excel — HTTP 直传
|
||
|
||
### 4.1 原则
|
||
|
||
AWS 库存 Excel 经文档提取器可达 **100 万+ tokens**,会触发 LLM 失败。**大 Excel never 进 LLM**;在 hxops 服务端解析。
|
||
|
||
### 4.2 如何判断走 HTTP 还是文档提取器
|
||
|
||
|
||
| 条件 | 路径 |
|
||
| --------------------------- | ------------------------------------------------- |
|
||
| `.xlsx` / `.xls` / `.csv` | **HTTP** `upload_inventory_file` |
|
||
| 文件名含 Inventory / 库存 / stock | **HTTP** |
|
||
| `.pdf` / Word / 邮件 | **文档提取器** → `parse_notice` |
|
||
| 聊天粘贴几行小表 | **Agent** → `parse_inventory(raw_inventory_text)` |
|
||
|
||
|
||
```text
|
||
.xlsx / .xls / .csv → HTTP(永远)
|
||
.pdf / Word → 文档提取器
|
||
```
|
||
|
||
### 4.3 HTTP 节点地址
|
||
|
||
```
|
||
POST http://192.168.36.114:5052/api/shipping-notice/tools/upload_inventory_file
|
||
```
|
||
|
||
(或 `http://192.168.100.192:8890/api/shipping-notice/tools/upload_inventory_file`)
|
||
|
||
|
||
| 项 | 值 |
|
||
| --------- | ---------------------------------------------------------------------------------------- |
|
||
| Method | POST |
|
||
| Headers | `X-Shipping-Tool-Key: <SHIPPING_NOTICE_TOOL_API_KEY>`(勿设 Content-Type: application/json) |
|
||
| Body | form-data |
|
||
| `case_id` | `{{#conversation.shipping_case_id#}}` |
|
||
| `file` | 开始节点库存 xlsx |
|
||
|
||
|
||
### 4.4 case_id 配置(会话变量)
|
||
|
||
1. 建会话变量 `**shipping_case_id**`
|
||
2. 第 1 轮 Agent 后 → **Code 节点**:
|
||
|
||
|
||
| Code 参数名 | 绑定 |
|
||
| ------------ | ---------------- |
|
||
| `agent_text` | Agent → **text** |
|
||
|
||
|
||
```python
|
||
import re
|
||
|
||
def main(agent_text: str) -> dict:
|
||
m = re.search(r"SN-\d{8}-[A-F0-9]{8}", agent_text or "", re.I)
|
||
return {"shipping_case_id": m.group(0) if m else ""}
|
||
```
|
||
|
||
1. **变量赋值** → `conversation.shipping_case_id`
|
||
2. 第 2 轮 HTTP 的 `case_id` = `{{#conversation.shipping_case_id#}}`
|
||
|
||
第 2 轮也可从 `sys.query` 提取(用户消息带 `SN-...`):Code 参数 `user_query` ← 开始 → `sys.query`。
|
||
|
||
**无 case_id 时不要调 HTTP**,提示用户先发发货通知。
|
||
|
||
### 4.5 推荐画布(单画布 + 合并 Code)
|
||
|
||
```text
|
||
开始 → 附件分类 → [PDF→文档提取器 | xlsx→HTTP] → 合并 Agent 输入 → Agent → 提取 case_id → 回复
|
||
```
|
||
|
||
详见 [§6 Chatflow 画布参考](#6-chatflow-画布参考)。
|
||
|
||
### 4.6 后端接口
|
||
|
||
**上传(仅保存 + 返回 schema_preview,不解析全文)**
|
||
|
||
```http
|
||
POST /api/shipping-notice/tools/upload_inventory_file
|
||
multipart: case_id + file
|
||
```
|
||
|
||
响应含 `data.schema_preview`:`headers`、`sample_rows`、`suggested_mapping`(规则兜底)。
|
||
|
||
**Agent 推断列映射并解析(Tool)**
|
||
|
||
```json
|
||
{
|
||
"case_id": "SN-...",
|
||
"column_mapping_json": "{\"sku\":\"AWS Sku\",\"reservation_id\":\"COM ID\",\"available_qty\":\"Qty Available\"}"
|
||
}
|
||
```
|
||
|
||
或先 `get_inventory_schema_preview`,再 `apply_inventory_mapping`。
|
||
|
||
**规则映射兜底(可选)**
|
||
|
||
```json
|
||
{ "case_id": "SN-...", "use_uploaded_file": "true" }
|
||
```
|
||
|
||
**curl 自测**
|
||
|
||
```powershell
|
||
curl.exe -X POST "http://127.0.0.1:5052/api/shipping-notice/tools/upload_inventory_file" `
|
||
-H "X-Shipping-Tool-Key: YOUR_KEY" `
|
||
-F "case_id=SN-20260529-TEST001" `
|
||
-F "file=@D:\path\to\inventory.xlsx"
|
||
```
|
||
|
||
### 4.7 Agent Query 模板(不含 Excel 正文)
|
||
|
||
Agent **输入变量**(均来自 §6.6「合并 Agent 输入」Code,勿直接绑文档提取器 / HTTP):
|
||
|
||
| Agent 变量 | 绑定 |
|
||
|------------|------|
|
||
| `extracted_text` | `合并 Agent 输入.extracted_text` |
|
||
| `inventory_upload_message` | `合并 Agent 输入.inventory_upload_message` |
|
||
|
||
Query 模板:
|
||
|
||
```jinja2
|
||
{% if inventory_upload_message %}
|
||
{{ inventory_upload_message }}
|
||
{% endif %}
|
||
|
||
{% if conversation.shipping_case_id %}
|
||
当前 case_id:{{#conversation.shipping_case_id#}}(确认/驳回时必须使用,禁止 create_case)
|
||
{% endif %}
|
||
|
||
用户消息:
|
||
{{#sys.query#}}
|
||
|
||
{% if extracted_text %}
|
||
发货通知提取内容(仅 parse_notice,勿用于库存):
|
||
{{ extracted_text }}
|
||
{% endif %}
|
||
```
|
||
|
||
> **第 2 轮仅上传库存 Excel 时**:不要接文档提取器,Query 里 `extracted_text` 应为空;库存信息来自 HTTP 的 `schema_preview` / Tool,不是 `extracted_text`。
|
||
|
||
---
|
||
|
||
## 5. Agent 系统指令(粘贴用)
|
||
|
||
将 **以下全文** 粘贴到 Dify Agent 节点 **指令 / Instructions**。
|
||
|
||
---
|
||
|
||
### ⛔ 最高优先级(违反即错误)
|
||
|
||
你处于 **状态机** 中。必须读取 Tool 返回的 `**checkpoint`**、`**stop_agent_turn**`、`**forbidden_next_tools**`。
|
||
|
||
#### 规则 A — 库存字段对齐是硬门禁
|
||
|
||
当 `apply_inventory_mapping` / `parse_inventory` / RPA 落盘返回:
|
||
|
||
- `status` = `needs_human`
|
||
- `checkpoint` = `inventory_field_mapping_review`
|
||
- `stop_agent_turn` = `true`
|
||
|
||
**立刻停止调用任何 Tool**,展示 `data.field_alignment`,**结束本轮,等待用户**。
|
||
|
||
**禁止同轮连调**:`apply_inventory_mapping → match_inventory` ❌
|
||
|
||
#### 规则 A2 — HTTP 上传后先推断列映射
|
||
|
||
当 HTTP 上传或 `get_inventory_schema_preview` 返回:
|
||
|
||
- `checkpoint` = `inventory_mapping_inference`
|
||
- `data.schema_preview` 含 `headers`、`sample_rows`、`suggested_mapping`
|
||
|
||
**你必须**根据表头与样例行推断 `column_mapping_json`(仅列名,不含全表数据),然后调用 `apply_inventory_mapping`。
|
||
|
||
`column_mapping_json` 格式:
|
||
|
||
```json
|
||
{"sku":"<Excel列名>","reservation_id":"<Excel列名或省略>","available_qty":"<Excel列名>"}
|
||
```
|
||
|
||
若 `suggested_mapping` 已完整且合理,可直接使用;否则根据 headers 自行推断。
|
||
|
||
#### 规则 B — 只有用户确认后才能匹配
|
||
|
||
1. 用户确认 **发货通知解析** → `submit_decision(confirm)` → 收到 `checkpoint=parse_confirmed` → **必须先做库存**,禁止 `generate_shipment_excel`
|
||
2. 库存字段对齐 → `submit_decision(confirm_inventory_mapping)` → `checkpoint=inventory_mapping_confirmed`
|
||
3. **下一轮** `match_inventory`(必须真实调用,不可臆造 matched)
|
||
4. `match_inventory` 成功且 `process_status=matched` 后,才可 `generate_shipment_excel`
|
||
|
||
|
||
| decision | 何时用 |
|
||
| --------------------------- | -------------------------------------------- |
|
||
| `confirm` / `confirm_parse` | 发货通知解析确认(**不会**设为 matched,禁止 generate_excel) |
|
||
| `confirm_inventory_mapping` | 字段对齐(match **前**) |
|
||
| `confirm` | 库存不足(match **后**,确认部分发货) |
|
||
|
||
|
||
#### 规则 C — 被拦截时不得重试 match
|
||
回到规则 A,展示 `field_alignment`,等用户确认。
|
||
#### 规则 E — 用户确认时禁止重建 case
|
||
当用户回复 **确认 / 重新解析 / 驳回**(或点击按钮)时:
|
||
1. **禁止** 调用 `create_case`(即使 submit 失败也不要重建)
|
||
2. 优先 `get_case_status(conversation_id={{sys.conversation_id}})` 取回 `case_id`
|
||
3. 再 `submit_decision(decision=confirm|reparse|reject, case_id=..., conversation_id=...)`
|
||
4. `submit_decision(confirm)` 返回 `checkpoint=parse_confirmed` 表示**成功**,下一步做库存,不是失败
|
||
`create_case` 必须带 `conversation_id={{sys.conversation_id}}`,以便后续轮次找回 case。
|
||
#### 规则 D — 大库存 Excel 禁止进 LLM
|
||
**禁止** 将库存 Excel 全文传入 `raw_inventory_text`。
|
||
- 大文件:Chatflow HTTP 上传 → 读 `schema_preview` → `apply_inventory_mapping`
|
||
- 小表(几行粘贴)才用 `parse_inventory(raw_inventory_text)`
|
||
---
|
||
### 角色与流程
|
||
你是 **货代发货通知助手**,通过 Hxops Tool API 处理发货指令。
|
||
```text
|
||
create_case → parse_notice → HTTP 上传 / 小表 parse_inventory
|
||
→ get_inventory_schema_preview(可选)→ apply_inventory_mapping
|
||
→ 【暂停】field_alignment → confirm_inventory_mapping → match_inventory
|
||
→ generate_shipment_excel → upload_to_freight
|
||
```
|
||
### Tool 响应字段
|
||
| 字段 | 含义 |
|
||
| ---------------------- | -------------------- |
|
||
| `checkpoint` | 当前卡点 |
|
||
| `stop_agent_turn` | true = 本轮停止 |
|
||
| `forbidden_next_tools` | 禁止调用的 Tool |
|
||
| `data.schema_preview` | 表头 + 样例行(LLM 推断列映射用) |
|
||
| `data.field_alignment` | 字段对齐详情 |
|
||
### 字段对齐展示模板
|
||
|
||
```text
|
||
【库存字段对齐确认 — 请确认后才能匹配】case_id: xxx
|
||
|
||
匹配规则:{field_alignment.match_rule}
|
||
|
||
| 系统字段 | 库存 Excel 列 | 订单字段 |
|
||
|---------|--------------|---------|
|
||
| AWS Sku | {mapping.sku.source_column} | sku |
|
||
| Reservation ID | {mapping.reservation_id.source_column} | reservation_id |
|
||
| 可用数量 | {mapping.available_qty.source_column} | qty |
|
||
| — | — | unit_price / amount(订单展示,不参与匹配) |
|
||
|
||
订单样例 / 库存样例 / warnings …
|
||
|
||
请回复「确认」「重新上传」或「取消」
|
||
```
|
||
|
||
**然后停止,不要在本轮调 Tool。**
|
||
|
||
|
||
| 用户说 | 调用 |
|
||
| ------------ | -------------------------------------------------------- |
|
||
| 点击 **确认** 按钮 | `submit_decision(confirm_inventory_mapping)` 或 `confirm` |
|
||
| 点击 **重新上传** | `submit_decision(reparse_inventory)` |
|
||
| 点击 **驳回/取消** | `submit_decision(reject)` |
|
||
|
||
|
||
> **Hxops 聊天页**(`/chat`)会在 Agent 回复下方自动显示操作按钮,用户无需打字。
|
||
|
||
**Hxops 聊天页上传库存(三种方式):**
|
||
|
||
| 入口 | 作用 |
|
||
|------|------|
|
||
| **「1 / 2 / 3」数字选项** | 按 Agent **上一轮菜单**解析:如 `1=上传`、`3=重试 RPA`(不会固定写死) |
|
||
| **「选择库存文件」按钮** | 弹出文件选择器,POST 到 hxops |
|
||
| **输入框左侧 📎 附件** | 把文件随消息发给 Dify(取决于 Chatflow 画布) |
|
||
|
||
数字 **`1` 不会固定等于 RPA**;系统会读取 Agent 消息里 `1.` `2.` `3.` 各行含义后再执行。
|
||
|
||
### 其他步骤摘要
|
||
|
||
- **parse_notice**:`raw_text` = 用户消息 + 发货通知提取内容(**不含**库存 Excel)
|
||
- **ensure_inventory**:仅无库存时 RPA;同样可能触发 field_alignment
|
||
- **match_inventory**:须先 `confirm_inventory_mapping`;不足时 `confirm/reject/rematch`
|
||
- **upload_to_freight**:RPA 或 `manual_confirm` + `freight_ref_no`
|
||
|
||
### 对话风格
|
||
|
||
- 简洁中文;复述 `case_id`
|
||
- 不编造列名、数量、单号;以 Tool `data` 为准
|
||
- **禁止**在未调用 `match_inventory` 成功前声称「已匹配 matched」或生成 Excel
|
||
|
||
---
|
||
|
||
## 6. Chatflow 画布参考
|
||
|
||
> 每轮用户消息:**开始 → 附件分流 → 合并 Agent 输入 → Agent → 提取 case_id → 回复**。
|
||
> PDF 走文档提取器;xlsx 走 HTTP;**Agent 只接「合并 Agent 输入」**,不直接接提取器 / HTTP。
|
||
|
||
### 6.0 总览
|
||
|
||
```mermaid
|
||
flowchart TD
|
||
START[开始] --> CLASSIFY[附件分类]
|
||
CLASSIFY --> IF_PDF{PDF/Word?}
|
||
CLASSIFY --> IF_XLSX{xlsx?}
|
||
|
||
IF_PDF -->|是| EXTRACT[文档提取器]
|
||
IF_XLSX -->|是+有case_id| HTTP[HTTP 上传]
|
||
IF_XLSX -->|是+无case_id| WARN[无 case_id 提示]
|
||
|
||
EXTRACT --> MERGE[合并 Agent 输入]
|
||
HTTP --> PARSE[解析 HTTP 响应]
|
||
PARSE --> MERGE
|
||
WARN --> MERGE
|
||
CLASSIFY --> MERGE
|
||
|
||
MERGE --> AGENT[Agent]
|
||
AGENT --> CASE[提取 case_id]
|
||
CASE --> ASSIGN[变量赋值]
|
||
ASSIGN --> ANSWER[直接回复]
|
||
```
|
||
|
||
| # | 节点 | 作用 |
|
||
|---|------|------|
|
||
| 1 | 开始 | 文字 + 可选附件 |
|
||
| 2 | 附件分类 | Code,判断 PDF / xlsx |
|
||
| 3 | 文档提取器 | 仅 PDF/Word,输出 `text` |
|
||
| 4 | HTTP + 解析响应 | 仅 xlsx 且有 `shipping_case_id` |
|
||
| 5 | 无 case_id 提示 | xlsx 但尚无 case_id 时 |
|
||
| 6 | **合并 Agent 输入** | **必做**,汇总两个 Agent 变量 |
|
||
| 7 | Agent | 11 Tools + Query §4.7 + 指令 §5 |
|
||
| 8 | 提取 case_id + 变量赋值 | 写入 `conversation.shipping_case_id` |
|
||
| 9 | 直接回复 | `{{#Agent.text#}}` |
|
||
|
||
---
|
||
|
||
### 6.1 前置
|
||
|
||
**会话变量**:`shipping_case_id`(string,初始 `""`)
|
||
|
||
**自定义工具(11 个)**:见 §2.4
|
||
|
||
---
|
||
|
||
### 6.2 开始节点
|
||
|
||
| 配置 | 值 |
|
||
|------|-----|
|
||
| 文件上传 | 开启,多文件 |
|
||
| 允许类型 | `.pdf` `.doc` `.docx` · `.xlsx` `.xls` `.csv` |
|
||
|
||
---
|
||
|
||
### 6.3 附件分类
|
||
|
||
Code 节点 `附件分类`:`query` ← `sys.query`,`files` ← 开始文件列表。
|
||
|
||
```python
|
||
def main(query: str, files: list | None = None) -> dict:
|
||
files = files or []
|
||
has_xlsx = has_notice_doc = False
|
||
for f in files:
|
||
name = str((f or {}).get("filename") or (f or {}).get("name") or "").lower()
|
||
ext = name.rsplit(".", 1)[-1] if "." in name else ""
|
||
if ext in {"xlsx", "xls", "csv"}:
|
||
has_xlsx = True
|
||
elif ext in {"pdf", "doc", "docx"}:
|
||
has_notice_doc = True
|
||
if any(k in name for k in ("inventory", "stock", "onhand", "库存")):
|
||
has_xlsx, has_notice_doc = True, False
|
||
return {"has_xlsx": has_xlsx, "has_notice_doc": has_notice_doc}
|
||
```
|
||
|
||
---
|
||
|
||
### 6.4 PDF 分支 → 文档提取器
|
||
|
||
**IF** `has_notice_doc == true` **且** `has_xlsx == false` → **文档提取器** `发货通知提取`
|
||
|
||
| 配置 | 绑定 |
|
||
|------|------|
|
||
| 输入文件 | 开始 → PDF/Word 附件 |
|
||
| 输出 | `text`(供 §6.6 的 `doc_text`) |
|
||
|
||
库存 xlsx **禁止**进文档提取器。
|
||
|
||
---
|
||
|
||
### 6.5 xlsx 分支 → HTTP 上传
|
||
|
||
**IF** `has_xlsx == true`:
|
||
|
||
| 子条件 | 节点 |
|
||
|--------|------|
|
||
| 有 `conversation.shipping_case_id` | HTTP `upload_inventory_file` → Code `解析上传响应` |
|
||
| 无 case_id | Code `无 case_id 提示`(不调用 HTTP) |
|
||
|
||
**HTTP 节点**
|
||
|
||
| 项 | 值 |
|
||
|----|-----|
|
||
| URL | `http://192.168.100.192:8890/api/shipping-notice/tools/upload_inventory_file` |
|
||
| Header | `X-Shipping-Tool-Key` |
|
||
| form-data | `case_id` = `{{#conversation.shipping_case_id#}}`,`file` = 开始附件 xlsx |
|
||
|
||
**解析上传响应**
|
||
|
||
```python
|
||
import json
|
||
|
||
def main(body: str) -> dict:
|
||
try:
|
||
data = json.loads(body or "{}")
|
||
except json.JSONDecodeError:
|
||
return {"inventory_upload_message": f"上传失败:非 JSON。{str(body)[:200]}"}
|
||
if data.get("status") != "ok":
|
||
return {"inventory_upload_message": f"上传失败:{data.get('message')}"}
|
||
preview = ((data.get("data") or {}).get("schema_preview")) or {}
|
||
headers = preview.get("headers") or []
|
||
suggested = preview.get("suggested_mapping") or {}
|
||
parts = [f"库存已 HTTP 上传。checkpoint={data.get('checkpoint')}。", f"case_id={data.get('case_id')}。"]
|
||
if headers:
|
||
parts.append("表头: " + " | ".join(str(h) for h in headers[:12]))
|
||
if suggested:
|
||
parts.append("suggested_mapping=" + json.dumps(suggested, ensure_ascii=False))
|
||
parts.append("请调用 apply_inventory_mapping。")
|
||
return {"inventory_upload_message": " ".join(parts)}
|
||
```
|
||
|
||
**无 case_id 提示**
|
||
|
||
```python
|
||
def main() -> dict:
|
||
return {
|
||
"inventory_upload_message": (
|
||
"已收到库存 Excel,但尚无 case_id。"
|
||
"请先上传发货通知并完成 parse_notice,获得 SN-... 后再传库存。"
|
||
)
|
||
}
|
||
```
|
||
|
||
---
|
||
|
||
### 6.6 合并 Agent 输入(必做)
|
||
|
||
**所有分支汇合后、Agent 之前**,添加 Code 节点 `合并 Agent 输入`:
|
||
|
||
| 输入参数 | 绑定 |
|
||
|----------|------|
|
||
| `doc_text` | `发货通知提取.text`(未跑提取器时为空) |
|
||
| `http_msg` | `解析上传响应.inventory_upload_message`(未跑 HTTP 时为空) |
|
||
| `no_case_msg` | `无 case_id 提示.inventory_upload_message`(未跑时为空) |
|
||
|
||
```python
|
||
def main(doc_text: str = "", http_msg: str = "", no_case_msg: str = "") -> dict:
|
||
return {
|
||
"extracted_text": doc_text or "",
|
||
"inventory_upload_message": http_msg or no_case_msg or "",
|
||
}
|
||
```
|
||
|
||
**Agent 变量绑定(Dify 操作)**
|
||
|
||
1. Agent 节点 → **变量** → 添加 `extracted_text`(String)→ 值选 **`合并 Agent 输入.extracted_text`**
|
||
2. 再添加 `inventory_upload_message`(String)→ 值选 **`合并 Agent 输入.inventory_upload_message`**
|
||
3. Query 模板用 §4.7(引用上述两个变量 + `{{#sys.query#}}` + `conversation.shipping_case_id`)
|
||
|
||
纯文字轮次(无 PDF、无 xlsx):三个输入均为空,Agent 仅靠 `sys.query` 与 case_id 工作。
|
||
|
||
**校验**:仅 PDF 时 Query 能看到发货通知正文;仅 xlsx 时 `extracted_text` 为空、`inventory_upload_message` 有内容。
|
||
|
||
---
|
||
|
||
### 6.7 Agent 节点
|
||
|
||
| 项 | 值 |
|
||
|----|-----|
|
||
| 工具 | 11 个 operation 全选 |
|
||
| 策略 | Function Calling,迭代 ≥ 10 |
|
||
| 指令 | §5 全文 |
|
||
| Query | §4.7 |
|
||
| 输入变量 | 仅 §6.6 两个输出 |
|
||
|
||
| 轮次 | Agent 主要 Tool |
|
||
|------|----------------|
|
||
| 1 | `create_case` → `parse_notice` |
|
||
| 2 | `apply_inventory_mapping`(HTTP 已上传后) |
|
||
| 3 | `submit_decision` / `match_inventory` |
|
||
| 4+ | `generate_shipment_excel` → `upload_to_freight` |
|
||
|
||
`create_case` 必带:`conversation_id={{sys.conversation_id}}`
|
||
|
||
---
|
||
|
||
### 6.8 提取 case_id
|
||
|
||
Code `提取 case_id`:`agent_text` ← Agent.text,`user_query` ← sys.query
|
||
|
||
```python
|
||
import re
|
||
_CASE = re.compile(r"SN-\d{8}-[A-F0-9]{8}", re.I)
|
||
|
||
def main(agent_text: str, user_query: str) -> dict:
|
||
for t in (agent_text or "", user_query or ""):
|
||
m = _CASE.search(t)
|
||
if m:
|
||
return {"shipping_case_id": m.group(0).upper()}
|
||
return {"shipping_case_id": ""}
|
||
```
|
||
|
||
**变量赋值**:`conversation.shipping_case_id` ← `提取 case_id.shipping_case_id`(非空时写入)
|
||
|
||
---
|
||
|
||
### 6.9 直接回复
|
||
|
||
```text
|
||
{{#货代 Agent.text#}}
|
||
```
|
||
|
||
---
|
||
|
||
### 6.10 各轮对照 · 验收 · 速查
|
||
|
||
**各轮路径**
|
||
|
||
| 轮次 | 用户 | 画布 |
|
||
|------|------|------|
|
||
| 1 | 发货通知 PDF | 提取器 → 合并 → Agent → case_id |
|
||
| 2 | 库存 xlsx | HTTP → 合并 → Agent |
|
||
| 3+ | 确认 / 更正映射 | 合并(空)→ Agent |
|
||
|
||
**Hxops `/chat`**:确认 / 更正映射 / 上传库存等按钮**直接调 hxops API**;Agent Query 必须用 `{{#sys.query#}}`。
|
||
|
||
**验收清单**
|
||
|
||
- [ ] `合并 Agent 输入` 在 Agent 之前
|
||
- [ ] Agent 变量只绑合并 Code,不直接绑提取器 / HTTP
|
||
- [ ] Query 含 `{{#sys.query#}}`
|
||
- [ ] xlsx 只走 HTTP;PDF 只走提取器
|
||
- [ ] `shipping_case_id` 第 1 轮后能写入
|
||
- [ ] `pytest tests/test_shipping_notice_tools.py` 通过
|
||
|
||
**Tool 速查**
|
||
|
||
| 输入 | Tool |
|
||
|------|------|
|
||
| 发货通知 PDF/文字 | `parse_notice` |
|
||
| 库存大 Excel | HTTP → `apply_inventory_mapping` |
|
||
| 库存小表 | `parse_inventory(raw_inventory_text)` |
|
||
| 字段对齐后 | `match_inventory` |
|
||
|
||
**典型顺序**:`create_case` → `parse_notice` → HTTP → `apply_inventory_mapping` → 确认 → `match_inventory` → `generate_shipment_excel` → `upload_to_freight`
|
||
|
||
**匹配规则**:SKU + Reservation ID 汇总 Qty Available,与订单 qty 比对(后端执行)
|
||
|
||
---
|
||
|
||
## 7. 相关代码与文件
|
||
|
||
|
||
| 路径 | 说明 |
|
||
| ------------------------------------------------------------ | --------------------------------- |
|
||
| `docs/shipping_notice_business.md` | 业务操作说明(与本文同步维护) |
|
||
| `backend/app/services/shipping_notice/tools.py` | Tool 状态机 |
|
||
| `backend/app/services/shipping_notice/match.py` | SKU + Reservation 匹配 |
|
||
| `backend/app/services/shipping_notice/inventory_snapshot.py` | Excel/文本解析 |
|
||
| `backend/app/services/shipping_notice/inventory_mapping.py` | LLM/规则列映射 |
|
||
| `backend/app/services/shipping_notice/inventory_upload.py` | 大文件上传 + schema preview |
|
||
| `backend/app/services/shipping_notice/openapi_spec.py` | OpenAPI 生成 |
|
||
| `backend/app/api/shipping_notice_tools.py` | Tool 路由 + `upload_inventory_file` |
|
||
| `docs/openapi/shipping_notice_tools.dify.json` | 静态 OpenAPI |
|
||
| `GET /api/shipping-notice/tools/openapi.json` | 动态 OpenAPI |
|
||
| `backend/tests/test_shipping_notice_tools.py` | 集成测试 |
|
||
| `scripts/rpa/freight_rpa_worker.py` | RPA Worker |
|
||
|
||
|