o
    Æ°iÞ  ã                   @   s\   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
 G dd„ dƒZG dd„ dƒZdS )	é    )ÚDictÚListÚMappingÚOptionalÚUnion)Úparse_qsN)ÚPASS_THROUGH_HEADER_PREFIXc                   @   sŠ   e Zd Ze	ddejdeeeee	f f de
eeeee	f f  deeeeee f f fdd„ƒZe	dd	ed
ede
e fdd„ƒZdS )ÚBasePassthroughUtilsNÚexisting_urlÚrequest_query_paramsÚdefault_query_paramsÚreturnc                 C   sP   | j  d¡}t|ƒ}dd„ | ¡ D ƒ}i }|r| |¡ | |¡ | |¡ |S )Nzutf-8c                 S   s*   i | ]\}}|t |ƒd kr|d n|“qS )é   r   )Úlen)Ú.0ÚkÚv© r   úP/home/app/Keep/.python/lib/python3.10/site-packages/litellm/passthrough/utils.pyÚ
<dictcomp>   s    ÿzDBasePassthroughUtils.get_merged_query_parameters.<locals>.<dictcomp>)ÚqueryÚdecoder   ÚitemsÚupdate)r
   r   r   Zexisting_query_stringZexisting_query_paramsZupdated_existing_query_paramsZmerged_paramsr   r   r   Úget_merged_query_parameters
   s   ÿ


z0BasePassthroughUtils.get_merged_query_parametersFÚrequest_headersÚheadersÚforward_headersc                 C   sh   |du r|   dd¡ |   dd¡ i | ¥|¥}|  ¡ D ]\}}| ¡  t¡r1|ttƒd… }|||< q|S )a$  
        Helper to forward headers from original request.

        Also handles 'x-pass-' prefixed headers which are always forwarded
        with the prefix stripped, regardless of forward_headers setting.
        e.g., 'x-pass-anthropic-beta: value' becomes 'anthropic-beta: value'
        Tzcontent-lengthNÚhost)Úpopr   ÚlowerÚ
startswithr   r   )r   r   r   Úheader_nameÚheader_valueZactual_header_namer   r   r   Úforward_headers_from_request&   s   €z1BasePassthroughUtils.forward_headers_from_request)N)F)Ú__name__Ú
__module__Ú__qualname__ÚstaticmethodÚhttpxÚURLr   Ústrr   Úlistr   r   r   r   ÚdictÚboolr$   r   r   r   r   r	   	   s*    ýÿþýüýÿþýr	   c                   @   s"   e Zd Zededefdd„ƒZdS )ÚCommonUtilsÚendpointr   c                 C   sN   ddl }d| vr
| S g d¢}|D ]\}}| || ¡r$| ||| ¡}  | S q| S )av  
        Encodes any "/" found in the modelId of an AWS Bedrock Runtime Endpoint when arns are passed in.
        - modelID value can be an ARN which contains slashes that SHOULD NOT be treated as path separators.
        e.g endpoint: /model/<modelId>/invoke
        <modelId> containing arns with slashes need to be encoded from
            arn:aws:bedrock:ap-southeast-1:123456789012:application-inference-profile/abdefg12334 =>
            arn:aws:bedrock:ap-southeast-1:123456789012:application-inference-profile%2Fabdefg12334
        so that it is treated as one part of the path.
        Otherwise, the encoded endpoint will return 500 error when passed to Bedrock endpoint.
            
        See the apis in https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Operations_Amazon_Bedrock_Runtime.html
        for more details on the regex patterns of modelId which we use in the regex logic below.
        
        Args:
            endpoint (str): The original endpoint string which may contain ARNs that contain slashes.
            
        Returns:
            str: The endpoint with properly encoded ARN slashes
        r   Nzarn:aws:)
)z((custom-model)/([a-z0-9.-]+)/([a-z0-9]+)z\1%2F\2%2F\3)z!(:application-inference-profile)/ú\1%2F)z(:inference-profile)/r1   )z(:foundation-model)/r1   )z(:imported-model)/r1   )z(:provisioned-model)/r1   )z
(:prompt)/r1   )z(:endpoint)/r1   )z(:prompt-router)/r1   )z(:default-prompt-router)/r1   )ÚreÚsearchÚsub)r0   r2   ÚpatternsÚpatternÚreplacementr   r   r   Ú"encode_bedrock_runtime_modelid_arnE   s   üz.CommonUtils.encode_bedrock_runtime_modelid_arnN)r%   r&   r'   r(   r+   r8   r   r   r   r   r/   D   s    r/   )Útypingr   r   r   r   r   Úurllib.parser   r)   Zlitellm.constantsr   r	   r/   r   r   r   r   Ú<module>   s    ;