fixbug: utilize the new message filtering feature

This commit is contained in:
莘权 马 2023-11-09 11:21:20 +08:00
parent ea9875a7fc
commit ff11cf69af
4 changed files with 21 additions and 9 deletions

View file

@ -21,7 +21,12 @@ from metagpt.const import WORKSPACE_ROOT
from metagpt.logs import logger
from metagpt.roles import Role
from metagpt.schema import Message
from metagpt.utils.common import CodeParser, get_class_name, get_object_name
from metagpt.utils.common import (
CodeParser,
any_to_str_set,
get_class_name,
get_object_name,
)
from metagpt.utils.special_tokens import FILENAME_CODE_SEP, MSG_SEP
@ -102,7 +107,7 @@ class Engineer(Role):
return CodeParser.parse_str(block="Python package name", text=system_design_msg.content)
def get_workspace(self) -> Path:
msg = self._rc.memory.get_by_action(WriteDesign)[-1]
msg = self._rc.memory.get_by_action(get_class_name(WriteDesign))[-1]
if not msg:
return WORKSPACE_ROOT / "src"
workspace = self.parse_workspace(msg)
@ -130,7 +135,7 @@ class Engineer(Role):
todo_coros = []
for todo in self.todos:
todo_coro = WriteCode().run(
context=self._rc.memory.get_by_actions([WriteTasks, WriteDesign]),
context=self._rc.memory.get_by_actions(any_to_str_set([WriteTasks, WriteDesign])),
filename=todo,
)
todo_coros.append(todo_coro)
@ -185,7 +190,7 @@ class Engineer(Role):
TODO: The goal is not to need it. After clear task decomposition, based on the design idea, you should be able to write a single file without needing other codes. If you can't, it means you need a clearer definition. This is the key to writing longer code.
"""
context = []
msg = self._rc.memory.get_by_actions([WriteDesign, WriteTasks, WriteCode])
msg = self._rc.memory.get_by_actions(any_to_str_set([WriteDesign, WriteTasks, WriteCode]))
for m in msg:
context.append(m.content)
context_str = "\n".join(context)

View file

@ -22,7 +22,12 @@ from metagpt.const import WORKSPACE_ROOT
from metagpt.logs import logger
from metagpt.roles import Role
from metagpt.schema import Message
from metagpt.utils.common import CodeParser, any_to_str_set, parse_recipient
from metagpt.utils.common import (
CodeParser,
any_to_str_set,
get_class_name,
parse_recipient,
)
from metagpt.utils.special_tokens import FILENAME_CODE_SEP, MSG_SEP
@ -50,7 +55,7 @@ class QaEngineer(Role):
return CodeParser.parse_str(block="Python package name", text=system_design_msg.content)
def get_workspace(self, return_proj_dir=True) -> Path:
msg = self._rc.memory.get_by_action(WriteDesign)[-1]
msg = self._rc.memory.get_by_action(get_class_name(WriteDesign))[-1]
if not msg:
return WORKSPACE_ROOT / "src"
workspace = self.parse_workspace(msg)

View file

@ -217,9 +217,11 @@ class Role:
async def _observe(self) -> int:
"""Prepare new messages for processing from the message buffer and other sources."""
# Read unprocessed messages from the msg buffer.
self._rc.news = self._rc.msg_buffer.pop_all()
news = self._rc.msg_buffer.pop_all()
# Store the read messages in your own memory to prevent duplicate processing.
self._rc.memory.add_batch(self._rc.news)
self._rc.memory.add_batch(news)
# Filter out messages of interest.
self._rc.news = [n for n in news if n.cause_by in self._rc.watch]
# Design Rules:
# If you need to further categorize Message objects, you can do so using the Message.set_meta function.