Files
OpenHands/opendevin/controller/command_manager.py
Robert Brennan 486ec2d94a Minor fixes for new Agent framework (#158)
* add message if exit happens before finish

* fix kill command

* fix browse action

* fix sandbox

* update requirements

* add status code

* refactor controller

* add try to callbacks

* fix background log collection

* format logs a bit more nicely

* run background procs in same container

* fix close command

* rename sockt

* fix up kills

* fix lint issues

* fix ruff

---------

Co-authored-by: Xingyao Wang <xingyao6@illinois.edu>
2024-03-26 12:55:33 +08:00

55 lines
1.9 KiB
Python

from typing import List
from opendevin.observation import CmdOutputObservation
from opendevin.sandbox.sandbox import DockerInteractive
class CommandManager:
def __init__(self, dir):
self.directory = dir
self.shell = DockerInteractive(id="default", workspace_dir=dir)
def run_command(self, command: str, background=False) -> CmdOutputObservation:
if background:
return self._run_background(command)
else:
return self._run_immediately(command)
def _run_immediately(self, command: str) -> CmdOutputObservation:
exit_code, output = self.shell.execute(command)
return CmdOutputObservation(
command_id=-1,
content=output,
command=command,
exit_code=exit_code
)
def _run_background(self, command: str) -> CmdOutputObservation:
bg_cmd = self.shell.execute_in_background(command)
return CmdOutputObservation(
content=f"Background command started. To stop it, send a `kill` action with id {bg_cmd.id}",
command_id=bg_cmd.id,
command=command,
exit_code=0
)
def kill_command(self, id: int) -> CmdOutputObservation:
cmd = self.shell.kill_background(id)
return CmdOutputObservation(
content=f"Background command with id {id} has been killed.",
command_id=id,
command=cmd.command,
exit_code=0
)
def get_background_obs(self) -> List[CmdOutputObservation]:
obs = []
for _id, cmd in self.shell.background_commands.items():
output = cmd.read_logs()
if output is not None and output != "":
obs.append(
CmdOutputObservation(
content=output, command_id=_id, command=cmd.command
)
)
return obs