o
    i                     @   s:   d Z ddlmZmZ ddlZddlmZ G dd dZdS )zLemonSlice API utilities for session management.

This module provides helper classes for interacting with the LemonSlice API,
including session creation and termination.
    )AnyOptionalN)loggerc                   @   s   e Zd ZdZdZdedejfddZddddddddd	e	e d
e	e de	e de	e
 de	e de	e de	eeef  defddZdedefddZdS )LemonSliceApizHelper class for interacting with the LemonSlice API.

    Provides methods for creating and managing sessions with LemonSlice avatars.
    z*https://lemonslice.com/api/liveai/sessionsapi_keysessionc                 C   s   || _ || _d| j d| _dS )zInitialize the LemonSliceApi client.

        Args:
            api_key: LemonSlice API key for authentication.
            session: An aiohttp session for making HTTP requests.
        zapplication/json)zContent-Typez	x-api-keyN)_api_key_session_headers)selfr   r    r   U/home/ubuntu/.local/lib/python3.10/site-packages/pipecat/transports/lemonslice/api.py__init__   s   zLemonSliceApi.__init__N)agent_image_urlagent_idagent_promptidle_timeoutdaily_room_urldaily_token
propertiesr   r   r   r   r   r   r   returnc             	      s:  |s|st d d}|r|rtdt d| d|  ddi}|dur+||d	< |dur3||d
< |dur;||d< |durC||d< |rIt|ni }	|durS||	d< |dur[||	d< |	ra|	|d< | jj| j| j|d4 I dH  }
|
  |
	 I dH }t d|  |W  d  I dH  S 1 I dH sw   Y  dS )uc  Create a new session with the specified agent_id or agent_image_url.

        Args:
            agent_image_url: The URL to an agent image. Provide either agent_id or agent_image_url.
            agent_id: ID of a LemonSlice agent. Provide either agent_id or agent_image_url.
            agent_prompt: A high-level system prompt that subtly influences the avatar’s movements, expressions, and emotional demeanor.
            idle_timeout: Idle timeout in seconds.
            daily_room_url: Daily room URL to use for the session.
            daily_token: Daily token for authenticating with the room.
            properties: Additional properties to pass to the session.

        Returns:
            Dictionary containing session_id, room_url, and control_url.

        Raises:
            ValueError: If neither agent_id nor agent_image_url is provided.
        z<No agent_id or agent_image_url provided, using default agentagent_080308d8b6e99f47z<Provide exactly one of agent_id or agent_image_url, not bothz&Creating LemonSlice session: agent_id=z, agent_image_url=transport_typedailyNr   r   r   r   	daily_urlr   r   headersjsonzCreated LemonSlice session: )
r   debug
ValueErrordictr	   postLEMONSLICE_URLr
   raise_for_statusr   )r   r   r   r   r   r   r   r   payloadproperties_dictrresponser   r   r   create_session&   sD   

0zLemonSliceApi.create_session
session_idcontrol_urlc              	      sp   ddi}| j j|| j|d4 I dH }|  td|  W d  I dH  dS 1 I dH s1w   Y  dS )zEnd an existing session.

        Args:
            session_id: ID of the session to end.
            control_url: The control URL from the create_session response.
        event	terminater   NzEnded LemonSlice session )r	   r!   r
   r#   r   r   )r   r)   r*   r$   r&   r   r   r   end_sessiond   s   .zLemonSliceApi.end_session)__name__
__module____qualname____doc__r"   straiohttpClientSessionr   r   intr    r   r(   r-   r   r   r   r   r      s:    	

>r   )r1   typingr   r   r3   logurur   r   r   r   r   r   <module>   s
   