commands.py 1.2 KB

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