
    i#                         d Z ddlZddlZddlmZ ddl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 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 G d de      Z G d de      Z y)z
Streaming iterators for the Interactions API.

This module provides streaming iterators that properly stream SSE responses
from the Google Interactions API, similar to the responses API streaming iterator.
    N)datetime)AnyDictOptional)verbose_logger)STREAM_SSE_DONE_STRING)run_async_function)process_response_headers)Logging)get_api_base)executor)BaseInteractionsAPIConfig) InteractionsAPIStreamingResponse)CustomStreamWrapperc                       e Zd ZdZ	 	 ddej
                  dee   dede	dee
eef      dee   fd	Zd
edee   fdZd Zy)$BaseInteractionsAPIStreamingIteratorz
    Base class for streaming iterators that process responses from the Interactions API.

    This class contains shared logic for both synchronous and asynchronous iterators.
    Nresponsemodelinteractions_api_configlogging_objlitellm_metadatacustom_llm_providerc                    || _         || _        || _        d| _        || _        d | _        t        j                         | _        || _	        || _
        t        |xs d| j                  j                  j                  di             }|r|j                  di       ni }|j                  dd       |d| _        t        | j                   j                   xs i       | j                  d<   y )	NF litellm_params)r   optional_params
model_infoid)model_idapi_baseadditional_headers)r   r   r   finishedr   completed_responser   now
start_timer   r   r   model_call_detailsget_hidden_paramsr
   headers)	selfr   r   r   r   r   r   	_api_base_model_infos	            x/Users/manta/Documents/Projects/TheRoad-I1/.venv/lib/python3.12/site-packages/litellm/interactions/streaming_iterator.py__init__z-BaseInteractionsAPIStreamingIterator.__init__$   s     !
&'>$NR",,. !1#6  !+2 ,,??CC "
	 7G  r2B 	 $d3!
 5MMM!!'R5
01    chunkreturnc                    |syt        j                  |      }|y|t        k(  rd| _        y	 t	        j
                  |      }t        |t              r]| j                  j                  | j                  || j                        }|r't        |dd      dk(  r|| _        | j                          |S y# t        j                  $ r t!        j"                  d|dd  d       Y yw xY w)	z/Process a single chunk of data from the stream.NT)r   parsed_chunkr   status	completedz!Failed to parse streaming chunk:    z...)r   _strip_sse_data_from_chunkr   r"   jsonloads
isinstancedictr   transform_streaming_responser   r   getattrr#   "_handle_logging_completed_responseJSONDecodeErrorr   debug)r*   r0   stripped_chunkr3   streaming_responses        r-   _process_chunkz3BaseInteractionsAPIStreamingIterator._process_chunkK   s     -GGN! 33 DM	::n5L ,-00MM"jj%1$($4$4 N  # ' 2HdC{R.@D+;;=))## 	  3N4C4H3IM 	s   BB1 1/C#"C#c                      y)z9Base implementation - should be overridden by subclasses.N r*   s    r-   r>   zGBaseInteractionsAPIStreamingIterator._handle_logging_completed_responsez   s    r/   NN)__name__
__module____qualname____doc__httpxResponser   strr   LiteLLMLoggingObjr   r   r.   r   rC   r>   rE   r/   r-   r   r      s     6:-1%
..%
 }%
 ";	%

 '%
 #4S>2%
 &c]%
N-C -H5U,V -^r/   r   c                        e Zd ZdZ	 	 ddej
                  dee   dede	dee
eef      dee   f fdZd	 Zd
efdZd Z xZS ) InteractionsAPIStreamingIteratorzV
    Async iterator for processing streaming responses from the Interactions API.
    r   r   r   r   r   r   c                 Z    t         |   ||||||       |j                         | _        y N)r   r   r   r   r   r   )superr.   aiter_linesstream_iteratorr*   r   r   r   r   r   r   	__class__s          r-   r.   z)InteractionsAPIStreamingIterator.__init__   s<     	$;#- 3 	 	
  (335r/   c                     | S NrE   rF   s    r-   	__aiter__z*InteractionsAPIStreamingIterator.__aiter__       r/   r1   c                 $  K   	 	 	 | j                   j                          d {   }| j	                  |      }| j                  rt        ||S K7 ,# t        $ r d| _        t        w xY w# t
        j                  $ r}d| _        |d }~ww xY wwNT)rV   	__anext__StopAsyncIterationr"   rC   rL   	HTTPErrorr*   r0   resultes       r-   r_   z*InteractionsAPIStreamingIterator.__anext__   s     	-"&"6"6"@"@"BBE
 ,,U3==,,'!M  C) -$(DM,,-  	 DMG	sT   BA, A AA &A, BA, A A))A, ,B?	BBBc                 r   ddl }|j                  | j                        }t        j                  | j
                  j                  || j                  t        j                         d             t        j                  | j
                  j                  |d| j                  t        j                                y)z8Handle logging for completed responses in async context.r   N)rc   r%   end_time	cache_hitrc   rg   r%   rf   )copydeepcopyr#   asynciocreate_taskr   async_success_handlerr%   r   r$   r   submitsuccess_handlerr*   ri   logging_responses      r-   r>   zCInteractionsAPIStreamingIterator._handle_logging_completed_response   s    ==)@)@A22'??!	 3 	
 	,,#\\^	
r/   rG   )rH   rI   rJ   rK   rL   rM   r   rN   r   rO   r   r   r.   r[   r   r_   r>   __classcell__rX   s   @r-   rQ   rQ      s     6:-16..6 }6 ";	6
 '6 #4S>26 &c]6&!A .
r/   rQ   c                        e Zd ZdZ	 	 ddej
                  dee   dede	dee
eef      dee   f fdZd	 Zd
efdZd Z xZS )$SyncInteractionsAPIStreamingIteratorz\
    Synchronous iterator for processing streaming responses from the Interactions API.
    r   r   r   r   r   r   c                 Z    t         |   ||||||       |j                         | _        y rS   )rT   r.   
iter_linesrV   rW   s          r-   r.   z-SyncInteractionsAPIStreamingIterator.__init__   s<     	$;#- 3 	 	
  (224r/   c                     | S rZ   rE   rF   s    r-   __iter__z-SyncInteractionsAPIStreamingIterator.__iter__   r\   r/   r1   c                     	 	 	 t        | j                        }| j	                  |      }| j                  rt        ||S ># t        $ r d| _        t        w xY w# t
        j                  $ r}d| _        |d }~ww xY wr^   )nextrV   StopIterationr"   rC   rL   ra   rb   s       r-   __next__z-SyncInteractionsAPIStreamingIterator.__next__   s    	( !5!56E
 ,,U3=='''!M  % ($(DM''(  	 DMG	s3   A A &A  A AA A<.	A77A<c                 V   ddl }|j                  | j                        }t        | j                  j
                  || j                  t        j                         d       t        j                  | j                  j                  |d| j                  t        j                                y)z7Handle logging for completed responses in sync context.r   N)async_functionrc   r%   rf   rg   rh   )ri   rj   r#   r	   r   rm   r%   r   r$   r   rn   ro   rp   s      r-   r>   zGSyncInteractionsAPIStreamingIterator._handle_logging_completed_response   sz    ==)@)@A++AA#\\^	
 	,,#\\^	
r/   rG   )rH   rI   rJ   rK   rL   rM   r   rN   r   rO   r   r   r.   ry   r   r}   r>   rr   rs   s   @r-   ru   ru      s     6:-15..5 }5 ";	5
 '5 #4S>25 &c]5&: .
r/   ru   )!rK   rk   r8   r   typingr   r   r   rL   litellm._loggingr   litellm.constantsr   #litellm.litellm_core_utils.asyncifyr	   'litellm.litellm_core_utils.core_helpersr
   *litellm.litellm_core_utils.litellm_loggingr   rO   :litellm.litellm_core_utils.llm_response_utils.get_api_baser   /litellm.litellm_core_utils.thread_pool_executorr   1litellm.llms.base_llm.interactions.transformationr   litellm.types.interactionsr   litellm.utilsr   r   rQ   ru   rE   r/   r-   <module>r      sl       & &  + 4 B L S S D W ._ _DG
'K G
TF
+O F
r/   