From c6f97f748717c030f752ebe492342aace4a4ab13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 6 Nov 2023 11:47:29 +0800 Subject: [PATCH] refactor: tx_from/tx_to --- examples/debate.py | 8 ++++---- metagpt/const.py | 4 ++-- metagpt/memory/memory.py | 2 +- metagpt/roles/engineer.py | 4 ++-- metagpt/roles/qa_engineer.py | 14 +++++++------- metagpt/roles/role.py | 4 ++-- metagpt/schema.py | 16 ++++++++-------- metagpt/software_company.py | 2 +- tests/metagpt/test_role.py | 2 +- tests/metagpt/test_schema.py | 8 ++++---- 10 files changed, 32 insertions(+), 32 deletions(-) diff --git a/examples/debate.py b/examples/debate.py index c1d997678..77a2ce129 100644 --- a/examples/debate.py +++ b/examples/debate.py @@ -78,8 +78,8 @@ class Trump(Role): content=rsp, role=self.profile, cause_by=ShoutOut, - tx_from=self.name, - tx_to=self.opponent_name, + msg_from=self.name, + msg_to=self.opponent_name, ) return msg @@ -121,8 +121,8 @@ class Biden(Role): content=rsp, role=self.profile, cause_by=ShoutOut, - tx_from=self.name, - tx_to=self.opponent_name, + msg_from=self.name, + msg_to=self.opponent_name, ) return msg diff --git a/metagpt/const.py b/metagpt/const.py index e783ec8d0..7b8203bce 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -44,7 +44,7 @@ SKILL_DIRECTORY = PROJECT_ROOT / "metagpt/skills" MEM_TTL = 24 * 30 * 3600 -MESSAGE_ROUTE_FROM = "tx_from" -MESSAGE_ROUTE_TO = "tx_to" +MESSAGE_ROUTE_FROM = "msg_from" +MESSAGE_ROUTE_TO = "msg_to" MESSAGE_ROUTE_CAUSE_BY = "cause_by" MESSAGE_META_ROLE = "role" diff --git a/metagpt/memory/memory.py b/metagpt/memory/memory.py index cf3140bdb..c6b732076 100644 --- a/metagpt/memory/memory.py +++ b/metagpt/memory/memory.py @@ -28,7 +28,7 @@ class Memory: self.storage.append(message) # According to the design of RFC 116, it allows message filtering based on different labels, thus # necessitating the creation of separate indices for each label. - for k in message.tx_to: + for k in message.msg_to: self.index[k].append(message) def add_batch(self, messages: Iterable[Message]): diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 7f05c52c5..8778471cc 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -170,7 +170,7 @@ class Engineer(Role): content=MSG_SEP.join(code_msg_all), role=self.profile, cause_by=get_object_name(self._rc.todo), - tx_to="QaEngineer", + msg_to="QaEngineer", ) return msg @@ -213,7 +213,7 @@ class Engineer(Role): content=MSG_SEP.join(code_msg_all), role=self.profile, cause_by=get_object_name(self._rc.todo), - tx_to="QaEngineer", + msg_to="QaEngineer", ) return msg diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index 64d7f9702..05fc5b217 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -100,8 +100,8 @@ class QaEngineer(Role): content=str(file_info), role=self.profile, cause_by=WriteTest, - tx_from=self.profile, - tx_to=self.profile, + msg_from=self.profile, + msg_to=self.profile, ) self.publish_message(msg) @@ -133,7 +133,7 @@ class QaEngineer(Role): recipient = parse_recipient(result_msg) # the recipient might be Engineer or myself content = str(file_info) + FILENAME_CODE_SEP + result_msg - msg = Message(content=content, role=self.profile, cause_by=RunCode, tx_from=self.profile, tx_to=recipient) + msg = Message(content=content, role=self.profile, cause_by=RunCode, msg_from=self.profile, msg_to=recipient) self.publish_message(msg) async def _debug_error(self, msg): @@ -146,8 +146,8 @@ class QaEngineer(Role): content=file_info, role=self.profile, cause_by=DebugError, - tx_from=self.profile, - tx_to=recipient, + msg_from=self.profile, + msg_to=recipient, ) self.publish_message(msg) @@ -164,7 +164,7 @@ class QaEngineer(Role): content=f"Exceeding {self.test_round_allowed} rounds of tests, skip (writing code counts as a round, too)", role=self.profile, cause_by=WriteTest, - tx_from=self.profile, + msg_from=self.profile, ) return result_msg @@ -188,6 +188,6 @@ class QaEngineer(Role): content=f"Round {self.test_round} of tests done", role=self.profile, cause_by=WriteTest, - tx_from=self.profile, + msg_from=self.profile, ) return result_msg diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 87a03b391..9bbba2070 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -211,14 +211,14 @@ class Role: instruct_content=response.instruct_content, role=self.profile, cause_by=get_object_name(self._rc.todo), - tx_from=get_object_name(self), + msg_from=get_object_name(self), ) else: msg = Message( content=response, role=self.profile, cause_by=get_object_name(self._rc.todo), - tx_from=get_object_name(self), + msg_from=get_object_name(self), ) return msg diff --git a/metagpt/schema.py b/metagpt/schema.py index 0be067cfe..39a62e706 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -77,13 +77,13 @@ class Routes(BaseModel): return False @property - def tx_from(self): + def msg_from(self): """Message route info tells who sent this message.""" route = self._get_route() return route.get(MESSAGE_ROUTE_FROM) @property - def tx_to(self): + def msg_to(self): """Labels for the consumer to filter its subscribed messages.""" route = self._get_route() return route.get(MESSAGE_ROUTE_TO) @@ -112,8 +112,8 @@ class Message(BaseModel): :param instruct_content: Message content struct. :param meta_info: Message meta info. :param route: Message route configuration. - :param tx_from: Message route info tells who sent this message. - :param tx_to: Labels for the consumer to filter its subscribed messages. + :param msg_from: Message route info tells who sent this message. + :param msg_to: Labels for the consumer to filter its subscribed messages. :param cause_by: Labels for the consumer to filter its subscribed messages, also serving as meta info. :param role: Message meta info tells who sent this message. """ @@ -174,14 +174,14 @@ class Message(BaseModel): self.route.replace(old_value, new_value) @property - def tx_from(self): + def msg_from(self): """Message route info tells who sent this message.""" - return self.route.tx_from + return self.route.msg_from @property - def tx_to(self): + def msg_to(self): """Labels for the consumer to filter its subscribed messages.""" - return self.route.tx_to + return self.route.msg_to def set_role(self, v): """Set the message's meta info indicating the sender.""" diff --git a/metagpt/software_company.py b/metagpt/software_company.py index 354773444..1b6936870 100644 --- a/metagpt/software_company.py +++ b/metagpt/software_company.py @@ -53,7 +53,7 @@ class SoftwareCompany(BaseModel): role="BOSS", content=idea, cause_by=BossRequirement, - tx_from=SoftwareCompany, + msg_from=SoftwareCompany, ) ) diff --git a/tests/metagpt/test_role.py b/tests/metagpt/test_role.py index 7794c9b57..69386e28c 100644 --- a/tests/metagpt/test_role.py +++ b/tests/metagpt/test_role.py @@ -69,7 +69,7 @@ async def test_react(): env = Environment() env.add_role(role) assert env.get_subscribed_tags(role) == {seed.subscription} - env.publish_message(Message(content="test", tx_to=seed.subscription)) + env.publish_message(Message(content="test", msg_to=seed.subscription)) assert not role.is_idle while not env.is_idle: await env.run() diff --git a/tests/metagpt/test_schema.py b/tests/metagpt/test_schema.py index e18ebbe79..5ebc7ce1d 100644 --- a/tests/metagpt/test_schema.py +++ b/tests/metagpt/test_schema.py @@ -66,13 +66,13 @@ def test_message(): def test_routes(): route = Routes() route.set_from("a") - assert route.tx_from == "a" + assert route.msg_from == "a" route.add_to("b") - assert route.tx_to == {"b"} + assert route.msg_to == {"b"} route.add_to("c") - assert route.tx_to == {"b", "c"} + assert route.msg_to == {"b", "c"} route.set_to({"e", "f"}) - assert route.tx_to == {"e", "f"} + assert route.msg_to == {"e", "f"} assert route.is_recipient({"e"}) assert route.is_recipient({"f"}) assert not route.is_recipient({"a"})