diff --git a/src/control_backend/agents/user_interrupt/user_interrupt_agent.py b/src/control_backend/agents/user_interrupt/user_interrupt_agent.py index 28ddeca..4dee823 100644 --- a/src/control_backend/agents/user_interrupt/user_interrupt_agent.py +++ b/src/control_backend/agents/user_interrupt/user_interrupt_agent.py @@ -8,7 +8,12 @@ from control_backend.agents.bdi.agentspeak_generator import AgentSpeakGenerator from control_backend.core.agent_system import InternalMessage from control_backend.core.config import settings from control_backend.schemas.program import ConditionalNorm, Program -from control_backend.schemas.ri_message import GestureCommand, RIEndpoint, SpeechCommand +from control_backend.schemas.ri_message import ( + GestureCommand, + PauseCommand, + RIEndpoint, + SpeechCommand, +) class UserInterruptAgent(BaseAgent): @@ -70,6 +75,9 @@ class UserInterruptAgent(BaseAgent): - type: "speech", context: string that the robot has to say. - type: "gesture", context: single gesture name that the robot has to perform. - type: "override", context: belief_id that overrides the goal/trigger/conditional norm. + - type: "pause", context: boolean indicating whether to pause + - type: "reset_phase", context: None, indicates to the BDI Core to + - type: "reset_experiment", context: None, indicates to the BDI Core to """ while True: topic, body = await self.sub_socket.recv_multipart() @@ -112,6 +120,18 @@ class UserInterruptAgent(BaseAgent): ) else: self.logger.warning("Could not determine which element to override.") + elif event_type == "pause": + self.logger.debug( + "Received pause/resume button press with context '%s'.", event_context + ) + await self._send_pause_command(event_context) + if event_context: + self.logger.info("Sent pause command.") + else: + self.logger.info("Sent resume command.") + + elif event_type in ["next_phase", "reset_phase", "reset_experiment"]: + await self._send_experiment_control_to_bdi_core(event_type) else: self.logger.warning( "Received button press with unknown type '%s' (context: '%s').", @@ -271,3 +291,65 @@ class UserInterruptAgent(BaseAgent): msg = InternalMessage(to=settings.agent_settings.bdi_core_name, thread=thread, body=body) await self.send(msg) self.logger.info(f"Directly forced {thread} in BDI: {body}") + + async def _send_experiment_control_to_bdi_core(self, type): + """ + method to send experiment control buttons to bdi core. + + :param type: the type of control button we should send to the bdi core. + """ + # Switch which thread we should send to bdi core + thread = "" + match type: + case "next_phase": + thread = "force_next_phase" + case "reset_phase": + thread = "reset_current_phase" + case "reset_experiment": + thread = "reset_experiment" + case _: + self.logger.warning( + "Received unknown experiment control type '%s' to send to BDI Core.", + type, + ) + + out_msg = InternalMessage( + to=settings.agent_settings.bdi_core_name, + sender=self.name, + thread=thread, + body="", + ) + self.logger.debug("Sending experiment control '%s' to BDI Core.", thread) + await self.send(out_msg) + + async def _send_pause_command(self, pause): + """ + Send a pause command to the Robot Interface via the RI Communication Agent. + Send a pause command to the other internal agents; for now just VAD agent. + """ + cmd = PauseCommand(data=pause) + message = InternalMessage( + to=settings.agent_settings.ri_communication_name, + sender=self.name, + body=cmd.model_dump_json(), + ) + await self.send(message) + + if pause == "true": + # Send pause to VAD agent + vad_message = InternalMessage( + to=settings.agent_settings.vad_name, + sender=self.name, + body="PAUSE", + ) + await self.send(vad_message) + self.logger.info("Sent pause command to VAD Agent and RI Communication Agent.") + else: + # Send resume to VAD agent + vad_message = InternalMessage( + to=settings.agent_settings.vad_name, + sender=self.name, + body="RESUME", + ) + await self.send(vad_message) + self.logger.info("Sent resume command to VAD Agent and RI Communication Agent.")