commands.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. keep_prompt: bool = True
  14. # if True, the command prompt will be kept in the command output observation
  15. # Example of command output:
  16. # root@sandbox:~# ls
  17. # file1.txt
  18. # file2.txt
  19. # root@sandbox:~# <-- this is the command prompt
  20. action: str = ActionType.RUN
  21. runnable: ClassVar[bool] = True
  22. is_confirmed: ActionConfirmationStatus = ActionConfirmationStatus.CONFIRMED
  23. security_risk: ActionSecurityRisk | None = None
  24. @property
  25. def message(self) -> str:
  26. return f'Running command: {self.command}'
  27. def __str__(self) -> str:
  28. ret = f'**CmdRunAction (source={self.source})**\n'
  29. if self.thought:
  30. ret += f'THOUGHT: {self.thought}\n'
  31. ret += f'COMMAND:\n{self.command}'
  32. return ret
  33. @dataclass
  34. class IPythonRunCellAction(Action):
  35. code: str
  36. thought: str = ''
  37. action: str = ActionType.RUN_IPYTHON
  38. runnable: ClassVar[bool] = True
  39. is_confirmed: ActionConfirmationStatus = ActionConfirmationStatus.CONFIRMED
  40. security_risk: ActionSecurityRisk | None = None
  41. kernel_init_code: str = '' # code to run in the kernel (if the kernel is restarted)
  42. def __str__(self) -> str:
  43. ret = '**IPythonRunCellAction**\n'
  44. if self.thought:
  45. ret += f'THOUGHT: {self.thought}\n'
  46. ret += f'CODE:\n{self.code}'
  47. return ret
  48. @property
  49. def message(self) -> str:
  50. return f'Running Python code interactively: {self.code}'