agent.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. """
  14. Initialize the Delegator Agent with an LLM
  15. Parameters:
  16. - llm (LLM): The llm to be used by this agent
  17. """
  18. super().__init__(llm)
  19. def step(self, state: State) -> Action:
  20. """
  21. Checks to see if current step is completed, returns AgentFinishAction if True.
  22. Otherwise, delegates the task to the next agent in the pipeline.
  23. Parameters:
  24. - state (State): The current state given the previous actions and observations
  25. Returns:
  26. - AgentFinishAction: If the last state was 'completed', 'verified', or 'abandoned'
  27. - AgentDelegateAction: The next agent to delegate the task to
  28. """
  29. if self.current_delegate == '':
  30. self.current_delegate = 'study'
  31. task = state.get_current_user_intent()
  32. return AgentDelegateAction(
  33. agent='StudyRepoForTaskAgent', inputs={'task': task}
  34. )
  35. last_observation = state.history[-1][1]
  36. if not isinstance(last_observation, AgentDelegateObservation):
  37. raise Exception('Last observation is not an AgentDelegateObservation')
  38. goal = state.get_current_user_intent()
  39. if self.current_delegate == 'study':
  40. self.current_delegate = 'coder'
  41. return AgentDelegateAction(
  42. agent='CoderAgent',
  43. inputs={
  44. 'task': goal,
  45. 'summary': last_observation.outputs['summary'],
  46. },
  47. )
  48. elif self.current_delegate == 'coder':
  49. self.current_delegate = 'verifier'
  50. return AgentDelegateAction(
  51. agent='VerifierAgent',
  52. inputs={
  53. 'task': goal,
  54. },
  55. )
  56. elif self.current_delegate == 'verifier':
  57. if (
  58. 'completed' in last_observation.outputs
  59. and last_observation.outputs['completed']
  60. ):
  61. return AgentFinishAction()
  62. else:
  63. self.current_delegate = 'coder'
  64. return AgentDelegateAction(
  65. agent='CoderAgent',
  66. inputs={
  67. 'task': goal,
  68. 'summary': last_observation.outputs['summary'],
  69. },
  70. )
  71. else:
  72. raise Exception('Invalid delegate state')
  73. def search_memory(self, query: str) -> list[str]:
  74. return []