implement execute_cli via file cp

This commit is contained in:
Xingyao Wang
2024-04-25 18:19:00 +08:00
parent 6e0fed5e78
commit 06f0155bc1
2 changed files with 25 additions and 17 deletions

View File

@@ -1,11 +1,11 @@
import os
import pathlib
import tempfile
from dataclasses import dataclass
from typing import TYPE_CHECKING
from .base import ExecutableAction
from opendevin import config
from opendevin.schema import ActionType, ConfigType
from opendevin.schema import ActionType
from opendevin.logger import opendevin_logger as logger
if TYPE_CHECKING:
from opendevin.controller import AgentController
@@ -57,24 +57,30 @@ class IPythonRunCellAction(ExecutableAction):
action: str = ActionType.RUN
async def run(self, controller: 'AgentController') -> 'CmdOutputObservation':
# echo "import math" | execute_cli
# write code to a temporary file and pass it to `execute_cli` via stdin
tmp_filepath = os.path.join(
config.get(ConfigType.WORKSPACE_BASE),
'.tmp', '.execution_tmp.py'
)
pathlib.Path(os.path.dirname(tmp_filepath)).mkdir(parents=True, exist_ok=True)
with open(tmp_filepath, 'w') as tmp_file:
tmp_file.write(self.code)
# create a temporary file
tmp_file = tempfile.NamedTemporaryFile(mode='w', delete=False)
tmp_filepath = tmp_file.name
tmp_file.write(self.code)
tmp_file.close()
tmp_filepath_inside_sandbox = os.path.join(
config.get(ConfigType.WORKSPACE_MOUNT_PATH_IN_SANDBOX),
'.tmp', '.execution_tmp.py'
# move the file to the sandbox
controller.action_manager.sandbox.copy_to(
tmp_filepath,
'/tmp/.execution_tmp.py'
)
return controller.action_manager.run_command(
f'execute_cli < {tmp_filepath_inside_sandbox}',
ret = controller.action_manager.run_command(
'execute_cli < /tmp/.execution_tmp.py',
background=False
)
_delete_res = controller.action_manager.run_command(
'rm /tmp/.execution_tmp.py',
background=False
)
if _delete_res.exit_code != 0:
logger.warning(f'Failed to delete temporary file for Jupyter: {_delete_res.content}')
# remove the temporary file on the host
os.remove(tmp_filepath)
return ret
def __str__(self) -> str:
ret = '**IPythonRunCellAction**\n'

View File

@@ -22,6 +22,8 @@ pip install jupyterlab notebook jupyter_kernel_gateway
# Create logs directory
sudo mkdir -p /opendevin/logs && sudo chmod 777 /opendevin/logs
# Create tmp directory
sudo mkdir -p /tmp && sudo chmod 777 /tmp
# Run background process to start jupyter kernel gateway
export JUPYTER_GATEWAY_PORT=18888