o
    i&                     @   s   d Z ddlZddlZddlmZ ddlmZmZmZm	Z	m
Z
 ddlmZ ddlmZ ddlmZ ddlmZmZ dd	lmZmZ dd
lmZ G dd de
ZG dd deZdS )z(OpenAI Realtime LLM adapter for Pipecat.    N)	dataclass)AnyDictListOptional	TypedDict)logger)BaseLLMAdapter)FunctionSchema)AdapterTypeToolsSchema)
LLMContextLLMContextMessage)eventsc                   @   s@   e Zd ZU dZee ed< eej	 ed< ee
eef  ed< dS )!OpenAIRealtimeLLMInvocationParamszContext-based parameters for invoking OpenAI Realtime API.

    This is a placeholder until support for universal LLMContext machinery is added for OpenAI Realtime.
    system_instructionmessagestoolsN)__name__
__module____qualname____doc__r   str__annotations__r   r   ConversationItemr   r    r   r   f/home/ubuntu/.local/lib/python3.10/site-packages/pipecat/adapters/services/open_ai_realtime_adapter.pyr      s
   
 r   c                   @   s   e Zd ZdZedefddZdedefddZ	de
eeef  fdd	ZeG d
d dZde
e defddZdedejfddZededeeef fddZdede
eeef  fddZdS )OpenAIRealtimeLLMAdapterzLLM adapter for OpenAI Realtime API function calling.

    Converts Pipecat's tool schemas into the specific format required by
    OpenAI's Realtime API for function calling capabilities.
    returnc                 C   s   dS )zLGet the identifier used in LLMSpecificMessage instances for OpenAI Realtime.zopenai-realtimer   selfr   r   r   id_for_llm_specific_messages)   s   z5OpenAIRealtimeLLMAdapter.id_for_llm_specific_messagescontextc                 C   s,   |  | |}|j|j| |jpg dS )a{  Get OpenAI Realtime-specific LLM invocation parameters from a universal LLM context.

        This is a placeholder until support for universal LLMContext machinery is added for OpenAI Realtime.

        Args:
            context: The LLM context containing messages, tools, etc.

        Returns:
            Dictionary of parameters for invoking OpenAI Realtime's API.
        )r   r   r   ) _from_universal_context_messagesget_messagesr   r   from_standard_toolsr   )r    r"   r   r   r   r   get_llm_invocation_params.   s
   z2OpenAIRealtimeLLMAdapter.get_llm_invocation_paramsc                 C   s   g }|  |D ]N}t|}d|v rAt|d trA|d D ]#}|d dkr4|d d dr4d|d d< |d dkr@d|d d	< qd
|v rP|d
 drPd|d	< || q|S )a  Get messages from a universal LLM context in a format ready for logging about OpenAI Realtime.

        Removes or truncates sensitive data like image content for safe logging.

        This is a placeholder until support for universal LLMContext machinery is added for OpenAI Realtime.

        Args:
            context: The LLM context containing messages.

        Returns:
            List of messages in a format ready for logging about OpenAI Realtime.
        contenttype	image_urlurlzdata:image/zdata:image/...input_audioz...data	mime_typezimage/)r$   copydeepcopy
isinstancelist
startswithappend)r    r"   msgsmessagemsgitemr   r   r   get_messages_for_loggingA   s    
z1OpenAIRealtimeLLMAdapter.get_messages_for_loggingc                   @   s0   e Zd ZU dZeej ed< dZe	e
 ed< dS )z*OpenAIRealtimeLLMAdapter.ConvertedMessageszIContainer for OpenAI-formatted messages converted from universal context.r   Nr   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ConvertedMessagesb   s   
 r9   universal_context_messagesc                 C   s   |s| j g dS t|}d }|d ddkr?|d}|d}t|tr*|}nt|tr6|d d}|s?| j g |dS t|dkr[|d dd	kr[| j | 	|d g|dS d
}d}| j d	ddd
|tj|dd|gdgdg|dS )N)r   r   rolesystemr'   text)r   r      userz
        This is a previously saved conversation. Please treat this conversation history as a
        starting point for the current conversation.aS  
        This is the end of the previously saved conversation. Please continue the conversation
        from here. If the last message is a user instruction or question, act on that instruction
        or answer the question. If the last message is an assistant response, simple say that you
        are ready to continue the conversation.r5   
input_textz

   )indentr(   r=   r;   r(   r'   )r9   r.   r/   getpopr0   r   r1   len_from_universal_context_messagejoinjsondumps)r    r:   r   r   r<   r'   
intro_texttrailing_textr   r   r   r#   i   sB   




z9OpenAIRealtimeLLMAdapter._from_universal_context_messagesr5   c                 C   s   | ddkrK| d}t| dtr=d}| dD ]!}| ddkr.|d| d 7 }qtd| d d	|  qtjdd
tjd|dgdS | ddkrq| drq| dd }tjd|d |d d |d d dS td|  d S )Nr;   r?   r'    r(   r=    z+Unhandled content type in context message: z - r5   r@   rC   rD   	assistant
tool_callsr   function_callidfunctionname	arguments)r(   call_idrU   rV   z;Unhandled message type in _from_universal_context_message: )rE   r0   r1   r   errorr   r   ItemContent)r    r5   r'   ctcr   r   r   rH      s0   


z8OpenAIRealtimeLLMAdapter._from_universal_context_messagerT   c                 C   s   d| j | jd| j| jddS )zConvert a function schema to OpenAI Realtime format.

        Args:
            function: The function schema to convert.

        Returns:
            Dictionary in OpenAI Realtime function format.
        rT   object)r(   
propertiesrequired)r(   rU   description
parameters)rU   r_   r]   r^   )rT   r   r   r   #_to_openai_realtime_function_format   s   z<OpenAIRealtimeLLMAdapter._to_openai_realtime_function_formattools_schemac                    s:   |j } fdd|D }g }|jr|jtjg }|| S )zConvert tool schemas to OpenAI Realtime function-calling format.

        Args:
            tools_schema: The tools schema containing functions to convert.

        Returns:
            List of function definitions in OpenAI Realtime format.
        c                    s   g | ]}  |qS r   )ra   ).0funcr   r   r   
<listcomp>   s    
zEOpenAIRealtimeLLMAdapter.to_provider_tools_format.<locals>.<listcomp>)standard_toolscustom_toolsrE   r   SHIM)r    rb   functions_schemarf   shimmed_toolsr   r   r   to_provider_tools_format   s   	

z1OpenAIRealtimeLLMAdapter.to_provider_tools_formatN)r   r   r   r   propertyr   r!   r   r   r&   r   r   r   r8   r   r9   r   r#   r   r   rH   staticmethodr
   ra   r   rk   r   r   r   r   r   "   s*    !
?
"r   )r   r.   rJ   dataclassesr   typingr   r   r   r   r   logurur   !pipecat.adapters.base_llm_adapterr	   (pipecat.adapters.schemas.function_schemar
   %pipecat.adapters.schemas.tools_schemar   r   *pipecat.processors.aggregators.llm_contextr   r    pipecat.services.openai.realtimer   r   r   r   r   r   r   <module>   s   