We have voice chat agents that should persist when...
# agents
g
We have voice chat agents that should persist when users have network hiccups or refresh the browser, but currently agents die on room disconnection. Manual exit/rejoin works perfectly with close_on_disconnect=False, but browser refresh causes the agent to exit with 'room disconnected'. What's the recommended pattern for maintaining agent persistence across network interruptions while still cleaning up properly when the session truly ends?
r
To maintain agent persistence across network interruptions while ensuring proper cleanup when the session truly ends, you can utilize the
close_on_disconnect
option in your agent configuration. Setting
close_on_disconnect=False
allows the agent to remain active even when a participant disconnects, such as during a browser refresh. This way, the agent can continue to respond to commands from the participant when they rejoin the room. Here’s a recommended approach: 1. Set `close_on_disconnect=False`: This configuration prevents the agent from shutting down immediately when a participant disconnects. Instead, it allows the agent to remain in the room and wait for the participant to reconnect. 2. Implement a Timeout Mechanism: You can implement a timeout mechanism to clean up the agent session after a certain period if the participant does not reconnect. This can be done by setting a job shutdown timeout that specifies how long the agent should wait before shutting down after a disconnect. 3. Handle Participant Reconnection: When a participant reconnects, the agent should be able to recognize them and continue the session without losing context. This can be managed by checking the participant's identity and ensuring that the agent can respond to their commands. 4. Log Disconnect Reasons: Utilize the disconnect reason to determine if the disconnection was intentional (e.g., user calling
room.disconnect()
) or unintentional (e.g., a browser refresh). This can help in deciding whether to keep the agent active or to shut it down. Here is an example of how you might implement this in your agent code:
Copy code
async def entrypoint(ctx: JobContext):
    # Connect to the room with auto_subscribe set to AUDIO_ONLY
    await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)

    # Wait for the first participant to arrive
    participant = await ctx.wait_for_participant()

    # Customize behavior based on the participant
    print(f"connected to room {ctx.room.name} with participant {participant.identity}")

    # Listen for participant attributes changes
    @ctx.room.on("participant_attributes_changed")
    def on_participant_attributes_changed(changed_attrs: dict[str, str], p: rtc.Participant):
        if p == participant:
            language = p.attributes.get("user.language")
            print(f"participant {p.identity} changed language to {language}")

    # Implement a timeout for cleanup
    await asyncio.sleep(300)  # Example timeout for cleanup
    ctx.shutdown('Session ended')
By following this pattern, you can ensure that your agents remain responsive during network interruptions while also providing a mechanism for proper cleanup when the session truly ends. Notes: - The
close_on_disconnect
option is crucial for managing agent behavior during participant disconnections. - Implementing a timeout for cleanup helps prevent resource leaks while allowing for participant reconnections. - Ensure that your agent can handle reconnections gracefully to maintain the user experience.