From 5a138749912893a037919963dbb2a90fe1eb6ba8 Mon Sep 17 00:00:00 2001 From: shenchucheng Date: Wed, 10 Jul 2024 11:07:35 +0800 Subject: [PATCH 1/2] terminal async reporter --- metagpt/tools/libs/terminal.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metagpt/tools/libs/terminal.py b/metagpt/tools/libs/terminal.py index 8d44c13e6..c2010ed6f 100644 --- a/metagpt/tools/libs/terminal.py +++ b/metagpt/tools/libs/terminal.py @@ -123,12 +123,12 @@ class Terminal: if ix >= 0: line = line[0:ix] if line: - observer.report(line, "output") + await observer.async_report(line, "output") # report stdout in real-time cmd_output.append(line) return "".join(cmd_output) # log stdout in real-time - observer.report(line, "output") + await observer.async_report(line, "output") cmd_output.append(line) if daemon: await self.stdout_queue.put(line) From 9dd9cf6f559bcda47951addc1d335bb61d1c6642 Mon Sep 17 00:00:00 2001 From: shenchucheng Date: Wed, 10 Jul 2024 15:20:45 +0800 Subject: [PATCH 2/2] redirect stderr to stdout --- metagpt/tools/libs/terminal.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/metagpt/tools/libs/terminal.py b/metagpt/tools/libs/terminal.py index c2010ed6f..a736d476a 100644 --- a/metagpt/tools/libs/terminal.py +++ b/metagpt/tools/libs/terminal.py @@ -1,6 +1,6 @@ import asyncio from asyncio import Queue -from asyncio.subprocess import PIPE +from asyncio.subprocess import PIPE, STDOUT from typing import Optional from metagpt.const import DEFAULT_WORKSPACE_ROOT, SWE_SETUP_PATH @@ -28,7 +28,7 @@ class Terminal: async def _start_process(self): # Start a persistent shell process self.process = await asyncio.create_subprocess_exec( - *self.shell_command, stdin=PIPE, stdout=PIPE, stderr=PIPE, executable="/bin/bash" + *self.shell_command, stdin=PIPE, stdout=PIPE, stderr=STDOUT, executable="/bin/bash" ) await self._check_state() @@ -115,6 +115,7 @@ class Terminal: # '\r' is changed to '\n', resulting in excessive output. tmp = b"" while True: + self.process.communicate() output = tmp + await self.process.stdout.read(1) *lines, tmp = output.splitlines(True) for line in lines: