Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restart Jupyter kernel if package is installed via bash too #3178

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions agenthub/codeact_agent/action_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ def __init__(
self,
):
self.python_code = None
self.jupyter_kernel_init_code: str = 'from agentskills import *'

def check_condition(self, action_str: str) -> bool:
self.python_code = re.search(
Expand All @@ -128,7 +127,6 @@ def parse(self, action_str: str) -> Action:
return IPythonRunCellAction(
code=code_group,
thought=thought,
kernel_init_code=self.jupyter_kernel_init_code,
)


Expand Down
2 changes: 0 additions & 2 deletions agenthub/codeact_swe_agent/action_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ def __init__(
self,
):
self.python_code = None
self.jupyter_kernel_init_code: str = 'from agentskills import *'

def check_condition(self, action_str: str) -> bool:
self.python_code = re.search(
Expand All @@ -87,7 +86,6 @@ def parse(self, action_str: str) -> Action:
return IPythonRunCellAction(
code=code_group,
thought=thought,
kernel_init_code=self.jupyter_kernel_init_code,
)


Expand Down
1 change: 0 additions & 1 deletion opendevin/events/action/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class IPythonRunCellAction(Action):
action: str = ActionType.RUN_IPYTHON
runnable: ClassVar[bool] = True
is_confirmed: ActionConfirmationStatus = ActionConfirmationStatus.CONFIRMED
kernel_init_code: str = '' # code to run in the kernel (if the kernel is restarted)

def __str__(self) -> str:
ret = '**IPythonRunCellAction**\n'
Expand Down
125 changes: 64 additions & 61 deletions opendevin/runtime/server/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,63 +113,75 @@ def init_runtime_tools(
async def run(self, action: CmdRunAction) -> Observation:
return self._run_command(action.command)

def restart_kernel(self) -> str:
if self.config.default_agent in ['CodeActAgent', 'CodeActSWEAgent']:
kernel_init_code = 'from agentskills import *'
else:
return ''

restart_kernel_code = (
'import IPython\nIPython.Application.instance().kernel.do_shutdown(True)'
)
self._run_command(
(
"cat > /tmp/opendevin_jupyter_temp.py <<'EOL'\n"
f'{restart_kernel_code}\n'
'EOL'
)
)
obs = self._run_command('cat /tmp/opendevin_jupyter_temp.py | execute_cli')
output = obs.content
if "{'status': 'ok', 'restart': True}" != output.strip():
print(output)
output = '\n[Failed to restart the kernel]'
else:
output = '\n[Kernel restarted successfully]'

# re-init the kernel after restart
self._run_command(
(
f"cat > /tmp/opendevin_jupyter_init.py <<'EOL'\n"
f'{kernel_init_code}\n'
'EOL'
),
)
self._run_command(
'cat /tmp/opendevin_jupyter_init.py | execute_cli',
)
return output

def parse_pip_output(self, code, output) -> str:
print(output)
package_names = code.split(' ', 2)[-1]
is_single_package = ' ' not in package_names

if 'Successfully installed' in output:
output = '[Package installed successfully]'
if (
'Note: you may need to restart the kernel to use updated packages.'
in output
):
output += self.restart_kernel()
else:
# restart kernel if installed via bash too
self.restart_kernel()
elif (
is_single_package
and f'Requirement already satisfied: {package_names}' in output
):
output = '[Package already installed]'

return output

async def run_ipython(self, action: IPythonRunCellAction) -> Observation:
self._run_command(
f"cat > /tmp/opendevin_jupyter_temp.py <<'EOL'\n{action.code}\nEOL"
)

# run the code
obs = self._run_command('cat /tmp/opendevin_jupyter_temp.py | execute_cli')
output = obs.content
if 'pip install' in action.code:
print(output)
package_names = action.code.split(' ', 2)[-1]
is_single_package = ' ' not in package_names

if 'Successfully installed' in output:
restart_kernel = 'import IPython\nIPython.Application.instance().kernel.do_shutdown(True)'
if (
'Note: you may need to restart the kernel to use updated packages.'
in output
):
self._run_command(
(
"cat > /tmp/opendevin_jupyter_temp.py <<'EOL'\n"
f'{restart_kernel}\n'
'EOL'
)
)
obs = self._run_command(
'cat /tmp/opendevin_jupyter_temp.py | execute_cli'
)
output = '[Package installed successfully]'
if "{'status': 'ok', 'restart': True}" != obs.content.strip():
print(obs.content)
output += (
'\n[But failed to restart the kernel to load the package]'
)
else:
output += (
'\n[Kernel restarted successfully to load the package]'
)

# re-init the kernel after restart
if action.kernel_init_code:
self._run_command(
(
f"cat > /tmp/opendevin_jupyter_init.py <<'EOL'\n"
f'{action.kernel_init_code}\n'
'EOL'
),
)
obs = self._run_command(
'cat /tmp/opendevin_jupyter_init.py | execute_cli',
)
elif (
is_single_package
and f'Requirement already satisfied: {package_names}' in output
):
output = '[Package already installed]'
action.code = action.code.replace('!pip', '%pip')
output = self.parse_pip_output(action.code, output)
return IPythonRunCellObservation(content=output, code=action.code)

async def read(self, action: FileReadAction) -> Observation:
Expand Down Expand Up @@ -206,17 +218,8 @@ async def browse_interactive(self, action: BrowseInteractiveAction) -> Observati
def _run_command(self, command: str) -> Observation:
try:
exit_code, output = self.sandbox.execute(command)
if 'pip install' in command:
package_names = command.split(' ', 2)[-1]
is_single_package = ' ' not in package_names
print(output)
if 'Successfully installed' in output:
output = '[Package installed successfully]'
elif (
is_single_package
and f'Requirement already satisfied: {package_names}' in output
):
output = '[Package already installed]'
if command.startswith('pip install'):
output = self.parse_pip_output(command, output)
return CmdOutputObservation(
command_id=-1, content=str(output), command=command, exit_code=exit_code
)
Expand Down