commands.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. def __str__(self) -> str:
  20. return f'**CmdOutputObservation (source={self.source}, exit code={self.exit_code})**\n{self.content}'
  21. @dataclass
  22. class IPythonRunCellObservation(Observation):
  23. """This data class represents the output of a IPythonRunCellAction."""
  24. code: str
  25. observation: str = ObservationType.RUN_IPYTHON
  26. @property
  27. def error(self) -> bool:
  28. return False # IPython cells do not return exit codes
  29. @property
  30. def message(self) -> str:
  31. return 'Code executed in IPython cell.'
  32. def __str__(self) -> str:
  33. return f'**IPythonRunCellObservation**\n{self.content}'