kind-iron-94532
08/05/2025, 11:15 AMtall-airline-16756
08/05/2025, 11:15 AMtall-airline-16756
08/05/2025, 11:16 AMSILENCE_THRESHOLD = 5 # seconds
async def monitor_interaction():
while True:
if time.time() - user_last_spoke_time > SILENCE_THRESHOLD:
<http://logger.info|logger.info>("Silence exceeded threshold. Disconnecting call.")
try:
await ctx.room.disconnect()
except Exception as e:
logger.exception("Error disconnecting call")
await asyncio.sleep(1)
2. Track User Speech: Update the silence timer whenever the user starts speaking: @agent.on("user_started_speaking")
def on_user_started_speaking(_msg):
global user_last_spoke_time
user_last_spoke_time = time.time()
3. Start Monitoring: Run the silence detection coroutine alongside your main execution: monitor_task = asyncio.create_task(monitor_interaction())
4. Clean Up: Ensure the monitoring task is properly cleaned up when the session shuts down: async def on_shutdown():
<http://logger.info|logger.info>("Shutting down session")
if monitor_task:
monitor_task.cancel()
ctx.add_shutdown_callback(on_shutdown)
Adjust the silence threshold and implement proper error handling as needed. Let me know if you need further assistance with this implementation!
Sources: SIP Integration | livekit/livekit_composite | DeepWikikind-iron-94532
08/05/2025, 11:20 AMtall-airline-16756
08/05/2025, 11:21 AM