base.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from dataclasses import dataclass, asdict
  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. def run(self, controller: 'AgentController') -> 'Observation':
  10. raise NotImplementedError
  11. def to_dict(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, 'message': self.message}
  18. @property
  19. def executable(self) -> bool:
  20. raise NotImplementedError
  21. @property
  22. def message(self) -> str:
  23. raise NotImplementedError
  24. @dataclass
  25. class ExecutableAction(Action):
  26. @property
  27. def executable(self) -> bool:
  28. return True
  29. @dataclass
  30. class NotExecutableAction(Action):
  31. @property
  32. def executable(self) -> bool:
  33. return False
  34. @dataclass
  35. class NullAction(NotExecutableAction):
  36. """An action that does nothing.
  37. This is used when the agent need to receive user follow-up messages from the frontend.
  38. """
  39. action: str = ActionType.NULL
  40. @property
  41. def message(self) -> str:
  42. return 'No action'