
    AHj                     h    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      Z G d d      Zy)	    )DictListMappingOptionalUnion)parse_qsN)PASS_THROUGH_HEADER_PREFIXc                       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       Ze	 ddeded	e
e   fd
       Zy)BasePassthroughUtilsNexisting_urlrequest_query_paramsdefault_query_paramsreturnc                 8   | j                   j                  d      }t        |      }|j                         D ci c]  \  }}|t	        |      dk(  r|d   n| }}}i }|r|j                  |       |j                  |       |j                  |       |S c c}}w )Nzutf-8   r   )querydecoder   itemslenupdate)	r   r   r   existing_query_stringexisting_query_paramskvupdated_existing_query_paramsmerged_paramss	            Q/root/tools/cai/cai_env/lib/python3.12/site-packages/litellm/passthrough/utils.pyget_merged_query_parametersz0BasePassthroughUtils.get_merged_query_parameters
   s     !- 2 2 9 9' B ()> ? 7L6Q6Q6S)
.2aAs1v{qt))
% )

   !56 	:; 	12)
s    Brequest_headersheadersforward_headersc                    |du r*| j                  dd       | j                  dd       i | |}| j                         D ]@  \  }}|j                         j                  t              s*|t        t              d }|||<   B |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_valueactual_header_names         r   forward_headers_from_requestz1BasePassthroughUtils.forward_headers_from_request&   s     d" 0$7- 54G4G *9)>)>)@ 	;%K  "--.HI%05O1P1R%S".:*+		;     )N)F)__name__
__module____qualname__staticmethodhttpxURLr   strr   listr   r   r   r   dictboolr*    r+   r   r   r   	   s     GKii%c5d+;&;< 'tCsDy1A,A'BC 
c5d3i((	)	 6  +0 "$ r+   r   c                   &    e Zd Zededefd       Zy)CommonUtilsendpointr   c                     ddl }d| vr| S g d}|D ]-  \  }}|j                  ||       s|j                  |||       }  | S  | 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)/r;   )z(:foundation-model)/r;   )z(:imported-model)/r;   )z(:provisioned-model)/r;   )z
(:prompt)/r;   )z(:endpoint)/r;   )z(:prompt-router)/r;   )z(:default-prompt-router)/r;   )researchsub)r9   r<   patternspatternreplacements        r   "encode_bedrock_runtime_modelid_arnz.CommonUtils.encode_bedrock_runtime_modelid_arnF   sb    * 	 X%O
 %- 	 G[yy(+66';A	 r+   N)r,   r-   r.   r/   r2   rB   r6   r+   r   r8   r8   E   s"    0S 0S 0 0r+   r8   )typingr   r   r   r   r   urllib.parser   r0   litellm.constantsr	   r   r8   r6   r+   r   <module>rF      s)    7 7 !  89 9x2 2r+   