bash.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import os
  2. import pathlib
  3. from dataclasses import dataclass
  4. from typing import TYPE_CHECKING
  5. from opendevin import config
  6. from opendevin.schema import ActionType, ConfigType
  7. from .base import ExecutableAction
  8. if TYPE_CHECKING:
  9. from opendevin.controller import AgentController
  10. from opendevin.observation import CmdOutputObservation, Observation
  11. from opendevin.observation import IPythonRunCellObservation
  12. @dataclass
  13. class CmdRunAction(ExecutableAction):
  14. command: str
  15. background: bool = False
  16. thought: str = ''
  17. action: str = ActionType.RUN
  18. async def run(self, controller: 'AgentController') -> 'Observation':
  19. return controller.action_manager.run_command(self.command, self.background)
  20. @property
  21. def message(self) -> str:
  22. return f'Running command: {self.command}'
  23. def __str__(self) -> str:
  24. ret = '**CmdRunAction**\n'
  25. if self.thought:
  26. ret += f'THOUGHT:{self.thought}\n'
  27. ret += f'COMMAND:\n{self.command}'
  28. return ret
  29. @dataclass
  30. class CmdKillAction(ExecutableAction):
  31. id: int
  32. thought: str = ''
  33. action: str = ActionType.KILL
  34. async def run(self, controller: 'AgentController') -> 'CmdOutputObservation':
  35. return controller.action_manager.kill_command(self.id)
  36. @property
  37. def message(self) -> str:
  38. return f'Killing command: {self.id}'
  39. def __str__(self) -> str:
  40. return f'**CmdKillAction**\n{self.id}'
  41. @dataclass
  42. class IPythonRunCellAction(ExecutableAction):
  43. code: str
  44. thought: str = ''
  45. action: str = ActionType.RUN_IPYTHON
  46. async def run(self, controller: 'AgentController') -> 'IPythonRunCellObservation':
  47. # echo "import math" | execute_cli
  48. # write code to a temporary file and pass it to `execute_cli` via stdin
  49. tmp_filepath = os.path.join(
  50. config.get(ConfigType.WORKSPACE_BASE),
  51. '.tmp', '.ipython_execution_tmp.py'
  52. )
  53. pathlib.Path(os.path.dirname(tmp_filepath)).mkdir(parents=True, exist_ok=True)
  54. with open(tmp_filepath, 'w') as tmp_file:
  55. tmp_file.write(self.code)
  56. tmp_filepath_inside_sandbox = os.path.join(
  57. config.get(ConfigType.WORKSPACE_MOUNT_PATH_IN_SANDBOX),
  58. '.tmp', '.ipython_execution_tmp.py'
  59. )
  60. obs = controller.action_manager.run_command(
  61. f'execute_cli < {tmp_filepath_inside_sandbox}',
  62. background=False
  63. )
  64. return IPythonRunCellObservation(
  65. content=obs.content,
  66. code=self.code
  67. )
  68. def __str__(self) -> str:
  69. ret = '**IPythonRunCellAction**\n'
  70. if self.thought:
  71. ret += f'THOUGHT:{self.thought}\n'
  72. ret += f'CODE:\n{self.code}'
  73. return ret
  74. @property
  75. def message(self) -> str:
  76. return f'Running Python code interactively: {self.code}'