From 3a9ac190372de1b95eb6b9c17d076eddbb26280d Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Wed, 20 Nov 2024 20:15:41 -0600 Subject: [PATCH] Adds code to have better control over the agent. Now, you can pause so you won't get interrupted while talking to other channel members, resume functionality, and set the participant the agent should actively listen to. --- .../agents/multimodal/multimodal_agent.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/livekit-agents/livekit/agents/multimodal/multimodal_agent.py b/livekit-agents/livekit/agents/multimodal/multimodal_agent.py index d694100a0..a730d7567 100644 --- a/livekit-agents/livekit/agents/multimodal/multimodal_agent.py +++ b/livekit-agents/livekit/agents/multimodal/multimodal_agent.py @@ -326,6 +326,25 @@ def _on_playout_stopped(interrupted: bool) -> None: for f in bstream.write(frame.data.tobytes()): self._session.input_audio_buffer.append(f) + def set_source_participant(self, participant_identity: str) -> None: + """Set the source participant for the agent.""" + self._link_participant(participant_identity) + + def pause_listening(self) -> None: + """Pause listening to the audio stream.""" + if self._read_micro_atask is not None: + self._read_micro_atask.cancel() # Stop the current task + self._read_micro_atask = None + logger.info("Paused listening to the audio stream.") + + def resume_listening(self) -> None: + """Resume listening to the audio stream.""" + if self._subscribed_track is not None and self._read_micro_atask is None: + self._read_micro_atask = asyncio.create_task( + self._micro_task(self._subscribed_track) # Restart the task + ) + logger.info("Resumed listening to the audio stream.") + def _on_participant_connected(self, participant: rtc.RemoteParticipant): if self._linked_participant is None: return