diff --git a/openhands/runtime/client/client.py b/openhands/runtime/client/client.py index 980d48eb84ed..33608277bca7 100644 --- a/openhands/runtime/client/client.py +++ b/openhands/runtime/client/client.py @@ -37,7 +37,6 @@ ErrorObservation, FileReadObservation, FileWriteObservation, - IPythonRunCellObservation, Observation, ) from openhands.events.serialization import event_from_dict, event_to_dict @@ -88,6 +87,9 @@ def __init__( self.lock = asyncio.Lock() self.plugins: dict[str, Plugin] = {} self.browser = BrowserEnv(browsergym_eval_env) + self.last_command = '' + self.last_code = '' + self.is_last_code_error = False @property def initial_pwd(self): @@ -441,11 +443,11 @@ async def run(self, action: CmdRunAction) -> CmdOutputObservation: commands = split_bash_commands(action.command) all_output = '' for command in commands: - # suggest alternative for vim/nano - output = None exit_code = 0 - if command.startswith('cd'): + if command == self.last_command and command in ['ls -l', 'ls -la']: + output = "[Why are you executing the same command twice? What's wrong with you? Please focus 🙏]" + elif command.startswith('cd'): path = command[3:].strip() if self.pwd == path: output = '[You are already in this directory.]' @@ -475,6 +477,7 @@ async def run(self, action: CmdRunAction) -> CmdOutputObservation: keep_prompt=action.keep_prompt, kill_on_timeout=False if not action.blocking else True, ) + self.last_command = command if command.startswith('pip install'): output = await self.parse_pip_output(command, output) if all_output: @@ -571,12 +574,19 @@ async def run_ipython(self, action: IPythonRunCellAction) -> Observation: # current working directory in Bash if self.pwd != getattr(self, '_jupyter_pwd', None): await self.chdir() - - action.code = action.code.replace('!pip', '%pip') - obs: IPythonRunCellObservation = await _jupyter_plugin.run(action) - if 'pip install' in action.code: - obs.content = await self.parse_pip_output(action.code, obs.content) - obs.content = obs.content.rstrip() + if action.code == self.last_code and self.is_last_code_error: + obs: Observation = ErrorObservation( + '[You are trying to run the same code cell twice. ' + 'Please focus and run a correct code cell.]' + ) + else: + action.code = action.code.replace('!pip', '%pip') + obs = await _jupyter_plugin.run(action) + if 'pip install' in action.code: + obs.content = await self.parse_pip_output(action.code, obs.content) + obs.content = obs.content.rstrip() + self.last_code = action.code + self.is_last_code_error = 'error' in obs.content # obs.content += f'\n[Jupyter current working directory: {self.pwd}]' # obs.content += f'\n[Jupyter Python interpreter: {_jupyter_plugin.python_interpreter_path}]' return obs diff --git a/upd_persistent_container.py b/upd_persistent_container.py index 0cf40c871fbe..6c4143c54b9a 100644 --- a/upd_persistent_container.py +++ b/upd_persistent_container.py @@ -2,8 +2,8 @@ import docker -full_file_name = r'openhands/runtime/client/client.py' full_file_name = 'openhands/runtime/plugins/agent_skills/file_ops/file_ops.py' +full_file_name = r'openhands/runtime/client/client.py' client = docker.from_env() for c in client.containers.list(): if 'persisted' in c.name: