feat(shipping-notice): 新增货代发货通知全流程业务功能
- 新增后端完整业务支持:数据库模型、API接口、核心服务与RPA对接逻辑 - 新增前端工作台页面、路由适配与多语言国际化 - 新增Dify自定义工具OpenAPI规范、Agent配置脚本与部署文档 - 新增发货Excel模板生成脚本与默认模板 - 修复测试配置文件中的环境变量命名错误 - 更新本地开发环境变量配置
This commit is contained in:
1 parent
93e99793d8
commit
2d7bdecc43
46 files changed
+3462
-7
No files matched your search
@@ -0,0 +1,445 @@
|
||||
{
|
||||
"openapi": "3.0.0",
|
||||
"info": {
|
||||
"title": "Hxops Shipping Notice Tools",
|
||||
"version": "1.0.0",
|
||||
"description": "货代发货通知 Agent Tool API。仅包含 Chatflow 编排工具,不含 RPA 回调。"
|
||||
},
|
||||
"servers": [
|
||||
{
|
||||
"url": "http://192.168.100.192:8890/api/shipping-notice"
|
||||
}
|
||||
],
|
||||
"components": {
|
||||
"securitySchemes": {
|
||||
"ShippingToolKey": {
|
||||
"type": "apiKey",
|
||||
"in": "header",
|
||||
"name": "X-Shipping-Tool-Key",
|
||||
"description": "与 hxops SHIPPING_NOTICE_TOOL_API_KEY 相同"
|
||||
}
|
||||
}
|
||||
},
|
||||
"security": [
|
||||
{
|
||||
"ShippingToolKey": []
|
||||
}
|
||||
],
|
||||
"paths": {
|
||||
"/tools/create_case": {
|
||||
"post": {
|
||||
"operationId": "create_case",
|
||||
"summary": "创建发货任务 case",
|
||||
"description": "创建发货任务 case",
|
||||
"parameters": [],
|
||||
"requestBody": {
|
||||
"required": true,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"user_id": {
|
||||
"type": "integer",
|
||||
"description": "Hxops 用户 ID(可选)"
|
||||
},
|
||||
"conversation_id": {
|
||||
"type": "string",
|
||||
"description": "Dify 会话 ID,便于工作台关联"
|
||||
},
|
||||
"source_type": {
|
||||
"type": "string",
|
||||
"description": "来源:manual|email|wecom|pdf",
|
||||
"enum": [
|
||||
"manual",
|
||||
"email",
|
||||
"wecom",
|
||||
"pdf"
|
||||
]
|
||||
},
|
||||
"raw_content": {
|
||||
"type": "string",
|
||||
"description": "原始发货通知文本(可选,后续 parse 也可传入)"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Tool response (status: ok|running|needs_human|failed)",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"security": [
|
||||
{
|
||||
"ShippingToolKey": []
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"/tools/parse_notice": {
|
||||
"post": {
|
||||
"operationId": "parse_notice",
|
||||
"summary": "解析发货通知文本",
|
||||
"description": "解析发货通知文本",
|
||||
"parameters": [],
|
||||
"requestBody": {
|
||||
"required": true,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"case_id": {
|
||||
"type": "string",
|
||||
"description": "发货任务编号,如 SN-20260529-ABCD1234"
|
||||
},
|
||||
"raw_text": {
|
||||
"type": "string",
|
||||
"description": "待解析的发货通知全文"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"case_id"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Tool response (status: ok|running|needs_human|failed)",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"security": [
|
||||
{
|
||||
"ShippingToolKey": []
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"/tools/ensure_inventory": {
|
||||
"post": {
|
||||
"operationId": "ensure_inventory",
|
||||
"summary": "确保库存快照可用(RPA 或手工 JSON)",
|
||||
"description": "确保库存快照可用(RPA 或手工 JSON)",
|
||||
"parameters": [],
|
||||
"requestBody": {
|
||||
"required": true,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"case_id": {
|
||||
"type": "string",
|
||||
"description": "发货任务编号,如 SN-20260529-ABCD1234"
|
||||
},
|
||||
"mode": {
|
||||
"type": "string",
|
||||
"description": "auto=自动/RPA;retry_rpa=重试 RPA",
|
||||
"enum": [
|
||||
"auto",
|
||||
"retry_rpa"
|
||||
]
|
||||
},
|
||||
"inventory_lines_json": {
|
||||
"type": "string",
|
||||
"description": "手工库存 JSON 数组字符串,如 [{\"sku\":\"A001\",\"available_qty\":10}]"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"case_id"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Tool response (status: ok|running|needs_human|failed)",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"security": [
|
||||
{
|
||||
"ShippingToolKey": []
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"/tools/match_inventory": {
|
||||
"post": {
|
||||
"operationId": "match_inventory",
|
||||
"summary": "匹配发货需求与库存",
|
||||
"description": "匹配发货需求与库存",
|
||||
"parameters": [],
|
||||
"requestBody": {
|
||||
"required": true,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"case_id": {
|
||||
"type": "string",
|
||||
"description": "发货任务编号,如 SN-20260529-ABCD1234"
|
||||
},
|
||||
"inventory_lines_json": {
|
||||
"type": "string",
|
||||
"description": "可选,覆盖库存 JSON 数组后再匹配"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"case_id"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Tool response (status: ok|running|needs_human|failed)",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"security": [
|
||||
{
|
||||
"ShippingToolKey": []
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"/tools/submit_decision": {
|
||||
"post": {
|
||||
"operationId": "submit_decision",
|
||||
"summary": "人工复核决策",
|
||||
"description": "人工复核决策",
|
||||
"parameters": [],
|
||||
"requestBody": {
|
||||
"required": true,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"case_id": {
|
||||
"type": "string",
|
||||
"description": "发货任务编号,如 SN-20260529-ABCD1234"
|
||||
},
|
||||
"decision": {
|
||||
"type": "string",
|
||||
"description": "confirm=确认继续;reject=驳回;rematch=重新匹配;reparse=重新解析",
|
||||
"enum": [
|
||||
"confirm",
|
||||
"reject",
|
||||
"rematch",
|
||||
"reparse"
|
||||
]
|
||||
},
|
||||
"comment": {
|
||||
"type": "string",
|
||||
"description": "备注(可选)"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"case_id",
|
||||
"decision"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Tool response (status: ok|running|needs_human|failed)",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"security": [
|
||||
{
|
||||
"ShippingToolKey": []
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"/tools/generate_shipment_excel": {
|
||||
"post": {
|
||||
"operationId": "generate_shipment_excel",
|
||||
"summary": "生成固定模板发货 Excel",
|
||||
"description": "生成固定模板发货 Excel",
|
||||
"parameters": [],
|
||||
"requestBody": {
|
||||
"required": true,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"case_id": {
|
||||
"type": "string",
|
||||
"description": "发货任务编号,如 SN-20260529-ABCD1234"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"case_id"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Tool response (status: ok|running|needs_human|failed)",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"security": [
|
||||
{
|
||||
"ShippingToolKey": []
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"/tools/upload_to_freight": {
|
||||
"post": {
|
||||
"operationId": "upload_to_freight",
|
||||
"summary": "上传 Excel 到货代系统(RPA 或手工确认)",
|
||||
"description": "上传 Excel 到货代系统(RPA 或手工确认)",
|
||||
"parameters": [],
|
||||
"requestBody": {
|
||||
"required": true,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"case_id": {
|
||||
"type": "string",
|
||||
"description": "发货任务编号,如 SN-20260529-ABCD1234"
|
||||
},
|
||||
"mode": {
|
||||
"type": "string",
|
||||
"description": "auto=触发 RPA;retry_upload=重试;manual_confirm=手工上传完成",
|
||||
"enum": [
|
||||
"auto",
|
||||
"retry_upload",
|
||||
"manual_confirm"
|
||||
]
|
||||
},
|
||||
"freight_ref_no": {
|
||||
"type": "string",
|
||||
"description": "货代系统单号(manual_confirm 时填写)"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"case_id"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Tool response (status: ok|running|needs_human|failed)",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"security": [
|
||||
{
|
||||
"ShippingToolKey": []
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"/tools/get_case_status": {
|
||||
"post": {
|
||||
"operationId": "get_case_status",
|
||||
"summary": "查询 case 当前状态与 human_options",
|
||||
"description": "查询 case 当前状态与 human_options",
|
||||
"parameters": [],
|
||||
"requestBody": {
|
||||
"required": true,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"case_id": {
|
||||
"type": "string",
|
||||
"description": "发货任务编号,如 SN-20260529-ABCD1234"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"case_id"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Tool response (status: ok|running|needs_human|failed)",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"security": [
|
||||
{
|
||||
"ShippingToolKey": []
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,211 @@
|
||||
# Dify Chatflow 绑定 OpenAPI — 操作手册
|
||||
|
||||
本文说明如何将 hxops **货代发货通知 Tool API** 绑定到 Dify **Chatflow** 应用「货代发货通知 Agent」。
|
||||
|
||||
---
|
||||
|
||||
## 前置条件
|
||||
|
||||
| 项 | 说明 |
|
||||
|----|------|
|
||||
| hxops 可访问 | Dify 服务器必须能 HTTP 访问 **hxops 后端**(不是 Dify 自己) |
|
||||
| Tool API 基地址 | 见下方「端口说明」 |
|
||||
| Tool Key | `.env` 中 `SHIPPING_NOTICE_TOOL_API_KEY` |
|
||||
| Dify 应用 | Chatflow 类型,名称 **货代发货通知 Agent** |
|
||||
|
||||
### 端口说明(易混)
|
||||
|
||||
| 端口 | 服务 | OpenAPI / Tool 是否用这个 |
|
||||
|------|------|---------------------------|
|
||||
| **8882** | **Dify**(`DIFY_API_BASE`) | ❌ **不要**写进 OpenAPI `servers.url` |
|
||||
| **8890** | hxops 前端 + `/api` 反代(docker `8890:80`) | ✅ **Dify 调 Tool 用这个** |
|
||||
| **8891** | 旧知识库/Integration 等(视环境而定) | 一般不用作 Tool API |
|
||||
| **5052** | hxops 后端直连(`backend/run.py`) | ✅ 本地开发、Dify 容器访问宿主机 |
|
||||
|
||||
OpenAPI JSON 里 `servers[0].url` 必须是 **hxops Tool API** 地址,例如:
|
||||
|
||||
- 部署/联调:`http://192.168.100.192:8890/api/shipping-notice`
|
||||
- 直连 backend(无 nginx):`http://192.168.100.192:5052/api/shipping-notice`
|
||||
|
||||
**8882 是 Dify 自己的端口**,Agent 执行工具时会从 Dify 发 HTTP 到 hxops;若写成 8882,请求会打到 Dify 自身,Tool 必然失败。
|
||||
|
||||
可在 `.env` 单独指定(优先于 `PUBLIC_BASE_URL`):
|
||||
|
||||
```env
|
||||
SHIPPING_NOTICE_TOOL_API_BASE=http://192.168.100.192:8890
|
||||
```
|
||||
|
||||
验证 OpenAPI 可访问:
|
||||
|
||||
```bash
|
||||
curl -s http://192.168.100.192:8890/api/shipping-notice/tools/openapi.json | head
|
||||
```
|
||||
|
||||
`servers[0].url` 应为 `http://192.168.100.192:8890/api/shipping-notice`。
|
||||
|
||||
本地导出静态文件(可选):
|
||||
|
||||
```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
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 步骤 1:在 Dify 创建 Chatflow 应用
|
||||
|
||||
1. 登录 Dify 控制台 → **工作室** → **创建应用**
|
||||
2. 类型选 **Chatflow**(对话型工作流,支持 Agent 工具调用)
|
||||
3. 名称:**货代发货通知 Agent**
|
||||
4. 创建后记录 **App ID** 与 **API Key**(应用 → API 访问)
|
||||
|
||||
写入 hxops `.env.dev` 并 seed:
|
||||
|
||||
```env
|
||||
SHIPPING_NOTICE_CHAT_DIFY_APP_ID=<app-id>
|
||||
SHIPPING_NOTICE_CHAT_DIFY_API_KEY=app-xxxxxxxx
|
||||
SHIPPING_NOTICE_CHAT_DIFY_BASE=http://192.168.100.192:8882/v1
|
||||
```
|
||||
|
||||
```bash
|
||||
cd backend
|
||||
python seed_shipping_notice_agents.py
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 步骤 2:导入自定义工具(OpenAPI)
|
||||
|
||||
### 方式 A — 从 URL 导入(推荐)
|
||||
|
||||
1. Dify → **工具** → **自定义** → **创建自定义工具**
|
||||
2. 名称:`Hxops Shipping Notice`(或 `货代发货通知 Tools`)
|
||||
3. Schema 来源:**从 URL 导入**
|
||||
4. URL:
|
||||
|
||||
```
|
||||
http://192.168.100.192:8890/api/shipping-notice/tools/openapi.json
|
||||
```
|
||||
|
||||
将 host 换成你的 `PUBLIC_BASE_URL`。
|
||||
|
||||
5. 若 Dify 未自动识别鉴权,手动配置:
|
||||
- 认证类型:**API Key**
|
||||
- Header 名称:`X-Shipping-Tool-Key`
|
||||
- API Key 值:与 hxops `SHIPPING_NOTICE_TOOL_API_KEY` 一致
|
||||
|
||||
6. 点击 **测试** / **保存**,确认 8 个工具均解析成功:
|
||||
- `create_case`
|
||||
- `parse_notice`
|
||||
- `ensure_inventory`
|
||||
- `match_inventory`
|
||||
- `submit_decision`
|
||||
- `generate_shipment_excel`
|
||||
- `upload_to_freight`
|
||||
- `get_case_status`
|
||||
|
||||
> OpenAPI 已针对 Dify 优化:每个 POST 含 `parameters: []`、扁平 `requestBody` 字段、不含 RPA 回调接口。
|
||||
|
||||
### 方式 B — 粘贴 Schema
|
||||
|
||||
若 URL 导入失败(网络隔离),使用导出的 JSON:
|
||||
|
||||
```
|
||||
docs/openapi/shipping_notice_tools.dify.json
|
||||
```
|
||||
|
||||
在创建自定义工具时选择 **粘贴 OpenAPI Schema**,粘贴文件内容后同样配置 API Key Header。
|
||||
|
||||
YAML 版本(可选):
|
||||
|
||||
```
|
||||
GET /api/shipping-notice/tools/openapi.dify.yaml
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 步骤 3:在 Chatflow 启用 Agent + 工具
|
||||
|
||||
1. 打开 **货代发货通知 Agent** Chatflow 编排页
|
||||
2. 确认画布中有 **Agent** 节点(或 **LLM + 工具** 节点,视 Dify 版本而定)
|
||||
3. 在 Agent 节点 → **工具** → 添加刚创建的 **Hxops Shipping Notice** 自定义工具
|
||||
4. 勾选全部 8 个 operation(或按需勾选,建议全选)
|
||||
|
||||
---
|
||||
|
||||
## 步骤 4:配置系统提示词
|
||||
|
||||
将 `docs/shipping_notice_dify_prompt.md` 中「角色」至「对话风格」段落粘贴到 Chatflow **系统提示词 / Instructions**。
|
||||
|
||||
要点:
|
||||
|
||||
- 必须先 `create_case`,全程携带 `case_id`
|
||||
- `status=needs_human` 时等待用户选择,勿跳过
|
||||
- `inventory_lines_json` 传 JSON 字符串,例如:`[{"sku":"A001","available_qty":10}]`
|
||||
|
||||
可选 Chatflow 变量(开始节点):
|
||||
|
||||
| 变量 | 用途 |
|
||||
|------|------|
|
||||
| `sys.user_id` / 自定义 `user_id` | 传入 `create_case.user_id` |
|
||||
| `sys.conversation_id` | 传入 `create_case.conversation_id` |
|
||||
|
||||
---
|
||||
|
||||
## 步骤 5:发布并验证
|
||||
|
||||
1. **发布** Chatflow 应用
|
||||
2. 在 hxops 前端打开该 Agent Chat,或 Dify 预览窗口测试
|
||||
3. 发送示例:
|
||||
|
||||
```
|
||||
客户:ACME,料号 A001 x 10,请处理发货。
|
||||
```
|
||||
|
||||
4. 在 Dify **日志** 中确认 Agent 依次调用:
|
||||
- `create_case` → `parse_notice` → `ensure_inventory` → …
|
||||
|
||||
5. 工作台 **货代发货** 应出现新 case;状态为 `running` 时表示 RPA 进行中,可 `get_case_status` 轮询。
|
||||
|
||||
---
|
||||
|
||||
## 常见问题
|
||||
|
||||
### OpenAPI 导入报 invalid schema
|
||||
|
||||
- 确认使用 `/tools/openapi.json`(新版 Dify 兼容 schema),不要用旧版泛型 `object`
|
||||
- POST 需含 `parameters: []`(已在 spec 中内置)
|
||||
- 避免 nested object 参数;库存用 `inventory_lines_json` 字符串
|
||||
|
||||
### Dify 调 Tool 401 Unauthorized
|
||||
|
||||
- 检查自定义工具 Header `X-Shipping-Tool-Key` 是否与 backend 一致
|
||||
- backend 未配置 key 时返回 503
|
||||
|
||||
### Dify 调 Tool 连接失败
|
||||
|
||||
- Dify 容器/进程访问的是 `localhost:5052` 会失败;`servers.url` 必须是 Dify 能 reach 的内网 IP
|
||||
- 设置 `PUBLIC_BASE_URL=http://<hxops-host>:<port>`
|
||||
|
||||
### Agent 不调用工具
|
||||
|
||||
- 确认 Chatflow 已发布(非草稿)
|
||||
- Agent 节点已绑定自定义工具
|
||||
- 系统提示词明确要求使用 Tool API
|
||||
|
||||
### 与内部 Workflow 的关系
|
||||
|
||||
字段抽取 Workflow **「发货通知字段抽取」** 由 hxops `parse_notice` 内部调用,**无需**在 Chatflow 再绑一次。
|
||||
|
||||
---
|
||||
|
||||
## 相关文件
|
||||
|
||||
| 文件 | 说明 |
|
||||
|------|------|
|
||||
| `backend/app/services/shipping_notice/openapi_spec.py` | Dify 兼容 OpenAPI 生成 |
|
||||
| `GET /api/shipping-notice/tools/openapi.json` | 动态 spec(含绝对 server URL) |
|
||||
| `docs/shipping_notice_dify_prompt.md` | 系统提示词 |
|
||||
| `docs/shipping_notice_runbook.md` | 整体部署 Runbook |
|
||||
@@ -0,0 +1,90 @@
|
||||
# 货代发货通知 Agent — Dify Chatflow 系统提示词
|
||||
|
||||
将以下内容配置到 Dify 应用 **「货代发货通知 Agent」**(`chatflow`)的系统提示词中,并绑定 Hxops Tool OpenAPI:`GET /api/shipping-notice/tools/openapi.json`(Header:`X-Shipping-Tool-Key`)。
|
||||
|
||||
---
|
||||
|
||||
## 角色
|
||||
|
||||
你是 **货代发货通知助手**,帮助业务人员处理客户发货指令(邮件/微信/PDF 文本)。你通过调用 Hxops Tool API 完成确定性流程,并在需要人工复核时暂停,等待用户明确选择后再继续。
|
||||
|
||||
## 核心原则
|
||||
|
||||
1. **先建 case**:收到新的发货需求时,先调用 `create_case`,保存 `case_id`(如 `SN-20260529-ABCD1234`),后续所有 Tool 都带上 `case_id`。
|
||||
2. **按序编排**:`parse_notice` → `ensure_inventory` → `match_inventory` →(人工确认)→ `generate_shipment_excel` → `upload_to_freight`。
|
||||
3. **尊重 Tool 响应**:每次 Tool 返回 `status` 为 `needs_human` 或 `running` 时,向用户说明原因与可选操作(`human_options`),**不要擅自跳过**。
|
||||
4. **人工决策**:用户说「确认/驳回/重试 RPA/手工上传完成」等时,调用 `submit_decision` 或对应 Tool 的 `mode` 参数。
|
||||
5. **状态查询**:不确定进度时调用 `get_case_status`。
|
||||
|
||||
## Tool 响应约定
|
||||
|
||||
```json
|
||||
{
|
||||
"status": "ok | running | needs_human | failed",
|
||||
"case_id": "SN-YYYYMMDD-XXXXXXXX",
|
||||
"message": "人类可读说明",
|
||||
"data": {},
|
||||
"human_options": ["confirm", "reject", "retry_rpa", ...]
|
||||
}
|
||||
```
|
||||
|
||||
| status | 含义 | 你的行为 |
|
||||
|--------|------|----------|
|
||||
| `ok` | 步骤成功 | 继续下一步或总结 |
|
||||
| `running` | 异步进行中(如 RPA) | 告知用户等待,可稍后 `get_case_status` |
|
||||
| `needs_human` | 必须人工选择 | 列出 `human_options`,等用户回复 |
|
||||
| `failed` | 不可恢复错误 | 说明错误,建议联系管理员 |
|
||||
|
||||
## 典型流程
|
||||
|
||||
### 1. 解析发货通知
|
||||
|
||||
用户粘贴邮件/微信/PDF 文本 → `parse_notice`(`case_id`, `raw_content`)。
|
||||
|
||||
- `needs_human` + `low_parse_confidence`:展示解析字段,请用户 **confirm / reject / reparse**。
|
||||
- `confirm` → `submit_decision`(`decision: confirm`)。
|
||||
|
||||
### 2. 库存
|
||||
|
||||
`ensure_inventory`(`case_id`, `mode: auto`)。
|
||||
|
||||
- `running`:RPA 已触发,告知等待。
|
||||
- `needs_human` + 无库存:提供 **retry_rpa** 或 **upload_inventory_file**(用户上传 Excel 路径/内容由 Tool 处理)。
|
||||
- 手工库存可传 `inventory_lines_json` 字符串,如 `[{"sku":"A001","available_qty":10}]`。
|
||||
- `ok` → 继续匹配。
|
||||
|
||||
### 3. 匹配
|
||||
|
||||
`match_inventory`(`case_id`)。
|
||||
|
||||
- `needs_human` + 库存不足:展示 shortage,请 **confirm / reject / rematch**。
|
||||
- `ok` + `fulfillable` → 生成 Excel。
|
||||
|
||||
### 4. 生成 Excel
|
||||
|
||||
`generate_shipment_excel`(`case_id`)→ 告知文件名,可下载链接:`/api/shipping-notice/cases/{case_no}/export`。
|
||||
|
||||
### 5. 上传货代
|
||||
|
||||
`upload_to_freight`(`case_id`, `mode: auto`)。
|
||||
|
||||
- `running`:RPA 上传中。
|
||||
- `needs_human`:提供 **retry_upload / download_excel / mark_manual_uploaded**。
|
||||
- 用户手工上传后说「已上传,单号 XXX」→ `upload_to_freight`(`mode: manual_confirm`, `freight_ref_no: XXX`)。
|
||||
|
||||
## 对话风格
|
||||
|
||||
- 简洁中文,先给结论再列明细表格(SKU、数量、缺口)。
|
||||
- 每次操作复述当前 `case_id`。
|
||||
- 不要编造库存或货代单号;以 Tool 返回的 `data` 为准。
|
||||
|
||||
## 变量(可选)
|
||||
|
||||
在 Chatflow 中可注入:
|
||||
|
||||
- `{{user_id}}` — 创建 case 时传入
|
||||
- `{{conversation_id}}` — Dify 会话 ID,便于工作台关联
|
||||
|
||||
## 内部 Workflow
|
||||
|
||||
字段抽取 Workflow **「发货通知字段抽取」** 由 `parse_notice` Tool 内部调用,Chatflow **无需**直接调用。
|
||||
@@ -0,0 +1,93 @@
|
||||
# 货代发货通知 — 本地部署 Runbook
|
||||
|
||||
## 1. 数据库迁移
|
||||
|
||||
```bash
|
||||
cd backend
|
||||
flask db upgrade
|
||||
```
|
||||
|
||||
## 2. 环境变量(`.env.dev` 示例)
|
||||
|
||||
```env
|
||||
# Tool API(Dify / RPA 回调共用,可与 INTEGRATION_API_KEY 相同)
|
||||
SHIPPING_NOTICE_TOOL_API_KEY=your-tool-key
|
||||
|
||||
# 文件存储(可选,默认 instance/shipping_notice)
|
||||
SHIPPING_NOTICE_STORAGE_ROOT=backend/data/shipping_notice
|
||||
|
||||
# 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 模板(可选,默认 backend/templates/freight_shipment_upload.xlsx)
|
||||
FREIGHT_SHIPMENT_TEMPLATE_PATH=backend/templates/freight_shipment_upload.xlsx
|
||||
|
||||
# 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
|
||||
```
|
||||
|
||||
## 3. 注册 Agent 记录
|
||||
|
||||
```bash
|
||||
cd backend
|
||||
python seed_shipping_notice_agents.py
|
||||
```
|
||||
|
||||
在 Dify 创建同名应用后,把 `dify_app_id` / API Key 写入 `.env` 并重新 seed。
|
||||
|
||||
## 4. Dify Chatflow 配置
|
||||
|
||||
详细图文步骤见 **`docs/shipping_notice_dify_openapi_setup.md`**。
|
||||
|
||||
1. 创建 Chatflow 应用 **货代发货通知 Agent**,写入 `.env` 后 `python seed_shipping_notice_agents.py`
|
||||
2. Dify → **工具 → 自定义** → 从 URL 导入:
|
||||
`http://<PUBLIC_BASE_URL>/api/shipping-notice/tools/openapi.json`
|
||||
3. 鉴权 Header:`X-Shipping-Tool-Key: <SHIPPING_NOTICE_TOOL_API_KEY>`
|
||||
4. Chatflow Agent 节点绑定该工具(8 个 operation)
|
||||
5. 系统提示词:复制 `docs/shipping_notice_dify_prompt.md`
|
||||
|
||||
## 5. 开发环境 Mock RPA
|
||||
|
||||
```powershell
|
||||
# 与 backend 相同四个 FREIGHT_* 目录
|
||||
$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
|
||||
```
|
||||
|
||||
详见 `scripts/rpa/README.md`。
|
||||
|
||||
## 6. 前端入口
|
||||
|
||||
- 工作台侧边栏 **货代发货**(只读 case 列表)
|
||||
- 独立路由 `/shipping-notice`
|
||||
- 点击 **在 Chat 中继续** → `/chat/{agentId}?case_id=SN-...` 自动查询状态
|
||||
|
||||
## 7. 端到端验证
|
||||
|
||||
```bash
|
||||
cd backend
|
||||
pytest tests/test_shipping_notice_tools.py -q
|
||||
```
|
||||
|
||||
Tool 流程:create → parse → ensure_inventory(inventory_lines)→ match → generate_excel → get_case_status。
|
||||
|
||||
## 8. 待业务提供
|
||||
|
||||
- 货代系统库存导出 SOP + 样例 Excel 列名(可配置 `FREIGHT_INVENTORY_COLUMN_MAP`)
|
||||
- 货代上传 SOP + 正式上传模板列映射
|
||||
- 生产 Windows RPA 账号与 UiPath/Power Automate 脚本
|
||||
Reference in new issue
Block a user