
    g                         d dl Z 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  G d d      Zy)    N)datetime)ListOptional)verbose_proxy_logger)Logging)&PassThroughEndpointLoggingResultValues)!StandardPassThroughResponseObject   )"AnthropicPassthroughLoggingHandler)VertexPassthroughLoggingHandler)PassThroughEndpointLogging)EndpointTypec                       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edededede	de
d	ee   d
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              k/var/www/openai/venv/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r   r   c           
        K   t         j                  |      }d}	i }
|t        j                  k(  r(t	        j
                  | |||||||      }|d   }	|d   }
n:|t        j                  k(  r't        j                  | |||||||      }|d   }	|d   }
|	t        d|       }	t        j                  | j                  |	||df      j                           | j                  d
|	||d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kwargsz8cannot parse chunks to standard response object. Chunks=)r   F)targetargs)r*   r   r   	cache_hit )r   _convert_raw_bytes_to_str_linesr   	ANTHROPICr   *_handle_logging_anthropic_collected_chunks	VERTEX_AIr   '_handle_logging_vertex_collected_chunksr	   	threadingThreadsuccess_handlerstartasync_success_handler)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, 	&660		
 %'7!77 
3!	

 
 	
 	
s   C-C7/C50C7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   sY     xx	*11': +7*<*<T*BS*B$djjl*B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 L
.L
)CL
 L
 	L

 $L
 L
 ;L
 L
 L
\ 4; 49  r?   r   )r   r5   r   typingr   r   rL   litellm._loggingr   *litellm.litellm_core_utils.litellm_loggingr   rO   litellm.proxy._typesr   litellm.types.utilsr	   ;llm_provider_handlers.anthropic_passthrough_logging_handlerr   8llm_provider_handlers.vertex_passthrough_logging_handlerr   r7   r   typesr   r   r/   r?   r&   <module>rY      s>       !  1 S G A 8 I Ir?   