manager.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import atexit
  2. from opendevin.logger import opendevin_logger as logger
  3. from opendevin.server.session import session_manager
  4. from .agent import AgentUnit
  5. class AgentManager:
  6. sid_to_agent: dict[str, 'AgentUnit'] = {}
  7. def __init__(self):
  8. atexit.register(self.close)
  9. def register_agent(self, sid: str):
  10. """Registers a new agent.
  11. Args:
  12. sid: The session ID of the agent.
  13. """
  14. if sid not in self.sid_to_agent:
  15. self.sid_to_agent[sid] = AgentUnit(sid)
  16. return
  17. # TODO: confirm whether the agent is alive
  18. async def dispatch(self, sid: str, action: str | None, data: dict):
  19. """Dispatches actions to the agent from the client."""
  20. if sid not in self.sid_to_agent:
  21. # self.register_agent(sid) # auto-register agent, may be opened later
  22. logger.error(f'Agent not registered: {sid}')
  23. await session_manager.send_error(sid, 'Agent not registered')
  24. return
  25. await self.sid_to_agent[sid].dispatch(action, data)
  26. def close(self):
  27. logger.info(f'Closing {len(self.sid_to_agent)} agent(s)...')
  28. for sid, agent in self.sid_to_agent.items():
  29. agent.close()