- 替换原有的hxops_context模块为全新的代理运行时上下文平台协议,统一跨业务上下文注入逻辑 - 新增多语言操作繁忙提示文案(覆盖中、繁、英、泰四语),前端绑定页面繁忙状态实现操作阻断 - 调整RPA库存导出重试策略:移除可配置的最大自动重试次数,改为固定单次自动重试,手动重试需用户显式触发 - 优化任务用户态状态展示逻辑,优先展示具体的pending原因而非通用状态文案 - 重构任务焦点切换逻辑,统一事件日志与处理流程,复用switch_batch_focus工具函数 - 修复clearAllHistory函数残留UI问题,确保清空会话后完全复位发货通知相关视图 - 更新OpenAPI文档与静态快照,补充工具使用约束说明 - 新增调试打点日志与问题排查文档,完善全套单元测试与快照校验用例 - 更新RPA脚本的README文档,调整登录方式说明与浏览器 profile 路径配置 - 删除废弃的hxops_context模块,迁移所有相关调用到新的上下文体系
180 lines
6.6 KiB
Python
180 lines
6.6 KiB
Python
"""
|
|
backend/tests/test_agent_runtime_context_tools.py
|
|
|
|
用途:验证 agent_runtime_context Tool API 的鉴权、白名单解析和 OpenAPI 文档。
|
|
依赖:Flask 测试 client、agent_runtime_context.builder。
|
|
逻辑:模拟 Dify 使用 X-Integration-Key 调用 parse 工具,确保只能读取允许字段且不会执行任意路径。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
from app.services.agent_runtime_context.builder import build_agent_runtime_context, serialize_for_dify
|
|
|
|
|
|
def _headers(app) -> dict[str, str]:
|
|
"""
|
|
构建 Tool API 测试鉴权 Header。
|
|
|
|
核心逻辑:复用平台 INTEGRATION_API_KEY,贴近 Dify OpenAPI Tool 的实际调用方式。
|
|
|
|
Args:
|
|
app: Flask 测试应用。
|
|
|
|
Returns:
|
|
请求 Header 字典。
|
|
"""
|
|
app.config["INTEGRATION_API_KEY"] = "test-integration-key"
|
|
return {
|
|
"Content-Type": "application/json",
|
|
"X-Integration-Key": "test-integration-key",
|
|
}
|
|
|
|
|
|
def _serialized_context() -> str:
|
|
"""
|
|
构建测试用 agent_runtime_context JSON 字符串。
|
|
|
|
核心逻辑:覆盖 platform、refs、domain.state_hint 三类常用路径。
|
|
|
|
Returns:
|
|
JSON 字符串。
|
|
"""
|
|
context = build_agent_runtime_context(
|
|
agent_code="shipping_notice",
|
|
scope="task_run",
|
|
session_id="conv-parse-001",
|
|
request_id="req-parse-001",
|
|
trigger_source="backend_orchestrator",
|
|
refs={
|
|
"task_no": "SN-PARSE-001",
|
|
"batch_no": "SB-PARSE-001",
|
|
"run_purpose": "parse_notice",
|
|
},
|
|
domain={
|
|
"type": "shipping_notice",
|
|
"summary": "当前任务 SN-PARSE-001 属于批次 SB-PARSE-001。",
|
|
"state_hint": {
|
|
"focus_task_no": "SN-PARSE-001",
|
|
"status": "created",
|
|
},
|
|
},
|
|
reference_context={
|
|
"recent_activity_summary": "用户已确认 SN-PARSE-000 解析;当前 focus 为 SN-PARSE-001。",
|
|
"focus_object": {"type": "task", "id": "SN-PARSE-001"},
|
|
"last_action_target": {"type": "task", "id": "SN-PARSE-000", "action": "confirm_parse"},
|
|
"last_failed_target": {"type": "task", "id": "SN-PARSE-002", "reason": "shortage"},
|
|
"candidates": [{"type": "task", "id": "SN-PARSE-002", "label": "异常:库存不足"}],
|
|
},
|
|
)
|
|
return serialize_for_dify(context)
|
|
|
|
|
|
def test_parse_agent_runtime_context_tool_returns_allowed_paths(app, client):
|
|
"""Dify 传入 JSON 字符串与白名单路径时,应返回 data/missing。"""
|
|
res = client.post(
|
|
"/api/agent-runtime-context/tools/parse",
|
|
headers=_headers(app),
|
|
json={
|
|
"agent_runtime_context": _serialized_context(),
|
|
"paths": [
|
|
"platform.session_id",
|
|
"refs.task_no",
|
|
"refs.batch_no",
|
|
"domain.state_hint.status",
|
|
"reference_context.focus_object",
|
|
"reference_context.last_action_target",
|
|
"reference_context.focus_object.id",
|
|
"reference_context.last_action_target.action",
|
|
"reference_context.last_failed_target.reason",
|
|
"refs.file_name",
|
|
],
|
|
},
|
|
)
|
|
|
|
assert res.status_code == 200
|
|
body = res.get_json()
|
|
assert body["status"] == "ok"
|
|
assert body["data"]["platform.session_id"] == "conv-parse-001"
|
|
assert body["data"]["refs.task_no"] == "SN-PARSE-001"
|
|
assert body["data"]["refs.batch_no"] == "SB-PARSE-001"
|
|
assert body["data"]["domain.state_hint.status"] == "created"
|
|
assert body["data"]["reference_context.focus_object"] == {"type": "task", "id": "SN-PARSE-001"}
|
|
assert body["data"]["reference_context.last_action_target"] == {
|
|
"type": "task",
|
|
"id": "SN-PARSE-000",
|
|
"action": "confirm_parse",
|
|
}
|
|
assert body["data"]["reference_context.focus_object.id"] == "SN-PARSE-001"
|
|
assert body["data"]["reference_context.last_action_target.action"] == "confirm_parse"
|
|
assert body["data"]["reference_context.last_failed_target.reason"] == "shortage"
|
|
assert body["missing"] == ["refs.file_name"]
|
|
|
|
|
|
def test_parse_agent_runtime_context_tool_rejects_disallowed_paths(app, client):
|
|
"""非白名单路径应被拒绝,防止 Dify 读取任意上下文片段。"""
|
|
res = client.post(
|
|
"/api/agent-runtime-context/tools/parse",
|
|
headers=_headers(app),
|
|
json={
|
|
"agent_runtime_context": _serialized_context(),
|
|
"paths": ["platform.session_id", "domain.state_hint.secret"],
|
|
},
|
|
)
|
|
|
|
assert res.status_code == 400
|
|
body = res.get_json()
|
|
assert body["ok"] is False
|
|
assert body["message"] == "paths_not_allowed"
|
|
|
|
|
|
def test_parse_agent_runtime_context_tool_requires_auth(app, client):
|
|
"""parse 工具必须携带平台集成密钥。"""
|
|
app.config["INTEGRATION_API_KEY"] = "test-integration-key"
|
|
|
|
res = client.post(
|
|
"/api/agent-runtime-context/tools/parse",
|
|
json={"agent_runtime_context": _serialized_context(), "paths": ["platform.session_id"]},
|
|
)
|
|
|
|
assert res.status_code == 401
|
|
body = res.get_json()
|
|
assert body["ok"] is False
|
|
assert body["error_code"] == "missing_auth"
|
|
|
|
|
|
def test_agent_runtime_context_openapi_is_public(app, client):
|
|
"""OpenAPI 文档应允许 Dify/开发者无鉴权导入。"""
|
|
res = client.get("/api/agent-runtime-context/tools/openapi.json")
|
|
|
|
assert res.status_code == 200
|
|
body = res.get_json()
|
|
assert body["openapi"] == "3.0.0"
|
|
assert "/tools/parse" in body["paths"]
|
|
assert body["paths"]["/tools/parse"]["post"]["operationId"] == "parse_agent_runtime_context"
|
|
spec_text = str(body)
|
|
assert "reference_context" in spec_text
|
|
assert "多候选" in spec_text
|
|
assert "追问" in spec_text
|
|
assert "state_hint" in spec_text
|
|
assert "状态机" in spec_text
|
|
assert "不能依据上下文直接宣布完成" in spec_text
|
|
|
|
|
|
def test_agent_runtime_context_static_openapi_contains_reference_contract():
|
|
"""静态 OpenAPI 快照应锁定 reference_context 的消费约束,避免导入文件与动态 spec 漂移。"""
|
|
spec_path = Path(__file__).resolve().parents[2] / "docs" / "openapi" / "agent_runtime_context_tools.dify.json"
|
|
spec = json.loads(spec_path.read_text(encoding="utf-8"))
|
|
|
|
assert spec["openapi"] == "3.0.0"
|
|
assert "/tools/parse" in spec["paths"]
|
|
spec_text = str(spec)
|
|
assert "reference_context" in spec_text
|
|
assert "多候选" in spec_text
|
|
assert "追问" in spec_text
|
|
assert "state_hint" in spec_text
|
|
assert "状态机" in spec_text
|
|
assert "不能依据上下文直接宣布完成" in spec_text
|