refactor(agent-runtime, shipping-notice, frontend): 重构上下文体系,修复重试RPA无响应问题

- 替换原有的hxops_context模块为全新的代理运行时上下文平台协议,统一跨业务上下文注入逻辑
- 新增多语言操作繁忙提示文案(覆盖中、繁、英、泰四语),前端绑定页面繁忙状态实现操作阻断
- 调整RPA库存导出重试策略:移除可配置的最大自动重试次数,改为固定单次自动重试,手动重试需用户显式触发
- 优化任务用户态状态展示逻辑,优先展示具体的pending原因而非通用状态文案
- 重构任务焦点切换逻辑,统一事件日志与处理流程,复用switch_batch_focus工具函数
- 修复clearAllHistory函数残留UI问题,确保清空会话后完全复位发货通知相关视图
- 更新OpenAPI文档与静态快照,补充工具使用约束说明
- 新增调试打点日志与问题排查文档,完善全套单元测试与快照校验用例
- 更新RPA脚本的README文档,调整登录方式说明与浏览器 profile 路径配置
- 删除废弃的hxops_context模块,迁移所有相关调用到新的上下文体系
This commit is contained in:
hugo committed 2026-07-03 17:01:42 +08:00
1 parent 60dd0c0f58
commit 8846ce159f
102 files changed
+18289 -9072

No files matched your search

+109
View File
@@ -0,0 +1,109 @@
# Dify OpenAPI Snapshots
> 文件:`docs/openapi/README.md`
> 用途:说明 Dify 导入用静态 OpenAPI 快照的生成、校验和刷新时机。
> 依赖:`scripts/export_openapi_snapshots.py`、`backend/app/services/shipping_notice/openapi_spec.py`、`backend/app/services/agent_runtime_context/openapi_spec.py`。
> 逻辑关系:后端 builder 是事实源,`docs/openapi/*.json` 是供 Dify 管理员导入的静态产物;修改 builder 或白名单后,需要重新导出快照。
## 1. 目录说明
当前 Dify 相关静态 OpenAPI 快照包括:
- `docs/openapi/agent_runtime_context_tools.dify.json`
- `docs/openapi/shipping_notice_tools.dify.json`
它们都由后端 builder 直接生成,不建议手工编辑。
## 2. 如何生成
在仓库根目录执行:
```bash
python scripts/export_openapi_snapshots.py
```
默认行为:
- 读取默认 base URL:`http://192.168.36.108:5052`
- 生成并覆盖 `docs/openapi` 下的静态 JSON 快照
也可以显式指定地址:
```bash
python scripts/export_openapi_snapshots.py --base-url http://localhost:5052
```
还可以通过环境变量覆盖:
```bash
HXOPS_OPENAPI_SNAPSHOT_BASE_URL=http://localhost:5052
python scripts/export_openapi_snapshots.py
```
## 3. 如何检查
在 CI 或本地校验快照是否最新:
```bash
python scripts/export_openapi_snapshots.py --check
```
行为说明:
- 若快照与 builder 输出一致,返回 0
- 若快照过期或缺失,返回非 0,并打印需要刷新的文件列表
## 4. 什么时候需要刷新
以下情况需要重新导出:
- 修改 `backend/app/services/agent_runtime_context/openapi_spec.py`
- 修改 `backend/app/services/shipping_notice/openapi_spec.py`
- 修改 `backend/app/services/agent_runtime_context/parser.py` 白名单路径
- 修改 Tool description
- 修改 Dify 导入所依赖的字段约束或 Prompt 契约关键词
## 5. Dify 应导入哪些文件
当前推荐导入:
- `docs/openapi/agent_runtime_context_tools.dify.json`
- `docs/openapi/shipping_notice_tools.dify.json`
用途分工:
- `agent_runtime_context_tools.dify.json`
- 提供只读 parse tool
- 用于解析 `reference_context`、`refs`、`domain.state_hint`
- `shipping_notice_tools.dify.json`
- 提供发货通知领域 Tool
- 用于状态查询、人工决策、字段修订、库存映射确认
## 6. 一致性保障
当前仓库已提供以下保障:
- `scripts/export_openapi_snapshots.py`
- 统一生成与 `--check` 校验逻辑
- `backend/tests/test_openapi_snapshots.py`
- 校验静态 JSON 与 builder 输出一致
- `backend/tests/test_agent_runtime_context_tools.py`
- 锁定 `reference_context`、`多候选`、`追问`、`状态机` 等关键词
- `backend/tests/test_shipping_notice_tools.py`
- 锁定 `唯一 task_id`、`不得自行宣布完成`、`不要在自然语言正文列出按钮清单` 等约束
## 7. 维护建议
推荐维护流程:
1. 修改后端 OpenAPI builder 或 parser 白名单
2. 执行 `python scripts/export_openapi_snapshots.py`
3. 执行 `python scripts/export_openapi_snapshots.py --check`
4. 运行相关测试
5. 将快照文件与代码改动一起提交
不建议:
- 手工编辑 `docs/openapi/*.json`
- 只改动态 builder,不刷新静态文件
- 只刷新静态文件,不更新测试或说明
@@ -0,0 +1,118 @@
{
"openapi": "3.0.0",
"info": {
"title": "HXOPS Agent Runtime Context Tools",
"version": "1.0.0",
"description": "HXOPS 平台运行态上下文只读解析工具。Dify 应在需要读取 session、task、batch、run purpose、state_hint 或模糊指代线索时调用本工具;遇到“继续”“刚才那个”“上一个失败的”等表达时,应优先读取 reference_context。若只需要 focus id、last action 或 failure reason,可优先读取 reference_context 的细粒度字段,减少上下文判断分支。若 reference_context 返回多个 candidates(多候选),不要猜测,应追问用户或调用业务 Tool 再确认。reference_context 与 state_hint 都只是提示,不是业务事实源,也不能替代 HXOPS 状态机。推进业务状态必须调用对应业务 Tool,不能依据上下文直接宣布完成、上传成功或任务关闭。鉴权:Authorization Bearer 或 X-Integration-Key。"
},
"servers": [
{
"url": "http://192.168.36.108:5052/api/agent-runtime-context"
}
],
"paths": {
"/tools/parse": {
"post": {
"operationId": "parse_agent_runtime_context",
"summary": "解析 HXOPS agent_runtime_context 中的白名单字段",
"description": "只读解析工具;agent_runtime_context 应为 Dify Start 节点中的 JSON 字符串。paths 最多 20 个,且必须属于 allowed_paths。推荐优先读取 reference_context.focus_object.id、reference_context.last_action_target.action、reference_context.last_failed_target.reason 等细粒度字段,降低提示词中的对象拆解成本。模糊指代建议优先读取 reference_context;若拿到多个 candidates(多候选),必须追问或回到业务 Tool 校验,禁止自行猜测推进。reference_context/state_hint 仅用于理解当前现场,不能替代状态机事实,更不能据此直接推进业务或宣布完成。",
"parameters": [],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"agent_runtime_context",
"paths"
],
"properties": {
"agent_runtime_context": {
"type": "string",
"description": "Dify Start 变量 agent_runtime_context 的 JSON 字符串。其中 reference_context 仅用于辅助理解当前现场,不可替代 HXOPS 业务事实源。"
},
"paths": {
"type": "array",
"description": "需要读取的字段路径。",
"maxItems": 20,
"items": {
"type": "string",
"enum": [
"agent_code",
"context_id",
"domain.state_hint.batch_no",
"domain.state_hint.current_step",
"domain.state_hint.focus_task_no",
"domain.state_hint.match_status",
"domain.state_hint.pending_human_reason",
"domain.state_hint.review_status",
"domain.state_hint.status",
"domain.state_hint.upload_status",
"domain.summary",
"domain.type",
"platform.locale",
"platform.parent_session_id",
"platform.request_id",
"platform.session_id",
"platform.trigger_source",
"reference_context.candidates",
"reference_context.focus_object",
"reference_context.focus_object.id",
"reference_context.focus_object.type",
"reference_context.last_action_target",
"reference_context.last_action_target.action",
"reference_context.last_action_target.id",
"reference_context.last_failed_target",
"reference_context.last_failed_target.id",
"reference_context.last_failed_target.reason",
"reference_context.recent_activity_summary",
"refs.batch_no",
"refs.file_name",
"refs.run_purpose",
"refs.schedule_task_id",
"refs.task_no",
"schema_version",
"scope"
]
}
}
}
}
}
}
},
"responses": {
"200": {
"description": "Parse response",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"status": {
"type": "string",
"enum": [
"ok"
]
},
"data": {
"type": "object",
"additionalProperties": true
},
"missing": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
}
}
}
}
}
}
}
}
+6 -6
View File
@@ -3,7 +3,7 @@
"info": {
"title": "Shipping Notice Tools",
"version": "1.0.0",
"description": "货代发货通知 Agent Tool API。仅导出适合 Dify AI 编排层使用的工具:通知理解、字段修订、库存映射建议、人工确认收口与状态查询;不包含由后台任务统一续跑的主推进工具,也不包含 RPA 回调。如果响应包含 action_buttons,Dify 只解释当前状态、业务原因和影响,不要在自然语言正文里列出按钮清单或“推荐下一步”动作列表;可点击按钮由宿主系统根据 action_buttons 统一渲染。鉴权:Authorization Bearer 或 X-Integration-Key。"
"description": "货代发货通知 Agent Tool API。仅导出适合 Dify AI 编排层使用的工具:通知理解、字段修订、库存映射建议、人工确认收口与状态查询;不包含由后台任务统一续跑的主推进工具,也不包含 RPA 回调。推进类 Tool 的统一约束如下:必须依赖 HXOPS 状态机校验,必须基于唯一 task_id 推进;模糊指代不能直接推进,必须先通过 reference_context、get_task_status 或其他 HXOPS Tool 解析到唯一任务。submit_decision、match_inventory、generate_shipment_excel、upload_to_freight、retry_task、confirm_inventory_source 这类推进动作都必须根据 HXOPS Tool 返回结果解释,不能脱离 Tool 返回自行编造状态。若 Tool 返回 needs_human 或 failed,不得自行改写成完成、成功上传或已处理完毕。如果响应包含 action_buttons,Dify 只解释当前状态、业务原因和影响,不要在自然语言正文列出按钮清单或“推荐下一步”动作列表;可点击按钮由宿主系统根据 action_buttons 统一渲染。鉴权:Authorization Bearer 或 X-Integration-Key。"
},
"servers": [
{
@@ -266,8 +266,8 @@
"/tools/get_inventory_schema_preview": {
"post": {
"operationId": "get_inventory_schema_preview",
"summary": "读取已上传批次库存快照的表头与样例行(小 payload,供 LLM 推断列映射)",
"description": "读取已上传批次库存快照的表头与样例行(小 payload,供 LLM 推断列映射)",
"summary": "读取已上传库存 Excel 的表头与样例行(小 payload,供 LLM 推断列映射)",
"description": "读取已上传库存 Excel 的表头与样例行(小 payload,供 LLM 推断列映射)",
"parameters": [],
"requestBody": {
"required": true,
@@ -305,8 +305,8 @@
"/tools/apply_inventory_mapping": {
"post": {
"operationId": "apply_inventory_mapping",
"summary": "应用 LLM/人工列映射并解析完整批次库存快照;返回 field_alignment 等待用户确认,后续本批各票由后台任务独立续跑",
"description": "应用 LLM/人工列映射并解析完整批次库存快照;返回 field_alignment 等待用户确认,后续本批各票由后台任务独立续跑",
"summary": "应用 LLM/人工列映射并解析完整库存文件;返回 field_alignment 等待用户确认,后续主流程由后台任务续跑",
"description": "应用 LLM/人工列映射并解析完整库存文件;返回 field_alignment 等待用户确认,后续主流程由后台任务续跑",
"parameters": [],
"requestBody": {
"required": true,
@@ -350,7 +350,7 @@
"post": {
"operationId": "submit_decision",
"summary": "人工复核决策。task_id 与 conversation_id 至少传一个;用户确认时禁止 create_task。决策提交后由后台任务统一续跑。",
"description": "人工复核决策。task_id 与 conversation_id 至少传一个;用户确认时禁止 create_task。决策提交后由后台任务统一续跑。",
"description": "人工复核决策入口。必须先定位到唯一 task_id 才能推进;若 conversation_id 对应多个任务,必须先追问或调用状态查询工具确认,禁止用模糊指代直接推进。本 Tool 的返回结果以 HXOPS 状态机为准,Dify 必须根据实际返回的 status、checkpoint、forbidden_next_tools、human_options 或 action_buttons 解释当前现场。若返回 needs_human 或 failed,不得自行宣布完成、不得改写为成功,也不要在正文列出按钮清单;按钮由 HXOPS 前端渲染。",
"parameters": [],
"requestBody": {
"required": true,