commands.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from dataclasses import dataclass
  2. from openhands.core.schema import ObservationType
  3. from openhands.events.observation.observation import Observation
  4. @dataclass
  5. class CmdOutputObservation(Observation):
  6. """This data class represents the output of a command."""
  7. command_id: int
  8. command: str
  9. exit_code: int = 0
  10. hidden: bool = False
  11. observation: str = ObservationType.RUN
  12. interpreter_details: str = ''
  13. @property
  14. def error(self) -> bool:
  15. return self.exit_code != 0
  16. @property
  17. def message(self) -> str:
  18. return f'Command `{self.command}` executed with exit code {self.exit_code}.'
  19. @property
  20. def success(self) -> bool:
  21. return not self.error
  22. def __str__(self) -> str:
  23. return f'**CmdOutputObservation (source={self.source}, exit code={self.exit_code})**\n{self.content}'
  24. @dataclass
  25. class IPythonRunCellObservation(Observation):
  26. """This data class represents the output of a IPythonRunCellAction."""
  27. code: str
  28. observation: str = ObservationType.RUN_IPYTHON
  29. @property
  30. def error(self) -> bool:
  31. return False # IPython cells do not return exit codes
  32. @property
  33. def message(self) -> str:
  34. return 'Code executed in IPython cell.'
  35. @property
  36. def success(self) -> bool:
  37. return True # IPython cells are always considered successful
  38. def __str__(self) -> str:
  39. return f'**IPythonRunCellObservation**\n{self.content}'