commands.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from dataclasses import dataclass
  2. from typing import ClassVar
  3. from openhands.core.schema import ActionType
  4. from openhands.events.action.action import (
  5. Action,
  6. ActionConfirmationStatus,
  7. ActionSecurityRisk,
  8. )
  9. @dataclass
  10. class CmdRunAction(Action):
  11. command: str
  12. thought: str = ''
  13. blocking: bool = False
  14. # If False, the command will be run in a non-blocking / interactive way
  15. # The partial command outputs will be returned as output observation.
  16. # If True, the command will be run for max .timeout seconds.
  17. keep_prompt: bool = True
  18. # if True, the command prompt will be kept in the command output observation
  19. # Example of command output:
  20. # root@sandbox:~# ls
  21. # file1.txt
  22. # file2.txt
  23. # root@sandbox:~# <-- this is the command prompt
  24. action: str = ActionType.RUN
  25. runnable: ClassVar[bool] = True
  26. is_confirmed: ActionConfirmationStatus = ActionConfirmationStatus.CONFIRMED
  27. security_risk: ActionSecurityRisk | None = None
  28. @property
  29. def message(self) -> str:
  30. return f'Running command: {self.command}'
  31. def __str__(self) -> str:
  32. ret = f'**CmdRunAction (source={self.source})**\n'
  33. if self.thought:
  34. ret += f'THOUGHT: {self.thought}\n'
  35. ret += f'COMMAND:\n{self.command}'
  36. return ret
  37. @dataclass
  38. class IPythonRunCellAction(Action):
  39. code: str
  40. thought: str = ''
  41. action: str = ActionType.RUN_IPYTHON
  42. runnable: ClassVar[bool] = True
  43. is_confirmed: ActionConfirmationStatus = ActionConfirmationStatus.CONFIRMED
  44. security_risk: ActionSecurityRisk | None = None
  45. kernel_init_code: str = '' # code to run in the kernel (if the kernel is restarted)
  46. def __str__(self) -> str:
  47. ret = '**IPythonRunCellAction**\n'
  48. if self.thought:
  49. ret += f'THOUGHT: {self.thought}\n'
  50. ret += f'CODE:\n{self.code}'
  51. return ret
  52. @property
  53. def message(self) -> str:
  54. return f'Running Python code interactively: {self.code}'