
    h%                         d dl Z d dlmZmZmZmZmZmZ d dlZd dl	m
Z
 d dlmZ d dlmZmZmZ d dlmZ d dlmZmZ  G d d	      Z G d
 d      Zy)    N)AnyDictOptionalUnioncastget_type_hints)verbose_logger)BaseResponsesAPIConfig)ResponseAPIUsage!ResponsesAPIOptionalRequestParamsResponsesAPIResponse)DecodedResponseId)SpecialEnumsUsagec                      e Zd ZdZededededefd       Z	edeee
f   defd       Ze	 dd
edee   deeee
f      defd       Zedee   dee   dedefd       Zededefd       Zedee   dee   fd       Zededefd       Zy	)ResponsesAPIRequestUtilsz2Helper utils for constructing ResponseAPI requestsmodelresponses_api_provider_configresponse_api_optional_paramsreturnc           	          |j                  |       }|D cg c]  }||vr|
 }}|r,t        j                  | d|  ddj                  |             |j	                  || t        j
                        }|S c c}w )aS  
        Get optional parameters for the responses API.

        Args:
            params: Dictionary of all parameters
            model: The model name
            responses_api_provider_config: The provider configuration for responses API

        Returns:
            A dictionary of supported parameters for the responses API
        z5The following parameters are not supported for model z: z, )r   message)r   r   drop_params)get_supported_openai_paramslitellmUnsupportedParamsErrorjoinmap_openai_paramsr   )r   r   r   supported_paramsparamunsupported_paramsmapped_paramss          S/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/responses/utils.py!get_optional_params_responses_apiz:ResponsesAPIRequestUtils.get_optional_params_responses_api   s    ( 9TT
 6
,, 
 
 00OPUwVXY]YbYbcuYvXwx  6GG)E++ H 
 '
s   A7paramsc                 0   t        t              j                         }| j                         D ci c]  \  }}||v s||| }}}d|v rt        j                  |d         }||d<   d|v rddlm}  ||d         |d<   t        t        |      S c c}}w )a  
        Filter parameters to only include those defined in ResponsesAPIOptionalRequestParams.

        Args:
            params: Dictionary of parameters to filter

        Returns:
            ResponsesAPIOptionalRequestParams instance with only the valid parameters
        previous_response_idmetadatar   )add_openai_metadata)	r   r   keysitemsr   <decode_previous_response_id_to_original_previous_response_idlitellm.utilsr)   r   )r%   
valid_keyskvfiltered_paramsdecoded_previous_response_idr)   s          r#   )get_requested_response_api_optional_paramzBResponsesAPIRequestUtils.get_requested_response_api_optional_paramA   s     $$EFKKM
#\\^
QqJ1=AqD
 

 "_4+C  ,A  ,A 67,( 7SO23(9*=
++OJ' 5GG%
s   BBBNresponses_api_responsecustom_llm_providerlitellm_metadatac                     |xs i }|j                  di       xs i }|j                  d      }t        j                  ||| j                        }|| _        | S )z
        Update the responses_api_response_id with model_id and custom_llm_provider

        This builds a composite ID containing the custom LLM provider, model ID, and original response ID
        
model_infoid)model_idr5   response_id)getr    _build_responses_api_response_idr9   )r4   r5   r6   r8   r:   
updated_ids         r#   /_update_responses_api_response_id_with_model_idzHResponsesAPIRequestUtils._update_responses_api_response_id_with_model_idc   sj     ,1r%5%9%9,%K%Qr
>>$'-NN 3.11 O 

 %/!%%    r:   r;   c                     t        t        j                  j                        j	                  | ||      }t        j                  |j                  d            j                  d      }d| S )z#Build the responses_api_response_idutf-8resp_)	strr   %LITELLM_MANAGED_RESPONSE_COMPLETE_STRvalueformatbase64	b64encodeencodedecode)r5   r:   r;   assembled_idbase64_encoded_ids        r#   r=   z9ResponsesAPIRequestUtils._build_responses_api_response_idz   sl      >>DD

&$h
< 	 "(!1!1,2E2Eg2N!O!V!V"
 ()**r@   c                 "   	 | j                  dd      }t        j                  |j                  d            j	                  d      }d|vrt        dd|       S |j                  d      }d}d}t        |      dk\  rF|d   }|d	   }|d
   }|j                  dd      }|j                  dd      }|j                  dd      }	n| }	t        |||	      S # t        $ r3}
t        j                  d|  d|
        t        dd|       cY d}
~
S d}
~
ww xY w)z
        Decode the responses_api_response_id

        Returns:
            DecodedResponseId: Structured tuple with custom_llm_provider, model_id, and response_id
        rC    rB   ;N)r5   r:   r;      r         zlitellm:custom_llm_provider:z	model_id:zresponse_id:zError decoding response_id 'z': )replacerH   	b64decoderJ   rK   r   splitlen	Exceptionr	   debug)r;   
cleaned_id
decoded_idpartsr5   r:   custom_llm_provider_partmodel_id_partresponse_partdecoded_response_ides              r#   !_decode_responses_api_response_idz:ResponsesAPIRequestUtils._decode_responses_api_response_id   sE   -	$,,Wb9J))**;*;G*DELLWUJ *$((,! +  $$S)E #'H E
a+08( %a %a&>&F&F2B'# )00bA&3&;&;NB&O#&1#$$7!/ 
  	  #?}CPQs!ST$$(' 	s%   AC A8C 	D(D	D	Dc                 \    | yt         j                  |       }|j                  d      xs dS )z%Get the model_id from the response_idNr:   r   rb   r<   )r;   r`   s     r#   get_model_id_from_response_idz6ResponsesAPIRequestUtils.get_model_id_from_response_id   s8     $FF{S 	 #&&z2:d:r@   r'   c                 P    t         j                  |       }|j                  d|       S )aS  
        Decode the previous_response_id to the original previous_response_id

        Why?
            - LiteLLM encodes the `custom_llm_provider` and `model_id` into the `previous_response_id` this helps with maintaining session consistency when load balancing multiple deployments of the same model.
            - We cannot send the litellm encoded b64 to the upstream llm api, hence we decode it to the original `previous_response_id`

        Args:
            previous_response_id: The previous_response_id to decode

        Returns:
            The original previous_response_id
        r;   rd   )r'   r`   s     r#   r,   zUResponsesAPIRequestUtils.decode_previous_response_id_to_original_previous_response_id   s0    $ %FF$ 	
 #&&}6JKKr@   )N)__name__
__module____qualname____doc__staticmethodrD   r
   r   r   r$   r   r3   r   r   r?   r=   r   rb   re   r,    r@   r#   r   r      ss   <++'=+ 'H+ 
	+ +Z HS#XH	*H HB  6:& 4&%c]& #4S>2& 
	& &, +%c]+3-+ + 
	+ + 66	6 6p ;8C= ;Xc] ; ; L!L	L Lr@   r   c                   Z    e Zd Zedeeef   defd       Zede	eeef      de
fd       Zy)ResponseAPILoggingUtilsusager   c                 8    t        | t              ryd| v rd| v ryy)z1returns True if usage is from OpenAI Response APITinput_tokensoutput_tokensF)
isinstancer   )ro   s    r#   _is_response_api_usagez.ResponseAPILoggingUtils._is_response_api_usage   s'     e-.U"%'?r@   c                     | t        ddd      S t        | t              rt        di | n| }|j                  xs d}|j
                  xs d}t        ||||z         S )z7Tranforms the ResponseAPIUsage object to a Usage objectr   )prompt_tokenscompletion_tokenstotal_tokensrl   )r   rs   dictr   rq   rr   )ro   response_api_usagerv   rw   s       r#   +_transform_response_api_usage_to_chat_usagezCResponseAPILoggingUtils._transform_response_api_usage_to_chat_usage   s|    
 ="#  *4E4)@%u%e 	 0<<A!3!A!A!FQ'/&)::
 	
r@   N)rg   rh   ri   rk   r   ry   r   boolrt   r   r   r{   rl   r@   r#   rn   rn      sa    eD2B,B&C    
d$4456
	
 
r@   rn   )rH   typingr   r   r   r   r   r   r   litellm._loggingr	   .litellm.llms.base_llm.responses.transformationr
   litellm.types.llms.openair   r   r   litellm.types.responses.mainr   litellm.types.utilsr   r   r   rn   rl   r@   r#   <module>r      sB     C C  + Q 
 ; 3RL RLj
 
r@   