From e50bb8fae5a37f71e72caa1a9593631764283890 Mon Sep 17 00:00:00 2001 From: dantoom Date: Tue, 26 May 2026 15:20:58 +0300 Subject: [PATCH] add: resolve agent for user, check agent health --- adapter/max/agent_registry.py | 16 ++++++++++++++++ adapter/max/bot.py | 9 +++++++++ 2 files changed, 25 insertions(+) diff --git a/adapter/max/agent_registry.py b/adapter/max/agent_registry.py index cc3c0af..edec923 100644 --- a/adapter/max/agent_registry.py +++ b/adapter/max/agent_registry.py @@ -7,6 +7,11 @@ from typing import Literal import yaml +import structlog + + + +logger = structlog.get_logger() class AgentRegistryError(ValueError): pass @@ -134,6 +139,17 @@ def load_agent_registry(path: str | Path) -> AgentRegistry: return AgentRegistry(agents=agents, user_agents=user_agents_map) +def resolve_agent_for_user(self, max_user_id: str) -> AgentAssignment: + agent_id = self.get_agent_id_for_user(max_user_id) + if agent_id is not None: + return AgentAssignment(agent_id=agent_id, source="configured") + if self.agents: + # Логируем использование default агента + logger.warning("using_default_agent_for_user", user_id=max_user_id, agent_id=self.agents[0].agent_id) + return AgentAssignment(agent_id=self.agents[0].agent_id, source="default") + return AgentAssignment(agent_id=None, source="none") + + def load_from_env() -> AgentRegistry: import os diff --git a/adapter/max/bot.py b/adapter/max/bot.py index 5bd9e61..574dee4 100644 --- a/adapter/max/bot.py +++ b/adapter/max/bot.py @@ -81,6 +81,7 @@ class RoutedMaxPlatformClient(PlatformClient): self._store = chat_store self._delegates = dict(delegates) self._default_client = default_client + self.agent_healthy = True async def get_or_create_user( self, external_id: str, platform: str, display_name: str | None = None @@ -89,6 +90,14 @@ class RoutedMaxPlatformClient(PlatformClient): external_id=external_id, platform=platform, display_name=display_name ) + async def _check_agent_health(self): + try: + async with httpx.AsyncClient() as client: + resp = await client.get(f"{self.agent_base_url}/health", timeout=2.0) + self.agent_healthy = resp.status_code == 200 + except Exception: + self.agent_healthy = False + async def send_message(self, user_id: str, chat_id: str, text: str, attachments=None): delegate, platform_chat_id = await self._resolve_delegate(user_id, chat_id) return await delegate.send_message(user_id, platform_chat_id, text, attachments)