
    j6                        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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mZmZmZmZmZ d dlmZ  G d d      Z G d de      Z  G d de      Z! G d de      Z"y)    N)datetime)AnyDictOptional)STREAM_SSE_DONE_STRING)run_async_function)Logging)executor)BaseResponsesAPIConfig)ResponsesAPIRequestUtils)OutputTextDeltaEventResponseAPIUsageResponseCompletedEventResponsesAPIResponseResponsesAPIStreamEventsResponsesAPIStreamingResponse)CustomStreamWrapperc                   x    e Zd ZdZ	 	 ddej
                  dededede	e
eef      de	e   fd	Zd
e	e   fdZd Zy)!BaseResponsesAPIStreamingIteratorz
    Base class for streaming iterators that process responses from the Responses API.

    This class contains shared logic for both synchronous and asynchronous iterators.
    Nresponsemodelresponses_api_provider_configlogging_objlitellm_metadatacustom_llm_providerc                     || _         || _        || _        d| _        || _        d | _        t        j                         | _        || _	        || _
        y )NF)r   r   r   finishedr   completed_responser   now
start_timer   r   )selfr   r   r   r   r   r   s          }/Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/litellm/responses/streaming_iterator.py__init__z*BaseResponsesAPIStreamingIterator.__init__!   sP     !
&-J*KO",,. !1#6     returnc                 ,   |syt        j                  |      }|y|t        k(  rd| _        y	 t	        j
                  |      }t        |t              r| j                  j                  | j                  || j                        }t        |dd      }|r9t        j                  || j                  | j                         }t#        |d|       |rt        |dd      t$        j&                  k(  r}|| _        t*        j,                  rV| j                  Jt        |dd      }|r;t        |dd      }|,	 | j                  j/                  |      }|t#        |d	|       | j3                          |S y# t0        $ r Y w xY w# t        j4                  $ r Y yw xY w)
z.Process a single chunk of data from the streamNT)r   parsed_chunkr   r   )responses_api_responser   r   typeusageresultcost)r   _strip_sse_data_from_chunkr   r   jsonloads
isinstancedictr   transform_streaming_responser   r   getattrr   /_update_responses_api_response_id_with_model_idr   r   setattrr   RESPONSE_COMPLETEDr   litellminclude_cost_in_streaming_usage_response_cost_calculator	Exception"_handle_logging_completed_responseJSONDecodeError)	r!   chunkr'   openai_responses_api_chunkresponse_objectr   response_obj	usage_objr-   s	            r"   _process_chunkz0BaseResponsesAPIStreamingIterator._process_chunk6   s    $>>uE= ** DM?	::e,L ,-66SS"jj%1$($4$4 T  + #**DjRV"W"7gg/>)-)>)>,0,D,D H
 6
HM / :FDI/BBC /ID+  ?? ,,8GN6
DH (DK ,gtEI  )4
!)(,(8(8(R(R3? )S )* %)
 (,'7(/	64(H
 ;;=11 (1 !)$(!) ## 		s6   DE= 0+E. E= .	E:7E= 9E::E= =FFc                      y)z8Base implementation - should be overridden by subclassesN r!   s    r"   r<   zDBaseResponsesAPIStreamingIterator._handle_logging_completed_response   s    r$   NN)__name__
__module____qualname____doc__httpxResponsestrr   LiteLLMLoggingObjr   r   r   r#   r   rC   r<   rE   r$   r"   r   r      s}     6:-17..7 7 (>	7
 '7 #4S>27 &c]7*Nx0M'N N`r$   r   c                        e Zd ZdZ	 	 ddej
                  d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 )ResponsesAPIStreamingIteratorzS
    Async iterator for processing streaming responses from the Responses API.
    r   r   r   r   r   r   c                 X    t         |   ||||||       |j                         | _        y N)superr#   aiter_linesstream_iteratorr!   r   r   r   r   r   r   	__class__s          r"   r#   z&ResponsesAPIStreamingIterator.__init__   s7     	)	
  (335r$   c                     | S rS   rE   rF   s    r"   	__aiter__z'ResponsesAPIStreamingIterator.__aiter__       r$   r%   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!   r>   r,   es       r"   r^   z'ResponsesAPIStreamingIterator.__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)z7Handle logging for completed responses in async contextr   N)r,   r    end_time	cache_hitr,   re   r    rd   )copydeepcopyr   asynciocreate_taskr   async_success_handlerr    r   r   r
   submitsuccess_handlerr!   rg   logging_responses      r"   r<   z@ResponsesAPIStreamingIterator._handle_logging_completed_response   s    
 	==)@)@A22'??!	 3 	
 	,,#\\^	
r$   rG   )rH   rI   rJ   rK   rL   rM   rN   r   rO   r   r   r   r#   rZ   r   r^   r<   __classcell__rX   s   @r"   rQ   rQ      s{     6:-16..6 6 (>	6
 '6 #4S>26 &c]6&!> .
r$   rQ   c                   ~     e Zd ZdZ	 	 ddej
                  dededede	e
eef      de	e   f fdZd	 Zd
 Zd Z xZS )!SyncResponsesAPIStreamingIteratorzY
    Synchronous iterator for processing streaming responses from the Responses API.
    r   r   r   r   r   r   c                 X    t         |   ||||||       |j                         | _        y rS   )rT   r#   
iter_linesrV   rW   s          r"   r#   z*SyncResponsesAPIStreamingIterator.__init__   s7     	)	
  (224r$   c                     | S rS   rE   rF   s    r"   __iter__z*SyncResponsesAPIStreamingIterator.__iter__   r[   r$   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   r`   ra   s       r"   __next__z*SyncResponsesAPIStreamingIterator.__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)z6Handle logging for completed responses in sync contextr   N)async_functionr,   r    rd   re   rf   )rg   rh   r   r   r   rk   r    r   r   r
   rl   rm   rn   s      r"   r<   zDSyncResponsesAPIStreamingIterator._handle_logging_completed_response	  s|    
 	==)@)@A++AA#\\^	
 	,,#\\^	
r$   rG   )rH   rI   rJ   rK   rL   rM   rN   r   rO   r   r   r   r#   rw   r{   r<   rp   rq   s   @r"   rs   rs      ss     6:-15..5 5 (>	5
 '5 #4S>25 &c]5&.
r$   rs   c                        e Zd ZdZdZ	 	 ddej                  d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defdZdedefdZ xZS )!MockResponsesAPIStreamingIteratoru   
    Mock iterator—fake a stream by slicing the full response text into
    5 char deltas, then emit a completed event.

    Models like o1-pro don't support streaming, so we fake it.
       r   r   r   r   r   r   c           
      l   t         |   ||||||       | j                  j                  | j                  ||      }| j                  |      }t        dt        |      | j                        D 	cg c]:  }	t        t        j                  ||	|	| j                  z    |j                  dd      < }
}	t        j                  r3|1t        |dd       }|"	 |j!                  |      }|t#        |d|       |
t'        t        j(                  |      gz   | _        d| _        y c c}	w # t$        $ r Y <w xY w)	N)r   r   r   r   r   r   )r   raw_responser   r   )r)   deltaitem_idoutput_indexcontent_indexr*   r+   r-   )r)   r   )rT   r#   r   transform_response_api_responser   _collect_textrangelen
CHUNK_SIZEr   r   OUTPUT_TEXT_DELTAidr8   r9   r4   r:   r6   r;   r   r7   _events_idx)r!   r   r   r   r   r   r   transformed	full_textideltasrB   r-   rX   s                r"   r#   z*MockResponsesAPIStreamingIterator.__init__,  ss    	*G#- 3 	 	
 ..NNjj%' O  	 &&{3	 1c)ndoo>	
 ? !-??A$78# ? 	 	
 22{7N4;Wd5I $,7,Q,Q* -R -D '	648 "-@@$!
 
 	E	
. ! s   2?D"!D' '	D32D3c                     | S rS   rE   rF   s    r"   rZ   z+MockResponsesAPIStreamingIterator.__aiter__m  r[   r$   r%   c                    K   | j                   t        | j                        k\  rt        | j                  | j                      }| xj                   dz  c_         |S wN   )r   r   r   r_   r!   evts     r"   r^   z+MockResponsesAPIStreamingIterator.__anext__p  sE     99DLL))$$ll499%		Q	
s   AAc                     | S rS   rE   rF   s    r"   rw   z*MockResponsesAPIStreamingIterator.__iter__w  r[   r$   c                     | j                   t        | j                        k\  rt        | j                  | j                      }| xj                   dz  c_         |S r   )r   r   r   rz   r   s     r"   r{   z*MockResponsesAPIStreamingIterator.__next__z  sA    99DLL))ll499%		Q	
r$   respc                     d}|j                   D ]6  }t        |dd       }|dk(  st        |dg       D ]  }||j                  z  } 8 |S )N r)   messagecontent)outputr4   text)r!   r   outout_item	item_typecs         r"   r   z/MockResponsesAPIStreamingIterator._collect_text  sR    H&$7II% 9b9A166MC : $
 
r$   rG   )rH   rI   rJ   rK   r   rL   rM   rN   r   rO   r   r   r   r#   rZ   r   r^   rw   r{   r   r   rp   rq   s   @r"   r   r   "  s     J 6:-1?..? ? (>	?
 '? #4S>2? &c]?B!> 7 "6 3 r$   r   )#ri   r/   r   typingr   r   r   rL   r8   litellm.constantsr   #litellm.litellm_core_utils.asyncifyr   *litellm.litellm_core_utils.litellm_loggingr	   rO   /litellm.litellm_core_utils.thread_pool_executorr
   .litellm.llms.base_llm.responses.transformationr   litellm.responses.utilsr   litellm.types.llms.openair   r   r   r   r   r   litellm.utilsr   r   rQ   rs   r   rE   r$   r"   <module>r      sw       & &   4 B S D Q <  .n nbI
$E I
XH
(I H
Vf(I fr$   