agent.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from opendevin.controller.agent import Agent
  2. from opendevin.controller.state.state import State
  3. from opendevin.events.action import Action, AgentDelegateAction, AgentFinishAction
  4. from opendevin.events.observation import AgentDelegateObservation
  5. from opendevin.llm.llm import LLM
  6. class DelegatorAgent(Agent):
  7. VERSION = '1.0'
  8. """
  9. The Delegator Agent is responsible for delegating tasks to other agents based on the current task.
  10. """
  11. current_delegate: str = ''
  12. def __init__(self, llm: LLM):
  13. """Initialize the Delegator Agent with an LLM
  14. Parameters:
  15. - llm (LLM): The llm to be used by this agent
  16. """
  17. super().__init__(llm)
  18. def step(self, state: State) -> Action:
  19. """Checks to see if current step is completed, returns AgentFinishAction if True.
  20. Otherwise, delegates the task to the next agent in the pipeline.
  21. Parameters:
  22. - state (State): The current state given the previous actions and observations
  23. Returns:
  24. - AgentFinishAction: If the last state was 'completed', 'verified', or 'abandoned'
  25. - AgentDelegateAction: The next agent to delegate the task to
  26. """
  27. if self.current_delegate == '':
  28. self.current_delegate = 'study'
  29. task = state.get_current_user_intent()
  30. return AgentDelegateAction(
  31. agent='StudyRepoForTaskAgent', inputs={'task': task}
  32. )
  33. # last observation in history should be from the delegate
  34. last_observation = state.history.get_last_observation()
  35. if not isinstance(last_observation, AgentDelegateObservation):
  36. raise Exception('Last observation is not an AgentDelegateObservation')
  37. goal = state.get_current_user_intent()
  38. if self.current_delegate == 'study':
  39. self.current_delegate = 'coder'
  40. return AgentDelegateAction(
  41. agent='CoderAgent',
  42. inputs={
  43. 'task': goal,
  44. 'summary': last_observation.outputs['summary'],
  45. },
  46. )
  47. elif self.current_delegate == 'coder':
  48. self.current_delegate = 'verifier'
  49. return AgentDelegateAction(
  50. agent='VerifierAgent',
  51. inputs={
  52. 'task': goal,
  53. },
  54. )
  55. elif self.current_delegate == 'verifier':
  56. if (
  57. 'completed' in last_observation.outputs
  58. and last_observation.outputs['completed']
  59. ):
  60. return AgentFinishAction()
  61. else:
  62. self.current_delegate = 'coder'
  63. return AgentDelegateAction(
  64. agent='CoderAgent',
  65. inputs={
  66. 'task': goal,
  67. 'summary': last_observation.outputs['summary'],
  68. },
  69. )
  70. else:
  71. raise Exception('Invalid delegate state')