Routers calling /registry/agents raised AttributeError because get_all() was not defined. Added method returning all registered agents with active status, capabilities and instance flags.
53 lines
1.8 KiB
Python
53 lines
1.8 KiB
Python
from __future__ import annotations
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class AgentRegistry:
|
|
def __init__(self):
|
|
self._agents: dict = {} # agent_key -> BaseAgent instance
|
|
self._active: set = set()
|
|
self._capabilities: dict = {}
|
|
|
|
async def load_from_odoo(self, odoo_client):
|
|
try:
|
|
rows = await odoo_client.search_read(
|
|
'ab.ai.agent.registry',
|
|
[['active', '=', True]],
|
|
['agent_name', 'domain', 'backend'])
|
|
self._active = {r['agent_name'] for r in rows}
|
|
self._capabilities = {r['agent_name']: r.get('domain', '') for r in rows}
|
|
logger.info('AgentRegistry loaded %d agents: %s', len(self._active), list(self._active))
|
|
except Exception as exc:
|
|
logger.error('AgentRegistry.load_from_odoo failed: %s', exc)
|
|
|
|
async def get_active_agents(self):
|
|
return [{'agent_key': k, 'capabilities_summary': self._capabilities.get(k, '')}
|
|
for k in self._active]
|
|
|
|
async def get_all(self):
|
|
"""Return all registered agents with active status and capabilities."""
|
|
return [
|
|
{
|
|
'agent_key': k,
|
|
'active': k in self._active,
|
|
'capabilities_summary': self._capabilities.get(k, ''),
|
|
'has_instance': k in self._agents,
|
|
}
|
|
for k in self._agents
|
|
]
|
|
|
|
async def is_active(self, agent_key):
|
|
return agent_key in self._active
|
|
|
|
async def sync(self, active_keys):
|
|
self._active = set(active_keys)
|
|
logger.info('AgentRegistry synced: active=%s', active_keys)
|
|
|
|
def register(self, agent_key, agent_instance):
|
|
self._agents[agent_key] = agent_instance
|
|
|
|
def get_agent_instance(self, agent_key):
|
|
return self._agents.get(agent_key)
|