faint-gold-41196
05/12/2025, 12:36 PMtall-airline-16756
05/12/2025, 12:36 PMtall-airline-16756
05/12/2025, 12:36 PMfaint-gold-41196
05/12/2025, 6:34 PMtall-airline-16756
05/12/2025, 6:35 PMimport asyncio
import time
SILENCE_THRESHOLD = 5 # seconds
async def entrypoint(ctx: JobContext):
user_last_spoke_time = time.time()
monitor_task = None
async def monitor_interaction():
while True:
if time.time() - user_last_spoke_time > SILENCE_THRESHOLD:
<http://logger.info|logger.info>("silent for too long! disconnecting")
try:
await ctx.room.disconnect()
except Exception as e:
logger.exception("Error while ending call")
else:
logger.trace("silence is not enough to disconnect")
await asyncio.sleep(1)
@agent.on("user_started_speaking")
def on_user_started_speaking(_msg: llm.ChatMessage):
user_last_spoke_time = time.time()
monitor_task = asyncio.create_task(monitor_interaction())
Key Points:
• SILENCE_THRESHOLD: This is the duration (in seconds) of silence after which the call will be terminated. You can adjust this value based on your requirements.
• Monitoring Task: A background task continuously checks the time elapsed since the user last spoke. If the elapsed time exceeds the threshold, the call is disconnected.
• Event Listener: The system listens for events like "user_started_speaking" to reset the silence timer whenever the user speaks.
This approach is particularly useful for scenarios like voicemail detection or ensuring efficient resource usage in automated systems. Let me know if you need further clarification or help with implementation!kind-branch-59377
05/13/2025, 4:00 AMtall-airline-16756
05/13/2025, 4:00 AMuser_started_speaking
event is no longer available as a separate event. Instead, it has been combined into a single user_state_changed
event, which encompasses both speaking and other state changes.
This change is part of the state change events overhaul introduced in version 1.0. Let me know if you need guidance on adapting to this updated event structure!
Sources: Agents 0.x migration guide | LiveKit Docskind-branch-59377
05/13/2025, 4:02 AMtall-airline-16756
05/13/2025, 4:02 AMfaint-gold-41196
05/13/2025, 4:53 AM