Ë
    tPêjQ  ã                   ój   — d Z ddlZddlmZmZmZ ddlmZ ddlm	Z	m
Z
 erddlmZ neZ G d„ d	«      Zy)
z‹
This file contains the LangFuseHandler class

Used to get the LangFuseLogger for a given request

Handles Key/Team Based Langfuse Logging
é    N)ÚTYPE_CHECKINGÚAnyÚFinal)ÚStandardCallbackDynamicParamsé   )ÚLangFuseLoggerÚLangfuseLoggingConfig)ÚDynamicLoggingCachec            
       óÜ   — e Zd Ze	 ddedededz  defd„«       Zededz  dedefd„«       Zede	dedefd	„«       Z
e	 ddededz  defd
„«       Zedededz  fd„«       Zededefd„«       Zy)ÚLangFuseHandlerNÚ standard_callback_dynamic_paramsÚin_memory_dynamic_logger_cacheÚglobalLangfuseLoggerÚreturnc                 ó  — |}t         j                  | «      du rt         j                  ||¬«      S t         j                  || ¬«      }t	        |«      }|j                  |d¬«      }|€t         j                  ||¬«      }|S )a‚  
        This function is used to get the LangFuseLogger for a given request

        1. If dynamic credentials are passed
            - check if a LangFuseLogger is cached for the dynamic credentials
            - if cached LangFuseLogger is not found, create a new LangFuseLogger and cache it

        2. If dynamic credentials are not passed return the globalLangfuseLogger

        F)r   r   )r   r   Úlangfuse©ÚcredentialsÚservice_name©r   r   )r   Ú(_dynamic_langfuse_credentials_are_passedÚ_return_global_langfuse_loggerÚ#get_dynamic_langfuse_logging_configÚdictÚ	get_cacheÚ(_create_langfuse_logger_from_credentials)r   r   r   Útemp_langfuse_loggerÚ_credentialsÚcredentials_dicts         ús/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/integrations/langfuse/langfuse_handler.pyÚget_langfuse_logger_for_requestz/LangFuseHandler.get_langfuse_logger_for_request   sµ   € ð  7KÐÜ×CÑCÐDdÓeÐinÑnÜ"×AÑAØ%9Ø/Mð Bó ð ô .×QÑQØ!5Ø-Mð Ró 
ˆô #' |Ó"4Ðð  >×GÑGØ(°zð  Hó  
Ðð
  Ð'Ü#2×#[Ñ#[Ø,Ø/Mð $\ó $Ð ð
 $Ð#ó    c                 ój   — | �| S i }|j                  |d¬«      } | €t        j                  ||¬«      } | S )a·  
        Returns the Global LangfuseLogger set on litellm

        (this is the default langfuse logger - used when no dynamic credentials are passed)

        If no Global LangfuseLogger is set, it will check in_memory_dynamic_logger_cache for a cached LangFuseLogger
        This function is used to return the globalLangfuseLogger if it exists, otherwise it will check in_memory_dynamic_logger_cache for a cached LangFuseLogger
        r   r   r   )r   r   r   )r   r   r   s      r    r   z.LangFuseHandler._return_global_langfuse_loggerC   sf   € ð  Ð+Ø'Ð'ð ð 	ð  >×GÑGØ(Ø#ð  Hó  
Ðð  Ð'Ü#2×#[Ñ#[Ø,Ø/Mð $\ó $Ð ð $Ð#r"   r   c           	      ó  — t        | j                  d«      | j                  d«      xs | j                  d«      | j                  d«      | j                  d«      | j                  d«      du ¬«      }|j                  | d|¬	«       |S )
z¹
        This function is used to
        1. create a LangFuseLogger from the credentials
        2. cache the LangFuseLogger to prevent re-creating it for the same credentials
        Úlangfuse_public_keyÚlangfuse_secretÚlangfuse_secret_keyÚlangfuse_hostÚlangfuse_environmentN)r%   r&   r(   r)   Úallow_env_credentialsr   )r   r   Úlogging_obj)r   ÚgetÚ	set_cache)r   r   Úlangfuse_loggers      r    r   z8LangFuseHandler._create_langfuse_logger_from_credentialsa   s‡   € ô "0Ø +§¡Ð0EÓ FØ'ŸO™OÐ,=Ó>ÒhÀ+Ç/Á/ÐRgÓBhØ%Ÿ/™/¨/Ó:Ø!,§¡Ð1GÓ!HØ"-§/¡/°/Ó"BÀdÐ"Jô"
ˆð 	'×0Ñ0Ø#Ø#Ø'ð 	1ô 	
ð
 Ðr"   c                 óÆ   — t        | j                  d«      xs | j                  d«      | j                  d«      | j                  d«      t        j                  | «      ¬«      S )aT  
        This function is used to get the Langfuse logging config to use for a given request.

        It checks if the dynamic parameters are provided in the standard_callback_dynamic_params and uses them to get the Langfuse logging config.

        If no dynamic parameters are provided, it uses the `globalLangfuseLogger` values
        r&   r'   r%   r(   )r&   r%   r(   r)   )r	   r,   r   Ú_meaningful_dynamic_environment)r   r   s     r    r   z3LangFuseHandler.get_dynamic_langfuse_logging_configz   sa   € ô %Ø<×@Ñ@ÐARÓSò KØ/×3Ñ3Ð4IÓJØ @× DÑ DÐEZÓ [Ø:×>Ñ>¸ÓOÜ!0×!PÑ!PÐQqÓ!rô
ð 	
r"   c                 óÄ   — | j                  d«      }|€yt        |«      j                  «       }|r/|t        j                  d«      k(  s|t        j                  «       k(  ry|S )až  Return the per-request environment only when it changes behavior.

        Empty/whitespace values and values equal to the deployment-wide
        LANGFUSE_TRACING_ENVIRONMENT fallback are treated as absent so an
        environment-only override that matches the default does not mint a
        duplicate SDK client (each client costs threads and counts against
        MAX_LANGFUSE_INITIALIZED_CLIENTS).
        r)   NÚLANGFUSE_TRACING_ENVIRONMENT)r,   ÚstrÚstripÚosÚgetenvr   Úresolve_deployment_environment)r   ÚrawÚvalues      r    r0   z/LangFuseHandler._meaningful_dynamic_environment�   s[   € ð /×2Ñ2Ð3IÓJˆØˆ;ØÜ�C“—‘Ó ˆáØœŸ	™	Ð"@ÓAÒAØœ×EÑEÓGÒGàØˆr"   c                 ó¸   — | j                  d«      €H| j                  d«      €7| j                  d«      €&| j                  d«      €t        j                  | «      �yy)zì
        This function is used to check if the dynamic langfuse credentials are passed in standard_callback_dynamic_params

        Returns:
            bool: True if the dynamic langfuse credentials are passed, False otherwise
        r(   r%   r&   r'   TF)r,   r   r0   )r   s    r    r   z8LangFuseHandler._dynamic_langfuse_credentials_are_passed§   sc   € ð -×0Ñ0°ÓAÐMØ/×3Ñ3Ð4IÓJÐVØ/×3Ñ3Ð4EÓFÐRØ/×3Ñ3Ð4IÓJÐVÜ×>Ñ>Ð?_Ó`ÐlàØr"   )N)Ú__name__Ú
__module__Ú__qualname__Ústaticmethodr   r
   r   r!   r   r   r   r	   r   r3   r0   Úboolr   © r"   r    r   r      s(  „ Øð 7;ñ)$Ø*Gð)$à(;ð)$ð -¨tÑ3ð)$ð 
ò	)$ó ð)$ðV ð$Ø,¨tÑ3ð$à(;ð$ð 
ò$ó ð$ð: ðØðà(;ðð 
òó ðð0 ð 7;ñ
Ø*Gð
à,¨tÑ3ð
ð 
ò
ó ð
ð( ðØ*Gðà	ˆt‰òó ðð. ðØ*Gðà	òó ñr"   r   )Ú__doc__r5   Útypingr   r   r   Ú*litellm.litellm_core_utils.litellm_loggingr   r   r   r	   r
   r   r@   r"   r    ú<module>rD      s3   ðñó 
ß ,Ñ ,å Tç ;áÞNàÐ÷dò dr"   