bulky-butcher-96683
02/24/2025, 11:15 AMgray-monkey-83525
02/24/2025, 11:15 AMuser_stopped_speaking
event to start the timer and the user_started_speaking
event to reset it. If the timer reaches 30 seconds, you can call the disconnect
method to make the agent leave the room.
Here's a basic outline of how you might implement this:
1. Start Timer on Silence: Use the user_stopped_speaking
event to start a 30-second timer.
2. Reset Timer on Speech: Use the user_started_speaking
event to reset the timer.
3. Disconnect on Timeout: If the timer completes without being reset, call the disconnect
method to make the agent leave the room.
Would you like more detailed guidance or a code example?
Sources:
- https://livekit-users.slack.com/archives/C07FVFM5NA1/p1731495431.174829 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.bulky-butcher-96683
02/24/2025, 11:17 AMgray-monkey-83525
02/24/2025, 11:17 AMimport asyncio
from livekit.agents import VoicePipelineAgent
from livekit import rtc
class SilenceDetector:
def __init__(self, agent, room, participant, timeout=30):
self.agent = agent
self.room = room
self.participant = participant
self.timeout = timeout
self.silence_task = None
def start(self):
self.agent.on("user_stopped_speaking", self.on_user_stopped_speaking)
self.agent.on("user_started_speaking", self.on_user_started_speaking)
def on_user_stopped_speaking(self, *args):
self.reset_silence_timer()
def on_user_started_speaking(self, *args):
self.cancel_silence_timer()
def reset_silence_timer(self):
self.cancel_silence_timer()
self.silence_task = asyncio.create_task(self.silence_timeout())
def cancel_silence_timer(self):
if self.silence_task and not self.silence_task.done():
self.silence_task.cancel()
async def silence_timeout(self):
try:
await asyncio.sleep(self.timeout)
await self.room.disconnect()
print("Disconnected due to 30 seconds of silence.")
except asyncio.CancelledError:
pass
async def main():
# Initialize your room and participant here
room = rtc.Room()
participant = rtc.Participant()
agent = VoicePipelineAgent()
silence_detector = SilenceDetector(agent, room, participant)
silence_detector.start()
# Keep the application running
await asyncio.Event().wait()
# Run the main function
asyncio.run(main())
This code sets up a SilenceDetector
class that listens for user_stopped_speaking
and user_started_speaking
events. It starts a timer when the user stops speaking and cancels it if the user starts speaking again. If the timer reaches 30 seconds, the agent disconnects from the room.
Sources:
- https://livekit-users.slack.com/archives/C07FY8WHGPM/p1732026501.742139
- https://livekit-users.slack.com/archives/C07FY8WHGPM/p1730325243.151429 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.bulky-butcher-96683
02/25/2025, 7:38 AMgray-monkey-83525
02/25/2025, 7:38 AMdisconnect
method on the room object. This can be done under a specific condition or event in your application. Here's a simple example using Python:
import asyncio
from livekit import rtc
async def trigger_agent_leave(room):
# Manually disconnect the agent from the room
await room.disconnect()
print("Agent has left the room.")
async def main():
# Initialize your room here
room = rtc.Room()
# Trigger the agent to leave the room manually
await trigger_agent_leave(room)
# Run the main function
asyncio.run(main())
This code snippet demonstrates how to manually disconnect an agent from a room by calling the disconnect
method on the room
object. You can place this call in any part of your application where you need the agent to leave the room.
Would you like more detailed guidance or further examples?
Sources:
- Room.disconnect
- Room.disconnect 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.