
    qi              
           d Z ddlZddlZddlmZ ddlmZ ddlmZ de	de	de	d	e	d
e	f
dZ
de	de	de	d	e	d
e	f
dZd Zddeej                     fdZy)a  LiveKit room and token configuration utilities.

This module provides helper functions for creating and configuring LiveKit
rooms and authentication tokens. It handles JWT token generation with
appropriate grants for both regular participants and AI agents.

The module supports creating tokens for development and testing, with
automatic agent detection for proper room permissions.

Required environment variables:

- LIVEKIT_API_KEY - LiveKit API key
- LIVEKIT_API_SECRET - LiveKit API secret
- LIVEKIT_URL - LiveKit server URL
- LIVEKIT_ROOM_NAME - Room name to join

Example::

    from pipecat.runner.livekit import configure

    url, token, room_name = await configure()
    # Use with LiveKitTransport
    N)Optional)api)logger	room_nameparticipant_nameapi_key
api_secretreturnc                     t        j                  ||      }|j                  |      j                  |      j	                  t        j
                  d|              |j                         S )a$  Generate a LiveKit access token for a participant.

    Args:
        room_name: Name of the LiveKit room.
        participant_name: Name of the participant.
        api_key: LiveKit API key.
        api_secret: LiveKit API secret.

    Returns:
        JWT token string for room access.
    T)	room_joinroomr   AccessTokenwith_identity	with_namewith_grantsVideoGrantsto_jwtr   r   r   r	   tokens        H/opt/pipecat/venv/lib/python3.12/site-packages/pipecat/runner/livekit.pygenerate_tokenr   '   sZ     OOGZ0E	()334DEQQ	
 <<>    c                     t        j                  ||      }|j                  |      j                  |      j	                  t        j
                  d| d             |j                         S )a1  Generate a LiveKit access token for an agent participant.

    Args:
        room_name: Name of the LiveKit room.
        participant_name: Name of the participant.
        api_key: LiveKit API key.
        api_secret: LiveKit API secret.

    Returns:
        JWT token string for agent room access.
    T)r   r   agentr   r   s        r   generate_token_with_agentr   >   s]     OOGZ0E	()334DEQQ	
 <<>r   c                  F   K   t                d{   \  } }}}| ||fS 7 w)zConfigure LiveKit room URL and token from arguments or environment.

    Returns:
        Tuple containing the server URL, authentication token, and room name.

    Raises:
        Exception: If required LiveKit configuration is not provided.
    N)configure_with_args)urlr   r   _s       r   	configurer!   X   s.      (;'<!<S%A	"" "=s   !!parserc                 n  K   | st        j                  d      } | j                  ddt        dd       | j                  dd	t        dd
       | j	                         \  }}|j
                  xs t        j                  d      }|j                  xs t        j                  d      }t        j                  d      }t        j                  d      }|st        d      |st        d      |r|st        d      t        |d||      }t        |d||      }t        j                  d|        ||||fS w)an  Configure LiveKit room with command-line argument parsing.

    Args:
        parser: Optional argument parser. If None, creates a default one.

    Returns:
        Tuple containing server URL, authentication token, room name, and parsed arguments.

    Raises:
        Exception: If required LiveKit configuration is not provided via arguments or environment.
    zLiveKit AI SDK Bot Sample)descriptionz-rz--roomFz Name of the LiveKit room to join)typerequiredhelpz-uz--urlzURL of the LiveKit serverLIVEKIT_ROOM_NAMELIVEKIT_URLLIVEKIT_API_KEYLIVEKIT_API_SECRETzxNo LiveKit room specified. Use the -r/--room option from the command line, or set LIVEKIT_ROOM_NAME in your environment.zwNo LiveKit server URL specified. Use the -u/--url option from the command line, or set LIVEKIT_URL in your environment.zLLIVEKIT_API_KEY and LIVEKIT_API_SECRET must be set in environment variables.zPipecat AgentUserzUser token: )argparseArgumentParseradd_argumentstrparse_known_argsr   osgetenvr   	Exceptionr   r   r   info)	r"   argsunknownr   r   r   r	   r   
user_tokens	            r   r   r   e   sA     ((5PQ
hS57Y   gC%Fab++-MD'		;RYY':;I
((
.bii.Cii)*G/0J G
 	
  F
 	
 *Z
 	
 &i':VE  	67JGJ
KK,zl+,	4((s   D3D5)N)__doc__r-   r2   typingr   livekitr   logurur   r0   r   r   r!   r.   r    r   r   <module>r>      s   0  	   c S 3 TW \_ .&)47EH4
#/)hx/F/F&G /)r   