o
    hp                     @  sL  d dl mZ d dlmZ d dlmZ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mZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z! d dl"m#Z#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/m0Z0 ddl1m2Z2 ddl3m4Z4 G dd dZ&eG dd dZ5G dd dZ6G dd dZ7dS )    )annotations)AsyncIterator)	dataclassfield)AsyncStream)ChatCompletionChunk)CompletionUsage)ResponseResponseCompletedEventResponseContentPartAddedEventResponseContentPartDoneEventResponseCreatedEvent'ResponseFunctionCallArgumentsDeltaEventResponseFunctionToolCallResponseOutputItemResponseOutputItemAddedEventResponseOutputItemDoneEventResponseOutputMessageResponseOutputRefusalResponseOutputTextResponseReasoningItem&ResponseReasoningSummaryPartAddedEvent%ResponseReasoningSummaryPartDoneEvent&ResponseReasoningSummaryTextDeltaEventResponseRefusalDeltaEventResponseTextDeltaEventResponseUsage)ContentSummary)Part)ResponseReasoningTextDeltaEvent)ResponseReasoningTextDoneEvent)InputTokensDetailsOutputTokensDetails   )TResponseStreamEvent   )FAKE_RESPONSES_IDc                   @  s   e Zd ZdddZdS )r   textstrtypec                 C  s   || _ || _d S )Nr(   r*   )selfr(   r*    r-   f/var/www/html/openai_agents/venv/lib/python3.10/site-packages/agents/models/chatcmpl_stream_handler.py__init__2   s   
zPart.__init__N)r(   r)   r*   r)   )__name__
__module____qualname__r/   r-   r-   r-   r.   r   1   s    r   c                   @  s   e Zd ZU dZded< dZded< dZded< dZd	ed
< ee	dZ
ded< ee	dZded< ee	dZded< dZded< dZded< dS )StreamingStateFboolstartedNz%tuple[int, ResponseOutputText] | Nonetext_content_index_and_outputz(tuple[int, ResponseOutputRefusal] | None refusal_content_index_and_outputz(tuple[int, ResponseReasoningItem] | None"reasoning_content_index_and_output)default_factoryz#dict[int, ResponseFunctionToolCall]function_callszdict[int, bool]function_call_streamingzdict[int, int]function_call_output_idx r)   thinking_textz
str | Nonethinking_signature)r0   r1   r2   r5   __annotations__r6   r7   r8   r   dictr:   r;   r<   r>   r?   r-   r-   r-   r.   r3   7   s   
 r3   c                   @  s   e Zd Zdd ZdddZdS )	SequenceNumberc                 C  s
   d| _ d S )Nr   _sequence_number)r,   r-   r-   r.   r/   G   s   
zSequenceNumber.__init__returnintc                 C  s   | j }|  j d7  _ |S )Nr&   rC   )r,   numr-   r-   r.   get_and_incrementJ   s   z SequenceNumber.get_and_incrementN)rE   rF   )r0   r1   r2   r/   rH   r-   r-   r-   r.   rB   F   s    rB   c                   @  s   e Zd Zed
ddZd	S )ChatCmplStreamHandlerresponser	   stream AsyncStream[ChatCompletionChunk]rE   #AsyncIterator[TResponseStreamEvent]c               
   C s
  d }t  }t }|2 z3 d H W }|js"d|_t|d| dV  t|dr*|jnd }|jr5|jd js6q|jd j}t|drh|j	rh|j	D ] }t
|trg|dd}	|	r]| j|	7  _|d	}
|
rg|
|_qGt|d
r|j}|r|jsdtttdddgddf|_ttttdddgdddd| dV  ttddtdddd| dV  |r|jrt|tddd| dV  |jd jd }|j| }t|dd}||jd jd< t|drP|j}|r|jsdttg tdddgddf|_tttg tdddgdddd| dV  |rP|jrPt|tddd| dV  |jd jd u r5tdddg|jd _|jd jd }|j| }t|dd}||jd jd< |jd ur|jsd}|jrd|d7 }|jrl|d7 }|t ddg df|_t!tg dddd}t||jd ud| dV  t"|jd t|jd ut ddg dd | d!V  t#|jd |jt|jd ud"| g d#V  |jd  j|j7  _t|d$r<|j$r<|jsd}|jr|d7 }|jr|d7 }|t%dd$d%f|_t!tg dddd}t||jd ud| dV  t"|jd t|jd ut ddg dd | d!V  t&|jd |j$t|jd ud&| d'V  |jd  j$|j$7  _$|j'r|j'D ]}|j(|j)vr_t*tddd(dd)|j)|j(< d*|j+|j(< |j,}|j)|j(  j-|ro|j-ndpsd7  _-|r|j.r|j.|j)|j( _.|j/r|j/|j)|j( _0|j)|j( }|j+|j( s|j.r|j0rd}|jr|d7 }|jr|d7 }|jr|d7 }|t1d+d, |j+2 D 7 }d|j+|j(< ||j3|j(< tt*t|j0d|j.d(d-|d| dV  |j+|j(d*r|r|j-r|j3|j( }t4|j-t|d.| d/V  qCq6 |jro|jd jrDt5|jd jdkrDt6tddt7|jd jd jddd0| dV  n|jd jd urat8tdd|jd jd jd1| d2V  t9|jd dd3| dV  d}|jry|d7 }|jr|d7 }t:|jd t|jd u|jd d4| d!V  |jr|d7 }t:|jd t|jd u|jd d4| d!V  |j); D ]\}}|j+|d*r|j3| }t9t*t|j0|j-|j.d(d-|d3| dV  qd}|jr|d7 }|jr|d7 }|jr|d7 }|t1d5d, |j+2 D 7 }tt*t|j0|j-|j.d(d-|d| dV  t4|j-t|d.| d/V  t9t*t|j0|j-|j.d(d-|d3| dV  qg }|jrq|jd }|jrd|jsYg |_|j<t|jdd |jrl|j|_=|<| |jsy|jrt!tg ddd6d}|jr|j<|jd  |jr|j<|jd  |<| t9||jd ud3| dV  |j)2 D ]}|<| q|> }||_?|rt@|jApd|jBpd|jCpdtD|jEr|jEjFr|jEjFndd7tG|jHr|jHjIr|jHjIndd8d9nd |_tJ|d:| dV  d S );NTzresponse.created)rJ   r*   sequence_numberusager   thinking_blocksthinkingr=   	signaturereasoning_contentsummary_textr+   	reasoning)idsummaryr*   zresponse.output_item.added)itemoutput_indexr*   rN   z%response.reasoning_summary_part.added)item_idrY   summary_indexpartr*   rN   z%response.reasoning_summary_text.delta)deltarZ   rY   r[   r*   rN   r&   reasoning_text)rV   rW   contentr*   zresponse.reasoning_text.delta)r]   rZ   rY   content_indexr*   rN   output_text)r(   r*   r   	assistantmessagein_progress)rV   r_   roler*   statuszresponse.content_part.added)r`   rZ   rY   r\   r*   rN   zresponse.output_text.delta)r`   r]   rZ   rY   r*   rN   logprobsrefusal)rh   r*   zresponse.refusal.delta)r`   r]   rZ   rY   r*   rN   function_call)rV   	argumentsnamer*   call_idFc                 s      | ]}|rd V  qdS r&   Nr-   .0	streamingr-   r-   r.   	<genexpr>v      
z6ChatCmplStreamHandler.handle_stream.<locals>.<genexpr>)rV   rl   rj   rk   r*   z&response.function_call_arguments.delta)r]   rZ   rY   r*   rN   z$response.reasoning_summary_part.donezresponse.reasoning_text.done)rZ   rY   r`   r(   r*   rN   zresponse.output_item.donezresponse.content_part.donec                 s  rm   rn   r-   ro   r-   r-   r.   rr     rs   	completed)reasoning_tokens)cached_tokens)input_tokensoutput_tokenstotal_tokensoutput_tokens_detailsinput_tokens_detailszresponse.completed)Kr3   rB   r5   r   rH   hasattrrO   choicesr]   rP   
isinstancerA   getr>   r?   rS   r8   r   r'   r   r   r   AddedEventPartr   rW   r(   rU   r   r    r_   r6   r7   r   r   r   r   rh   r   r   
tool_callsindexr:   r   r;   functionrj   rk   rV   rl   sumvaluesr<   r   lenr   DoneEventPartr!   r   r   itemsappendencrypted_content
model_copyoutputr   prompt_tokenscompletion_tokensry   r#   completion_tokens_detailsru   r"   prompt_tokens_detailsrv   r
   ) clsrJ   rK   rO   staterN   chunkr]   blockr>   rR   rS   current_contentupdated_textnew_contentr^   current_textnew_text_contentr`   assistant_itemrefusal_indextc_deltatc_functionri   function_call_starting_indexrY   r   fallback_starting_indexoutputsreasoning_itemassistant_msgfinal_responser-   r-   r.   handle_streamQ   s  






	

	


	






    E











z#ChatCmplStreamHandler.handle_streamN)rJ   r	   rK   rL   rE   rM   )r0   r1   r2   classmethodr   r-   r-   r-   r.   rI   P   s    rI   N)8
__future__r   collections.abcr   dataclassesr   r   openair   openai.types.chatr   openai.types.completion_usager   openai.types.responsesr	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   .openai.types.responses.response_reasoning_itemr   r   Bopenai.types.responses.response_reasoning_summary_part_added_eventr   r   Aopenai.types.responses.response_reasoning_summary_part_done_eventr   :openai.types.responses.response_reasoning_text_delta_eventr    9openai.types.responses.response_reasoning_text_done_eventr!   %openai.types.responses.response_usager"   r#   r   r%   fake_idr'   r3   rB   rI   r-   r-   r-   r.   <module>   s(    X
