feat: face recognition agent #53

Open
2584433 wants to merge 31 commits from feat/face-recognition into main
3 changed files with 88 additions and 14 deletions
Showing only changes of commit ac20048f02 - Show all commits

View File

@@ -60,6 +60,9 @@ class BaseAgent(ABC):
self._tasks: set[asyncio.Task] = set() self._tasks: set[asyncio.Task] = set()
self._running = False self._running = False
self._internal_pub_socket: None | azmq.Socket = None
self._internal_sub_socket: None | azmq.Socket = None
# Register immediately # Register immediately
AgentDirectory.register(name, self) AgentDirectory.register(name, self)
@@ -130,13 +133,19 @@ class BaseAgent(ABC):
:param message: The message to send. :param message: The message to send.
""" """
target = AgentDirectory.get(message.to) message.sender = self.name
to = message.to
receivers = [to] if isinstance(to, str) else to
for receiver in receivers:
target = AgentDirectory.get(receiver)
if target: if target:
await target.inbox.put(message) await target.inbox.put(message)
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 regular inbox.")
else: else:
# Apparently target agent is on a different process, send via ZMQ # Apparently target agent is on a different process, send via ZMQ
topic = f"internal/{message.to}".encode() topic = f"internal/{receiver}".encode()
body = message.model_dump_json().encode() body = message.model_dump_json().encode()
await self._internal_pub_socket.send_multipart([topic, body]) await self._internal_pub_socket.send_multipart([topic, body])
self.logger.debug(f"Sent message {message.body} to {message.to} via ZMQ.") self.logger.debug(f"Sent message {message.body} to {message.to} via ZMQ.")

View File

@@ -1,3 +1,5 @@
from collections.abc import Iterable
from pydantic import BaseModel from pydantic import BaseModel
@@ -5,13 +7,13 @@ class InternalMessage(BaseModel):
""" """
Standard message envelope for communication between agents within the Control Backend. Standard message envelope for communication between agents within the Control Backend.
:ivar to: The name of the destination agent. :ivar to: The name(s) of the destination agent(s).
:ivar sender: The name of the sending agent. :ivar sender: The name of the sending agent.
:ivar body: The string payload (often a JSON-serialized model). :ivar body: The string payload (often a JSON-serialized model).
: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

View File

@@ -99,12 +99,75 @@ async def test_send_to_local_agent(monkeypatch):
# Patch inbox.put # Patch inbox.put
target.inbox.put = AsyncMock() target.inbox.put = AsyncMock()
message = InternalMessage(to="receiver", sender="sender", body="hello") message = InternalMessage(to=target.name, sender=sender.name, body="hello")
await sender.send(message) await sender.send(message)
target.inbox.put.assert_awaited_once_with(message) target.inbox.put.assert_awaited_once_with(message)
sender.logger.debug.assert_called_once()
@pytest.mark.asyncio
async def test_send_to_zmq_agent(monkeypatch):
sender = DummyAgent("sender")
target = "remote_receiver"
# Fake logger
sender.logger = MagicMock()
# Fake zmq
sender._internal_pub_socket = AsyncMock()
message = InternalMessage(to=target, sender=sender.name, body="hello")
await sender.send(message)
zmq_calls = sender._internal_pub_socket.send_multipart.call_args[0][0]
assert zmq_calls[0] == f"internal/{target}".encode()
@pytest.mark.asyncio
async def test_send_to_multiple_local_agents(monkeypatch):
sender = DummyAgent("sender")
target1 = DummyAgent("receiver1")
target2 = DummyAgent("receiver2")
# Fake logger
sender.logger = MagicMock()
# Patch inbox.put
target1.inbox.put = AsyncMock()
target2.inbox.put = AsyncMock()
message = InternalMessage(to=[target1.name, target2.name], sender=sender.name, body="hello")
await sender.send(message)
target1.inbox.put.assert_awaited_once_with(message)
target2.inbox.put.assert_awaited_once_with(message)
@pytest.mark.asyncio
async def test_send_to_multiple_agents(monkeypatch):
sender = DummyAgent("sender")
target1 = DummyAgent("receiver1")
target2 = "remote_receiver"
# Fake logger
sender.logger = MagicMock()
# Fake zmq
sender._internal_pub_socket = AsyncMock()
# Patch inbox.put
target1.inbox.put = AsyncMock()
message = InternalMessage(to=[target1.name, target2], sender=sender.name, body="hello")
await sender.send(message)
target1.inbox.put.assert_awaited_once_with(message)
zmq_calls = sender._internal_pub_socket.send_multipart.call_args[0][0]
assert zmq_calls[0] == f"internal/{target2}".encode()
@pytest.mark.asyncio @pytest.mark.asyncio