diff --git a/docs/shipping_notice.md b/docs/shipping_notice.md index 76e4dc8c..59a7b7da 100644 --- a/docs/shipping_notice.md +++ b/docs/shipping_notice.md @@ -83,6 +83,21 @@ FREIGHT_SHIPMENT_TEMPLATE_PATH=templates/freight_shipment_upload.xlsx # 建单后延后 parse_notice 到 Dify 画布(默认 true,符合 Dify 编排优先) # SHIPPING_NOTICE_DEFER_PARSE_TO_DIFY=true +# 批次后台调度:每个批次最多同时派发几张票(默认 3) +# SHIPPING_NOTICE_BATCH_DISPATCH_CONCURRENCY=3 + +# 单票调用 Dify 解析的总超时(秒,默认 180) +# SHIPPING_NOTICE_DIFY_PARSE_TIMEOUT_SECONDS=180 + +# 发货后台任务执行模式:worker / sync / manual +# SHIPPING_NOTICE_TASK_EXECUTION_MODE=worker + +# 发货 worker 轮询间隔(秒) +# SHIPPING_NOTICE_WORKER_POLL_SECONDS=2 + +# 发货后台任务 running 超时回收(秒) +# SHIPPING_NOTICE_JOB_RUNNING_TIMEOUT_SECONDS=300 + # Dify Chatflow(对话续办) SHIPPING_NOTICE_CHAT_DIFY_APP_ID= SHIPPING_NOTICE_CHAT_DIFY_API_KEY= @@ -147,10 +162,11 @@ pytest tests/test_shipping_notice_tools.py tests/test_shipping_notice_query_serv - **一个 batch,多个 task** - **多文件上传 = 批次建单,不合单** -- **Dify 负责首轮订单需求解析 + 续办编排和解释**(库存映射建议、确认、匹配、上传推进;**禁止**在后端偷偷调 Dify blocking 替代画布) +- **Dify 负责单票订单需求解析 + 对话续办编排和解释**(库存映射建议、确认、匹配、上传推进) - **hxops 后端为唯一真相源** - **Hxops 聊天前端 + Dify 对话编排**(唯一现行集成方式) - **一期包含自动上传,但必须可人工接管** +- **批次控制权在 hxops 后端**:后端负责拆票、派发、并发、超时、失败隔离;Dify 不得循环整批 后端实现要求: @@ -163,15 +179,26 @@ pytest tests/test_shipping_notice_tools.py tests/test_shipping_notice_query_serv #### Dify 负责 - 对话编排(Chatflow / Agent) +- 单票发货通知理解与结构化抽取 - 库存字段映射建议(LLM → `map_json`) - 用业务语言解释当前步骤 - 引导确认 / 驳回 / 重试 / 切换任务 - 解析结果修订、库存确认、匹配推进、导出上传 - 自定义分流、Code 节点、Tool 节点组合(**任何能在 Dify 实现的续办逻辑都应放在 Dify**) +**Dify 禁止承担的职责** + +- 循环整批任务 +- 切换到下一票继续处理 +- 长时间等待人工节点恢复 +- 维护批次级重试、超时、并发和失败隔离 +- 代替 hxops 持有主状态机 + #### hxops 后端负责 - batch / task 创建与持久化 +- 批次拆票、后台派发、并发控制 +- 单票执行锁、超时、失败隔离 - 7 步状态机 - 审计与快照 - **PDF/Word 抽文**(edkb loader,二进制入口无法迁入 Dify) @@ -219,6 +246,183 @@ OpenAPI `servers[0].url` 示例: **1.14.1 实配要点** +- 当前唯一推荐方式是:**hxops 负责批次调度,Dify 负责单票理解与续办** +- Chatflow 中必须保留 `current_task_no`,它是 Dify 当前票处理的唯一主键 +- 禁止在画布中以 `current_batch_no` 做循环 +- 禁止在画布中调用 `switch_focus_task` 作为自动推进手段 +- 单票 parse 节点执行时间必须短,超时后由 hxops 标记失败或等待重试 + +### 2.2.2 Dify Chatflow 完整配置步骤 + +#### Step 1:创建应用 + +- Dify 新建一个 `Chatflow` +- 应用模式选择 `advanced-chat` +- 应用名称建议:`货代发货通知 Agent` + +#### Step 2:导入 Tool + +- 进入 Dify 工具页 +- 导入 hxops 导出的 OpenAPI +- Server URL 使用: + - `http://:8890/api/shipping-notice` + - 或 `http://:5052/api/shipping-notice` +- 认证方式使用: + - `Authorization: Bearer ` + - 或 `X-Integration-Key: ` + +#### Step 3:配置 Start 输入 + +Start 节点至少保留以下输入: + +- `conversation_id` +- `current_batch_no` +- `current_task_no` + +约束: + +- `current_task_no` 是必需输入 +- `current_batch_no` 仅用于日志和展示,不能用于循环 +- 不要在 Start 节点放“批次任务列表”变量 + +#### Step 4:配置单票解析主链路 + +推荐画布结构: + +1. `Start` +2. `get_task_status` +3. `IF: 当前票是否仍待解析` +4. `LLM: 解析发货通知正文` +5. `parse_notice(structured=...)` +6. `Answer: 返回当前票解析摘要` + +其中: + +- `get_task_status` 必须传 `task_id = current_task_no` +- `parse_notice` 必须传 `task_id = current_task_no` +- 只允许读取和处理当前票 `raw_text` + +#### Step 5:配置解析节点 Prompt + +LLM 节点必须满足以下约束: + +- 只处理当前票 +- 禁止切换任务 +- 禁止循环整批 +- 禁止询问“是否开始解析” +- 优先输出结构化 JSON,供 `parse_notice(structured=...)` 使用 + +建议抽取字段: + +- `customer_name` +- `customer_code` +- `ship_to` +- `po_number` +- `required_date` +- `lines` + +`lines` 每项建议包含: + +- `sku` +- `reservation_id` +- `qty` +- `uom` +- `required_date` + +#### Step 6:配置续办节点 + +首轮解析之后,Dify 可以继续负责编排这些动作: + +- 解析结果确认 / 修订 +- 库存字段映射建议 +- 字段对齐确认问答 +- 匹配结果解释 +- 生成 Excel / 上传前说明 +- 调用 `submit_decision`、`apply_inventory_mapping`、`match_inventory`、`generate_excel` 等 Tool + +但必须遵守: + +- 人工节点一旦出现,当前轮次结束 +- 不要在 Dify 内等待人工回复后继续跑长链路 +- 用户下一次回复后,再由新的 Tool 调用继续推进 + +### 2.2.3 hxops 调度配置 + +当前后端调度策略如下: + +- 批次上传后,hxops 创建一个 batch 和多个 task +- 后台批次入口只负责**逐票入队** +- 独立 worker 消费单票任务,执行 Dify 解析与后续自动推进 +- 单票失败不会阻塞整批 + +关键配置项: + +| 配置项 | 默认值 | 说明 | +|------|------|------| +| `SHIPPING_NOTICE_BATCH_ORCHESTRATOR_ENABLED` | `true` | 是否启用后台派发 | +| `SHIPPING_NOTICE_BATCH_DISPATCH_CONCURRENCY` | `3` | 单个批次最多同时派发几张票 | +| `SHIPPING_NOTICE_DIFY_PARSE_TIMEOUT_SECONDS` | `180` | 单票 Dify 解析总超时 | +| `SHIPPING_NOTICE_DEFER_PARSE_TO_DIFY` | `true` | 建单后是否把首轮 parse 延后到 Dify | +| `SHIPPING_NOTICE_TASK_EXECUTION_MODE` | `worker` | 发货后台任务执行模式 | +| `SHIPPING_NOTICE_WORKER_POLL_SECONDS` | `2` | worker 轮询间隔 | +| `SHIPPING_NOTICE_JOB_RUNNING_TIMEOUT_SECONDS` | `300` | running 任务超时回收 | + +建议: + +- 首次上线从 `2~3` 并发开始 +- Dify 模型较慢时,不要把并发开太高 +- 超时值先用 `180` 秒,后续按真实运行情况收敛 + +### 2.2.5 Shipping Worker 启动 + +当前发货后台任务已改为持久化队列 + 独立 worker 消费,不再依赖 Web 进程内长线程。 + +推荐启动方式: + +```bash +cd backend +python workers/worker_shipping_notice.py +``` + +若希望一个 worker 同时消费知识库和发货队列: + +```bash +cd backend +python worker.py --queues kb,shipping --config production +``` + +运行预期: + +- 上传通知后,Web 侧只负责建 batch/task 并入队 +- `worker_shipping_notice.py` 轮询领取 `shipping_notice_run` 任务 +- worker 执行单票 Dify parse 与后续 `auto_progress` +- 单票卡住、失败、超时只影响当前票,不会把 Web 进程拖死 + +### 2.2.4 失败恢复与重试 + +当前恢复策略: + +- 单票 Dify 解析超时或自动推进中断时,只影响当前票 +- 批次中其他票仍可继续派发 +- 需要人工确认的票不会被批次重试入口强行重跑 + +当前提供两个用户态恢复入口: + +| 接口 | 说明 | +|------|------| +| `POST /api/workspace/shipping-tasks/{task_no}/retry` | 重试当前票 | +| `POST /api/workspace/shipping-batches/{batch_no}/retry-pending` | 重试批次中仍可自动推进的票 | + +恢复规则: + +- `retry task` 会清理 `review_status`、`pending_human_reason`、`error_message` +- 后端会按现有业务真相恢复任务到最近可续跑状态: + - 无解析结果:回到 `created` + - 已有通知行但无库存:回到 `parsed` + - 已有库存:回到 `inventory_ready` + - 已生成导出文件:回到 `matched` +- 对于已经进入人工节点的票,批次级重试会自动跳过,避免误把人工决策覆盖掉 + - 当前唯一方案采用 **Dify Chatflow + Agent + OpenAPI Tool**:由 Chatflow 承载流程,由 Agent 读取 hxops 状态、进行自然语言解释,并调用 Tool 自动编排推进业务步骤。 - 须在 Dify **工具**页导入 `shipping_notice_tools.dify.json`,在 OpenAPI Credential 中配置 Server URL,并使用 `Authorization: Bearer ` 或 Header `X-Integration-Key`;**无需** Start 输入变量或 Chatflow 环境变量注入 `HXOPS_TOOLS_API_BASE`。 - 自定义 API 工具的 `provider_id` **因 Dify 实例而异**;手动创建画布后,须在各 **Tool 节点**中重新选择 **Hxops Shipping Notice** 对应工具。 @@ -242,6 +446,13 @@ Hxops 聊天区上传 1..N 个通知 → Dify:get_task_status → LLM 解析订单需求 → parse_notice(structured=...) → 解析确认/修订 → 库存映射/匹配/上传推进 ``` +**强制口径**: + +- 当前端已完成通知上传并自动发起续办消息后,Dify **必须立即进入通知解析**。 +- Dify **禁止**再次询问用户“是否开始解析”“是否现在开始解析”“请确认是否开始解析”等同义话术。 +- 正确行为是:读取 `raw_text` → LLM 抽取 `structured` → 调 `parse_notice(structured=...)` → 直接返回解析结果或解析复核卡片。 +- 只有在 `parse_notice` / 后端状态机明确返回 `needs_human` 时,才允许进入人工确认语义,而不是在解析前额外加一道口头确认。 + **分工要点** - 建单、抽文、`raw_text` 落库:**Hxops** @@ -641,7 +852,17 @@ flowchart TD V --> W W --> X["Tool 读取任务状态"] X --> Y["工具结果标准化"] - Y --> AA{"是否待解析通知?"} + Y --> Y1["当前任务动作判断"] + Y1 --> Y2{"是否进入 Agent?"} + Y2 -- "否" --> Y3{"当前任务直连动作分支"} + Y3 -- "retry_rpa" --> Y4["Tool 重试库存导出"] + Y3 -- "confirm / reject / reparse / confirm_inventory_mapping / correct_inventory_mapping / reparse_inventory" --> Y5["Tool 提交当前任务决策"] + Y3 -- "rematch" --> Y6["Tool 重新匹配库存"] + Y4 --> Y8["直连动作结果标准化"] + Y5 --> Y8 + Y6 --> Y8 + Y8 --> Z["直接回复"] + Y2 -- "是" --> AA{"是否待解析通知?"} AA -- "是" --> AB["提取通知原文"] AB --> AC["订单需求解析 LLM"] AC --> AD["结构化结果标准化"] @@ -706,15 +927,45 @@ flowchart TD -> Tool 读取任务状态 -> 工具结果标准化 -> 是否待解析通知 - -> 否:Agent 状态输入标准化 + -> 否:当前任务动作判断 + -> 直接动作:直连 Tool 执行链 + -> 其他:Agent 状态输入标准化 -> 是:提取 raw_text -> 订单需求解析 LLM -> structured JSON 标准化 -> Tool parse_notice(structured=...) -> 解析结果标准化 -> Agent 状态输入标准化 -> 货代续办 Agent -> match_inventory / auto_progress / generate_shipment_excel / upload_to_freight ``` +##### D. 当前任务直连动作链 + +```text +用户点击按钮 / 输入短动作 +-> 预处理上下文 +-> 附件意图分类 +-> 分支调度 +-> Tool 读取任务状态 +-> 工具结果标准化 +-> 当前任务动作判断 +-> 是否进入 Agent + -> true ----------------------> 是否待解析通知 / Agent 状态输入标准化 / 货代续办 Agent + -> false ---------------------> 当前任务直连动作分支 + -> retry_rpa -------------------------> Tool ensure_inventory(task_id, mode=retry_rpa) + -> confirm / reject / reparse -------> Tool submit_decision(task_id, decision={{direct_action}}) + -> confirm_inventory_mapping ---------> Tool submit_decision(task_id, decision=confirm_inventory_mapping) + -> correct_inventory_mapping ---------> Tool submit_decision(task_id, decision=correct_inventory_mapping) + -> reparse_inventory -----------------> Tool submit_decision(task_id, decision=reparse_inventory) + -> rematch ---------------------------> Tool match_inventory(task_id) +-> 直连动作结果标准化 +-> 直接回复(动作结果) +``` + +说明: + +- `retry_rpa / confirm / reject / reparse / confirm_inventory_mapping` 都属于**确定性动作**,生产口径应优先走画布直连 Tool,不要进入 Agent 二次思考。 +- Agent 只保留给**模糊自由输入**,例如“下一步怎么办”“为什么还卡在这里”“第二行数量改成 12”这类需要理解上下文的消息。 + #### 2.5.7 节点总表 -##### 主链节点 +##### A. 入口与总路由节点 | 节点名 | 节点 ID | 类型 | 主要职责 | 核心输入 | 核心输出 | |------|------|------|------|------|------| @@ -723,42 +974,95 @@ flowchart TD | 附件意图分类 | `1740000001003` | Code | 区分通知附件、库存附件、切票、续办 | `预处理上下文.normalized_query`、`sys.files` | `route_key`、`reason` | | 分支调度 | `1740000001004` | Code | 决定 direct / inventory / agent 三条路径 | `附件意图分类.route_key`、任务号 | `branch`、`final_reply`、`resolved_task_no` | | 主路由分支节点 | `1740000001005` | If-Else / Branch | 按 `branch` 一次分流到库存链 / 任务续办链 / 引导回复 | `分支调度.branch` | `inventory_path / agent_path / direct_answer` | + +##### B. `inventory_path` 分支节点 + +| 节点名 | 节点 ID | 类型 | 主要职责 | 核心输入 | 核心输出 | +|------|------|------|------|------|------| +| 库存任务检查 | 自动生成 | Code | 判断库存文件当前是否有可绑定任务 | `resolved_task_no`、`current_task_no` | `task_id`、`has_task`、`final_reply` | +| 是否已有库存绑定任务 | 自动生成 | If-Else | 判断当前库存链是否已有 task_id | `库存任务检查.has_task` | true / false | +| 无TASK_ID提示 | 自动生成 | Answer | 没任务号时直接提示 | `库存任务检查.final_reply` | 最终回复 | +| Tool 读取库存 schema 预览 | 自动生成 | Tool | 调 `get_inventory_schema_preview` | `库存任务检查.task_id` | Tool `text` | +| 库存 schema 标准化 | 自动生成 | Code | 提取 headers / sample_rows | `Tool 读取库存 schema 预览.text` | `headers_json`、`sample_rows_json`、`schema_preview_json` | +| 库存列映射 LLM | 自动生成 | LLM | 推断 `column_mapping_json` | `headers_json`、`sample_rows_json` | `text` | +| 列映射 JSON 标准化 | 自动生成 | Code | 把 LLM 输出转成合法 JSON | `库存列映射 LLM.text` | `column_mapping_json`、`mapping_ok`、`error_message` | +| 库存列映射是否成功 | 自动生成 | If-Else | 判断列映射 JSON 是否可用于 apply | `列映射 JSON 标准化.mapping_ok` | true / false | +| 列映射失败回复 | 自动生成 | Answer | 非法 JSON 时回复用户 | `列映射 JSON 标准化.error_message` | 最终回复 | +| Tool 应用库存列映射 | 自动生成 | Tool | 调 `apply_inventory_mapping` | `task_id`、`column_mapping_json` | Tool `text` | +| 映射结果标准化 | 自动生成 | Code | 标准化 apply 结果 JSON | `Tool 应用库存列映射.text` | `tool_status`、`tool_message`、`tool_checkpoint`、`raw_json` | + +##### C. `direct_answer` 分支节点 + +| 节点名 | 节点 ID | 类型 | 主要职责 | 核心输入 | 核心输出 | +|------|------|------|------|------|------| +| 直接回复(引导) | `1740000001014` | Answer | 输出固定引导文案 | `分支调度.final_reply` | 最终回复 | + +##### D. `agent_path` 公共前置节点 + +| 节点名 | 节点 ID | 类型 | 主要职责 | 核心输入 | 核心输出 | +|------|------|------|------|------|------| | 任务切换意图解析 | `1740000001006` | Code | 识别 `切到 SN-...`,并兼容 `next` / `prev` 意图标记 | `normalized_query`、`route_key` | `switch_mode`、`target_task_no` | | 是否需要切换任务 | `1740000001007` | If-Else | 决定是否调切票 Tool | `任务切换意图解析.switch_mode` | true / false | | Tool 切换聚焦任务 | `1740000001008` | Tool | 调 `switch_focus_task`,把批次焦点切到新的 `SN-...` | `current_batch_no`、`target_task_no` | Tool `text` | | 切换结果标准化 | 自动生成 | Code | 解析 `switch_focus_task.text`,提取新的 `task_no` / `batch_no` | `Tool 切换聚焦任务.text` | `switched_task_no`、`switched_batch_no`、`switch_status`、`raw_json` | | 准备读取任务状态 | `1740000001009` | Code | 显式收口本轮最新 `task_id`,优先读取“切换结果标准化”的输出 | `切换结果标准化.switched_task_no`、上游任务号、会话号 | `task_id`、`conversation_id` | | Tool 读取任务状态 | `1740000001010` | Tool | 调 `get_task_status` | `task_id`、`conversation_id` | Tool `text` | -| 工具结果标准化 | `1740000001011` | Code | 标准化任务状态 JSON | `Tool 读取任务状态.text` | `tool_status`、`tool_message`、`tool_checkpoint`、`raw_json` | -| 是否待解析通知 | 自动生成 | If-Else | 判断当前是否应走订单需求解析链 | `工具结果标准化.raw_json` | true / false | +| 工具结果标准化 | `1740000001011` | Code | 标准化任务状态 JSON,并产出首轮通知解析判断布尔值 | `Tool 读取任务状态.text` | `tool_status`、`tool_message`、`tool_checkpoint`、`tool_data_json`、`raw_json`、`should_parse_notice` | +| 当前任务动作判断 | 自动生成 | Code | 判断当前是否为可直连 Tool 的确定性动作 | `intent_hint`、`resolved_task_no` | `direct_action`、`need_agent` | +| 是否进入 Agent | 自动生成 | If-Else | 当 `need_agent=true` 时继续通知解析 / Agent 链,否则进入直连动作链 | `当前任务动作判断.need_agent` | true / false | + +##### E. 当前任务直连动作分支节点 + +| 节点名 | 节点 ID | 类型 | 主要职责 | 核心输入 | 核心输出 | +|------|------|------|------|------|------| +| 当前任务直连动作分支 | 自动生成 | If-Else / Branch | 按 Tool 分组分发确定性动作,减少重复 Tool 节点 | `当前任务动作判断.direct_action` | `retry_rpa / submit_decision_path / rematch` | +| Tool 重试库存导出 | 自动生成 | Tool | 调 `ensure_inventory(task_id, mode=retry_rpa)` | `task_id` | Tool `text` | +| Tool 提交当前任务决策 | 自动生成 | Tool | 调 `submit_decision(task_id, decision={{direct_action}})`,统一承接确认、驳回、重解析、字段对齐确认等动作 | `task_id`、`direct_action` | Tool `text` | +| Tool 重新匹配库存 | 自动生成 | Tool | 调 `match_inventory(task_id)` | `task_id` | Tool `text` | +| 直连动作结果标准化 | 自动生成 | Code | 统一解析直连 Tool 返回,并生成最终回复文本 | 各直连 Tool `text` | `tool_status`、`tool_message`、`tool_checkpoint`、`raw_json`、`answer_text` | +| 直接回复(动作结果) | 自动生成 | Answer | 输出直连动作 Tool 文本 | `直连动作结果标准化.answer_text` | 最终回复 | + +##### F. 通知解析与 Agent 分支节点 + +| 节点名 | 节点 ID | 类型 | 主要职责 | 核心输入 | 核心输出 | +|------|------|------|------|------|------| +| 是否待解析通知 | 自动生成 | If-Else | 判断当前是否应走订单需求解析链 | `工具结果标准化.should_parse_notice` | true / false | | 提取通知原文 | 自动生成 | Code | 从状态 JSON 提取 `raw_text` / `parse_msg` | `工具结果标准化.raw_json` | `notice_raw_text`、`task_id` | | 订单需求解析 LLM | 自动生成 | LLM | 将 `raw_text` 解析成 `structured` | `提取通知原文.notice_raw_text` | `text` | | 结构化结果标准化 | 自动生成 | Code | 把 LLM 输出转成 `structured_json` | `订单需求解析 LLM.text` | `structured_json`、`parse_ok`、`error_message` | | Tool 解析发货通知 | 自动生成 | Tool | 调 `parse_notice(structured=...)` | `task_id`、`structured_json` | Tool `text` | | 解析结果标准化 | 自动生成 | Code | 标准化 parse_notice 返回 | `Tool 解析发货通知.text` | `tool_status`、`tool_message`、`tool_checkpoint`、`raw_json` | | Agent 状态输入标准化 | 自动生成 | Code | 合并普通续办 / 通知解析 / 库存映射三条链的最新状态 | `工具结果标准化.raw_json`、`解析结果标准化.raw_json`、`映射结果标准化.raw_json` | `agent_raw_json`、`agent_task_id`、`agent_checkpoint` | -| 货代续办 Agent | `1740000001012` | Agent | 根据状态推进业务 | `normalized_query`、`agent_raw_json`、`agent_task_id` | `text` | +| 货代续办 Agent | `1740000001012` | Agent | 仅处理模糊自由输入、解释性问题与复杂续办 | `normalized_query`、`agent_raw_json`、`agent_task_id` | `text` | +| Agent 指令(维护入口) | 自动生成 | 文档锚点 | 指向生产可粘贴的完整 Agent Instruction | 无 | 无 | | 直接回复 | `1740000001013` | Answer | 输出 Agent 文本 | `货代续办 Agent.text` | 最终回复 | -| 直接回复(引导) | `1740000001014` | Answer | 输出固定引导文案 | `分支调度.final_reply` | 最终回复 | - -##### 库存补充链节点 - -| 节点名 | 类型 | 主要职责 | 核心输入 | 核心输出 | -|------|------|------|------|------| -| 库存任务检查 | Code | 判断库存文件当前是否有可绑定任务 | `resolved_task_no`、`current_task_no` | `task_id`、`has_task`、`final_reply` | -| 是否已有库存绑定任务 | If-Else | 判断当前库存链是否已有 task_id | `库存任务检查.has_task` | true / false | -| 无TASK_ID提示 | Answer | 没任务号时直接提示 | `库存任务检查.final_reply` | 最终回复 | -| Tool 读取库存 schema 预览 | Tool | 调 `get_inventory_schema_preview` | `库存任务检查.task_id` | Tool `text` | -| 库存 schema 标准化 | Code | 提取 headers / sample_rows | `Tool 读取库存 schema 预览.text` | `headers_json`、`sample_rows_json`、`schema_preview_json` | -| 库存列映射 LLM | LLM | 推断 `column_mapping_json` | `headers_json`、`sample_rows_json` | `text` | -| 列映射 JSON 标准化 | Code | 把 LLM 输出转成合法 JSON | `库存列映射 LLM.text` | `column_mapping_json`、`mapping_ok`、`error_message` | -| 库存列映射是否成功 | If-Else | 判断列映射 JSON 是否可用于 apply | `列映射 JSON 标准化.mapping_ok` | true / false | -| 列映射失败回复 | Answer | 非法 JSON 时回复用户 | `列映射 JSON 标准化.error_message` | 最终回复 | -| Tool 应用库存列映射 | Tool | 调 `apply_inventory_mapping` | `task_id`、`column_mapping_json` | Tool `text` | -| 映射结果标准化 | Code | 标准化 apply 结果 JSON | `Tool 应用库存列映射.text` | `tool_status`、`tool_message`、`tool_checkpoint`、`raw_json` | #### 2.5.8 节点逐项配置 +##### 阅读顺序(按流程图) + +建议按下面顺序阅读本章节点配置,和 `§2.5.5 总流程图` 保持一致: + +```text +A. 入口与总路由 + 节点 1 -> 节点 2 -> 节点 3 -> 节点 4 -> 节点 5 + +B. inventory_path 分支 + 节点 12 -> 节点 12A -> 节点 13 -> 节点 14 -> 节点 15 -> 节点 16 -> 节点 17 -> 节点 17A -> 节点 18 -> 节点 19 -> 节点 20 + +C. direct_answer 分支 + 节点 24 + +D. agent_path 公共前置 + 节点 6 -> 节点 7 -> 节点 8 -> 节点 8.1 -> 节点 9 -> 节点 10 -> 节点 11 -> 节点 11H -> 节点 11I + +E. 当前任务直连动作分支 + 节点 11J -> 节点 11K-11M -> 节点 11S -> 节点 11T + +F. 通知解析与 Agent 分支 + 节点 11A -> 节点 11B -> 节点 11C -> 节点 11D -> 节点 11E -> 节点 11F -> 节点 11G -> 节点 21 -> 节点 22 -> 节点 23 +``` + ##### 节点 1:开始(1740000001001) 类型:`Start` @@ -789,7 +1093,7 @@ flowchart TD | `normalized_query` | 标准化后的用户消息 | | `detected_batch_no` | 识别出的批次号 | | `detected_task_no` | 识别出的任务号 | -| `intent_hint` | `message / batch_switch / task_switch` | +| `intent_hint` | `message / batch_switch / task_switch / retry_rpa / confirm / reparse / reject / confirm_inventory_mapping / correct_inventory_mapping / reparse_inventory` | | `conversation_id` | 透传会话号 | | `error_message` | 预留错误文本 | @@ -807,11 +1111,24 @@ def main(query: str = "", conversation_id: str = "", current_batch_no: str = "", batch_match = BATCH_RE.search(text) normalized_query = " ".join(text.split()) intent_hint = "message" + lower_text = normalized_query.lower() if batch_match: intent_hint = "batch_switch" - elif task_match: - intent_hint = "task_switch" - elif any(word in normalized_query for word in ["上一票", "下一票", "切到", "切换"]): + elif re.search(r"(重试|重新)\s*rpa|重试.*(导出|获取|拉取).*库存|rpa.*(导出|获取|拉取).*重试", normalized_query, re.I): + intent_hint = "retry_rpa" + elif normalized_query in {"确认", "confirm"}: + intent_hint = "confirm" + elif re.search(r"重新解析|重解析|reparse", normalized_query, re.I): + intent_hint = "reparse" + elif re.search(r"驳回|取消|reject", normalized_query, re.I): + intent_hint = "reject" + elif re.search(r"确认字段对齐|确认映射|confirm_inventory_mapping", normalized_query, re.I): + intent_hint = "confirm_inventory_mapping" + elif re.search(r"更正映射|更正列映射|correct_inventory_mapping", normalized_query, re.I): + intent_hint = "correct_inventory_mapping" + elif re.search(r"重新上传库存|重传库存|reparse_inventory", normalized_query, re.I): + intent_hint = "reparse_inventory" + elif re.search(r"切到|切换到|换到|处理\s*sn-|上一票|下一票|\bnext\b|\bprev\b|\bprevious\b", lower_text, re.I): intent_hint = "task_switch" return { "normalized_query": normalized_query, @@ -853,11 +1170,27 @@ def main(normalized_query: str = "", intent_hint: str = "", sys_files=None): return {"route_key": "notice_batch_upload", "reason": "notice_files"} if intent_hint == "task_switch": return {"route_key": "task_switch", "reason": "switch_intent"} + if intent_hint in { + "retry_rpa", + "confirm", + "reparse", + "reject", + "confirm_inventory_mapping", + "correct_inventory_mapping", + "reparse_inventory", + }: + return {"route_key": "continue_task", "reason": "task_action"} if text: return {"route_key": "continue_task", "reason": "normal_message"} return {"route_key": "direct_reply", "reason": "empty_input"} ``` +说明: + +- `retry_rpa / confirm / reparse / reject / confirm_inventory_mapping / correct_inventory_mapping / reparse_inventory` 都属于**当前任务动作**,必须落到 `continue_task`,不能因为文本里带了 `SN-...` 就提前归成 `task_switch`。 +- 只有出现明确切换语义(如 `切到 SN-...`、`切换到`、`换到`、`上一票`、`下一票`)时,`intent_hint` 才允许为 `task_switch`。 +- 本规则的目的,是避免把“重试 RPA 导出库存 任务编号:SN-...”这类动作误送进 `switch_focus_task`。 + ##### 节点 4:分支调度(1740000001004) 输入映射: @@ -868,6 +1201,7 @@ def main(normalized_query: str = "", intent_hint: str = "", sys_files=None): | `normalized_query` | `预处理上下文(1740000001002).normalized_query` | | `detected_task_no` | `预处理上下文(1740000001002).detected_task_no` | | `current_task_no` | `conversation.current_task_no` | +| `intent_hint` | `预处理上下文(1740000001002).intent_hint` | 输出: @@ -876,29 +1210,34 @@ def main(normalized_query: str = "", intent_hint: str = "", sys_files=None): | `branch` | `direct_answer / inventory_path / agent_path` | | `final_reply` | 引导回复 | | `resolved_task_no` | 最终任务号 | +| `action_hint` | 当前任务动作提示;供续办链直接执行 | 代码: ```python -def main(route_key: str = "", normalized_query: str = "", detected_task_no: str = "", current_task_no: str = ""): +def main(route_key: str = "", normalized_query: str = "", detected_task_no: str = "", current_task_no: str = "", intent_hint: str = ""): rk = str(route_key or "").strip() + hint = str(intent_hint or "").strip() if rk == "inventory_upload": return { "branch": "inventory_path", "final_reply": "", "resolved_task_no": str(current_task_no or detected_task_no or "").strip().upper(), + "action_hint": "", } if rk == "notice_batch_upload": return { "branch": "direct_answer", "final_reply": "检测到发货通知附件。请在 Hxops 聊天区上传通知文件,由系统完成建批次、建任务和通知解析;完成后发送“继续处理”或直接发送 SN- 任务号。", "resolved_task_no": "", + "action_hint": "", } if rk == "direct_reply": return { "branch": "direct_answer", "final_reply": "请输入要继续处理的内容,或上传发货通知 / 库存文件。也可以直接发送 SN- 任务号。", "resolved_task_no": "", + "action_hint": "", } task = str(current_task_no or detected_task_no or "").strip().upper() if rk in {"continue_task", "task_switch"} and not task and rk == "continue_task": @@ -906,10 +1245,16 @@ def main(route_key: str = "", normalized_query: str = "", detected_task_no: str "branch": "direct_answer", "final_reply": "尚未识别到任务编号。请先通过 Hxops 上传发货通知创建任务,或在消息中直接带上 SN- 任务号。", "resolved_task_no": "", + "action_hint": "", } - return {"branch": "agent_path", "final_reply": "", "resolved_task_no": task} + return {"branch": "agent_path", "final_reply": "", "resolved_task_no": task, "action_hint": hint} ``` +说明: + +- `分支调度` 不能仅因消息里出现 `SN-...` 就把动作送入切任务链。 +- 当 `intent_hint` 是 `retry_rpa / confirm / reparse / reject / confirm_inventory_mapping / correct_inventory_mapping / reparse_inventory` 时,应视为**当前任务上的动作执行**,进入统一续办链,不得先调 `switch_focus_task`。 + ##### 节点 5:主路由分支节点(1740000001005) 类型:`If-Else / Branch` @@ -994,7 +1339,7 @@ def main(normalized_query: str = "", route_key: str = "", resolved_task_no: str ```text 左值:任务切换意图解析(1740000001006).switch_mode 运算符:is not -右值:none + 右值:none ``` true: @@ -1005,6 +1350,11 @@ false: - 进入 `准备读取任务状态(1740000001009)` +说明: + +- 本节点只对**明确切换任务**语义生效。 +- `retry_rpa`、`confirm`、`reparse`、`reject` 等当前任务动作,必须在这里返回 `false`,不能进入 `Tool 切换聚焦任务`。 + ##### 节点 8:Tool 切换聚焦任务(1740000001008) Tool 绑定: @@ -1019,12 +1369,14 @@ Tool 绑定: | 参数名 | 来源 | 是否必填 | 说明 | |------|------|------|------| | `batch_no` | `{{#conversation.current_batch_no#}}` | 是 | 当前批次号;用于在批次内切换焦点任务 | +| `task_no` | `{{#分支调度(1740000001004).resolved_task_no#}}` | 是 | 当前聚焦任务号;与 `batch_no` 共同定位当前焦点 | | `target_task_no` | `{{#1740000001006.target_task_no#}}` | 是 | 生产口径下应传明确 SN 任务号;不要依赖 `next / prev` 空参切换 | 补充说明: -- Dify 配置时只维护 `target_task_no` 即可,不需要再额外维护 `task_no`。 -- 后端会兼容把 `task_no` 当作同义别名读取,但这不是本手册的生产配置口径。 +- 生产配置必须同时传 `batch_no`、`task_no`、`target_task_no`。 +- 若缺少 `batch_no` 或 `task_no`,后端会返回 `batch_no and task_no are required`。 +- 因此,像“重试 RPA 导出库存”这类当前任务动作,不应误进入本节点。 输出: @@ -1149,6 +1501,7 @@ Tool 绑定: | `tool_checkpoint` | 后端 `checkpoint` | | `tool_data_json` | `data` JSON 字符串 | | `raw_json` | 全量 JSON 字符串 | +| `should_parse_notice` | 当前任务是否应进入首轮通知解析 | 代码: @@ -1163,12 +1516,25 @@ def main(text: str = ""): data = {} if not isinstance(data, dict): data = {} + payload = data.get("data") if isinstance(data.get("data"), dict) else {} + raw_text = str(payload.get("raw_text") or "").strip() + lines = payload.get("lines") if isinstance(payload.get("lines"), list) else [] + process_status = str(payload.get("process_status") or "").strip().lower() + current_step = str(payload.get("current_step") or "").strip() + has_lines = isinstance(lines, list) and len(lines) > 0 + should_parse_notice = bool( + raw_text + and not has_lines + and process_status in {"created", "pending_review", ""} + and current_step in {"", "step1_notice_submit"} + ) return { "tool_status": str(data.get("status") or "").strip(), "tool_message": str(data.get("message") or "").strip(), "tool_checkpoint": str(data.get("checkpoint") or "").strip(), - "tool_data_json": json.dumps(data.get("data") or {}, ensure_ascii=False), + "tool_data_json": json.dumps(payload, ensure_ascii=False), "raw_json": json.dumps(data, ensure_ascii=False), + "should_parse_notice": should_parse_notice, } ``` @@ -1176,6 +1542,8 @@ def main(text: str = ""): - 本节点只读取 `Tool 读取任务状态(1740000001010).text` - 不要写 `body` +- **首轮通知解析时 `tool_checkpoint` 允许为空**,这是正常现象;不能把 `tool_checkpoint` 当成“是否进入订单解析”的唯一条件 +- 正确口径应综合判断:`raw_text` 是否存在、`lines` 是否为空、`process_status` 是否为 `created/pending_review`、`current_step` 是否为空或仍停在提交通知阶段 ##### 节点 11A:是否待解析通知 @@ -1184,27 +1552,22 @@ def main(text: str = ""): 推荐条件配置(直接照填): ```text -条件组 1(推荐主条件): - 左值:工具结果标准化(1740000001011).tool_checkpoint - 运算符:in - 右值:created,parse_pending,parse_review - -条件组 2(可选增强条件): - 左值:工具结果标准化(1740000001011).tool_message - 运算符:contains - 右值:请确认解析结果 - -整体逻辑: - 条件组 1 OR 条件组 2 +条件组 1(唯一生产条件): + 左值:工具结果标准化(1740000001011).should_parse_notice + 运算符:is + 右值:true ``` 说明: -- 如果你的 Dify 版本不方便配置 `in`,就拆成 3 条 OR: - - `tool_checkpoint is created` - - `tool_checkpoint is parse_pending` - - `tool_checkpoint is parse_review` -- 不建议在 If-Else 里直接解析 `raw_json` 内部结构;稳定性不如直接判断 `tool_checkpoint` +- **不要**再按 `tool_checkpoint` 判断首轮通知解析,因为新任务在解析前很可能出现: + - `tool_checkpoint = ""` + - `process_status = created` + - `current_step = null` + - `raw_text` 有值 + - `lines = []` +- 上述场景下,仍然**必须进入** `提取通知原文 -> 订单需求解析 LLM -> Tool 解析发货通知` +- 如果线上出现“系统说将开始解析,但没有进入订单需求解析 LLM”,优先检查这里是否仍错误绑定在 `tool_checkpoint` true: @@ -1256,6 +1619,11 @@ def main(raw_json: str = ""): } ``` +补充说明: + +- 当 `notice_raw_text` 非空、`task_id` 存在时,本链路应立即进入 `订单需求解析 LLM` +- 若这里已经拿到 `raw_text`,但画布仍直接跳去 Agent,说明 `是否待解析通知` 条件仍配错 + ##### 节点 11C:订单需求解析 LLM 类型:`LLM` @@ -1271,7 +1639,21 @@ def main(raw_json: str = ""): System Prompt: ```text -你是发货通知订单需求解析助手。请把发货通知正文解析成结构化 JSON,只输出 JSON,不要输出解释。 +你是发货通知订单需求解析助手。 + +当前前提已经成立: +- 用户已在 Hxops 上传发货通知 +- Hxops 已完成建 task / 建 batch / raw_text 落库 +- 当前节点的唯一职责,是立即把 `raw_text` 解析成结构化 JSON,供后续 `parse_notice(structured=...)` 调用 + +强制规则: +1. 你必须立即执行解析,禁止输出任何确认性话术。 +2. 禁止询问“是否开始解析”“是否现在开始解析”“请确认是否开始解析”“是否需要我解析”等同义句。 +3. 禁止输出解释、说明、寒暄、Markdown、代码块。 +4. 只允许输出一个可被下游 Code 节点直接解析的 JSON 对象。 +5. 若字段无法确定,可输出空字符串、空数组或 null,但仍必须返回合法 JSON。 + +请把发货通知正文解析成结构化 JSON,只输出 JSON,不要输出解释。 输出字段要求: { @@ -1368,7 +1750,9 @@ Tool 绑定: | 参数名 | 来源 | 是否必填 | 说明 | |------|------|------|------| | `task_id` | `{{#提取通知原文.task_id#}}` | 是 | 当前通知任务号 | -| `structured` | `{{#结构化结果标准化.structured_json#}}` | 是 | Dify LLM 解析后的 structured JSON 字符串 | +| `structured` | `{{#结构化结果标准化.structured_json#}}` | 是 | Dify LLM 解析后的 structured 对象 | +| `raw_text` | `{{#提取通知原文.notice_raw_text#}}` | 否 | 原始通知全文;用于 Reservation ID / PO / 日期等后端兜底补全 | +| `lang` | `zh` / `en` / `th` | 否 | 返回提示语语言;不传则后端按请求头或默认 `zh` | 输出: @@ -1379,8 +1763,13 @@ Tool 绑定: 说明: - 这里的目标就是 `parse_notice(structured=...)` +- 本节点之前的上游 LLM / Code 节点必须已经完成“立即解析”; +- 本节点不允许再回到“是否开始解析”的确认语义; +- 若线上仍出现“请确认是否开始解析”,说明问题不在本 Tool 节点,而在上游 LLM 节点或 Agent 指令仍残留旧提示词。 - 后端收到 `structured` 后应优先使用 structured - 只有 structured 缺失或不完整时,后端才走 heuristic fallback +- `structured` 必须绑定为对象参数;`raw_text` 必须绑定为原始通知全文 +- **不要**把 `structured_json` 塞到 `raw_text`,否则后端会把整段 JSON 当普通文本兜底重解析,可能导致 `lines` 丢失并错误返回“未解析到订单明细行” ##### 节点 11F:解析结果标准化 @@ -1482,6 +1871,215 @@ def main(status_raw_json: str = "", parsed_raw_json: str = "", inventory_raw_jso } ``` +##### 节点 11H:当前任务动作判断 + +输入: + +| 变量名 | 来源 | +|------|------| +| `intent_hint` | `预处理上下文(1740000001002).intent_hint` | +| `resolved_task_no` | `分支调度(1740000001004).resolved_task_no` | +| `tool_checkpoint` | `工具结果标准化(1740000001011).tool_checkpoint` | + +输出: + +| 字段 | 说明 | +|------|------| +| `direct_action` | 可绕开 Agent 直接执行的动作名 | +| `need_agent` | `true / false` | + +代码: + +```python +def main(intent_hint: str = "", resolved_task_no: str = "", tool_checkpoint: str = ""): + hint = str(intent_hint or "").strip() + task_no = str(resolved_task_no or "").strip().upper() + checkpoint = str(tool_checkpoint or "").strip().lower() + + direct_actions = { + "retry_rpa", + "confirm", + "reject", + "reparse", + "confirm_inventory_mapping", + "correct_inventory_mapping", + "reparse_inventory", + "rematch", + } + + direct_action = "" + if hint == "confirm" and checkpoint == "align_review": + direct_action = "confirm_inventory_mapping" + elif hint in direct_actions: + direct_action = hint + + if direct_action and task_no: + return { + "direct_action": direct_action, + "need_agent": "false", + } + + return { + "direct_action": "", + "need_agent": "true", + } +``` + +说明: + +- 本节点的目标是**减少 Agent 调用时间**。 +- 对于已由上游代码节点明确识别的短动作,直接走 Tool 即可,不要再让 Agent 做第二轮判断。 +- 当前生产优先绕开 Agent 的动作包括:`retry_rpa / confirm / reject / reparse / confirm_inventory_mapping / correct_inventory_mapping / reparse_inventory / rematch`。 +- 当用户只输入 `确认` 且当前 `checkpoint=align_review` 时,应标准化为 `confirm_inventory_mapping`,优先进入库存字段对齐确认直连链,而不是误调普通 `confirm`。 + +##### 节点 11I:是否进入 Agent + +类型:`If-Else` + +条件配置(直接照填): + +```text +左值:当前任务动作判断.need_agent +运算符:is +右值:true +``` + +true: + +- 进入 `是否待解析通知` + +false: + +- 进入 `当前任务直连动作分支` + +说明: + +- 这个节点的目标是把**短动作**与**模糊自由输入**彻底分开。 +- `继续处理 / 为什么卡住 / 第二行改成 12` 这类消息仍走通知解析 / Agent 链。 +- `确认 / 驳回 / 重解析 / 重新匹配 / 重试 RPA` 这类确定性动作直接进入 Tool,不再经过 Agent。 + +##### 节点 11J:当前任务直连动作分支 + +类型:`If-Else / Branch` + +输入字段: + +```text +当前任务动作判断.direct_action +``` + +分支配置(直接照填): + +```text +retry_rpa ----------------------> Tool 重试库存导出 +confirm ------------------------> Tool 提交当前任务决策 +reject -------------------------> Tool 提交当前任务决策 +reparse ------------------------> Tool 提交当前任务决策 +confirm_inventory_mapping -----> Tool 提交当前任务决策 +correct_inventory_mapping -----> Tool 提交当前任务决策 +reparse_inventory -------------> Tool 提交当前任务决策 +rematch -----------------------> Tool 重新匹配库存 +其他 / 默认 --------------------> Agent 状态输入标准化 +``` + +说明: + +- 这里不要再把 `direct_action` 交给 Agent 解释。 +- 只要分支命中,就直接执行对应 Tool,并统一汇入 `直连动作结果标准化`。 + +##### 节点 11K-11M:当前任务直连 Tool 节点 + +以下节点建议统一从 `准备读取任务状态(1740000001009).task_id` 取 `task_id`,并在执行后全部汇入 `直连动作结果标准化`。 + +| 节点名 | 绑定 Tool | 参数映射 | 说明 | +|------|------|------|------| +| Tool 重试库存导出 | `ensure_inventory` | `task_id={{#1740000001009.task_id#}}`;`mode=retry_rpa` | 用户触发重试 RPA 导出库存时使用 | +| Tool 提交当前任务决策 | `submit_decision` | `task_id={{#1740000001009.task_id#}}`;`decision={{#当前任务动作判断.direct_action#}}` | 用同一个 Tool 节点统一承接 `confirm / reject / reparse / confirm_inventory_mapping / correct_inventory_mapping / reparse_inventory` | +| Tool 重新匹配库存 | `match_inventory` | `task_id={{#1740000001009.task_id#}}` | 在库存已就绪的前提下重新执行匹配 | + +说明: + +- 这里故意**不**把 `confirm_inventory_mapping` 绑定成 `auto_progress`。 +- 原因是“确认字段对齐”语义上属于**确认当前步骤**,更适合显式落到 `submit_decision(confirm_inventory_mapping)`;后续是否自动推进,交给后端状态机或后续 `继续` / Agent 决策处理。 +- 这样可以避免用户只是点击“确认映射”,却在画布层面被直接解释成“立刻推进后续全部动作”。 + +##### 节点 11S:直连动作结果标准化 + +输入: + +| 变量名 | 来源 | +|------|------| +| `retry_rpa_text` | `Tool 重试库存导出.text` | +| `submit_decision_text` | `Tool 提交当前任务决策.text` | +| `rematch_text` | `Tool 重新匹配库存.text` | + +输出: + +| 字段 | 说明 | +|------|------| +| `tool_status` | Tool 状态 | +| `tool_message` | Tool message | +| `tool_checkpoint` | Tool checkpoint | +| `raw_json` | 全量 JSON | +| `answer_text` | 给 Answer 节点直接回复的文本 | + +代码: + +```python +import json + +def _pick_first(*values): + for value in values: + raw = str(value or "").strip() + if raw: + return raw + return "" + +def main( + retry_rpa_text: str = "", + submit_decision_text: str = "", + rematch_text: str = "", +): + raw = _pick_first( + retry_rpa_text, + submit_decision_text, + rematch_text, + ) + try: + data = json.loads(raw) if raw else {} + except json.JSONDecodeError: + data = {} + if not isinstance(data, dict): + data = {} + + answer_text = str(data.get("message") or "").strip() + if not answer_text: + answer_text = "操作已提交,请继续根据当前任务提示处理。" + + return { + "tool_status": str(data.get("status") or "").strip(), + "tool_message": str(data.get("message") or "").strip(), + "tool_checkpoint": str(data.get("checkpoint") or "").strip(), + "raw_json": json.dumps(data, ensure_ascii=False), + "answer_text": answer_text, + } +``` + +说明: + +- 所有直连 Tool 都应汇入本节点,避免为每个短动作单独配一个 Answer 节点。 +- 本节点只做**统一收口**,不负责下一步智能判断。 + +##### 节点 11T:直接回复(动作结果) + +类型:`Answer` + +内容: + +```text +{{#直连动作结果标准化.answer_text#}} +``` + ##### 节点 12:库存任务检查 输入: @@ -1887,7 +2485,22 @@ Tool 切换聚焦任务(1740000001008) -> 切换结果标准化(自动生 准备读取任务状态(1740000001009) -> Tool 读取任务状态(1740000001010) Tool 读取任务状态(1740000001010) -> 工具结果标准化(1740000001011) 货代续办 Agent(1740000001012) -> 直接回复(1740000001013) -工具结果标准化(1740000001011) -> 是否待解析通知 +工具结果标准化(1740000001011) -> 当前任务动作判断 +当前任务动作判断 -> 是否进入 Agent +是否进入 Agent(true) -> 是否待解析通知 +是否进入 Agent(false) -> 当前任务直连动作分支 +当前任务直连动作分支(retry_rpa) -> Tool 重试库存导出 +当前任务直连动作分支(confirm) -> Tool 提交当前任务决策 +当前任务直连动作分支(reject) -> Tool 提交当前任务决策 +当前任务直连动作分支(reparse) -> Tool 提交当前任务决策 +当前任务直连动作分支(confirm_inventory_mapping) -> Tool 提交当前任务决策 +当前任务直连动作分支(correct_inventory_mapping) -> Tool 提交当前任务决策 +当前任务直连动作分支(reparse_inventory) -> Tool 提交当前任务决策 +当前任务直连动作分支(rematch) -> Tool 重新匹配库存 +Tool 重试库存导出 -> 直连动作结果标准化 +Tool 提交当前任务决策 -> 直连动作结果标准化 +Tool 重新匹配库存 -> 直连动作结果标准化 +直连动作结果标准化 -> 直接回复(动作结果) 是否待解析通知(false) -> Agent 状态输入标准化 是否待解析通知(true) -> 提取通知原文 提取通知原文 -> 订单需求解析 LLM @@ -1977,14 +2590,16 @@ Agent 状态输入标准化 -> 货代续办 Agent(1740000001012) 4. `任务切换意图解析(1740000001006)` 5. `准备读取任务状态(1740000001009)` 6. `工具结果标准化(1740000001011)` -7. `提取通知原文` -8. `结构化结果标准化` -9. `解析结果标准化` -10. `Agent 状态输入标准化` -11. `库存任务检查` -12. `库存 schema 标准化` -13. `列映射 JSON 标准化` -14. `映射结果标准化` +7. `当前任务动作判断` +8. `提取通知原文` +9. `结构化结果标准化` +10. `解析结果标准化` +11. `直连动作结果标准化` +12. `Agent 状态输入标准化` +13. `库存任务检查` +14. `库存 schema 标准化` +15. `列映射 JSON 标准化` +16. `映射结果标准化` 原因很简单:Code 节点定义了后面 Tool / If / Agent 要引用的变量名,先把字段骨架定下来,后面就不容易串错。 @@ -2012,6 +2627,9 @@ Agent 状态输入标准化 -> 货代续办 Agent(1740000001012) 3. `Tool 解析发货通知` 4. `Tool 读取库存 schema 预览` 5. `Tool 应用库存列映射` +6. `Tool 重试库存导出` +7. `Tool 提交当前任务决策` +8. `Tool 重新匹配库存` 这一步重点检查两件事: @@ -2024,9 +2642,11 @@ Agent 状态输入标准化 -> 货代续办 Agent(1740000001012) 1. `主路由分支节点(1740000001005)` 2. `是否需要切换任务(1740000001007)` -3. `是否待解析通知` -4. `是否已有库存绑定任务` -5. `库存列映射是否成功` +3. `是否进入 Agent` +4. `当前任务直连动作分支` +5. `是否待解析通知` +6. `是否已有库存绑定任务` +7. `库存列映射是否成功` 这一轮只做条件,不要同时改节点代码。 @@ -2039,8 +2659,9 @@ Agent 状态输入标准化 -> 货代续办 Agent(1740000001012) - 取消不该绑定的 Tool 2. `直接回复(1740000001013)` 3. `直接回复(引导)(1740000001014)` -4. `无TASK_ID提示` -5. `列映射失败回复` +4. `直接回复(动作结果)` +5. `无TASK_ID提示` +6. `列映射失败回复` Agent 一定放到最后配,因为它依赖前面所有节点的变量名、Tool 名和状态 JSON 口径。 @@ -2072,19 +2693,25 @@ Agent 一定放到最后配,因为它依赖前面所有节点的变量名、To 1. `Tool 切换聚焦任务.text -> 切换结果标准化.text` 2. `切换结果标准化.switched_task_no -> 准备读取任务状态.switched_task_no` 3. `准备读取任务状态.task_id -> Tool 读取任务状态.task_id` -4. `提取通知原文.task_id -> Tool 解析发货通知.task_id` -5. `结构化结果标准化.structured_json -> Tool 解析发货通知.structured` -6. `库存任务检查.task_id -> Tool 读取库存 schema 预览.task_id` -7. `列映射 JSON 标准化.column_mapping_json -> Tool 应用库存列映射.column_mapping_json` -8. `Agent 状态输入标准化.agent_raw_json -> Agent Query` -9. `Agent 状态输入标准化.agent_task_id -> Agent Query` +4. `当前任务动作判断.need_agent -> 是否进入 Agent` +5. `当前任务动作判断.direct_action -> 当前任务直连动作分支` +6. `准备读取任务状态.task_id -> 各直连动作 Tool 节点.task_id` +7. `各直连动作 Tool 节点.text -> 直连动作结果标准化` +8. `提取通知原文.task_id -> Tool 解析发货通知.task_id` +9. `结构化结果标准化.structured_json -> Tool 解析发货通知.structured` +10. `库存任务检查.task_id -> Tool 读取库存 schema 预览.task_id` +11. `列映射 JSON 标准化.column_mapping_json -> Tool 应用库存列映射.column_mapping_json` +12. `Agent 状态输入标准化.agent_raw_json -> Agent Query` +13. `Agent 状态输入标准化.agent_task_id -> Agent Query` ##### 第 3 轮:再检查条件节点 1. `branch` 两个分流条件没写反 2. `switch_mode is not none` 没写成 `is` -3. `是否待解析通知` 的 OR 条件完整 -4. `has_task` / `mapping_ok` 都是判断 `true` +3. `是否进入 Agent` 的 `true / false` 去向没写反 +4. `当前任务直连动作分支` 已覆盖所有短动作 +5. `是否待解析通知` 的 OR 条件完整 +6. `has_task` / `mapping_ok` 都是判断 `true` ##### 第 4 轮:再检查 Agent 绑定 @@ -2149,17 +2776,22 @@ Agent 一定放到最后配,因为它依赖前面所有节点的变量名、To - [ ] `任务切换意图解析(1740000001006)` 代码正确 - [ ] `准备读取任务状态(1740000001009)` 代码正确 - [ ] `工具结果标准化(1740000001011)` 代码正确 +- [ ] `当前任务动作判断` 代码正确 - [ ] `提取通知原文` 代码正确 - [ ] `订单需求解析 LLM` Prompt 正确 - [ ] `结构化结果标准化` 代码正确 - [ ] `Tool 解析发货通知` 已绑定 `parse_notice` - [ ] `解析结果标准化` 代码正确 +- [ ] `直连动作结果标准化` 代码正确 - [ ] `Agent 状态输入标准化` 代码正确 - [ ] `库存任务检查` 代码正确 - [ ] `库存 schema 标准化` 代码正确 - [ ] `库存列映射 LLM` Prompt 正确 - [ ] `列映射 JSON 标准化` 代码正确 - [ ] `映射结果标准化` 代码正确 +- [ ] `是否进入 Agent` 与 `当前任务直连动作分支` 条件正确 +- [ ] `Tool 提交当前任务决策` 已绑定 `decision={{direct_action}}` +- [ ] `Tool 重试库存导出 / Tool 重新匹配库存` 已绑定正确 Tool 与参数 - [ ] Agent 已绑定必开 Tool - [ ] Agent 未绑定 `parse_notice / create_task / create_batch / apply_inventory_mapping` @@ -2168,6 +2800,7 @@ Agent 一定放到最后配,因为它依赖前面所有节点的变量名、To - [ ] 用户发 `继续处理` 时能进入 `get_task_status` - [ ] 用户发 `SN-...` 时能读取对应任务状态 - [ ] 用户发 `切到 SN-...` 时能先切任务再读状态 +- [ ] 用户发 `确认 / 驳回 / 重解析 / 重新匹配 / 重试 RPA` 时优先走直连 Tool,不进入 Agent - [ ] 用户上传 `.xlsx/.xls/.csv` 时能进入库存 schema / 列映射 / apply 链 - [ ] `apply_inventory_mapping` 返回 `align_review` 时,Agent 会停住等待确认 - [ ] `match_inventory` 成功后,Agent 同轮继续 `auto_progress` @@ -2185,7 +2818,7 @@ Agent 一定放到最后配,因为它依赖前面所有节点的变量名、To 5. 上传一个库存 `.xlsx` - 预期:进入 `get_inventory_schema_preview -> 库存列映射 LLM -> apply_inventory_mapping` 6. 在字段对齐确认状态下输入 `确认` - - 预期:Agent 调 `submit_decision(confirm_inventory_mapping)` 或 `auto_progress` + - 预期:不进入 Agent,直接走 `Tool 提交当前任务决策(decision=confirm_inventory_mapping)` 7. 在库存待匹配状态下输入 `开始匹配` - 预期:Agent 调 `match_inventory`,成功后继续 `auto_progress` 8. 在可上传货代状态下输入 `继续` @@ -2240,16 +2873,17 @@ Agent 一定放到最后配,因为它依赖前面所有节点的变量名、To 强约束: 1. 当前会话可能有 batch_no 和 current_task_no,一次只处理一个当前 task 2. 不能自行创建业务结论,必须以后端返回为准 -3. Dify 完成 `parse_notice(structured=...)` 后,要根据 checkpoint 判断是否等待确认 -4. 画布库存链完成列映射后,如果返回 needs_human,必须等待用户确认字段映射 -5. 当当前处于 align_review,且用户回复“确认”时,先调用 submit_decision(confirm_inventory_mapping) -6. 只有 match_inventory 返回 status=ok 后,才在同一轮继续调用 auto_progress(task_id=...) -7. shortage 存在时,不得自动替用户确认继续 -8. upload_to_freight 返回 running 或 needs_human 时,不得口头宣布上传成功 -9. 当用户只回复“确认/驳回/重试”等短语时,应优先考虑调用 submit_decision -10. 若用户说“下一票”“切到第 2 票”“切到 SN-...”时,应先切换聚焦任务再继续 -11. 若用户在 parse_review 阶段要求修改客户、PO、交期、料号、预订单号、数量,优先调用 update_notice_fields 或 update_notice_line -12. 不要接管首层分流职责;通知上传建单由 Hxops 前端负责,订单需求解析与 `parse_notice(structured=...)` 由画布负责,库存 Excel 上传后的列映射由画布 **Tool 节点**负责 +3. 当 Hxops 已上传通知并触发首轮续办时,必须立即进入通知解析;禁止再次询问“是否开始解析”“是否现在开始解析”“请确认是否开始解析” +4. Dify 完成 `parse_notice(structured=...)` 后,要根据 checkpoint 判断是否等待确认 +5. 画布库存链完成列映射后,如果返回 needs_human,必须等待用户确认字段映射 +6. 当当前处于 align_review,且用户回复“确认”时,先调用 submit_decision(confirm_inventory_mapping) +7. 只有 match_inventory 返回 status=ok 后,才在同一轮继续调用 auto_progress(task_id=...) +8. shortage 存在时,不得自动替用户确认继续 +9. upload_to_freight 返回 running 或 needs_human 时,不得口头宣布上传成功 +10. 当用户只回复“确认/驳回/重试”等短语时,应优先考虑调用 submit_decision +11. 若用户说“下一票”“切到第 2 票”“切到 SN-...”时,应先切换聚焦任务再继续 +12. 若用户在 parse_review 阶段要求修改客户、PO、交期、料号、预订单号、数量,优先调用 update_notice_fields 或 update_notice_line +13. 不要接管首层分流职责;通知上传建单由 Hxops 前端负责,订单需求解析与 `parse_notice(structured=...)` 由画布负责,库存 Excel 上传后的列映射由画布 **Tool 节点**负责 输出要求: - 优先回显 task_id;有 batch 时也回显 batch 上下文 @@ -2275,6 +2909,8 @@ Agent 一定放到最后配,因为它依赖前面所有节点的变量名、To 发货通知已由 **Hxops 聊天区上传并保存 raw_text**;随后由画布 LLM 节点解析订单需求,并调用 `parse_notice(structured=...)` 落库。你收到 `parse_msg`(或 `get_task_status.message`)后**原文展示**并引导确认。库存 Excel 由用户在 Hxops 上传后,由画布 **Tool 节点**完成列映射;Agent **禁止**自行推断 `map_json`,也不要脱离既定解析节点重复执行 `parse_notice`。 若用户用自然语言要求“改采购单号 / 改预订单号 / 改料号 / 改数量 / 新增一行 / 删除一行”,应优先调用 `update_notice_fields`、`update_notice_line`、`add_notice_line`、`remove_notice_line` 持久化修改,禁止只在回复里口头改写解析结果。 +当 Hxops 已上传通知并自动发起首轮续办时,你**不得**再询问用户“是否开始解析”“是否现在开始解析”“请确认是否开始解析”。首轮续办的正确行为是:立即读取 `raw_text` → 由画布 LLM 节点抽取 structured → 调用 `parse_notice(structured=...)` → 再根据返回结果决定是否进入人工确认。只有当 `parse_notice` 或后端状态机明确返回 `needs_human` / `stop_agent_turn=true` 时,才允许进入确认语义。 + 首层附件分流由 Chatflow 的 Code 节点完成(通知/库存附件 → 引导回 Hxops 上传)。你负责第二层业务理解与 Tool 调用,**不要接管首层分流职责**。 #### 主流程 @@ -2332,6 +2968,19 @@ Agent 一定放到最后配,因为它依赖前面所有节点的变量名、To - `parse_msg`、`align_msg`、`match_inventory.message`:**完整原文输出**,禁止二次摘要、禁止自编 Markdown 表格、禁止写「待确认 / 待从任务中获取」。 - **禁止**在未成功调用 `match_inventory` 前声称「已匹配」或生成 Excel。 +##### 3A. 首轮通知解析禁止二次确认 + +- 当当前轮次的前提是“Hxops 已上传通知并自动续办”时,Agent **禁止**再问: + - 是否开始解析 + - 是否现在开始解析 + - 请确认是否开始解析 + - 是否需要我立即解析 +- 这类问句一旦出现,说明 Agent 指令或上游 LLM 提示词仍残留旧逻辑,属于配置错误。 +- 正确行为只能是: + 1. 立即进入 `raw_text -> structured -> parse_notice(structured=...)` + 2. 解析完成后,展示 `parse_msg` 或解析复核结果 + 3. 若返回 `needs_human`,再等待用户确认 / 修订 / 驳回 + ##### 4. 字段对齐硬门禁(align_review) 当 Tool `apply_inventory_mapping` 返回 `status=needs_human`、`checkpoint=align_review`、`stop_agent_turn=true` 时: @@ -2358,6 +3007,40 @@ Agent 一定放到最后配,因为它依赖前面所有节点的变量名、To `auto_progress` 自动:确认字段对齐(无 warnings)→ 匹配 → 生成 Excel →(可选)RPA 上传。遇 blockers(warnings、缺预订单号列、库存不足等)返回 `needs_human`。 +#### 2.7.2 自动解析排查(若线上仍出现“是否开始解析”) + +如果用户已经在 Hxops 上传了通知,聊天区仍出现: + +- “是否开始解析” +- “是否现在开始解析” +- “请确认是否开始解析” + +优先检查以下 Dify 配置,且按顺序排查: + +1. **订单需求解析 LLM 节点 Prompt** + - 是否仍保留“先确认再解析”的旧文案 + - 是否严格要求“只输出 JSON” + +2. **货代续办 Agent 指令** + - 是否已替换为本文 **§2.7.1 完整版** + - 是否仍残留“解析前先询问用户”的旧规则 + +3. **上传后自动续办消息的实际落点** + - 是否真的进入了“待解析通知链” + - 是否被错误路由到了普通问答分支 + +4. **是否待解析通知 If-Else 节点** + - 是否把当前 task 误判成“不需要解析” + - 是否导致后续直接进入 Agent,自然语言兜圈子 + +5. **Tool 解析发货通知 前的 structured 标准化** + - 若 `parse_ok=false`,是否被错误地交给 Agent 自由回答,而不是进入明确失败分支 + +结论标准只有一个: + +- 正确:上传后自动续办 → `raw_text` 解析 → `parse_notice(structured=...)` → 直接展示解析结果 / 解析复核 +- 错误:上传后自动续办 → Agent 再问“是否开始解析” + - **硬性规则 1**:`match_inventory` 返回 `status=ok` 后,**禁止**停在 `matched` 状态结束本轮;必须在**同一轮**继续调用 `auto_progress(task_id=...)`。 - **硬性规则 2**:只有当 `match_inventory` 返回 `status!=ok`(如 `needs_human` / `failed`)时,才不得继续调用 `auto_progress`。 - **硬性规则 3**:`auto_progress` 返回 `ok` / `running` / `needs_human` 都属于正确收口;**禁止**在 `match_inventory` 成功后仅回复「待生成发货 Excel」就结束。