Skip to content

🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming

terminal stream opt

shenchuchengcaa42ff7c178
diff --git a/metagpt/tools/libs/terminal.py b/metagpt/tools/libs/terminal.py
index 5f5989e1a..85aacbef8 100644
--- a/metagpt/tools/libs/terminal.py
+++ b/metagpt/tools/libs/terminal.py
@@ -26,8 +26,6 @@ class Terminal:
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
- text=True,
- bufsize=1, # Line buffered
executable="/bin/bash",
)
self.stdout_queue = Queue()
@@ -58,9 +56,9 @@ class Terminal:
"""
# Send the command
- self.process.stdin.write(cmd + self.command_terminator)
+ self.process.stdin.write((cmd + self.command_terminator).encode())
self.process.stdin.write(
- f'echo "{END_MARKER_VALUE}"' + self.command_terminator # write EOF
+ (f'echo "{END_MARKER_VALUE}"{self.command_terminator}').encode() # write EOF
) # Unique marker to signal command end
self.process.stdin.flush()
if daemon:
@@ -100,22 +98,24 @@ class Terminal:
# report the command
# Read the output until the unique marker is found
+ tmp = b""
while True:
- line = self.process.stdout.readline()
- ix = line.rfind(END_MARKER_VALUE)
- if ix >= 0:
- line = line[0:ix]
- if line:
- observer.report(line, "output")
- # report stdout in real-time
- cmd_output.append(line)
- break
- # log stdout in real-time
- observer.report(line, "output")
- cmd_output.append(line)
- self.stdout_queue.put(line)
-
- return "".join(cmd_output)
+ output = tmp + self.process.stdout.read(1)
+ *lines, tmp = output.splitlines(True)
+ for line in lines:
+ line = line.decode()
+ ix = line.rfind(END_MARKER_VALUE)
+ if ix >= 0:
+ line = line[0:ix]
+ if line:
+ observer.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")
+ cmd_output.append(line)
+ self.stdout_queue.put(line)
def close(self):
"""Close the persistent shell process."""
diff --git a/metagpt/utils/report.py b/metagpt/utils/report.py
index 20f9ccd01..a61c77381 100644
--- a/metagpt/utils/report.py
+++ b/metagpt/utils/report.py
@@ -42,7 +42,7 @@ class BlockType(str, Enum):
END_MARKER_NAME = "end_marker"
-END_MARKER_VALUE = "\x18\x19\x1B\x18"
+END_MARKER_VALUE = "\x18\x19\x1B\x18\n"
class ResourceReporter(BaseModel):