
    ^h                         S SK r S SKrS SKJrJrJrJrJr  S SKrS SK	r	S SK
Jr  S SKJrJrJr  S SKJr  S SKJrJrJrJrJr   " S S\5      r " S	 S
\5      r " S S\5      rg)    N)AsyncIteratorIteratorListOptionalUnion)BaseModelResponseIterator)
BaseConfigBaseLLMExceptionLiteLLMLoggingObj)AllMessageValues)ChatCompletionToolCallChunkChatCompletionUsageBlockGenericStreamingChunkModelResponseUsagec                   (   ^  \ rS rSrU 4S jrSrU =r$ )CloudflareError   c                    > Xl         X l        [        R                  " SSS9U l        [        R
                  " XR                  S9U l        [        TU ]!  UUU R                  U R                  S9  g )NPOSTzhttps://api.cloudflare.com)methodurl)status_coderequest)r   messager   response)	r   r   httpxRequestr   Responser   super__init__)selfr   r   	__class__s      e/home/james-whalen/.local/lib/python3.13/site-packages/litellm/llms/cloudflare/chat/transformation.pyr!   CloudflareError.__init__   s[    &}}F8TU;U#LL]]	 	 	
    )r   r   r   r   )__name__
__module____qualname____firstlineno__r!   __static_attributes____classcell__r#   s   @r$   r   r      s    

 

r&   r   c                     ^  \ rS rSr% Sr\\   \S'   Sr\\	   \S'     S$S\\   S\\	   SS4S jjr
\U 4S j5       r S%S\S	\S
\\   S\S\\   S\4S jjrS\S	\S\4S jrS	\S\\   4S jrS\S\S	\S\	S\4
S jrS	\S
\\   S\S\S\S\4S jr  S$S	\S\R.                  S\S\S\S
\\   S\S\S\S\\   S\\	   S\4S jjrS\S\S\\\R8                  4   S\4S jr S&S \\\   \ \   \4   S!\	S\\	   4S" jjr!S#r"U =r#$ )'CloudflareChatConfig&   N
max_tokensstreamreturnc                     [        5       nUR                  5        H(  u  pEUS:w  d  M  Uc  M  [        U R                  XE5        M*     g )Nr"   )localsitemssetattrr#   )r"   r1   r2   locals_keyvalues         r$   r!   CloudflareChatConfig.__init__*   s7    
 (!--/JCf}!23 *r&   c                     > [         TU ]  5       $ N)r    
get_config)clsr#   s    r$   r>   CloudflareChatConfig.get_config4   s    w!##r&   headersmodelmessagesoptional_paramsapi_keyc                 4    Uc  [        S5      eSSSU-   S.nU$ )NzMissing CloudflareError API Key - A call is being made to cloudflare but no key is set either in the environment variables or via paramszapplication/jsonzapbplication/jsonzBearer )acceptzcontent-typeAuthorization)
ValueError)r"   rA   rB   rC   rD   rE   s         r$   validate_environment)CloudflareChatConfig.validate_environment8   s:     ? [  )/&0

 r&   api_basec                 
    X-   $ r=    )r"   rL   rB   s      r$   get_complete_url%CloudflareChatConfig.get_complete_urlK   s    r&   c                 
    SS/$ )Nr2   r1   rN   )r"   rB   s     r$   get_supported_openai_params0CloudflareChatConfig.get_supported_openai_paramsN   s    
 	
r&   non_default_paramsdrop_paramsc                     U R                  US9nUR                  5        H  u  pgUS:X  a  XrS'   M  Xe;   d  M  XrU'   M     U$ )N)rB   max_completion_tokensr1   )rR   r6   )r"   rT   rD   rB   rU   supported_openai_paramsparamr:   s           r$   map_openai_params&CloudflareChatConfig.map_openai_paramsT   sT     #'"B"B"B"O.446LE//05-1).&	 7
 r&   litellm_paramsc                     [         R                  R                  5       nUR                  5        H  u  pxXs;  d  M  XU'   M     SU0UEn	U	$ )NrC   )litellmr/   r>   r6   )
r"   rB   rC   rD   r\   rA   configkvdatas
             r$   transform_request&CloudflareChatConfig.transform_requestc   sU     --88:LLNDA'%&" #
 

 r&   raw_responsemodel_responselogging_objrequest_dataencoding	json_modec                    UR                  5       nUS   S   UR                  S   R                  l        [        R
                  R                  XaS9n[        U	R                  US   S   S   R                  SS5      5      5      n[        [        R                  " 5       5      Ul        S	U-   Ul        [        UUX-   S
9n[        USU5        U$ )Nresultr   r   )rC   rB   choicesr   content zcloudflare/)prompt_tokenscompletion_tokenstotal_tokensusage)jsonrm   r   rn   r^   utilsget_token_countlenencodegetinttimecreatedrB   r   r7   )r"   rB   re   rf   rg   rh   rC   rD   r\   ri   rE   rj   completion_responserp   rq   rs   s                   r$   transform_response'CloudflareChatConfig.transform_responsev   s     +//14G4Q5
q!))1  55x5UOON95a8CGG	SUVW
 "%TYY[!1,u4'/&:

 	/r&   error_messager   c                     [        UUS9$ )N)r   r   )r   )r"   r   r   rA   s       r$   get_error_class$CloudflareChatConfig.get_error_class   s     #!
 	
r&   streaming_responsesync_streamc                     [        UUUS9$ )N)r   r   rj   )CloudflareChatResponseIterator)r"   r   r   rj   s       r$   get_model_response_iterator0CloudflareChatConfig.get_model_response_iterator   s     .1#
 	
r&   rN   )NNr=   )F)$r'   r(   r)   r*   r1   r   rz   __annotations__r2   boolr!   classmethodr>   dictstrr   r   rJ   rO   rR   rZ   rc   r   r   r   r   r~   r   Headersr
   r   r   r   r   r+   r,   r-   s   @r$   r/   r/   &   sj    $J$!FHTN! %)!%4SM4 4 
	4 $ $ "&  '(	
  # 
&   S  S  
 
c 
   	
  
 '( 	
   
< "&$(!! nn! &	!
 '! ! '(! ! ! ! #! D>! 
!F
 
/2
=B4CV=W
	
 %*	

!(3-s1C]"RS

 

 D>	

 

r&   r/   c                   &    \ rS rSrS\S\4S jrSrg)r      chunkr3   c           
           SnS nSnSnS nS n[        UR                  SS5      5      nSU;   a  US   n[        UUUUUUUS9n	U	$ ! [        R                   a    [        SU 35      ef = f)Nro   Findexr   r   )texttool_useis_finishedfinish_reasonrs   r   provider_specific_fieldsz"Failed to decode JSON from chunk: )rz   ry   r   rt   JSONDecodeErrorrI   )
r"   r   r   r   r   r   rs   r   r   returned_chunks
             r$   chunk_parser+CloudflareChatResponseIterator.chunk_parser   s    	KD>BHKM8<E'+$		'1-.EU"Z(2!'+)AN "!## 	KA%IJJ	Ks   AA #A(rN   N)r'   r(   r)   r*   r   r   r   r+   rN   r&   r$   r   r      s    K$ K+@ Kr&   r   )rt   r{   typingr   r   r   r   r   r   r^   )litellm.llms.base_llm.base_model_iteratorr   )litellm.llms.base_llm.chat.transformationr	   r
   r   litellm.types.llms.openair   litellm.types.utilsr   r   r   r   r   r   r/   r   rN   r&   r$   <module>r      s^      A A   O 
 7 
& 
E
: E
PK%> Kr&   