2026-03-04 08:53:50 +08:00
|
|
|
from __future__ import annotations
|
2026-03-02 00:04:16 +08:00
|
|
|
|
2026-03-04 08:53:50 +08:00
|
|
|
from deepagents.backends import CompositeBackend
|
|
|
|
|
|
2026-03-05 01:42:53 +08:00
|
|
|
from src.agents.common.middlewares.skills_middleware import normalize_selected_skills
|
2026-03-20 21:18:13 +08:00
|
|
|
from src.sandbox import ProvisionerSandboxBackend
|
2026-03-02 00:04:16 +08:00
|
|
|
|
|
|
|
|
from .skills_backend import SelectedSkillsReadonlyBackend
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _get_visible_skills_from_runtime(runtime) -> list[str]:
|
2026-03-05 01:42:53 +08:00
|
|
|
"""获取运行时可见的 skills 列表"""
|
2026-03-02 00:04:16 +08:00
|
|
|
context = getattr(runtime, "context", None)
|
|
|
|
|
selected = getattr(context, "skills", None) or []
|
|
|
|
|
return normalize_selected_skills(selected)
|
|
|
|
|
|
|
|
|
|
|
2026-03-04 08:53:50 +08:00
|
|
|
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")
|
|
|
|
|
|
|
|
|
|
|
2026-03-02 00:04:16 +08:00
|
|
|
def create_agent_composite_backend(runtime) -> CompositeBackend:
|
|
|
|
|
visible_skills = _get_visible_skills_from_runtime(runtime)
|
2026-03-04 08:53:50 +08:00
|
|
|
thread_id = _extract_thread_id(runtime)
|
2026-03-02 00:04:16 +08:00
|
|
|
return CompositeBackend(
|
2026-03-04 08:53:50 +08:00
|
|
|
default=ProvisionerSandboxBackend(thread_id=thread_id),
|
2026-03-02 00:04:16 +08:00
|
|
|
routes={
|
|
|
|
|
"/skills/": SelectedSkillsReadonlyBackend(selected_slugs=visible_skills),
|
|
|
|
|
},
|
|
|
|
|
)
|