o
    ùT·j;  ã                   @  sŒ  d dl mZ d dl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mZmZmZ ddlmZ ddlmZmZmZmZmZ d	d
lmZmZ d	dlmZmZmZ d	dlm Z  d	dl!m"Z"m#Z# d	dl$m%Z%m&Z& d	dl'm(Z(mZ) ddl*m+Z+m,Z,m-Z- d	dl.m/Z/ d	dl0m1Z1m2Z2m3Z3m4Z4 d	dl5m6Z6 G dd„ dee+ ƒZ7G dd„ dee+ ƒZ8G dd„ dee+ ƒZ9G dd„ dee+ ƒZ:G dd„ dee+ ƒZ;dS )é    )ÚannotationsN)ÚTracebackType)ÚAnyÚListÚGenericÚIterableÚ	AwaitableÚcast)ÚSelfÚCallableÚIteratorÚAsyncIteratoré   )ÚParsedResponseSnapshot)ÚResponseStreamEventÚResponseTextDoneEventÚResponseCompletedEventÚResponseTextDeltaEventÚ'ResponseFunctionCallArgumentsDeltaEventé   )ÚOmitÚomit)Úis_givenÚconsume_sync_iteratorÚconsume_async_iterator)Ú
model_copy)ÚbuildÚconstruct_type_unchecked)ÚStreamÚAsyncStream)ÚParsedResponser   é   )ÚTextFormatTÚ
parse_textÚparse_response)Ú	ToolParam)ÚParsedContentÚParsedResponseOutputItemÚParsedResponseOutputMessageÚParsedResponseFunctionToolCall)ÚResponseOutputItemc                   @  sf   e Zd Zd(dd„Zd)dd„Zd*dd„Zd+dd„Zd*dd„Zd,dd„Zd-d d!„Z	d.d#d$„Z
d+d%d&„Zd'S )/ÚResponseStreamÚ
raw_streamúStream[RawResponseStreamEvent]Útext_formatútype[TextFormatT] | OmitÚinput_toolsúIterable[ToolParam] | OmitÚstarting_afterú
int | NoneÚreturnÚNonec                C  ó0   || _ |j| _|  ¡ | _t||d�| _|| _d S ©N)r.   r0   ©Ú_raw_streamÚresponseÚ	_responseÚ
__stream__Ú	_iteratorÚResponseStreamStateÚ_stateÚ_starting_after©Úselfr,   r.   r0   r2   © rC   úl/home/dinkstrade/pdmp-scanner/venv/lib/python3.10/site-packages/openai/lib/streaming/responses/_responses.pyÚ__init__"   ó
   

zResponseStream.__init__ú ResponseStreamEvent[TextFormatT]c                 C  s
   | j  ¡ S ©N)r=   Ú__next__©rB   rC   rC   rD   rI   0   s   
zResponseStream.__next__ú*Iterator[ResponseStreamEvent[TextFormatT]]c                 c  s   � | j D ]}|V  qd S rH   ©r=   ©rB   ÚitemrC   rC   rD   Ú__iter__3   s   €
ÿzResponseStream.__iter__r
   c                 C  s   | S rH   rC   rJ   rC   rC   rD   Ú	__enter__7   s   zResponseStream.__enter__c                 c  sD   � | j D ]}| j |¡}|D ]}| jd u s|j| jkr|V  qqd S rH   ©r9   r?   Úhandle_eventr@   Úsequence_number©rB   Ú	sse_eventÚevents_to_fireÚeventrC   rC   rD   r<   :   s   €
€þþzResponseStream.__stream__Úexc_typeútype[BaseException] | NoneÚexcúBaseException | NoneÚexc_tbúTracebackType | Nonec                 C  s   |   ¡  d S rH   ©Úclose©rB   rX   rZ   r\   rC   rC   rD   Ú__exit__A   s   zResponseStream.__exit__c                 C  s   | j  ¡  dS ©zŠ
        Close the response and release the connection.

        Automatically called if the response body is read to completion.
        N)r;   r_   rJ   rC   rC   rD   r_   I   s   zResponseStream.closeúParsedResponse[TextFormatT]c                 C  s    |   ¡  | jj}|stdƒ‚|S )úxWaits until the stream has been read to completion and returns
        the accumulated `ParsedResponse` object.
        ú,Didn't receive a `response.completed` event.©Ú
until_doner?   Ú_completed_responseÚRuntimeError©rB   r:   rC   rC   rD   Úget_final_responseQ   s
   z!ResponseStream.get_final_responsec                 C  s   t | ƒ | S )ú*Blocks until the stream has been consumed.)r   rJ   rC   rC   rD   rg   \   s   zResponseStream.until_doneN)
r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   ©r4   rG   )r4   rK   ©r4   r
   ©rX   rY   rZ   r[   r\   r]   r4   r5   ©r4   r5   ©r4   rc   )Ú__name__Ú
__module__Ú__qualname__rE   rI   rO   rP   r<   ra   r_   rk   rg   rC   rC   rC   rD   r+   !   s    







r+   c                   @  ó*   e Zd Zddd„Zddd„Zddd„ZdS )ÚResponseStreamManagerÚapi_requestú,Callable[[], Stream[RawResponseStreamEvent]]r.   r/   r0   r1   r2   r3   r4   r5   c                C  ó"   d | _ || _|| _|| _|| _d S rH   )Ú_ResponseStreamManager__streamÚ#_ResponseStreamManager__api_requestÚ#_ResponseStreamManager__text_formatÚ#_ResponseStreamManager__input_toolsÚ&_ResponseStreamManager__starting_after©rB   rw   r.   r0   r2   rC   rC   rD   rE   c   ó
   
zResponseStreamManager.__init__úResponseStream[TextFormatT]c                 C  s&   |   ¡ }t|| j| j| jd�| _| jS ©N)r,   r.   r0   r2   )r{   r+   r|   r}   r~   rz   ©rB   r,   rC   rC   rD   rP   q   s   üzResponseStreamManager.__enter__rX   rY   rZ   r[   r\   r]   c                 C  s   | j d ur| j  ¡  d S d S rH   )rz   r_   r`   rC   rC   rD   ra   }   s   
ÿzResponseStreamManager.__exit__N)
rw   rx   r.   r/   r0   r1   r2   r3   r4   r5   )r4   r�   ro   )rr   rs   rt   rE   rP   ra   rC   rC   rC   rD   rv   b   ó    

rv   c                   @  sf   e Zd Zd(dd„Zd)dd„Zd*dd„Zd*dd„Zd+dd„Zd,dd„Zd-d d!„Z	d.d#d$„Z
d+d%d&„Zd'S )/ÚAsyncResponseStreamr,   ú#AsyncStream[RawResponseStreamEvent]r.   r/   r0   r1   r2   r3   r4   r5   c                C  r6   r7   r8   rA   rC   rC   rD   rE   ˆ   rF   zAsyncResponseStream.__init__rG   c                 Ã  s   �| j  ¡ I d H S rH   )r=   Ú	__anext__rJ   rC   rC   rD   r‡   –   s   €zAsyncResponseStream.__anext__ú/AsyncIterator[ResponseStreamEvent[TextFormatT]]c                 C s"   �| j 2 z	3 d H W }|V  q6 d S rH   rL   rM   rC   rC   rD   Ú	__aiter__™   s   €ÿzAsyncResponseStream.__aiter__c                 C sN   �| j 2 z3 d H W }| j |¡}|D ]}| jd u s|j| jkr"|V  qq6 d S rH   rQ   rT   rC   rC   rD   r<   �   s   €€þþzAsyncResponseStream.__stream__r
   c                 Ã  s   �| S rH   rC   rJ   rC   rC   rD   Ú
__aenter__¤   s   €zAsyncResponseStream.__aenter__rX   rY   rZ   r[   r\   r]   c                 Ã  s   �|   ¡ I d H  d S rH   r^   r`   rC   rC   rD   Ú	__aexit__§   s   €zAsyncResponseStream.__aexit__c                 Ã  s   �| j  ¡ I dH  dS rb   )r;   ÚacloserJ   rC   rC   rD   r_   ¯   s   €zAsyncResponseStream.closerc   c                 Ã  s(   �|   ¡ I dH  | jj}|stdƒ‚|S )rd   Nre   rf   rj   rC   rC   rD   rk   ·   s   €z&AsyncResponseStream.get_final_responsec                 Ã  s   �t | ƒI dH  | S )rl   N)r   rJ   rC   rC   rD   rg   Â   s   €zAsyncResponseStream.until_doneN)
r,   r†   r.   r/   r0   r1   r2   r3   r4   r5   rm   )r4   rˆ   rn   ro   rp   rq   )rr   rs   rt   rE   r‡   r‰   r<   rŠ   r‹   r_   rk   rg   rC   rC   rC   rD   r…   ‡   s    







r…   c                   @  ru   )ÚAsyncResponseStreamManagerrw   ú.Awaitable[AsyncStream[RawResponseStreamEvent]]r.   r/   r0   r1   r2   r3   r4   r5   c                C  ry   rH   )Ú#_AsyncResponseStreamManager__streamÚ(_AsyncResponseStreamManager__api_requestÚ(_AsyncResponseStreamManager__text_formatÚ(_AsyncResponseStreamManager__input_toolsÚ+_AsyncResponseStreamManager__starting_afterr   rC   rC   rD   rE   É   r€   z#AsyncResponseStreamManager.__init__ú AsyncResponseStream[TextFormatT]c                 Ã  s,   �| j I d H }t|| j| j| jd�| _| jS r‚   )r�   r…   r‘   r’   r“   r�   rƒ   rC   rC   rD   rŠ   ×   s   €üz%AsyncResponseStreamManager.__aenter__rX   rY   rZ   r[   r\   r]   c                 Ã  s$   �| j d ur| j  ¡ I d H  d S d S rH   )r�   r_   r`   rC   rC   rD   r‹   ã   s   €
ÿz$AsyncResponseStreamManager.__aexit__N)
rw   rŽ   r.   r/   r0   r1   r2   r3   r4   r5   )r4   r”   ro   )rr   rs   rt   rE   rŠ   r‹   rC   rC   rC   rD   r�   È   r„   r�   c                   @  s>   e Zd Zddd„Zddd„Zddd„Zddd„Zddd„ZdS )r>   r0   r1   r.   r/   r4   r5   c                C  sX   d | _ d | _i | _i | _t|ƒrdd„ |D ƒng | _|| _t |¡r'|| _
d S t	| _
d S )Nc                 S  s   g | ]}|‘qS rC   rC   )Ú.0ÚtoolrC   rC   rD   Ú
<listcomp>ø   s    z0ResponseStreamState.__init__.<locals>.<listcomp>)Ú&_ResponseStreamState__current_snapshotrh   Ú_completed_outputÚ_output_itemsr   Ú_input_toolsÚ_text_formatÚinspectÚisclassr   Ú_rich_text_format)rB   r0   r.   rC   rC   rD   rE   î   s   zResponseStreamState.__init__rW   ÚRawResponseStreamEventú&List[ResponseStreamEvent[TextFormatT]]c                 C  s˜  |   |¡| _g }|jdkr@|  |j¡}|jdksJ ‚|j|j }|jdks'J ‚| tt	|j|j
|j|j|j|jd|jd�	¡ |S |jdkr‚|  |j¡}|jdksRJ ‚|j|j }|jdks_J ‚| ttdtƒ|j|j|j|j|jd|jt|j| j|jd�d�	¡ |S |jd	kr©|  |j¡}|jd
ks”J ‚| tt|j
|j|j|jd	|jd�¡ |S |jdkrÅ| j}|d us·J ‚| tt|jd|d�¡ |S | |¡ |S )Núresponse.output_text.deltaÚmessageÚoutput_text)Úcontent_indexÚdeltaÚitem_idÚoutput_indexrS   ÚlogprobsÚtypeÚsnapshotzresponse.output_text.donez(type[ResponseTextDoneEvent[TextFormatT]])r.   Úphase)r¥   r§   r¨   rS   r©   rª   ÚtextÚparsedú&response.function_call_arguments.deltaÚfunction_call)r¦   r§   r¨   rS   rª   r«   úresponse.completed)rS   rª   r:   )Úaccumulate_eventr˜   rª   Ú_get_output_itemr¨   Úcontentr¥   Úappendr   r   r¦   r§   rS   r©   r­   r	   r   r#   rœ   r¬   r   Ú	argumentsrh   r   )rB   rW   ÚeventsÚoutputr´   r:   rC   rC   rD   rR   ü   sŠ   
÷ÿ@
Í÷ÿ,
áùÿ
ñüÿ
þz ResponseStreamState.handle_eventr   c                   sò  ˆ j }|d u rˆ  |¡S |jdkr[t|dd ƒd u r|S |jjdkr2|j ttt	t
ƒ|j ¡ d�¡ n|jjdkrI|j ttt	tƒ|j ¡ d�¡ n|j |j¡ |jd ˆ j|j< |S |jdkr}ˆ  |j¡}|jdkr{|j ttt	tƒ|j ¡ d�¡ |S |jdkr¤ˆ  |j¡}|jdkr¢|j|j }|jd	ksšJ ‚| j|j7  _|S |jd
kr¾ˆ  |j¡}|jdkr¼| j|j7  _|S |jdkrÌ|jˆ j|j< |S |jdkr÷|j}t|dd ƒd u rít|ƒ}‡ fdd„tˆ jƒD ƒ|_tˆ j|ˆ jd�ˆ _|S )Nzresponse.output_item.addedrN   r°   ©Útype_Úvaluer£   éÿÿÿÿzresponse.content_part.addedr¢   r¤   r¯   zresponse.output_item.doner±   r¸   c                   s   g | ]}ˆ j | ‘qS rC   )r™   )r•   ÚindexrJ   rC   rD   r—   w  s    z8ResponseStreamState.accumulate_event.<locals>.<listcomp>)r.   r:   r0   )r˜   Ú_create_initial_responserª   ÚgetattrrN   r¸   rµ   r   r	   r   r)   Úto_dictr(   rš   r¨   r³   r´   r&   Úpartr¥   r­   r¦   r¶   r™   r:   r   Úsortedr$   rœ   r›   rh   )rB   rW   r«   r¸   r´   r:   rC   rJ   rD   r²   J  sh   

ÿÿÿ
â
ÿ
è

î

ò
ôýz$ResponseStreamState.accumulate_eventr¨   Úintú ParsedResponseOutputItem[object]c                 C  s.   z| j | W S  ty   td|› d�ƒd ‚w )Nz*Received a content event for output index z! before receiving its output item)rš   ÚKeyErrorri   )rB   r¨   rC   rC   rD   r³   €  s   
ÿþÿz$ResponseStreamState._get_output_itemc                 C  sF   |j dkrtd|j › d�ƒ‚tt|j ¡ d�}tt|jpg ƒƒ| _	|S )Nzresponse.createdz5Expected to have received `response.created` before `ú`r¹   )
rª   ri   r   r   r:   rÀ   ÚdictÚ	enumerater¸   rš   )rB   rW   r«   rC   rC   rD   r¾   ˆ  s
   
z,ResponseStreamState._create_initial_responseN)r0   r1   r.   r/   r4   r5   )rW   r    r4   r¡   )rW   r    r4   r   )r¨   rÃ   r4   rÄ   )rr   rs   rt   rE   rR   r²   r³   r¾   rC   rC   rC   rD   r>   í   s    


N
6r>   )<Ú
__future__r   r�   Útypesr   Útypingr   r   r   r   r   r	   Útyping_extensionsr
   r   r   r   Ú_typesr   Ú_eventsr   r   r   r   r   r   r   Ú_utilsr   r   r   Ú_compatr   Ú_modelsr   r   Ú
_streamingr   r   Útypes.responsesr    r    Ú_parsing._responsesr"   r#   r$   Útypes.responses.tool_paramr%   Útypes.responses.parsed_responser&   r'   r(   r)   Ú$types.responses.response_output_itemr*   r+   rv   r…   r�   r>   rC   rC   rC   rD   Ú<module>   s,     A%A%