
    qi:                        d Z ddlZddlmZmZmZmZmZ ddlm	Z	 ddl
Z
ddlmZmZmZmZ  G d de      Z G d d	e      Z G d
 de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d d      Zy)zoDaily REST Helpers.

Methods that wrap the Daily API to create rooms, check room URLs, and get meeting tokens.
    N)AnyDictListLiteralOptional)urlparse)	BaseModel
ConfigDictFieldValidationErrorc                       e Zd ZU dZdZeed<   dZeed<   dZ	eed<   dZ
eed	<   d
Zeeeee   f      ed<   d
Zee   ed<   y
)DailyRoomSipParamsa  SIP configuration parameters for Daily rooms.

    Parameters:
        display_name: Name shown for the SIP endpoint.
        video: Whether video is enabled for SIP.
        sip_mode: SIP connection mode, typically 'dial-in'.
        num_endpoints: Number of allowed SIP endpoints.
        codecs: Codecs to support for audio and video. If None, uses Daily defaults.
            Example: {"audio": ["OPUS"], "video": ["H264"]}
        provider: Optional SIP provider name (defaults to None).
            Example: "daily"
    zsw-sip-dialindisplay_nameFvideozdial-insip_mode   num_endpointsNcodecsprovider)__name__
__module____qualname____doc__r   str__annotations__r   boolr   r   intr   r   r   r   r        P/opt/pipecat/venv/lib/python3.12/site-packages/pipecat/transports/daily/utils.pyr   r      s[     (L#'E4HcM3-1FHT#tCy.)*1"Hhsm"r   r   c                   >    e Zd ZU dZeed<   eed<   eed<   dZeed<   y)RecordingsBucketConfiga  Configuration for storing Daily recordings in a custom S3 bucket.

    Refer to the Daily API documentation for more information:
    https://docs.daily.co/guides/products/live-streaming-recording/storing-recordings-in-a-custom-s3-bucket

    Parameters:
        bucket_name: Name of the S3 bucket for storing recordings.
        bucket_region: AWS region where the S3 bucket is located.
        assume_role_arn: ARN of the IAM role to assume for S3 access.
        allow_api_access: Whether to allow API access to the recordings.
    bucket_namebucket_regionassume_role_arnFallow_api_accessNr   r   r   r   r   r   r&   r   r   r   r    r"   r"   *   &    
 "d"r   r"   c                   >    e Zd ZU dZeed<   eed<   eed<   dZeed<   y)TranscriptionBucketConfiga   Configuration for storing Daily transcription in a custom S3 bucket.

    Refer to the Daily API documentation for more information:
    https://docs.daily.co/guides/products/live-streaming-recording/storing-recordings-in-a-custom-s3-bucket

    Parameters:
        bucket_name: Name of the S3 bucket for storing transcription.
        bucket_region: AWS region where the S3 bucket is located.
        assume_role_arn: ARN of the IAM role to assume for S3 access.
        allow_api_access: Whether to allow API access to the transcription.
    r#   r$   r%   Fr&   Nr'   r   r   r    r*   r*   =   r(   r   r*   c                   X   e Zd ZU dZ ed      ZdZee   e	d<   dZ
ee	d<   dZee	d<   dZee	d	<   dZee	d
<   dZee   e	d<   dZeed      e	d<   dZee   e	d<   dZee   e	d<   dZee   e	d<   dZee   e	d<   dZee   e	d<   dZee   e	d<   dZeeeef      e	d<   dZ ee	d<   e!defd       Z"y)DailyRoomPropertiesa  Properties for configuring a Daily room.

    Reference: https://docs.daily.co/reference/rest-api/rooms/create-room#properties

    Parameters:
        exp: Optional Unix epoch timestamp for room expiration (e.g., time.time() + 300 for 5 minutes).
        enable_chat: Whether chat is enabled in the room.
        enable_prejoin_ui: Whether the pre-join UI is enabled.
        enable_emoji_reactions: Whether emoji reactions are enabled.
        eject_at_room_exp: Whether to remove participants when room expires.
        enable_dialout: Whether SIP dial-out is enabled.
        enable_recording: Recording settings ('cloud', 'local', 'raw-tracks').
        enable_transcription_storage: Whether transcription storage is enabled.
        geo: Geographic region for room.
        max_participants: Maximum number of participants allowed in the room.
        recordings_bucket: Configuration for custom S3 bucket recordings.
        transcription_bucket: Configuration for custom S3 bucket transcription.
        sip: SIP configuration parameters.
        sip_uri: SIP URI information returned by Daily.
        start_video_off: Whether video is off by default.
    allow)extraNexpFenable_chatenable_prejoin_uienable_emoji_reactionseject_at_room_expenable_dialoutcloudzcloud-audio-onlylocalz
raw-tracksenable_recordingenable_transcription_storagegeomax_participantsrecordings_buckettranscription_bucketsipsip_uristart_video_offreturnc                 @    | j                   syd| j                   d   z  S )zGet the SIP endpoint URI if available.

        Returns:
            SIP endpoint URI or empty string if not available.
         zsip:%sendpoint)r?   )selfs    r    sip_endpointz DailyRoomProperties.sip_endpointy   s"     ||dll:666r   )#r   r   r   r   r
   model_configr/   r   floatr   r0   r   r1   r2   r3   r4   r8   r   r9   r:   r   r;   r   r<   r"   r=   r*   r>   r   r?   r   r   r@   propertyrF   r   r   r    r,   r,   P   s   , G,LC%K#t##(D(#t#%)NHTN)^bhw'YZ[b37 (4.7C#&*hsm*:>x 67>@D(#<=D(,C$	%,(,GXd38n%,!OT!	7c 	7 	7r   r,   c                   V    e Zd ZU dZdZee   ed<   dZe	d   ed<    e
e      Zeed<   y)	DailyRoomParamszParameters for creating a Daily room.

    Parameters:
        name: Optional custom name for the room.
        privacy: Room privacy setting ('private' or 'public').
        properties: Room configuration properties.
    Nnamepublic)privaterM   privacydefault_factory
properties)r   r   r   r   rL   r   r   r   rO   r   r   r,   rR   r   r   r    rK   rK      s8     D(3-,4GW()4&+<O&PJ#Pr   rK   c                   X    e Zd ZU dZeed<   eed<   eed<   eed<   eed<   eed<   eed<   y	)
DailyRoomObjecta  Represents a Daily room returned by the API.

    Parameters:
        id: Unique room identifier.
        name: Room name.
        api_created: Whether room was created via API.
        privacy: Room privacy setting ('private' or 'public').
        url: Full URL for joining the room.
        created_at: Timestamp of room creation in ISO 8601 format (e.g., "2019-01-26T09:01:22.000Z").
        config: Room configuration properties.
    idrL   api_createdrO   url
created_atconfigN)r   r   r   r   r   r   r   r,   r   r   r    rT   rT      s.    
 	G
IL	HOr   rT   c                   N   e Zd ZU dZdZee   ed<   dZee	   ed<   dZ
ee   ed<   dZee   ed<   dZee   ed<   dZee	   ed<   dZee   ed	<   dZee   ed
<   dZee	   ed<   dZee	   ed<   dZee	   ed<   dZeed      ed<   dZee	   ed<   dZee	   ed<   dZeeeef      ed<   y)DailyMeetingTokenPropertiesai  Properties for configuring a Daily meeting token.

    Refer to the Daily API documentation for more information:
    https://docs.daily.co/reference/rest-api/meeting-tokens/create-meeting-token#properties

    Parameters:
        room_name: The room for which this token is valid. If not set, the token is valid for all rooms in your domain.
        eject_at_token_exp: If True, the user will be ejected from the room when the token expires.
        eject_after_elapsed: The number of seconds after which the user will be ejected from the room.
        nbf: Not before timestamp - users cannot join with this token before this time.
        exp: Expiration time (unix timestamp in seconds). Strongly recommended for security.
        is_owner: If True, the token will grant owner privileges in the room.
        user_name: The name of the user. This will be added to the token payload.
        user_id: A unique identifier for the user. This will be added to the token payload.
        enable_screenshare: If True, the user will be able to share their screen.
        start_video_off: If True, the user's video will be turned off when they join the room.
        start_audio_off: If True, the user's audio will be turned off when they join the room.
        enable_recording: Recording settings for the token. Must be one of 'cloud', 'local' or 'raw-tracks'.
        enable_prejoin_ui: If True, the user will see the prejoin UI before joining the room.
        start_cloud_recording: Start cloud recording when the user joins the room.
        permissions: Specifies the initial default permissions for a non-meeting-owner participant.
    N	room_nameeject_at_token_expeject_after_elapsednbfr/   is_owner	user_nameuser_idenable_screensharer@   start_audio_offr5   r8   r1   start_cloud_recordingpermissions)r   r   r   r   r\   r   r   r   r]   r   r^   r   r_   r/   r`   ra   rb   rc   r@   rd   r8   r   r1   re   rf   r   r   r   r   r    r[   r[      s    .  $Ix}#)--)-#-C#C##Hhtn##Ix}#!GXc]!)--&*OXd^*&*OXd^*^bhw'YZ[b(,x~,,08D>0,0K$sCx.)0r   r[   c                   .    e Zd ZU dZ ee      Zeed<   y)DailyMeetingTokenParamsa  Parameters for creating a Daily meeting token.

    Refer to the Daily API documentation for more information:
    https://docs.daily.co/reference/rest-api/meeting-tokens/create-meeting-token#body-params

    Parameters:
        properties: Meeting token configuration properties.
    rP   rR   N)r   r   r   r   r   r[   rR   r   r   r   r    rh   rh      s     /4D_.`J+`r   rh   c                       e Zd ZdZdddededej                  fdZded	efd
Zded	e	fdZ
ded	e	fdZ	 	 	 	 ddededededee   d	efdZded	efdZded	efdZded	e	fdZy)DailyRESTHelperzHelper class for interacting with Daily's REST API.

    Provides methods for creating, managing, and accessing Daily rooms.
    zhttps://api.daily.co/v1)daily_api_urldaily_api_keyrk   aiohttp_sessionc                .    || _         || _        || _        y)zInitialize the Daily REST helper.

        Args:
            daily_api_key: Your Daily API key.
            daily_api_url: Daily API base URL (e.g. "https://api.daily.co/v1").
            aiohttp_session: Async HTTP session for making requests.
        N)rl   rk   rm   )rE   rl   rk   rm   s       r    __init__zDailyRESTHelper.__init__   s     +*.r   room_urlrA   c                 2    t        |      j                  dd S )zExtract room name from a Daily room URL.

        Args:
            room_url: Full Daily room URL.

        Returns:
            Room name portion of the URL.
        r   N)r   path)rE   rp   s     r    get_name_from_urlz!DailyRESTHelper.get_name_from_url   s     !&&qr**r   c                 b   K   | j                  |      }| j                  |       d{   S 7 w)zGet room details from a Daily room URL.

        Args:
            room_url: Full Daily room URL.

        Returns:
            DailyRoomObject instance for the room.
        N)rs   _get_room_from_namerE   rp   r\   s      r    get_room_from_urlz!DailyRESTHelper.get_room_from_url  /      **84	--i8888   &/-/paramsc                    K   dd| j                    i}|j                  d      }| j                  j                  | j                   d||      4 d{   }|j
                  dk7  r3|j                          d{   }t        d	|j
                   d
|       |j                          d{   }ddd      d{    	 t        di }|S 7 |7 W7 &7 # 1 d{  7  sw Y   (xY w# t        $ r}t        d|       d}~ww xY ww)a  Create a new Daily room.

        Args:
            params: Room configuration parameters.

        Returns:
            DailyRoomObject instance for the created room.

        Raises:
            Exception: If room creation fails or response is invalid.
        AuthorizationBearer Texclude_nonez/roomsheadersjsonN   zUnable to create room (status: ): Invalid response: r   )rl   
model_dumprm   postrk   statustext	Exceptionr   rT   r   )	rE   rz   r   r   rr   dataroomes	            r    create_roomzDailyRESTHelper.create_room  s     #gd.@.@-A$BC  d 3'',,!!"&)7 - 
 	" 	"xx3VVX~"A!((3tf UVV>D	" 	"	6"*T*D 	" & "	" 	" 	" 	"  	60455	6s   ADCD#C<C=2C/C0C4D?C DC/ DCCDC, C#!C,(D/	D8DDDNexpiry_timer]   ownerc                   K   |st        d      t        t        j                         |z         }| j                  |      }dd| j                   i}|t        t        ||||            }nD||j                  _        ||j                  _	        ||j                  _
        ||j                  _        |j                  d      }	| j                  j                  | j                   d	||	
      4 d{   }
|
j                   dk7  r3|
j#                          d{   }t        d|
j                    d|       |
j%                          d{   }ddd      d{    |d   S 7 s7 N7 7 # 1 d{  7  sw Y   d   S xY ww)a  Generate a meeting token for user to join a Daily room.

        Args:
            room_url: Daily room URL.
            expiry_time: Token validity duration in seconds (default: 1 hour).
            eject_at_token_exp: Whether to eject user when token expires.
            owner: Whether token has owner privileges.
            params: Optional additional token properties. Note that room_name,
                exp, and is_owner will be set based on the other function
                parameters regardless of values in params.

        Returns:
            Meeting token.

        Raises:
            Exception: If token generation fails or room URL is missing.
        zXNo Daily room specified. You must specify a Daily room in order a token to be generated.r|   r}   N)r\   r`   r/   r]   )rR   Tr~   z/meeting-tokensr   r   z(Failed to create meeting token (status: r   token)r   r   timers   rl   rh   r[   rR   r\   r/   r]   r`   r   rm   r   rk   r   r   r   )rE   rp   r   r]   r   rz   
expirationr\   r   r   r   r   r   s                r    	get_tokenzDailyRESTHelper.get_token-  s    2 j  diikK78
**84	"gd.@.@-A$BC>,6'""'9	F +4F'$.F!3EF0).F&  d 3'',,!!"/2G$ - 
 	" 	"xx3VVX~"J188*TWX\W] ^__>D	" 	" G}	" & "	" 	" 	" 	" G}sl   C1F3E'4F7#E/E)2E/E+E/FE-
F)E/+E/-F/F5E86F=Fc                 b   K   | j                  |      }| j                  |       d{   S 7 w)zDelete a room using its URL.

        Args:
            room_url: Daily room URL.

        Returns:
            True if deletion was successful.
        N)rs   delete_room_by_namerv   s      r    delete_room_by_urlz"DailyRESTHelper.delete_room_by_urlm  rx   ry   r\   c           	        K   dd| j                    i}| j                  j                  | j                   d| |      4 d{   }|j                  dk7  rE|j                  dk7  r6|j                          d{   }t        d| d	|j                   d
|       ddd      d{    y7 i7 57 	# 1 d{  7  sw Y   yxY ww)zDelete a room using its name.

        Args:
            room_name: Name of the room to delete.

        Returns:
            True if deletion was successful.

        Raises:
            Exception: If deletion fails (excluding 404 Not Found).
        r|   r}   /rooms/r   Nr   i  zFailed to delete room [z] (status: r   T)rl   rm   deleterk   r   r   r   )rE   r\   r   r   r   s        r    r   z#DailyRESTHelper.delete_room_by_namey  s      #gd.@.@-A$BC''..!!"')5w / 
 	e 	exx3188s?VVX~"9)KPQPXPXzY\]a\b cdd	e 	e 	e &		e 	e 	e 	e sZ   AC	B.C	2B4:B0;"B4C	(B2)C	0B42C	4C:B=;CC	c                   K   dd| j                    i}| j                  j                  | j                   d| |      4 d{   }|j                  dk7  rt        d|       |j                          d{   }ddd      d{    	 t        d	i }|S 7 W7 $7 # 1 d{  7  sw Y   &xY w# t        $ r}t        d|       d}~ww xY ww)
z,Internal method to get room details by name.r|   r}   r   r   Nr   zRoom not found: r   r   )	rl   rm   getrk   r   r   r   rT   r   )rE   r\   r   r   r   r   r   s          r    ru   z#DailyRESTHelper._get_room_from_name  s     "gd.@.@-A$BC''++!!"')5w , 
 	" 	"xx3"29+ >??>D	" 	"	6"*T*D 	" "	" 	" 	" 	"  	60455	6s~   ACBC1B"9B:B">C	B 
CB7 CB" C"B4(B+)B40C7	C CCC)i  FTN)r   r   r   r   r   aiohttpClientSessionro   rs   rT   rw   rK   r   rH   r   r   rh   r   r   r   ru   r   r   r    rj   rj      s     7	/ / 	/
 !../$	+# 	+# 	+
9 
9 
9 O B %#(48>> > !	>
 > 01> 
>@
9 
9 
93 4 ,3 ? r   rj   )r   r   typingr   r   r   r   r   urllib.parser   r   pydanticr	   r
   r   r   r   r"   r*   r,   rK   rT   r[   rh   rj   r   r   r    <module>r      s   
  5 5 !  B B# #,#Y #&#	 #&37) 37lQi Q i  ,&1) &1R
ai 
a r   