feat: able to send to multiple receivers #42
@@ -130,16 +130,21 @@ class BaseAgent(ABC):
|
|||||||
|
|
||||||
:param message: The message to send.
|
:param message: The message to send.
|
||||||
"""
|
"""
|
||||||
target = AgentDirectory.get(message.to)
|
to = message.to
|
||||||
if target:
|
receivers = [to] if isinstance(to, str) else to
|
||||||
await target.inbox.put(message)
|
|
||||||
self.logger.debug(f"Sent message {message.body} to {message.to} via regular inbox.")
|
for receiver in receivers:
|
||||||
else:
|
target = AgentDirectory.get(receiver)
|
||||||
# Apparently target agent is on a different process, send via ZMQ
|
|
||||||
topic = f"internal/{message.to}".encode()
|
if target:
|
||||||
body = message.model_dump_json().encode()
|
await target.inbox.put(message)
|
||||||
await self._internal_pub_socket.send_multipart([topic, body])
|
self.logger.debug(f"Sent message {message.body} to {message.to} via regular inbox.")
|
||||||
self.logger.debug(f"Sent message {message.body} to {message.to} via ZMQ.")
|
else:
|
||||||
|
# Apparently target agent is on a different process, send via ZMQ
|
||||||
|
topic = f"internal/{message.to}".encode()
|
||||||
|
body = message.model_dump_json().encode()
|
||||||
|
await self._internal_pub_socket.send_multipart([topic, body])
|
||||||
|
self.logger.debug(f"Sent message {message.body} to {message.to} via ZMQ.")
|
||||||
|
|
||||||
async def _process_inbox(self):
|
async def _process_inbox(self):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
from collections.abc import Iterable
|
||||||
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
@@ -11,7 +13,7 @@ class InternalMessage(BaseModel):
|
|||||||
:ivar thread: An optional thread identifier/topic to categorize the message (e.g., 'beliefs').
|
:ivar thread: An optional thread identifier/topic to categorize the message (e.g., 'beliefs').
|
||||||
"""
|
"""
|
||||||
|
|
||||||
to: str
|
to: str | Iterable[str]
|
||||||
sender: str
|
sender: str
|
||||||
body: str
|
body: str
|
||||||
thread: str | None = None
|
thread: str | None = None
|
||||||
|
|||||||
Reference in New Issue
Block a user