base.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from dataclasses import asdict, dataclass
  2. from typing import TYPE_CHECKING
  3. from opendevin.schema import ActionType
  4. if TYPE_CHECKING:
  5. from opendevin.controller import AgentController
  6. from opendevin.observation import Observation
  7. @dataclass
  8. class Action:
  9. async def run(self, controller: 'AgentController') -> 'Observation':
  10. raise NotImplementedError
  11. def to_memory(self):
  12. d = asdict(self)
  13. try:
  14. v = d.pop('action')
  15. except KeyError:
  16. raise NotImplementedError(f'{self=} does not have action attribute set')
  17. return {'action': v, 'args': d}
  18. def to_dict(self):
  19. d = self.to_memory()
  20. d['message'] = self.message
  21. return d
  22. @property
  23. def executable(self) -> bool:
  24. raise NotImplementedError
  25. @property
  26. def message(self) -> str:
  27. raise NotImplementedError
  28. @dataclass
  29. class ExecutableAction(Action):
  30. @property
  31. def executable(self) -> bool:
  32. return True
  33. @dataclass
  34. class NotExecutableAction(Action):
  35. @property
  36. def executable(self) -> bool:
  37. return False
  38. @dataclass
  39. class NullAction(NotExecutableAction):
  40. """An action that does nothing.
  41. """
  42. action: str = ActionType.NULL
  43. @property
  44. def message(self) -> str:
  45. return 'No action'