
    j(                         d Z ddlmZ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 ddlmZmZ dd	lmZ d
dlmZ  G d de      Z G d de      Zy)z
Support for CometAPI's `/v1/chat/completions` endpoint.

Based on OpenAI-compatible API interface implementation
Documentation: [CometAPI Documentation Link]
    )AnyAsyncIteratorIteratorListOptionalTupleUnionN)BaseModelResponseIterator)BaseLLMException)AllMessageValuesChatCompletionToolParam)ModelResponseModelResponseStream   )OpenAIGPTConfig   CometAPIExceptionc                   Z    e Zd ZdZdededededef
 fdZ	 ddedee	   d	e
ed
      deee	   e
ed
      f   f fdZdedee	   dedededef fdZ	 dde
e   de
e   dededede
e   defdZdededeeej$                  f   defdZ	 ddeee   ee   ef   dede
e   defdZ xZS )CometAPIConfigz
    CometAPI configuration class, inherits from OpenAIGPTConfig
    
    Since CometAPI is OpenAI-compatible API, we inherit from OpenAIGPTConfig
    and only need to override necessary methods to handle CometAPI-specific features
    non_default_paramsoptional_paramsmodeldrop_paramsreturnc                 >    t         |   ||||      }i }|r||d<   |S )zA
        Map OpenAI format parameters to CometAPI format
        
extra_body)supermap_openai_params)selfr   r   r   r   mapped_openai_paramsr   	__class__s          /Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/litellm/llms/cometapi/chat/transformation.pyr   z CometAPIConfig.map_openai_params   s;      %w8 

 &(
 1; .##    messagestoolsr   c                 &    t         |   |||      S )zU
        Remove cache control flags from messages and tools if not supported
        )r   1remove_cache_control_flag_from_messages_and_tools)r    r   r%   r&   r"   s       r#   r(   z@CometAPIConfig.remove_cache_control_flag_from_messages_and_tools8   s     wH8U
 	
r$   litellm_paramsheadersc                 t    |j                  di       }t        | 	  |||||      }|j                  |       |S )z
        Transform the overall request to be sent to the API.

        Returns:
            dict: The transformed request. Sent as the body of the API call.
        r   )popr   transform_requestupdate)	r    r   r%   r   r)   r*   r   responser"   s	           r#   r-   z CometAPIConfig.transform_requestF   sD     %((r:
7,8_ng
 	
#r$   api_baseapi_keystreamc                     |d}d}|j                  d      }||v r|S |j                  d      r| d| S |j                  d      r| | S |dk(  r| d| S d|j                  dd      d	   vr| d| S | d| S )
z
        Get the complete URL for the CometAPI call.

        Returns:
            str: The complete URL for the API call.
        zhttps://api.cometapi.com/v1zchat/completions/z/v1z/v1/zhttps://api.cometapi.comz//   )rstripendswithsplit)r    r0   r1   r   r   r)   r2   endpoints           r#   get_complete_urlzCometAPIConfig.get_complete_url[   s      4H% ??3' xO U#Zq
++V$Zz**11ZtH:..tQ/33ZtH:..1XJ''r$   error_messagestatus_codec                     t        |||      S )z6
        Return CometAPI-specific error class
        messager=   r*   r   )r    r<   r=   r*   s       r#   get_error_classzCometAPIConfig.get_error_class   s     !!#
 	
r$   streaming_responsesync_stream	json_modec                     t        |||      S )zE
        Get model response iterator for streaming responses
        )rB   rC   rD   )&CometAPIChatCompletionStreamingHandler)r    rB   rC   rD   s       r#   get_model_response_iteratorz*CometAPIConfig.get_model_response_iterator   s     61#
 	
r$   )N)F)__name__
__module____qualname____doc__dictstrboolr   r   r   r   r   r(   r-   r;   intr	   httpxHeadersr   rA   r   r   r   r   rG   __classcell__)r"   s   @r#   r   r      s   $ $ $ 	$
 $ 
$> <@	

 '(
 678	

 
t$%x5N0O'PP	Q
 '( 	
   
8 "&&(3-&( #&( 	&(
 &( &( &( 
&(P

 
 tU]]*+	

 

& %*	
!(3-s1C]"RS
 
 D>	

 

r$   r   c                        e Zd ZdZdedefdZy)rF   zB
    Handler for CometAPI streaming chat completion responses
    chunkr   c                    	 d|v rF|d   }dj                  |j                  dd            }t        ||j                  dd      ddi	      g }|d
   D ]8  }d|v r!d|d   v r|d   j                  d      |d   d<   |j                  |       : t	        |d   d|d   |j                  d      |d   |      S # t
        $ r}t        d| d| dddi	      d}~wt        $ r}|d}~ww xY w)zA
        Parse individual chunks from streaming response
        errorzCometAPI Error: {}r@   zUnknown errorcodei  zContent-Typezapplication/jsonr?   choicesdelta	reasoningreasoning_contentidzchat.completion.chunkcreatedusager   )r\   objectr]   r^   r   rX   z
KeyError: z), Got unexpected response from CometAPI: N)formatgetr   appendr   KeyError	Exception)r    rT   error_chunkr<   new_choiceschoicees          r#   chunk_parserz3CometAPIChatCompletionStreamingHandler.chunk_parser   s9   $	%#Gn 4 ; ;OOI?! () + <+-?@  K	*f$w)G;A'?;N;N{;[F7O$78""6*	 + ';.i(ii(Gn#   	#$QC'PQVPWX');< 
  	G	s$   B4B7 7	C) CC)"C$$C)N)rH   rI   rJ   rK   rL   r   ri    r$   r#   rF   rF      s    ($ (+> (r$   rF   )rK   typingr   r   r   r   r   r   r	   rP   )litellm.llms.base_llm.base_model_iteratorr
   )litellm.llms.base_llm.chat.transformationr   litellm.types.llms.openair   r   litellm.types.utilsr   r   openai.chat.gpt_transformationr   common_utilsr   r   rF   rj   r$   r#   <module>rr      sG    N M M  O F O B = ,J
_ J
Z--F -r$   