
    h                         d dl Z d dlmZ d dlmZmZ d dlZd dlmZ d dlm	Z
 d dlmZ d dlmZ d dlmZ d d	lmZ d
dlmZ d
dlmZ d
dlmZ  G d d      Zy)    N)datetime)ListOptional)verbose_proxy_logger)Logging)executor)&PassThroughEndpointLoggingResultValues)EndpointType)!StandardPassThroughResponseObject   )"AnthropicPassthroughLoggingHandler)VertexPassthroughLoggingHandler)PassThroughEndpointLoggingc                       e Zd Zedej
                  dee   dede	de
dedefd       Ze	 ddedededede	de
d
ee   de
dee   fd       Zed
ee   dee   fd       Zy	)PassThroughStreamingHandlerresponserequest_bodylitellm_logging_objendpoint_type
start_timepassthrough_success_handler_obj	url_routec                n  K   	 g }| j                         2 3 d{   }|j                  |       |  7 6 t        j                         }	t	        j
                  t        j                  ||||xs i ||||	             y# t        $ r'}
t        j                  dt        |
               d}
~
ww xY ww)zt
        - Yields chunks from the response
        - Collect non-empty chunks for post-processing (logging)
        N)r   r   r   r   r   r   	raw_bytesend_timezError in chunk_processor: )aiter_bytesappendr   nowasynciocreate_taskr   #_route_streaming_logging_to_handler	Exceptionr   errorstr)r   r   r   r   r   r   r   r   chunkr   es              r/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/proxy/pass_through_endpoints/streaming_handler.pychunk_processorz+PassThroughStreamingHandler.chunk_processor   s     	%'I'335  e  '5
  ||~H+OO(;4S'!-!3"/)'% P 	  	 &&)CCF8'LM	sC   B5B 868B 8A	B B5	B2"B--B22B5Nr   r   modelc	                   K   t         j                  |      }	d}
i }|t        j                  k(  r(t	        j
                  | ||||||	|      }|d   }
|d   }n;|t        j                  k(  r(t        j                  | ||||||	||	      }|d   }
|d   }|
t        d|	       }
 | j                  d|
||dd	| d{    | j                         du ryt        j                  | j                  f|
|d|d
| y7 =w)z
        Route the logging for the collected chunks to the appropriate handler

        Supported endpoint types:
        - Anthropic
        - Vertex AI
        N)r   r   r   r   r   r   
all_chunksr   resultkwargs)	r   r   r   r   r   r   r+   r   r)   z8cannot parse chunks to standard response object. Chunks=)r   F)r,   r   r   	cache_hit)r,   r   r.   r    )r   _convert_raw_bytes_to_str_linesr
   	ANTHROPICr   *_handle_logging_anthropic_collected_chunks	VERTEX_AIr   '_handle_logging_vertex_collected_chunksr   async_success_handler*_should_run_sync_callbacks_for_async_callsr   submitsuccess_handler)r   r   r   r   r   r   r   r   r)   r+    standard_logging_response_objectr-   ,anthropic_passthrough_logging_handler_result)vertex_passthrough_logging_handler_results                 r'   r!   z?PassThroughStreamingHandler._route_streaming_logging_to_handler?   s|    & 1PP


  	) L222;]  <I  <I$70O#)+%%!	<8 =XF - B(KFl444/WW(;4S'!-"/))%
 6 :(C - ?xHF+3/PST^S_`0, 8!77 
3!	

 
 	
 	
 IIKuT//	
3!	
 	
	
s   B;C==C;>>C=returnc                     dj                  |       j                  d      }|j                  d      D cg c]#  }|j                         s|j                         % }}|S c c}w )a  
        Converts a list of raw bytes into a list of string lines, similar to aiter_lines()

        Args:
            raw_bytes: List of bytes chunks from aiter.bytes()

        Returns:
            List of string lines, with each line being a complete data: {} chunk
            zutf-8
)joindecodesplitstrip)r   combined_strlineliness       r'   r0   z;PassThroughStreamingHandler._convert_raw_bytes_to_str_lines   sV     xx	*11': +7*<*<T*BS$djjlSS Ts   A 
A )N)__name__
__module____qualname__staticmethodhttpxResponser   dictLiteLLMLoggingObjr
   r   r   r$   r(   r   bytesr!   r0   r/   r>   r'   r   r      s   $..$tn$ /$ $	$
 $ *D$ $ $L   $P
.P
)CP
 P
 	P

 $P
 P
 ;P
 P
 }P
 P
d 4; 49  r>   r   )r   r   typingr   r   rK   litellm._loggingr   *litellm.litellm_core_utils.litellm_loggingr   rN   /litellm.litellm_core_utils.thread_pool_executorr   litellm.proxy._typesr	   :litellm.types.passthrough_endpoints.pass_through_endpointsr
   litellm.types.utilsr   ;llm_provider_handlers.anthropic_passthrough_logging_handlerr   8llm_provider_handlers.vertex_passthrough_logging_handlerr   r8   r   r   r/   r>   r'   <module>rY      s>      !  1 S D G S A 8L Lr>   