refactor: tx_from/tx_to

This commit is contained in:
莘权 马 2023-11-06 11:47:29 +08:00
parent ed7eb4d08a
commit c6f97f7487
10 changed files with 32 additions and 32 deletions

View file

@ -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

View file

@ -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"

View file

@ -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]):

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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."""

View file

@ -53,7 +53,7 @@ class SoftwareCompany(BaseModel):
role="BOSS",
content=idea,
cause_by=BossRequirement,
tx_from=SoftwareCompany,
msg_from=SoftwareCompany,
)
)

View file

@ -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()

View file

@ -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"})