Skip to content

Liveavatar plugin stop session api call on cleanup #4193

@MSameerAbbas

Description

@MSameerAbbas

Feature Type

Nice to have

Feature Description

Can we please call stop session in the cleanup of liveavatar's plugin. Right now we're disconnecting from the websocket but the session stays alive until the keepalive timer expires which is a waste of credits.

Workarounds / Alternatives

Add this to api.py

async def stop_streaming_session(self, session_id: str, session_token: str) -> dict[str, Any]:
    """Stop the streaming session"""
    payload = {"session_id": self._session_id, "reason": "USER_DISCONNECTED"} # Session id may not be required according to docs if we're using token. Required if we're using api key
    headers = {"content-type": "application/json", "Authorization": f"Bearer {self._session_token}"}
    response_data = await self._post(endpoint="/stop", payload=payload, headers=headers)
    return response_data

Then update the 'finally' block in '_main_task' in 'avatar.py'

finally:   
    await utils.aio.cancel_and_wait(*io_tasks)
    await utils.aio.cancel_and_wait(*self._tasks)
    await self._audio_buffer.aclose()
    await ws_conn.close()
    # Stop the LiveAvatar session via API
    try:
        if hasattr(self, '_session_id') and hasattr(self, '_session_token'):
            await self._api.stop_streaming_session(self._session_id, self._session_token)
            logger.info(f"LiveAvatar session stopped: {self._session_id}")
    except Exception as e:
        logger.warning(f"Failed to stop LiveAvatar session: {e}", exc_info=True)

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions