commands.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. hidden: bool = False
  25. action: str = ActionType.RUN
  26. runnable: ClassVar[bool] = True
  27. confirmation_state: ActionConfirmationStatus = ActionConfirmationStatus.CONFIRMED
  28. security_risk: ActionSecurityRisk | None = None
  29. @property
  30. def message(self) -> str:
  31. return f'Running command: {self.command}'
  32. def __str__(self) -> str:
  33. ret = f'**CmdRunAction (source={self.source})**\n'
  34. if self.thought:
  35. ret += f'THOUGHT: {self.thought}\n'
  36. ret += f'COMMAND:\n{self.command}'
  37. return ret
  38. @dataclass
  39. class IPythonRunCellAction(Action):
  40. code: str
  41. thought: str = ''
  42. include_extra: bool = (
  43. True # whether to include CWD & Python interpreter in the output
  44. )
  45. action: str = ActionType.RUN_IPYTHON
  46. runnable: ClassVar[bool] = True
  47. confirmation_state: ActionConfirmationStatus = ActionConfirmationStatus.CONFIRMED
  48. security_risk: ActionSecurityRisk | None = None
  49. kernel_init_code: str = '' # code to run in the kernel (if the kernel is restarted)
  50. def __str__(self) -> str:
  51. ret = '**IPythonRunCellAction**\n'
  52. if self.thought:
  53. ret += f'THOUGHT: {self.thought}\n'
  54. ret += f'CODE:\n{self.code}'
  55. return ret
  56. @property
  57. def message(self) -> str:
  58. return f'Running Python code interactively: {self.code}'