Merge branch 'fixbug/terminal' into 'mgx_ops'

fixbug: terminal

See merge request pub/MetaGPT!31
This commit is contained in:
洪思睿 2024-04-12 05:57:20 +00:00
commit 43ccb23189
3 changed files with 16 additions and 3 deletions

View file

@ -26,7 +26,7 @@ class ToolLogItem(BaseModel):
TOOL_LOG_END_MARKER = ToolLogItem(
type="str", name="end_marker", value="#END#"
type="str", name="end_marker", value="\x18\x19\x1B\x18"
) # A special log item to suggest the end of a stream log

View file

@ -39,7 +39,7 @@ class Terminal:
# Send the command
self.process.stdin.write(cmd + self.command_terminator)
self.process.stdin.write(
f'echo "{TOOL_LOG_END_MARKER.value}"' + self.command_terminator
f'echo "{TOOL_LOG_END_MARKER.value}"' + self.command_terminator # write EOF
) # Unique marker to signal command end
self.process.stdin.flush()
log_tool_output(
@ -49,7 +49,14 @@ class Terminal:
# Read the output until the unique marker is found
while True:
line = self.process.stdout.readline()
if line.strip() == TOOL_LOG_END_MARKER.value:
ix = line.rfind(TOOL_LOG_END_MARKER.value)
if ix >= 0:
line = line[0:ix]
if line:
log_tool_output(
output=ToolLogItem(name="output", value=line), tool_name="Terminal"
) # log stdout in real-time
cmd_output.append(line)
log_tool_output(TOOL_LOG_END_MARKER)
break
log_tool_output(

View file

@ -1,3 +1,5 @@
import pytest
from metagpt.const import DATA_PATH, METAGPT_ROOT
from metagpt.tools.libs.terminal import Terminal
@ -13,3 +15,7 @@ def test_terminal():
terminal.run_command("cd data")
output = terminal.run_command("pwd")
assert output.strip() == str(DATA_PATH)
if __name__ == "__main__":
pytest.main([__file__, "-s"])