From 53e593baa4500d46e1a64d476b9ce68a7f8e5eb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 28 Mar 2024 15:18:23 +0800 Subject: [PATCH] feat: +init python folder --- metagpt/roles/engineer.py | 14 +++++++++++++- metagpt/utils/common.py | 8 ++++++++ tests/metagpt/actions/test_import_repo.py | 9 ++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 1cd180793..6a4f905ad 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -22,7 +22,7 @@ from __future__ import annotations import json from collections import defaultdict from pathlib import Path -from typing import Set +from typing import List, Set from metagpt.actions import Action, WriteCode, WriteCodeReview, WriteTasks from metagpt.actions.fix_bug import FixBug @@ -50,6 +50,7 @@ from metagpt.utils.common import ( any_to_str, any_to_str_set, get_project_srcs_path, + init_python_folder, ) IS_PASS_PROMPT = """ @@ -310,6 +311,7 @@ class Engineer(Role): task_doc = await self.project_repo.docs.task.get(filename) code_plan_and_change_doc = await self.project_repo.docs.code_plan_and_change.get(filename) task_list = self._parse_tasks(task_doc) + await self._init_python_folder(task_list) for task_filename in task_list: old_code_doc = await self.project_repo.srcs.get(task_filename) if not old_code_doc: @@ -363,6 +365,8 @@ class Engineer(Role): ctx = CodeSummarizeContext.loads(filenames=list(dependencies)) summarizations[ctx].append(filename) for ctx, filenames in summarizations.items(): + if not ctx.design_filename or not ctx.task_filename: + continue # cause by `__init__.py` which is created by `init_python_folder` ctx.codes_filenames = filenames new_summarize = SummarizeCode(i_context=ctx, context=self.context, llm=self.llm) for i, act in enumerate(self.summarize_todos): @@ -388,3 +392,11 @@ class Engineer(Role): def action_description(self) -> str: """AgentStore uses this attribute to display to the user what actions the current role should take.""" return self.next_todo_action + + async def _init_python_folder(self, task_list: List[str]): + for i in task_list: + filename = Path(i) + if filename.suffix != ".py": + continue + workdir = self.src_workspace / filename.parent + await init_python_folder(workdir) diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index 22bfc356e..f7b0cbff5 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -889,3 +889,11 @@ def get_project_srcs_path(workdir: str | Path) -> Path: else: src_name = Path(workdir).name return Path(workdir) / src_name + + +async def init_python_folder(workdir: str | Path): + init_filename = Path(workdir) / "__init__.py" + if init_filename.exists(): + return + async with aiofiles.open(init_filename, "a"): + os.utime(init_filename, None) diff --git a/tests/metagpt/actions/test_import_repo.py b/tests/metagpt/actions/test_import_repo.py index cfe08ee75..d498be039 100644 --- a/tests/metagpt/actions/test_import_repo.py +++ b/tests/metagpt/actions/test_import_repo.py @@ -9,7 +9,14 @@ from metagpt.utils.common import list_files @pytest.mark.asyncio -@pytest.mark.parametrize("repo_path", ["https://github.com/geekan/MetaGPT.git"]) +@pytest.mark.parametrize( + "repo_path", + [ + "https://github.com/spec-first/connexion.git", + # "https://github.com/geekan/MetaGPT.git" + ], +) +@pytest.mark.skip async def test_import_repo(repo_path): context = Context() action = ImportRepo(repo_path=repo_path, context=context)