o
    Mf                     @  sf  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mZmZ d dlmZmZmZmZ d dlZddlmZmZmZmZ ddlmZ dd	lmZ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( ddl)m*Z*m+Z+m,Z,m-Z- G dd dZ.e	de.dZ/G dd dee/ Z0G dd dZ1e	de1dZ2G dd dee2 Z3d*ddZ4d+d#d$Z5d,d(d)Z6dS )-    )annotationsN)TracebackType)TYPE_CHECKINGAnyGenericTypeVarCallableIterableIteratorcast)	AwaitableAsyncIterableAsyncIteratorassert_never   )is_dictis_listconsume_sync_iteratorconsume_async_iterator)construct_type)StreamAsyncStream)AssistantStreamEvent)RunTextMessage	ImageFile	TextDeltaMessageDeltaMessageContentMessageContentDelta)RunStepToolCallRunStepDeltaToolCallDeltac                   @  sr  e Zd ZU ded< 	 dgddZdhd	d
ZdiddZdjddZedkddZ	edlddZ
edmddZednddZdgddZdgdd Zdod"d#Zdpd%d&Zdqd(d)Zdrd+d,Zdgd-d.Zdsd0d1Zdtd4d5Zdud9d:Zdtd;d<Zdvd?d@ZdwdBdCZdvdDdEZdxdHdIZdgdJdKZdydNdOZdzdQdRZdydSdTZd{dWdXZ d|dZd[Z!d{d\d]Z"d}d`daZ#dsdbdcZ$djdddeZ%dfS )~AssistantEventHandlerzIterable[str]text_deltasreturnNonec                 C  Z   d | _ d | _d | _d | _d | _d | _d | _i | _i | _d | _	| 
 | _|  | _d | _d S N)_current_event_current_message_content_index_current_message_content_current_tool_call_index_current_tool_call+_AssistantEventHandler__current_run_step_id#_AssistantEventHandler__current_run*_AssistantEventHandler__run_step_snapshots)_AssistantEventHandler__message_snapshots0_AssistantEventHandler__current_message_snapshot__text_deltas__r&   
__stream__	_iterator_AssistantEventHandler__streamself r;   b/var/www/html/analyze/labelStudio/lib/python3.10/site-packages/openai/lib/streaming/_assistants.py__init__)      


zAssistantEventHandler.__init__streamStream[AssistantStreamEvent]c                 C     | j rtd|| _ d S NzyA single event handler cannot be shared between multiple streams; You will need to construct a new event handler instance)r8   RuntimeErrorr:   r?   r;   r;   r<   _init9   
   
zAssistantEventHandler._initr   c                 C  s
   | j  S r*   )r7   __next__r9   r;   r;   r<   rG   A   s   
zAssistantEventHandler.__next__Iterator[AssistantStreamEvent]c                 c  s    | j D ]}|V  qd S r*   r7   r:   itemr;   r;   r<   __iter__D   s   
zAssistantEventHandler.__iter__AssistantStreamEvent | Nonec                 C     | j S r*   r+   r9   r;   r;   r<   current_eventH      z#AssistantEventHandler.current_event
Run | Nonec                 C  rN   r*   )r1   r9   r;   r;   r<   current_runL   rQ   z!AssistantEventHandler.current_runRunStep | Nonec                 C     | j sd S | j| j  S r*   )r0   r2   r9   r;   r;   r<   current_run_step_snapshotP      z/AssistantEventHandler.current_run_step_snapshotMessage | Nonec                 C  rN   r*   )r4   r9   r;   r;   r<   current_message_snapshotW   rQ   z.AssistantEventHandler.current_message_snapshotc                 C  s   | j r
| j   dS dS z~
        Close the response and release the connection.

        Automatically called when the context manager exits.
        N)r8   closer9   r;   r;   r<   r[   [   s   zAssistantEventHandler.closec                 C  s   t |  dS z(Waits until the stream has been consumedN)r   r9   r;   r;   r<   
until_doned   s   z AssistantEventHandler.until_doner   c                 C  s   |    | jstd| jS )BWait for the stream to finish and returns the completed Run objectNo final run object found)r]   r1   rC   r9   r;   r;   r<   get_final_runh   s   z#AssistantEventHandler.get_final_runlist[RunStep]c                 C  *   |    | jstddd | j D S )EWait for the stream to finish and returns the steps taken in this runNo run steps foundc                 S     g | ]}|qS r;   r;   .0stepr;   r;   r<   
<listcomp>x       z=AssistantEventHandler.get_final_run_steps.<locals>.<listcomp>)r]   r2   rC   valuesr9   r;   r;   r<   get_final_run_stepsq      z)AssistantEventHandler.get_final_run_stepslist[Message]c                 C  rb   )JWait for the stream to finish and returns the messages emitted in this runNo messages foundc                 S  re   r;   r;   rg   messager;   r;   r<   ri      rj   z<AssistantEventHandler.get_final_messages.<locals>.<listcomp>)r]   r3   rC   rk   r9   r;   r;   r<   get_final_messagesz   rm   z(AssistantEventHandler.get_final_messagesIterator[str]c                 c  sR    | D ]#}|j dkrq|jjjpg D ]}|jdkr%|jr%|jjr%|jjV  qqd S Nthread.message.deltatexteventdatadeltacontenttyperw   valuer:   ry   content_deltar;   r;   r<   r5      s   

z%AssistantEventHandler.__text_deltas__c                 C     dS zFires when the stream has finished.

        This happens if the stream is read to completion
        or if an exception occurs during iteration.
        Nr;   r9   r;   r;   r<   on_end       zAssistantEventHandler.on_endry   c                 C  r   z2Callback that is fired for every Server-Sent-EventNr;   r:   ry   r;   r;   r<   on_event   r   zAssistantEventHandler.on_eventrun_stepr!   c                 C  r   z1Callback that is fired when a run step is createdNr;   r:   r   r;   r;   r<   on_run_step_created   r   z)AssistantEventHandler.on_run_step_createdr{   r#   snapshotc                 C  r   aX  Callback that is fired whenever a run step delta is returned from the API

        The first argument is just the delta as sent by the API and the second argument
        is the accumulated snapshot of the run step. For example, a tool calls event may
        look like this:

        # delta
        tool_calls=[
            RunStepDeltaToolCallsCodeInterpreter(
                index=0,
                type='code_interpreter',
                id=None,
                code_interpreter=CodeInterpreter(input=' sympy', outputs=None)
            )
        ]
        # snapshot
        tool_calls=[
            CodeToolCall(
                id='call_wKayJlcYV12NiadiZuJXxcfx',
                code_interpreter=CodeInterpreter(input='from sympy', outputs=[]),
                type='code_interpreter',
                index=0
            )
        ],
        Nr;   r:   r{   r   r;   r;   r<   on_run_step_delta   r   z'AssistantEventHandler.on_run_step_deltac                 C  r   z3Callback that is fired when a run step is completedNr;   r   r;   r;   r<   on_run_step_done   r   z&AssistantEventHandler.on_run_step_done	tool_callr"   c                 C  r   z2Callback that is fired when a tool call is createdNr;   r:   r   r;   r;   r<   on_tool_call_created   r   z*AssistantEventHandler.on_tool_call_createdr$   c                 C  r   z<Callback that is fired when a tool call delta is encounteredNr;   r   r;   r;   r<   on_tool_call_delta   r   z(AssistantEventHandler.on_tool_call_deltac                 C  r   r   r;   r   r;   r;   r<   on_tool_call_done   r   z'AssistantEventHandler.on_tool_call_done	exception	Exceptionc                 C  r   z4Fired whenever an exception happens during streamingNr;   r:   r   r;   r;   r<   on_exception   r   z"AssistantEventHandler.on_exceptionc                 C  r   zFires if the request times outNr;   r9   r;   r;   r<   
on_timeout   r   z AssistantEventHandler.on_timeoutrr   r   c                 C  r   z0Callback that is fired when a message is createdNr;   r:   rr   r;   r;   r<   on_message_created   r   z(AssistantEventHandler.on_message_createdr   c                 C  r   ax  Callback that is fired whenever a message delta is returned from the API

        The first argument is just the delta as sent by the API and the second argument
        is the accumulated snapshot of the message. For example, a text content event may
        look like this:

        # delta
        MessageDeltaText(
            index=0,
            type='text',
            text=Text(
                value=' Jane'
            ),
        )
        # snapshot
        MessageContentText(
            index=0,
            type='text',
            text=Text(
                value='Certainly, Jane'
            ),
        )
        Nr;   r   r;   r;   r<   on_message_delta   r   z&AssistantEventHandler.on_message_deltac                 C  r   z2Callback that is fired when a message is completedNr;   r   r;   r;   r<   on_message_done   r   z%AssistantEventHandler.on_message_donerw   r   c                 C  r   z;Callback that is fired when a text content block is createdNr;   r:   rw   r;   r;   r<   on_text_created   r   z%AssistantEventHandler.on_text_createdr   c                 C  r   a  Callback that is fired whenever a text content delta is returned
        by the API.

        The first argument is just the delta as sent by the API and the second argument
        is the accumulated snapshot of the text. For example:

        on_text_delta(TextDelta(value="The"), Text(value="The")),
        on_text_delta(TextDelta(value=" solution"), Text(value="The solution")),
        on_text_delta(TextDelta(value=" to"), Text(value="The solution to")),
        on_text_delta(TextDelta(value=" the"), Text(value="The solution to the")),
        on_text_delta(TextDelta(value=" equation"), Text(value="The solution to the equivalent")),
        Nr;   r   r;   r;   r<   on_text_delta   r   z#AssistantEventHandler.on_text_deltac                 C  r   z<Callback that is fired when a text content block is finishedNr;   r   r;   r;   r<   on_text_done   r   z"AssistantEventHandler.on_text_done
image_filer   c                 C  r   z;Callback that is fired when an image file block is finishedNr;   r:   r   r;   r;   r<   on_image_file_done   r   z(AssistantEventHandler.on_image_file_donec                 C  sv  || _ | | t|| jd\| _}| jd ur| j| j| jj< t|| jd |D ]}| jd us1J | jj|j	 }|j
dkrC| |j q(|jdks]|jdks]|jdks]|jdks]|jdkrl|j| _| jrj| | j n|jd	ks|jd
ks|jdks|jdkr|j| _n|jdkr| |j n|jdkr| j}|d usJ |jj}|jd ur|jD ]W}|j
dkr|jr|j|j	 }|j
dksJ | |j|j |j	| jkr| jd ur| jj
dkr| | jj n| jj
dkr| | jj |j	| _|j|j	 | _|j|j	 | _q| |jj| n%|jdks|jdkrV|j| _|j| j|jj< | jd urO|jj| j }|j
dkrC| |j n|j
dkrO| |j | |j n|jdkrh|jj| _| |j n|jdkrt|jj| _n|jdkr| j|jj }	|jj}
|
jr|
jj
dkr|
jjd ur|	jj
dksJ |
jjD ]B}|j	| j kr| !||	jj|j	  |j	| j kr| jd ur| | j |j	| _ |	jj|j	 | _| "| j |	jj|j	 | _q| #|jj|	 nF|jdks|jdks|jdks|jdkr| jr| | j | $|j d | _n|jdks.|jdks.|jdkr/nt%r6t&| d | _ d S N)ry   rY   )ry   run_step_snapshotsrw   zthread.run.completedzthread.run.cancelledzthread.run.expiredzthread.run.failedzthread.run.requires_actionzthread.run.createdzthread.run.in_progresszthread.run.cancellingzthread.run.queuedthread.message.createdrv   r   zthread.message.completedzthread.message.incompletethread.run.step.createdzthread.run.step.in_progressthread.run.step.delta
tool_callszthread.run.step.completedzthread.run.step.cancelledzthread.run.step.expiredzthread.run.step.failedzthread.createdzthread.message.in_progresserror)'r+   r   accumulate_eventr4   r3   idaccumulate_run_stepr2   r|   indexr}   r   rw   ry   rz   r1   r/   r   r   r{   r   r,   r-   r   r   r   r   r   r0   r   step_detailsr   r.   r   r   r   r   r   r   r:   ry   new_contentr   blockr   message_deltasnapshot_contentr|   step_snapshotrun_step_deltatool_call_deltar;   r;   r<   _emit_sse_event   s   















$
z%AssistantEventHandler._emit_sse_eventc              
   c  s    | j }|s
tdzBz|D ]
}| | |V  qW n+ tjtjfy4 } z
|   | |  d }~w t	yE } z| |  d }~ww W | 
  d S | 
  w NzStream has not been started yet)r8   rC   r   httpxTimeoutExceptionasyncioTimeoutErrorr   r   r   r   r:   r?   ry   excr;   r;   r<   r6     s*   


z AssistantEventHandler.__stream__Nr'   r(   )r?   r@   r'   r(   r'   r   )r'   rH   r'   rM   r'   rR   r'   rT   r'   rX   r'   r   r'   ra   r'   rn   )r'   rt   ry   r   r'   r(   r   r!   r'   r(   r{   r#   r   r!   r'   r(   r   r"   r'   r(   r{   r$   r   r"   r'   r(   r   r   r'   r(   rr   r   r'   r(   r{   r   r   r   r'   r(   rw   r   r'   r(   r{   r   r   r   r'   r(   r   r   r'   r(   )&__name__
__module____qualname____annotations__r=   rE   rG   rL   propertyrP   rS   rV   rY   r[   r]   r`   rl   rs   r5   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r6   r;   r;   r;   r<   r%      sR   
 





	

	
	
	

















 r%   AssistantEventHandlerT)boundc                   @  .   e Zd ZdZddd	Zdd
dZdddZdS )AssistantStreamManagerzWrapper over AssistantStreamEventHandler that is returned by `.stream()`
    so that a context manager can be used.

    ```py
    with client.threads.create_and_run_stream(...) as stream:
        for event in stream:
            ...
    ```
    api_request*Callable[[], Stream[AssistantStreamEvent]]event_handlerr   r'   r(   c                C     d | _ || _|| _d S r*   )_AssistantStreamManager__stream&_AssistantStreamManager__event_handler$_AssistantStreamManager__api_requestr:   r   r   r;   r;   r<   r=        
zAssistantStreamManager.__init__c                 C  s   |   | _| j| j | jS r*   )r   r   r   rE   r9   r;   r;   r<   	__enter__  s   
z AssistantStreamManager.__enter__exc_typetype[BaseException] | Noner   BaseException | Noneexc_tbTracebackType | Nonec                 C  s   | j d ur| j   d S d S r*   )r   r[   r:   r   r   r   r;   r;   r<   __exit__  s   
zAssistantStreamManager.__exit__N)r   r   r   r   r'   r(   )r'   r   r   r   r   r   r   r   r'   r(   )r   r   r   __doc__r=   r   r   r;   r;   r;   r<   r     s
    



r   c                   @  sr  e Zd ZU ded< 	 dgddZdhd	d
ZdiddZdjddZdgddZe	dkddZ
e	dlddZe	dmddZe	dnddZdgdd Zdod"d#Zdpd%d&Zdqd(d)Zdrd+d,Zdgd-d.Zdsd0d1Zdtd4d5Zdud9d:Zdtd;d<Zdvd?d@ZdwdBdCZdvdDdEZdxdHdIZdgdJdKZdydNdOZdzdQdRZdydSdTZd{dWdXZ d|dZd[Z!d{d\d]Z"d}d`daZ#dsdbdcZ$djdddeZ%dfS )~AsyncAssistantEventHandlerzAsyncIterable[str]r&   r'   r(   c                 C  r)   r*   )r+   r,   r-   r.   r/   0_AsyncAssistantEventHandler__current_run_step_id(_AsyncAssistantEventHandler__current_run/_AsyncAssistantEventHandler__run_step_snapshots._AsyncAssistantEventHandler__message_snapshots5_AsyncAssistantEventHandler__current_message_snapshotr5   r&   r6   r7   #_AsyncAssistantEventHandler__streamr9   r;   r;   r<   r=     r>   z#AsyncAssistantEventHandler.__init__r?   !AsyncStream[AssistantStreamEvent]c                 C  rA   rB   )r  rC   rD   r;   r;   r<   rE     rF   z AsyncAssistantEventHandler._initr   c                   s   | j  I d H S r*   )r7   	__anext__r9   r;   r;   r<   r    s   z$AsyncAssistantEventHandler.__anext__#AsyncIterator[AssistantStreamEvent]c                 C s"   | j 2 z	3 d H W }|V  q6 d S r*   rI   rJ   r;   r;   r<   	__aiter__  s   z$AsyncAssistantEventHandler.__aiter__c                   s    | j r| j  I dH  dS dS rZ   )r  r[   r9   r;   r;   r<   r[     s   z AsyncAssistantEventHandler.closerM   c                 C  rN   r*   rO   r9   r;   r;   r<   rP      rQ   z(AsyncAssistantEventHandler.current_eventrR   c                 C  rN   r*   )r   r9   r;   r;   r<   rS     rQ   z&AsyncAssistantEventHandler.current_runrT   c                 C  rU   r*   )r   r  r9   r;   r;   r<   rV     rW   z4AsyncAssistantEventHandler.current_run_step_snapshotrX   c                 C  rN   r*   )r  r9   r;   r;   r<   rY     rQ   z3AsyncAssistantEventHandler.current_message_snapshotc                   s   t | I dH  dS r\   )r   r9   r;   r;   r<   r]     s   z%AsyncAssistantEventHandler.until_doner   c                   s$   |   I dH  | jstd| jS )r^   Nr_   )r]   r   rC   r9   r;   r;   r<   r`     s
   z(AsyncAssistantEventHandler.get_final_runra   c                   2   |   I dH  | jstddd | j D S )rc   Nrd   c                 S  re   r;   r;   rf   r;   r;   r<   ri   '  rj   zBAsyncAssistantEventHandler.get_final_run_steps.<locals>.<listcomp>)r]   r  rC   rk   r9   r;   r;   r<   rl      
   z.AsyncAssistantEventHandler.get_final_run_stepsrn   c                   r	  )ro   Nrp   c                 S  re   r;   r;   rq   r;   r;   r<   ri   0  rj   zAAsyncAssistantEventHandler.get_final_messages.<locals>.<listcomp>)r]   r  rC   rk   r9   r;   r;   r<   rs   )  r
  z-AsyncAssistantEventHandler.get_final_messagesAsyncIterator[str]c                 C s\   | 2 z'3 d H W }|j dkrq|jjjpg D ]}|jdkr)|jr)|jjr)|jjV  qq6 d S ru   rx   r   r;   r;   r<   r5   2  s   

z*AsyncAssistantEventHandler.__text_deltas__c                      dS r   r;   r9   r;   r;   r<   r   =      z!AsyncAssistantEventHandler.on_endry   c                   r  r   r;   r   r;   r;   r<   r   D  r  z#AsyncAssistantEventHandler.on_eventr   r!   c                   r  r   r;   r   r;   r;   r<   r   G  r  z.AsyncAssistantEventHandler.on_run_step_createdr{   r#   r   c                   r  r   r;   r   r;   r;   r<   r   J  r  z,AsyncAssistantEventHandler.on_run_step_deltac                   r  r   r;   r   r;   r;   r<   r   e  r  z+AsyncAssistantEventHandler.on_run_step_doner   r"   c                   r  r   r;   r   r;   r;   r<   r   h  r  z/AsyncAssistantEventHandler.on_tool_call_createdr$   c                   r  r   r;   r   r;   r;   r<   r   k  r  z-AsyncAssistantEventHandler.on_tool_call_deltac                   r  r   r;   r   r;   r;   r<   r   n  r  z,AsyncAssistantEventHandler.on_tool_call_doner   r   c                   r  r   r;   r   r;   r;   r<   r   q  r  z'AsyncAssistantEventHandler.on_exceptionc                   r  r   r;   r9   r;   r;   r<   r   t  r  z%AsyncAssistantEventHandler.on_timeoutrr   r   c                   r  r   r;   r   r;   r;   r<   r   w  r  z-AsyncAssistantEventHandler.on_message_createdr   c                   r  r   r;   r   r;   r;   r<   r   z  r  z+AsyncAssistantEventHandler.on_message_deltac                   r  r   r;   r   r;   r;   r<   r     r  z*AsyncAssistantEventHandler.on_message_donerw   r   c                   r  r   r;   r   r;   r;   r<   r     r  z*AsyncAssistantEventHandler.on_text_createdr   c                   r  r   r;   r   r;   r;   r<   r     r  z(AsyncAssistantEventHandler.on_text_deltac                   r  r   r;   r   r;   r;   r<   r     r  z'AsyncAssistantEventHandler.on_text_doner   r   c                   r  r   r;   r   r;   r;   r<   r     r  z-AsyncAssistantEventHandler.on_image_file_donec                   s  || _ | |I d H  t|| jd\| _}| jd ur#| j| j| jj< t|| jd |D ]}| jd us5J | jj|j	 }|j
dkrJ| |jI d H  q,|jdksd|jdksd|jdksd|jdksd|jdkrv|j| _| jrt| | jI d H  n|jd	ks|jd
ks|jdks|jdkr|j| _n|jdkr| |jI d H  n|jdkr-| j}|d usJ |jj}|jd ur |jD ]c}|j
dkr|jr|j|j	 }|j
dksJ | |j|jI d H  |j	| jkr| jd ur| jj
dkr| | jjI d H  n| jj
dkr| | jjI d H  |j	| _|j|j	 | _|j|j	 | _q| |jj|I d H  nC|jdks9|jdkr{|j| _|j| j|jj< | jd urq|jj| j }|j
dkrb| |jI d H  n|j
dkrq| |jI d H  | |jI d H  n|jdkr|jj| _| |jI d H  n|jdkr|jj| _n|jdkr$| j|jj }	|jj}
|
jr|
jj
dkr|
jjd ur|	jj
dksJ |
jjD ]K}|j	| j kr| !||	jj|j	 I d H  |j	| j kr| jd ur| | jI d H  |j	| _ |	jj|j	 | _| "| jI d H  |	jj|j	 | _q| #|jj|	I d H  nL|jdks<|jdks<|jdks<|jdkrV| jrI| | jI d H  | $|jI d H  d | _n|jdksh|jdksh|jdkrint%rpt&| d | _ d S r   )'r+   r   r   r  r  r   r   r  r|   r   r}   r   rw   ry   rz   r   r/   r   r   r{   r   r,   r-   r   r   r   r   r   r   r   r   r   r.   r   r   r   r   r   r   r   r;   r;   r<   r     s   














$
z*AsyncAssistantEventHandler._emit_sse_eventc              
   C s   | j }|s
tdzVz|2 z3 d H W }| |I d H  |V  q6 W n4 tjtjfyB } z|  I d H  | |I d H   d }~w t	yV } z	| |I d H   d }~ww W | 
 I d H  d S | 
 I d H  w r   )r  rC   r   r   r   r   r   r   r   r   r   r   r;   r;   r<   r6   =  s*   "z%AsyncAssistantEventHandler.__stream__Nr   )r?   r  r'   r(   r   )r'   r  r   r   r   r   r   r   r   )r'   r  r   r   r   r   r   r   r   r   r   r   r   )&r   r   r   r   r=   rE   r  r  r[   r   rP   rS   rV   rY   r]   r`   rl   rs   r5   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r6   r;   r;   r;   r<   r     sR   
 




	


	
	
	

















 r   AsyncAssistantEventHandlerTc                   @  r   )AsyncAssistantStreamManagera>  Wrapper over AsyncAssistantStreamEventHandler that is returned by `.stream()`
    so that an async context manager can be used without `await`ing the
    original client call.

    ```py
    async with client.threads.create_and_run_stream(...) as stream:
        async for event in stream:
            ...
    ```
    r   ,Awaitable[AsyncStream[AssistantStreamEvent]]r   r  r'   r(   c                C  r   r*   )$_AsyncAssistantStreamManager__stream+_AsyncAssistantStreamManager__event_handler)_AsyncAssistantStreamManager__api_requestr   r;   r;   r<   r=   a  r   z$AsyncAssistantStreamManager.__init__c                   s$   | j I d H | _| j| j | jS r*   )r  r  r  rE   r9   r;   r;   r<   
__aenter__k  s   z&AsyncAssistantStreamManager.__aenter__r   r   r   r   r   r   c                   s$   | j d ur| j  I d H  d S d S r*   )r  r[   r   r;   r;   r<   	__aexit__p  s   
z%AsyncAssistantStreamManager.__aexit__N)r   r  r   r  r'   r(   )r'   r  r   )r   r   r   r   r=   r  r  r;   r;   r;   r<   r  U  s
    


r  ry   r   r   dict[str, RunStep]r'   r(   c                 C  s   | j dkr| j|| jj< d S | j dkr>| j}||j }|jr>ttd|jddtd|jjdd}tttt|d||j< d S )Nr   r   dict[object, object]Texclude_unsettype_r~   )	ry   rz   r   r{   accumulate_deltar   
model_dumpr!   r   )ry   r   rz   r   mergedr;   r;   r<   r   z  s&   




r   rY   rX   0tuple[Message | None, list[MessageContentDelta]]c                 C  s   | j dkr
| jg fS g }| j dkr|g fS |std| j}|jjry|jjD ]R}z|j|j }W n$ tyT   |j|jtt	t
ttt	|jddd || Y q&w ttd|jddtd|jdd}tt	t
ttt	|d|j|j< q&||fS )zIReturns a tuple of message snapshot and newly created text message deltasr   rv   z5Encountered a message delta with no previous snapshotTr  r  r  )ry   rz   rC   r{   r|   r   
IndexErrorinsertr   r   r   r   r  appendr  )ry   rY   r   rz   r   r   r  r;   r;   r<   r     sV   






	r   accr  r{   c           	      C  s  |  D ]\}}|| vr|| |< q| | }|d u r|| |< q|dks&|dkr+|| |< qt|tr:t|tr:||7 }nt|ttfrMt|ttfrM||7 }nt|r[t|r[t||}nst|rt|rtdd |D rr|	| q|D ]Y}t|st
d| z|d }W n ty } ztd| |d }~ww t|tst
d| z|| }W n ty   ||| Y qtw t|st
dt||||< qt|| |< q| S )	Nr   r}   c                 s  s     | ]}t |tttfV  qd S r*   )
isinstancestrintfloat)rg   xr;   r;   r<   	<genexpr>  s    z#accumulate_delta.<locals>.<genexpr>z1Unexpected list delta entry is not a dictionary: z2Expected list delta entry to have an `index` key; z>Unexpected, list delta entry `index` value is not an integer; znot handled yet)itemsr$  r%  r&  r'  r   r  r   allextend	TypeErrorKeyErrorrC   r   r!  )	r#  r{   keydelta_value	acc_valuedelta_entryr   r   	acc_entryr;   r;   r<   r    sT   




r  )ry   r   r   r  r'   r(   )ry   r   rY   rX   r'   r  )r#  r  r{   r  r'   r  )7
__future__r   r   typesr   typingr   r   r   r   r   r	   r
   r   typing_extensionsr   r   r   r   r   _utilsr   r   r   r   _modelsr   
_streamingr   r   
types.betar   types.beta.threadsr   r   r   r   r   r   r   r    types.beta.threads.runsr!   r"   r#   r$   r%   r   r   r   r  r  r   r   r  r;   r;   r;   r<   <module>   s6    ((
   $   
%
: