o
    Úó”iØ$  ã                   @   sP   d Z ddlmZ ddlmZmZmZmZ dededB fdd„Z	G d	d
„ d
ƒZ
dS )z¨
Service loading and validation logic.

This module handles loading services, validating configurations,
and providing utilities for working with the service registry.
é   )ÚSERVICE_CONFIGS)ÚBotTypeÚServiceDefinitionÚServiceRegistryÚServiceTypeÚpackageÚreturnNc                 C   s,   d| v rd| v r|   d¡d   d¡d S dS )as  
    Extract the extra name from a package string.

    Args:
        package: Package string like "pipecat-ai[deepgram]" or "pipecat-ai"

    Returns:
        The extra name (e.g., "deepgram") or None if no extra

    Examples:
        >>> extract_package_extra("pipecat-ai[deepgram]")
        'deepgram'
        >>> extract_package_extra("pipecat-ai")
        None
    ú[ú]r   é    N)Úsplit)r   © r   úW/home/ubuntu/.local/lib/python3.10/site-packages/pipecat_cli/registry/service_loader.pyÚextract_package_extra   s   r   c                   @   s6  e Zd ZdZedee dededB fdd„ƒZede	dee fd	d
„ƒZ
edededB fdd„ƒZededee fdd„ƒZedeeeee B f dee fdd„ƒZededefdd„ƒZededee fdd„ƒZe	ddeeeee B f deeef dedee fdd„ƒZedeeee f fdd„ƒZdS )ÚServiceLoaderz:Handles loading and validating services from the registry.Úservice_listÚvaluer   Nc                 C   s    | D ]}|j |kr|  S qdS )z÷
        Find a service definition by its value.

        Args:
            service_list: List of service definitions to search
            value: Service value to find

        Returns:
            Service definition or None if not found
        N)r   )r   r   Úservicer   r   r   Úget_service_by_value*   s
   
ÿz"ServiceLoader.get_service_by_valueÚservice_typec                 C   s,   t jt j t jt jt jt jdœ}| | g ¡S )zµ
        Get all services of a specific type.

        Args:
            service_type: Type of service to retrieve

        Returns:
            List of service definitions
        )Ú	transportÚsttÚllmÚttsÚrealtime)r   ÚWEBRTC_TRANSPORTSÚTELEPHONY_TRANSPORTSÚSTT_SERVICESÚLLM_SERVICESÚTTS_SERVICESÚREALTIME_SERVICESÚget)r   Útype_mapr   r   r   Úget_all_services_by_type=   s   
ûz&ServiceLoader.get_all_services_by_typeÚservice_valuec                 C   s
   t  | ¡S )zÞ
        Get the initialization code for a service.

        Args:
            service_value: Service identifier (e.g., "deepgram_stt")

        Returns:
            Initialization code string or None if not found
        )r   r!   ©r$   r   r   r   Úget_service_configQ   s   
z ServiceLoader.get_service_configc                 C   s   t j | g ¡S )z«
        Get import statements for a service.

        Args:
            service_value: Service identifier

        Returns:
            List of import statements
        )r   ÚIMPORTSr!   r%   r   r   r   Úget_service_import^   s   z ServiceLoader.get_service_importÚservicesc                 C   sì   t ƒ }| d¡ | d¡ d| v r@t| d tƒr| d n| d g}tjtj }|D ]}t ||¡}|r?t	|j
ƒ}|r?| |¡ q)tjtjtjtjtjdœ}| ¡ D ]"\}}	|| v rs| | rst |	| | ¡}
|
rst	|
j
ƒ}|rs| |¡ qQ|S )aJ  
        Extract all package extras needed for selected services.

        Args:
            services: Dict mapping service type to service value(s)
                     e.g., {"transports": ["daily"], "stt": "deepgram_stt", ...}

        Returns:
            Set of package extras (e.g., {"daily", "deepgram", "openai"})
        ÚrunnerÚsileroÚ
transports©r   r   r   r   Úvideo)ÚsetÚaddÚ
isinstanceÚlistr   r   r   r   r   r   r   r   r   r   r    ÚVIDEO_SERVICESÚitems)r)   ÚextrasÚtransport_listÚall_transportsr   Útransport_defÚextraÚservice_type_mapr   r   Úservice_defr   r   r   Úextract_extras_for_servicesk   sD   


ÿý
ÿ

€ûÿ

€z)ServiceLoader.extract_extras_for_servicesc                 C   s,   | t v rdS tjtj }t || ¡rdS dS )zÈ
        Check if a service exists in the registry.

        Args:
            service_value: Service identifier to check

        Returns:
            True if service exists, False otherwise
        TF)r   r   r   r   r   r   )r$   r7   r   r   r   Úvalidate_service_exists£   s   z%ServiceLoader.validate_service_existsÚbot_typec                 C   s    | dkrt jS | dkrt jS g S )zÆ
        Get transport options based on bot type.

        Args:
            bot_type: Type of bot ("web" or "telephony")

        Returns:
            List of transport service definitions
        ÚwebÚ	telephony)r   r   r   )r>   r   r   r   Úget_transport_options¹   s
   z#ServiceLoader.get_transport_optionsr?   Úfeaturesc                 C   s   t tjƒ}| tjd ¡ | tjd ¡ | tjd ¡ | tjd ¡ d| v rKt| d tƒr4| d n| d g}|D ]}|tjv rJ| tj| ¡ q;dD ]}|| v rd| | }|tjv rd| tj| ¡ qM| d¡rr| tjd ¡ | d¡r| tjd ¡ | d	¡rŒ| tjd	 ¡ t|ƒS )
aL  
        Get all necessary import statements for selected services and features.

        Args:
            services: Dict mapping service type to service value(s)
            features: Dict of enabled features
            bot_type: Type of bot ("web" or "telephony")

        Returns:
            List of import statements
        ÚpipelineÚcontextr*   Úvadr,   r-   Ú	recordingÚtranscriptionÚobservability)	r/   r   ÚBASE_IMPORTSÚupdateÚFEATURE_IMPORTSr1   r2   r'   r!   )r)   rB   r>   Úimportsr6   r   r   r$   r   r   r   Úget_imports_for_servicesÊ   s6   

ÿý
€
€


z&ServiceLoader.get_imports_for_servicesc                  C   sŒ   g g dœ} g }|  tj¡ |  tj¡ |  tj¡ |  tj¡ |  tj¡ |D ]}|j}|tvr7| d  	|¡ |tj
vrC| d  	|¡ q'| S )z¦
        Find services that are defined but missing configs or imports.

        Returns:
            Dict with 'missing_configs' and 'missing_imports' lists
        )Úmissing_configsÚmissing_importsrN   rO   )Úextendr   r   r   r   r    r3   r   r   Úappendr'   )ÚmissingÚall_servicesr   r$   r   r   r   Úget_missing_servicesý   s   

€z"ServiceLoader.get_missing_services)r?   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústaticmethodr2   r   Ústrr   r   r#   r&   r(   Údictr/   r<   Úboolr=   r   rA   rM   rT   r   r   r   r   r   '   sF    ÿÿþ(7ÿÿ
ÿÿþ2 r   )rX   Ú_configsr   Úservice_metadatar   r   r   r   rZ   r   r   r   r   r   r   Ú<module>   s
   