commands.py 1.2 KB

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