base.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from dataclasses import dataclass, asdict
  2. from typing import TYPE_CHECKING
  3. if TYPE_CHECKING:
  4. from opendevin.controller import AgentController
  5. from opendevin.observation import Observation
  6. @dataclass
  7. class Action:
  8. def run(self, controller: "AgentController") -> "Observation":
  9. raise NotImplementedError
  10. def to_dict(self):
  11. d = asdict(self)
  12. try:
  13. v = d.pop('action')
  14. except KeyError:
  15. raise NotImplementedError(f'{self=} does not have action attribute set')
  16. return {'action': v, "args": d, "message": self.message}
  17. @property
  18. def executable(self) -> bool:
  19. raise NotImplementedError
  20. @property
  21. def message(self) -> str:
  22. raise NotImplementedError
  23. @dataclass
  24. class ExecutableAction(Action):
  25. @property
  26. def executable(self) -> bool:
  27. return True
  28. @dataclass
  29. class NotExecutableAction(Action):
  30. @property
  31. def executable(self) -> bool:
  32. return False
  33. @dataclass
  34. class NullAction(NotExecutableAction):
  35. """An action that does nothing.
  36. This is used when the agent need to receive user follow-up messages from the frontend.
  37. """
  38. action: str = "null"
  39. @property
  40. def message(self) -> str:
  41. return "No action"