add: resolve agent for user, check agent health

This commit is contained in:
dantoom 2026-05-26 15:20:58 +03:00
parent b74277a189
commit e50bb8fae5
2 changed files with 25 additions and 0 deletions

View file

@ -7,6 +7,11 @@ from typing import Literal
import yaml import yaml
import structlog
logger = structlog.get_logger()
class AgentRegistryError(ValueError): class AgentRegistryError(ValueError):
pass pass
@ -134,6 +139,17 @@ def load_agent_registry(path: str | Path) -> AgentRegistry:
return AgentRegistry(agents=agents, user_agents=user_agents_map) 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: def load_from_env() -> AgentRegistry:
import os import os

View file

@ -81,6 +81,7 @@ class RoutedMaxPlatformClient(PlatformClient):
self._store = chat_store self._store = chat_store
self._delegates = dict(delegates) self._delegates = dict(delegates)
self._default_client = default_client self._default_client = default_client
self.agent_healthy = True
async def get_or_create_user( async def get_or_create_user(
self, external_id: str, platform: str, display_name: str | None = None 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 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): 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) delegate, platform_chat_id = await self._resolve_delegate(user_id, chat_id)
return await delegate.send_message(user_id, platform_chat_id, text, attachments) return await delegate.send_message(user_id, platform_chat_id, text, attachments)