- 替换原有的hxops_context模块为全新的代理运行时上下文平台协议,统一跨业务上下文注入逻辑 - 新增多语言操作繁忙提示文案(覆盖中、繁、英、泰四语),前端绑定页面繁忙状态实现操作阻断 - 调整RPA库存导出重试策略:移除可配置的最大自动重试次数,改为固定单次自动重试,手动重试需用户显式触发 - 优化任务用户态状态展示逻辑,优先展示具体的pending原因而非通用状态文案 - 重构任务焦点切换逻辑,统一事件日志与处理流程,复用switch_batch_focus工具函数 - 修复clearAllHistory函数残留UI问题,确保清空会话后完全复位发货通知相关视图 - 更新OpenAPI文档与静态快照,补充工具使用约束说明 - 新增调试打点日志与问题排查文档,完善全套单元测试与快照校验用例 - 更新RPA脚本的README文档,调整登录方式说明与浏览器 profile 路径配置 - 删除废弃的hxops_context模块,迁移所有相关调用到新的上下文体系
123 lines
4.4 KiB
Python
123 lines
4.4 KiB
Python
"""
|
|
backend/tests/test_agent_runtime_context_builder.py
|
|
|
|
用途:验证 agent_runtime_context 平台协议构建、序列化与 reference builder 行为。
|
|
依赖:agent_runtime_context.builder/policy/reference。
|
|
逻辑:覆盖平台上下文字段、refs 规范化、Dify JSON 字符串输入与 reference_context 裁剪规则。
|
|
"""
|
|
|
|
import json
|
|
|
|
import pytest
|
|
|
|
from app.services.agent_runtime_context import AGENT_RUNTIME_CONTEXT_INPUT
|
|
from app.services.agent_runtime_context.builder import (
|
|
build_agent_runtime_context,
|
|
build_dify_inputs,
|
|
serialize_for_dify,
|
|
)
|
|
from app.services.agent_runtime_context.policy import (
|
|
MAX_CONTEXT_JSON_CHARS,
|
|
MAX_DOMAIN_SUMMARY_CHARS,
|
|
MAX_RECENT_ACTIVITY_SUMMARY_CHARS,
|
|
)
|
|
from app.services.agent_runtime_context.reference import (
|
|
MAX_REFERENCE_CANDIDATES,
|
|
build_object_ref,
|
|
build_reference_context,
|
|
)
|
|
|
|
|
|
def test_build_agent_runtime_context_normalizes_core_fields():
|
|
"""新版上下文应输出稳定协议字段并裁剪重复/空 refs。"""
|
|
context = build_agent_runtime_context(
|
|
agent_code="shipping_notice",
|
|
scope="task_run",
|
|
session_id=" conv-001 ",
|
|
request_id=" req-001 ",
|
|
trigger_source=" frontend_chat ",
|
|
locale=" zh-CN ",
|
|
refs={"task_no": " SN-001 ", "batch_no": "SB-001", "empty": ""},
|
|
domain={"type": "shipping_notice", "summary": "当前处理 SN-001"},
|
|
)
|
|
|
|
assert context["schema_version"] == "1"
|
|
assert context["context_id"].startswith("ctx_")
|
|
assert context["agent_code"] == "shipping_notice"
|
|
assert context["scope"] == "task_run"
|
|
assert context["platform"] == {
|
|
"session_id": "conv-001",
|
|
"parent_session_id": "conv-001",
|
|
"request_id": "req-001",
|
|
"trigger_source": "frontend_chat",
|
|
"locale": "zh-CN",
|
|
}
|
|
assert context["refs"] == {"task_no": "SN-001", "batch_no": "SB-001"}
|
|
assert context["domain"]["summary"] == "当前处理 SN-001"
|
|
|
|
|
|
def test_serialize_for_dify_outputs_json_string():
|
|
"""Dify Start 节点应接收 JSON 字符串而不是 object。"""
|
|
context = build_agent_runtime_context(
|
|
agent_code="file_check",
|
|
scope="chat_turn",
|
|
session_id="conv-file-check-001",
|
|
refs={"schedule_task_id": 123},
|
|
)
|
|
|
|
inputs = build_dify_inputs(context)
|
|
serialized = inputs[AGENT_RUNTIME_CONTEXT_INPUT]
|
|
|
|
assert isinstance(serialized, str)
|
|
assert json.loads(serialized)["refs"] == {"schedule_task_id": "123"}
|
|
|
|
|
|
def test_policy_truncates_domain_summary_before_serialization():
|
|
"""超长领域摘要应先被裁剪,避免把历史流水塞进 Runtime。"""
|
|
long_summary = "A" * (MAX_DOMAIN_SUMMARY_CHARS + 50)
|
|
|
|
context = build_agent_runtime_context(
|
|
session_id="conv-long-summary-001",
|
|
domain={"type": "generic", "summary": long_summary},
|
|
)
|
|
|
|
assert len(context["domain"]["summary"]) == MAX_DOMAIN_SUMMARY_CHARS
|
|
|
|
|
|
def test_build_reference_context_normalizes_objects_candidates_and_summary():
|
|
"""reference_context builder 应统一裁剪摘要、规范对象引用并限制候选数量。"""
|
|
reference_context = build_reference_context(
|
|
recent_activity_summary="A" * (MAX_RECENT_ACTIVITY_SUMMARY_CHARS + 32),
|
|
focus_object=build_object_ref("task", "SN-FOCUS-001"),
|
|
last_action_target={"type": "task", "id": "SN-ACTION-001", "action": "confirm"},
|
|
candidates=[
|
|
{"type": "task", "id": f"SN-CAND-{index:03d}", "label": f"候选 {index}"}
|
|
for index in range(MAX_REFERENCE_CANDIDATES + 3)
|
|
],
|
|
)
|
|
|
|
assert len(reference_context["recent_activity_summary"]) == MAX_RECENT_ACTIVITY_SUMMARY_CHARS
|
|
assert reference_context["focus_object"] == {"type": "task", "id": "SN-FOCUS-001"}
|
|
assert reference_context["last_action_target"] == {
|
|
"type": "task",
|
|
"id": "SN-ACTION-001",
|
|
"action": "confirm",
|
|
}
|
|
assert len(reference_context["candidates"]) == MAX_REFERENCE_CANDIDATES
|
|
|
|
|
|
def test_serialize_for_dify_rejects_oversized_context():
|
|
"""序列化后超过总长度上限时应显式失败,避免输出非法截断 JSON。"""
|
|
context = {
|
|
"schema_version": "1",
|
|
"context_id": "ctx-too-large",
|
|
"agent_code": "generic",
|
|
"scope": "chat_turn",
|
|
"platform": {"session_id": "conv-too-large"},
|
|
"refs": {},
|
|
"payload": "X" * (MAX_CONTEXT_JSON_CHARS + 1),
|
|
}
|
|
|
|
with pytest.raises(ValueError):
|
|
serialize_for_dify(context)
|