From 27c731d11a94901d4b51dbfe57042ee1a9681b24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Tue, 21 Nov 2023 15:05:23 +0800 Subject: [PATCH] feat: archive --- metagpt/actions/prepare_documents.py | 12 +++++++++++- metagpt/environment.py | 9 ++++++++- metagpt/roles/product_manager.py | 11 ++++++++++- metagpt/roles/role.py | 4 ++++ 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/metagpt/actions/prepare_documents.py b/metagpt/actions/prepare_documents.py index 7cf05c5d1..b0185996b 100644 --- a/metagpt/actions/prepare_documents.py +++ b/metagpt/actions/prepare_documents.py @@ -12,4 +12,14 @@ from metagpt.actions import Action class PrepareDocuments(Action): def __init__(self, name="", context=None, llm=None): - pass + super().__init__(name, context, llm) + + async def run(self, with_message, **kwargs): + parent = self.context.get("parent") + if not parent: + raise ValueError("Invalid owner") + env = parent.get_env() + if env.git_repository: + return + env.git_repository = GitRepository() + env.git_repository.open(WORKS) diff --git a/metagpt/environment.py b/metagpt/environment.py index b3c296dac..df93a818b 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -12,7 +12,7 @@ functionality is to be consolidated into the `Environment` class. """ import asyncio -from typing import Iterable, Set +from typing import Iterable, Optional, Set from pydantic import BaseModel, Field @@ -20,6 +20,7 @@ from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message from metagpt.utils.common import is_subscribed +from metagpt.utils.git_repository import GitRepository class Environment(BaseModel): @@ -31,6 +32,7 @@ class Environment(BaseModel): roles: dict[str, Role] = Field(default_factory=dict) consumers: dict[Role, Set] = Field(default_factory=dict) history: str = Field(default="") # For debug + git_repository: Optional[GitRepository] = None class Config: arbitrary_types_allowed = True @@ -111,3 +113,8 @@ class Environment(BaseModel): def set_subscription(self, obj, tags): """Set the labels for message to be consumed by the object""" self.consumers[obj] = tags + + def dict(self, *args, **kwargs): + """Generate a dictionary representation of the model, optionally specifying which fields to include or + exclude.""" + return super(Environment, self).dict(exclude={"git_repository"}) diff --git a/metagpt/roles/product_manager.py b/metagpt/roles/product_manager.py index a58ea5385..c10aba6d1 100644 --- a/metagpt/roles/product_manager.py +++ b/metagpt/roles/product_manager.py @@ -6,6 +6,7 @@ @File : product_manager.py """ from metagpt.actions import BossRequirement, WritePRD +from metagpt.actions.prepare_documents import PrepareDocuments from metagpt.roles import Role @@ -37,5 +38,13 @@ class ProductManager(Role): constraints (str): Constraints or limitations for the product manager. """ super().__init__(name, profile, goal, constraints) - self._init_actions([WritePRD]) + self._init_actions([PrepareDocuments(context={"parent": self}), WritePRD]) self._watch([BossRequirement]) + + async def _think(self) -> None: + """Decide what to do""" + if self._rc.env.git_repository: + self._set_state(1) + else: + self._set_state(0) + return self._rc.todo diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 2e3bcbbd5..d1e65a4e0 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -164,6 +164,10 @@ class Role: if env: env.set_subscription(self, self._subscription) + def get_env(self): + """Return the environment in which the role works.""" + return self._rc.env + @property def profile(self): """Get the role description (position)"""