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

feat: tee command output to stdout/stderr in /execute_action endpoint #5267

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions openhands/runtime/action_execution_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ async def run(
self, action: CmdRunAction
) -> CmdOutputObservation | ErrorObservation:
obs = await call_sync_from_async(self.bash_session.run, action)
if isinstance(obs, CmdOutputObservation):
print(obs.content, flush=True)
return obs

async def run_ipython(self, action: IPythonRunCellAction) -> Observation:
Expand Down Expand Up @@ -203,6 +205,7 @@ async def run_ipython(self, action: IPythonRunCellAction) -> Observation:
f'\n[Jupyter current working directory: {self.bash_session.pwd}]'
)
obs.content += f'\n[Jupyter Python interpreter: {_jupyter_plugin.python_interpreter_path}]'
print(obs.content, flush=True)
return obs
else:
raise RuntimeError(
Expand Down
20 changes: 17 additions & 3 deletions openhands/runtime/utils/bash.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,24 @@ def _continue_bash(
kill_on_timeout: bool = True,
) -> tuple[str, int]:
logger.debug(f'Continuing bash with timeout={timeout}')
output = ''
try:
self.shell.expect(self.__bash_expect_regex, timeout=timeout)

output = self.shell.before
# Instead of waiting for the full output, read character by character
while True:
try:
# Try to match the prompt pattern
index = self.shell.expect([self.__bash_expect_regex, '.'], timeout=0.1)
if index == 0: # Found the prompt pattern
break
elif index == 1: # Found a character
char = self.shell.after
output += char
print(char, end='', flush=True) # Print in real-time
except pexpect.TIMEOUT:
# No output available, continue waiting
continue
except pexpect.EOF:
break

# Get exit code
self.shell.sendline('echo $?')
Expand Down
Loading