command_executor.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # This file parses commands given to simulator.py and evaluates them using agents
  2. # Facilitates interaction with agents
  3. # Enables commands to be executed concurrently
  4. from concurrent.futures import ThreadPoolExecutor
  5. class Executor:
  6. def __init__(self, agent_factory):
  7. self.agent_thread_pool = ThreadPoolExecutor(max_workers=64)
  8. self.command_table = {
  9. "run": self.run_agent,
  10. "print": self.print
  11. }
  12. self.agent_factory = agent_factory
  13. # self.agent_process_queue = agent_process_queue
  14. def execute(self, command):
  15. """Executes a given command."""
  16. command_type = command["command_type"]
  17. command_name = command["command_name"]
  18. command_body = command["command_body"]
  19. try:
  20. self.command_table[command_type](
  21. command_name,
  22. command_body
  23. )
  24. except KeyError:
  25. print("usage: command_type: [command_name] [command_body]")
  26. def print_agent_memory():
  27. pass
  28. def print(self, command_name = None, command_body = None):
  29. """List status of agent process."""
  30. if command_name == "agent":
  31. self.agent_factory.print_agent()
  32. elif command_name == "agent-process":
  33. return NotImplementedError
  34. def run_agent(self, agent_name, task_input):
  35. """ run command """
  36. # self.agent_factory.activate_agent(agent_name, task_input)
  37. # print(agent_name, task_input)
  38. self.agent_thread_pool.submit(
  39. self.agent_factory.run_agent,
  40. agent_name,
  41. task_input
  42. )
  43. # result = task.result()