agent.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. from dataclasses import dataclass, field
  2. from typing import TYPE_CHECKING, Dict
  3. from opendevin.observation import (
  4. AgentMessageObservation,
  5. AgentRecallObservation,
  6. NullObservation,
  7. Observation,
  8. )
  9. from opendevin.schema import ActionType
  10. from .base import ExecutableAction, NotExecutableAction
  11. if TYPE_CHECKING:
  12. from opendevin.controller import AgentController
  13. @dataclass
  14. class AgentRecallAction(ExecutableAction):
  15. query: str
  16. thought: str = ''
  17. action: str = ActionType.RECALL
  18. async def run(self, controller: 'AgentController') -> AgentRecallObservation:
  19. return AgentRecallObservation(
  20. content='',
  21. memories=controller.agent.search_memory(self.query),
  22. )
  23. @property
  24. def message(self) -> str:
  25. return f"Let me dive into my memories to find what you're looking for! Searching for: '{self.query}'. This might take a moment."
  26. @dataclass
  27. class AgentThinkAction(NotExecutableAction):
  28. thought: str
  29. action: str = ActionType.THINK
  30. async def run(self, controller: 'AgentController') -> 'Observation':
  31. raise NotImplementedError
  32. @property
  33. def message(self) -> str:
  34. return self.thought
  35. @dataclass
  36. class AgentTalkAction(NotExecutableAction):
  37. content: str
  38. action: str = ActionType.TALK
  39. async def run(self, controller: 'AgentController') -> 'Observation':
  40. raise NotImplementedError
  41. @property
  42. def message(self) -> str:
  43. return self.content
  44. def __str__(self) -> str:
  45. return self.content
  46. @dataclass
  47. class AgentEchoAction(ExecutableAction):
  48. content: str
  49. action: str = 'echo'
  50. async def run(self, controller: 'AgentController') -> 'Observation':
  51. return AgentMessageObservation(self.content)
  52. @property
  53. def message(self) -> str:
  54. return self.content
  55. @dataclass
  56. class AgentSummarizeAction(NotExecutableAction):
  57. summary: str
  58. action: str = ActionType.SUMMARIZE
  59. @property
  60. def message(self) -> str:
  61. return self.summary
  62. @dataclass
  63. class AgentFinishAction(NotExecutableAction):
  64. outputs: Dict = field(default_factory=dict)
  65. thought: str = ''
  66. action: str = ActionType.FINISH
  67. async def run(self, controller: 'AgentController') -> 'Observation':
  68. raise NotImplementedError
  69. @property
  70. def message(self) -> str:
  71. return "All done! What's next on the agenda?"
  72. @dataclass
  73. class AgentDelegateAction(ExecutableAction):
  74. agent: str
  75. inputs: dict
  76. thought: str = ''
  77. action: str = ActionType.DELEGATE
  78. async def run(self, controller: 'AgentController') -> 'Observation':
  79. await controller.start_delegate(self)
  80. return NullObservation('')
  81. @property
  82. def message(self) -> str:
  83. return f"I'm asking {self.agent} for help with this task."