Skip to content

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

fix: now present the results of failure and success code in different ways.

刘棒棒6cb2910d144c
diff --git a/metagpt/actions/execute_code.py b/metagpt/actions/execute_code.py
index 5b6cba57d..851794b91 100644
--- a/metagpt/actions/execute_code.py
+++ b/metagpt/actions/execute_code.py
@@ -15,14 +15,13 @@ import nbformat
from nbclient import NotebookClient
from nbclient.exceptions import CellTimeoutError, DeadKernelError
from nbformat import NotebookNode
-from nbformat.v4 import new_code_cell, new_output, new_markdown_cell
-from rich.console import Console
-from rich.syntax import Syntax
-from rich.markdown import Markdown
-from rich.panel import Panel
+from nbformat.v4 import new_code_cell, new_markdown_cell, new_output
from rich.box import MINIMAL
+from rich.console import Console, Group
from rich.live import Live
-from rich.console import Group
+from rich.markdown import Markdown
+from rich.panel import Panel
+from rich.syntax import Syntax
from metagpt.actions import Action
from metagpt.logs import logger
@@ -229,7 +228,7 @@ class ExecutePyCode(ExecuteCode, Action):
# code success
outputs = self.parse_outputs(self.nb.cells[-1].outputs)
return truncate(remove_escape_and_color_codes(outputs), is_success=success)
- elif language == 'markdown':
+ elif language == "markdown":
# markdown
self.add_markdown_cell(code)
return code, True
@@ -238,26 +237,27 @@ class ExecutePyCode(ExecuteCode, Action):
def truncate(result: str, keep_len: int = 2000, is_success: bool = True):
- desc = f"Executed code {'successfully' if is_success else 'failed, please reflect the cause of bug and then debug'}"
+ """执行失败的代码, 展示result后keep_len个字符; 执行成功的代码, 展示result前keep_len个字符。"""
+ desc = f"Executed code {'successfully. ' if is_success else 'failed, please reflect the cause of bug and then debug. '}"
if is_success:
- desc += f"Truncated to show only {keep_len} characters\n"
+ desc += f"Truncated to show only first {keep_len} characters\n"
else:
- desc += "Show complete information for you."
+ desc += f"Truncated to show only last {keep_len} characters\n"
if result.startswith(desc):
result = result[len(desc) :]
if len(result) > keep_len:
- result = result[-keep_len:] if not is_success else result
+ result = result[-keep_len:] if not is_success else result[:keep_len]
if not result:
- result = 'No output about your code. Only when importing packages it is normal case. Recap and go ahead.'
+ result = "No output about your code. Only when importing packages it is normal case. Recap and go ahead."
return result, False
if result.strip().startswith("<coroutine object"):
result = "Executed code failed, you need use key word 'await' to run a async code."
return result, False
- return desc + result[:keep_len+500], is_success
+ return desc + result[:keep_len], is_success
return result, is_success
@@ -271,13 +271,13 @@ def remove_escape_and_color_codes(input_str):
def display_markdown(content: str):
# 使用正则表达式逐个匹配代码块
- matches = re.finditer(r'```(.+?)```', content, re.DOTALL)
+ matches = re.finditer(r"```(.+?)```", content, re.DOTALL)
start_index = 0
content_panels = []
# 逐个打印匹配到的文本和代码
for match in matches:
- text_content = content[start_index:match.start()].strip()
- code_content = match.group(0).strip()[3:-3] # Remove triple backticks
+ text_content = content[start_index : match.start()].strip()
+ code_content = match.group(0).strip()[3:-3] # Remove triple backticks
if text_content:
content_panels.append(Panel(Markdown(text_content), box=MINIMAL))