o
    +i6                     @  sR  d dl mZ d dlZd dlZd dl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 d dlmZmZmZmZmZmZ d dlZddlmZmZ e	rTddlmZmZ ed	ZG d
d dejZ G dd dee e dZ!G dd dejZ"G dd dee e"dZ#G dd dZ$G dd dZ%eG dd deZ&d%ddZ'ddd&d#d$Z(dS )'    )annotationsN)TracebackType)TYPE_CHECKINGAnyGenericTypeVarIteratorAsyncIteratorcast)SelfProtocol	TypeGuardoverride
get_originruntime_checkable   )is_dictextract_type_var_from_base)	AnthropicAsyncAnthropic_Tc                   @     e Zd ZedddZdS )	_SyncStreamMetainstancer   returnboolc                 C  .   ddl m} t||rtjdtdd dS dS )Nr   )MessageStreamzUsing `isinstance()` to check if a `MessageStream` object is an instance of `Stream` is deprecated & will be removed in the next major version   
stacklevelTF)lib.streamingr   
isinstancewarningswarnDeprecationWarning)selfr   r    r'   Q/var/www/html/psymed-ai/venv/lib/python3.10/site-packages/anthropic/_streaming.py__instancecheck__      
z!_SyncStreamMeta.__instancecheck__Nr   r   r   r   __name__
__module____qualname__r   r)   r'   r'   r'   r(   r          r   c                   @  r   e Zd ZU dZded< ded< 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 ).StreamzJProvides the core interface to iterate over a synchronous stream response.httpx.ResponseresponseSSEBytesDecoder_decodercast_totype[_T]clientr   r   Nonec                C  *   || _ || _|| _| | _|  | _d S Nr4   _cast_to_client_make_sse_decoderr6   
__stream__	_iteratorr&   r7   r4   r9   r'   r'   r(   __init__3   
   
zStream.__init__r   c                 C  s
   | j  S r<   )rB   __next__r&   r'   r'   r(   rF   @   s   
zStream.__next__Iterator[_T]c                 c  s    | j D ]}|V  qd S r<   rB   r&   itemr'   r'   r(   __iter__C   s   
zStream.__iter__Iterator[ServerSentEvent]c                 c  s    | j | j E d H  d S r<   )r6   
iter_bytesr4   rG   r'   r'   r(   _iter_eventsG   s   zStream._iter_eventsc           	   	   c  s.   t t| j}| j}| jj}|  }|D ]|}|jdkr%|| ||dV  |jdksC|jdksC|jdksC|jdksC|jdksC|jdkr\| }t	|rTd	|vrT|j|d	< ||||dV  |jd
krbq|jdkr|j
}z	| }| }W n ty   |j
pd|j }Y nw | jj||| jdq|  d S N
completion)datar7   r4   message_startmessage_deltamessage_stopcontent_block_startcontent_block_deltacontent_block_stoptypepingerrorzError code: )bodyr4   )r
   r   r>   r4   r?   _process_response_datarO   eventjsonr   rR   	Exceptionstatus_code_make_status_errorclose	r&   r7   r4   process_dataiteratorsserR   r\   err_msgr'   r'   r(   rA   J   sF   










zStream.__stream__r   c                 C  s   | S r<   r'   rG   r'   r'   r(   	__enter__w   s   zStream.__enter__exc_typetype[BaseException] | NoneexcBaseException | Noneexc_tbTracebackType | Nonec                 C  s   |    d S r<   rc   r&   rj   rl   rn   r'   r'   r(   __exit__z   s   zStream.__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)r4   rc   rG   r'   r'   r(   rc      s   zStream.closeN)r7   r8   r4   r3   r9   r   r   r:   r   r   )r   rH   )r   rM   r   r   rj   rk   rl   rm   rn   ro   r   r:   r   r:   )r-   r.   r/   __doc____annotations__rD   rF   rL   rO   rA   ri   rr   rc   r'   r'   r'   r(   r2   ,   s   
 





-
r2   )	metaclassc                   @  r   )	_AsyncStreamMetar   r   r   r   c                 C  r   )Nr   )AsyncMessageStreamzUsing `isinstance()` to check if a `AsyncMessageStream` object is an instance of `AsyncStream` is deprecated & will be removed in the next major versionr   r   TF)r!   r|   r"   r#   r$   r%   )r&   r   r|   r'   r'   r(   r)      r*   z"_AsyncStreamMeta.__instancecheck__Nr+   r,   r'   r'   r'   r(   r{      r0   r{   c                   @  r1   ).AsyncStreamzLProvides the core interface to iterate over an asynchronous stream response.r3   r4   zSSEDecoder | SSEBytesDecoderr6   r7   r8   r9   r   r   r:   c                C  r;   r<   r=   rC   r'   r'   r(   rD      rE   zAsyncStream.__init__r   c                   s   | j  I d H S r<   )rB   	__anext__rG   r'   r'   r(   r~      s   zAsyncStream.__anext__AsyncIterator[_T]c                 C s"   | j 2 z	3 d H W }|V  q6 d S r<   rI   rJ   r'   r'   r(   	__aiter__   s   zAsyncStream.__aiter__AsyncIterator[ServerSentEvent]c                 C s.   | j | j 2 z	3 d H W }|V  q
6 d S r<   )r6   aiter_bytesr4   )r&   rg   r'   r'   r(   rO      s   zAsyncStream._iter_eventsc           	   	   C s>  t t| j}| j}| jj}|  }|2 z3 d H W }|jdkr)|| ||dV  |jdksG|jdksG|jdksG|jdksG|jdksG|jdkr`| }t	|rXd	|vrX|j|d	< ||||dV  |jd
krfq|jdkr|j
}z	| }| }W n ty   |j
pd|j }Y nw | jj||| jdq6 | I d H  d S rP   )r
   r   r>   r4   r?   r]   rO   r^   r_   r   rR   r`   ra   rb   acloserd   r'   r'   r(   rA      sH   










%zAsyncStream.__stream__r   c                   s   | S r<   r'   rG   r'   r'   r(   
__aenter__   s   zAsyncStream.__aenter__rj   rk   rl   rm   rn   ro   c                   s   |   I d H  d S r<   rp   rq   r'   r'   r(   	__aexit__   s   zAsyncStream.__aexit__c                   s   | j  I dH  dS rs   )r4   r   rG   r'   r'   r(   rc      s   zAsyncStream.closeN)r7   r8   r4   r3   r9   r   r   r:   rt   )r   r   )r   r   ru   rv   rw   )r-   r.   r/   rx   ry   rD   r~   r   rO   rA   r   r   rc   r'   r'   r'   r(   r}      s   
 





-
r}   c                   @  sr   e Zd ZddddddddZedddZedddZedddZedddZdddZ	e
dddZdS ) ServerSentEventNr^   rR   idretryr^   
str | NonerR   r   r   
int | Noner   r:   c                C  s,   |d u rd}|| _ || _|pd | _|| _d S )N )_id_data_event_retry)r&   r^   rR   r   r   r'   r'   r(   rD     s   

zServerSentEvent.__init__c                 C     | j S r<   )r   rG   r'   r'   r(   r^        zServerSentEvent.eventc                 C  r   r<   )r   rG   r'   r'   r(   r     r   zServerSentEvent.idc                 C  r   r<   )r   rG   r'   r'   r(   r     r   zServerSentEvent.retrystrc                 C  r   r<   )r   rG   r'   r'   r(   rR     r   zServerSentEvent.datar   c                 C  s   t | jS r<   )r_   loadsrR   rG   r'   r'   r(   r_   !  s   zServerSentEvent.jsonc              	   C  s&   d| j  d| j d| j d| j d	S )NzServerSentEvent(event=z, data=z, id=z, retry=)r   rG   r'   r'   r(   __repr__$  s   &zServerSentEvent.__repr__)
r^   r   rR   r   r   r   r   r   r   r:   )r   r   )r   r   )r   r   )r   r   )r-   r.   r/   rD   propertyr^   r   r   rR   r_   r   r   r'   r'   r'   r(   r      s"    
r   c                   @  sj   e Zd ZU ded< ded< ded< ded< 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 )%
SSEDecoderz	list[str]r   r   r   r   r   _last_event_idr   r:   c                 C  s   d | _ g | _d | _d | _d S r<   )r   r   r   r   rG   r'   r'   r(   rD   /  s   
zSSEDecoder.__init__rf   Iterator[bytes]rM   c                 c  sB    |  |D ]}| D ]}|d}| |}|r|V  qqdS )^Given an iterator that yields raw binary data, iterate over it & yield every event encounteredutf-8N)_iter_chunks
splitlinesdecoder&   rf   chunkraw_linelinerg   r'   r'   r(   rN   5  s   

zSSEDecoder.iter_bytesc                 c  sP    d}|D ]}|j ddD ]}||7 }|dr|V  d}qq|r&|V  dS dS )^Given an iterator that yields raw binary data, iterate over it and yield individual SSE chunks    Tkeependss   s   

s   

Nr   endswithr&   rf   rR   r   r   r'   r'   r(   r   ?  s   

zSSEDecoder._iter_chunksAsyncIterator[bytes]r   c                 C sL   |  |2 z3 dH W }| D ]}|d}| |}|r!|V  qq6 dS )r   Nr   )_aiter_chunksr   r   r   r'   r'   r(   r   K  s   

zSSEDecoder.aiter_bytesc                 C sZ   d}|2 z3 dH W }|j ddD ]}||7 }|dr!|V  d}qq6 |r+|V  dS dS )r   r   NTr   r   r   r   r'   r'   r(   r   U  s   

zSSEDecoder._aiter_chunksr   r   ServerSentEvent | Nonec              	   C  s  |s,| j s| js| js| jd u rd S t| j d| j| j| jd}d | _ g | _d | _|S |dr3d S |d\}}}|drF|dd  }|dkrO|| _ d S |dkr[| j| d S |dkrkd	|v rf	 d S || _d S |d
krzt	|| _W d S  t
tfy   Y d S w 	 d S )N
r   : r   r^   rR   r    r   )r   r   r   r   r   join
startswith	partitionappendint	TypeError
ValueError)r&   r   rg   	fieldname_valuer'   r'   r(   r   a  sP   


	zSSEDecoder.decodeNrw   rf   r   r   rM   )rf   r   r   r   rf   r   r   r   )rf   r   r   r   )r   r   r   r   )
r-   r.   r/   ry   rD   rN   r   r   r   r   r'   r'   r'   r(   r   )  s   
 






r   c                   @  s    e Zd ZdddZdd	d
ZdS )r5   rf   r   r   rM   c                 C     dS )r   Nr'   r&   rf   r'   r'   r(   rN        zSSEBytesDecoder.iter_bytesr   r   c                 C  r   )zdGiven an async iterator that yields raw binary data, iterate over it & yield every event encounteredNr'   r   r'   r'   r(   r     r   zSSEBytesDecoder.aiter_bytesNr   r   )r-   r.   r/   rN   r   r'   r'   r'   r(   r5     s    
r5   typrY   r   ;TypeGuard[type[Stream[object]] | type[AsyncStream[object]]]c                 C  s$   t | p| }t|ot|ttfS )zaTypeGuard for determining whether or not the given type is a subclass of `Stream` / `AsyncStream`)r   inspectisclass
issubclassr2   r}   )r   originr'   r'   r(   is_stream_class_type  s   r   )failure_message
stream_clsr   r   c                C  s*   ddl m}m} t| dtd||f|dS )a  Given a type like `Stream[T]`, returns the generic type variable `T`.

    This also handles the case where a concrete subclass is given, e.g.
    ```py
    class MyStream(Stream[bytes]):
        ...

    extract_stream_chunk_type(MyStream) -> bytes
    ```
    r   )r2   r}   r   ztuple[type, ...])indexgeneric_basesr   )_base_clientr2   r}   r   r
   )r   r   r2   r}   r'   r'   r(   extract_stream_chunk_type  s   r   )r   rY   r   r   )r   rY   r   r   r   rY   ))
__future__r   abcr_   r   r#   typesr   typingr   r   r   r   r   r	   r
   typing_extensionsr   r   r   r   r   r   httpx_utilsr   r   r?   r   r   r   ABCMetar   r2   r{   r}   r   r   r5   r   r   r'   r'   r'   r(   <module>   s0   $ _`)i

	