- 替换原有的hxops_context模块为全新的代理运行时上下文平台协议,统一跨业务上下文注入逻辑 - 新增多语言操作繁忙提示文案(覆盖中、繁、英、泰四语),前端绑定页面繁忙状态实现操作阻断 - 调整RPA库存导出重试策略:移除可配置的最大自动重试次数,改为固定单次自动重试,手动重试需用户显式触发 - 优化任务用户态状态展示逻辑,优先展示具体的pending原因而非通用状态文案 - 重构任务焦点切换逻辑,统一事件日志与处理流程,复用switch_batch_focus工具函数 - 修复clearAllHistory函数残留UI问题,确保清空会话后完全复位发货通知相关视图 - 更新OpenAPI文档与静态快照,补充工具使用约束说明 - 新增调试打点日志与问题排查文档,完善全套单元测试与快照校验用例 - 更新RPA脚本的README文档,调整登录方式说明与浏览器 profile 路径配置 - 删除废弃的hxops_context模块,迁移所有相关调用到新的上下文体系
104 lines
3.1 KiB
Python
104 lines
3.1 KiB
Python
"""
|
|
backend/app/services/agent_runtime_context/policy.py
|
|
|
|
用途:集中管理 agent_runtime_context 的长度、字段与安全裁剪策略。
|
|
依赖:schema 中的平台协议常量;不访问数据库或业务服务。
|
|
逻辑:builder 在输出给 Dify 前调用本模块,确保上下文短小、稳定且不会把大段历史塞进 Runtime。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
MAX_CONTEXT_JSON_CHARS = 8192
|
|
MAX_DOMAIN_SUMMARY_CHARS = 500
|
|
MAX_RECENT_ACTIVITY_SUMMARY_CHARS = 500
|
|
MAX_REF_VALUE_CHARS = 200
|
|
MAX_REFS = 30
|
|
|
|
|
|
def truncate_text(value: Any, max_chars: int) -> str:
|
|
"""
|
|
将任意输入规范化为短文本。
|
|
|
|
核心逻辑:strip 后按字符数截断,避免上下文摘要或 ref 值撑爆 Dify Start 变量。
|
|
|
|
Args:
|
|
value: 待规范化的文本或可转字符串对象。
|
|
max_chars: 最大保留字符数。
|
|
|
|
Returns:
|
|
规范化后的字符串;空值返回空字符串。
|
|
"""
|
|
normalized = str(value or "").strip()
|
|
if max_chars <= 0 or len(normalized) <= max_chars:
|
|
return normalized
|
|
return normalized[:max_chars]
|
|
|
|
|
|
def normalize_refs(refs: dict[str, Any] | None) -> dict[str, str]:
|
|
"""
|
|
规范化业务对象锚点。
|
|
|
|
核心逻辑:保留首个有效 key/value,裁剪 value 长度,并限制总数量,防止业务 adapter 注入过长对象。
|
|
|
|
Args:
|
|
refs: 业务 adapter 传入的 refs 字典。
|
|
|
|
Returns:
|
|
字段名和值均为非空字符串的 refs 字典。
|
|
"""
|
|
normalized: dict[str, str] = {}
|
|
for raw_key, raw_value in (refs or {}).items():
|
|
key = str(raw_key or "").strip()
|
|
value = truncate_text(raw_value, MAX_REF_VALUE_CHARS)
|
|
if not key or not value or key in normalized:
|
|
continue
|
|
normalized[key] = value
|
|
if len(normalized) >= MAX_REFS:
|
|
break
|
|
return normalized
|
|
|
|
|
|
def normalize_domain(domain: dict[str, Any] | None) -> dict[str, Any]:
|
|
"""
|
|
规范化领域上下文。
|
|
|
|
核心逻辑:只裁剪已知长文本字段,其余结构保持给业务 adapter 自主表达。
|
|
|
|
Args:
|
|
domain: 业务域上下文片段。
|
|
|
|
Returns:
|
|
裁剪后的领域上下文字典。
|
|
"""
|
|
if not domain:
|
|
return {}
|
|
normalized = dict(domain)
|
|
if "summary" in normalized:
|
|
normalized["summary"] = truncate_text(normalized.get("summary"), MAX_DOMAIN_SUMMARY_CHARS)
|
|
return normalized
|
|
|
|
|
|
def normalize_reference_context(reference_context: dict[str, Any] | None) -> dict[str, Any]:
|
|
"""
|
|
规范化短期指代上下文。
|
|
|
|
核心逻辑:限制 recent_activity_summary 长度,保留 focus/candidates 等结构化候选。
|
|
|
|
Args:
|
|
reference_context: 指代治理上下文。
|
|
|
|
Returns:
|
|
裁剪后的 reference_context 字典。
|
|
"""
|
|
if not reference_context:
|
|
return {}
|
|
normalized = dict(reference_context)
|
|
if "recent_activity_summary" in normalized:
|
|
normalized["recent_activity_summary"] = truncate_text(
|
|
normalized.get("recent_activity_summary"),
|
|
MAX_RECENT_ACTIVITY_SUMMARY_CHARS,
|
|
)
|
|
return normalized
|