ForcePilot/backend/package/yuxi/agents/backends/composite.py
Wenjie Zhang c772e5ca3a refactor: Agent 运行时架构重构 - 移除 RuntimeConfigMiddleware,统一上下文准备与工具解析
- 删除 runtime_config_middleware.py,将功能合并到:
  - prepare_agent_runtime_context(): 统一上下文准备入口
  - resolve_configured_runtime_tools(): 运行时工具解析
- context.py: 规范化函数重命名 (_names → _keys),重构 config 加载流程
- chatbot/deep_agent graph: 集成新上下文准备流程,移除 RuntimeConfigMiddleware 引用
- skills_middleware: 抽取 normalize_string_list 为共享工具函数
- subagent_service: get_subagents_from_names → get_subagents_from_slugs,支持 slug 查询
- 对应更新 repositories/services/routers 适配新接口
2026-05-26 17:40:17 +08:00

120 lines
4.6 KiB
Python

from __future__ import annotations
from deepagents.backends.composite import (
CompositeBackend,
_remap_file_info_path,
_route_for_path,
_strip_route_from_pattern,
)
from deepagents.backends.protocol import FileInfo
from yuxi.services.skill_service import normalize_string_list
from .sandbox import ProvisionerSandboxBackend
from .skills_backend import SelectedSkillsReadonlyBackend
class CustomCompositeBackend(CompositeBackend):
"""修复 glob_info 路由逻辑的 CompositeBackend。
修复内容:当 path 不匹配任何路由时应该只搜索 default 后端,
而不是错误地遍历所有路由后端搜索。
"""
def glob_info(self, pattern: str, path: str = "/") -> list[FileInfo]:
backend, backend_path, route_prefix = _route_for_path(
default=self.default,
sorted_routes=self.sorted_routes,
path=path,
)
if route_prefix is not None:
infos = backend.glob_info(pattern, backend_path)
return [_remap_file_info_path(fi, route_prefix) for fi in infos]
# 只在 path 为 None 或 "/" 时搜索所有后端,其他只搜索 default
if path is None or path == "/":
results: list[FileInfo] = []
results.extend(self.default.glob_info(pattern, path))
for route_prefix, backend in self.routes.items():
route_pattern = _strip_route_from_pattern(pattern, route_prefix)
infos = backend.glob_info(route_pattern, "/")
results.extend(_remap_file_info_path(fi, route_prefix) for fi in infos)
results.sort(key=lambda x: x.get("path", ""))
return results
return self.default.glob_info(pattern, path)
async def aglob_info(self, pattern: str, path: str = "/") -> list[FileInfo]:
backend, backend_path, route_prefix = _route_for_path(
default=self.default,
sorted_routes=self.sorted_routes,
path=path,
)
if route_prefix is not None:
infos = await backend.aglob_info(pattern, backend_path)
return [_remap_file_info_path(fi, route_prefix) for fi in infos]
if path is None or path == "/":
results: list[FileInfo] = []
results.extend(await self.default.aglob_info(pattern, path))
for route_prefix, backend in self.routes.items():
route_pattern = _strip_route_from_pattern(pattern, route_prefix)
infos = await backend.aglob_info(route_pattern, "/")
results.extend(_remap_file_info_path(fi, route_prefix) for fi in infos)
results.sort(key=lambda x: x.get("path", ""))
return results
return await self.default.aglob_info(pattern, path)
def _get_readable_skills_from_runtime(runtime) -> list[str]:
context = getattr(runtime, "context", None)
selected = getattr(context, "_readable_skills", [])
return normalize_string_list(selected if isinstance(selected, list) else [])
def _extract_thread_id(runtime) -> str:
config = getattr(runtime, "config", None)
if isinstance(config, dict):
configurable = config.get("configurable", {})
if isinstance(configurable, dict):
thread_id = configurable.get("thread_id")
if isinstance(thread_id, str) and thread_id.strip():
return thread_id.strip()
context = getattr(runtime, "context", None)
thread_id = getattr(context, "thread_id", None)
if isinstance(thread_id, str) and thread_id.strip():
return thread_id.strip()
raise ValueError("thread_id is required in runtime configurable context")
def _extract_uid(runtime) -> str:
config = getattr(runtime, "config", None)
if isinstance(config, dict):
configurable = config.get("configurable", {})
if isinstance(configurable, dict):
uid = configurable.get("uid")
if isinstance(uid, str) and uid.strip():
return uid.strip()
context = getattr(runtime, "context", None)
uid = getattr(context, "uid", None)
if isinstance(uid, str) and uid.strip():
return uid.strip()
raise ValueError("uid is required in runtime configurable context")
def create_agent_composite_backend(runtime) -> CompositeBackend:
readable_skills = _get_readable_skills_from_runtime(runtime)
thread_id = _extract_thread_id(runtime)
uid = _extract_uid(runtime)
return CustomCompositeBackend(
default=ProvisionerSandboxBackend(thread_id=thread_id, uid=uid, readable_skills=readable_skills),
routes={
"/skills/": SelectedSkillsReadonlyBackend(selected_slugs=readable_skills),
},
)