Preventing HeyGenVideoService from disconnecting.

This commit is contained in:
Filipi Fuchter
2025-11-11 11:37:46 -03:00
parent 54e8d29615
commit 62caadfc7c
2 changed files with 13 additions and 2 deletions

View File

@@ -83,6 +83,7 @@ class HeyGenClient:
version="v2",
),
callbacks: HeyGenCallbacks,
connect_as_user: bool = False,
) -> None:
"""Initialize the HeyGen client.
@@ -92,6 +93,7 @@ class HeyGenClient:
params: Transport configuration parameters
session_request: Configuration for the HeyGen session (default: uses Shawn_Therapist_public avatar)
callbacks: Callback handlers for HeyGen events
connect_as_user: Whether to connect using the user token or not (default: False)
"""
self._api = HeyGenApi(api_key, session=session)
self._heyGen_session: Optional[HeyGenSession] = None
@@ -119,6 +121,11 @@ class HeyGenClient:
self._next_send_time = 0
self._audio_seconds_sent = 0.0
self._transport_ready = False
# HeyGen enforces a protection mechanism that will automatically disconnect the avatar if a user does not join within 5 minutes,
# regardless of whether the Pipecat agent remains present in the room.
# To prevent unexpected disconnections in HeyGenVideoService, we ensure that a user connection is established using the user's token.
# This keeps the avatar session active and avoids forced logouts due to inactivity from the user side.
self._connect_as_user = connect_as_user
async def _initialize(self):
self._heyGen_session = await self._api.new_session(self._session_request)
@@ -562,9 +569,12 @@ class HeyGenClient:
self._callbacks.on_participant_disconnected, participant.identity
)
await self._livekit_room.connect(
self._heyGen_session.url, self._heyGen_session.livekit_agent_token
access_token = (
self._heyGen_session.livekit_agent_token
if not self._connect_as_user
else self._heyGen_session.access_token
)
await self._livekit_room.connect(self._heyGen_session.url, access_token)
logger.debug(f"Successfully connected to LiveKit room: {self._livekit_room.name}")
logger.debug(f"Local participant SID: {self._livekit_room.local_participant.sid}")
logger.debug(

View File

@@ -121,6 +121,7 @@ class HeyGenVideoService(AIService):
on_participant_connected=self._on_participant_connected,
on_participant_disconnected=self._on_participant_disconnected,
),
connect_as_user=True,
)
await self._client.setup(setup)