🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming
add AttrDict
geekan77938b076c4d
diff --git a/metagpt/context.py b/metagpt/context.pyindex e24e99afc..0556add8a 100644--- a/metagpt/context.py+++ b/metagpt/context.py@@ -7,7 +7,7 @@"""import osfrom pathlib import Path-from typing import Dict, Optional+from typing import Optionalfrom metagpt.config2 import Configfrom metagpt.const import OPTIONS@@ -17,8 +17,27 @@ from metagpt.utils.cost_manager import CostManagerfrom metagpt.utils.git_repository import GitRepository+class AttrDict:+ def __init__(self, d=None):+ if d is None:+ d = {}+ self.__dict__["_dict"] = d++ def __getattr__(self, key):+ return self._dict.get(key, None)++ def __setattr__(self, key, value):+ self._dict[key] = value++ def __delattr__(self, key):+ if key in self._dict:+ del self._dict[key]+ else:+ raise AttributeError(f"No such attribute: {key}")++class Context:- kwargs: Dict = {}+ kwargs: AttrDict = {}config: Config = Config.default()git_repo: Optional[GitRepository] = Nonesrc_workspace: Optional[Path] = Nonediff --git a/metagpt/roles/assistant.py b/metagpt/roles/assistant.pyindex d96d8a895..90a33ad6a 100644--- a/metagpt/roles/assistant.py+++ b/metagpt/roles/assistant.py@@ -22,7 +22,7 @@ from pydantic import Fieldfrom metagpt.actions.skill_action import ArgumentsParingAction, SkillActionfrom metagpt.actions.talk_action import TalkAction-from metagpt.config import CONFIG+from metagpt.context import contextfrom metagpt.learn.skill_loader import SkillsDeclarationfrom metagpt.logs import loggerfrom metagpt.memory.brain_memory import BrainMemory@@ -48,7 +48,7 @@ class Assistant(Role):def __init__(self, **kwargs):super().__init__(**kwargs)- self.constraints = self.constraints.format(language=kwargs.get("language") or CONFIG.language or "Chinese")+ self.constraints = self.constraints.format(language=kwargs.get("language") or context.kwargs.language)async def think(self) -> bool:"""Everything will be done part by part."""@@ -56,7 +56,7 @@ class Assistant(Role):if not last_talk:return Falseif not self.skills:- skill_path = Path(CONFIG.SKILL_PATH) if CONFIG.SKILL_PATH else None+ skill_path = Path(context.kwargs.SKILL_PATH) if context.kwargs.SKILL_PATH else Noneself.skills = await SkillsDeclaration.load(skill_yaml_file_name=skill_path)prompt = ""diff --git a/tests/metagpt/roles/test_architect.py b/tests/metagpt/roles/test_architect.pyindex 06e4b2d11..69afbcfe1 100644--- a/tests/metagpt/roles/test_architect.py+++ b/tests/metagpt/roles/test_architect.py@@ -12,8 +12,8 @@ import uuidimport pytestfrom metagpt.actions import WriteDesign, WritePRD-from metagpt.config import CONFIGfrom metagpt.const import PRDS_FILE_REPO+from metagpt.context import contextfrom metagpt.logs import loggerfrom metagpt.roles import Architectfrom metagpt.schema import Message@@ -25,7 +25,7 @@ from tests.metagpt.roles.mock import MockMessagesasync def test_architect():# Prerequisitesfilename = uuid.uuid4().hex + ".json"- await awrite(CONFIG.git_repo.workdir / PRDS_FILE_REPO / filename, data=MockMessages.prd.content)+ await awrite(context.git_repo.workdir / PRDS_FILE_REPO / filename, data=MockMessages.prd.content)role = Architect()rsp = await role.run(with_message=Message(content="", cause_by=WritePRD))diff --git a/tests/metagpt/roles/test_assistant.py b/tests/metagpt/roles/test_assistant.pyindex 24096b357..8797ba7f1 100644--- a/tests/metagpt/roles/test_assistant.py+++ b/tests/metagpt/roles/test_assistant.py@@ -12,7 +12,7 @@ from pydantic import BaseModelfrom metagpt.actions.skill_action import SkillActionfrom metagpt.actions.talk_action import TalkAction-from metagpt.config import CONFIG+from metagpt.context import contextfrom metagpt.memory.brain_memory import BrainMemoryfrom metagpt.roles.assistant import Assistantfrom metagpt.schema import Message@@ -21,7 +21,7 @@ from metagpt.utils.common import any_to_str@pytest.mark.asyncioasync def test_run():- CONFIG.language = "Chinese"+ context.kwargs.language = "Chinese"class Input(BaseModel):memory: BrainMemory@@ -65,7 +65,7 @@ async def test_run():"cause_by": any_to_str(SkillAction),},]- CONFIG.agent_skills = [+ context.kwargs.agent_skills = [{"id": 1, "name": "text_to_speech", "type": "builtin", "config": {}, "enabled": True},{"id": 2, "name": "text_to_image", "type": "builtin", "config": {}, "enabled": True},{"id": 3, "name": "ai_call", "type": "builtin", "config": {}, "enabled": True},@@ -77,8 +77,8 @@ async def test_run():for i in inputs:seed = Input(**i)- CONFIG.language = seed.language- CONFIG.agent_description = seed.agent_description+ context.kwargs.language = seed.language+ context.kwargs.agent_description = seed.agent_descriptionrole = Assistant(language="Chinese")role.memory = seed.memory # Restore historical conversation content.while True:@@ -118,13 +118,7 @@ async def test_memory(memory):assert valawait role.talk("draw apple")-- agent_skills = CONFIG.agent_skills- CONFIG.agent_skills = []- try:- await role.think()- finally:- CONFIG.agent_skills = agent_skills+ await role.think()assert isinstance(role.rc.todo, TalkAction)