120 lines
4.6 KiB
Python
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.agents.skills.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),
|
|
},
|
|
)
|