From 30831d1e31dd6c5d52e6e709f86b19134ad5b62c Mon Sep 17 00:00:00 2001 From: yzlin Date: Wed, 3 Apr 2024 22:16:17 +0800 Subject: [PATCH] log tasks and files --- metagpt/logs.py | 7 +++++-- metagpt/schema.py | 19 +++++++++++-------- metagpt/tools/libs/file_manager.py | 21 ++++++++++++++++++++- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/metagpt/logs.py b/metagpt/logs.py index f102c1be3..480477e6b 100644 --- a/metagpt/logs.py +++ b/metagpt/logs.py @@ -11,6 +11,7 @@ from __future__ import annotations import sys from datetime import datetime from functools import partial +from typing import Any from loguru import logger as _logger from pydantic import BaseModel, Field @@ -21,7 +22,7 @@ from metagpt.const import METAGPT_ROOT class ToolLogItem(BaseModel): type_: str = Field(alias="type", default="str", description="Data type of `value` field.") name: str - value: str + value: Any TOOL_LOG_END_MARKER = ToolLogItem( @@ -66,4 +67,6 @@ def set_tool_output_logfunc(func): _llm_stream_log = partial(print, end="") -_tool_output_log = lambda output, tool_name: print(output) +_tool_output_log = ( + lambda *args, **kwargs: None +) # a dummy function to avoid errors if set_tool_output_logfunc is not called diff --git a/metagpt/schema.py b/metagpt/schema.py index ebd0e5be3..055fa933f 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -433,13 +433,6 @@ class Plan(BaseModel): final_tasks = self.tasks[:prefix_length] + new_tasks[prefix_length:] self.tasks = final_tasks - log_tool_output( - ToolLogItem( - name="output", value="\n\n".join([f"Task {task.task_id}: {task.instruction}" for task in self.tasks]) - ), - tool_name="Plan", - ) - # Update current_task_id to the first unfinished task in the merged list self._update_current_task() @@ -483,6 +476,8 @@ class Plan(BaseModel): if new_task.task_id in task.dependent_task_ids: self.reset_task(task.task_id) + self._update_current_task() + def append_task(self, new_task: Task): """ Append a new task to the end of existing task sequences @@ -513,7 +508,15 @@ class Plan(BaseModel): if not task.is_finished: current_task_id = task.task_id break - self.current_task_id = current_task_id # all tasks finished + self.current_task_id = current_task_id + + log_tool_output( + [ + ToolLogItem(type="object", name="tasks", value=self.tasks), + ToolLogItem(type="object", name="current_task_id", value=self.current_task_id), + ], + tool_name="Plan", + ) @property def current_task(self) -> Task: diff --git a/metagpt/tools/libs/file_manager.py b/metagpt/tools/libs/file_manager.py index aae5a6875..75c173d5e 100644 --- a/metagpt/tools/libs/file_manager.py +++ b/metagpt/tools/libs/file_manager.py @@ -4,6 +4,7 @@ import subprocess from pydantic import BaseModel, Field +from metagpt.logs import ToolLogItem, log_tool_output from metagpt.tools.tool_registry import register_tool @@ -26,11 +27,13 @@ class FileManager: """Write the whole content to a file.""" with open(path, "w") as f: f.write(content) + log_tool_output(ToolLogItem(name="write_file_path", value=path), tool_name="FileManager") def read(self, path: str) -> str: """Read the whole content of a file.""" with open(path, "r") as f: return f.read() + log_tool_output(ToolLogItem(name="read_file_path", value=path), tool_name="FileManager") def search_content(self, symbol: str, root_path: str = "", window: int = 20) -> FileBlock: """ @@ -67,7 +70,7 @@ class FileManager: start = max(i - window, 0) end = min(i + window, len(lines) - 1) block_content = "".join(lines[start : end + 1]) - return FileBlock( + result = FileBlock( file_path=file_path, block_content=block_content, block_start_line=start + 1, @@ -75,6 +78,11 @@ class FileManager: symbol=symbol, symbol_line=i + 1, ) + log_tool_output( + ToolLogItem(type="object", name="file_block_searched", value=result), + tool_name="FileManager", + ) + return result return None def write_content(self, file_path: str, start_line: int, end_line: int, new_block_content: str = "") -> str: @@ -109,6 +117,17 @@ class FileManager: # If linting passes, overwrite the original file with the temporary file shutil.move(temp_file_path, file_path) + + new_file_block = FileBlock( + file_path=file_path, + block_content=new_block_content, + block_start_line=start_line, + block_end_line=-1 if end_line < start_line else start_line + new_block_content.count("\n"), + ) + log_tool_output( + ToolLogItem(type="object", name="file_block_written", value=new_file_block), tool_name="FileManager" + ) + return f"Content written successfully to {file_path}" finally: