from __future__ import annotations import re import sys from pathlib import Path from urllib.parse import urlsplit, urlunsplit _api_root = Path(__file__).resolve().parents[1] / "external" / "platform-agent_api" if str(_api_root) not in sys.path: sys.path.insert(0, str(_api_root)) from lambda_agent_api.agent_api import AgentApi # noqa: E402 class AgentApiWrapper(AgentApi): """Thin construction/factory shim over the pinned upstream AgentApi.""" def __init__( self, agent_id: str, base_url: str, *, chat_id: int | str = 0, **kwargs, ) -> None: self._base_url = self._normalize_base_url(base_url) self._init_kwargs = dict(kwargs) self.chat_id = chat_id super().__init__( agent_id=agent_id, base_url=self._base_url, chat_id=chat_id, **kwargs, ) @staticmethod def _normalize_base_url(base_url: str) -> str: parsed = urlsplit(base_url) path = re.sub(r"(?:/v1)?/agent_ws(?:/[^/]+)?/?$", "", parsed.path.rstrip("/")) return urlunsplit((parsed.scheme, parsed.netloc, path, "", "")) def for_chat(self, chat_id: int | str) -> AgentApiWrapper: return type(self)( agent_id=self.id, base_url=self._base_url, chat_id=chat_id, **self._init_kwargs, )