From b83a362abe7c7247a1be2a4b3135b0f219237d6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Otgaar?= Date: Wed, 29 Oct 2025 13:31:24 +0100 Subject: [PATCH 01/26] fix: wait for req socket send to make sure we dont stay stuck - if there's no REP this would be awaited forever. ref: N25B-205 --- .../agents/ri_communication_agent.py | 29 ++++++++++++++++--- .../api/v1/endpoints/command.py | 1 - 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/control_backend/agents/ri_communication_agent.py b/src/control_backend/agents/ri_communication_agent.py index 504c707..2b92989 100644 --- a/src/control_backend/agents/ri_communication_agent.py +++ b/src/control_backend/agents/ri_communication_agent.py @@ -17,6 +17,7 @@ class RICommunicationAgent(Agent): req_socket: zmq.Socket _address = "" _bind = True + connected = False def __init__( self, @@ -40,17 +41,34 @@ class RICommunicationAgent(Agent): # We need to listen and sent pings. message = {"endpoint": "ping", "data": {"id": "e.g. some reference id"}} - await self.agent.req_socket.send_json(message) + seconds_to_wait_total = 4.0 + try: + await asyncio.wait_for( + self.agent.req_socket.send_json(message), timeout=seconds_to_wait_total / 2 + ) + except TimeoutError as e: + logger.debug( + f"Waited too long to send message - we probably dont have any receivers... but let's check!" + ) # Wait up to three seconds for a reply:) try: - message = await asyncio.wait_for(self.agent.req_socket.recv_json(), timeout=3.0) + logger.debug(f"waiting for message for {seconds_to_wait_total / 2} seconds.") + message = await asyncio.wait_for( + self.agent.req_socket.recv_json(), timeout=seconds_to_wait_total / 2 + ) # We didnt get a reply :( - except asyncio.TimeoutError as e: - logger.info("No ping retrieved in 3 seconds, killing myself.") + except TimeoutError as e: + logger.info(f"No ping back retrieved in {seconds_to_wait_total/2} seconds totalling {seconds_to_wait_total} of time, killing myself.") + self.agent.connected = False + # TODO: Send event to UI letting know that we've lost connection + self.kill() + except Exception as e: + logger.debug(f"Differennt exception: {e}") + logger.debug('Received message "%s"', message) if "endpoint" not in message: logger.error("No received endpoint in message, excepted ping endpoint.") @@ -162,4 +180,7 @@ class RICommunicationAgent(Agent): # Set up ping behaviour listen_behaviour = self.ListenBehaviour() self.add_behaviour(listen_behaviour) + + # TODO: Let UI know that we're connected >:) + self.connected = True logger.info("Finished setting up %s", self.jid) diff --git a/src/control_backend/api/v1/endpoints/command.py b/src/control_backend/api/v1/endpoints/command.py index badaf90..e7fef60 100644 --- a/src/control_backend/api/v1/endpoints/command.py +++ b/src/control_backend/api/v1/endpoints/command.py @@ -17,6 +17,5 @@ async def receive_command(command: SpeechCommand, request: Request): topic = b"command" pub_socket: Socket = request.app.state.internal_comm_socket pub_socket.send_multipart([topic, command.model_dump_json().encode()]) - return {"status": "Command received"} From 59c2edc3c6d676441fed7ff212aa840430037fe4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Otgaar?= Date: Wed, 29 Oct 2025 13:33:01 +0100 Subject: [PATCH 02/26] fix: small fix for testing ping timeouts ref: N25B-205 --- test/integration/agents/test_ri_communication_agent.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/agents/test_ri_communication_agent.py b/test/integration/agents/test_ri_communication_agent.py index 3e4a056..e8643c8 100644 --- a/test/integration/agents/test_ri_communication_agent.py +++ b/test/integration/agents/test_ri_communication_agent.py @@ -493,7 +493,7 @@ async def test_listen_behaviour_timeout(caplog): with caplog.at_level("INFO"): await behaviour.run() - assert "No ping retrieved in 3 seconds" in caplog.text + assert "No ping" in caplog.text @pytest.mark.asyncio From 669d0190d6b5f9564d6941883b557fdc7d4d4d7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Otgaar?= Date: Wed, 29 Oct 2025 19:22:06 +0100 Subject: [PATCH 03/26] feat: started ping router and internal messaging for pings ref: N25B-151 --- .../agents/ri_communication_agent.py | 10 ++- .../api/v1/endpoints/command.py | 21 ------ src/control_backend/api/v1/endpoints/robot.py | 74 +++++++++++++++++++ src/control_backend/api/v1/router.py | 4 +- ...and_endpoint.py => test_robot_endpoint.py} | 4 +- 5 files changed, 87 insertions(+), 26 deletions(-) delete mode 100644 src/control_backend/api/v1/endpoints/command.py create mode 100644 src/control_backend/api/v1/endpoints/robot.py rename test/integration/api/endpoints/{test_command_endpoint.py => test_robot_endpoint.py} (95%) diff --git a/src/control_backend/agents/ri_communication_agent.py b/src/control_backend/agents/ri_communication_agent.py index 2b92989..4da2c69 100644 --- a/src/control_backend/agents/ri_communication_agent.py +++ b/src/control_backend/agents/ri_communication_agent.py @@ -5,11 +5,13 @@ from spade.agent import Agent from spade.behaviour import CyclicBehaviour import zmq + from control_backend.core.config import settings from control_backend.core.zmq_context import context -from control_backend.schemas.message import Message +from control_backend.schemas.ri_message import RIMessage from control_backend.agents.ri_command_agent import RICommandAgent + logger = logging.getLogger(__name__) @@ -63,6 +65,11 @@ class RICommunicationAgent(Agent): logger.info(f"No ping back retrieved in {seconds_to_wait_total/2} seconds totalling {seconds_to_wait_total} of time, killing myself.") self.agent.connected = False # TODO: Send event to UI letting know that we've lost connection + topic = b"ping" + data = json.dumps(False).encode() + pub_socket = context.socket(zmq.PUB) + pub_socket.connect(settings.zmq_settings.internal_comm_address) + pub_socket.send_multipart([topic, data]) self.kill() @@ -90,6 +97,7 @@ class RICommunicationAgent(Agent): logger.info("Setting up %s", self.jid) retries = 0 + # Let's try a certain amount of times before failing connection while retries < max_retries: # Bind request socket diff --git a/src/control_backend/api/v1/endpoints/command.py b/src/control_backend/api/v1/endpoints/command.py deleted file mode 100644 index e7fef60..0000000 --- a/src/control_backend/api/v1/endpoints/command.py +++ /dev/null @@ -1,21 +0,0 @@ -from fastapi import APIRouter, Request -import logging - -from zmq import Socket - -from control_backend.schemas.ri_message import SpeechCommand, RIEndpoint - -logger = logging.getLogger(__name__) - -router = APIRouter() - - -@router.post("/command", status_code=202) -async def receive_command(command: SpeechCommand, request: Request): - # Validate and retrieve data. - SpeechCommand.model_validate(command) - topic = b"command" - pub_socket: Socket = request.app.state.internal_comm_socket - pub_socket.send_multipart([topic, command.model_dump_json().encode()]) - - return {"status": "Command received"} diff --git a/src/control_backend/api/v1/endpoints/robot.py b/src/control_backend/api/v1/endpoints/robot.py new file mode 100644 index 0000000..1d0da9c --- /dev/null +++ b/src/control_backend/api/v1/endpoints/robot.py @@ -0,0 +1,74 @@ +from fastapi import APIRouter, Request +from fastapi.responses import JSONResponse, StreamingResponse +import logging +import asyncio +import zmq.asyncio +import json +import datetime + +from zmq import Socket +from control_backend.core.zmq_context import context +from control_backend.core.config import settings +from control_backend.schemas.ri_message import SpeechCommand, RIEndpoint + + + + +logger = logging.getLogger(__name__) + +router = APIRouter() + + +@router.post("/command", status_code=202) +async def receive_command(command: SpeechCommand, request: Request): + # Validate and retrieve data. + SpeechCommand.model_validate(command) + topic = b"command" + pub_socket: Socket = request.app.state.internal_comm_socket + pub_socket.send_multipart([topic, command.model_dump_json().encode()]) + + return {"status": "Command received"} + + +@router.get("/ping_check") +async def ping(request: Request): + pass + + +@router.get("/ping_stream") +async def ping_stream(request: Request): + """Stream live updates whenever the device state changes.""" + async def event_stream(): + # Set up internal socket to receive ping updates + logger.debug("Ping stream router event stream entered.") + sub_socket = zmq.asyncio.Context().socket(zmq.SUB) + sub_socket.connect(settings.zmq_settings.internal_comm_address) + sub_socket.setsockopt(zmq.SUBSCRIBE, b"ping") + connected = True + + ping_frequency = 1 # How many seconds between ping attempts + + # Even though its most likely the updates should alternate + # So, True - False - True - False for connectivity. + # Let's still check:) + while True: + logger.debug("Ping stream entered listening ") + try: + topic, body = await asyncio.wait_for(sub_socket.recv_multipart(), timeout=ping_frequency) + logger.debug("got ping change in ping_stream router") + connected = json.loads(body) + except TimeoutError as e: + await asyncio.sleep(0.1) + + # Stop if client disconnected + if await request.is_disconnected(): + print("Client disconnected from SSE") + break + + + logger.debug(f"Yielded new connection event in robot ping router: {str(connected)}") + yield f"data: {str(connected)}, time:{str(datetime.datetime.now().strftime("%H:%M:%S"))}\n\n" + + + + return StreamingResponse(event_stream(), media_type="text/event-stream") \ No newline at end of file diff --git a/src/control_backend/api/v1/router.py b/src/control_backend/api/v1/router.py index dc7aea9..dca7e27 100644 --- a/src/control_backend/api/v1/router.py +++ b/src/control_backend/api/v1/router.py @@ -1,6 +1,6 @@ from fastapi.routing import APIRouter -from control_backend.api.v1.endpoints import message, sse, command +from control_backend.api.v1.endpoints import message, sse, robot api_router = APIRouter() @@ -8,4 +8,4 @@ api_router.include_router(message.router, tags=["Messages"]) api_router.include_router(sse.router, tags=["SSE"]) -api_router.include_router(command.router, tags=["Commands"]) +api_router.include_router(robot.router, prefix="/robot", tags=["Pings", "Commands"]) \ No newline at end of file diff --git a/test/integration/api/endpoints/test_command_endpoint.py b/test/integration/api/endpoints/test_robot_endpoint.py similarity index 95% rename from test/integration/api/endpoints/test_command_endpoint.py rename to test/integration/api/endpoints/test_robot_endpoint.py index 07bd866..827fb17 100644 --- a/test/integration/api/endpoints/test_command_endpoint.py +++ b/test/integration/api/endpoints/test_robot_endpoint.py @@ -3,7 +3,7 @@ from fastapi import FastAPI from fastapi.testclient import TestClient from unittest.mock import MagicMock -from control_backend.api.v1.endpoints import command +from control_backend.api.v1.endpoints import robot from control_backend.schemas.ri_message import SpeechCommand @@ -14,7 +14,7 @@ def app(): Also sets up a mock internal_comm_socket. """ app = FastAPI() - app.include_router(command.router) + app.include_router(robot.router) app.state.internal_comm_socket = MagicMock() # mock ZMQ socket return app From 4f2d45fb44c17fe4575a10261f605c55c314c36c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Otgaar?= Date: Wed, 29 Oct 2025 21:55:23 +0100 Subject: [PATCH 04/26] feat: fixed socket typing for communication agent and ping router- automatically try to reconnect with robot. ref: N25B-151 --- .../agents/ri_communication_agent.py | 58 ++++++++++++------- src/control_backend/api/v1/endpoints/robot.py | 10 ++-- src/control_backend/main.py | 7 ++- 3 files changed, 47 insertions(+), 28 deletions(-) diff --git a/src/control_backend/agents/ri_communication_agent.py b/src/control_backend/agents/ri_communication_agent.py index 4da2c69..f46c623 100644 --- a/src/control_backend/agents/ri_communication_agent.py +++ b/src/control_backend/agents/ri_communication_agent.py @@ -4,6 +4,7 @@ import logging from spade.agent import Agent from spade.behaviour import CyclicBehaviour import zmq +import zmq.asyncio from control_backend.core.config import settings @@ -16,7 +17,8 @@ logger = logging.getLogger(__name__) class RICommunicationAgent(Agent): - req_socket: zmq.Socket + _pub_socket: zmq.asyncio.Socket + req_socket: zmq.asyncio.Socket | None _address = "" _bind = True connected = False @@ -25,14 +27,18 @@ class RICommunicationAgent(Agent): self, jid: str, password: str, + pub_socket: zmq.asyncio.Socket, port: int = 5222, verify_security: bool = False, address="tcp://localhost:0000", bind=False, + ): super().__init__(jid, password, port, verify_security) self._address = address self._bind = bind + self.req_socket = None + self._pub_socket = pub_socket class ListenBehaviour(CyclicBehaviour): async def run(self): @@ -43,7 +49,7 @@ class RICommunicationAgent(Agent): # We need to listen and sent pings. message = {"endpoint": "ping", "data": {"id": "e.g. some reference id"}} - seconds_to_wait_total = 4.0 + seconds_to_wait_total = 1.0 try: await asyncio.wait_for( self.agent.req_socket.send_json(message), timeout=seconds_to_wait_total / 2 @@ -62,16 +68,12 @@ class RICommunicationAgent(Agent): # We didnt get a reply :( except TimeoutError as e: - logger.info(f"No ping back retrieved in {seconds_to_wait_total/2} seconds totalling {seconds_to_wait_total} of time, killing myself.") - self.agent.connected = False + logger.info(f"No ping back retrieved in {seconds_to_wait_total/2} seconds totalling {seconds_to_wait_total} of time, killing myself (or maybe just laying low).") # TODO: Send event to UI letting know that we've lost connection topic = b"ping" data = json.dumps(False).encode() - pub_socket = context.socket(zmq.PUB) - pub_socket.connect(settings.zmq_settings.internal_comm_address) - pub_socket.send_multipart([topic, data]) - - self.kill() + self.agent._pub_socket.send_multipart([topic, data]) + await self.agent.setup() except Exception as e: logger.debug(f"Differennt exception: {e}") @@ -90,25 +92,38 @@ class RICommunicationAgent(Agent): "Received message with topic different than ping, while ping expected." ) - async def setup(self, max_retries: int = 5): - """ - Try to setup the communication agent, we have 5 retries in case we dont have a response yet. - """ - logger.info("Setting up %s", self.jid) - retries = 0 - - # Let's try a certain amount of times before failing connection - while retries < max_retries: - # Bind request socket + async def setup_req_socket(self, force = False): + """ + Sets up request socket for communication agent. + """ + if self.req_socket is None or force: self.req_socket = context.socket(zmq.REQ) if self._bind: self.req_socket.bind(self._address) else: self.req_socket.connect(self._address) + + async def setup(self, max_retries: int = 5): + """ + Try to setup the communication agent, we have 5 retries in case we dont have a response yet. + """ + logger.info("Setting up %s", self.jid) + + # Bind request socket + await self.setup_req_socket() + + retries = 0 + # Let's try a certain amount of times before failing connection + while retries < max_retries: + + # Make sure the socket is properly setup. + if self.req_socket is None: + continue + # Send our message and receive one back:) - message = {"endpoint": "negotiate/ports", "data": None} + message = {"endpoint": "negotiate/ports", "data": {}} await self.req_socket.send_json(message) try: @@ -190,5 +205,8 @@ class RICommunicationAgent(Agent): self.add_behaviour(listen_behaviour) # TODO: Let UI know that we're connected >:) + topic = b"ping" + data = json.dumps(True).encode() + await self._pub_socket.send_multipart([topic, data]) self.connected = True logger.info("Finished setting up %s", self.jid) diff --git a/src/control_backend/api/v1/endpoints/robot.py b/src/control_backend/api/v1/endpoints/robot.py index 1d0da9c..aa1b532 100644 --- a/src/control_backend/api/v1/endpoints/robot.py +++ b/src/control_backend/api/v1/endpoints/robot.py @@ -6,7 +6,7 @@ import zmq.asyncio import json import datetime -from zmq import Socket +from zmq.asyncio import Socket from control_backend.core.zmq_context import context from control_backend.core.config import settings from control_backend.schemas.ri_message import SpeechCommand, RIEndpoint @@ -24,7 +24,7 @@ async def receive_command(command: SpeechCommand, request: Request): # Validate and retrieve data. SpeechCommand.model_validate(command) topic = b"command" - pub_socket: Socket = request.app.state.internal_comm_socket + pub_socket : Socket = request.app.state.internal_comm_socket pub_socket.send_multipart([topic, command.model_dump_json().encode()]) return {"status": "Command received"} @@ -41,7 +41,8 @@ async def ping_stream(request: Request): async def event_stream(): # Set up internal socket to receive ping updates logger.debug("Ping stream router event stream entered.") - sub_socket = zmq.asyncio.Context().socket(zmq.SUB) + + sub_socket = context.socket(zmq.SUB) sub_socket.connect(settings.zmq_settings.internal_comm_address) sub_socket.setsockopt(zmq.SUBSCRIBE, b"ping") connected = True @@ -69,6 +70,5 @@ async def ping_stream(request: Request): logger.debug(f"Yielded new connection event in robot ping router: {str(connected)}") yield f"data: {str(connected)}, time:{str(datetime.datetime.now().strftime("%H:%M:%S"))}\n\n" - - + return StreamingResponse(event_stream(), media_type="text/event-stream") \ No newline at end of file diff --git a/src/control_backend/main.py b/src/control_backend/main.py index e398552..bd0cc74 100644 --- a/src/control_backend/main.py +++ b/src/control_backend/main.py @@ -32,11 +32,12 @@ async def lifespan(app: FastAPI): # Initiate agents ri_communication_agent = RICommunicationAgent( - settings.agent_settings.ri_communication_agent_name + "@" + settings.agent_settings.host, - settings.agent_settings.ri_communication_agent_name, + jid=settings.agent_settings.ri_communication_agent_name + "@" + settings.agent_settings.host, + password=settings.agent_settings.ri_communication_agent_name, + pub_socket=internal_comm_socket, address="tcp://*:5555", bind=True, - ) + ) await ri_communication_agent.start() bdi_core = BDICoreAgent( From df6a39866bbc3a87d85dae95fac7e4d1ae01180e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Otgaar?= Date: Wed, 29 Oct 2025 22:05:13 +0100 Subject: [PATCH 05/26] fix: fix unit tests with new feat ref: N25B-151 --- .../agents/test_ri_communication_agent.py | 61 +++++++++++-------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/test/integration/agents/test_ri_communication_agent.py b/test/integration/agents/test_ri_communication_agent.py index e8643c8..9a7cb41 100644 --- a/test/integration/agents/test_ri_communication_agent.py +++ b/test/integration/agents/test_ri_communication_agent.py @@ -92,6 +92,8 @@ async def test_setup_creates_socket_and_negotiate_1(monkeypatch): fake_socket.send_json = AsyncMock() fake_socket.recv_json = fake_json_correct_negototiate_1() + fake_pub_socket = AsyncMock() + # Mock context.socket to return our fake socket monkeypatch.setattr( "control_backend.agents.ri_communication_agent.context.socket", lambda _: fake_socket @@ -103,16 +105,17 @@ async def test_setup_creates_socket_and_negotiate_1(monkeypatch): ) as MockCommandAgent: fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() + fake_pub_socket = AsyncMock() # --- Act --- agent = RICommunicationAgent( - "test@server", "password", address="tcp://localhost:5555", bind=False + "test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=False ) await agent.setup() # --- Assert --- fake_socket.connect.assert_any_call("tcp://localhost:5555") - fake_socket.send_json.assert_any_call({"endpoint": "negotiate/ports", "data": None}) + fake_socket.send_json.assert_any_call({"endpoint": "negotiate/ports", "data": {}}) fake_socket.recv_json.assert_awaited() fake_agent_instance.start.assert_awaited() MockCommandAgent.assert_called_once_with( @@ -146,16 +149,17 @@ async def test_setup_creates_socket_and_negotiate_2(monkeypatch): ) as MockCommandAgent: fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() + fake_pub_socket = AsyncMock() # --- Act --- agent = RICommunicationAgent( - "test@server", "password", address="tcp://localhost:5555", bind=False + "test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=False ) await agent.setup() # --- Assert --- fake_socket.connect.assert_any_call("tcp://localhost:5555") - fake_socket.send_json.assert_any_call({"endpoint": "negotiate/ports", "data": None}) + fake_socket.send_json.assert_any_call({"endpoint": "negotiate/ports", "data": {}}) fake_socket.recv_json.assert_awaited() fake_agent_instance.start.assert_awaited() MockCommandAgent.assert_called_once_with( @@ -192,11 +196,11 @@ async def test_setup_creates_socket_and_negotiate_3(monkeypatch, caplog): ) as MockCommandAgent: fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() - + fake_pub_socket = AsyncMock() # --- Act --- with caplog.at_level("ERROR"): agent = RICommunicationAgent( - "test@server", "password", address="tcp://localhost:5555", bind=False + "test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=False ) await agent.setup(max_retries=1) @@ -233,16 +237,16 @@ async def test_setup_creates_socket_and_negotiate_4(monkeypatch): ) as MockCommandAgent: fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() - + fake_pub_socket = AsyncMock() # --- Act --- agent = RICommunicationAgent( - "test@server", "password", address="tcp://localhost:5555", bind=True + "test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=True ) await agent.setup() # --- Assert --- fake_socket.bind.assert_any_call("tcp://localhost:5555") - fake_socket.send_json.assert_any_call({"endpoint": "negotiate/ports", "data": None}) + fake_socket.send_json.assert_any_call({"endpoint": "negotiate/ports", "data": {}}) fake_socket.recv_json.assert_awaited() fake_agent_instance.start.assert_awaited() MockCommandAgent.assert_called_once_with( @@ -276,16 +280,16 @@ async def test_setup_creates_socket_and_negotiate_5(monkeypatch): ) as MockCommandAgent: fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() - + fake_pub_socket = AsyncMock() # --- Act --- agent = RICommunicationAgent( - "test@server", "password", address="tcp://localhost:5555", bind=False + "test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=False ) await agent.setup() # --- Assert --- fake_socket.connect.assert_any_call("tcp://localhost:5555") - fake_socket.send_json.assert_any_call({"endpoint": "negotiate/ports", "data": None}) + fake_socket.send_json.assert_any_call({"endpoint": "negotiate/ports", "data": {}}) fake_socket.recv_json.assert_awaited() fake_agent_instance.start.assert_awaited() MockCommandAgent.assert_called_once_with( @@ -319,16 +323,16 @@ async def test_setup_creates_socket_and_negotiate_6(monkeypatch): ) as MockCommandAgent: fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() - + fake_pub_socket = AsyncMock() # --- Act --- agent = RICommunicationAgent( - "test@server", "password", address="tcp://localhost:5555", bind=False + "test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=False ) await agent.setup() # --- Assert --- fake_socket.connect.assert_any_call("tcp://localhost:5555") - fake_socket.send_json.assert_any_call({"endpoint": "negotiate/ports", "data": None}) + fake_socket.send_json.assert_any_call({"endpoint": "negotiate/ports", "data": {}}) fake_socket.recv_json.assert_awaited() fake_agent_instance.start.assert_awaited() MockCommandAgent.assert_called_once_with( @@ -365,11 +369,12 @@ async def test_setup_creates_socket_and_negotiate_7(monkeypatch, caplog): ) as MockCommandAgent: fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() + fake_pub_socket = AsyncMock() # --- Act --- with caplog.at_level("WARNING"): agent = RICommunicationAgent( - "test@server", "password", address="tcp://localhost:5555", bind=False + "test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=False ) await agent.setup(max_retries=1) @@ -402,11 +407,12 @@ async def test_setup_creates_socket_and_negotiate_timeout(monkeypatch, caplog): ) as MockCommandAgent: fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() - + fake_pub_socket = AsyncMock() + # --- Act --- with caplog.at_level("WARNING"): agent = RICommunicationAgent( - "test@server", "password", address="tcp://localhost:5555", bind=False + "test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=False ) await agent.setup(max_retries=1) @@ -426,9 +432,10 @@ async def test_listen_behaviour_ping_correct(caplog): fake_socket = AsyncMock() fake_socket.send_json = AsyncMock() fake_socket.recv_json = AsyncMock(return_value={"endpoint": "ping", "data": {}}) + fake_pub_socket = AsyncMock() # TODO: Integration test between actual server and password needed for spade agents - agent = RICommunicationAgent("test@server", "password") + agent = RICommunicationAgent("test@server", "password", fake_pub_socket) agent.req_socket = fake_socket behaviour = agent.ListenBehaviour() @@ -461,8 +468,9 @@ async def test_listen_behaviour_ping_wrong_endpoint(caplog): ], } ) + fake_pub_socket = AsyncMock() - agent = RICommunicationAgent("test@server", "password") + agent = RICommunicationAgent("test@server", "password", fake_pub_socket) agent.req_socket = fake_socket behaviour = agent.ListenBehaviour() @@ -483,8 +491,9 @@ async def test_listen_behaviour_timeout(caplog): fake_socket.send_json = AsyncMock() # recv_json will never resolve, simulate timeout fake_socket.recv_json = AsyncMock(side_effect=asyncio.TimeoutError) + fake_pub_socket = AsyncMock() - agent = RICommunicationAgent("test@server", "password") + agent = RICommunicationAgent("test@server", "password", fake_pub_socket) agent.req_socket = fake_socket behaviour = agent.ListenBehaviour() @@ -510,8 +519,9 @@ async def test_listen_behaviour_ping_no_endpoint(caplog): "data": "I dont have an endpoint >:)", } ) + fake_pub_socket = AsyncMock() - agent = RICommunicationAgent("test@server", "password") + agent = RICommunicationAgent("test@server", "password", fake_pub_socket) agent.req_socket = fake_socket behaviour = agent.ListenBehaviour() @@ -530,15 +540,17 @@ async def test_listen_behaviour_ping_no_endpoint(caplog): async def test_setup_unexpected_exception(monkeypatch, caplog): fake_socket = MagicMock() fake_socket.send_json = AsyncMock() + fake_pub_socket = AsyncMock() # Simulate unexpected exception during recv_json() fake_socket.recv_json = AsyncMock(side_effect=Exception("boom!")) + monkeypatch.setattr( "control_backend.agents.ri_communication_agent.context.socket", lambda _: fake_socket ) agent = RICommunicationAgent( - "test@server", "password", address="tcp://localhost:5555", bind=False + "test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=False ) with caplog.at_level("ERROR"): @@ -572,9 +584,10 @@ async def test_setup_unpacking_exception(monkeypatch, caplog): ) as MockCommandAgent: fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() + fake_pub_socket = AsyncMock() agent = RICommunicationAgent( - "test@server", "password", address="tcp://localhost:5555", bind=False + "test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=False ) # --- Act & Assert --- From af3e4ae56a49d1b60287fa49de000b7ac08bfc7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Otgaar?= Date: Thu, 30 Oct 2025 13:07:01 +0100 Subject: [PATCH 06/26] fix: adjusted ping data on ping_stream, and made it so that communication agent is more robust and quick in ping communication. ref: N25B-142 --- src/control_backend/agents/ri_communication_agent.py | 3 +++ src/control_backend/api/v1/endpoints/robot.py | 5 +++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/control_backend/agents/ri_communication_agent.py b/src/control_backend/agents/ri_communication_agent.py index f46c623..79e44ed 100644 --- a/src/control_backend/agents/ri_communication_agent.py +++ b/src/control_backend/agents/ri_communication_agent.py @@ -86,6 +86,9 @@ class RICommunicationAgent(Agent): # See what endpoint we received match message["endpoint"]: case "ping": + topic = b"ping" + data = json.dumps(True).encode() + await self.agent._pub_socket.send_multipart([topic, data]) await asyncio.sleep(1) case _: logger.info( diff --git a/src/control_backend/api/v1/endpoints/robot.py b/src/control_backend/api/v1/endpoints/robot.py index aa1b532..b8291ac 100644 --- a/src/control_backend/api/v1/endpoints/robot.py +++ b/src/control_backend/api/v1/endpoints/robot.py @@ -45,7 +45,7 @@ async def ping_stream(request: Request): sub_socket = context.socket(zmq.SUB) sub_socket.connect(settings.zmq_settings.internal_comm_address) sub_socket.setsockopt(zmq.SUBSCRIBE, b"ping") - connected = True + connected = False ping_frequency = 1 # How many seconds between ping attempts @@ -68,7 +68,8 @@ async def ping_stream(request: Request): logger.debug(f"Yielded new connection event in robot ping router: {str(connected)}") - yield f"data: {str(connected)}, time:{str(datetime.datetime.now().strftime("%H:%M:%S"))}\n\n" + falseJson = json.dumps(connected) + yield (f"data: {falseJson}\n\n") return StreamingResponse(event_stream(), media_type="text/event-stream") \ No newline at end of file From 30453be4b20ae910a98a3d467bc68d4ba2ce63b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Otgaar?= Date: Thu, 30 Oct 2025 16:41:35 +0100 Subject: [PATCH 07/26] fix: ruff checks is now in order:) ref: N25B-205 --- .../agents/ri_command_agent.py | 3 +- .../agents/ri_communication_agent.py | 36 +++++---- src/control_backend/api/v1/endpoints/robot.py | 34 ++++----- src/control_backend/api/v1/router.py | 4 +- src/control_backend/main.py | 9 ++- src/control_backend/schemas/ri_message.py | 4 +- .../agents/test_ri_commands_agent.py | 8 +- .../agents/test_ri_communication_agent.py | 75 ++++++++++++++----- .../api/endpoints/test_robot_endpoint.py | 3 +- test/integration/schemas/test_ri_message.py | 25 ++----- 10 files changed, 117 insertions(+), 84 deletions(-) diff --git a/src/control_backend/agents/ri_command_agent.py b/src/control_backend/agents/ri_command_agent.py index 01fc824..51b8064 100644 --- a/src/control_backend/agents/ri_command_agent.py +++ b/src/control_backend/agents/ri_command_agent.py @@ -1,8 +1,9 @@ import json import logging + +import zmq from spade.agent import Agent from spade.behaviour import CyclicBehaviour -import zmq from control_backend.core.config import settings from control_backend.core.zmq_context import context diff --git a/src/control_backend/agents/ri_communication_agent.py b/src/control_backend/agents/ri_communication_agent.py index 79e44ed..0bb369d 100644 --- a/src/control_backend/agents/ri_communication_agent.py +++ b/src/control_backend/agents/ri_communication_agent.py @@ -1,23 +1,21 @@ import asyncio import json import logging -from spade.agent import Agent -from spade.behaviour import CyclicBehaviour + import zmq import zmq.asyncio +from spade.agent import Agent +from spade.behaviour import CyclicBehaviour - +from control_backend.agents.ri_command_agent import RICommandAgent from control_backend.core.config import settings from control_backend.core.zmq_context import context -from control_backend.schemas.ri_message import RIMessage -from control_backend.agents.ri_command_agent import RICommandAgent - logger = logging.getLogger(__name__) class RICommunicationAgent(Agent): - _pub_socket: zmq.asyncio.Socket + _pub_socket: zmq.asyncio.Socket req_socket: zmq.asyncio.Socket | None _address = "" _bind = True @@ -32,7 +30,6 @@ class RICommunicationAgent(Agent): verify_security: bool = False, address="tcp://localhost:0000", bind=False, - ): super().__init__(jid, password, port, verify_security) self._address = address @@ -54,9 +51,10 @@ class RICommunicationAgent(Agent): await asyncio.wait_for( self.agent.req_socket.send_json(message), timeout=seconds_to_wait_total / 2 ) - except TimeoutError as e: + except TimeoutError: logger.debug( - f"Waited too long to send message - we probably dont have any receivers... but let's check!" + "Waited too long to send message - " + "we probably dont have any receivers... but let's check!" ) # Wait up to three seconds for a reply:) @@ -67,8 +65,11 @@ class RICommunicationAgent(Agent): ) # We didnt get a reply :( - except TimeoutError as e: - logger.info(f"No ping back retrieved in {seconds_to_wait_total/2} seconds totalling {seconds_to_wait_total} of time, killing myself (or maybe just laying low).") + except TimeoutError: + logger.info( + f"No ping back retrieved in {seconds_to_wait_total / 2} seconds totalling" + f"{seconds_to_wait_total} of time, killing myself (or maybe just laying low)." + ) # TODO: Send event to UI letting know that we've lost connection topic = b"ping" data = json.dumps(False).encode() @@ -95,8 +96,7 @@ class RICommunicationAgent(Agent): "Received message with topic different than ping, while ping expected." ) - - async def setup_req_socket(self, force = False): + async def setup_req_socket(self, force=False): """ Sets up request socket for communication agent. """ @@ -107,7 +107,6 @@ class RICommunicationAgent(Agent): else: self.req_socket.connect(self._address) - async def setup(self, max_retries: int = 5): """ Try to setup the communication agent, we have 5 retries in case we dont have a response yet. @@ -116,15 +115,14 @@ class RICommunicationAgent(Agent): # Bind request socket await self.setup_req_socket() - + retries = 0 # Let's try a certain amount of times before failing connection while retries < max_retries: - # Make sure the socket is properly setup. if self.req_socket is None: continue - + # Send our message and receive one back:) message = {"endpoint": "negotiate/ports", "data": {}} await self.req_socket.send_json(message) @@ -132,7 +130,7 @@ class RICommunicationAgent(Agent): try: received_message = await asyncio.wait_for(self.req_socket.recv_json(), timeout=20.0) - except asyncio.TimeoutError: + except TimeoutError: logger.warning( "No connection established in 20 seconds (attempt %d/%d)", retries + 1, diff --git a/src/control_backend/api/v1/endpoints/robot.py b/src/control_backend/api/v1/endpoints/robot.py index b8291ac..e114757 100644 --- a/src/control_backend/api/v1/endpoints/robot.py +++ b/src/control_backend/api/v1/endpoints/robot.py @@ -1,18 +1,15 @@ -from fastapi import APIRouter, Request -from fastapi.responses import JSONResponse, StreamingResponse -import logging import asyncio -import zmq.asyncio import json -import datetime +import logging +import zmq.asyncio +from fastapi import APIRouter, Request +from fastapi.responses import StreamingResponse from zmq.asyncio import Socket -from control_backend.core.zmq_context import context + from control_backend.core.config import settings -from control_backend.schemas.ri_message import SpeechCommand, RIEndpoint - - - +from control_backend.core.zmq_context import context +from control_backend.schemas.ri_message import SpeechCommand logger = logging.getLogger(__name__) @@ -24,7 +21,7 @@ async def receive_command(command: SpeechCommand, request: Request): # Validate and retrieve data. SpeechCommand.model_validate(command) topic = b"command" - pub_socket : Socket = request.app.state.internal_comm_socket + pub_socket: Socket = request.app.state.internal_comm_socket pub_socket.send_multipart([topic, command.model_dump_json().encode()]) return {"status": "Command received"} @@ -38,6 +35,7 @@ async def ping(request: Request): @router.get("/ping_stream") async def ping_stream(request: Request): """Stream live updates whenever the device state changes.""" + async def event_stream(): # Set up internal socket to receive ping updates logger.debug("Ping stream router event stream entered.") @@ -47,7 +45,7 @@ async def ping_stream(request: Request): sub_socket.setsockopt(zmq.SUBSCRIBE, b"ping") connected = False - ping_frequency = 1 # How many seconds between ping attempts + ping_frequency = 1 # How many seconds between ping attempts # Even though its most likely the updates should alternate # So, True - False - True - False for connectivity. @@ -55,21 +53,21 @@ async def ping_stream(request: Request): while True: logger.debug("Ping stream entered listening ") try: - topic, body = await asyncio.wait_for(sub_socket.recv_multipart(), timeout=ping_frequency) + topic, body = await asyncio.wait_for( + sub_socket.recv_multipart(), timeout=ping_frequency + ) logger.debug("got ping change in ping_stream router") connected = json.loads(body) - except TimeoutError as e: + except TimeoutError: await asyncio.sleep(0.1) - + # Stop if client disconnected if await request.is_disconnected(): print("Client disconnected from SSE") break - logger.debug(f"Yielded new connection event in robot ping router: {str(connected)}") falseJson = json.dumps(connected) yield (f"data: {falseJson}\n\n") - - return StreamingResponse(event_stream(), media_type="text/event-stream") \ No newline at end of file + return StreamingResponse(event_stream(), media_type="text/event-stream") diff --git a/src/control_backend/api/v1/router.py b/src/control_backend/api/v1/router.py index dca7e27..5c48872 100644 --- a/src/control_backend/api/v1/router.py +++ b/src/control_backend/api/v1/router.py @@ -1,6 +1,6 @@ from fastapi.routing import APIRouter -from control_backend.api.v1.endpoints import message, sse, robot +from control_backend.api.v1.endpoints import message, robot, sse api_router = APIRouter() @@ -8,4 +8,4 @@ api_router.include_router(message.router, tags=["Messages"]) api_router.include_router(sse.router, tags=["SSE"]) -api_router.include_router(robot.router, prefix="/robot", tags=["Pings", "Commands"]) \ No newline at end of file +api_router.include_router(robot.router, prefix="/robot", tags=["Pings", "Commands"]) diff --git a/src/control_backend/main.py b/src/control_backend/main.py index bd0cc74..a824ab1 100644 --- a/src/control_backend/main.py +++ b/src/control_backend/main.py @@ -8,9 +8,10 @@ import zmq from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware +from control_backend.agents.bdi.bdi_core import BDICoreAgent + # Internal imports from control_backend.agents.ri_communication_agent import RICommunicationAgent -from control_backend.agents.bdi.bdi_core import BDICoreAgent from control_backend.api.v1.router import api_router from control_backend.core.config import settings from control_backend.core.zmq_context import context @@ -32,12 +33,14 @@ async def lifespan(app: FastAPI): # Initiate agents ri_communication_agent = RICommunicationAgent( - jid=settings.agent_settings.ri_communication_agent_name + "@" + settings.agent_settings.host, + jid=settings.agent_settings.ri_communication_agent_name + + "@" + + settings.agent_settings.host, password=settings.agent_settings.ri_communication_agent_name, pub_socket=internal_comm_socket, address="tcp://*:5555", bind=True, - ) + ) await ri_communication_agent.start() bdi_core = BDICoreAgent( diff --git a/src/control_backend/schemas/ri_message.py b/src/control_backend/schemas/ri_message.py index 97b7930..488b823 100644 --- a/src/control_backend/schemas/ri_message.py +++ b/src/control_backend/schemas/ri_message.py @@ -1,7 +1,7 @@ from enum import Enum -from typing import Any, Literal +from typing import Any -from pydantic import BaseModel, Field, ValidationError +from pydantic import BaseModel class RIEndpoint(str, Enum): diff --git a/test/integration/agents/test_ri_commands_agent.py b/test/integration/agents/test_ri_commands_agent.py index 219d682..4249401 100644 --- a/test/integration/agents/test_ri_commands_agent.py +++ b/test/integration/agents/test_ri_commands_agent.py @@ -1,10 +1,10 @@ -import asyncio -import zmq import json -import pytest from unittest.mock import AsyncMock, MagicMock, patch + +import pytest +import zmq + from control_backend.agents.ri_command_agent import RICommandAgent -from control_backend.schemas.ri_message import SpeechCommand @pytest.mark.asyncio diff --git a/test/integration/agents/test_ri_communication_agent.py b/test/integration/agents/test_ri_communication_agent.py index 9a7cb41..baeb717 100644 --- a/test/integration/agents/test_ri_communication_agent.py +++ b/test/integration/agents/test_ri_communication_agent.py @@ -1,6 +1,8 @@ import asyncio +from unittest.mock import ANY, AsyncMock, MagicMock, patch + import pytest -from unittest.mock import AsyncMock, MagicMock, patch, ANY + from control_backend.agents.ri_communication_agent import RICommunicationAgent @@ -109,7 +111,11 @@ async def test_setup_creates_socket_and_negotiate_1(monkeypatch): # --- Act --- agent = RICommunicationAgent( - "test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=False + "test@server", + "password", + pub_socket=fake_pub_socket, + address="tcp://localhost:5555", + bind=False, ) await agent.setup() @@ -153,7 +159,11 @@ async def test_setup_creates_socket_and_negotiate_2(monkeypatch): # --- Act --- agent = RICommunicationAgent( - "test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=False + "test@server", + "password", + pub_socket=fake_pub_socket, + address="tcp://localhost:5555", + bind=False, ) await agent.setup() @@ -189,8 +199,8 @@ async def test_setup_creates_socket_and_negotiate_3(monkeypatch, caplog): # Mock RICommandAgent agent startup - # We are sending wrong negotiation info to the communication agent, so we should retry and expect a - # better response, within a limited time. + # We are sending wrong negotiation info to the communication agent, + # so we should retry and expect a better response, within a limited time. with patch( "control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True ) as MockCommandAgent: @@ -200,7 +210,11 @@ async def test_setup_creates_socket_and_negotiate_3(monkeypatch, caplog): # --- Act --- with caplog.at_level("ERROR"): agent = RICommunicationAgent( - "test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=False + "test@server", + "password", + pub_socket=fake_pub_socket, + address="tcp://localhost:5555", + bind=False, ) await agent.setup(max_retries=1) @@ -240,7 +254,11 @@ async def test_setup_creates_socket_and_negotiate_4(monkeypatch): fake_pub_socket = AsyncMock() # --- Act --- agent = RICommunicationAgent( - "test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=True + "test@server", + "password", + pub_socket=fake_pub_socket, + address="tcp://localhost:5555", + bind=True, ) await agent.setup() @@ -283,7 +301,11 @@ async def test_setup_creates_socket_and_negotiate_5(monkeypatch): fake_pub_socket = AsyncMock() # --- Act --- agent = RICommunicationAgent( - "test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=False + "test@server", + "password", + pub_socket=fake_pub_socket, + address="tcp://localhost:5555", + bind=False, ) await agent.setup() @@ -326,7 +348,11 @@ async def test_setup_creates_socket_and_negotiate_6(monkeypatch): fake_pub_socket = AsyncMock() # --- Act --- agent = RICommunicationAgent( - "test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=False + "test@server", + "password", + pub_socket=fake_pub_socket, + address="tcp://localhost:5555", + bind=False, ) await agent.setup() @@ -362,8 +388,8 @@ async def test_setup_creates_socket_and_negotiate_7(monkeypatch, caplog): # Mock RICommandAgent agent startup - # We are sending wrong negotiation info to the communication agent, so we should retry and expect a - # better response, within a limited time. + # We are sending wrong negotiation info to the communication agent, + # so we should retry and expect a etter response, within a limited time. with patch( "control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True ) as MockCommandAgent: @@ -374,7 +400,11 @@ async def test_setup_creates_socket_and_negotiate_7(monkeypatch, caplog): # --- Act --- with caplog.at_level("WARNING"): agent = RICommunicationAgent( - "test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=False + "test@server", + "password", + pub_socket=fake_pub_socket, + address="tcp://localhost:5555", + bind=False, ) await agent.setup(max_retries=1) @@ -408,11 +438,15 @@ async def test_setup_creates_socket_and_negotiate_timeout(monkeypatch, caplog): fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() fake_pub_socket = AsyncMock() - + # --- Act --- with caplog.at_level("WARNING"): agent = RICommunicationAgent( - "test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=False + "test@server", + "password", + pub_socket=fake_pub_socket, + address="tcp://localhost:5555", + bind=False, ) await agent.setup(max_retries=1) @@ -544,13 +578,16 @@ async def test_setup_unexpected_exception(monkeypatch, caplog): # Simulate unexpected exception during recv_json() fake_socket.recv_json = AsyncMock(side_effect=Exception("boom!")) - monkeypatch.setattr( "control_backend.agents.ri_communication_agent.context.socket", lambda _: fake_socket ) agent = RICommunicationAgent( - "test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=False + "test@server", + "password", + pub_socket=fake_pub_socket, + address="tcp://localhost:5555", + bind=False, ) with caplog.at_level("ERROR"): @@ -587,7 +624,11 @@ async def test_setup_unpacking_exception(monkeypatch, caplog): fake_pub_socket = AsyncMock() agent = RICommunicationAgent( - "test@server", "password", pub_socket=fake_pub_socket, address="tcp://localhost:5555", bind=False + "test@server", + "password", + pub_socket=fake_pub_socket, + address="tcp://localhost:5555", + bind=False, ) # --- Act & Assert --- diff --git a/test/integration/api/endpoints/test_robot_endpoint.py b/test/integration/api/endpoints/test_robot_endpoint.py index 827fb17..3fd175f 100644 --- a/test/integration/api/endpoints/test_robot_endpoint.py +++ b/test/integration/api/endpoints/test_robot_endpoint.py @@ -1,7 +1,8 @@ +from unittest.mock import MagicMock + import pytest from fastapi import FastAPI from fastapi.testclient import TestClient -from unittest.mock import MagicMock from control_backend.api.v1.endpoints import robot from control_backend.schemas.ri_message import SpeechCommand diff --git a/test/integration/schemas/test_ri_message.py b/test/integration/schemas/test_ri_message.py index aef9ae6..966b582 100644 --- a/test/integration/schemas/test_ri_message.py +++ b/test/integration/schemas/test_ri_message.py @@ -1,7 +1,8 @@ import pytest -from control_backend.schemas.ri_message import RIMessage, RIEndpoint, SpeechCommand from pydantic import ValidationError +from control_backend.schemas.ri_message import RIEndpoint, RIMessage, SpeechCommand + def valid_command_1(): return SpeechCommand(data="Hallo?") @@ -13,24 +14,14 @@ def invalid_command_1(): def test_valid_speech_command_1(): command = valid_command_1() - try: - RIMessage.model_validate(command) - SpeechCommand.model_validate(command) - assert True - except ValidationError: - assert False + RIMessage.model_validate(command) + SpeechCommand.model_validate(command) + assert True def test_invalid_speech_command_1(): command = invalid_command_1() - passed_ri_message_validation = False - try: - # Should succeed, still. - RIMessage.model_validate(command) - passed_ri_message_validation = True - - # Should fail. + RIMessage.model_validate(command) + with pytest.raises(ValidationError): SpeechCommand.model_validate(command) - assert False - except ValidationError: - assert passed_ri_message_validation + assert True From ca8b57fec51396a63505faafc76beb777bbd8660 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Otgaar?= Date: Wed, 5 Nov 2025 16:59:36 +0100 Subject: [PATCH 08/26] fix: robot pings to router ref: N25B-256 --- src/control_backend/api/v1/endpoints/robot.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/control_backend/api/v1/endpoints/robot.py b/src/control_backend/api/v1/endpoints/robot.py index b2ca053..96e32ac 100644 --- a/src/control_backend/api/v1/endpoints/robot.py +++ b/src/control_backend/api/v1/endpoints/robot.py @@ -47,7 +47,7 @@ async def ping_stream(request: Request): sub_socket.setsockopt(zmq.SUBSCRIBE, b"ping") connected = False - ping_frequency = 1 # How many seconds between ping attempts + ping_frequency = 2 # Even though its most likely the updates should alternate # So, True - False - True - False for connectivity. @@ -58,9 +58,10 @@ async def ping_stream(request: Request): topic, body = await asyncio.wait_for( sub_socket.recv_multipart(), timeout=ping_frequency ) - logger.debug("got ping change in ping_stream router") + logger.debug(f"got ping change in ping_stream router: {body}") connected = json.loads(body) except TimeoutError: + logger.debug("got timeout error in ping loop in ping router") await asyncio.sleep(0.1) # Stop if client disconnected @@ -69,7 +70,7 @@ async def ping_stream(request: Request): break logger.debug(f"Yielded new connection event in robot ping router: {str(connected)}") - falseJson = json.dumps(connected) - yield (f"data: {falseJson}\n\n") + connectedJson = json.dumps(connected) + yield (f"data: {connectedJson}\n\n") return StreamingResponse(event_stream(), media_type="text/event-stream") From feb6875a4c3f9a9ef133495a630cf46443785d1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Otgaar?= Date: Thu, 6 Nov 2025 14:16:55 +0100 Subject: [PATCH 09/26] fix: make sure that the communication agent reboots propperly. ref: N25B-256 --- .../agents/ri_communication_agent.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/control_backend/agents/ri_communication_agent.py b/src/control_backend/agents/ri_communication_agent.py index fe99ad4..8d72c8a 100644 --- a/src/control_backend/agents/ri_communication_agent.py +++ b/src/control_backend/agents/ri_communication_agent.py @@ -39,6 +39,10 @@ class RICommunicationAgent(BaseAgent): """ assert self.agent is not None + if not self.agent.connected: + await asyncio.sleep(1) + return + # We need to listen and sent pings. message = {"endpoint": "ping", "data": {"id": "e.g. some reference id"}} seconds_to_wait_total = 1.0 @@ -63,7 +67,13 @@ class RICommunicationAgent(BaseAgent): # We didnt get a reply :( except TimeoutError: - self.agent.logger.info("No ping retrieved in 3 seconds, killing myself.") + self.agent.logger.info( + f"No ping retrieved in {seconds_to_wait_total} seconds, " + "sending UI disconnection event and soft killing myself." + ) + + # Make sure we dont retry receiving messages untill we're setup. + self.agent.connected = False # Tell UI we're disconnected. topic = b"ping" @@ -84,7 +94,7 @@ class RICommunicationAgent(BaseAgent): ) # Try to reboot. - self.agent.setup() + await self.agent.setup() self.agent.logger.debug('Received message "%s"', message) if "endpoint" not in message: @@ -111,12 +121,11 @@ class RICommunicationAgent(BaseAgent): # Bind request socket if self._req_socket is None or force: self._req_socket = Context.instance().socket(zmq.REQ) - if self._bind: # TODO: Should this ever be the case with new architecture? + if self._bind: self._req_socket.bind(self._address) else: self._req_socket.connect(self._address) - # TODO: Check with Kasper if self.pub_socket is None or force: self.pub_socket = Context.instance().socket(zmq.PUB) self.pub_socket.connect(settings.zmq_settings.internal_pub_address) @@ -231,5 +240,7 @@ class RICommunicationAgent(BaseAgent): self.logger.error( "Initial connection ping for router timed out in ri_communication_agent." ) + + # Make sure to start listening now that we're connected. self.connected = True self.logger.info("Finished setting up %s", self.jid) From be5dc7f04b2e036a3c9ea8e250153a0de039d8ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Otgaar?= Date: Thu, 6 Nov 2025 14:26:02 +0100 Subject: [PATCH 10/26] fix: fixed integration tests due to new change ref: N25B-256 --- test/integration/agents/test_ri_communication_agent.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/integration/agents/test_ri_communication_agent.py b/test/integration/agents/test_ri_communication_agent.py index 33051c8..a82a837 100644 --- a/test/integration/agents/test_ri_communication_agent.py +++ b/test/integration/agents/test_ri_communication_agent.py @@ -428,6 +428,7 @@ async def test_listen_behaviour_ping_correct(caplog): # TODO: Integration test between actual server and password needed for spade agents agent = RICommunicationAgent("test@server", "password") agent._req_socket = fake_socket + agent.connected = True behaviour = agent.ListenBehaviour() agent.add_behaviour(behaviour) @@ -463,6 +464,7 @@ async def test_listen_behaviour_ping_wrong_endpoint(caplog): agent = RICommunicationAgent("test@server", "password", fake_pub_socket) agent._req_socket = fake_socket + agent.connected = True behaviour = agent.ListenBehaviour() agent.add_behaviour(behaviour) @@ -486,6 +488,7 @@ async def test_listen_behaviour_timeout(zmq_context, caplog): agent = RICommunicationAgent("test@server", "password") agent._req_socket = fake_socket + agent.connected = True behaviour = agent.ListenBehaviour() agent.add_behaviour(behaviour) @@ -514,6 +517,7 @@ async def test_listen_behaviour_ping_no_endpoint(caplog): agent = RICommunicationAgent("test@server", "password") agent._req_socket = fake_socket + agent.connected = True behaviour = agent.ListenBehaviour() agent.add_behaviour(behaviour) From 6cc03efdaf911b39763f1e28698fff55a26bf21d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Otgaar?= Date: Thu, 6 Nov 2025 14:42:02 +0100 Subject: [PATCH 11/26] feat: new integration tests for robot, making sure to get 100% code coverage ref: N25B-256 --- src/control_backend/api/v1/endpoints/robot.py | 1 - .../api/endpoints/test_robot_endpoint.py | 97 ++++++++++++++++++- 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/src/control_backend/api/v1/endpoints/robot.py b/src/control_backend/api/v1/endpoints/robot.py index 96e32ac..dfa7332 100644 --- a/src/control_backend/api/v1/endpoints/robot.py +++ b/src/control_backend/api/v1/endpoints/robot.py @@ -41,7 +41,6 @@ async def ping_stream(request: Request): # Set up internal socket to receive ping updates logger.debug("Ping stream router event stream entered.") - # TODO: Check with Kasper sub_socket = Context.instance().socket(zmq.SUB) sub_socket.connect(settings.zmq_settings.internal_sub_address) sub_socket.setsockopt(zmq.SUBSCRIBE, b"ping") diff --git a/test/integration/api/endpoints/test_robot_endpoint.py b/test/integration/api/endpoints/test_robot_endpoint.py index 3a2df88..0f71951 100644 --- a/test/integration/api/endpoints/test_robot_endpoint.py +++ b/test/integration/api/endpoints/test_robot_endpoint.py @@ -1,4 +1,5 @@ -from unittest.mock import AsyncMock +import json +from unittest.mock import AsyncMock, MagicMock import pytest from fastapi import FastAPI @@ -59,3 +60,97 @@ def test_receive_command_invalid_payload(client): bad_payload = {"invalid": "data"} response = client.post("/command", json=bad_payload) assert response.status_code == 422 # validation error + + +def test_ping_check_returns_none(client): + """Ensure /ping_check returns 200 and None (currently unimplemented).""" + response = client.get("/ping_check") + assert response.status_code == 200 + assert response.json() is None + + +@pytest.mark.asyncio +async def test_ping_stream_yields_ping_event(monkeypatch): + """Test that ping_stream yields a proper SSE message when a ping is received.""" + mock_sub_socket = AsyncMock() + mock_sub_socket.connect = MagicMock() + mock_sub_socket.setsockopt = MagicMock() + mock_sub_socket.recv_multipart = AsyncMock(return_value=[b"ping", b"true"]) + + mock_context = MagicMock() + mock_context.socket.return_value = mock_sub_socket + monkeypatch.setattr(robot.Context, "instance", lambda: mock_context) + + mock_request = AsyncMock() + mock_request.is_disconnected = AsyncMock(side_effect=[False, True]) + + response = await robot.ping_stream(mock_request) + generator = aiter(response.body_iterator) + + event = await anext(generator) + event_text = event.decode() if isinstance(event, bytes) else str(event) + assert event_text.strip() == "data: true" + + with pytest.raises(StopAsyncIteration): + await anext(generator) + + mock_sub_socket.connect.assert_called_once() + mock_sub_socket.setsockopt.assert_called_once_with(robot.zmq.SUBSCRIBE, b"ping") + mock_sub_socket.recv_multipart.assert_awaited() + + +@pytest.mark.asyncio +async def test_ping_stream_handles_timeout(monkeypatch): + """Test that ping_stream continues looping on TimeoutError.""" + mock_sub_socket = AsyncMock() + mock_sub_socket.connect = MagicMock() + mock_sub_socket.setsockopt = MagicMock() + mock_sub_socket.recv_multipart.side_effect = TimeoutError() + + mock_context = MagicMock() + mock_context.socket.return_value = mock_sub_socket + monkeypatch.setattr(robot.Context, "instance", lambda: mock_context) + + mock_request = AsyncMock() + mock_request.is_disconnected = AsyncMock(return_value=True) + + response = await robot.ping_stream(mock_request) + generator = aiter(response.body_iterator) + + with pytest.raises(StopAsyncIteration): + await anext(generator) + + mock_sub_socket.connect.assert_called_once() + mock_sub_socket.setsockopt.assert_called_once_with(robot.zmq.SUBSCRIBE, b"ping") + mock_sub_socket.recv_multipart.assert_awaited() + + +@pytest.mark.asyncio +async def test_ping_stream_yields_json_values(monkeypatch): + """Ensure ping_stream correctly parses and yields JSON body values.""" + mock_sub_socket = AsyncMock() + mock_sub_socket.connect = MagicMock() + mock_sub_socket.setsockopt = MagicMock() + mock_sub_socket.recv_multipart = AsyncMock( + return_value=[b"ping", json.dumps({"connected": True}).encode()] + ) + + mock_context = MagicMock() + mock_context.socket.return_value = mock_sub_socket + monkeypatch.setattr(robot.Context, "instance", lambda: mock_context) + + mock_request = AsyncMock() + mock_request.is_disconnected = AsyncMock(side_effect=[False, True]) + + response = await robot.ping_stream(mock_request) + generator = aiter(response.body_iterator) + + event = await anext(generator) + event_text = event.decode() if isinstance(event, bytes) else str(event) + + assert "connected" in event_text + assert "true" in event_text + + mock_sub_socket.connect.assert_called_once() + mock_sub_socket.setsockopt.assert_called_once_with(robot.zmq.SUBSCRIBE, b"ping") + mock_sub_socket.recv_multipart.assert_awaited() From 2d1a25e4ae4f97e262006c418cc9a594e47ed516 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Otgaar?= Date: Thu, 6 Nov 2025 14:49:54 +0100 Subject: [PATCH 12/26] chore: fixing up logging messages --- src/control_backend/agents/ri_communication_agent.py | 7 ++----- src/control_backend/api/v1/endpoints/robot.py | 3 --- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/control_backend/agents/ri_communication_agent.py b/src/control_backend/agents/ri_communication_agent.py index 8d72c8a..960a4b8 100644 --- a/src/control_backend/agents/ri_communication_agent.py +++ b/src/control_backend/agents/ri_communication_agent.py @@ -56,11 +56,8 @@ class RICommunicationAgent(BaseAgent): "we probably dont have any receivers... but let's check!" ) - # Wait up to three seconds for a reply:) + # Wait up to {seconds_to_wait_total/2} seconds for a reply:) try: - self.agent.logger.debug( - f"waiting for message for{seconds_to_wait_total / 2} seconds." - ) message = await asyncio.wait_for( self.agent._req_socket.recv_json(), timeout=seconds_to_wait_total / 2 ) @@ -96,7 +93,7 @@ class RICommunicationAgent(BaseAgent): # Try to reboot. await self.agent.setup() - self.agent.logger.debug('Received message "%s"', message) + self.agent.logger.debug(f'Received message "{message}" from RI.') if "endpoint" not in message: self.agent.logger.error("No received endpoint in message, excepted ping endpoint.") return diff --git a/src/control_backend/api/v1/endpoints/robot.py b/src/control_backend/api/v1/endpoints/robot.py index dfa7332..ccc6bd6 100644 --- a/src/control_backend/api/v1/endpoints/robot.py +++ b/src/control_backend/api/v1/endpoints/robot.py @@ -39,7 +39,6 @@ async def ping_stream(request: Request): async def event_stream(): # Set up internal socket to receive ping updates - logger.debug("Ping stream router event stream entered.") sub_socket = Context.instance().socket(zmq.SUB) sub_socket.connect(settings.zmq_settings.internal_sub_address) @@ -52,12 +51,10 @@ async def ping_stream(request: Request): # So, True - False - True - False for connectivity. # Let's still check:) while True: - logger.debug("Ping stream entered listening ") try: topic, body = await asyncio.wait_for( sub_socket.recv_multipart(), timeout=ping_frequency ) - logger.debug(f"got ping change in ping_stream router: {body}") connected = json.loads(body) except TimeoutError: logger.debug("got timeout error in ping loop in ping router") From debc87c0bb4491a1d984fd476e86cb68e655325f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Otgaar?= Date: Tue, 11 Nov 2025 10:18:43 +0100 Subject: [PATCH 13/26] fix: Fix up merging request changes and make sure that there is no racing condition errors, and UI always gets correct information. ref: N25B-256 --- .../agents/ri_communication_agent.py | 47 ++++++---- src/control_backend/api/v1/endpoints/robot.py | 9 +- .../agents/test_ri_communication_agent.py | 86 ++++++++----------- 3 files changed, 67 insertions(+), 75 deletions(-) diff --git a/src/control_backend/agents/ri_communication_agent.py b/src/control_backend/agents/ri_communication_agent.py index 960a4b8..b489338 100644 --- a/src/control_backend/agents/ri_communication_agent.py +++ b/src/control_backend/agents/ri_communication_agent.py @@ -56,28 +56,29 @@ class RICommunicationAgent(BaseAgent): "we probably dont have any receivers... but let's check!" ) - # Wait up to {seconds_to_wait_total/2} seconds for a reply:) + # Wait up to {seconds_to_wait_total/2} seconds for a reply try: message = await asyncio.wait_for( self.agent._req_socket.recv_json(), timeout=seconds_to_wait_total / 2 ) - # We didnt get a reply :( + # We didnt get a reply except TimeoutError: self.agent.logger.info( f"No ping retrieved in {seconds_to_wait_total} seconds, " - "sending UI disconnection event and soft killing myself." + "sending UI disconnection event and attempting to restart." ) # Make sure we dont retry receiving messages untill we're setup. self.agent.connected = False + self.agent.remove_behaviour(self) # Tell UI we're disconnected. topic = b"ping" data = json.dumps(False).encode() if self.agent.pub_socket is None: self.agent.logger.error( - "communication agent pub socket not correctly initialized." + "Communication agent pub socket not correctly initialized." ) else: try: @@ -85,17 +86,20 @@ class RICommunicationAgent(BaseAgent): self.agent.pub_socket.send_multipart([topic, data]), 5 ) except TimeoutError: - self.agent.logger.error( + self.agent.logger.warning( "Initial connection ping for router timed" " out in ri_communication_agent." ) # Try to reboot. + self.agent.logger.debug("Restarting communication agent.") await self.agent.setup() self.agent.logger.debug(f'Received message "{message}" from RI.') if "endpoint" not in message: - self.agent.logger.error("No received endpoint in message, excepted ping endpoint.") + self.agent.logger.warning( + "No received endpoint in message, expected ping endpoint." + ) return # See what endpoint we received @@ -107,7 +111,7 @@ class RICommunicationAgent(BaseAgent): await self.agent.pub_socket.send_multipart([topic, data]) await asyncio.sleep(1) case _: - self.agent.logger.info( + self.agent.logger.debug( "Received message with topic different than ping, while ping expected." ) @@ -143,16 +147,20 @@ class RICommunicationAgent(BaseAgent): if self._req_socket is None: continue - # Send our message and receive one back:) + # Send our message and receive one back message = {"endpoint": "negotiate/ports", "data": {}} await self._req_socket.send_json(message) + retry_frequency = 1.0 try: - received_message = await asyncio.wait_for(self._req_socket.recv_json(), timeout=1.0) + received_message = await asyncio.wait_for( + self._req_socket.recv_json(), timeout=retry_frequency + ) except TimeoutError: self.logger.warning( - "No connection established in 20 seconds (attempt %d/%d)", + "No connection established in %d seconds (attempt %d/%d)", + retries * retry_frequency, retries + 1, max_retries, ) @@ -160,21 +168,21 @@ class RICommunicationAgent(BaseAgent): continue except Exception as e: - self.logger.error("Unexpected error during negotiation: %s", e) + self.logger.warning("Unexpected error during negotiation: %s", e) retries += 1 continue # Validate endpoint endpoint = received_message.get("endpoint") if endpoint != "negotiate/ports": - # TODO: Should this send a message back? - self.logger.error( + self.logger.warning( "Invalid endpoint '%s' received (attempt %d/%d)", endpoint, retries + 1, max_retries, ) retries += 1 + await asyncio.sleep(1) continue # At this point, we have a valid response @@ -194,7 +202,7 @@ class RICommunicationAgent(BaseAgent): if addr != self._address: if not bind: self._req_socket.connect(addr) - else: # TODO: Should this ever be the case? + else: self._req_socket.bind(addr) case "actuation": ri_commands_agent = RICommandAgent( @@ -210,31 +218,32 @@ class RICommunicationAgent(BaseAgent): self.logger.warning("Unhandled negotiation id: %s", id) except Exception as e: - self.logger.error("Error unpacking negotiation data: %s", e) + self.logger.warning("Error unpacking negotiation data: %s", e) retries += 1 + await asyncio.sleep(1) continue # setup succeeded break else: - self.logger.error("Failed to set up RICommunicationAgent after %d retries", max_retries) + self.logger.error("Failed to set up %s after %d retries", self.name, max_retries) return # Set up ping behaviour listen_behaviour = self.ListenBehaviour() self.add_behaviour(listen_behaviour) - # Let UI know that we're connected >:) + # Let UI know that we're connected topic = b"ping" data = json.dumps(True).encode() if self.pub_socket is None: - self.logger.error("communication agent pub socket not correctly initialized.") + self.logger.error("Communication agent pub socket not correctly initialized.") else: try: await asyncio.wait_for(self.pub_socket.send_multipart([topic, data]), 5) except TimeoutError: - self.logger.error( + self.logger.warning( "Initial connection ping for router timed out in ri_communication_agent." ) diff --git a/src/control_backend/api/v1/endpoints/robot.py b/src/control_backend/api/v1/endpoints/robot.py index ccc6bd6..eb67b0e 100644 --- a/src/control_backend/api/v1/endpoints/robot.py +++ b/src/control_backend/api/v1/endpoints/robot.py @@ -21,7 +21,6 @@ async def receive_command(command: SpeechCommand, request: Request): SpeechCommand.model_validate(command) topic = b"command" - # TODO: Check with Kasper pub_socket: Socket = request.app.state.endpoints_pub_socket await pub_socket.send_multipart([topic, command.model_dump_json().encode()]) @@ -48,8 +47,8 @@ async def ping_stream(request: Request): ping_frequency = 2 # Even though its most likely the updates should alternate - # So, True - False - True - False for connectivity. - # Let's still check:) + # (So, True - False - True - False for connectivity), + # let's still check. while True: try: topic, body = await asyncio.wait_for( @@ -58,11 +57,11 @@ async def ping_stream(request: Request): connected = json.loads(body) except TimeoutError: logger.debug("got timeout error in ping loop in ping router") - await asyncio.sleep(0.1) + connected = False # Stop if client disconnected if await request.is_disconnected(): - print("Client disconnected from SSE") + logger.info("Client disconnected from SSE") break logger.debug(f"Yielded new connection event in robot ping router: {str(connected)}") diff --git a/test/integration/agents/test_ri_communication_agent.py b/test/integration/agents/test_ri_communication_agent.py index a82a837..1925afa 100644 --- a/test/integration/agents/test_ri_communication_agent.py +++ b/test/integration/agents/test_ri_communication_agent.py @@ -196,14 +196,14 @@ async def test_setup_creates_socket_and_negotiate_3(zmq_context, caplog): fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() # --- Act --- - with caplog.at_level("ERROR"): - agent = RICommunicationAgent( - "test@server", - "password", - address="tcp://localhost:5555", - bind=False, - ) - await agent.setup(max_retries=1) + + agent = RICommunicationAgent( + "test@server", + "password", + address="tcp://localhost:5555", + bind=False, + ) + await agent.setup(max_retries=1) # --- Assert --- fake_socket.connect.assert_any_call("tcp://localhost:5555") @@ -211,7 +211,6 @@ async def test_setup_creates_socket_and_negotiate_3(zmq_context, caplog): # Since it failed, there should not be any command agent. fake_agent_instance.start.assert_not_awaited() - assert "Failed to set up RICommunicationAgent" in caplog.text # Ensure the agent did not attach a ListenBehaviour assert not any(isinstance(b, agent.ListenBehaviour) for b in agent.behaviours) @@ -362,14 +361,14 @@ async def test_setup_creates_socket_and_negotiate_7(zmq_context, caplog): fake_agent_instance.start = AsyncMock() # --- Act --- - with caplog.at_level("WARNING"): - agent = RICommunicationAgent( - "test@server", - "password", - address="tcp://localhost:5555", - bind=False, - ) - await agent.setup(max_retries=1) + + agent = RICommunicationAgent( + "test@server", + "password", + address="tcp://localhost:5555", + bind=False, + ) + await agent.setup(max_retries=1) # --- Assert --- fake_socket.connect.assert_any_call("tcp://localhost:5555") @@ -377,7 +376,6 @@ async def test_setup_creates_socket_and_negotiate_7(zmq_context, caplog): # Since it failed, there should not be any command agent. fake_agent_instance.start.assert_not_awaited() - assert "Unhandled negotiation id:" in caplog.text @pytest.mark.asyncio @@ -398,21 +396,20 @@ async def test_setup_creates_socket_and_negotiate_timeout(zmq_context, caplog): fake_agent_instance.start = AsyncMock() # --- Act --- - with caplog.at_level("WARNING"): - agent = RICommunicationAgent( - "test@server", - "password", - address="tcp://localhost:5555", - bind=False, - ) - await agent.setup(max_retries=1) + + agent = RICommunicationAgent( + "test@server", + "password", + address="tcp://localhost:5555", + bind=False, + ) + await agent.setup(max_retries=1) # --- Assert --- fake_socket.connect.assert_any_call("tcp://localhost:5555") # Since it failed, there should not be any command agent. fake_agent_instance.start.assert_not_awaited() - assert "No connection established in 20 seconds" in caplog.text # Ensure the agent did not attach a ListenBehaviour assert not any(isinstance(b, agent.ListenBehaviour) for b in agent.behaviours) @@ -425,7 +422,6 @@ async def test_listen_behaviour_ping_correct(caplog): fake_socket.recv_json = AsyncMock(return_value={"endpoint": "ping", "data": {}}) fake_socket.send_multipart = AsyncMock() - # TODO: Integration test between actual server and password needed for spade agents agent = RICommunicationAgent("test@server", "password") agent._req_socket = fake_socket agent.connected = True @@ -433,13 +429,10 @@ async def test_listen_behaviour_ping_correct(caplog): behaviour = agent.ListenBehaviour() agent.add_behaviour(behaviour) - # Run once (CyclicBehaviour normally loops) - with caplog.at_level("DEBUG"): - await behaviour.run() + await behaviour.run() fake_socket.send_json.assert_awaited() fake_socket.recv_json.assert_awaited() - assert "Received message" in caplog.text @pytest.mark.asyncio @@ -470,10 +463,9 @@ async def test_listen_behaviour_ping_wrong_endpoint(caplog): agent.add_behaviour(behaviour) # Run once (CyclicBehaviour normally loops) - with caplog.at_level("INFO"): - await behaviour.run() - assert "Received message with topic different than ping, while ping expected." in caplog.text + await behaviour.run() + fake_socket.send_json.assert_awaited() fake_socket.recv_json.assert_awaited() @@ -493,10 +485,9 @@ async def test_listen_behaviour_timeout(zmq_context, caplog): behaviour = agent.ListenBehaviour() agent.add_behaviour(behaviour) - with caplog.at_level("INFO"): - await behaviour.run() - - assert "No ping" in caplog.text + await behaviour.run() + assert not any(isinstance(b, agent.ListenBehaviour) for b in agent.behaviours) + assert not agent.connected @pytest.mark.asyncio @@ -522,11 +513,8 @@ async def test_listen_behaviour_ping_no_endpoint(caplog): behaviour = agent.ListenBehaviour() agent.add_behaviour(behaviour) - # Run once (CyclicBehaviour normally loops) - with caplog.at_level("ERROR"): - await behaviour.run() + await behaviour.run() - assert "No received endpoint in message, excepted ping endpoint." in caplog.text fake_socket.send_json.assert_awaited() fake_socket.recv_json.assert_awaited() @@ -546,11 +534,10 @@ async def test_setup_unexpected_exception(zmq_context, caplog): bind=False, ) - with caplog.at_level("ERROR"): - await agent.setup(max_retries=1) + await agent.setup(max_retries=1) - # Ensure that the error was logged - assert "Unexpected error during negotiation: boom!" in caplog.text + assert not any(isinstance(b, agent.ListenBehaviour) for b in agent.behaviours) + assert not agent.connected @pytest.mark.asyncio @@ -582,11 +569,8 @@ async def test_setup_unpacking_exception(zmq_context, caplog): ) # --- Act & Assert --- - with caplog.at_level("ERROR"): - await agent.setup(max_retries=1) - # Ensure the unpacking exception was logged - assert "Error unpacking negotiation data" in caplog.text + await agent.setup(max_retries=1) # Ensure no command agent was started fake_agent_instance.start.assert_not_awaited() From 0e45383027c1226c819c24143d986dad16aca3fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Otgaar?= Date: Wed, 12 Nov 2025 11:04:49 +0100 Subject: [PATCH 14/26] refactor: rename all agents and improve structure pt1 ref: N25B-257 --- src/control_backend/agents/__init__.py | 6 - .../agents/act_agents/__init__.py | 1 + .../act_speech_agent.py} | 2 +- src/control_backend/agents/bdi/__init__.py | 2 - .../agents/bdi_agents/__init__.py | 1 + .../bdi_core_agent.py} | 0 .../behaviours/belief_setter.py | 2 +- .../behaviours/receive_llm_resp_behaviour.py | 2 +- .../agents/{bdi => bdi_agents}/rules.asl | 0 .../bel_collector_agent/__init__.py | 1 + .../behaviours/continuous_collect.py | 6 +- .../bel_collector_agent.py} | 6 +- .../behaviours/text_belief_extractor.py | 6 +- .../bel_text_extract_agent.py} | 2 +- .../agents/com_agents/__init__.py | 1 + .../com_ri_agent.py} | 12 +- .../agents/llm_agents/__init__.py | 1 + .../{llm/llm.py => llm_agents/llm_agent.py} | 6 +- .../{llm => llm_agents}/llm_instructions.py | 0 ...{belief_text_mock.py => bel_text_agent.py} | 6 +- .../agents/per_agents/__init__.py | 4 + .../per_transcription_agent.py} | 12 +- .../speech_recognizer.py | 0 .../per_vad_agent.py} | 10 +- src/control_backend/core/config.py | 14 +-- src/control_backend/main.py | 46 ++++---- .../speech_with_pauses_16k_1c_float32.wav | Bin .../test_per_vad_agent.py} | 64 ++++++----- .../test_per_vad_with_audio.py} | 6 +- ...ands_agent.py => test_act_speech_agent.py} | 26 +++-- ...nication_agent.py => test_com_ri_agent.py} | 106 +++++++----------- .../bdi/behaviours/test_belief_setter.py | 8 +- .../behaviours/test_continuous_collect.py | 8 +- .../behaviours/test_belief_from_text.py | 17 +-- test/unit/agents/test_vad_socket_poller.py | 10 +- test/unit/agents/test_vad_streaming.py | 4 +- .../transcription/test_speech_recognizer.py | 2 +- 37 files changed, 199 insertions(+), 201 deletions(-) create mode 100644 src/control_backend/agents/act_agents/__init__.py rename src/control_backend/agents/{ri_command_agent.py => act_agents/act_speech_agent.py} (98%) delete mode 100644 src/control_backend/agents/bdi/__init__.py create mode 100644 src/control_backend/agents/bdi_agents/__init__.py rename src/control_backend/agents/{bdi/bdi_core.py => bdi_agents/bdi_core_agent.py} (100%) rename src/control_backend/agents/{bdi => bdi_agents}/behaviours/belief_setter.py (97%) rename src/control_backend/agents/{bdi => bdi_agents}/behaviours/receive_llm_resp_behaviour.py (94%) rename src/control_backend/agents/{bdi => bdi_agents}/rules.asl (100%) create mode 100644 src/control_backend/agents/bel_agents/bel_collector_agent/__init__.py rename src/control_backend/agents/{belief_collector => bel_agents/bel_collector_agent}/behaviours/continuous_collect.py (95%) rename src/control_backend/agents/{belief_collector/belief_collector.py => bel_agents/bel_collector_agent/bel_collector_agent.py} (63%) rename src/control_backend/agents/{bdi => bel_agents/bel_text_extract_agent}/behaviours/text_belief_extractor.py (94%) rename src/control_backend/agents/{bdi/text_extractor.py => bel_agents/bel_text_extract_agent/bel_text_extract_agent.py} (82%) create mode 100644 src/control_backend/agents/com_agents/__init__.py rename src/control_backend/agents/{ri_communication_agent.py => com_agents/com_ri_agent.py} (92%) create mode 100644 src/control_backend/agents/llm_agents/__init__.py rename src/control_backend/agents/{llm/llm.py => llm_agents/llm_agent.py} (97%) rename src/control_backend/agents/{llm => llm_agents}/llm_instructions.py (100%) rename src/control_backend/agents/mock_agents/{belief_text_mock.py => bel_text_agent.py} (89%) create mode 100644 src/control_backend/agents/per_agents/__init__.py rename src/control_backend/agents/{transcription/transcription_agent.py => per_agents/per_transcription_agent/per_transcription_agent.py} (89%) rename src/control_backend/agents/{transcription => per_agents/per_transcription_agent}/speech_recognizer.py (100%) rename src/control_backend/agents/{vad_agent.py => per_agents/per_vad_agent.py} (94%) rename test/integration/agents/{vad_agent => per_vad_agent}/speech_with_pauses_16k_1c_float32.wav (100%) rename test/integration/agents/{vad_agent/test_vad_agent.py => per_vad_agent/test_per_vad_agent.py} (54%) rename test/integration/agents/{vad_agent/test_vad_with_audio.py => per_vad_agent/test_per_vad_with_audio.py} (89%) rename test/integration/agents/{test_ri_commands_agent.py => test_act_speech_agent.py} (77%) rename test/integration/agents/{test_ri_communication_agent.py => test_com_ri_agent.py} (84%) diff --git a/src/control_backend/agents/__init__.py b/src/control_backend/agents/__init__.py index 65ee335..1618d55 100644 --- a/src/control_backend/agents/__init__.py +++ b/src/control_backend/agents/__init__.py @@ -1,7 +1 @@ from .base import BaseAgent as BaseAgent -from .belief_collector.belief_collector import BeliefCollectorAgent as BeliefCollectorAgent -from .llm.llm import LLMAgent as LLMAgent -from .ri_command_agent import RICommandAgent as RICommandAgent -from .ri_communication_agent import RICommunicationAgent as RICommunicationAgent -from .transcription.transcription_agent import TranscriptionAgent as TranscriptionAgent -from .vad_agent import VADAgent as VADAgent diff --git a/src/control_backend/agents/act_agents/__init__.py b/src/control_backend/agents/act_agents/__init__.py new file mode 100644 index 0000000..c09a449 --- /dev/null +++ b/src/control_backend/agents/act_agents/__init__.py @@ -0,0 +1 @@ +from .act_speech_agent import ActSpeechAgent as ActSpeechAgent diff --git a/src/control_backend/agents/ri_command_agent.py b/src/control_backend/agents/act_agents/act_speech_agent.py similarity index 98% rename from src/control_backend/agents/ri_command_agent.py rename to src/control_backend/agents/act_agents/act_speech_agent.py index ac561ed..726a07d 100644 --- a/src/control_backend/agents/ri_command_agent.py +++ b/src/control_backend/agents/act_agents/act_speech_agent.py @@ -10,7 +10,7 @@ from control_backend.core.config import settings from control_backend.schemas.ri_message import SpeechCommand -class RICommandAgent(BaseAgent): +class ActSpeechAgent(BaseAgent): subsocket: zmq.Socket pubsocket: zmq.Socket address = "" diff --git a/src/control_backend/agents/bdi/__init__.py b/src/control_backend/agents/bdi/__init__.py deleted file mode 100644 index ec48472..0000000 --- a/src/control_backend/agents/bdi/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -from .bdi_core import BDICoreAgent as BDICoreAgent -from .text_extractor import TBeliefExtractorAgent as TBeliefExtractorAgent diff --git a/src/control_backend/agents/bdi_agents/__init__.py b/src/control_backend/agents/bdi_agents/__init__.py new file mode 100644 index 0000000..76f4876 --- /dev/null +++ b/src/control_backend/agents/bdi_agents/__init__.py @@ -0,0 +1 @@ +from .bdi_core_agent import BDICoreAgent as BDICoreAgent diff --git a/src/control_backend/agents/bdi/bdi_core.py b/src/control_backend/agents/bdi_agents/bdi_core_agent.py similarity index 100% rename from src/control_backend/agents/bdi/bdi_core.py rename to src/control_backend/agents/bdi_agents/bdi_core_agent.py diff --git a/src/control_backend/agents/bdi/behaviours/belief_setter.py b/src/control_backend/agents/bdi_agents/behaviours/belief_setter.py similarity index 97% rename from src/control_backend/agents/bdi/behaviours/belief_setter.py rename to src/control_backend/agents/bdi_agents/behaviours/belief_setter.py index 195fb76..9ffb2f3 100644 --- a/src/control_backend/agents/bdi/behaviours/belief_setter.py +++ b/src/control_backend/agents/bdi_agents/behaviours/belief_setter.py @@ -32,7 +32,7 @@ class BeliefSetterBehaviour(CyclicBehaviour): self.agent.logger.debug("Processing message from sender: %s", sender) match sender: - case settings.agent_settings.belief_collector_agent_name: + case settings.agent_settings.bel_collector_agent_name: self.agent.logger.debug( "Message is from the belief collector agent. Processing as belief message." ) diff --git a/src/control_backend/agents/bdi/behaviours/receive_llm_resp_behaviour.py b/src/control_backend/agents/bdi_agents/behaviours/receive_llm_resp_behaviour.py similarity index 94% rename from src/control_backend/agents/bdi/behaviours/receive_llm_resp_behaviour.py rename to src/control_backend/agents/bdi_agents/behaviours/receive_llm_resp_behaviour.py index a891eca..2a06fb9 100644 --- a/src/control_backend/agents/bdi/behaviours/receive_llm_resp_behaviour.py +++ b/src/control_backend/agents/bdi_agents/behaviours/receive_llm_resp_behaviour.py @@ -22,7 +22,7 @@ class ReceiveLLMResponseBehaviour(CyclicBehaviour): speech_command = SpeechCommand(data=content) message = Message( - to=settings.agent_settings.ri_command_agent_name + to=settings.agent_settings.act_speech_agent_name + "@" + settings.agent_settings.host, sender=self.agent.jid, diff --git a/src/control_backend/agents/bdi/rules.asl b/src/control_backend/agents/bdi_agents/rules.asl similarity index 100% rename from src/control_backend/agents/bdi/rules.asl rename to src/control_backend/agents/bdi_agents/rules.asl diff --git a/src/control_backend/agents/bel_agents/bel_collector_agent/__init__.py b/src/control_backend/agents/bel_agents/bel_collector_agent/__init__.py new file mode 100644 index 0000000..3b5a313 --- /dev/null +++ b/src/control_backend/agents/bel_agents/bel_collector_agent/__init__.py @@ -0,0 +1 @@ +from .bel_collector_agent import BelCollectorAgent as BelCollectorAgent diff --git a/src/control_backend/agents/belief_collector/behaviours/continuous_collect.py b/src/control_backend/agents/bel_agents/bel_collector_agent/behaviours/continuous_collect.py similarity index 95% rename from src/control_backend/agents/belief_collector/behaviours/continuous_collect.py rename to src/control_backend/agents/bel_agents/bel_collector_agent/behaviours/continuous_collect.py index 4dc62e8..512be47 100644 --- a/src/control_backend/agents/belief_collector/behaviours/continuous_collect.py +++ b/src/control_backend/agents/bel_agents/bel_collector_agent/behaviours/continuous_collect.py @@ -35,7 +35,7 @@ class ContinuousBeliefCollector(CyclicBehaviour): msg_type = payload.get("type") # Prefer explicit 'type' field - if msg_type == "belief_extraction_text" or sender_node == "belief_text_agent_mock": + if msg_type == "belief_extraction_text" or sender_node == "bel_text_agent_mock": self.agent.logger.debug( "Message routed to _handle_belief_text (sender=%s)", sender_node ) @@ -83,7 +83,9 @@ class ContinuousBeliefCollector(CyclicBehaviour): if not beliefs: return - to_jid = f"{settings.agent_settings.bdi_core_agent_name}@{settings.agent_settings.host}" + to_jid = ( + f"{settings.agent_settings.bdi_core_agent_agent_name}@{settings.agent_settings.host}" + ) msg = Message(to=to_jid, sender=self.agent.jid, thread="beliefs") msg.body = json.dumps(beliefs) diff --git a/src/control_backend/agents/belief_collector/belief_collector.py b/src/control_backend/agents/bel_agents/bel_collector_agent/bel_collector_agent.py similarity index 63% rename from src/control_backend/agents/belief_collector/belief_collector.py rename to src/control_backend/agents/bel_agents/bel_collector_agent/bel_collector_agent.py index 17aacb8..5b633d5 100644 --- a/src/control_backend/agents/belief_collector/belief_collector.py +++ b/src/control_backend/agents/bel_agents/bel_collector_agent/bel_collector_agent.py @@ -3,9 +3,9 @@ from control_backend.agents.base import BaseAgent from .behaviours.continuous_collect import ContinuousBeliefCollector -class BeliefCollectorAgent(BaseAgent): +class BelCollectorAgent(BaseAgent): async def setup(self): - self.logger.info("BeliefCollectorAgent starting (%s)", self.jid) + self.logger.info("BelCollectorAgent starting (%s)", self.jid) # Attach the continuous collector behaviour (listens and forwards to BDI) self.add_behaviour(ContinuousBeliefCollector()) - self.logger.info("BeliefCollectorAgent ready.") + self.logger.info("BelCollectorAgent ready.") diff --git a/src/control_backend/agents/bdi/behaviours/text_belief_extractor.py b/src/control_backend/agents/bel_agents/bel_text_extract_agent/behaviours/text_belief_extractor.py similarity index 94% rename from src/control_backend/agents/bdi/behaviours/text_belief_extractor.py rename to src/control_backend/agents/bel_agents/bel_text_extract_agent/behaviours/text_belief_extractor.py index 8a8273e..3606bd0 100644 --- a/src/control_backend/agents/bdi/behaviours/text_belief_extractor.py +++ b/src/control_backend/agents/bel_agents/bel_text_extract_agent/behaviours/text_belief_extractor.py @@ -44,7 +44,7 @@ class BeliefFromText(CyclicBehaviour): sender = msg.sender.node match sender: - case settings.agent_settings.transcription_agent_name: + case settings.agent_settings.per_transcription_agent_name: self.logger.debug("Received text from transcriber: %s", msg.body) await self._process_transcription_demo(msg.body) case _: @@ -71,7 +71,7 @@ class BeliefFromText(CyclicBehaviour): belief_message = Message() belief_message.to = ( - settings.agent_settings.belief_collector_agent_name + settings.agent_settings.bel_collector_agent_name + "@" + settings.agent_settings.host ) @@ -95,7 +95,7 @@ class BeliefFromText(CyclicBehaviour): belief_msg = Message() belief_msg.to = ( - settings.agent_settings.belief_collector_agent_name + "@" + settings.agent_settings.host + settings.agent_settings.bel_collector_agent_name + "@" + settings.agent_settings.host ) belief_msg.body = payload belief_msg.thread = "beliefs" diff --git a/src/control_backend/agents/bdi/text_extractor.py b/src/control_backend/agents/bel_agents/bel_text_extract_agent/bel_text_extract_agent.py similarity index 82% rename from src/control_backend/agents/bdi/text_extractor.py rename to src/control_backend/agents/bel_agents/bel_text_extract_agent/bel_text_extract_agent.py index 9f77d36..8716b4c 100644 --- a/src/control_backend/agents/bdi/text_extractor.py +++ b/src/control_backend/agents/bel_agents/bel_text_extract_agent/bel_text_extract_agent.py @@ -3,6 +3,6 @@ from control_backend.agents.base import BaseAgent from .behaviours.text_belief_extractor import BeliefFromText -class TBeliefExtractorAgent(BaseAgent): +class BelTextExtractAgent(BaseAgent): async def setup(self): self.add_behaviour(BeliefFromText()) diff --git a/src/control_backend/agents/com_agents/__init__.py b/src/control_backend/agents/com_agents/__init__.py new file mode 100644 index 0000000..c91ad14 --- /dev/null +++ b/src/control_backend/agents/com_agents/__init__.py @@ -0,0 +1 @@ +from .com_ri_agent import ComRIAgent as ComRIAgent diff --git a/src/control_backend/agents/ri_communication_agent.py b/src/control_backend/agents/com_agents/com_ri_agent.py similarity index 92% rename from src/control_backend/agents/ri_communication_agent.py rename to src/control_backend/agents/com_agents/com_ri_agent.py index 76d6431..d4513c1 100644 --- a/src/control_backend/agents/ri_communication_agent.py +++ b/src/control_backend/agents/com_agents/com_ri_agent.py @@ -7,10 +7,10 @@ from zmq.asyncio import Context from control_backend.agents import BaseAgent from control_backend.core.config import settings -from .ri_command_agent import RICommandAgent +from ..act_agents.act_speech_agent import ActSpeechAgent -class RICommunicationAgent(BaseAgent): +class ComRIAgent(BaseAgent): req_socket: zmq.Socket _address = "" _bind = True @@ -132,11 +132,11 @@ class RICommunicationAgent(BaseAgent): else: self.req_socket.bind(addr) case "actuation": - ri_commands_agent = RICommandAgent( - settings.agent_settings.ri_command_agent_name + ri_commands_agent = ActSpeechAgent( + settings.agent_settings.act_speech_agent_name + "@" + settings.agent_settings.host, - settings.agent_settings.ri_command_agent_name, + settings.agent_settings.act_speech_agent_name, address=addr, bind=bind, ) @@ -153,7 +153,7 @@ class RICommunicationAgent(BaseAgent): break else: - self.logger.error("Failed to set up RICommunicationAgent after %d retries", max_retries) + self.logger.error("Failed to set up ComRIAgent after %d retries", max_retries) return # Set up ping behaviour diff --git a/src/control_backend/agents/llm_agents/__init__.py b/src/control_backend/agents/llm_agents/__init__.py new file mode 100644 index 0000000..e12ff29 --- /dev/null +++ b/src/control_backend/agents/llm_agents/__init__.py @@ -0,0 +1 @@ +from .llm_agent import LLMAgent as LLMAgent diff --git a/src/control_backend/agents/llm/llm.py b/src/control_backend/agents/llm_agents/llm_agent.py similarity index 97% rename from src/control_backend/agents/llm/llm.py rename to src/control_backend/agents/llm_agents/llm_agent.py index 4aec46b..ce1b791 100644 --- a/src/control_backend/agents/llm/llm.py +++ b/src/control_backend/agents/llm_agents/llm_agent.py @@ -39,7 +39,7 @@ class LLMAgent(BaseAgent): sender, ) - if sender == settings.agent_settings.bdi_core_agent_name: + if sender == settings.agent_settings.bdi_core_agent_agent_name: self.agent.logger.debug("Processing message from BDI Core Agent") await self._process_bdi_message(msg) else: @@ -63,7 +63,9 @@ class LLMAgent(BaseAgent): Sends a response message back to the BDI Core Agent. """ reply = Message( - to=settings.agent_settings.bdi_core_agent_name + "@" + settings.agent_settings.host, + to=settings.agent_settings.bdi_core_agent_agent_name + + "@" + + settings.agent_settings.host, body=msg, ) await self.send(reply) diff --git a/src/control_backend/agents/llm/llm_instructions.py b/src/control_backend/agents/llm_agents/llm_instructions.py similarity index 100% rename from src/control_backend/agents/llm/llm_instructions.py rename to src/control_backend/agents/llm_agents/llm_instructions.py diff --git a/src/control_backend/agents/mock_agents/belief_text_mock.py b/src/control_backend/agents/mock_agents/bel_text_agent.py similarity index 89% rename from src/control_backend/agents/mock_agents/belief_text_mock.py rename to src/control_backend/agents/mock_agents/bel_text_agent.py index 27c5e49..2808cae 100644 --- a/src/control_backend/agents/mock_agents/belief_text_mock.py +++ b/src/control_backend/agents/mock_agents/bel_text_agent.py @@ -7,11 +7,11 @@ from spade.message import Message from control_backend.core.config import settings -class BeliefTextAgent(Agent): +class BelTextAgent(Agent): class SendOnceBehaviourBlfText(OneShotBehaviour): async def run(self): to_jid = ( - settings.agent_settings.belief_collector_agent_name + settings.agent_settings.bel_collector_agent_name + "@" + settings.agent_settings.host ) @@ -39,6 +39,6 @@ class BeliefTextAgent(Agent): await self.agent.stop() async def setup(self): - print("BeliefTextAgent started") + print("BelTextAgent started") self.b = self.SendOnceBehaviourBlfText() self.add_behaviour(self.b) diff --git a/src/control_backend/agents/per_agents/__init__.py b/src/control_backend/agents/per_agents/__init__.py new file mode 100644 index 0000000..e3d9cf0 --- /dev/null +++ b/src/control_backend/agents/per_agents/__init__.py @@ -0,0 +1,4 @@ +from .per_transcription_agent.per_transcription_agent import ( + PerTranscriptionAgent as PerTranscriptionAgent, +) +from .per_vad_agent import PerVADAgent as PerVADAgent diff --git a/src/control_backend/agents/transcription/transcription_agent.py b/src/control_backend/agents/per_agents/per_transcription_agent/per_transcription_agent.py similarity index 89% rename from src/control_backend/agents/transcription/transcription_agent.py rename to src/control_backend/agents/per_agents/per_transcription_agent/per_transcription_agent.py index 64edf79..50571b8 100644 --- a/src/control_backend/agents/transcription/transcription_agent.py +++ b/src/control_backend/agents/per_agents/per_transcription_agent/per_transcription_agent.py @@ -12,15 +12,19 @@ from control_backend.core.config import settings from .speech_recognizer import SpeechRecognizer -class TranscriptionAgent(BaseAgent): +class PerTranscriptionAgent(BaseAgent): """ An agent which listens to audio fragments with voice, transcribes them, and sends the transcription to other agents. """ def __init__(self, audio_in_address: str): - jid = settings.agent_settings.transcription_agent_name + "@" + settings.agent_settings.host - super().__init__(jid, settings.agent_settings.transcription_agent_name) + jid = ( + settings.agent_settings.per_transcription_agent_name + + "@" + + settings.agent_settings.host + ) + super().__init__(jid, settings.agent_settings.per_transcription_agent_name) self.audio_in_address = audio_in_address self.audio_in_socket: azmq.Socket | None = None @@ -43,7 +47,7 @@ class TranscriptionAgent(BaseAgent): async def _share_transcription(self, transcription: str): """Share a transcription to the other agents that depend on it.""" receiver_jids = [ - settings.agent_settings.text_belief_extractor_agent_name + settings.agent_settings.texbel_text_extractor_agent_name + "@" + settings.agent_settings.host, ] # Set message receivers here diff --git a/src/control_backend/agents/transcription/speech_recognizer.py b/src/control_backend/agents/per_agents/per_transcription_agent/speech_recognizer.py similarity index 100% rename from src/control_backend/agents/transcription/speech_recognizer.py rename to src/control_backend/agents/per_agents/per_transcription_agent/speech_recognizer.py diff --git a/src/control_backend/agents/vad_agent.py b/src/control_backend/agents/per_agents/per_vad_agent.py similarity index 94% rename from src/control_backend/agents/vad_agent.py rename to src/control_backend/agents/per_agents/per_vad_agent.py index c49613b..f065e2a 100644 --- a/src/control_backend/agents/vad_agent.py +++ b/src/control_backend/agents/per_agents/per_vad_agent.py @@ -7,7 +7,7 @@ from spade.behaviour import CyclicBehaviour from control_backend.agents import BaseAgent from control_backend.core.config import settings -from .transcription.transcription_agent import TranscriptionAgent +from .per_transcription_agent.per_transcription_agent import PerTranscriptionAgent class SocketPoller[T]: @@ -102,15 +102,15 @@ class Streaming(CyclicBehaviour): self.audio_buffer = chunk -class VADAgent(BaseAgent): +class PerVADAgent(BaseAgent): """ An agent which listens to an audio stream, does Voice Activity Detection (VAD), and sends fragments with detected speech to other agents over ZeroMQ. """ def __init__(self, audio_in_address: str, audio_in_bind: bool): - jid = settings.agent_settings.vad_agent_name + "@" + settings.agent_settings.host - super().__init__(jid, settings.agent_settings.vad_agent_name) + jid = settings.agent_settings.per_vad_agent_name + "@" + settings.agent_settings.host + super().__init__(jid, settings.agent_settings.per_vad_agent_name) self.audio_in_address = audio_in_address self.audio_in_bind = audio_in_bind @@ -166,7 +166,7 @@ class VADAgent(BaseAgent): self.add_behaviour(self.streaming_behaviour) # Start agents dependent on the output audio fragments here - transcriber = TranscriptionAgent(audio_out_address) + transcriber = PerTranscriptionAgent(audio_out_address) await transcriber.start() self.logger.info("Finished setting up %s", self.jid) diff --git a/src/control_backend/core/config.py b/src/control_backend/core/config.py index 8de2403..55fd583 100644 --- a/src/control_backend/core/config.py +++ b/src/control_backend/core/config.py @@ -9,16 +9,16 @@ class ZMQSettings(BaseModel): class AgentSettings(BaseModel): host: str = "localhost" - bdi_core_agent_name: str = "bdi_core" - belief_collector_agent_name: str = "belief_collector" - text_belief_extractor_agent_name: str = "text_belief_extractor" - vad_agent_name: str = "vad_agent" + bdi_core_agent_agent_name: str = "bdi_core_agent" + bel_collector_agent_name: str = "bel_collector_agent" + texbel_text_extractor_agent_name: str = "text_belief_extractor" + per_vad_agent_name: str = "per_vad_agent" llm_agent_name: str = "llm_agent" test_agent_name: str = "test_agent" - transcription_agent_name: str = "transcription_agent" + per_transcription_agent_name: str = "per_transcription_agent" - ri_communication_agent_name: str = "ri_communication_agent" - ri_command_agent_name: str = "ri_command_agent" + com_ri_agent_name: str = "com_ri_agent" + act_speech_agent_name: str = "act_speech_agent" class LLMSettings(BaseModel): diff --git a/src/control_backend/main.py b/src/control_backend/main.py index 4bb8ded..25e2a7c 100644 --- a/src/control_backend/main.py +++ b/src/control_backend/main.py @@ -8,12 +8,12 @@ from fastapi.middleware.cors import CORSMiddleware from zmq.asyncio import Context from control_backend.agents import ( - BeliefCollectorAgent, + BelCollectorAgent, + ComRIAgent, LLMAgent, - RICommunicationAgent, - VADAgent, + PerVADAgent, ) -from control_backend.agents.bdi import BDICoreAgent, TBeliefExtractorAgent +from control_backend.agents.bdi_agents import BDICoreAgent, BelTextExtractAgent from control_backend.api.v1.router import api_router from control_backend.core.config import settings from control_backend.logging import setup_logging @@ -64,13 +64,13 @@ async def lifespan(app: FastAPI): # --- Initialize Agents --- logger.info("Initializing and starting agents.") agents_to_start = { - "RICommunicationAgent": ( - RICommunicationAgent, + "ComRIAgent": ( + ComRIAgent, { - "name": settings.agent_settings.ri_communication_agent_name, - "jid": f"{settings.agent_settings.ri_communication_agent_name}" + "name": settings.agent_settings.com_ri_agent_name, + "jid": f"{settings.agent_settings.com_ri_agent_name}" f"@{settings.agent_settings.host}", - "password": settings.agent_settings.ri_communication_agent_name, + "password": settings.agent_settings.com_ri_agent_name, "address": "tcp://*:5555", "bind": True, }, @@ -86,33 +86,33 @@ async def lifespan(app: FastAPI): "BDICoreAgent": ( BDICoreAgent, { - "name": settings.agent_settings.bdi_core_agent_name, - "jid": f"{settings.agent_settings.bdi_core_agent_name}@" + "name": settings.agent_settings.bdi_core_agent_agent_name, + "jid": f"{settings.agent_settings.bdi_core_agent_agent_name}@" f"{settings.agent_settings.host}", - "password": settings.agent_settings.bdi_core_agent_name, + "password": settings.agent_settings.bdi_core_agent_agent_name, "asl": "src/control_backend/agents/bdi/rules.asl", }, ), - "BeliefCollectorAgent": ( - BeliefCollectorAgent, + "BelCollectorAgent": ( + BelCollectorAgent, { - "name": settings.agent_settings.belief_collector_agent_name, - "jid": f"{settings.agent_settings.belief_collector_agent_name}@" + "name": settings.agent_settings.bel_collector_agent_name, + "jid": f"{settings.agent_settings.bel_collector_agent_name}@" f"{settings.agent_settings.host}", - "password": settings.agent_settings.belief_collector_agent_name, + "password": settings.agent_settings.bel_collector_agent_name, }, ), "TBeliefExtractor": ( - TBeliefExtractorAgent, + BelTextExtractAgent, { - "name": settings.agent_settings.text_belief_extractor_agent_name, - "jid": f"{settings.agent_settings.text_belief_extractor_agent_name}@" + "name": settings.agent_settings.texbel_text_extractor_agent_name, + "jid": f"{settings.agent_settings.texbel_text_extractor_agent_name}@" f"{settings.agent_settings.host}", - "password": settings.agent_settings.text_belief_extractor_agent_name, + "password": settings.agent_settings.texbel_text_extractor_agent_name, }, ), - "VADAgent": ( - VADAgent, + "PerVADAgent": ( + PerVADAgent, {"audio_in_address": "tcp://localhost:5558", "audio_in_bind": False}, ), } diff --git a/test/integration/agents/vad_agent/speech_with_pauses_16k_1c_float32.wav b/test/integration/agents/per_vad_agent/speech_with_pauses_16k_1c_float32.wav similarity index 100% rename from test/integration/agents/vad_agent/speech_with_pauses_16k_1c_float32.wav rename to test/integration/agents/per_vad_agent/speech_with_pauses_16k_1c_float32.wav diff --git a/test/integration/agents/vad_agent/test_vad_agent.py b/test/integration/agents/per_vad_agent/test_per_vad_agent.py similarity index 54% rename from test/integration/agents/vad_agent/test_vad_agent.py rename to test/integration/agents/per_vad_agent/test_per_vad_agent.py index 0e1fae2..65c46ea 100644 --- a/test/integration/agents/vad_agent/test_vad_agent.py +++ b/test/integration/agents/per_vad_agent/test_per_vad_agent.py @@ -5,43 +5,47 @@ import pytest import zmq from spade.agent import Agent -from control_backend.agents.vad_agent import VADAgent +from control_backend.agents.per_agents.per_vad_agent import PerVADAgent @pytest.fixture def zmq_context(mocker): - mock_context = mocker.patch("control_backend.agents.vad_agent.azmq.Context.instance") + mock_context = mocker.patch( + "control_backend.agents.per_agents.per_vad_agent.azmq.Context.instance" + ) mock_context.return_value = MagicMock() return mock_context @pytest.fixture def streaming(mocker): - return mocker.patch("control_backend.agents.vad_agent.Streaming") + return mocker.patch("control_backend.agents.per_agents.per_vad_agent.Streaming") @pytest.fixture -def transcription_agent(mocker): - return mocker.patch("control_backend.agents.vad_agent.TranscriptionAgent", autospec=True) +def per_transcription_agent(mocker): + return mocker.patch( + "control_backend.agents.per_agents.per_vad_agent.PerTranscriptionAgent", autospec=True + ) @pytest.mark.asyncio -async def test_normal_setup(streaming, transcription_agent): +async def test_normal_setup(streaming, per_transcription_agent): """ Test that during normal setup, the VAD agent creates a Streaming behavior and creates audio - sockets, and starts the TranscriptionAgent without loading real models. + sockets, and starts the PerTranscriptionAgent without loading real models. """ - vad_agent = VADAgent("tcp://localhost:12345", False) - vad_agent.add_behaviour = MagicMock() + per_vad_agent = PerVADAgent("tcp://localhost:12345", False) + per_vad_agent.add_behaviour = MagicMock() - await vad_agent.setup() + await per_vad_agent.setup() streaming.assert_called_once() - vad_agent.add_behaviour.assert_called_once_with(streaming.return_value) - transcription_agent.assert_called_once() - transcription_agent.return_value.start.assert_called_once() - assert vad_agent.audio_in_socket is not None - assert vad_agent.audio_out_socket is not None + per_vad_agent.add_behaviour.assert_called_once_with(streaming.return_value) + per_transcription_agent.assert_called_once() + per_transcription_agent.return_value.start.assert_called_once() + assert per_vad_agent.audio_in_socket is not None + assert per_vad_agent.audio_out_socket is not None @pytest.mark.parametrize("do_bind", [True, False]) @@ -50,11 +54,11 @@ def test_in_socket_creation(zmq_context, do_bind: bool): Test that the VAD agent creates an audio input socket, differentiating between binding and connecting. """ - vad_agent = VADAgent(f"tcp://{'*' if do_bind else 'localhost'}:12345", do_bind) + per_vad_agent = PerVADAgent(f"tcp://{'*' if do_bind else 'localhost'}:12345", do_bind) - vad_agent._connect_audio_in_socket() + per_vad_agent._connect_audio_in_socket() - assert vad_agent.audio_in_socket is not None + assert per_vad_agent.audio_in_socket is not None zmq_context.return_value.socket.assert_called_once_with(zmq.SUB) zmq_context.return_value.socket.return_value.setsockopt_string.assert_called_once_with( @@ -74,11 +78,11 @@ def test_out_socket_creation(zmq_context): """ Test that the VAD agent creates an audio output socket correctly. """ - vad_agent = VADAgent("tcp://localhost:12345", False) + per_vad_agent = PerVADAgent("tcp://localhost:12345", False) - vad_agent._connect_audio_out_socket() + per_vad_agent._connect_audio_out_socket() - assert vad_agent.audio_out_socket is not None + assert per_vad_agent.audio_out_socket is not None zmq_context.return_value.socket.assert_called_once_with(zmq.PUB) zmq_context.return_value.socket.return_value.bind_to_random_port.assert_called_once() @@ -93,28 +97,28 @@ async def test_out_socket_creation_failure(zmq_context): zmq_context.return_value.socket.return_value.bind_to_random_port.side_effect = ( zmq.ZMQBindError ) - vad_agent = VADAgent("tcp://localhost:12345", False) + per_vad_agent = PerVADAgent("tcp://localhost:12345", False) - await vad_agent.setup() + await per_vad_agent.setup() - assert vad_agent.audio_out_socket is None + assert per_vad_agent.audio_out_socket is None mock_super_stop.assert_called_once() @pytest.mark.asyncio -async def test_stop(zmq_context, transcription_agent): +async def test_stop(zmq_context, per_transcription_agent): """ Test that when the VAD agent is stopped, the sockets are closed correctly. """ - vad_agent = VADAgent("tcp://localhost:12345", False) + per_vad_agent = PerVADAgent("tcp://localhost:12345", False) zmq_context.return_value.socket.return_value.bind_to_random_port.return_value = random.randint( 1000, 10000, ) - await vad_agent.setup() - await vad_agent.stop() + await per_vad_agent.setup() + await per_vad_agent.stop() assert zmq_context.return_value.socket.return_value.close.call_count == 2 - assert vad_agent.audio_in_socket is None - assert vad_agent.audio_out_socket is None + assert per_vad_agent.audio_in_socket is None + assert per_vad_agent.audio_out_socket is None diff --git a/test/integration/agents/vad_agent/test_vad_with_audio.py b/test/integration/agents/per_vad_agent/test_per_vad_with_audio.py similarity index 89% rename from test/integration/agents/vad_agent/test_vad_with_audio.py rename to test/integration/agents/per_vad_agent/test_per_vad_with_audio.py index bae15af..0198911 100644 --- a/test/integration/agents/vad_agent/test_vad_with_audio.py +++ b/test/integration/agents/per_vad_agent/test_per_vad_with_audio.py @@ -5,7 +5,7 @@ import pytest import soundfile as sf import zmq -from control_backend.agents.vad_agent import Streaming +from control_backend.agents.per_agents.per_vad_agent import Streaming def get_audio_chunks() -> list[bytes]: @@ -42,7 +42,9 @@ async def test_real_audio(mocker): audio_in_socket = AsyncMock() audio_in_socket.recv.side_effect = audio_chunks - mock_poller: MagicMock = mocker.patch("control_backend.agents.vad_agent.zmq.Poller") + mock_poller: MagicMock = mocker.patch( + "control_backend.agents.per_agents.per_vad_agent.zmq.Poller" + ) mock_poller.return_value.poll.return_value = [(audio_in_socket, zmq.POLLIN)] audio_out_socket = AsyncMock() diff --git a/test/integration/agents/test_ri_commands_agent.py b/test/integration/agents/test_act_speech_agent.py similarity index 77% rename from test/integration/agents/test_ri_commands_agent.py rename to test/integration/agents/test_act_speech_agent.py index 00edcb1..909c51c 100644 --- a/test/integration/agents/test_ri_commands_agent.py +++ b/test/integration/agents/test_act_speech_agent.py @@ -4,12 +4,14 @@ from unittest.mock import AsyncMock, MagicMock, patch import pytest import zmq -from control_backend.agents.ri_command_agent import RICommandAgent +from control_backend.agents.act_agents.act_speech_agent import ActSpeechAgent @pytest.fixture def zmq_context(mocker): - mock_context = mocker.patch("control_backend.agents.vad_agent.azmq.Context.instance") + mock_context = mocker.patch( + "control_backend.agents.act_agents.act_speech_agent.zmq.Context.instance" + ) mock_context.return_value = MagicMock() return mock_context @@ -19,8 +21,8 @@ async def test_setup_bind(zmq_context, mocker): """Test setup with bind=True""" fake_socket = zmq_context.return_value.socket.return_value - agent = RICommandAgent("test@server", "password", address="tcp://localhost:5555", bind=True) - settings = mocker.patch("control_backend.agents.ri_command_agent.settings") + agent = ActSpeechAgent("test@server", "password", address="tcp://localhost:5555", bind=True) + settings = mocker.patch("control_backend.agents.act_agents.act_speech_agent.settings") settings.zmq_settings.internal_sub_address = "tcp://internal:1234" await agent.setup() @@ -40,8 +42,8 @@ async def test_setup_connect(zmq_context, mocker): """Test setup with bind=False""" fake_socket = zmq_context.return_value.socket.return_value - agent = RICommandAgent("test@server", "password", address="tcp://localhost:5555", bind=False) - settings = mocker.patch("control_backend.agents.ri_command_agent.settings") + agent = ActSpeechAgent("test@server", "password", address="tcp://localhost:5555", bind=False) + settings = mocker.patch("control_backend.agents.act_agents.act_speech_agent.settings") settings.zmq_settings.internal_sub_address = "tcp://internal:1234" await agent.setup() @@ -60,14 +62,16 @@ async def test_send_commands_behaviour_valid_message(): ) fake_socket.send_json = AsyncMock() - agent = RICommandAgent("test@server", "password") + agent = ActSpeechAgent("test@server", "password") agent.subsocket = fake_socket agent.pubsocket = fake_socket behaviour = agent.SendCommandsBehaviour() behaviour.agent = agent - with patch("control_backend.agents.ri_command_agent.SpeechCommand") as MockSpeechCommand: + with patch( + "control_backend.agents.act_agents.act_speech_agent.SpeechCommand" + ) as MockSpeechCommand: mock_message = MagicMock() MockSpeechCommand.model_validate.return_value = mock_message @@ -84,16 +88,14 @@ async def test_send_commands_behaviour_invalid_message(caplog): fake_socket.recv_multipart = AsyncMock(return_value=(b"command", b"{invalid_json}")) fake_socket.send_json = AsyncMock() - agent = RICommandAgent("test@server", "password") + agent = ActSpeechAgent("test@server", "password") agent.subsocket = fake_socket agent.pubsocket = fake_socket behaviour = agent.SendCommandsBehaviour() behaviour.agent = agent - with caplog.at_level("ERROR"): - await behaviour.run() + await behaviour.run() fake_socket.recv_multipart.assert_awaited() fake_socket.send_json.assert_not_awaited() - assert "Error processing message" in caplog.text diff --git a/test/integration/agents/test_ri_communication_agent.py b/test/integration/agents/test_com_ri_agent.py similarity index 84% rename from test/integration/agents/test_ri_communication_agent.py rename to test/integration/agents/test_com_ri_agent.py index 443d609..870645c 100644 --- a/test/integration/agents/test_ri_communication_agent.py +++ b/test/integration/agents/test_com_ri_agent.py @@ -3,7 +3,11 @@ from unittest.mock import ANY, AsyncMock, MagicMock, patch import pytest -from control_backend.agents.ri_communication_agent import RICommunicationAgent +from control_backend.agents.com_agents.com_ri_agent import ComRIAgent + + +def act_agent_path(): + return "control_backend.agents.com_agents.com_ri_agent.ActSpeechAgent" def fake_json_correct_negototiate_1(): @@ -86,7 +90,9 @@ def fake_json_invalid_id_negototiate(): @pytest.fixture def zmq_context(mocker): - mock_context = mocker.patch("control_backend.agents.vad_agent.azmq.Context.instance") + mock_context = mocker.patch( + "control_backend.agents.com_agents.com_ri_agent.zmq.Context.instance" + ) mock_context.return_value = MagicMock() return mock_context @@ -101,17 +107,13 @@ async def test_setup_creates_socket_and_negotiate_1(zmq_context): fake_socket.send_json = AsyncMock() fake_socket.recv_json = fake_json_correct_negototiate_1() - # Mock RICommandAgent agent startup - with patch( - "control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True - ) as MockCommandAgent: + # Mock ActSpeechAgent agent startup + with patch(act_agent_path(), autospec=True) as MockCommandAgent: fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() # --- Act --- - agent = RICommunicationAgent( - "test@server", "password", address="tcp://localhost:5555", bind=False - ) + agent = ComRIAgent("test@server", "password", address="tcp://localhost:5555", bind=False) await agent.setup() # --- Assert --- @@ -139,17 +141,13 @@ async def test_setup_creates_socket_and_negotiate_2(zmq_context): fake_socket.send_json = AsyncMock() fake_socket.recv_json = fake_json_correct_negototiate_2() - # Mock RICommandAgent agent startup - with patch( - "control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True - ) as MockCommandAgent: + # Mock ActSpeechAgent agent startup + with patch(act_agent_path(), autospec=True) as MockCommandAgent: fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() # --- Act --- - agent = RICommunicationAgent( - "test@server", "password", address="tcp://localhost:5555", bind=False - ) + agent = ComRIAgent("test@server", "password", address="tcp://localhost:5555", bind=False) await agent.setup() # --- Assert --- @@ -177,19 +175,17 @@ async def test_setup_creates_socket_and_negotiate_3(zmq_context, caplog): fake_socket.send_json = AsyncMock() fake_socket.recv_json = fake_json_wrong_negototiate_1() - # Mock RICommandAgent agent startup + # Mock ActSpeechAgent agent startup # We are sending wrong negotiation info to the communication agent, # so we should retry and expect a better response, within a limited time. - with patch( - "control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True - ) as MockCommandAgent: + with patch(act_agent_path(), autospec=True) as MockCommandAgent: fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() # --- Act --- with caplog.at_level("ERROR"): - agent = RICommunicationAgent( + agent = ComRIAgent( "test@server", "password", address="tcp://localhost:5555", bind=False ) await agent.setup(max_retries=1) @@ -200,7 +196,7 @@ async def test_setup_creates_socket_and_negotiate_3(zmq_context, caplog): # Since it failed, there should not be any command agent. fake_agent_instance.start.assert_not_awaited() - assert "Failed to set up RICommunicationAgent" in caplog.text + assert "Failed to set up ComRIAgent" in caplog.text # Ensure the agent did not attach a ListenBehaviour assert not any(isinstance(b, agent.ListenBehaviour) for b in agent.behaviours) @@ -216,17 +212,13 @@ async def test_setup_creates_socket_and_negotiate_4(zmq_context): fake_socket.send_json = AsyncMock() fake_socket.recv_json = fake_json_correct_negototiate_3() - # Mock RICommandAgent agent startup - with patch( - "control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True - ) as MockCommandAgent: + # Mock ActSpeechAgent agent startup + with patch(act_agent_path(), autospec=True) as MockCommandAgent: fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() # --- Act --- - agent = RICommunicationAgent( - "test@server", "password", address="tcp://localhost:5555", bind=True - ) + agent = ComRIAgent("test@server", "password", address="tcp://localhost:5555", bind=True) await agent.setup() # --- Assert --- @@ -254,17 +246,13 @@ async def test_setup_creates_socket_and_negotiate_5(zmq_context): fake_socket.send_json = AsyncMock() fake_socket.recv_json = fake_json_correct_negototiate_4() - # Mock RICommandAgent agent startup - with patch( - "control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True - ) as MockCommandAgent: + # Mock ActSpeechAgent agent startup + with patch(act_agent_path(), autospec=True) as MockCommandAgent: fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() # --- Act --- - agent = RICommunicationAgent( - "test@server", "password", address="tcp://localhost:5555", bind=False - ) + agent = ComRIAgent("test@server", "password", address="tcp://localhost:5555", bind=False) await agent.setup() # --- Assert --- @@ -292,17 +280,13 @@ async def test_setup_creates_socket_and_negotiate_6(zmq_context): fake_socket.send_json = AsyncMock() fake_socket.recv_json = fake_json_correct_negototiate_5() - # Mock RICommandAgent agent startup - with patch( - "control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True - ) as MockCommandAgent: + # Mock ActSpeechAgent agent startup + with patch(act_agent_path(), autospec=True) as MockCommandAgent: fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() # --- Act --- - agent = RICommunicationAgent( - "test@server", "password", address="tcp://localhost:5555", bind=False - ) + agent = ComRIAgent("test@server", "password", address="tcp://localhost:5555", bind=False) await agent.setup() # --- Assert --- @@ -330,19 +314,17 @@ async def test_setup_creates_socket_and_negotiate_7(zmq_context, caplog): fake_socket.send_json = AsyncMock() fake_socket.recv_json = fake_json_invalid_id_negototiate() - # Mock RICommandAgent agent startup + # Mock ActSpeechAgent agent startup # We are sending wrong negotiation info to the communication agent, # so we should retry and expect a better response, within a limited time. - with patch( - "control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True - ) as MockCommandAgent: + with patch(act_agent_path(), autospec=True) as MockCommandAgent: fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() # --- Act --- with caplog.at_level("WARNING"): - agent = RICommunicationAgent( + agent = ComRIAgent( "test@server", "password", address="tcp://localhost:5555", bind=False ) await agent.setup(max_retries=1) @@ -366,15 +348,13 @@ async def test_setup_creates_socket_and_negotiate_timeout(zmq_context, caplog): fake_socket.send_json = AsyncMock() fake_socket.recv_json = AsyncMock(side_effect=asyncio.TimeoutError) - with patch( - "control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True - ) as MockCommandAgent: + with patch(act_agent_path(), autospec=True) as MockCommandAgent: fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() # --- Act --- with caplog.at_level("WARNING"): - agent = RICommunicationAgent( + agent = ComRIAgent( "test@server", "password", address="tcp://localhost:5555", bind=False ) await agent.setup(max_retries=1) @@ -397,7 +377,7 @@ async def test_listen_behaviour_ping_correct(caplog): fake_socket.recv_json = AsyncMock(return_value={"endpoint": "ping", "data": {}}) # TODO: Integration test between actual server and password needed for spade agents - agent = RICommunicationAgent("test@server", "password") + agent = ComRIAgent("test@server", "password") agent.req_socket = fake_socket behaviour = agent.ListenBehaviour() @@ -431,7 +411,7 @@ async def test_listen_behaviour_ping_wrong_endpoint(caplog): } ) - agent = RICommunicationAgent("test@server", "password") + agent = ComRIAgent("test@server", "password") agent.req_socket = fake_socket behaviour = agent.ListenBehaviour() @@ -453,7 +433,7 @@ async def test_listen_behaviour_timeout(zmq_context, caplog): # recv_json will never resolve, simulate timeout fake_socket.recv_json = AsyncMock(side_effect=asyncio.TimeoutError) - agent = RICommunicationAgent("test@server", "password") + agent = ComRIAgent("test@server", "password") agent.req_socket = fake_socket behaviour = agent.ListenBehaviour() @@ -480,7 +460,7 @@ async def test_listen_behaviour_ping_no_endpoint(caplog): } ) - agent = RICommunicationAgent("test@server", "password") + agent = ComRIAgent("test@server", "password") agent.req_socket = fake_socket behaviour = agent.ListenBehaviour() @@ -502,9 +482,7 @@ async def test_setup_unexpected_exception(zmq_context, caplog): # Simulate unexpected exception during recv_json() fake_socket.recv_json = AsyncMock(side_effect=Exception("boom!")) - agent = RICommunicationAgent( - "test@server", "password", address="tcp://localhost:5555", bind=False - ) + agent = ComRIAgent("test@server", "password", address="tcp://localhost:5555", bind=False) with caplog.at_level("ERROR"): await agent.setup(max_retries=1) @@ -526,16 +504,12 @@ async def test_setup_unpacking_exception(zmq_context, caplog): } # missing 'port' and 'bind' fake_socket.recv_json = AsyncMock(return_value=malformed_data) - # Patch RICommandAgent so it won't actually start - with patch( - "control_backend.agents.ri_communication_agent.RICommandAgent", autospec=True - ) as MockCommandAgent: + # Patch ActSpeechAgent so it won't actually start + with patch(act_agent_path(), autospec=True) as MockCommandAgent: fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() - agent = RICommunicationAgent( - "test@server", "password", address="tcp://localhost:5555", bind=False - ) + agent = ComRIAgent("test@server", "password", address="tcp://localhost:5555", bind=False) # --- Act & Assert --- with caplog.at_level("ERROR"): diff --git a/test/unit/agents/bdi/behaviours/test_belief_setter.py b/test/unit/agents/bdi/behaviours/test_belief_setter.py index b0e76ec..5a3b18e 100644 --- a/test/unit/agents/bdi/behaviours/test_belief_setter.py +++ b/test/unit/agents/bdi/behaviours/test_belief_setter.py @@ -4,10 +4,10 @@ from unittest.mock import AsyncMock, MagicMock, call import pytest -from control_backend.agents.bdi.behaviours.belief_setter import BeliefSetterBehaviour +from control_backend.agents.bdi_agents.behaviours.belief_setter import BeliefSetterBehaviour # Define a constant for the collector agent name to use in tests -COLLECTOR_AGENT_NAME = "belief_collector" +COLLECTOR_AGENT_NAME = "bel_collector_agent" COLLECTOR_AGENT_JID = f"{COLLECTOR_AGENT_NAME}@test" @@ -25,7 +25,7 @@ def belief_setter(mock_agent, mocker): """Fixture to create an instance of BeliefSetterBehaviour with a mocked agent.""" # Patch the settings to use a predictable agent name mocker.patch( - "control_backend.agents.bdi.behaviours.belief_setter.settings.agent_settings.belief_collector_agent_name", + "control_backend.agents.bdi_agents.behaviours.belief_setter.settings.agent_settings.bel_collector_agent_name", COLLECTOR_AGENT_NAME, ) @@ -62,7 +62,7 @@ async def test_run_message_received(belief_setter, mocker): belief_setter._process_message.assert_called_once_with(msg) -def test_process_message_from_belief_collector(belief_setter, mocker): +def test_process_message_from_bel_collector_agent(belief_setter, mocker): """ Test processing a message from the correct belief collector agent. """ diff --git a/test/unit/agents/belief_collector/behaviours/test_continuous_collect.py b/test/unit/agents/belief_collector/behaviours/test_continuous_collect.py index 706a5b8..a21cc06 100644 --- a/test/unit/agents/belief_collector/behaviours/test_continuous_collect.py +++ b/test/unit/agents/belief_collector/behaviours/test_continuous_collect.py @@ -3,7 +3,7 @@ from unittest.mock import AsyncMock, MagicMock import pytest -from control_backend.agents.belief_collector.behaviours.continuous_collect import ( +from control_backend.agents.bel_agents.bel_collector_agent.behaviours.continuous_collect import ( ContinuousBeliefCollector, ) @@ -20,7 +20,7 @@ def create_mock_message(sender_node: str, body: str) -> MagicMock: def mock_agent(mocker): """Fixture to create a mock Agent.""" agent = MagicMock() - agent.jid = "belief_collector_agent@test" + agent.jid = "bel_collector_agent@test" return agent @@ -68,7 +68,7 @@ async def test_routes_to_handle_belief_text_by_type(continuous_collector, mocker @pytest.mark.asyncio async def test_routes_to_handle_belief_text_by_sender(continuous_collector, mocker): msg = create_mock_message( - "belief_text_agent_mock", json.dumps({"beliefs": {"user_said": [["hi"]]}}) + "bel_text_agent_mock", json.dumps({"beliefs": {"user_said": [["hi"]]}}) ) spy = mocker.patch.object(continuous_collector, "_handle_belief_text", new=AsyncMock()) await continuous_collector._process_message(msg) @@ -87,7 +87,7 @@ async def test_routes_to_handle_emo_text(continuous_collector, mocker): async def test_belief_text_happy_path_sends(continuous_collector, mocker): payload = {"type": "belief_extraction_text", "beliefs": {"user_said": ["hello test", "No"]}} continuous_collector.send = AsyncMock() - await continuous_collector._handle_belief_text(payload, "belief_text_agent_mock") + await continuous_collector._handle_belief_text(payload, "bel_text_agent_mock") # make sure we attempted a send continuous_collector.send.assert_awaited_once() diff --git a/test/unit/agents/belief_from_text/behaviours/test_belief_from_text.py b/test/unit/agents/belief_from_text/behaviours/test_belief_from_text.py index 7a3eacd..4efd0e3 100644 --- a/test/unit/agents/belief_from_text/behaviours/test_belief_from_text.py +++ b/test/unit/agents/belief_from_text/behaviours/test_belief_from_text.py @@ -4,7 +4,9 @@ from unittest.mock import AsyncMock, MagicMock, patch import pytest from spade.message import Message -from control_backend.agents.bdi.behaviours.text_belief_extractor import BeliefFromText +from control_backend.agents.bel_agents.bel_text_extract_agent.behaviours.text_belief_extractor import ( # noqa: E501, We can't shorten this import. + BeliefFromText, +) @pytest.fixture @@ -15,15 +17,16 @@ def mock_settings(): """ # Create a mock object that mimics the nested structure settings_mock = MagicMock() - settings_mock.agent_settings.transcription_agent_name = "transcriber" - settings_mock.agent_settings.belief_collector_agent_name = "collector" + settings_mock.agent_settings.per_transcription_agent_name = "transcriber" + settings_mock.agent_settings.bel_collector_agent_name = "collector" settings_mock.agent_settings.host = "fake.host" # Use patch to replace the settings object during the test # Adjust 'control_backend.behaviours.belief_from_text.settings' to where # your behaviour file imports it from. with patch( - "control_backend.agents.bdi.behaviours.text_belief_extractor.settings", settings_mock + "control_backend.agents.bel_agents.bel_text_extract_agent.behaviours.text_belief_extractor.settings", + settings_mock, ): yield settings_mock @@ -100,7 +103,7 @@ async def test_run_message_from_transcriber_demo(behavior, mock_settings, monkey # Arrange: Create a mock message from the transcriber transcription_text = "hello world" mock_msg = create_mock_message( - mock_settings.agent_settings.transcription_agent_name, transcription_text, None + mock_settings.agent_settings.per_transcription_agent_name, transcription_text, None ) behavior.receive.return_value = mock_msg @@ -119,7 +122,7 @@ async def test_run_message_from_transcriber_demo(behavior, mock_settings, monkey assert ( sent_msg.to - == mock_settings.agent_settings.belief_collector_agent_name + == mock_settings.agent_settings.bel_collector_agent_name + "@" + mock_settings.agent_settings.host ) @@ -159,7 +162,7 @@ async def test_process_transcription_success(behavior, mock_settings): # 2. Inspect the sent message sent_msg: Message = behavior.send.call_args[0][0] expected_to = ( - mock_settings.agent_settings.belief_collector_agent_name + mock_settings.agent_settings.bel_collector_agent_name + "@" + mock_settings.agent_settings.host ) diff --git a/test/unit/agents/test_vad_socket_poller.py b/test/unit/agents/test_vad_socket_poller.py index aaf8d0f..d0c2fc5 100644 --- a/test/unit/agents/test_vad_socket_poller.py +++ b/test/unit/agents/test_vad_socket_poller.py @@ -3,7 +3,7 @@ from unittest.mock import AsyncMock, MagicMock import pytest import zmq -from control_backend.agents.vad_agent import SocketPoller +from control_backend.agents.per_agents.per_vad_agent import SocketPoller @pytest.fixture @@ -16,7 +16,9 @@ async def test_socket_poller_with_data(socket, mocker): socket_data = b"test" socket.recv.return_value = socket_data - mock_poller: MagicMock = mocker.patch("control_backend.agents.vad_agent.zmq.Poller") + mock_poller: MagicMock = mocker.patch( + "control_backend.agents.per_agents.per_vad_agent.zmq.Poller" + ) mock_poller.return_value.poll.return_value = [(socket, zmq.POLLIN)] poller = SocketPoller(socket) @@ -35,7 +37,9 @@ async def test_socket_poller_with_data(socket, mocker): @pytest.mark.asyncio async def test_socket_poller_no_data(socket, mocker): - mock_poller: MagicMock = mocker.patch("control_backend.agents.vad_agent.zmq.Poller") + mock_poller: MagicMock = mocker.patch( + "control_backend.agents.per_agents.per_vad_agent.zmq.Poller" + ) mock_poller.return_value.poll.return_value = [] poller = SocketPoller(socket) diff --git a/test/unit/agents/test_vad_streaming.py b/test/unit/agents/test_vad_streaming.py index 0cd8161..1c35c9f 100644 --- a/test/unit/agents/test_vad_streaming.py +++ b/test/unit/agents/test_vad_streaming.py @@ -3,7 +3,7 @@ from unittest.mock import AsyncMock, MagicMock import numpy as np import pytest -from control_backend.agents.vad_agent import Streaming +from control_backend.agents.per_agents.per_vad_agent import Streaming @pytest.fixture @@ -20,7 +20,7 @@ def audio_out_socket(): def mock_agent(mocker): """Fixture to create a mock BDIAgent.""" agent = MagicMock() - agent.jid = "vad_agent@test" + agent.jid = "per_vad_agent@test" return agent diff --git a/test/unit/agents/transcription/test_speech_recognizer.py b/test/unit/agents/transcription/test_speech_recognizer.py index d0dfdea..347233a 100644 --- a/test/unit/agents/transcription/test_speech_recognizer.py +++ b/test/unit/agents/transcription/test_speech_recognizer.py @@ -1,6 +1,6 @@ import numpy as np -from control_backend.agents.transcription.speech_recognizer import ( +from control_backend.agents.per_agents.per_transcription_agent.speech_recognizer import ( OpenAIWhisperSpeechRecognizer, SpeechRecognizer, ) From dfebe6f7726f45b9e678f2fef4a7254709cf47d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Otgaar?= Date: Wed, 12 Nov 2025 11:36:51 +0100 Subject: [PATCH 15/26] refactor: make sure that in main the correct names and passwords are called for starting the agents ref: N25B-257 --- .../agents/bel_agents/__init__.py | 4 +++ .../bel_collector_agent/__init__.py | 1 - src/control_backend/core/config.py | 2 +- src/control_backend/main.py | 31 +++++++++++++------ 4 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 src/control_backend/agents/bel_agents/__init__.py delete mode 100644 src/control_backend/agents/bel_agents/bel_collector_agent/__init__.py diff --git a/src/control_backend/agents/bel_agents/__init__.py b/src/control_backend/agents/bel_agents/__init__.py new file mode 100644 index 0000000..5f2ce21 --- /dev/null +++ b/src/control_backend/agents/bel_agents/__init__.py @@ -0,0 +1,4 @@ +from .bel_collector_agent.bel_collector_agent import BelCollectorAgent as BelCollectorAgent +from .bel_text_extract_agent.bel_text_extract_agent import ( + BelTextExtractAgent as BelTextExtractAgent, +) diff --git a/src/control_backend/agents/bel_agents/bel_collector_agent/__init__.py b/src/control_backend/agents/bel_agents/bel_collector_agent/__init__.py deleted file mode 100644 index 3b5a313..0000000 --- a/src/control_backend/agents/bel_agents/bel_collector_agent/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from .bel_collector_agent import BelCollectorAgent as BelCollectorAgent diff --git a/src/control_backend/core/config.py b/src/control_backend/core/config.py index 55fd583..955ce16 100644 --- a/src/control_backend/core/config.py +++ b/src/control_backend/core/config.py @@ -11,7 +11,7 @@ class AgentSettings(BaseModel): host: str = "localhost" bdi_core_agent_agent_name: str = "bdi_core_agent" bel_collector_agent_name: str = "bel_collector_agent" - texbel_text_extractor_agent_name: str = "text_belief_extractor" + bel_text_extractor_agent_name: str = "bel_text_extractor_agent" per_vad_agent_name: str = "per_vad_agent" llm_agent_name: str = "llm_agent" test_agent_name: str = "test_agent" diff --git a/src/control_backend/main.py b/src/control_backend/main.py index 25e2a7c..be0827e 100644 --- a/src/control_backend/main.py +++ b/src/control_backend/main.py @@ -7,13 +7,24 @@ from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from zmq.asyncio import Context -from control_backend.agents import ( - BelCollectorAgent, - ComRIAgent, - LLMAgent, - PerVADAgent, -) -from control_backend.agents.bdi_agents import BDICoreAgent, BelTextExtractAgent +# Act agents +# BDI agents +from control_backend.agents.bdi_agents import BDICoreAgent + +# Believe Agents +from control_backend.agents.bel_agents import BelCollectorAgent, BelTextExtractAgent + +# Communication agents +from control_backend.agents.com_agents import ComRIAgent + +# Emotional Agents +# LLM Agents +from control_backend.agents.llm_agents import LLMAgent + +# Perceive agents +from control_backend.agents.per_agents import PerVADAgent + +# Other backend imports from control_backend.api.v1.router import api_router from control_backend.core.config import settings from control_backend.logging import setup_logging @@ -105,10 +116,10 @@ async def lifespan(app: FastAPI): "TBeliefExtractor": ( BelTextExtractAgent, { - "name": settings.agent_settings.texbel_text_extractor_agent_name, - "jid": f"{settings.agent_settings.texbel_text_extractor_agent_name}@" + "name": settings.agent_settings.bel_text_extractor_agent_name, + "jid": f"{settings.agent_settings.bel_text_extractor_agent_name}@" f"{settings.agent_settings.host}", - "password": settings.agent_settings.texbel_text_extractor_agent_name, + "password": settings.agent_settings.bel_text_extractor_agent_name, }, ), "PerVADAgent": ( From 9365f109ab22f329cf3f9fd2af110c816fcacff2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Otgaar?= Date: Wed, 12 Nov 2025 12:01:37 +0100 Subject: [PATCH 16/26] refactor: restructure to make sure the Bel agents are also part of BDI. ref: N25B-257 --- .../agents/bdi_agents/__init__.py | 6 ++++- .../behaviours/continuous_collect.py | 0 .../bel_collector_agent.py | 6 ++--- .../{ => bdi_core_agent}/bdi_core_agent.py | 0 .../behaviours/belief_setter.py | 2 +- .../behaviours/receive_llm_resp_behaviour.py | 0 .../bdi_agents/{ => bdi_core_agent}/rules.asl | 0 .../bdi_text_belief_agent.py} | 2 +- .../behaviours/text_belief_extractor.py | 6 +++-- .../agents/bel_agents/__init__.py | 4 --- .../agents/mock_agents/bel_text_agent.py | 2 +- .../per_transcription_agent.py | 2 +- src/control_backend/core/config.py | 4 +-- src/control_backend/main.py | 27 ++++++++++--------- .../bdi/behaviours/test_belief_setter.py | 11 +++++--- .../behaviours/test_continuous_collect.py | 4 +-- .../behaviours/test_belief_from_text.py | 10 +++---- 17 files changed, 46 insertions(+), 40 deletions(-) rename src/control_backend/agents/{bel_agents/bel_collector_agent => bdi_agents/bdi_belief_collector_agent}/behaviours/continuous_collect.py (100%) rename src/control_backend/agents/{bel_agents/bel_collector_agent => bdi_agents/bdi_belief_collector_agent}/bel_collector_agent.py (61%) rename src/control_backend/agents/bdi_agents/{ => bdi_core_agent}/bdi_core_agent.py (100%) rename src/control_backend/agents/bdi_agents/{ => bdi_core_agent}/behaviours/belief_setter.py (97%) rename src/control_backend/agents/bdi_agents/{ => bdi_core_agent}/behaviours/receive_llm_resp_behaviour.py (100%) rename src/control_backend/agents/bdi_agents/{ => bdi_core_agent}/rules.asl (100%) rename src/control_backend/agents/{bel_agents/bel_text_extract_agent/bel_text_extract_agent.py => bdi_agents/bdi_text_belief_agent/bdi_text_belief_agent.py} (83%) rename src/control_backend/agents/{bel_agents/bel_text_extract_agent => bdi_agents/bdi_text_belief_agent}/behaviours/text_belief_extractor.py (95%) delete mode 100644 src/control_backend/agents/bel_agents/__init__.py diff --git a/src/control_backend/agents/bdi_agents/__init__.py b/src/control_backend/agents/bdi_agents/__init__.py index 76f4876..9b17f30 100644 --- a/src/control_backend/agents/bdi_agents/__init__.py +++ b/src/control_backend/agents/bdi_agents/__init__.py @@ -1 +1,5 @@ -from .bdi_core_agent import BDICoreAgent as BDICoreAgent +from .bdi_belief_collector_agent.bel_collector_agent import ( + BDIBeliefCollectorAgent as BDIBeliefCollectorAgent, +) +from .bdi_core_agent.bdi_core_agent import BDICoreAgent as BDICoreAgent +from .bdi_text_belief_agent.bdi_text_belief_agent import BDITextBeliefAgent as BDITextBeliefAgent diff --git a/src/control_backend/agents/bel_agents/bel_collector_agent/behaviours/continuous_collect.py b/src/control_backend/agents/bdi_agents/bdi_belief_collector_agent/behaviours/continuous_collect.py similarity index 100% rename from src/control_backend/agents/bel_agents/bel_collector_agent/behaviours/continuous_collect.py rename to src/control_backend/agents/bdi_agents/bdi_belief_collector_agent/behaviours/continuous_collect.py diff --git a/src/control_backend/agents/bel_agents/bel_collector_agent/bel_collector_agent.py b/src/control_backend/agents/bdi_agents/bdi_belief_collector_agent/bel_collector_agent.py similarity index 61% rename from src/control_backend/agents/bel_agents/bel_collector_agent/bel_collector_agent.py rename to src/control_backend/agents/bdi_agents/bdi_belief_collector_agent/bel_collector_agent.py index 5b633d5..39e55ff 100644 --- a/src/control_backend/agents/bel_agents/bel_collector_agent/bel_collector_agent.py +++ b/src/control_backend/agents/bdi_agents/bdi_belief_collector_agent/bel_collector_agent.py @@ -3,9 +3,9 @@ from control_backend.agents.base import BaseAgent from .behaviours.continuous_collect import ContinuousBeliefCollector -class BelCollectorAgent(BaseAgent): +class BDIBeliefCollectorAgent(BaseAgent): async def setup(self): - self.logger.info("BelCollectorAgent starting (%s)", self.jid) + self.logger.info("BDIBeliefCollectorAgent starting (%s)", self.jid) # Attach the continuous collector behaviour (listens and forwards to BDI) self.add_behaviour(ContinuousBeliefCollector()) - self.logger.info("BelCollectorAgent ready.") + self.logger.info("BDIBeliefCollectorAgent ready.") diff --git a/src/control_backend/agents/bdi_agents/bdi_core_agent.py b/src/control_backend/agents/bdi_agents/bdi_core_agent/bdi_core_agent.py similarity index 100% rename from src/control_backend/agents/bdi_agents/bdi_core_agent.py rename to src/control_backend/agents/bdi_agents/bdi_core_agent/bdi_core_agent.py diff --git a/src/control_backend/agents/bdi_agents/behaviours/belief_setter.py b/src/control_backend/agents/bdi_agents/bdi_core_agent/behaviours/belief_setter.py similarity index 97% rename from src/control_backend/agents/bdi_agents/behaviours/belief_setter.py rename to src/control_backend/agents/bdi_agents/bdi_core_agent/behaviours/belief_setter.py index 9ffb2f3..d96a239 100644 --- a/src/control_backend/agents/bdi_agents/behaviours/belief_setter.py +++ b/src/control_backend/agents/bdi_agents/bdi_core_agent/behaviours/belief_setter.py @@ -32,7 +32,7 @@ class BeliefSetterBehaviour(CyclicBehaviour): self.agent.logger.debug("Processing message from sender: %s", sender) match sender: - case settings.agent_settings.bel_collector_agent_name: + case settings.agent_settings.bdi_belief_collector_agent_name: self.agent.logger.debug( "Message is from the belief collector agent. Processing as belief message." ) diff --git a/src/control_backend/agents/bdi_agents/behaviours/receive_llm_resp_behaviour.py b/src/control_backend/agents/bdi_agents/bdi_core_agent/behaviours/receive_llm_resp_behaviour.py similarity index 100% rename from src/control_backend/agents/bdi_agents/behaviours/receive_llm_resp_behaviour.py rename to src/control_backend/agents/bdi_agents/bdi_core_agent/behaviours/receive_llm_resp_behaviour.py diff --git a/src/control_backend/agents/bdi_agents/rules.asl b/src/control_backend/agents/bdi_agents/bdi_core_agent/rules.asl similarity index 100% rename from src/control_backend/agents/bdi_agents/rules.asl rename to src/control_backend/agents/bdi_agents/bdi_core_agent/rules.asl diff --git a/src/control_backend/agents/bel_agents/bel_text_extract_agent/bel_text_extract_agent.py b/src/control_backend/agents/bdi_agents/bdi_text_belief_agent/bdi_text_belief_agent.py similarity index 83% rename from src/control_backend/agents/bel_agents/bel_text_extract_agent/bel_text_extract_agent.py rename to src/control_backend/agents/bdi_agents/bdi_text_belief_agent/bdi_text_belief_agent.py index 8716b4c..deaffa3 100644 --- a/src/control_backend/agents/bel_agents/bel_text_extract_agent/bel_text_extract_agent.py +++ b/src/control_backend/agents/bdi_agents/bdi_text_belief_agent/bdi_text_belief_agent.py @@ -3,6 +3,6 @@ from control_backend.agents.base import BaseAgent from .behaviours.text_belief_extractor import BeliefFromText -class BelTextExtractAgent(BaseAgent): +class BDITextBeliefAgent(BaseAgent): async def setup(self): self.add_behaviour(BeliefFromText()) diff --git a/src/control_backend/agents/bel_agents/bel_text_extract_agent/behaviours/text_belief_extractor.py b/src/control_backend/agents/bdi_agents/bdi_text_belief_agent/behaviours/text_belief_extractor.py similarity index 95% rename from src/control_backend/agents/bel_agents/bel_text_extract_agent/behaviours/text_belief_extractor.py rename to src/control_backend/agents/bdi_agents/bdi_text_belief_agent/behaviours/text_belief_extractor.py index 3606bd0..812bcc9 100644 --- a/src/control_backend/agents/bel_agents/bel_text_extract_agent/behaviours/text_belief_extractor.py +++ b/src/control_backend/agents/bdi_agents/bdi_text_belief_agent/behaviours/text_belief_extractor.py @@ -71,7 +71,7 @@ class BeliefFromText(CyclicBehaviour): belief_message = Message() belief_message.to = ( - settings.agent_settings.bel_collector_agent_name + settings.agent_settings.bdi_belief_collector_agent_name + "@" + settings.agent_settings.host ) @@ -95,7 +95,9 @@ class BeliefFromText(CyclicBehaviour): belief_msg = Message() belief_msg.to = ( - settings.agent_settings.bel_collector_agent_name + "@" + settings.agent_settings.host + settings.agent_settings.bdi_belief_collector_agent_name + + "@" + + settings.agent_settings.host ) belief_msg.body = payload belief_msg.thread = "beliefs" diff --git a/src/control_backend/agents/bel_agents/__init__.py b/src/control_backend/agents/bel_agents/__init__.py deleted file mode 100644 index 5f2ce21..0000000 --- a/src/control_backend/agents/bel_agents/__init__.py +++ /dev/null @@ -1,4 +0,0 @@ -from .bel_collector_agent.bel_collector_agent import BelCollectorAgent as BelCollectorAgent -from .bel_text_extract_agent.bel_text_extract_agent import ( - BelTextExtractAgent as BelTextExtractAgent, -) diff --git a/src/control_backend/agents/mock_agents/bel_text_agent.py b/src/control_backend/agents/mock_agents/bel_text_agent.py index 2808cae..2827307 100644 --- a/src/control_backend/agents/mock_agents/bel_text_agent.py +++ b/src/control_backend/agents/mock_agents/bel_text_agent.py @@ -11,7 +11,7 @@ class BelTextAgent(Agent): class SendOnceBehaviourBlfText(OneShotBehaviour): async def run(self): to_jid = ( - settings.agent_settings.bel_collector_agent_name + settings.agent_settings.bdi_belief_collector_agent_name + "@" + settings.agent_settings.host ) diff --git a/src/control_backend/agents/per_agents/per_transcription_agent/per_transcription_agent.py b/src/control_backend/agents/per_agents/per_transcription_agent/per_transcription_agent.py index 50571b8..e82b5d7 100644 --- a/src/control_backend/agents/per_agents/per_transcription_agent/per_transcription_agent.py +++ b/src/control_backend/agents/per_agents/per_transcription_agent/per_transcription_agent.py @@ -47,7 +47,7 @@ class PerTranscriptionAgent(BaseAgent): async def _share_transcription(self, transcription: str): """Share a transcription to the other agents that depend on it.""" receiver_jids = [ - settings.agent_settings.texbel_text_extractor_agent_name + settings.agent_settings.texbdi_text_belief_agent_name + "@" + settings.agent_settings.host, ] # Set message receivers here diff --git a/src/control_backend/core/config.py b/src/control_backend/core/config.py index 955ce16..150a06e 100644 --- a/src/control_backend/core/config.py +++ b/src/control_backend/core/config.py @@ -10,8 +10,8 @@ class ZMQSettings(BaseModel): class AgentSettings(BaseModel): host: str = "localhost" bdi_core_agent_agent_name: str = "bdi_core_agent" - bel_collector_agent_name: str = "bel_collector_agent" - bel_text_extractor_agent_name: str = "bel_text_extractor_agent" + bdi_belief_collector_agent_name: str = "bdi_belief_collector_agent" + bdi_text_belief_agent_name: str = "bdi_text_belief_agent" per_vad_agent_name: str = "per_vad_agent" llm_agent_name: str = "llm_agent" test_agent_name: str = "test_agent" diff --git a/src/control_backend/main.py b/src/control_backend/main.py index be0827e..008a8e3 100644 --- a/src/control_backend/main.py +++ b/src/control_backend/main.py @@ -9,10 +9,11 @@ from zmq.asyncio import Context # Act agents # BDI agents -from control_backend.agents.bdi_agents import BDICoreAgent - -# Believe Agents -from control_backend.agents.bel_agents import BelCollectorAgent, BelTextExtractAgent +from control_backend.agents.bdi_agents import ( + BDIBeliefCollectorAgent, + BDICoreAgent, + BDITextBeliefAgent, +) # Communication agents from control_backend.agents.com_agents import ComRIAgent @@ -104,22 +105,22 @@ async def lifespan(app: FastAPI): "asl": "src/control_backend/agents/bdi/rules.asl", }, ), - "BelCollectorAgent": ( - BelCollectorAgent, + "BDIBeliefCollectorAgent": ( + BDIBeliefCollectorAgent, { - "name": settings.agent_settings.bel_collector_agent_name, - "jid": f"{settings.agent_settings.bel_collector_agent_name}@" + "name": settings.agent_settings.bdi_belief_collector_agent_name, + "jid": f"{settings.agent_settings.bdi_belief_collector_agent_name}@" f"{settings.agent_settings.host}", - "password": settings.agent_settings.bel_collector_agent_name, + "password": settings.agent_settings.bdi_belief_collector_agent_name, }, ), "TBeliefExtractor": ( - BelTextExtractAgent, + BDITextBeliefAgent, { - "name": settings.agent_settings.bel_text_extractor_agent_name, - "jid": f"{settings.agent_settings.bel_text_extractor_agent_name}@" + "name": settings.agent_settings.bdi_text_belief_agent_name, + "jid": f"{settings.agent_settings.bdi_text_belief_agent_name}@" f"{settings.agent_settings.host}", - "password": settings.agent_settings.bel_text_extractor_agent_name, + "password": settings.agent_settings.bdi_text_belief_agent_name, }, ), "PerVADAgent": ( diff --git a/test/unit/agents/bdi/behaviours/test_belief_setter.py b/test/unit/agents/bdi/behaviours/test_belief_setter.py index 5a3b18e..238285a 100644 --- a/test/unit/agents/bdi/behaviours/test_belief_setter.py +++ b/test/unit/agents/bdi/behaviours/test_belief_setter.py @@ -4,10 +4,12 @@ from unittest.mock import AsyncMock, MagicMock, call import pytest -from control_backend.agents.bdi_agents.behaviours.belief_setter import BeliefSetterBehaviour +from control_backend.agents.bdi_agents.bdi_core_agent.behaviours.belief_setter import ( + BeliefSetterBehaviour, +) # Define a constant for the collector agent name to use in tests -COLLECTOR_AGENT_NAME = "bel_collector_agent" +COLLECTOR_AGENT_NAME = "bdi_belief_collector_agent" COLLECTOR_AGENT_JID = f"{COLLECTOR_AGENT_NAME}@test" @@ -25,7 +27,8 @@ def belief_setter(mock_agent, mocker): """Fixture to create an instance of BeliefSetterBehaviour with a mocked agent.""" # Patch the settings to use a predictable agent name mocker.patch( - "control_backend.agents.bdi_agents.behaviours.belief_setter.settings.agent_settings.bel_collector_agent_name", + "control_backend.agents.bdi_agents.bdi_core_agent." + "behaviours.belief_setter.settings.agent_settings.bdi_belief_collector_agent_name", COLLECTOR_AGENT_NAME, ) @@ -62,7 +65,7 @@ async def test_run_message_received(belief_setter, mocker): belief_setter._process_message.assert_called_once_with(msg) -def test_process_message_from_bel_collector_agent(belief_setter, mocker): +def test_process_message_from_bdi_belief_collector_agent(belief_setter, mocker): """ Test processing a message from the correct belief collector agent. """ diff --git a/test/unit/agents/belief_collector/behaviours/test_continuous_collect.py b/test/unit/agents/belief_collector/behaviours/test_continuous_collect.py index a21cc06..40136c6 100644 --- a/test/unit/agents/belief_collector/behaviours/test_continuous_collect.py +++ b/test/unit/agents/belief_collector/behaviours/test_continuous_collect.py @@ -3,7 +3,7 @@ from unittest.mock import AsyncMock, MagicMock import pytest -from control_backend.agents.bel_agents.bel_collector_agent.behaviours.continuous_collect import ( +from control_backend.agents.bdi_agents.bdi_belief_collector_agent.behaviours.continuous_collect import ( # noqa: E501 ContinuousBeliefCollector, ) @@ -20,7 +20,7 @@ def create_mock_message(sender_node: str, body: str) -> MagicMock: def mock_agent(mocker): """Fixture to create a mock Agent.""" agent = MagicMock() - agent.jid = "bel_collector_agent@test" + agent.jid = "bdi_belief_collector_agent@test" return agent diff --git a/test/unit/agents/belief_from_text/behaviours/test_belief_from_text.py b/test/unit/agents/belief_from_text/behaviours/test_belief_from_text.py index 4efd0e3..cd2fda1 100644 --- a/test/unit/agents/belief_from_text/behaviours/test_belief_from_text.py +++ b/test/unit/agents/belief_from_text/behaviours/test_belief_from_text.py @@ -4,7 +4,7 @@ from unittest.mock import AsyncMock, MagicMock, patch import pytest from spade.message import Message -from control_backend.agents.bel_agents.bel_text_extract_agent.behaviours.text_belief_extractor import ( # noqa: E501, We can't shorten this import. +from control_backend.agents.bdi_agents.bdi_text_belief_agent.behaviours.text_belief_extractor import ( # noqa: E501, We can't shorten this import. BeliefFromText, ) @@ -18,14 +18,14 @@ def mock_settings(): # Create a mock object that mimics the nested structure settings_mock = MagicMock() settings_mock.agent_settings.per_transcription_agent_name = "transcriber" - settings_mock.agent_settings.bel_collector_agent_name = "collector" + settings_mock.agent_settings.bdi_belief_collector_agent_name = "collector" settings_mock.agent_settings.host = "fake.host" # Use patch to replace the settings object during the test # Adjust 'control_backend.behaviours.belief_from_text.settings' to where # your behaviour file imports it from. with patch( - "control_backend.agents.bel_agents.bel_text_extract_agent.behaviours.text_belief_extractor.settings", + "control_backend.agents.bdi_agents.bdi_text_belief_agent.behaviours.text_belief_extractor.settings", settings_mock, ): yield settings_mock @@ -122,7 +122,7 @@ async def test_run_message_from_transcriber_demo(behavior, mock_settings, monkey assert ( sent_msg.to - == mock_settings.agent_settings.bel_collector_agent_name + == mock_settings.agent_settings.bdi_belief_collector_agent_name + "@" + mock_settings.agent_settings.host ) @@ -162,7 +162,7 @@ async def test_process_transcription_success(behavior, mock_settings): # 2. Inspect the sent message sent_msg: Message = behavior.send.call_args[0][0] expected_to = ( - mock_settings.agent_settings.bel_collector_agent_name + mock_settings.agent_settings.bdi_belief_collector_agent_name + "@" + mock_settings.agent_settings.host ) From 7a707cf9a03607cb8d89f822218d3925f322759c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Otgaar?= Date: Wed, 12 Nov 2025 12:42:54 +0100 Subject: [PATCH 17/26] refactor: change test folder structure, rename functions to account for (non)changing behaviours and clarity ref: N25B-257 --- .../agents/act_agents/act_speech_agent.py | 8 +-- ..._collect.py => bel_collector_behaviour.py} | 2 +- .../bel_collector_agent.py | 4 +- .../bdi_core_agent/bdi_core_agent.py | 2 +- ...f_setter.py => belief_setter_behaviour.py} | 0 .../bdi_text_belief_agent.py | 4 +- ...ractor.py => bdi_text_belief_behaviour.py} | 2 +- src/control_backend/main.py | 2 +- .../{ => act_agents}/test_act_speech_agent.py | 6 +- .../{ => com_agents}/test_com_ri_agent.py | 0 .../speech_with_pauses_16k_1c_float32.wav | Bin .../per_vad_agent/test_per_vad_agent.py | 0 .../per_vad_agent/test_per_vad_with_audio.py | 0 .../behaviours/test_continuous_collect.py | 54 +++++++-------- .../behaviours/test_belief_setter.py | 64 +++++++++--------- .../behaviours/test_belief_from_text.py | 10 +-- .../test_speech_recognizer.py | 0 .../per_vad_agent}/test_vad_socket_poller.py | 0 .../per_vad_agent}/test_vad_streaming.py | 0 19 files changed, 79 insertions(+), 79 deletions(-) rename src/control_backend/agents/bdi_agents/bdi_belief_collector_agent/behaviours/{continuous_collect.py => bel_collector_behaviour.py} (98%) rename src/control_backend/agents/bdi_agents/bdi_core_agent/behaviours/{belief_setter.py => belief_setter_behaviour.py} (100%) rename src/control_backend/agents/bdi_agents/bdi_text_belief_agent/behaviours/{text_belief_extractor.py => bdi_text_belief_behaviour.py} (98%) rename test/integration/agents/{ => act_agents}/test_act_speech_agent.py (94%) rename test/integration/agents/{ => com_agents}/test_com_ri_agent.py (100%) rename test/integration/agents/{ => per_agents}/per_vad_agent/speech_with_pauses_16k_1c_float32.wav (100%) rename test/integration/agents/{ => per_agents}/per_vad_agent/test_per_vad_agent.py (100%) rename test/integration/agents/{ => per_agents}/per_vad_agent/test_per_vad_with_audio.py (100%) rename test/unit/agents/{belief_collector => bdi_agents/bdi_belief_collector_agent}/behaviours/test_continuous_collect.py (51%) rename test/unit/agents/{bdi => bdi_agents/bdi_core_agent}/behaviours/test_belief_setter.py (67%) rename test/unit/agents/{belief_from_text => bdi_agents/bdi_text_belief_agent}/behaviours/test_belief_from_text.py (95%) rename test/unit/agents/{transcription => per_agents/per_transcription_agent}/test_speech_recognizer.py (100%) rename test/unit/agents/{ => per_agents/per_vad_agent}/test_vad_socket_poller.py (100%) rename test/unit/agents/{ => per_agents/per_vad_agent}/test_vad_streaming.py (100%) diff --git a/src/control_backend/agents/act_agents/act_speech_agent.py b/src/control_backend/agents/act_agents/act_speech_agent.py index 726a07d..0b63a36 100644 --- a/src/control_backend/agents/act_agents/act_speech_agent.py +++ b/src/control_backend/agents/act_agents/act_speech_agent.py @@ -29,7 +29,7 @@ class ActSpeechAgent(BaseAgent): self.address = address self.bind = bind - class SendCommandsBehaviour(CyclicBehaviour): + class SendZMQCommandsBehaviour(CyclicBehaviour): """Behaviour for sending commands received from the UI.""" async def run(self): @@ -50,7 +50,7 @@ class ActSpeechAgent(BaseAgent): except Exception as e: self.agent.logger.error("Error processing message: %s", e) - class SendPythonCommandsBehaviour(CyclicBehaviour): + class SendSpadeCommandsBehaviour(CyclicBehaviour): """Behaviour for sending commands received from other Python agents.""" async def run(self): @@ -83,8 +83,8 @@ class ActSpeechAgent(BaseAgent): self.subsocket.setsockopt(zmq.SUBSCRIBE, b"command") # Add behaviour to our agent - commands_behaviour = self.SendCommandsBehaviour() + commands_behaviour = self.SendZMQCommandsBehaviour() self.add_behaviour(commands_behaviour) - self.add_behaviour(self.SendPythonCommandsBehaviour()) + self.add_behaviour(self.SendSpadeCommandsBehaviour()) self.logger.info("Finished setting up %s", self.jid) diff --git a/src/control_backend/agents/bdi_agents/bdi_belief_collector_agent/behaviours/continuous_collect.py b/src/control_backend/agents/bdi_agents/bdi_belief_collector_agent/behaviours/bel_collector_behaviour.py similarity index 98% rename from src/control_backend/agents/bdi_agents/bdi_belief_collector_agent/behaviours/continuous_collect.py rename to src/control_backend/agents/bdi_agents/bdi_belief_collector_agent/behaviours/bel_collector_behaviour.py index 512be47..ff89eae 100644 --- a/src/control_backend/agents/bdi_agents/bdi_belief_collector_agent/behaviours/continuous_collect.py +++ b/src/control_backend/agents/bdi_agents/bdi_belief_collector_agent/behaviours/bel_collector_behaviour.py @@ -7,7 +7,7 @@ from spade.behaviour import CyclicBehaviour from control_backend.core.config import settings -class ContinuousBeliefCollector(CyclicBehaviour): +class BelCollectorBehaviour(CyclicBehaviour): """ Continuously collects beliefs/emotions from extractor agents: Then we send a unified belief packet to the BDI agent. diff --git a/src/control_backend/agents/bdi_agents/bdi_belief_collector_agent/bel_collector_agent.py b/src/control_backend/agents/bdi_agents/bdi_belief_collector_agent/bel_collector_agent.py index 39e55ff..f08e6a6 100644 --- a/src/control_backend/agents/bdi_agents/bdi_belief_collector_agent/bel_collector_agent.py +++ b/src/control_backend/agents/bdi_agents/bdi_belief_collector_agent/bel_collector_agent.py @@ -1,11 +1,11 @@ from control_backend.agents.base import BaseAgent -from .behaviours.continuous_collect import ContinuousBeliefCollector +from .behaviours.bel_collector_behaviour import BelCollectorBehaviour class BDIBeliefCollectorAgent(BaseAgent): async def setup(self): self.logger.info("BDIBeliefCollectorAgent starting (%s)", self.jid) # Attach the continuous collector behaviour (listens and forwards to BDI) - self.add_behaviour(ContinuousBeliefCollector()) + self.add_behaviour(BelCollectorBehaviour()) self.logger.info("BDIBeliefCollectorAgent ready.") diff --git a/src/control_backend/agents/bdi_agents/bdi_core_agent/bdi_core_agent.py b/src/control_backend/agents/bdi_agents/bdi_core_agent/bdi_core_agent.py index 4d68e26..9a8a9f1 100644 --- a/src/control_backend/agents/bdi_agents/bdi_core_agent/bdi_core_agent.py +++ b/src/control_backend/agents/bdi_agents/bdi_core_agent/bdi_core_agent.py @@ -7,7 +7,7 @@ from spade_bdi.bdi import BDIAgent from control_backend.core.config import settings -from .behaviours.belief_setter import BeliefSetterBehaviour +from .behaviours.belief_setter_behaviour import BeliefSetterBehaviour from .behaviours.receive_llm_resp_behaviour import ReceiveLLMResponseBehaviour diff --git a/src/control_backend/agents/bdi_agents/bdi_core_agent/behaviours/belief_setter.py b/src/control_backend/agents/bdi_agents/bdi_core_agent/behaviours/belief_setter_behaviour.py similarity index 100% rename from src/control_backend/agents/bdi_agents/bdi_core_agent/behaviours/belief_setter.py rename to src/control_backend/agents/bdi_agents/bdi_core_agent/behaviours/belief_setter_behaviour.py diff --git a/src/control_backend/agents/bdi_agents/bdi_text_belief_agent/bdi_text_belief_agent.py b/src/control_backend/agents/bdi_agents/bdi_text_belief_agent/bdi_text_belief_agent.py index deaffa3..c9987cf 100644 --- a/src/control_backend/agents/bdi_agents/bdi_text_belief_agent/bdi_text_belief_agent.py +++ b/src/control_backend/agents/bdi_agents/bdi_text_belief_agent/bdi_text_belief_agent.py @@ -1,8 +1,8 @@ from control_backend.agents.base import BaseAgent -from .behaviours.text_belief_extractor import BeliefFromText +from .behaviours.bdi_text_belief_behaviour import BDITextBeliefBehaviour class BDITextBeliefAgent(BaseAgent): async def setup(self): - self.add_behaviour(BeliefFromText()) + self.add_behaviour(BDITextBeliefBehaviour()) diff --git a/src/control_backend/agents/bdi_agents/bdi_text_belief_agent/behaviours/text_belief_extractor.py b/src/control_backend/agents/bdi_agents/bdi_text_belief_agent/behaviours/bdi_text_belief_behaviour.py similarity index 98% rename from src/control_backend/agents/bdi_agents/bdi_text_belief_agent/behaviours/text_belief_extractor.py rename to src/control_backend/agents/bdi_agents/bdi_text_belief_agent/behaviours/bdi_text_belief_behaviour.py index 812bcc9..c17a4d0 100644 --- a/src/control_backend/agents/bdi_agents/bdi_text_belief_agent/behaviours/text_belief_extractor.py +++ b/src/control_backend/agents/bdi_agents/bdi_text_belief_agent/behaviours/bdi_text_belief_behaviour.py @@ -7,7 +7,7 @@ from spade.message import Message from control_backend.core.config import settings -class BeliefFromText(CyclicBehaviour): +class BDITextBeliefBehaviour(CyclicBehaviour): logger = logging.getLogger(__name__) # TODO: LLM prompt nog hardcoded diff --git a/src/control_backend/main.py b/src/control_backend/main.py index 008a8e3..5e8bcaa 100644 --- a/src/control_backend/main.py +++ b/src/control_backend/main.py @@ -114,7 +114,7 @@ async def lifespan(app: FastAPI): "password": settings.agent_settings.bdi_belief_collector_agent_name, }, ), - "TBeliefExtractor": ( + "BDITextBeliefAgent": ( BDITextBeliefAgent, { "name": settings.agent_settings.bdi_text_belief_agent_name, diff --git a/test/integration/agents/test_act_speech_agent.py b/test/integration/agents/act_agents/test_act_speech_agent.py similarity index 94% rename from test/integration/agents/test_act_speech_agent.py rename to test/integration/agents/act_agents/test_act_speech_agent.py index 909c51c..ccd9d9f 100644 --- a/test/integration/agents/test_act_speech_agent.py +++ b/test/integration/agents/act_agents/test_act_speech_agent.py @@ -34,7 +34,7 @@ async def test_setup_bind(zmq_context, mocker): fake_socket.setsockopt.assert_any_call(zmq.SUBSCRIBE, b"command") # Ensure behaviour attached - assert any(isinstance(b, agent.SendCommandsBehaviour) for b in agent.behaviours) + assert any(isinstance(b, agent.SendZMQCommandsBehaviour) for b in agent.behaviours) @pytest.mark.asyncio @@ -66,7 +66,7 @@ async def test_send_commands_behaviour_valid_message(): agent.subsocket = fake_socket agent.pubsocket = fake_socket - behaviour = agent.SendCommandsBehaviour() + behaviour = agent.SendZMQCommandsBehaviour() behaviour.agent = agent with patch( @@ -92,7 +92,7 @@ async def test_send_commands_behaviour_invalid_message(caplog): agent.subsocket = fake_socket agent.pubsocket = fake_socket - behaviour = agent.SendCommandsBehaviour() + behaviour = agent.SendZMQCommandsBehaviour() behaviour.agent = agent await behaviour.run() diff --git a/test/integration/agents/test_com_ri_agent.py b/test/integration/agents/com_agents/test_com_ri_agent.py similarity index 100% rename from test/integration/agents/test_com_ri_agent.py rename to test/integration/agents/com_agents/test_com_ri_agent.py diff --git a/test/integration/agents/per_vad_agent/speech_with_pauses_16k_1c_float32.wav b/test/integration/agents/per_agents/per_vad_agent/speech_with_pauses_16k_1c_float32.wav similarity index 100% rename from test/integration/agents/per_vad_agent/speech_with_pauses_16k_1c_float32.wav rename to test/integration/agents/per_agents/per_vad_agent/speech_with_pauses_16k_1c_float32.wav diff --git a/test/integration/agents/per_vad_agent/test_per_vad_agent.py b/test/integration/agents/per_agents/per_vad_agent/test_per_vad_agent.py similarity index 100% rename from test/integration/agents/per_vad_agent/test_per_vad_agent.py rename to test/integration/agents/per_agents/per_vad_agent/test_per_vad_agent.py diff --git a/test/integration/agents/per_vad_agent/test_per_vad_with_audio.py b/test/integration/agents/per_agents/per_vad_agent/test_per_vad_with_audio.py similarity index 100% rename from test/integration/agents/per_vad_agent/test_per_vad_with_audio.py rename to test/integration/agents/per_agents/per_vad_agent/test_per_vad_with_audio.py diff --git a/test/unit/agents/belief_collector/behaviours/test_continuous_collect.py b/test/unit/agents/bdi_agents/bdi_belief_collector_agent/behaviours/test_continuous_collect.py similarity index 51% rename from test/unit/agents/belief_collector/behaviours/test_continuous_collect.py rename to test/unit/agents/bdi_agents/bdi_belief_collector_agent/behaviours/test_continuous_collect.py index 40136c6..001262f 100644 --- a/test/unit/agents/belief_collector/behaviours/test_continuous_collect.py +++ b/test/unit/agents/bdi_agents/bdi_belief_collector_agent/behaviours/test_continuous_collect.py @@ -3,8 +3,8 @@ from unittest.mock import AsyncMock, MagicMock import pytest -from control_backend.agents.bdi_agents.bdi_belief_collector_agent.behaviours.continuous_collect import ( # noqa: E501 - ContinuousBeliefCollector, +from control_backend.agents.bdi_agents.bdi_belief_collector_agent.behaviours.bel_collector_behaviour import ( # noqa: E501 + BelCollectorBehaviour, ) @@ -25,12 +25,12 @@ def mock_agent(mocker): @pytest.fixture -def continuous_collector(mock_agent, mocker): - """Fixture to create an instance of ContinuousBeliefCollector with a mocked agent.""" +def bel_collector_behaviouror(mock_agent, mocker): + """Fixture to create an instance of BelCollectorBehaviour with a mocked agent.""" # Patch asyncio.sleep to prevent tests from actually waiting mocker.patch("asyncio.sleep", return_value=None) - collector = ContinuousBeliefCollector() + collector = BelCollectorBehaviour() collector.agent = mock_agent # Mock the receive method, we will control its return value in each test collector.receive = AsyncMock() @@ -38,64 +38,64 @@ def continuous_collector(mock_agent, mocker): @pytest.mark.asyncio -async def test_run_message_received(continuous_collector, mocker): +async def test_run_message_received(bel_collector_behaviouror, mocker): """ Test that when a message is received, _process_message is called with that message. """ # Arrange mock_msg = MagicMock() - continuous_collector.receive.return_value = mock_msg - mocker.patch.object(continuous_collector, "_process_message") + bel_collector_behaviouror.receive.return_value = mock_msg + mocker.patch.object(bel_collector_behaviouror, "_process_message") # Act - await continuous_collector.run() + await bel_collector_behaviouror.run() # Assert - continuous_collector._process_message.assert_awaited_once_with(mock_msg) + bel_collector_behaviouror._process_message.assert_awaited_once_with(mock_msg) @pytest.mark.asyncio -async def test_routes_to_handle_belief_text_by_type(continuous_collector, mocker): +async def test_routes_to_handle_belief_text_by_type(bel_collector_behaviouror, mocker): msg = create_mock_message( "anyone", json.dumps({"type": "belief_extraction_text", "beliefs": {"user_said": [["hi"]]}}), ) - spy = mocker.patch.object(continuous_collector, "_handle_belief_text", new=AsyncMock()) - await continuous_collector._process_message(msg) + spy = mocker.patch.object(bel_collector_behaviouror, "_handle_belief_text", new=AsyncMock()) + await bel_collector_behaviouror._process_message(msg) spy.assert_awaited_once() @pytest.mark.asyncio -async def test_routes_to_handle_belief_text_by_sender(continuous_collector, mocker): +async def test_routes_to_handle_belief_text_by_sender(bel_collector_behaviouror, mocker): msg = create_mock_message( "bel_text_agent_mock", json.dumps({"beliefs": {"user_said": [["hi"]]}}) ) - spy = mocker.patch.object(continuous_collector, "_handle_belief_text", new=AsyncMock()) - await continuous_collector._process_message(msg) + spy = mocker.patch.object(bel_collector_behaviouror, "_handle_belief_text", new=AsyncMock()) + await bel_collector_behaviouror._process_message(msg) spy.assert_awaited_once() @pytest.mark.asyncio -async def test_routes_to_handle_emo_text(continuous_collector, mocker): +async def test_routes_to_handle_emo_text(bel_collector_behaviouror, mocker): msg = create_mock_message("anyone", json.dumps({"type": "emotion_extraction_text"})) - spy = mocker.patch.object(continuous_collector, "_handle_emo_text", new=AsyncMock()) - await continuous_collector._process_message(msg) + spy = mocker.patch.object(bel_collector_behaviouror, "_handle_emo_text", new=AsyncMock()) + await bel_collector_behaviouror._process_message(msg) spy.assert_awaited_once() @pytest.mark.asyncio -async def test_belief_text_happy_path_sends(continuous_collector, mocker): +async def test_belief_text_happy_path_sends(bel_collector_behaviouror, mocker): payload = {"type": "belief_extraction_text", "beliefs": {"user_said": ["hello test", "No"]}} - continuous_collector.send = AsyncMock() - await continuous_collector._handle_belief_text(payload, "bel_text_agent_mock") + bel_collector_behaviouror.send = AsyncMock() + await bel_collector_behaviouror._handle_belief_text(payload, "bel_text_agent_mock") # make sure we attempted a send - continuous_collector.send.assert_awaited_once() + bel_collector_behaviouror.send.assert_awaited_once() @pytest.mark.asyncio -async def test_belief_text_coerces_non_strings(continuous_collector, mocker): +async def test_belief_text_coerces_non_strings(bel_collector_behaviouror, mocker): payload = {"type": "belief_extraction_text", "beliefs": {"user_said": [["hi", 123]]}} - continuous_collector.send = AsyncMock() - await continuous_collector._handle_belief_text(payload, "origin") - continuous_collector.send.assert_awaited_once() + bel_collector_behaviouror.send = AsyncMock() + await bel_collector_behaviouror._handle_belief_text(payload, "origin") + bel_collector_behaviouror.send.assert_awaited_once() diff --git a/test/unit/agents/bdi/behaviours/test_belief_setter.py b/test/unit/agents/bdi_agents/bdi_core_agent/behaviours/test_belief_setter.py similarity index 67% rename from test/unit/agents/bdi/behaviours/test_belief_setter.py rename to test/unit/agents/bdi_agents/bdi_core_agent/behaviours/test_belief_setter.py index 238285a..fa6b1de 100644 --- a/test/unit/agents/bdi/behaviours/test_belief_setter.py +++ b/test/unit/agents/bdi_agents/bdi_core_agent/behaviours/test_belief_setter.py @@ -4,7 +4,7 @@ from unittest.mock import AsyncMock, MagicMock, call import pytest -from control_backend.agents.bdi_agents.bdi_core_agent.behaviours.belief_setter import ( +from control_backend.agents.bdi_agents.bdi_core_agent.behaviours.belief_setter_behaviour import ( BeliefSetterBehaviour, ) @@ -23,12 +23,12 @@ def mock_agent(mocker): @pytest.fixture -def belief_setter(mock_agent, mocker): +def belief_setter_behaviour(mock_agent, mocker): """Fixture to create an instance of BeliefSetterBehaviour with a mocked agent.""" # Patch the settings to use a predictable agent name mocker.patch( "control_backend.agents.bdi_agents.bdi_core_agent." - "behaviours.belief_setter.settings.agent_settings.bdi_belief_collector_agent_name", + "behaviours.belief_setter_behaviour.settings.agent_settings.bdi_belief_collector_agent_name", COLLECTOR_AGENT_NAME, ) @@ -49,53 +49,53 @@ def create_mock_message(sender_node: str, body: str, thread: str) -> MagicMock: @pytest.mark.asyncio -async def test_run_message_received(belief_setter, mocker): +async def test_run_message_received(belief_setter_behaviour, mocker): """ Test that when a message is received, _process_message is called. """ # Arrange msg = MagicMock() - belief_setter.receive.return_value = msg - mocker.patch.object(belief_setter, "_process_message") + belief_setter_behaviour.receive.return_value = msg + mocker.patch.object(belief_setter_behaviour, "_process_message") # Act - await belief_setter.run() + await belief_setter_behaviour.run() # Assert - belief_setter._process_message.assert_called_once_with(msg) + belief_setter_behaviour._process_message.assert_called_once_with(msg) -def test_process_message_from_bdi_belief_collector_agent(belief_setter, mocker): +def test_process_message_from_bdi_belief_collector_agent(belief_setter_behaviour, mocker): """ Test processing a message from the correct belief collector agent. """ # Arrange msg = create_mock_message(sender_node=COLLECTOR_AGENT_NAME, body="", thread="") - mock_process_belief = mocker.patch.object(belief_setter, "_process_belief_message") + mock_process_belief = mocker.patch.object(belief_setter_behaviour, "_process_belief_message") # Act - belief_setter._process_message(msg) + belief_setter_behaviour._process_message(msg) # Assert mock_process_belief.assert_called_once_with(msg) -def test_process_message_from_other_agent(belief_setter, mocker): +def test_process_message_from_other_agent(belief_setter_behaviour, mocker): """ Test that messages from other agents are ignored. """ # Arrange msg = create_mock_message(sender_node="other_agent", body="", thread="") - mock_process_belief = mocker.patch.object(belief_setter, "_process_belief_message") + mock_process_belief = mocker.patch.object(belief_setter_behaviour, "_process_belief_message") # Act - belief_setter._process_message(msg) + belief_setter_behaviour._process_message(msg) # Assert mock_process_belief.assert_not_called() -def test_process_belief_message_valid_json(belief_setter, mocker): +def test_process_belief_message_valid_json(belief_setter_behaviour, mocker): """ Test processing a valid belief message with correct thread and JSON body. """ @@ -104,16 +104,16 @@ def test_process_belief_message_valid_json(belief_setter, mocker): msg = create_mock_message( sender_node=COLLECTOR_AGENT_JID, body=json.dumps(beliefs_payload), thread="beliefs" ) - mock_set_beliefs = mocker.patch.object(belief_setter, "_set_beliefs") + mock_set_beliefs = mocker.patch.object(belief_setter_behaviour, "_set_beliefs") # Act - belief_setter._process_belief_message(msg) + belief_setter_behaviour._process_belief_message(msg) # Assert mock_set_beliefs.assert_called_once_with(beliefs_payload) -def test_process_belief_message_invalid_json(belief_setter, mocker, caplog): +def test_process_belief_message_invalid_json(belief_setter_behaviour, mocker, caplog): """ Test that a message with invalid JSON is handled gracefully and an error is logged. """ @@ -121,16 +121,16 @@ def test_process_belief_message_invalid_json(belief_setter, mocker, caplog): msg = create_mock_message( sender_node=COLLECTOR_AGENT_JID, body="this is not a json string", thread="beliefs" ) - mock_set_beliefs = mocker.patch.object(belief_setter, "_set_beliefs") + mock_set_beliefs = mocker.patch.object(belief_setter_behaviour, "_set_beliefs") # Act - belief_setter._process_belief_message(msg) + belief_setter_behaviour._process_belief_message(msg) # Assert mock_set_beliefs.assert_not_called() -def test_process_belief_message_wrong_thread(belief_setter, mocker): +def test_process_belief_message_wrong_thread(belief_setter_behaviour, mocker): """ Test that a message with an incorrect thread is ignored. """ @@ -138,31 +138,31 @@ def test_process_belief_message_wrong_thread(belief_setter, mocker): msg = create_mock_message( sender_node=COLLECTOR_AGENT_JID, body='{"some": "data"}', thread="not_beliefs" ) - mock_set_beliefs = mocker.patch.object(belief_setter, "_set_beliefs") + mock_set_beliefs = mocker.patch.object(belief_setter_behaviour, "_set_beliefs") # Act - belief_setter._process_belief_message(msg) + belief_setter_behaviour._process_belief_message(msg) # Assert mock_set_beliefs.assert_not_called() -def test_process_belief_message_empty_body(belief_setter, mocker): +def test_process_belief_message_empty_body(belief_setter_behaviour, mocker): """ Test that a message with an empty body is ignored. """ # Arrange msg = create_mock_message(sender_node=COLLECTOR_AGENT_JID, body="", thread="beliefs") - mock_set_beliefs = mocker.patch.object(belief_setter, "_set_beliefs") + mock_set_beliefs = mocker.patch.object(belief_setter_behaviour, "_set_beliefs") # Act - belief_setter._process_belief_message(msg) + belief_setter_behaviour._process_belief_message(msg) # Assert mock_set_beliefs.assert_not_called() -def test_set_beliefs_success(belief_setter, mock_agent, caplog): +def test_set_beliefs_success(belief_setter_behaviour, mock_agent, caplog): """ Test that beliefs are correctly set on the agent's BDI. """ @@ -174,7 +174,7 @@ def test_set_beliefs_success(belief_setter, mock_agent, caplog): # Act with caplog.at_level(logging.INFO): - belief_setter._set_beliefs(beliefs_to_set) + belief_setter_behaviour._set_beliefs(beliefs_to_set) # Assert expected_calls = [ @@ -185,18 +185,18 @@ def test_set_beliefs_success(belief_setter, mock_agent, caplog): assert mock_agent.bdi.set_belief.call_count == 2 -# def test_responded_unset(belief_setter, mock_agent): +# def test_responded_unset(belief_setter_behaviour, mock_agent): # # Arrange # new_beliefs = {"user_said": ["message"]} # # # Act -# belief_setter._set_beliefs(new_beliefs) +# belief_setter_behaviour._set_beliefs(new_beliefs) # # # Assert # mock_agent.bdi.set_belief.assert_has_calls([call("user_said", "message")]) # mock_agent.bdi.remove_belief.assert_has_calls([call("responded")]) -# def test_set_beliefs_bdi_not_initialized(belief_setter, mock_agent, caplog): +# def test_set_beliefs_bdi_not_initialized(belief_setter_behaviour, mock_agent, caplog): # """ # Test that a warning is logged if the agent's BDI is not initialized. # """ @@ -206,7 +206,7 @@ def test_set_beliefs_success(belief_setter, mock_agent, caplog): # # # Act # with caplog.at_level(logging.WARNING): -# belief_setter._set_beliefs(beliefs_to_set) +# belief_setter_behaviour._set_beliefs(beliefs_to_set) # # # Assert # assert "Cannot set beliefs, since agent's BDI is not yet initialized." in caplog.text diff --git a/test/unit/agents/belief_from_text/behaviours/test_belief_from_text.py b/test/unit/agents/bdi_agents/bdi_text_belief_agent/behaviours/test_belief_from_text.py similarity index 95% rename from test/unit/agents/belief_from_text/behaviours/test_belief_from_text.py rename to test/unit/agents/bdi_agents/bdi_text_belief_agent/behaviours/test_belief_from_text.py index cd2fda1..9c0021b 100644 --- a/test/unit/agents/belief_from_text/behaviours/test_belief_from_text.py +++ b/test/unit/agents/bdi_agents/bdi_text_belief_agent/behaviours/test_belief_from_text.py @@ -4,8 +4,8 @@ from unittest.mock import AsyncMock, MagicMock, patch import pytest from spade.message import Message -from control_backend.agents.bdi_agents.bdi_text_belief_agent.behaviours.text_belief_extractor import ( # noqa: E501, We can't shorten this import. - BeliefFromText, +from control_backend.agents.bdi_agents.bdi_text_belief_agent.behaviours.bdi_text_belief_behaviour import ( # noqa: E501, We can't shorten this import. + BDITextBeliefBehaviour, ) @@ -25,7 +25,7 @@ def mock_settings(): # Adjust 'control_backend.behaviours.belief_from_text.settings' to where # your behaviour file imports it from. with patch( - "control_backend.agents.bdi_agents.bdi_text_belief_agent.behaviours.text_belief_extractor.settings", + "control_backend.agents.bdi_agents.bdi_text_belief_agent.behaviours.bdi_text_belief_behaviour.settings", settings_mock, ): yield settings_mock @@ -34,10 +34,10 @@ def mock_settings(): @pytest.fixture def behavior(mock_settings): """ - Creates an instance of the BeliefFromText behaviour and mocks its + Creates an instance of the BDITextBeliefBehaviour behaviour and mocks its agent, logger, send, and receive methods. """ - b = BeliefFromText() + b = BDITextBeliefBehaviour() b.agent = MagicMock() b.send = AsyncMock() diff --git a/test/unit/agents/transcription/test_speech_recognizer.py b/test/unit/agents/per_agents/per_transcription_agent/test_speech_recognizer.py similarity index 100% rename from test/unit/agents/transcription/test_speech_recognizer.py rename to test/unit/agents/per_agents/per_transcription_agent/test_speech_recognizer.py diff --git a/test/unit/agents/test_vad_socket_poller.py b/test/unit/agents/per_agents/per_vad_agent/test_vad_socket_poller.py similarity index 100% rename from test/unit/agents/test_vad_socket_poller.py rename to test/unit/agents/per_agents/per_vad_agent/test_vad_socket_poller.py diff --git a/test/unit/agents/test_vad_streaming.py b/test/unit/agents/per_agents/per_vad_agent/test_vad_streaming.py similarity index 100% rename from test/unit/agents/test_vad_streaming.py rename to test/unit/agents/per_agents/per_vad_agent/test_vad_streaming.py From 9152985bdb7e8742eecc91e584b7e94643e17373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Otgaar?= Date: Wed, 12 Nov 2025 12:46:47 +0100 Subject: [PATCH 18/26] fix: send correct asl path to BDI agent ref: N25B-257 --- src/control_backend/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/control_backend/main.py b/src/control_backend/main.py index 5e8bcaa..62d6718 100644 --- a/src/control_backend/main.py +++ b/src/control_backend/main.py @@ -102,7 +102,7 @@ async def lifespan(app: FastAPI): "jid": f"{settings.agent_settings.bdi_core_agent_agent_name}@" f"{settings.agent_settings.host}", "password": settings.agent_settings.bdi_core_agent_agent_name, - "asl": "src/control_backend/agents/bdi/rules.asl", + "asl": "src/control_backend/agents/bdi_agents/bdi_core_agent/rules.asl", }, ), "BDIBeliefCollectorAgent": ( From 43f3cba1a8da6e007ce1dd1c98fc98c9e4a588fd Mon Sep 17 00:00:00 2001 From: JobvAlewijk Date: Wed, 12 Nov 2025 13:18:56 +0100 Subject: [PATCH 19/26] feat: ui program to cb connection ref: N25B-198 --- .../api/v1/endpoints/program.py | 52 +++++++++++++++++++ src/control_backend/api/v1/router.py | 4 +- src/control_backend/schemas/program.py | 38 ++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 src/control_backend/api/v1/endpoints/program.py create mode 100644 src/control_backend/schemas/program.py diff --git a/src/control_backend/api/v1/endpoints/program.py b/src/control_backend/api/v1/endpoints/program.py new file mode 100644 index 0000000..fe0fbac --- /dev/null +++ b/src/control_backend/api/v1/endpoints/program.py @@ -0,0 +1,52 @@ +import json +import logging + +from fastapi import APIRouter, HTTPException, Request + +from control_backend.schemas.message import Message +from control_backend.schemas.program import Phase + +logger = logging.getLogger(__name__) +router = APIRouter() + + +@router.post("/program", status_code=202) +async def receive_message(program: Message, request: Request): + """ + Receives a BehaviorProgram as a stringified JSON list inside `message`. + Converts it into real Phase objects. + """ + logger.info("Received raw program: ") + logger.debug("%s", program) + raw_str = program.message # This is the JSON string + + # Convert Json into dict. + try: + program_list = json.loads(raw_str) + except json.JSONDecodeError as e: + logger.error("Failed to decode program JSON: %s", e) + raise HTTPException(status_code=400, detail="Undecodeable Json string") from None + + # Validate Phases + try: + phases: list[Phase] = [Phase(**phase) for phase in program_list] + except Exception as e: + logger.error("❌ Failed to convert to Phase objects: %s", e) + raise HTTPException(status_code=400, detail="Non-Phase String") from None + + logger.info(f"Succesfully recieved {len(phases)} Phase(s).") + for p in phases: + logger.info( + f"Phase {p.id}: " + f"{len(p.phaseData.norms)} norms, " + f"{len(p.phaseData.goals)} goals, " + f"{len(p.phaseData.triggers) if hasattr(p.phaseData, 'triggers') else 0} triggers" + ) + + # send away + topic = b"program" + body = json.dumps([p.model_dump() for p in phases]).encode("utf-8") + pub_socket = request.app.state.endpoints_pub_socket + await pub_socket.send_multipart([topic, body]) + + return {"status": "Program parsed", "phase_count": len(phases)} diff --git a/src/control_backend/api/v1/router.py b/src/control_backend/api/v1/router.py index f11dc9c..809b412 100644 --- a/src/control_backend/api/v1/router.py +++ b/src/control_backend/api/v1/router.py @@ -1,6 +1,6 @@ from fastapi.routing import APIRouter -from control_backend.api.v1.endpoints import command, logs, message, sse +from control_backend.api.v1.endpoints import command, logs, message, program, sse api_router = APIRouter() @@ -11,3 +11,5 @@ api_router.include_router(sse.router, tags=["SSE"]) api_router.include_router(command.router, tags=["Commands"]) api_router.include_router(logs.router, tags=["Logs"]) + +api_router.include_router(program.router, tags=["Program"]) diff --git a/src/control_backend/schemas/program.py b/src/control_backend/schemas/program.py new file mode 100644 index 0000000..c207757 --- /dev/null +++ b/src/control_backend/schemas/program.py @@ -0,0 +1,38 @@ +from pydantic import BaseModel + + +class Norm(BaseModel): + id: str + name: str + value: str + + +class Goal(BaseModel): + id: str + name: str + description: str + achieved: bool + + +class Trigger(BaseModel): + id: str + label: str + type: str + value: list[str] + + +class PhaseData(BaseModel): + norms: list[Norm] + goals: list[Goal] + triggers: list[Trigger] + + +class Phase(BaseModel): + id: str + name: str + nextPhaseId: str + phaseData: PhaseData + + +class Program(BaseModel): + phases: list[Phase] From 79d3bfb3a6567117e3e9464a4b4d36cc45390571 Mon Sep 17 00:00:00 2001 From: JobvAlewijk Date: Wed, 12 Nov 2025 17:36:00 +0100 Subject: [PATCH 20/26] test: added tests for programs and its scheme ref: N25B-198 --- .../api/v1/endpoints/program.py | 2 +- .../api/endpoints/test_program_endpoint.py | 91 +++++++++++++++++++ .../schemas/test_ui_program_message.py | 85 +++++++++++++++++ 3 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 test/integration/api/endpoints/test_program_endpoint.py create mode 100644 test/integration/schemas/test_ui_program_message.py diff --git a/src/control_backend/api/v1/endpoints/program.py b/src/control_backend/api/v1/endpoints/program.py index fe0fbac..b711a58 100644 --- a/src/control_backend/api/v1/endpoints/program.py +++ b/src/control_backend/api/v1/endpoints/program.py @@ -31,7 +31,7 @@ async def receive_message(program: Message, request: Request): try: phases: list[Phase] = [Phase(**phase) for phase in program_list] except Exception as e: - logger.error("❌ Failed to convert to Phase objects: %s", e) + logger.error("Failed to convert to Phase objects: %s", e) raise HTTPException(status_code=400, detail="Non-Phase String") from None logger.info(f"Succesfully recieved {len(phases)} Phase(s).") diff --git a/test/integration/api/endpoints/test_program_endpoint.py b/test/integration/api/endpoints/test_program_endpoint.py new file mode 100644 index 0000000..689961f --- /dev/null +++ b/test/integration/api/endpoints/test_program_endpoint.py @@ -0,0 +1,91 @@ +import json +from unittest.mock import AsyncMock + +import pytest +from fastapi import FastAPI +from fastapi.testclient import TestClient + +from control_backend.api.v1.endpoints import program # <-- import your router +from control_backend.schemas.message import Message + + +@pytest.fixture +def app(): + """Create a FastAPI app with the /program route and mock socket.""" + app = FastAPI() + app.include_router(program.router) + return app + + +@pytest.fixture +def client(app): + """Create a TestClient.""" + return TestClient(app) + + +def make_valid_phase_dict(): + """Helper to create a valid Phase JSON structure.""" + return { + "id": "phase1", + "name": "basephase", + "nextPhaseId": "phase2", + "phaseData": { + "norms": [{"id": "n1", "name": "norm", "value": "be nice"}], + "goals": [{"id": "g1", "name": "goal", "description": "test goal", "achieved": False}], + "triggers": [ + {"id": "t1", "label": "trigger", "type": "keyword", "value": ["stop", "exit"]} + ], + }, + } + + +def test_receive_program_success(client): + """Valid program JSON string should parse and be sent via the socket.""" + # Arrange + mock_pub_socket = AsyncMock() + client.app.state.endpoints_pub_socket = mock_pub_socket + + phases_list = [make_valid_phase_dict()] + message_body = json.dumps(phases_list) + msg = Message(message=message_body) + + # Act + response = client.post("/program", json=msg.model_dump()) + + # Assert + assert response.status_code == 202 + assert response.json() == {"status": "Program parsed", "phase_count": 1} + + # Check the mocked socket + expected_body = json.dumps(phases_list).encode("utf-8") + mock_pub_socket.send_multipart.assert_awaited_once_with([b"program", expected_body]) + + +def test_receive_program_invalid_json(client): + """Malformed JSON string should return 400 with 'Undecodeable Json string'.""" + mock_pub_socket = AsyncMock() + client.app.state.endpoints_pub_socket = mock_pub_socket + + # Not valid JSON + bad_message = Message(message="{not valid json}") + response = client.post("/program", json=bad_message.model_dump()) + + assert response.status_code == 400 + assert response.json()["detail"] == "Undecodeable Json string" + mock_pub_socket.send_multipart.assert_not_called() + + +def test_receive_program_invalid_phase(client): + """Decodable JSON but invalid Phase structure should return 400 with 'Non-Phase String'.""" + mock_pub_socket = AsyncMock() + client.app.state.endpoints_pub_socket = mock_pub_socket + + # Missing required Phase fields + invalid_phase = [{"id": "only_id"}] + bad_message = Message(message=json.dumps(invalid_phase)) + + response = client.post("/program", json=bad_message.model_dump()) + + assert response.status_code == 400 + assert response.json()["detail"] == "Non-Phase String" + mock_pub_socket.send_multipart.assert_not_called() diff --git a/test/integration/schemas/test_ui_program_message.py b/test/integration/schemas/test_ui_program_message.py new file mode 100644 index 0000000..36352d6 --- /dev/null +++ b/test/integration/schemas/test_ui_program_message.py @@ -0,0 +1,85 @@ +import pytest +from pydantic import ValidationError + +from control_backend.schemas.program import Goal, Norm, Phase, PhaseData, Program, Trigger + + +def base_norm() -> Norm: + return Norm( + id="norm1", + name="testNorm", + value="you should act nice", + ) + + +def base_goal() -> Goal: + return Goal( + id="goal1", + name="testGoal", + description="you should act nice", + achieved=False, + ) + + +def base_trigger() -> Trigger: + return Trigger( + id="trigger1", + label="testTrigger", + type="keyword", + value=["Stop", "Exit"], + ) + + +def base_phase_data() -> PhaseData: + return PhaseData( + norms=[base_norm()], + goals=[base_goal()], + triggers=[base_trigger()], + ) + + +def base_phase() -> Phase: + return Phase( + id="phase1", + name="basephase", + nextPhaseId="phase2", + phaseData=base_phase_data(), + ) + + +def base_program() -> Program: + return Program(phases=[base_phase()]) + + +def invalid_program() -> dict: + # wrong types inside phases list (not Phase objects) + return { + "phases": [ + {"id": "phase1"}, # incomplete + {"not_a_phase": True}, + ] + } + + +def test_valid_program(): + program = base_program() + validated = Program.model_validate(program) + assert isinstance(validated, Program) + assert validated.phases[0].phaseData.norms[0].name == "testNorm" + + +def test_valid_deepprogram(): + program = base_program() + validated = Program.model_validate(program) + # validate nested components directly + phase = validated.phases[0] + assert isinstance(phase.phaseData, PhaseData) + assert isinstance(phase.phaseData.goals[0], Goal) + assert isinstance(phase.phaseData.triggers[0], Trigger) + assert isinstance(phase.phaseData.norms[0], Norm) + + +def test_invalid_program(): + bad = invalid_program() + with pytest.raises(ValidationError): + Program.model_validate(bad) From 2ed2a84f130017af23882423f279d6b6486cf93d Mon Sep 17 00:00:00 2001 From: JobvAlewijk Date: Wed, 12 Nov 2025 18:04:39 +0100 Subject: [PATCH 21/26] style: compacted program and reworked tests ref: N25B-198 --- .../api/v1/endpoints/program.py | 37 ++---- .../api/endpoints/test_program_endpoint.py | 106 ++++++++++++------ 2 files changed, 83 insertions(+), 60 deletions(-) diff --git a/src/control_backend/api/v1/endpoints/program.py b/src/control_backend/api/v1/endpoints/program.py index b711a58..e9812ea 100644 --- a/src/control_backend/api/v1/endpoints/program.py +++ b/src/control_backend/api/v1/endpoints/program.py @@ -1,10 +1,10 @@ -import json import logging from fastapi import APIRouter, HTTPException, Request +from pydantic import ValidationError from control_backend.schemas.message import Message -from control_backend.schemas.program import Phase +from control_backend.schemas.program import Program logger = logging.getLogger(__name__) router = APIRouter() @@ -16,37 +16,20 @@ async def receive_message(program: Message, request: Request): Receives a BehaviorProgram as a stringified JSON list inside `message`. Converts it into real Phase objects. """ - logger.info("Received raw program: ") - logger.debug("%s", program) + logger.debug("Received raw program: %s", program) raw_str = program.message # This is the JSON string - # Convert Json into dict. + # Validate program try: - program_list = json.loads(raw_str) - except json.JSONDecodeError as e: - logger.error("Failed to decode program JSON: %s", e) - raise HTTPException(status_code=400, detail="Undecodeable Json string") from None - - # Validate Phases - try: - phases: list[Phase] = [Phase(**phase) for phase in program_list] - except Exception as e: - logger.error("Failed to convert to Phase objects: %s", e) - raise HTTPException(status_code=400, detail="Non-Phase String") from None - - logger.info(f"Succesfully recieved {len(phases)} Phase(s).") - for p in phases: - logger.info( - f"Phase {p.id}: " - f"{len(p.phaseData.norms)} norms, " - f"{len(p.phaseData.goals)} goals, " - f"{len(p.phaseData.triggers) if hasattr(p.phaseData, 'triggers') else 0} triggers" - ) + program = Program.model_validate_json(raw_str) + except ValidationError as e: + logger.error("Failed to validate program JSON: %s", e) + raise HTTPException(status_code=400, detail="Not a valid program") from None # send away topic = b"program" - body = json.dumps([p.model_dump() for p in phases]).encode("utf-8") + body = program.model_dump_json().encode() pub_socket = request.app.state.endpoints_pub_socket await pub_socket.send_multipart([topic, body]) - return {"status": "Program parsed", "phase_count": len(phases)} + return {"status": "Program parsed"} diff --git a/test/integration/api/endpoints/test_program_endpoint.py b/test/integration/api/endpoints/test_program_endpoint.py index 689961f..05ce63c 100644 --- a/test/integration/api/endpoints/test_program_endpoint.py +++ b/test/integration/api/endpoints/test_program_endpoint.py @@ -5,8 +5,9 @@ import pytest from fastapi import FastAPI from fastapi.testclient import TestClient -from control_backend.api.v1.endpoints import program # <-- import your router +from control_backend.api.v1.endpoints import program from control_backend.schemas.message import Message +from control_backend.schemas.program import Program @pytest.fixture @@ -23,30 +24,40 @@ def client(app): return TestClient(app) -def make_valid_phase_dict(): - """Helper to create a valid Phase JSON structure.""" +def make_valid_program_dict(): + """Helper to create a valid Program JSON structure.""" return { - "id": "phase1", - "name": "basephase", - "nextPhaseId": "phase2", - "phaseData": { - "norms": [{"id": "n1", "name": "norm", "value": "be nice"}], - "goals": [{"id": "g1", "name": "goal", "description": "test goal", "achieved": False}], - "triggers": [ - {"id": "t1", "label": "trigger", "type": "keyword", "value": ["stop", "exit"]} - ], - }, + "phases": [ + { + "id": "phase1", + "name": "basephase", + "nextPhaseId": "phase2", + "phaseData": { + "norms": [{"id": "n1", "name": "norm", "value": "be nice"}], + "goals": [ + {"id": "g1", "name": "goal", "description": "test goal", "achieved": False} + ], + "triggers": [ + { + "id": "t1", + "label": "trigger", + "type": "keyword", + "value": ["stop", "exit"], + } + ], + }, + } + ] } def test_receive_program_success(client): - """Valid program JSON string should parse and be sent via the socket.""" - # Arrange + """Valid Program JSON should be parsed and sent through the socket.""" mock_pub_socket = AsyncMock() client.app.state.endpoints_pub_socket = mock_pub_socket - phases_list = [make_valid_phase_dict()] - message_body = json.dumps(phases_list) + program_dict = make_valid_program_dict() + message_body = json.dumps(program_dict) msg = Message(message=message_body) # Act @@ -54,38 +65,67 @@ def test_receive_program_success(client): # Assert assert response.status_code == 202 - assert response.json() == {"status": "Program parsed", "phase_count": 1} + assert response.json() == {"status": "Program parsed"} - # Check the mocked socket - expected_body = json.dumps(phases_list).encode("utf-8") - mock_pub_socket.send_multipart.assert_awaited_once_with([b"program", expected_body]) + # Verify socket call (don't compare raw JSON string) + mock_pub_socket.send_multipart.assert_awaited_once() + args, kwargs = mock_pub_socket.send_multipart.await_args + + assert args[0][0] == b"program" + sent_bytes = args[0][1] + + # Decode sent bytes and compare actual structures + sent_obj = json.loads(sent_bytes.decode()) + expected_obj = Program.model_validate_json(message_body).model_dump() + + assert sent_obj == expected_obj def test_receive_program_invalid_json(client): - """Malformed JSON string should return 400 with 'Undecodeable Json string'.""" + """Invalid JSON string (not parseable) should trigger HTTP 400.""" mock_pub_socket = AsyncMock() client.app.state.endpoints_pub_socket = mock_pub_socket - # Not valid JSON - bad_message = Message(message="{not valid json}") - response = client.post("/program", json=bad_message.model_dump()) + bad_json_str = "{invalid json}" + msg = Message(message=bad_json_str) + + response = client.post("/program", json=msg.model_dump()) assert response.status_code == 400 - assert response.json()["detail"] == "Undecodeable Json string" + assert response.json()["detail"] == "Not a valid program" mock_pub_socket.send_multipart.assert_not_called() -def test_receive_program_invalid_phase(client): - """Decodable JSON but invalid Phase structure should return 400 with 'Non-Phase String'.""" +def test_receive_program_invalid_deep_structure(client): + """Valid JSON shape but invalid deep nested data should still raise 400.""" mock_pub_socket = AsyncMock() client.app.state.endpoints_pub_socket = mock_pub_socket - # Missing required Phase fields - invalid_phase = [{"id": "only_id"}] - bad_message = Message(message=json.dumps(invalid_phase)) + # Structurally correct Program, but with missing elements + bad_program = { + "phases": [ + { + "id": "phase1", + "name": "deepfail", + "nextPhaseId": "phase2", + "phaseData": { + "norms": [ + {"id": "n1", "name": "norm"} # Missing "value" + ], + "goals": [ + {"id": "g1", "name": "goal", "description": "desc", "achieved": False} + ], + "triggers": [ + {"id": "t1", "label": "trigger", "type": "keyword", "value": ["start"]} + ], + }, + } + ] + } - response = client.post("/program", json=bad_message.model_dump()) + msg = Message(message=json.dumps(bad_program)) + response = client.post("/program", json=msg.model_dump()) assert response.status_code == 400 - assert response.json()["detail"] == "Non-Phase String" + assert response.json()["detail"] == "Not a valid program" mock_pub_socket.send_multipart.assert_not_called() From 41993a902b19dea4e899f6b6194f31b4cf02d4f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Otgaar?= Date: Mon, 17 Nov 2025 16:04:54 +0100 Subject: [PATCH 22/26] chore: remove caplog from test cases --- .../agents/test_ri_commands_agent.py | 6 ++---- .../agents/test_ri_communication_agent.py | 18 +++++++++--------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/test/integration/agents/test_ri_commands_agent.py b/test/integration/agents/test_ri_commands_agent.py index ea9fca9..477ab78 100644 --- a/test/integration/agents/test_ri_commands_agent.py +++ b/test/integration/agents/test_ri_commands_agent.py @@ -73,7 +73,7 @@ async def test_send_commands_behaviour_valid_message(): @pytest.mark.asyncio -async def test_send_commands_behaviour_invalid_message(caplog): +async def test_send_commands_behaviour_invalid_message(): """Test behaviour with invalid JSON message triggers error logging""" fake_socket = AsyncMock() fake_socket.recv_multipart = AsyncMock(return_value=(b"command", b"{invalid_json}")) @@ -86,9 +86,7 @@ async def test_send_commands_behaviour_invalid_message(caplog): behaviour = agent.SendCommandsBehaviour() behaviour.agent = agent - with caplog.at_level("ERROR"): - await behaviour.run() + await behaviour.run() fake_socket.recv_multipart.assert_awaited() fake_socket.send_json.assert_not_awaited() - assert "Error processing message" in caplog.text diff --git a/test/integration/agents/test_ri_communication_agent.py b/test/integration/agents/test_ri_communication_agent.py index 1925afa..6e29340 100644 --- a/test/integration/agents/test_ri_communication_agent.py +++ b/test/integration/agents/test_ri_communication_agent.py @@ -176,7 +176,7 @@ async def test_setup_creates_socket_and_negotiate_2(zmq_context): @pytest.mark.asyncio -async def test_setup_creates_socket_and_negotiate_3(zmq_context, caplog): +async def test_setup_creates_socket_and_negotiate_3(zmq_context): """ Test the functionality of setup with incorrect negotiation message """ @@ -340,7 +340,7 @@ async def test_setup_creates_socket_and_negotiate_6(zmq_context): @pytest.mark.asyncio -async def test_setup_creates_socket_and_negotiate_7(zmq_context, caplog): +async def test_setup_creates_socket_and_negotiate_7(zmq_context): """ Test the functionality of setup with incorrect id """ @@ -379,7 +379,7 @@ async def test_setup_creates_socket_and_negotiate_7(zmq_context, caplog): @pytest.mark.asyncio -async def test_setup_creates_socket_and_negotiate_timeout(zmq_context, caplog): +async def test_setup_creates_socket_and_negotiate_timeout(zmq_context): """ Test the functionality of setup with incorrect negotiation message """ @@ -416,7 +416,7 @@ async def test_setup_creates_socket_and_negotiate_timeout(zmq_context, caplog): @pytest.mark.asyncio -async def test_listen_behaviour_ping_correct(caplog): +async def test_listen_behaviour_ping_correct(): fake_socket = AsyncMock() fake_socket.send_json = AsyncMock() fake_socket.recv_json = AsyncMock(return_value={"endpoint": "ping", "data": {}}) @@ -436,7 +436,7 @@ async def test_listen_behaviour_ping_correct(caplog): @pytest.mark.asyncio -async def test_listen_behaviour_ping_wrong_endpoint(caplog): +async def test_listen_behaviour_ping_wrong_endpoint(): """ Test if our listen behaviour can work with wrong messages (wrong endpoint) """ @@ -471,7 +471,7 @@ async def test_listen_behaviour_ping_wrong_endpoint(caplog): @pytest.mark.asyncio -async def test_listen_behaviour_timeout(zmq_context, caplog): +async def test_listen_behaviour_timeout(zmq_context): fake_socket = zmq_context.return_value.socket.return_value fake_socket.send_json = AsyncMock() # recv_json will never resolve, simulate timeout @@ -491,7 +491,7 @@ async def test_listen_behaviour_timeout(zmq_context, caplog): @pytest.mark.asyncio -async def test_listen_behaviour_ping_no_endpoint(caplog): +async def test_listen_behaviour_ping_no_endpoint(): """ Test if our listen behaviour can work with wrong messages (wrong endpoint) """ @@ -520,7 +520,7 @@ async def test_listen_behaviour_ping_no_endpoint(caplog): @pytest.mark.asyncio -async def test_setup_unexpected_exception(zmq_context, caplog): +async def test_setup_unexpected_exception(zmq_context): fake_socket = zmq_context.return_value.socket.return_value fake_socket.send_json = AsyncMock() # Simulate unexpected exception during recv_json() @@ -541,7 +541,7 @@ async def test_setup_unexpected_exception(zmq_context, caplog): @pytest.mark.asyncio -async def test_setup_unpacking_exception(zmq_context, caplog): +async def test_setup_unpacking_exception(zmq_context): # --- Arrange --- fake_socket = zmq_context.return_value.socket.return_value fake_socket.send_json = AsyncMock() From 2eefcc4553a73a5c8cd8c6772441d13e73571488 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Otgaar?= Date: Mon, 17 Nov 2025 16:09:02 +0100 Subject: [PATCH 23/26] chore: fix error messages to be warnings. --- src/control_backend/agents/ri_communication_agent.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/control_backend/agents/ri_communication_agent.py b/src/control_backend/agents/ri_communication_agent.py index b489338..93fbf6c 100644 --- a/src/control_backend/agents/ri_communication_agent.py +++ b/src/control_backend/agents/ri_communication_agent.py @@ -77,7 +77,7 @@ class RICommunicationAgent(BaseAgent): topic = b"ping" data = json.dumps(False).encode() if self.agent.pub_socket is None: - self.agent.logger.error( + self.agent.logger.warning( "Communication agent pub socket not correctly initialized." ) else: @@ -227,7 +227,7 @@ class RICommunicationAgent(BaseAgent): break else: - self.logger.error("Failed to set up %s after %d retries", self.name, max_retries) + self.logger.warning("Failed to set up %s after %d retries", self.name, max_retries) return # Set up ping behaviour @@ -238,7 +238,7 @@ class RICommunicationAgent(BaseAgent): topic = b"ping" data = json.dumps(True).encode() if self.pub_socket is None: - self.logger.error("Communication agent pub socket not correctly initialized.") + self.logger.warning("Communication agent pub socket not correctly initialized.") else: try: await asyncio.wait_for(self.pub_socket.send_multipart([topic, data]), 5) From 39c07dd3cf7d08ffb2bd69d8f5e91ac3a2674e6e Mon Sep 17 00:00:00 2001 From: JobvAlewijk Date: Tue, 18 Nov 2025 12:35:44 +0100 Subject: [PATCH 24/26] refactor: made pydantic check the input. no longer by the code itself. ref: N25B-198 --- .../api/v1/endpoints/program.py | 16 ++----- .../api/endpoints/test_program_endpoint.py | 42 ++++++++----------- 2 files changed, 21 insertions(+), 37 deletions(-) diff --git a/src/control_backend/api/v1/endpoints/program.py b/src/control_backend/api/v1/endpoints/program.py index e9812ea..a0679d0 100644 --- a/src/control_backend/api/v1/endpoints/program.py +++ b/src/control_backend/api/v1/endpoints/program.py @@ -1,9 +1,7 @@ import logging -from fastapi import APIRouter, HTTPException, Request -from pydantic import ValidationError +from fastapi import APIRouter, Request -from control_backend.schemas.message import Message from control_backend.schemas.program import Program logger = logging.getLogger(__name__) @@ -11,20 +9,12 @@ router = APIRouter() @router.post("/program", status_code=202) -async def receive_message(program: Message, request: Request): +async def receive_message(program: Program, request: Request): """ - Receives a BehaviorProgram as a stringified JSON list inside `message`. + Receives a BehaviorProgram, pydantic checks it. Converts it into real Phase objects. """ logger.debug("Received raw program: %s", program) - raw_str = program.message # This is the JSON string - - # Validate program - try: - program = Program.model_validate_json(raw_str) - except ValidationError as e: - logger.error("Failed to validate program JSON: %s", e) - raise HTTPException(status_code=400, detail="Not a valid program") from None # send away topic = b"program" diff --git a/test/integration/api/endpoints/test_program_endpoint.py b/test/integration/api/endpoints/test_program_endpoint.py index 05ce63c..f6bb261 100644 --- a/test/integration/api/endpoints/test_program_endpoint.py +++ b/test/integration/api/endpoints/test_program_endpoint.py @@ -6,7 +6,6 @@ from fastapi import FastAPI from fastapi.testclient import TestClient from control_backend.api.v1.endpoints import program -from control_backend.schemas.message import Message from control_backend.schemas.program import Program @@ -57,51 +56,48 @@ def test_receive_program_success(client): client.app.state.endpoints_pub_socket = mock_pub_socket program_dict = make_valid_program_dict() - message_body = json.dumps(program_dict) - msg = Message(message=message_body) - # Act - response = client.post("/program", json=msg.model_dump()) + response = client.post("/program", json=program_dict) - # Assert assert response.status_code == 202 assert response.json() == {"status": "Program parsed"} - # Verify socket call (don't compare raw JSON string) + # Verify socket call mock_pub_socket.send_multipart.assert_awaited_once() args, kwargs = mock_pub_socket.send_multipart.await_args assert args[0][0] == b"program" + sent_bytes = args[0][1] - - # Decode sent bytes and compare actual structures sent_obj = json.loads(sent_bytes.decode()) - expected_obj = Program.model_validate_json(message_body).model_dump() + expected_obj = Program.model_validate(program_dict).model_dump() assert sent_obj == expected_obj def test_receive_program_invalid_json(client): - """Invalid JSON string (not parseable) should trigger HTTP 400.""" + """ + Invalid JSON (malformed) -> FastAPI never calls endpoint. + It returns a 422 Unprocessable Entity. + """ mock_pub_socket = AsyncMock() client.app.state.endpoints_pub_socket = mock_pub_socket - bad_json_str = "{invalid json}" - msg = Message(message=bad_json_str) + # FastAPI only accepts valid JSON bodies, so send raw string + response = client.post("/program", content="{invalid json}") - response = client.post("/program", json=msg.model_dump()) - - assert response.status_code == 400 - assert response.json()["detail"] == "Not a valid program" + assert response.status_code == 422 mock_pub_socket.send_multipart.assert_not_called() def test_receive_program_invalid_deep_structure(client): - """Valid JSON shape but invalid deep nested data should still raise 400.""" + """ + Valid JSON but schema invalid -> Pydantic throws validation error -> 422. + """ mock_pub_socket = AsyncMock() client.app.state.endpoints_pub_socket = mock_pub_socket - # Structurally correct Program, but with missing elements + # Missing "value" in norms element bad_program = { "phases": [ { @@ -110,7 +106,7 @@ def test_receive_program_invalid_deep_structure(client): "nextPhaseId": "phase2", "phaseData": { "norms": [ - {"id": "n1", "name": "norm"} # Missing "value" + {"id": "n1", "name": "norm"} # INVALID: missing "value" ], "goals": [ {"id": "g1", "name": "goal", "description": "desc", "achieved": False} @@ -123,9 +119,7 @@ def test_receive_program_invalid_deep_structure(client): ] } - msg = Message(message=json.dumps(bad_program)) - response = client.post("/program", json=msg.model_dump()) + response = client.post("/program", json=bad_program) - assert response.status_code == 400 - assert response.json()["detail"] == "Not a valid program" + assert response.status_code == 422 mock_pub_socket.send_multipart.assert_not_called() From efe49c219cb7ae2ba75095bc68a42b6e191dccb9 Mon Sep 17 00:00:00 2001 From: Twirre Meulenbelt <43213592+TwirreM@users.noreply.github.com> Date: Wed, 19 Nov 2025 15:56:09 +0100 Subject: [PATCH 25/26] feat: apply new agent naming standards Expanding abbreviations to remove ambiguity, simplifying agent names to reduce repetition. ref: N25B-257 --- .../agents/act_agents/__init__.py | 1 - .../agents/actuation/__init__.py | 1 + .../robot_speech_agent.py} | 4 +- src/control_backend/agents/bdi/__init__.py | 7 ++ .../bdi_core_agent/bdi_core_agent.py | 2 +- .../behaviours/belief_setter_behaviour.py | 2 +- .../behaviours/receive_llm_resp_behaviour.py | 4 +- .../bdi_core_agent/rules.asl | 0 .../behaviours/belief_collector_behaviour.py} | 6 +- .../belief_collector_agent.py} | 4 +- .../text_belief_extractor_behaviour.py} | 10 +- .../text_belief_extractor_agent.py | 8 ++ .../agents/bdi_agents/__init__.py | 5 - .../bdi_text_belief_agent.py | 8 -- .../agents/com_agents/__init__.py | 1 - .../agents/communication/__init__.py | 1 + .../ri_communication_agent.py} | 10 +- .../agents/{llm_agents => llm}/__init__.py | 0 .../agents/{llm_agents => llm}/llm_agent.py | 6 +- .../{llm_agents => llm}/llm_instructions.py | 0 .../agents/mock_agents/bel_text_agent.py | 44 -------- .../agents/per_agents/__init__.py | 4 - .../agents/perception/__init__.py | 4 + .../transcription_agent}/speech_recognizer.py | 0 .../transcription_agent.py} | 14 +-- .../vad_agent.py} | 16 +-- src/control_backend/core/config.py | 18 +-- src/control_backend/main.py | 55 ++++----- .../test_robot_speech_agent.py} | 18 +-- .../test_ri_communication_agent.py} | 104 +++++++++++++----- .../speech_with_pauses_16k_1c_float32.wav | Bin .../vad_agent/test_vad_agent.py} | 22 ++-- .../vad_agent/test_vad_with_audio.py} | 8 +- .../behaviours/test_belief_setter.py | 8 +- .../behaviours/test_continuous_collect.py | 8 +- .../behaviours/test_belief_from_text.py | 19 ++-- .../test_speech_recognizer.py | 2 +- .../vad_agent}/test_vad_socket_poller.py | 10 +- .../vad_agent}/test_vad_streaming.py | 6 +- 39 files changed, 218 insertions(+), 222 deletions(-) delete mode 100644 src/control_backend/agents/act_agents/__init__.py create mode 100644 src/control_backend/agents/actuation/__init__.py rename src/control_backend/agents/{act_agents/act_speech_agent.py => actuation/robot_speech_agent.py} (97%) create mode 100644 src/control_backend/agents/bdi/__init__.py rename src/control_backend/agents/{bdi_agents => bdi}/bdi_core_agent/bdi_core_agent.py (95%) rename src/control_backend/agents/{bdi_agents => bdi}/bdi_core_agent/behaviours/belief_setter_behaviour.py (97%) rename src/control_backend/agents/{bdi_agents => bdi}/bdi_core_agent/behaviours/receive_llm_resp_behaviour.py (89%) rename src/control_backend/agents/{bdi_agents => bdi}/bdi_core_agent/rules.asl (100%) rename src/control_backend/agents/{bdi_agents/bdi_belief_collector_agent/behaviours/bel_collector_behaviour.py => bdi/belief_collector_agent/behaviours/belief_collector_behaviour.py} (94%) rename src/control_backend/agents/{bdi_agents/bdi_belief_collector_agent/bel_collector_agent.py => bdi/belief_collector_agent/belief_collector_agent.py} (72%) rename src/control_backend/agents/{bdi_agents/bdi_text_belief_agent/behaviours/bdi_text_belief_behaviour.py => bdi/text_belief_extractor_agent/behaviours/text_belief_extractor_behaviour.py} (92%) create mode 100644 src/control_backend/agents/bdi/text_belief_extractor_agent/text_belief_extractor_agent.py delete mode 100644 src/control_backend/agents/bdi_agents/__init__.py delete mode 100644 src/control_backend/agents/bdi_agents/bdi_text_belief_agent/bdi_text_belief_agent.py delete mode 100644 src/control_backend/agents/com_agents/__init__.py create mode 100644 src/control_backend/agents/communication/__init__.py rename src/control_backend/agents/{com_agents/com_ri_agent.py => communication/ri_communication_agent.py} (96%) rename src/control_backend/agents/{llm_agents => llm}/__init__.py (100%) rename src/control_backend/agents/{llm_agents => llm}/llm_agent.py (96%) rename src/control_backend/agents/{llm_agents => llm}/llm_instructions.py (100%) delete mode 100644 src/control_backend/agents/mock_agents/bel_text_agent.py delete mode 100644 src/control_backend/agents/per_agents/__init__.py create mode 100644 src/control_backend/agents/perception/__init__.py rename src/control_backend/agents/{per_agents/per_transcription_agent => perception/transcription_agent}/speech_recognizer.py (100%) rename src/control_backend/agents/{per_agents/per_transcription_agent/per_transcription_agent.py => perception/transcription_agent/transcription_agent.py} (88%) rename src/control_backend/agents/{per_agents/per_vad_agent.py => perception/vad_agent.py} (91%) rename test/integration/agents/{act_agents/test_act_speech_agent.py => actuation/test_robot_speech_agent.py} (77%) rename test/integration/agents/{com_agents/test_com_ri_agent.py => communication/test_ri_communication_agent.py} (84%) rename test/integration/agents/{per_agents/per_vad_agent => perception/vad_agent}/speech_with_pauses_16k_1c_float32.wav (100%) rename test/integration/agents/{per_agents/per_vad_agent/test_per_vad_agent.py => perception/vad_agent/test_vad_agent.py} (80%) rename test/integration/agents/{per_agents/per_vad_agent/test_per_vad_with_audio.py => perception/vad_agent/test_vad_with_audio.py} (85%) rename test/unit/agents/{bdi_agents => bdi}/bdi_core_agent/behaviours/test_belief_setter.py (96%) rename test/unit/agents/{bdi_agents/bdi_belief_collector_agent => bdi/belief_collector_agent}/behaviours/test_continuous_collect.py (93%) rename test/unit/agents/{bdi_agents/bdi_text_belief_agent => bdi/text_belief_agent}/behaviours/test_belief_from_text.py (87%) rename test/unit/agents/{per_agents/per_transcription_agent => perception/transcription_agent}/test_speech_recognizer.py (93%) rename test/unit/agents/{per_agents/per_vad_agent => perception/vad_agent}/test_vad_socket_poller.py (76%) rename test/unit/agents/{per_agents/per_vad_agent => perception/vad_agent}/test_vad_streaming.py (94%) diff --git a/src/control_backend/agents/act_agents/__init__.py b/src/control_backend/agents/act_agents/__init__.py deleted file mode 100644 index c09a449..0000000 --- a/src/control_backend/agents/act_agents/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from .act_speech_agent import ActSpeechAgent as ActSpeechAgent diff --git a/src/control_backend/agents/actuation/__init__.py b/src/control_backend/agents/actuation/__init__.py new file mode 100644 index 0000000..e745333 --- /dev/null +++ b/src/control_backend/agents/actuation/__init__.py @@ -0,0 +1 @@ +from .robot_speech_agent import RobotSpeechAgent as RobotSpeechAgent diff --git a/src/control_backend/agents/act_agents/act_speech_agent.py b/src/control_backend/agents/actuation/robot_speech_agent.py similarity index 97% rename from src/control_backend/agents/act_agents/act_speech_agent.py rename to src/control_backend/agents/actuation/robot_speech_agent.py index bc20f1b..d540698 100644 --- a/src/control_backend/agents/act_agents/act_speech_agent.py +++ b/src/control_backend/agents/actuation/robot_speech_agent.py @@ -10,7 +10,7 @@ from control_backend.core.config import settings from control_backend.schemas.ri_message import SpeechCommand -class ActSpeechAgent(BaseAgent): +class RobotSpeechAgent(BaseAgent): subsocket: zmq.Socket pubsocket: zmq.Socket address = "" @@ -64,7 +64,7 @@ class ActSpeechAgent(BaseAgent): async def setup(self): """ - Setup the command agent + Setup the robot speech command agent """ self.logger.info("Setting up %s", self.jid) diff --git a/src/control_backend/agents/bdi/__init__.py b/src/control_backend/agents/bdi/__init__.py new file mode 100644 index 0000000..a7f6082 --- /dev/null +++ b/src/control_backend/agents/bdi/__init__.py @@ -0,0 +1,7 @@ +from .bdi_core_agent.bdi_core_agent import BDICoreAgent as BDICoreAgent +from .belief_collector_agent.belief_collector_agent import ( + BDIBeliefCollectorAgent as BDIBeliefCollectorAgent, +) +from .text_belief_extractor_agent.text_belief_extractor_agent import ( + TextBeliefExtractorAgent as TextBeliefExtractorAgent, +) diff --git a/src/control_backend/agents/bdi_agents/bdi_core_agent/bdi_core_agent.py b/src/control_backend/agents/bdi/bdi_core_agent/bdi_core_agent.py similarity index 95% rename from src/control_backend/agents/bdi_agents/bdi_core_agent/bdi_core_agent.py rename to src/control_backend/agents/bdi/bdi_core_agent/bdi_core_agent.py index 9a8a9f1..b20c872 100644 --- a/src/control_backend/agents/bdi_agents/bdi_core_agent/bdi_core_agent.py +++ b/src/control_backend/agents/bdi/bdi_core_agent/bdi_core_agent.py @@ -57,7 +57,7 @@ class BDICoreAgent(BDIAgent): class SendBehaviour(OneShotBehaviour): async def run(self) -> None: msg = Message( - to=settings.agent_settings.llm_agent_name + "@" + settings.agent_settings.host, + to=settings.agent_settings.llm_name + "@" + settings.agent_settings.host, body=text, ) diff --git a/src/control_backend/agents/bdi_agents/bdi_core_agent/behaviours/belief_setter_behaviour.py b/src/control_backend/agents/bdi/bdi_core_agent/behaviours/belief_setter_behaviour.py similarity index 97% rename from src/control_backend/agents/bdi_agents/bdi_core_agent/behaviours/belief_setter_behaviour.py rename to src/control_backend/agents/bdi/bdi_core_agent/behaviours/belief_setter_behaviour.py index d96a239..105d6d2 100644 --- a/src/control_backend/agents/bdi_agents/bdi_core_agent/behaviours/belief_setter_behaviour.py +++ b/src/control_backend/agents/bdi/bdi_core_agent/behaviours/belief_setter_behaviour.py @@ -32,7 +32,7 @@ class BeliefSetterBehaviour(CyclicBehaviour): self.agent.logger.debug("Processing message from sender: %s", sender) match sender: - case settings.agent_settings.bdi_belief_collector_agent_name: + case settings.agent_settings.bdi_belief_collector_name: self.agent.logger.debug( "Message is from the belief collector agent. Processing as belief message." ) diff --git a/src/control_backend/agents/bdi_agents/bdi_core_agent/behaviours/receive_llm_resp_behaviour.py b/src/control_backend/agents/bdi/bdi_core_agent/behaviours/receive_llm_resp_behaviour.py similarity index 89% rename from src/control_backend/agents/bdi_agents/bdi_core_agent/behaviours/receive_llm_resp_behaviour.py rename to src/control_backend/agents/bdi/bdi_core_agent/behaviours/receive_llm_resp_behaviour.py index 2a06fb9..cf5cc03 100644 --- a/src/control_backend/agents/bdi_agents/bdi_core_agent/behaviours/receive_llm_resp_behaviour.py +++ b/src/control_backend/agents/bdi/bdi_core_agent/behaviours/receive_llm_resp_behaviour.py @@ -15,14 +15,14 @@ class ReceiveLLMResponseBehaviour(CyclicBehaviour): sender = msg.sender.node match sender: - case settings.agent_settings.llm_agent_name: + case settings.agent_settings.llm_name: content = msg.body self.agent.logger.info("Received LLM response: %s", content) speech_command = SpeechCommand(data=content) message = Message( - to=settings.agent_settings.act_speech_agent_name + to=settings.agent_settings.robot_speech_name + "@" + settings.agent_settings.host, sender=self.agent.jid, diff --git a/src/control_backend/agents/bdi_agents/bdi_core_agent/rules.asl b/src/control_backend/agents/bdi/bdi_core_agent/rules.asl similarity index 100% rename from src/control_backend/agents/bdi_agents/bdi_core_agent/rules.asl rename to src/control_backend/agents/bdi/bdi_core_agent/rules.asl diff --git a/src/control_backend/agents/bdi_agents/bdi_belief_collector_agent/behaviours/bel_collector_behaviour.py b/src/control_backend/agents/bdi/belief_collector_agent/behaviours/belief_collector_behaviour.py similarity index 94% rename from src/control_backend/agents/bdi_agents/bdi_belief_collector_agent/behaviours/bel_collector_behaviour.py rename to src/control_backend/agents/bdi/belief_collector_agent/behaviours/belief_collector_behaviour.py index ff89eae..7dfee28 100644 --- a/src/control_backend/agents/bdi_agents/bdi_belief_collector_agent/behaviours/bel_collector_behaviour.py +++ b/src/control_backend/agents/bdi/belief_collector_agent/behaviours/belief_collector_behaviour.py @@ -7,7 +7,7 @@ from spade.behaviour import CyclicBehaviour from control_backend.core.config import settings -class BelCollectorBehaviour(CyclicBehaviour): +class BeliefCollectorBehaviour(CyclicBehaviour): """ Continuously collects beliefs/emotions from extractor agents: Then we send a unified belief packet to the BDI agent. @@ -83,9 +83,7 @@ class BelCollectorBehaviour(CyclicBehaviour): if not beliefs: return - to_jid = ( - f"{settings.agent_settings.bdi_core_agent_agent_name}@{settings.agent_settings.host}" - ) + to_jid = f"{settings.agent_settings.bdi_core_name}@{settings.agent_settings.host}" msg = Message(to=to_jid, sender=self.agent.jid, thread="beliefs") msg.body = json.dumps(beliefs) diff --git a/src/control_backend/agents/bdi_agents/bdi_belief_collector_agent/bel_collector_agent.py b/src/control_backend/agents/bdi/belief_collector_agent/belief_collector_agent.py similarity index 72% rename from src/control_backend/agents/bdi_agents/bdi_belief_collector_agent/bel_collector_agent.py rename to src/control_backend/agents/bdi/belief_collector_agent/belief_collector_agent.py index f08e6a6..a82e230 100644 --- a/src/control_backend/agents/bdi_agents/bdi_belief_collector_agent/bel_collector_agent.py +++ b/src/control_backend/agents/bdi/belief_collector_agent/belief_collector_agent.py @@ -1,11 +1,11 @@ from control_backend.agents.base import BaseAgent -from .behaviours.bel_collector_behaviour import BelCollectorBehaviour +from .behaviours.belief_collector_behaviour import BeliefCollectorBehaviour class BDIBeliefCollectorAgent(BaseAgent): async def setup(self): self.logger.info("BDIBeliefCollectorAgent starting (%s)", self.jid) # Attach the continuous collector behaviour (listens and forwards to BDI) - self.add_behaviour(BelCollectorBehaviour()) + self.add_behaviour(BeliefCollectorBehaviour()) self.logger.info("BDIBeliefCollectorAgent ready.") diff --git a/src/control_backend/agents/bdi_agents/bdi_text_belief_agent/behaviours/bdi_text_belief_behaviour.py b/src/control_backend/agents/bdi/text_belief_extractor_agent/behaviours/text_belief_extractor_behaviour.py similarity index 92% rename from src/control_backend/agents/bdi_agents/bdi_text_belief_agent/behaviours/bdi_text_belief_behaviour.py rename to src/control_backend/agents/bdi/text_belief_extractor_agent/behaviours/text_belief_extractor_behaviour.py index c17a4d0..e09ed0c 100644 --- a/src/control_backend/agents/bdi_agents/bdi_text_belief_agent/behaviours/bdi_text_belief_behaviour.py +++ b/src/control_backend/agents/bdi/text_belief_extractor_agent/behaviours/text_belief_extractor_behaviour.py @@ -7,7 +7,7 @@ from spade.message import Message from control_backend.core.config import settings -class BDITextBeliefBehaviour(CyclicBehaviour): +class TextBeliefExtractorBehaviour(CyclicBehaviour): logger = logging.getLogger(__name__) # TODO: LLM prompt nog hardcoded @@ -44,7 +44,7 @@ class BDITextBeliefBehaviour(CyclicBehaviour): sender = msg.sender.node match sender: - case settings.agent_settings.per_transcription_agent_name: + case settings.agent_settings.transcription_name: self.logger.debug("Received text from transcriber: %s", msg.body) await self._process_transcription_demo(msg.body) case _: @@ -71,7 +71,7 @@ class BDITextBeliefBehaviour(CyclicBehaviour): belief_message = Message() belief_message.to = ( - settings.agent_settings.bdi_belief_collector_agent_name + settings.agent_settings.bdi_belief_collector_name + "@" + settings.agent_settings.host ) @@ -95,9 +95,7 @@ class BDITextBeliefBehaviour(CyclicBehaviour): belief_msg = Message() belief_msg.to = ( - settings.agent_settings.bdi_belief_collector_agent_name - + "@" - + settings.agent_settings.host + settings.agent_settings.bdi_belief_collector_name + "@" + settings.agent_settings.host ) belief_msg.body = payload belief_msg.thread = "beliefs" diff --git a/src/control_backend/agents/bdi/text_belief_extractor_agent/text_belief_extractor_agent.py b/src/control_backend/agents/bdi/text_belief_extractor_agent/text_belief_extractor_agent.py new file mode 100644 index 0000000..4baa420 --- /dev/null +++ b/src/control_backend/agents/bdi/text_belief_extractor_agent/text_belief_extractor_agent.py @@ -0,0 +1,8 @@ +from control_backend.agents.base import BaseAgent + +from .behaviours.text_belief_extractor_behaviour import TextBeliefExtractorBehaviour + + +class TextBeliefExtractorAgent(BaseAgent): + async def setup(self): + self.add_behaviour(TextBeliefExtractorBehaviour()) diff --git a/src/control_backend/agents/bdi_agents/__init__.py b/src/control_backend/agents/bdi_agents/__init__.py deleted file mode 100644 index 9b17f30..0000000 --- a/src/control_backend/agents/bdi_agents/__init__.py +++ /dev/null @@ -1,5 +0,0 @@ -from .bdi_belief_collector_agent.bel_collector_agent import ( - BDIBeliefCollectorAgent as BDIBeliefCollectorAgent, -) -from .bdi_core_agent.bdi_core_agent import BDICoreAgent as BDICoreAgent -from .bdi_text_belief_agent.bdi_text_belief_agent import BDITextBeliefAgent as BDITextBeliefAgent diff --git a/src/control_backend/agents/bdi_agents/bdi_text_belief_agent/bdi_text_belief_agent.py b/src/control_backend/agents/bdi_agents/bdi_text_belief_agent/bdi_text_belief_agent.py deleted file mode 100644 index c9987cf..0000000 --- a/src/control_backend/agents/bdi_agents/bdi_text_belief_agent/bdi_text_belief_agent.py +++ /dev/null @@ -1,8 +0,0 @@ -from control_backend.agents.base import BaseAgent - -from .behaviours.bdi_text_belief_behaviour import BDITextBeliefBehaviour - - -class BDITextBeliefAgent(BaseAgent): - async def setup(self): - self.add_behaviour(BDITextBeliefBehaviour()) diff --git a/src/control_backend/agents/com_agents/__init__.py b/src/control_backend/agents/com_agents/__init__.py deleted file mode 100644 index c91ad14..0000000 --- a/src/control_backend/agents/com_agents/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from .com_ri_agent import ComRIAgent as ComRIAgent diff --git a/src/control_backend/agents/communication/__init__.py b/src/control_backend/agents/communication/__init__.py new file mode 100644 index 0000000..2aa1535 --- /dev/null +++ b/src/control_backend/agents/communication/__init__.py @@ -0,0 +1 @@ +from .ri_communication_agent import RICommunicationAgent as RICommunicationAgent diff --git a/src/control_backend/agents/com_agents/com_ri_agent.py b/src/control_backend/agents/communication/ri_communication_agent.py similarity index 96% rename from src/control_backend/agents/com_agents/com_ri_agent.py rename to src/control_backend/agents/communication/ri_communication_agent.py index 389aff5..3e52df3 100644 --- a/src/control_backend/agents/com_agents/com_ri_agent.py +++ b/src/control_backend/agents/communication/ri_communication_agent.py @@ -8,10 +8,10 @@ from zmq.asyncio import Context from control_backend.agents import BaseAgent from control_backend.core.config import settings -from ..act_agents.act_speech_agent import ActSpeechAgent +from ..actuation.robot_speech_agent import RobotSpeechAgent -class ComRIAgent(BaseAgent): +class RICommunicationAgent(BaseAgent): req_socket: zmq.Socket _address = "" _bind = True @@ -204,11 +204,11 @@ class ComRIAgent(BaseAgent): else: self._req_socket.bind(addr) case "actuation": - ri_commands_agent = ActSpeechAgent( - settings.agent_settings.act_speech_agent_name + ri_commands_agent = RobotSpeechAgent( + settings.agent_settings.robot_speech_name + "@" + settings.agent_settings.host, - settings.agent_settings.act_speech_agent_name, + settings.agent_settings.robot_speech_name, address=addr, bind=bind, ) diff --git a/src/control_backend/agents/llm_agents/__init__.py b/src/control_backend/agents/llm/__init__.py similarity index 100% rename from src/control_backend/agents/llm_agents/__init__.py rename to src/control_backend/agents/llm/__init__.py diff --git a/src/control_backend/agents/llm_agents/llm_agent.py b/src/control_backend/agents/llm/llm_agent.py similarity index 96% rename from src/control_backend/agents/llm_agents/llm_agent.py rename to src/control_backend/agents/llm/llm_agent.py index ce1b791..eae41fd 100644 --- a/src/control_backend/agents/llm_agents/llm_agent.py +++ b/src/control_backend/agents/llm/llm_agent.py @@ -39,7 +39,7 @@ class LLMAgent(BaseAgent): sender, ) - if sender == settings.agent_settings.bdi_core_agent_agent_name: + if sender == settings.agent_settings.bdi_core_name: self.agent.logger.debug("Processing message from BDI Core Agent") await self._process_bdi_message(msg) else: @@ -63,9 +63,7 @@ class LLMAgent(BaseAgent): Sends a response message back to the BDI Core Agent. """ reply = Message( - to=settings.agent_settings.bdi_core_agent_agent_name - + "@" - + settings.agent_settings.host, + to=settings.agent_settings.bdi_core_name + "@" + settings.agent_settings.host, body=msg, ) await self.send(reply) diff --git a/src/control_backend/agents/llm_agents/llm_instructions.py b/src/control_backend/agents/llm/llm_instructions.py similarity index 100% rename from src/control_backend/agents/llm_agents/llm_instructions.py rename to src/control_backend/agents/llm/llm_instructions.py diff --git a/src/control_backend/agents/mock_agents/bel_text_agent.py b/src/control_backend/agents/mock_agents/bel_text_agent.py deleted file mode 100644 index 2827307..0000000 --- a/src/control_backend/agents/mock_agents/bel_text_agent.py +++ /dev/null @@ -1,44 +0,0 @@ -import json - -from spade.agent import Agent -from spade.behaviour import OneShotBehaviour -from spade.message import Message - -from control_backend.core.config import settings - - -class BelTextAgent(Agent): - class SendOnceBehaviourBlfText(OneShotBehaviour): - async def run(self): - to_jid = ( - settings.agent_settings.bdi_belief_collector_agent_name - + "@" - + settings.agent_settings.host - ) - - # Send multiple beliefs in one JSON payload - payload = { - "type": "belief_extraction_text", - "beliefs": { - "user_said": [ - "hello test", - "Can you help me?", - "stop talking to me", - "No", - "Pepper do a dance", - ] - }, - } - - msg = Message(to=to_jid) - msg.body = json.dumps(payload) - await self.send(msg) - print(f"Beliefs sent to {to_jid}!") - - self.exit_code = "Job Finished!" - await self.agent.stop() - - async def setup(self): - print("BelTextAgent started") - self.b = self.SendOnceBehaviourBlfText() - self.add_behaviour(self.b) diff --git a/src/control_backend/agents/per_agents/__init__.py b/src/control_backend/agents/per_agents/__init__.py deleted file mode 100644 index e3d9cf0..0000000 --- a/src/control_backend/agents/per_agents/__init__.py +++ /dev/null @@ -1,4 +0,0 @@ -from .per_transcription_agent.per_transcription_agent import ( - PerTranscriptionAgent as PerTranscriptionAgent, -) -from .per_vad_agent import PerVADAgent as PerVADAgent diff --git a/src/control_backend/agents/perception/__init__.py b/src/control_backend/agents/perception/__init__.py new file mode 100644 index 0000000..e18361a --- /dev/null +++ b/src/control_backend/agents/perception/__init__.py @@ -0,0 +1,4 @@ +from .transcription_agent.transcription_agent import ( + TranscriptionAgent as TranscriptionAgent, +) +from .vad_agent import VADAgent as VADAgent diff --git a/src/control_backend/agents/per_agents/per_transcription_agent/speech_recognizer.py b/src/control_backend/agents/perception/transcription_agent/speech_recognizer.py similarity index 100% rename from src/control_backend/agents/per_agents/per_transcription_agent/speech_recognizer.py rename to src/control_backend/agents/perception/transcription_agent/speech_recognizer.py diff --git a/src/control_backend/agents/per_agents/per_transcription_agent/per_transcription_agent.py b/src/control_backend/agents/perception/transcription_agent/transcription_agent.py similarity index 88% rename from src/control_backend/agents/per_agents/per_transcription_agent/per_transcription_agent.py rename to src/control_backend/agents/perception/transcription_agent/transcription_agent.py index e82b5d7..8da1721 100644 --- a/src/control_backend/agents/per_agents/per_transcription_agent/per_transcription_agent.py +++ b/src/control_backend/agents/perception/transcription_agent/transcription_agent.py @@ -12,24 +12,20 @@ from control_backend.core.config import settings from .speech_recognizer import SpeechRecognizer -class PerTranscriptionAgent(BaseAgent): +class TranscriptionAgent(BaseAgent): """ An agent which listens to audio fragments with voice, transcribes them, and sends the transcription to other agents. """ def __init__(self, audio_in_address: str): - jid = ( - settings.agent_settings.per_transcription_agent_name - + "@" - + settings.agent_settings.host - ) - super().__init__(jid, settings.agent_settings.per_transcription_agent_name) + jid = settings.agent_settings.transcription_name + "@" + settings.agent_settings.host + super().__init__(jid, settings.agent_settings.transcription_name) self.audio_in_address = audio_in_address self.audio_in_socket: azmq.Socket | None = None - class Transcribing(CyclicBehaviour): + class TranscribingBehaviour(CyclicBehaviour): def __init__(self, audio_in_socket: azmq.Socket): super().__init__() self.audio_in_socket = audio_in_socket @@ -83,7 +79,7 @@ class PerTranscriptionAgent(BaseAgent): self._connect_audio_in_socket() - transcribing = self.Transcribing(self.audio_in_socket) + transcribing = self.TranscribingBehaviour(self.audio_in_socket) transcribing.warmup() self.add_behaviour(transcribing) diff --git a/src/control_backend/agents/per_agents/per_vad_agent.py b/src/control_backend/agents/perception/vad_agent.py similarity index 91% rename from src/control_backend/agents/per_agents/per_vad_agent.py rename to src/control_backend/agents/perception/vad_agent.py index f065e2a..cab27c2 100644 --- a/src/control_backend/agents/per_agents/per_vad_agent.py +++ b/src/control_backend/agents/perception/vad_agent.py @@ -7,7 +7,7 @@ from spade.behaviour import CyclicBehaviour from control_backend.agents import BaseAgent from control_backend.core.config import settings -from .per_transcription_agent.per_transcription_agent import PerTranscriptionAgent +from .transcription_agent.transcription_agent import TranscriptionAgent class SocketPoller[T]: @@ -40,7 +40,7 @@ class SocketPoller[T]: return None -class Streaming(CyclicBehaviour): +class StreamingBehaviour(CyclicBehaviour): def __init__(self, audio_in_socket: azmq.Socket, audio_out_socket: azmq.Socket): super().__init__() self.audio_in_poller = SocketPoller[bytes](audio_in_socket) @@ -102,15 +102,15 @@ class Streaming(CyclicBehaviour): self.audio_buffer = chunk -class PerVADAgent(BaseAgent): +class VADAgent(BaseAgent): """ An agent which listens to an audio stream, does Voice Activity Detection (VAD), and sends fragments with detected speech to other agents over ZeroMQ. """ def __init__(self, audio_in_address: str, audio_in_bind: bool): - jid = settings.agent_settings.per_vad_agent_name + "@" + settings.agent_settings.host - super().__init__(jid, settings.agent_settings.per_vad_agent_name) + jid = settings.agent_settings.vad_name + "@" + settings.agent_settings.host + super().__init__(jid, settings.agent_settings.vad_name) self.audio_in_address = audio_in_address self.audio_in_bind = audio_in_bind @@ -118,7 +118,7 @@ class PerVADAgent(BaseAgent): self.audio_in_socket: azmq.Socket | None = None self.audio_out_socket: azmq.Socket | None = None - self.streaming_behaviour: Streaming | None = None + self.streaming_behaviour: StreamingBehaviour | None = None async def stop(self): """ @@ -162,11 +162,11 @@ class PerVADAgent(BaseAgent): return audio_out_address = f"tcp://localhost:{audio_out_port}" - self.streaming_behaviour = Streaming(self.audio_in_socket, self.audio_out_socket) + self.streaming_behaviour = StreamingBehaviour(self.audio_in_socket, self.audio_out_socket) self.add_behaviour(self.streaming_behaviour) # Start agents dependent on the output audio fragments here - transcriber = PerTranscriptionAgent(audio_out_address) + transcriber = TranscriptionAgent(audio_out_address) await transcriber.start() self.logger.info("Finished setting up %s", self.jid) diff --git a/src/control_backend/core/config.py b/src/control_backend/core/config.py index 150a06e..e0f1292 100644 --- a/src/control_backend/core/config.py +++ b/src/control_backend/core/config.py @@ -9,16 +9,16 @@ class ZMQSettings(BaseModel): class AgentSettings(BaseModel): host: str = "localhost" - bdi_core_agent_agent_name: str = "bdi_core_agent" - bdi_belief_collector_agent_name: str = "bdi_belief_collector_agent" - bdi_text_belief_agent_name: str = "bdi_text_belief_agent" - per_vad_agent_name: str = "per_vad_agent" - llm_agent_name: str = "llm_agent" - test_agent_name: str = "test_agent" - per_transcription_agent_name: str = "per_transcription_agent" + bdi_core_name: str = "bdi_core_agent" + bdi_belief_collector_name: str = "belief_collector_agent" + text_belief_extractor_name: str = "text_belief_extractor_agent" + vad_name: str = "vad_agent" + llm_name: str = "llm_agent" + test_name: str = "test_agent" + transcription_name: str = "transcription_agent" - com_ri_agent_name: str = "com_ri_agent" - act_speech_agent_name: str = "act_speech_agent" + ri_communication_name: str = "ri_communication_agent" + robot_speech_name: str = "robot_speech_agent" class LLMSettings(BaseModel): diff --git a/src/control_backend/main.py b/src/control_backend/main.py index 62d6718..20c8482 100644 --- a/src/control_backend/main.py +++ b/src/control_backend/main.py @@ -9,21 +9,21 @@ from zmq.asyncio import Context # Act agents # BDI agents -from control_backend.agents.bdi_agents import ( +from control_backend.agents.bdi import ( BDIBeliefCollectorAgent, BDICoreAgent, - BDITextBeliefAgent, + TextBeliefExtractorAgent, ) # Communication agents -from control_backend.agents.com_agents import ComRIAgent +from control_backend.agents.communication import RICommunicationAgent # Emotional Agents # LLM Agents -from control_backend.agents.llm_agents import LLMAgent +from control_backend.agents.llm import LLMAgent # Perceive agents -from control_backend.agents.per_agents import PerVADAgent +from control_backend.agents.perception import VADAgent # Other backend imports from control_backend.api.v1.router import api_router @@ -77,12 +77,12 @@ async def lifespan(app: FastAPI): logger.info("Initializing and starting agents.") agents_to_start = { "ComRIAgent": ( - ComRIAgent, + RICommunicationAgent, { - "name": settings.agent_settings.com_ri_agent_name, - "jid": f"{settings.agent_settings.com_ri_agent_name}" + "name": settings.agent_settings.ri_communication_name, + "jid": f"{settings.agent_settings.ri_communication_name}" f"@{settings.agent_settings.host}", - "password": settings.agent_settings.com_ri_agent_name, + "password": settings.agent_settings.ri_communication_name, "address": "tcp://*:5555", "bind": True, }, @@ -90,56 +90,61 @@ async def lifespan(app: FastAPI): "LLMAgent": ( LLMAgent, { - "name": settings.agent_settings.llm_agent_name, - "jid": f"{settings.agent_settings.llm_agent_name}@{settings.agent_settings.host}", - "password": settings.agent_settings.llm_agent_name, + "name": settings.agent_settings.llm_name, + "jid": f"{settings.agent_settings.llm_name}@{settings.agent_settings.host}", + "password": settings.agent_settings.llm_name, }, ), "BDICoreAgent": ( BDICoreAgent, { - "name": settings.agent_settings.bdi_core_agent_agent_name, - "jid": f"{settings.agent_settings.bdi_core_agent_agent_name}@" - f"{settings.agent_settings.host}", - "password": settings.agent_settings.bdi_core_agent_agent_name, - "asl": "src/control_backend/agents/bdi_agents/bdi_core_agent/rules.asl", + "name": settings.agent_settings.bdi_core_name, + "jid": f"{settings.agent_settings.bdi_core_name}@{settings.agent_settings.host}", + "password": settings.agent_settings.bdi_core_name, + "asl": "src/control_backend/agents/bdi/bdi_core_agent/rules.asl", }, ), "BDIBeliefCollectorAgent": ( BDIBeliefCollectorAgent, { - "name": settings.agent_settings.bdi_belief_collector_agent_name, - "jid": f"{settings.agent_settings.bdi_belief_collector_agent_name}@" + "name": settings.agent_settings.bdi_belief_collector_name, + "jid": f"{settings.agent_settings.bdi_belief_collector_name}@" f"{settings.agent_settings.host}", - "password": settings.agent_settings.bdi_belief_collector_agent_name, + "password": settings.agent_settings.bdi_belief_collector_name, }, ), "BDITextBeliefAgent": ( - BDITextBeliefAgent, + TextBeliefExtractorAgent, { - "name": settings.agent_settings.bdi_text_belief_agent_name, - "jid": f"{settings.agent_settings.bdi_text_belief_agent_name}@" + "name": settings.agent_settings.text_belief_extractor_name, + "jid": f"{settings.agent_settings.text_belief_extractor_name}@" f"{settings.agent_settings.host}", - "password": settings.agent_settings.bdi_text_belief_agent_name, + "password": settings.agent_settings.text_belief_extractor_name, }, ), "PerVADAgent": ( - PerVADAgent, + VADAgent, {"audio_in_address": "tcp://localhost:5558", "audio_in_bind": False}, ), } + vad_agent = None + for name, (agent_class, kwargs) in agents_to_start.items(): try: logger.debug("Starting agent: %s", name) agent_instance = agent_class(**{k: v for k, v in kwargs.items() if k != "name"}) await agent_instance.start() + if isinstance(agent_instance, VADAgent): + vad_agent = agent_instance logger.info("Agent '%s' started successfully.", name) except Exception as e: logger.error("Failed to start agent '%s': %s", name, e, exc_info=True) # Consider if the application should continue if an agent fails to start. raise + await vad_agent.streaming_behaviour.reset() + logger.info("Application startup complete.") yield diff --git a/test/integration/agents/act_agents/test_act_speech_agent.py b/test/integration/agents/actuation/test_robot_speech_agent.py similarity index 77% rename from test/integration/agents/act_agents/test_act_speech_agent.py rename to test/integration/agents/actuation/test_robot_speech_agent.py index e81a09a..327415c 100644 --- a/test/integration/agents/act_agents/test_act_speech_agent.py +++ b/test/integration/agents/actuation/test_robot_speech_agent.py @@ -4,13 +4,13 @@ from unittest.mock import AsyncMock, MagicMock, patch import pytest import zmq -from control_backend.agents.act_agents.act_speech_agent import ActSpeechAgent +from control_backend.agents.actuation.robot_speech_agent import RobotSpeechAgent @pytest.fixture def zmq_context(mocker): mock_context = mocker.patch( - "control_backend.agents.act_agents.act_speech_agent.zmq.Context.instance" + "control_backend.agents.actuation.robot_speech_agent.zmq.Context.instance" ) mock_context.return_value = MagicMock() return mock_context @@ -21,8 +21,8 @@ async def test_setup_bind(zmq_context, mocker): """Test setup with bind=True""" fake_socket = zmq_context.return_value.socket.return_value - agent = ActSpeechAgent("test@server", "password", address="tcp://localhost:5555", bind=True) - settings = mocker.patch("control_backend.agents.act_agents.act_speech_agent.settings") + agent = RobotSpeechAgent("test@server", "password", address="tcp://localhost:5555", bind=True) + settings = mocker.patch("control_backend.agents.actuation.robot_speech_agent.settings") settings.zmq_settings.internal_sub_address = "tcp://internal:1234" await agent.setup() @@ -40,8 +40,8 @@ async def test_setup_connect(zmq_context, mocker): """Test setup with bind=False""" fake_socket = zmq_context.return_value.socket.return_value - agent = ActSpeechAgent("test@server", "password", address="tcp://localhost:5555", bind=False) - settings = mocker.patch("control_backend.agents.act_agents.act_speech_agent.settings") + agent = RobotSpeechAgent("test@server", "password", address="tcp://localhost:5555", bind=False) + settings = mocker.patch("control_backend.agents.actuation.robot_speech_agent.settings") settings.zmq_settings.internal_sub_address = "tcp://internal:1234" await agent.setup() @@ -59,7 +59,7 @@ async def test_send_commands_behaviour_valid_message(): ) fake_socket.send_json = AsyncMock() - agent = ActSpeechAgent("test@server", "password") + agent = RobotSpeechAgent("test@server", "password") agent.subsocket = fake_socket agent.pubsocket = fake_socket @@ -67,7 +67,7 @@ async def test_send_commands_behaviour_valid_message(): behaviour.agent = agent with patch( - "control_backend.agents.act_agents.act_speech_agent.SpeechCommand" + "control_backend.agents.actuation.robot_speech_agent.SpeechCommand" ) as MockSpeechCommand: mock_message = MagicMock() MockSpeechCommand.model_validate.return_value = mock_message @@ -85,7 +85,7 @@ async def test_send_commands_behaviour_invalid_message(): fake_socket.recv_multipart = AsyncMock(return_value=(b"command", b"{invalid_json}")) fake_socket.send_json = AsyncMock() - agent = ActSpeechAgent("test@server", "password") + agent = RobotSpeechAgent("test@server", "password") agent.subsocket = fake_socket agent.pubsocket = fake_socket diff --git a/test/integration/agents/com_agents/test_com_ri_agent.py b/test/integration/agents/communication/test_ri_communication_agent.py similarity index 84% rename from test/integration/agents/com_agents/test_com_ri_agent.py rename to test/integration/agents/communication/test_ri_communication_agent.py index 49c04ac..b82234b 100644 --- a/test/integration/agents/com_agents/test_com_ri_agent.py +++ b/test/integration/agents/communication/test_ri_communication_agent.py @@ -3,11 +3,11 @@ from unittest.mock import ANY, AsyncMock, MagicMock, patch import pytest -from control_backend.agents.com_agents.com_ri_agent import ComRIAgent +from control_backend.agents.communication.ri_communication_agent import RICommunicationAgent -def act_agent_path(): - return "control_backend.agents.com_agents.com_ri_agent.ActSpeechAgent" +def speech_agent_path(): + return "control_backend.agents.communication.ri_communication_agent.RobotSpeechAgent" def fake_json_correct_negototiate_1(): @@ -91,7 +91,7 @@ def fake_json_invalid_id_negototiate(): @pytest.fixture def zmq_context(mocker): mock_context = mocker.patch( - "control_backend.agents.com_agents.com_ri_agent.zmq.Context.instance" + "control_backend.agents.communication.ri_communication_agent.zmq.Context.instance" ) mock_context.return_value = MagicMock() return mock_context @@ -109,12 +109,17 @@ async def test_setup_creates_socket_and_negotiate_1(zmq_context): fake_socket.send_multipart = AsyncMock() # Mock ActSpeechAgent agent startup - with patch(act_agent_path(), autospec=True) as MockCommandAgent: + with patch(speech_agent_path(), autospec=True) as MockCommandAgent: fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() # --- Act --- - agent = ComRIAgent("test@server", "password", address="tcp://localhost:5555", bind=False) + agent = RICommunicationAgent( + "test@server", + "password", + address="tcp://localhost:5555", + bind=False, + ) await agent.setup() # --- Assert --- @@ -144,12 +149,17 @@ async def test_setup_creates_socket_and_negotiate_2(zmq_context): fake_socket.send_multipart = AsyncMock() # Mock ActSpeechAgent agent startup - with patch(act_agent_path(), autospec=True) as MockCommandAgent: + with patch(speech_agent_path(), autospec=True) as MockCommandAgent: fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() # --- Act --- - agent = ComRIAgent("test@server", "password", address="tcp://localhost:5555", bind=False) + agent = RICommunicationAgent( + "test@server", + "password", + address="tcp://localhost:5555", + bind=False, + ) await agent.setup() # --- Assert --- @@ -182,12 +192,17 @@ async def test_setup_creates_socket_and_negotiate_3(zmq_context): # We are sending wrong negotiation info to the communication agent, # so we should retry and expect a better response, within a limited time. - with patch(act_agent_path(), autospec=True) as MockCommandAgent: + with patch(speech_agent_path(), autospec=True) as MockCommandAgent: fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() # --- Act --- - agent = ComRIAgent("test@server", "password", address="tcp://localhost:5555", bind=False) + agent = RICommunicationAgent( + "test@server", + "password", + address="tcp://localhost:5555", + bind=False, + ) await agent.setup(max_retries=1) # --- Assert --- @@ -213,11 +228,16 @@ async def test_setup_creates_socket_and_negotiate_4(zmq_context): fake_socket.send_multipart = AsyncMock() # Mock ActSpeechAgent agent startup - with patch(act_agent_path(), autospec=True) as MockCommandAgent: + with patch(speech_agent_path(), autospec=True) as MockCommandAgent: fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() # --- Act --- - agent = ComRIAgent("test@server", "password", address="tcp://localhost:5555", bind=True) + agent = RICommunicationAgent( + "test@server", + "password", + address="tcp://localhost:5555", + bind=True, + ) await agent.setup() # --- Assert --- @@ -247,11 +267,16 @@ async def test_setup_creates_socket_and_negotiate_5(zmq_context): fake_socket.send_multipart = AsyncMock() # Mock ActSpeechAgent agent startup - with patch(act_agent_path(), autospec=True) as MockCommandAgent: + with patch(speech_agent_path(), autospec=True) as MockCommandAgent: fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() # --- Act --- - agent = ComRIAgent("test@server", "password", address="tcp://localhost:5555", bind=False) + agent = RICommunicationAgent( + "test@server", + "password", + address="tcp://localhost:5555", + bind=False, + ) await agent.setup() # --- Assert --- @@ -281,11 +306,16 @@ async def test_setup_creates_socket_and_negotiate_6(zmq_context): fake_socket.send_multipart = AsyncMock() # Mock ActSpeechAgent agent startup - with patch(act_agent_path(), autospec=True) as MockCommandAgent: + with patch(speech_agent_path(), autospec=True) as MockCommandAgent: fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() # --- Act --- - agent = ComRIAgent("test@server", "password", address="tcp://localhost:5555", bind=False) + agent = RICommunicationAgent( + "test@server", + "password", + address="tcp://localhost:5555", + bind=False, + ) await agent.setup() # --- Assert --- @@ -318,13 +348,18 @@ async def test_setup_creates_socket_and_negotiate_7(zmq_context): # We are sending wrong negotiation info to the communication agent, # so we should retry and expect a better response, within a limited time. - with patch(act_agent_path(), autospec=True) as MockCommandAgent: + with patch(speech_agent_path(), autospec=True) as MockCommandAgent: fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() # --- Act --- - agent = ComRIAgent("test@server", "password", address="tcp://localhost:5555", bind=False) + agent = RICommunicationAgent( + "test@server", + "password", + address="tcp://localhost:5555", + bind=False, + ) await agent.setup(max_retries=1) # --- Assert --- @@ -346,13 +381,18 @@ async def test_setup_creates_socket_and_negotiate_timeout(zmq_context): fake_socket.recv_json = AsyncMock(side_effect=asyncio.TimeoutError) fake_socket.send_multipart = AsyncMock() - with patch(act_agent_path(), autospec=True) as MockCommandAgent: + with patch(speech_agent_path(), autospec=True) as MockCommandAgent: fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() # --- Act --- - agent = ComRIAgent("test@server", "password", address="tcp://localhost:5555", bind=False) + agent = RICommunicationAgent( + "test@server", + "password", + address="tcp://localhost:5555", + bind=False, + ) await agent.setup(max_retries=1) # --- Assert --- @@ -372,7 +412,7 @@ async def test_listen_behaviour_ping_correct(): fake_socket.recv_json = AsyncMock(return_value={"endpoint": "ping", "data": {}}) fake_socket.send_multipart = AsyncMock() - agent = ComRIAgent("test@server", "password") + agent = RICommunicationAgent("test@server", "password") agent._req_socket = fake_socket agent.connected = True @@ -405,7 +445,7 @@ async def test_listen_behaviour_ping_wrong_endpoint(): ) fake_pub_socket = AsyncMock() - agent = ComRIAgent("test@server", "password", fake_pub_socket) + agent = RICommunicationAgent("test@server", "password", fake_pub_socket) agent._req_socket = fake_socket agent.connected = True @@ -428,7 +468,7 @@ async def test_listen_behaviour_timeout(zmq_context): fake_socket.recv_json = AsyncMock(side_effect=asyncio.TimeoutError) fake_socket.send_multipart = AsyncMock() - agent = ComRIAgent("test@server", "password") + agent = RICommunicationAgent("test@server", "password") agent._req_socket = fake_socket agent.connected = True @@ -456,7 +496,7 @@ async def test_listen_behaviour_ping_no_endpoint(): } ) - agent = ComRIAgent("test@server", "password") + agent = RICommunicationAgent("test@server", "password") agent._req_socket = fake_socket agent.connected = True @@ -477,7 +517,12 @@ async def test_setup_unexpected_exception(zmq_context): fake_socket.recv_json = AsyncMock(side_effect=Exception("boom!")) fake_socket.send_multipart = AsyncMock() - agent = ComRIAgent("test@server", "password", address="tcp://localhost:5555", bind=False) + agent = RICommunicationAgent( + "test@server", + "password", + address="tcp://localhost:5555", + bind=False, + ) await agent.setup(max_retries=1) @@ -500,11 +545,16 @@ async def test_setup_unpacking_exception(zmq_context): fake_socket.recv_json = AsyncMock(return_value=malformed_data) # Patch ActSpeechAgent so it won't actually start - with patch(act_agent_path(), autospec=True) as MockCommandAgent: + with patch(speech_agent_path(), autospec=True) as MockCommandAgent: fake_agent_instance = MockCommandAgent.return_value fake_agent_instance.start = AsyncMock() - agent = ComRIAgent("test@server", "password", address="tcp://localhost:5555", bind=False) + agent = RICommunicationAgent( + "test@server", + "password", + address="tcp://localhost:5555", + bind=False, + ) # --- Act & Assert --- diff --git a/test/integration/agents/per_agents/per_vad_agent/speech_with_pauses_16k_1c_float32.wav b/test/integration/agents/perception/vad_agent/speech_with_pauses_16k_1c_float32.wav similarity index 100% rename from test/integration/agents/per_agents/per_vad_agent/speech_with_pauses_16k_1c_float32.wav rename to test/integration/agents/perception/vad_agent/speech_with_pauses_16k_1c_float32.wav diff --git a/test/integration/agents/per_agents/per_vad_agent/test_per_vad_agent.py b/test/integration/agents/perception/vad_agent/test_vad_agent.py similarity index 80% rename from test/integration/agents/per_agents/per_vad_agent/test_per_vad_agent.py rename to test/integration/agents/perception/vad_agent/test_vad_agent.py index 65c46ea..ecf9634 100644 --- a/test/integration/agents/per_agents/per_vad_agent/test_per_vad_agent.py +++ b/test/integration/agents/perception/vad_agent/test_vad_agent.py @@ -5,27 +5,25 @@ import pytest import zmq from spade.agent import Agent -from control_backend.agents.per_agents.per_vad_agent import PerVADAgent +from control_backend.agents.perception.vad_agent import VADAgent @pytest.fixture def zmq_context(mocker): - mock_context = mocker.patch( - "control_backend.agents.per_agents.per_vad_agent.azmq.Context.instance" - ) + mock_context = mocker.patch("control_backend.agents.perception.vad_agent.azmq.Context.instance") mock_context.return_value = MagicMock() return mock_context @pytest.fixture def streaming(mocker): - return mocker.patch("control_backend.agents.per_agents.per_vad_agent.Streaming") + return mocker.patch("control_backend.agents.perception.vad_agent.StreamingBehaviour") @pytest.fixture def per_transcription_agent(mocker): return mocker.patch( - "control_backend.agents.per_agents.per_vad_agent.PerTranscriptionAgent", autospec=True + "control_backend.agents.perception.vad_agent.TranscriptionAgent", autospec=True ) @@ -33,9 +31,9 @@ def per_transcription_agent(mocker): async def test_normal_setup(streaming, per_transcription_agent): """ Test that during normal setup, the VAD agent creates a Streaming behavior and creates audio - sockets, and starts the PerTranscriptionAgent without loading real models. + sockets, and starts the TranscriptionAgent without loading real models. """ - per_vad_agent = PerVADAgent("tcp://localhost:12345", False) + per_vad_agent = VADAgent("tcp://localhost:12345", False) per_vad_agent.add_behaviour = MagicMock() await per_vad_agent.setup() @@ -54,7 +52,7 @@ def test_in_socket_creation(zmq_context, do_bind: bool): Test that the VAD agent creates an audio input socket, differentiating between binding and connecting. """ - per_vad_agent = PerVADAgent(f"tcp://{'*' if do_bind else 'localhost'}:12345", do_bind) + per_vad_agent = VADAgent(f"tcp://{'*' if do_bind else 'localhost'}:12345", do_bind) per_vad_agent._connect_audio_in_socket() @@ -78,7 +76,7 @@ def test_out_socket_creation(zmq_context): """ Test that the VAD agent creates an audio output socket correctly. """ - per_vad_agent = PerVADAgent("tcp://localhost:12345", False) + per_vad_agent = VADAgent("tcp://localhost:12345", False) per_vad_agent._connect_audio_out_socket() @@ -97,7 +95,7 @@ async def test_out_socket_creation_failure(zmq_context): zmq_context.return_value.socket.return_value.bind_to_random_port.side_effect = ( zmq.ZMQBindError ) - per_vad_agent = PerVADAgent("tcp://localhost:12345", False) + per_vad_agent = VADAgent("tcp://localhost:12345", False) await per_vad_agent.setup() @@ -110,7 +108,7 @@ async def test_stop(zmq_context, per_transcription_agent): """ Test that when the VAD agent is stopped, the sockets are closed correctly. """ - per_vad_agent = PerVADAgent("tcp://localhost:12345", False) + per_vad_agent = VADAgent("tcp://localhost:12345", False) zmq_context.return_value.socket.return_value.bind_to_random_port.return_value = random.randint( 1000, 10000, diff --git a/test/integration/agents/per_agents/per_vad_agent/test_per_vad_with_audio.py b/test/integration/agents/perception/vad_agent/test_vad_with_audio.py similarity index 85% rename from test/integration/agents/per_agents/per_vad_agent/test_per_vad_with_audio.py rename to test/integration/agents/perception/vad_agent/test_vad_with_audio.py index 0198911..b197c31 100644 --- a/test/integration/agents/per_agents/per_vad_agent/test_per_vad_with_audio.py +++ b/test/integration/agents/perception/vad_agent/test_vad_with_audio.py @@ -5,7 +5,7 @@ import pytest import soundfile as sf import zmq -from control_backend.agents.per_agents.per_vad_agent import Streaming +from control_backend.agents.perception.vad_agent import StreamingBehaviour def get_audio_chunks() -> list[bytes]: @@ -42,14 +42,12 @@ async def test_real_audio(mocker): audio_in_socket = AsyncMock() audio_in_socket.recv.side_effect = audio_chunks - mock_poller: MagicMock = mocker.patch( - "control_backend.agents.per_agents.per_vad_agent.zmq.Poller" - ) + mock_poller: MagicMock = mocker.patch("control_backend.agents.perception.vad_agent.zmq.Poller") mock_poller.return_value.poll.return_value = [(audio_in_socket, zmq.POLLIN)] audio_out_socket = AsyncMock() - vad_streamer = Streaming(audio_in_socket, audio_out_socket) + vad_streamer = StreamingBehaviour(audio_in_socket, audio_out_socket) vad_streamer._ready = True vad_streamer.agent = MagicMock() for _ in audio_chunks: diff --git a/test/unit/agents/bdi_agents/bdi_core_agent/behaviours/test_belief_setter.py b/test/unit/agents/bdi/bdi_core_agent/behaviours/test_belief_setter.py similarity index 96% rename from test/unit/agents/bdi_agents/bdi_core_agent/behaviours/test_belief_setter.py rename to test/unit/agents/bdi/bdi_core_agent/behaviours/test_belief_setter.py index fa6b1de..53b991e 100644 --- a/test/unit/agents/bdi_agents/bdi_core_agent/behaviours/test_belief_setter.py +++ b/test/unit/agents/bdi/bdi_core_agent/behaviours/test_belief_setter.py @@ -4,12 +4,12 @@ from unittest.mock import AsyncMock, MagicMock, call import pytest -from control_backend.agents.bdi_agents.bdi_core_agent.behaviours.belief_setter_behaviour import ( +from control_backend.agents.bdi.bdi_core_agent.behaviours.belief_setter_behaviour import ( BeliefSetterBehaviour, ) # Define a constant for the collector agent name to use in tests -COLLECTOR_AGENT_NAME = "bdi_belief_collector_agent" +COLLECTOR_AGENT_NAME = "belief_collector_agent" COLLECTOR_AGENT_JID = f"{COLLECTOR_AGENT_NAME}@test" @@ -27,8 +27,8 @@ def belief_setter_behaviour(mock_agent, mocker): """Fixture to create an instance of BeliefSetterBehaviour with a mocked agent.""" # Patch the settings to use a predictable agent name mocker.patch( - "control_backend.agents.bdi_agents.bdi_core_agent." - "behaviours.belief_setter_behaviour.settings.agent_settings.bdi_belief_collector_agent_name", + "control_backend.agents.bdi.bdi_core_agent." + "behaviours.belief_setter_behaviour.settings.agent_settings.bdi_belief_collector_name", COLLECTOR_AGENT_NAME, ) diff --git a/test/unit/agents/bdi_agents/bdi_belief_collector_agent/behaviours/test_continuous_collect.py b/test/unit/agents/bdi/belief_collector_agent/behaviours/test_continuous_collect.py similarity index 93% rename from test/unit/agents/bdi_agents/bdi_belief_collector_agent/behaviours/test_continuous_collect.py rename to test/unit/agents/bdi/belief_collector_agent/behaviours/test_continuous_collect.py index 001262f..4cb5ba1 100644 --- a/test/unit/agents/bdi_agents/bdi_belief_collector_agent/behaviours/test_continuous_collect.py +++ b/test/unit/agents/bdi/belief_collector_agent/behaviours/test_continuous_collect.py @@ -3,8 +3,8 @@ from unittest.mock import AsyncMock, MagicMock import pytest -from control_backend.agents.bdi_agents.bdi_belief_collector_agent.behaviours.bel_collector_behaviour import ( # noqa: E501 - BelCollectorBehaviour, +from control_backend.agents.bdi.belief_collector_agent.behaviours.belief_collector_behaviour import ( # noqa: E501 + BeliefCollectorBehaviour, ) @@ -20,7 +20,7 @@ def create_mock_message(sender_node: str, body: str) -> MagicMock: def mock_agent(mocker): """Fixture to create a mock Agent.""" agent = MagicMock() - agent.jid = "bdi_belief_collector_agent@test" + agent.jid = "belief_collector_agent@test" return agent @@ -30,7 +30,7 @@ def bel_collector_behaviouror(mock_agent, mocker): # Patch asyncio.sleep to prevent tests from actually waiting mocker.patch("asyncio.sleep", return_value=None) - collector = BelCollectorBehaviour() + collector = BeliefCollectorBehaviour() collector.agent = mock_agent # Mock the receive method, we will control its return value in each test collector.receive = AsyncMock() diff --git a/test/unit/agents/bdi_agents/bdi_text_belief_agent/behaviours/test_belief_from_text.py b/test/unit/agents/bdi/text_belief_agent/behaviours/test_belief_from_text.py similarity index 87% rename from test/unit/agents/bdi_agents/bdi_text_belief_agent/behaviours/test_belief_from_text.py rename to test/unit/agents/bdi/text_belief_agent/behaviours/test_belief_from_text.py index 9c0021b..294f00d 100644 --- a/test/unit/agents/bdi_agents/bdi_text_belief_agent/behaviours/test_belief_from_text.py +++ b/test/unit/agents/bdi/text_belief_agent/behaviours/test_belief_from_text.py @@ -4,8 +4,8 @@ from unittest.mock import AsyncMock, MagicMock, patch import pytest from spade.message import Message -from control_backend.agents.bdi_agents.bdi_text_belief_agent.behaviours.bdi_text_belief_behaviour import ( # noqa: E501, We can't shorten this import. - BDITextBeliefBehaviour, +from control_backend.agents.bdi.text_belief_extractor_agent.behaviours.text_belief_extractor_behaviour import ( # noqa: E501, We can't shorten this import. + TextBeliefExtractorBehaviour, ) @@ -17,15 +17,16 @@ def mock_settings(): """ # Create a mock object that mimics the nested structure settings_mock = MagicMock() - settings_mock.agent_settings.per_transcription_agent_name = "transcriber" - settings_mock.agent_settings.bdi_belief_collector_agent_name = "collector" + settings_mock.agent_settings.transcription_name = "transcriber" + settings_mock.agent_settings.bdi_belief_collector_name = "collector" settings_mock.agent_settings.host = "fake.host" # Use patch to replace the settings object during the test # Adjust 'control_backend.behaviours.belief_from_text.settings' to where # your behaviour file imports it from. with patch( - "control_backend.agents.bdi_agents.bdi_text_belief_agent.behaviours.bdi_text_belief_behaviour.settings", + "control_backend.agents.bdi.text_belief_extractor_agent.behaviours" + ".text_belief_extractor_behaviour.settings", settings_mock, ): yield settings_mock @@ -37,7 +38,7 @@ def behavior(mock_settings): Creates an instance of the BDITextBeliefBehaviour behaviour and mocks its agent, logger, send, and receive methods. """ - b = BDITextBeliefBehaviour() + b = TextBeliefExtractorBehaviour() b.agent = MagicMock() b.send = AsyncMock() @@ -103,7 +104,7 @@ async def test_run_message_from_transcriber_demo(behavior, mock_settings, monkey # Arrange: Create a mock message from the transcriber transcription_text = "hello world" mock_msg = create_mock_message( - mock_settings.agent_settings.per_transcription_agent_name, transcription_text, None + mock_settings.agent_settings.transcription_name, transcription_text, None ) behavior.receive.return_value = mock_msg @@ -122,7 +123,7 @@ async def test_run_message_from_transcriber_demo(behavior, mock_settings, monkey assert ( sent_msg.to - == mock_settings.agent_settings.bdi_belief_collector_agent_name + == mock_settings.agent_settings.bdi_belief_collector_name + "@" + mock_settings.agent_settings.host ) @@ -162,7 +163,7 @@ async def test_process_transcription_success(behavior, mock_settings): # 2. Inspect the sent message sent_msg: Message = behavior.send.call_args[0][0] expected_to = ( - mock_settings.agent_settings.bdi_belief_collector_agent_name + mock_settings.agent_settings.bdi_belief_collector_name + "@" + mock_settings.agent_settings.host ) diff --git a/test/unit/agents/per_agents/per_transcription_agent/test_speech_recognizer.py b/test/unit/agents/perception/transcription_agent/test_speech_recognizer.py similarity index 93% rename from test/unit/agents/per_agents/per_transcription_agent/test_speech_recognizer.py rename to test/unit/agents/perception/transcription_agent/test_speech_recognizer.py index 347233a..d0b8df6 100644 --- a/test/unit/agents/per_agents/per_transcription_agent/test_speech_recognizer.py +++ b/test/unit/agents/perception/transcription_agent/test_speech_recognizer.py @@ -1,6 +1,6 @@ import numpy as np -from control_backend.agents.per_agents.per_transcription_agent.speech_recognizer import ( +from control_backend.agents.perception.transcription_agent.speech_recognizer import ( OpenAIWhisperSpeechRecognizer, SpeechRecognizer, ) diff --git a/test/unit/agents/per_agents/per_vad_agent/test_vad_socket_poller.py b/test/unit/agents/perception/vad_agent/test_vad_socket_poller.py similarity index 76% rename from test/unit/agents/per_agents/per_vad_agent/test_vad_socket_poller.py rename to test/unit/agents/perception/vad_agent/test_vad_socket_poller.py index d0c2fc5..6ac074f 100644 --- a/test/unit/agents/per_agents/per_vad_agent/test_vad_socket_poller.py +++ b/test/unit/agents/perception/vad_agent/test_vad_socket_poller.py @@ -3,7 +3,7 @@ from unittest.mock import AsyncMock, MagicMock import pytest import zmq -from control_backend.agents.per_agents.per_vad_agent import SocketPoller +from control_backend.agents.perception.vad_agent import SocketPoller @pytest.fixture @@ -16,9 +16,7 @@ async def test_socket_poller_with_data(socket, mocker): socket_data = b"test" socket.recv.return_value = socket_data - mock_poller: MagicMock = mocker.patch( - "control_backend.agents.per_agents.per_vad_agent.zmq.Poller" - ) + mock_poller: MagicMock = mocker.patch("control_backend.agents.perception.vad_agent.zmq.Poller") mock_poller.return_value.poll.return_value = [(socket, zmq.POLLIN)] poller = SocketPoller(socket) @@ -37,9 +35,7 @@ async def test_socket_poller_with_data(socket, mocker): @pytest.mark.asyncio async def test_socket_poller_no_data(socket, mocker): - mock_poller: MagicMock = mocker.patch( - "control_backend.agents.per_agents.per_vad_agent.zmq.Poller" - ) + mock_poller: MagicMock = mocker.patch("control_backend.agents.perception.vad_agent.zmq.Poller") mock_poller.return_value.poll.return_value = [] poller = SocketPoller(socket) diff --git a/test/unit/agents/per_agents/per_vad_agent/test_vad_streaming.py b/test/unit/agents/perception/vad_agent/test_vad_streaming.py similarity index 94% rename from test/unit/agents/per_agents/per_vad_agent/test_vad_streaming.py rename to test/unit/agents/perception/vad_agent/test_vad_streaming.py index 1c35c9f..de488ff 100644 --- a/test/unit/agents/per_agents/per_vad_agent/test_vad_streaming.py +++ b/test/unit/agents/perception/vad_agent/test_vad_streaming.py @@ -3,7 +3,7 @@ from unittest.mock import AsyncMock, MagicMock import numpy as np import pytest -from control_backend.agents.per_agents.per_vad_agent import Streaming +from control_backend.agents.perception.vad_agent import StreamingBehaviour @pytest.fixture @@ -20,7 +20,7 @@ def audio_out_socket(): def mock_agent(mocker): """Fixture to create a mock BDIAgent.""" agent = MagicMock() - agent.jid = "per_vad_agent@test" + agent.jid = "vad_agent@test" return agent @@ -29,7 +29,7 @@ def streaming(audio_in_socket, audio_out_socket, mock_agent): import torch torch.hub.load.return_value = (..., ...) # Mock - streaming = Streaming(audio_in_socket, audio_out_socket) + streaming = StreamingBehaviour(audio_in_socket, audio_out_socket) streaming._ready = True streaming.agent = mock_agent return streaming From 5f3d290fb6b61214273d1dfd82075b4f770e2fe4 Mon Sep 17 00:00:00 2001 From: Twirre Meulenbelt <43213592+TwirreM@users.noreply.github.com> Date: Wed, 19 Nov 2025 16:29:13 +0100 Subject: [PATCH 26/26] fix: use the correct name in the transcription agent ref: N25B-257 --- .../perception/transcription_agent/transcription_agent.py | 2 +- src/control_backend/main.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/control_backend/agents/perception/transcription_agent/transcription_agent.py b/src/control_backend/agents/perception/transcription_agent/transcription_agent.py index 8da1721..d6c1207 100644 --- a/src/control_backend/agents/perception/transcription_agent/transcription_agent.py +++ b/src/control_backend/agents/perception/transcription_agent/transcription_agent.py @@ -43,7 +43,7 @@ class TranscriptionAgent(BaseAgent): async def _share_transcription(self, transcription: str): """Share a transcription to the other agents that depend on it.""" receiver_jids = [ - settings.agent_settings.texbdi_text_belief_agent_name + settings.agent_settings.text_belief_extractor_name + "@" + settings.agent_settings.host, ] # Set message receivers here diff --git a/src/control_backend/main.py b/src/control_backend/main.py index 20c8482..5a38f39 100644 --- a/src/control_backend/main.py +++ b/src/control_backend/main.py @@ -76,7 +76,7 @@ async def lifespan(app: FastAPI): # --- Initialize Agents --- logger.info("Initializing and starting agents.") agents_to_start = { - "ComRIAgent": ( + "RICommunicationAgent": ( RICommunicationAgent, { "name": settings.agent_settings.ri_communication_name, @@ -104,7 +104,7 @@ async def lifespan(app: FastAPI): "asl": "src/control_backend/agents/bdi/bdi_core_agent/rules.asl", }, ), - "BDIBeliefCollectorAgent": ( + "BeliefCollectorAgent": ( BDIBeliefCollectorAgent, { "name": settings.agent_settings.bdi_belief_collector_name, @@ -113,7 +113,7 @@ async def lifespan(app: FastAPI): "password": settings.agent_settings.bdi_belief_collector_name, }, ), - "BDITextBeliefAgent": ( + "TextBeliefExtractorAgent": ( TextBeliefExtractorAgent, { "name": settings.agent_settings.text_belief_extractor_name, @@ -122,7 +122,7 @@ async def lifespan(app: FastAPI): "password": settings.agent_settings.text_belief_extractor_name, }, ), - "PerVADAgent": ( + "VADAgent": ( VADAgent, {"audio_in_address": "tcp://localhost:5558", "audio_in_bind": False}, ),