🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming
Merge branch 'feature-terminal' into 'mgx_ops'
洪思睿19c29b0613ca
diff --cc metagpt/tools/libs/terminal.pyindex 351aa4f6f,ee0d155cb..e8b3043ab--- a/metagpt/tools/libs/terminal.py+++ b/metagpt/tools/libs/terminal.py@@@ -39,9 -57,41 +57,41 @@@ class Terminal# Send the commandself.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 endself.process.stdin.flush()+ if daemon:+ threading.Thread(target=self._read_and_process_output, args=(cmd,), daemon=True).start()+ return ""+ else:+ return self._read_and_process_output(cmd)++ def execute_in_conda_env(self, cmd: str, env, daemon=False) -> str:+ """+ Executes a given command within a specified Conda environment automatically without+ the need for manual activation. Users just need to provide the name of the Conda+ environment and the command to execute.++ Args:+ cmd (str): The command to execute within the Conda environment.+ env (str, optional): The name of the Conda environment to activate before executing the command.+ If not specified, the command will run in the current active environment.+ daemon (bool): If True, the command is run in a background thread, similar to `run_command`,+ affecting error logging and handling in the same manner.++ Returns:+ str: The command's output, or an empty string if `daemon` is True, with output processed+ asynchronously in that case.++ Note:+ This function wraps `run_command`, prepending the necessary Conda activation commands+ to ensure the specified environment is active for the command's execution.+ """+ cmd = f"conda run -n {env} {cmd}"+ return self.run_command(cmd, daemon=daemon)++ def _read_and_process_output(self, cmd):+ cmd_output = []log_tool_output(output=ToolLogItem(name="cmd", value=cmd + self.command_terminator), tool_name="Terminal") # log the command@@@ -49,20 -99,13 +99,20 @@@# Read the output until the unique marker is foundwhile 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(- output=ToolLogItem(name="output", value=line), tool_name="Terminal"- ) # log stdout in real-time+ # log stdout in real-time+ log_tool_output(output=ToolLogItem(name="output", value=line), tool_name="Terminal")cmd_output.append(line)+ self.stdout_queue.put(line)return "".join(cmd_output)