🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming
Move cmd restrictions from RoleZero to the terminal
黄伟韬86ad3edacaee
diff --git a/metagpt/roles/di/engineer2.py b/metagpt/roles/di/engineer2.pyindex 2ff696a54..2fb9d05ee 100644--- a/metagpt/roles/di/engineer2.py+++ b/metagpt/roles/di/engineer2.py@@ -34,8 +34,6 @@ class Engineer2(RoleZero):goal: str = "Take on game, app, and web development."instruction: str = ENGINEER2_INSTRUCTIONterminal: Terminal = Field(default_factory=Terminal, exclude=True)- # The cmd in forbidden_terminal_commands will be replace by pass ana return the advise.- forbidden_terminal_commands: dict = {"npm run dev": "Use Deployer.deploy_to_public instead."}deployer: Deployer = Field(default_factory=Deployer)tools: list[str] = ["Plan",diff --git a/metagpt/roles/di/role_zero.py b/metagpt/roles/di/role_zero.pyindex 175309523..c284db702 100644--- a/metagpt/roles/di/role_zero.py+++ b/metagpt/roles/di/role_zero.py@@ -85,8 +85,6 @@ class RoleZero(Role):"Editor.append_file","Editor.open_file",]- # The cmd in forbidden_terminal_commands will be replace by pass ana return the advise. {"cmd":"forbidden_reason/advice"}- forbidden_terminal_commands: dict = {}# Equipped with three basic tools by default for optional useeditor: Editor = Editor(enable_auto_lint=True)browser: Browser = Browser()@@ -546,16 +544,8 @@ class RoleZero(Role):return human_response# output from bash.run may be empty, add decorations to the output to ensure visibility.elif cmd["command_name"] == "Terminal.run_command":- tool_output = ""- # Remove forbidden commands- if any([forbidden_cmd in cmd["args"]["cmd"] for forbidden_cmd in self.forbidden_terminal_commands.keys()]):- for cmd_name, reason in self.forbidden_terminal_commands.items():- # 'true' is a pass command in linux terminal.- cmd["args"]["cmd"] = cmd["args"]["cmd"].replace(cmd_name, "true")- tool_output += f"{cmd_name} is failed to executed. {reason}\n"-tool_obj = self.tool_execution_map[cmd["command_name"]]- tool_output += await tool_obj(**cmd["args"])+ tool_output = await tool_obj(**cmd["args"])if len(tool_output) <= 10:command_output += (f"\n[command]: {cmd['args']['cmd']} \n[command output] : {tool_output} (pay attention to this.)"diff --git a/metagpt/strategy/experience_retriever.py b/metagpt/strategy/experience_retriever.pyindex d1772dc22..794fe19f3 100644--- a/metagpt/strategy/experience_retriever.py+++ b/metagpt/strategy/experience_retriever.py@@ -839,11 +839,11 @@ Explanation: I will first need to read the system design document and the projec}]```-## example 2.1+## example 2User Requirement: Implement the core game project in Vue/React framework.Explanation: This is a project that needs to be implemented using Vue.js. Therefore, I need to copy the Vue/React template to the project folder first.-## example 2.2+## example 3User Requirement: Writing code.The template is :├── public@@ -910,7 +910,7 @@ If the project is a Vue or React Project, install the dependencies after finishi]```-## example 3+## example 4Explanation: Take on one task, such as writing or rewriting a file. Upon completion, finish current task.```json@@ -927,7 +927,7 @@ Explanation: Take on one task, such as writing or rewriting a file. Upon complet}]```-## example 3.2+## example 5Explanation: The project have been completed. And this project is a Vue/React Project,so i will deploy the project to the public.```json@@ -939,7 +939,7 @@ Explanation: The project have been completed. And this project is a Vue/React Pr}}]-## example 3.3+## example 6Explanation: After install the project. I will deploy the project to the public.```json[@@ -951,7 +951,7 @@ Explanation: After install the project. I will deploy the project to the public.}]-## example 4+## example 7I have received a GitHub issue URL.I will use browser to review the detailed information of this issue in order to understand the problem.```json@@ -965,7 +965,7 @@ I will use browser to review the detailed information of this issue in order to]```-## example 6+## example 8I need to locating the `openai_api.py` file, so I will search for the `openai_api.py` file.```json[@@ -980,7 +980,7 @@ I need to locating the `openai_api.py` file, so I will search for the `openai_ap-## example 7+## example 9I have located the openai_api.py file. I want to edit this file, so I will open it first.```json[@@ -993,7 +993,7 @@ I have located the openai_api.py file. I want to edit this file, so I will open]```-## example 8+## example 10I have opened the openai_api.py file. However, the range of lines shown is from 001 to 100, and I want to see more. Therefore, I want to use the scroll_down command to view additional lines.```json[@@ -1004,7 +1004,7 @@ I have opened the openai_api.py file. However, the range of lines shown is from]```-## example 9+## example 11I want to change the key bindings from (w/s) to the arrow keys (up, down). And add the space bar to pause.the previous file look like:142| while not self.is_game_over():@@ -1031,7 +1031,7 @@ Editor tool is exclusive. If I use this tool, I cannot use any other commands in]```-## example 10+## example 12I want to add a score variable in the initialization of the game.the previous file look like:028| if restart:@@ -1063,7 +1063,7 @@ After executing the command, the file will be:033| self.location = (0,0)In the next turn, I will try to add another code snippet-## example 11+## example 13Create a pull request (Optional): Merge the changes from the new branch into the master branch.Thought: Now that the changes have been pushed to the remote repository, due to the user's requirement, let's create a pull request to merge the changes into the master branch.@@ -1084,7 +1084,7 @@ Thought: Now that the changes have been pushed to the remote repository, due to]```-## example 12+## example 14The requirement is to create a product website featuring goods such as caps, dresses, and T-shirts.I believe pictures would improve the site, so I will get the images first.```jsondiff --git a/metagpt/tools/libs/terminal.py b/metagpt/tools/libs/terminal.pyindex a621e4b0e..0150e26e6 100644--- a/metagpt/tools/libs/terminal.py+++ b/metagpt/tools/libs/terminal.py@@ -26,6 +26,11 @@ class Terminal:self.stdout_queue = Queue(maxsize=1000)self.observer = TerminalReporter()self.process: Optional[asyncio.subprocess.Process] = None+ # The cmd in forbidden_terminal_commands will be replace by pass ana return the advise. example:{"cmd":"forbidden_reason/advice"}+ self.forbidden_commands = {+ "npm run dev": "Use Deployer.deploy_to_public instead.",+ "pnpm run dev": "Use Deployer.deploy_to_public instead.",+ }async def _start_process(self):# Start a persistent shell process@@ -60,6 +65,14 @@ class Terminal:if self.process is None:await self._start_process()+ output = ""+ # Remove forbidden commands+ for cmd_name, reason in self.forbidden_commands.items():+ # 'true' is a pass command in linux terminal.+ if cmd_name in cmd:+ cmd = cmd.replace(cmd_name, "true")+ output += f"{cmd_name} is failed to executed. {reason}\n"+# Send the commandself.process.stdin.write((cmd + self.command_terminator).encode())self.process.stdin.write(@@ -68,9 +81,10 @@ class Terminal:await self.process.stdin.drain()if daemon:asyncio.create_task(self._read_and_process_output(cmd))- return ""else:- return await self._read_and_process_output(cmd)+ output += await self._read_and_process_output(cmd)++ return outputasync def execute_in_conda_env(self, cmd: str, env, daemon=False) -> str:"""