
    =ht'                    H   d dl m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 erddlmZmZ  e
d	      Z G d
 de	e         Z G d de	e         Z G d d      Z G d d      Ze G d de             Z ddZ!dd	 	 	 	 	 ddZ"y)    )annotationsN)TracebackType)TYPE_CHECKINGAnyGenericTypeVarIteratorAsyncIteratorcast)SelfProtocol	TypeGuardoverride
get_originruntime_checkable   )extract_type_var_from_base)RetellAsyncRetell_Tc                      e Zd ZU dZded<   ded<   	 	 	 	 	 	 	 	 ddZddZddZdd	Zdd
Z	ddZ
	 	 	 	 	 	 	 	 ddZddZy)StreamzJProvides the core interface to iterate over a synchronous stream response.httpx.ResponseresponseSSEBytesDecoder_decoderc                   || _         || _        || _        |j                         | _        | j                         | _        y Nr   _cast_to_client_make_sse_decoderr   
__stream__	_iteratorselfcast_tor   clients       a/var/www/html/phonemate/phone_mate_backend/venv/lib/python3.12/site-packages/retell/_streaming.py__init__zStream.__init__   7     !002*    c                6    | j                   j                         S r   )r$   __next__r&   s    r)   r.   zStream.__next__)   s    ~~&&((r,   c              #  6   K   | j                   D ]  }|  y wr   r$   r&   items     r)   __iter__zStream.__iter__,   s     NNDJ #s   c              #     K   | j                   j                  | j                  j                               E d {    y 7 wr   )r   
iter_bytesr   r/   s    r)   _iter_eventszStream._iter_events0   s+     ==++DMM,D,D,FGGGs   7A?Ac              #     K   t        t        | j                        }| j                  }| j                  j
                  }| j                         }|D ]  } ||j                         ||        |D ]  } y wN)datar'   r   r   r   r    r   r!   _process_response_datar7   jsonr&   r'   r   process_dataiteratorsse_sses          r)   r#   zStream.__stream__3   sj     sDMM*==||::$$&CCHHJ(SS  D s   A7A9c                    | S r    r/   s    r)   	__enter__zStream.__enter__@   s    r,   c                $    | j                          y r   closer&   exc_typeexcexc_tbs       r)   __exit__zStream.__exit__C   s     	

r,   c                8    | j                   j                          yz
        Close the response and release the connection.

        Automatically called if the response body is read to completion.
        N)r   rH   r/   s    r)   rH   zStream.closeK   s     	r,   N)r'   type[_T]r   r   r(   r   returnNonerQ   r   )rQ   zIterator[_T])rQ   Iterator[ServerSentEvent]rQ   r   rJ   ztype[BaseException] | NonerK   zBaseException | NonerL   zTracebackType | NonerQ   rR   rQ   rR   )__name__
__module____qualname____doc____annotations__r*   r.   r4   r7   r#   rE   rM   rH   rD   r,   r)   r   r      s    T+ + !	+
 + 
+)H, " %	
 
r,   r   c                      e Zd ZU dZded<   ded<   	 	 	 	 	 	 	 	 ddZddZddZdd	Zdd
Z	ddZ
	 	 	 	 	 	 	 	 ddZddZy)AsyncStreamzLProvides the core interface to iterate over an asynchronous stream response.r   r   zSSEDecoder | SSEBytesDecoderr   c                   || _         || _        || _        |j                         | _        | j                         | _        y r   r   r%   s       r)   r*   zAsyncStream.__init__[   r+   r,   c                R   K   | j                   j                          d {   S 7 wr   )r$   	__anext__r/   s    r)   ra   zAsyncStream.__anext__h   s     ^^--////s   '%'c               H   K   | j                   2 3 d {   }| 7 
6 y wr   r1   r2   s     r)   	__aiter__zAsyncStream.__aiter__k   s!     .. 	$J	.s   "  " "c                  K   | j                   j                  | j                  j                               2 3 d {   }| 7 
6 y wr   )r   aiter_bytesr   )r&   rA   s     r)   r7   zAsyncStream._iter_eventso   s:     224==3L3L3NO 	#I	Os%   4A	AAAA	AA	c                 K   t        t        | j                        }| j                  }| j                  j
                  }| j                         }|2 3 d {   } ||j                         ||       &7 !6 |2 3 d {  7  }6 y wr9   r;   r>   s          r)   r#   zAsyncStream.__stream__s   s~     sDMM*==||::$$&! 	T#CHHJ(SS	T # 	 	$ #sB   AB	A9A7A9B	7A99B	<B BBB	c                   K   | S wr   rD   r/   s    r)   
__aenter__zAsyncStream.__aenter__   s     s   c                @   K   | j                          d {    y 7 wr   rG   rI   s       r)   	__aexit__zAsyncStream.__aexit__   s      jjls   c                T   K   | j                   j                          d{    y7 wrO   )r   acloser/   s    r)   rH   zAsyncStream.close   s      mm""$$$s   (&(N)r'   rP   r   r   r(   r   rQ   rR   rS   )rQ   zAsyncIterator[_T])rQ   AsyncIterator[ServerSentEvent]rU   rV   rW   )rX   rY   rZ   r[   r\   r*   ra   rc   r7   r#   rh   rj   rH   rD   r,   r)   r^   r^   T   s    V**+ + !	+
 + 
+0, " %	
 
%r,   r^   c                      e Zd Zddddd	 	 	 	 	 	 	 	 	 d
dZedd       Zedd       Zedd       Zedd       ZddZ	e
dd	       Zy)ServerSentEventNeventr:   idretryc               L    |d}|| _         || _        |xs d | _        || _        y )N )_id_data_event_retry)r&   rq   r:   rr   rs   s        r)   r*   zServerSentEvent.__init__   s.     <D
mtr,   c                    | j                   S r   )rx   r/   s    r)   rq   zServerSentEvent.event       {{r,   c                    | j                   S r   )rv   r/   s    r)   rr   zServerSentEvent.id   s    xxr,   c                    | j                   S r   )ry   r/   s    r)   rs   zServerSentEvent.retry   r{   r,   c                    | j                   S r   )rw   r/   s    r)   r:   zServerSentEvent.data   s    zzr,   c                @    t        j                  | j                        S r   )r=   loadsr:   r/   s    r)   r=   zServerSentEvent.json   s    zz$))$$r,   c           	     p    d| j                    d| j                   d| j                   d| j                   d	S )NzServerSentEvent(event=z, data=z, id=z, retry=)rp   r/   s    r)   __repr__zServerSentEvent.__repr__   s9    '

|7499+U477)S[\`\f\f[gghiir,   )
rq   
str | Noner:   r   rr   r   rs   
int | NonerQ   rR   )rQ   r   )rQ   r   )rQ   str)rQ   r   )rX   rY   rZ   r*   propertyrq   rr   rs   r:   r=   r   r   rD   r,   r)   ro   ro      s     !   	
   
         % j jr,   ro   c                  f    e Zd ZU ded<   ded<   ded<   ded<   ddZdd	Zdd
ZddZddZddZ	y)
SSEDecoderz	list[str]rw   r   rx   r   ry   _last_event_idc                <    d | _         g | _        d | _        d | _        y r   )rx   rw   r   ry   r/   s    r)   r*   zSSEDecoder.__init__   s    
"r,   c              #     K   | j                  |      D ]@  }|j                         D ]+  }|j                  d      }| j                  |      }|s(| - B yw)^Given an iterator that yields raw binary data, iterate over it & yield every event encounteredutf-8N)_iter_chunks
splitlinesdecoder&   r@   chunkraw_linelinerA   s         r)   r6   zSSEDecoder.iter_bytes   sR     &&x0E!,,.w/kk$'I	 / 1s   AA	Ac              #     K   d}|D ]6  }|j                  d      D ]  }||z  }|j                  d      s| d}! 8 |r| yyw)^Given an iterator that yields raw binary data, iterate over it and yield individual SSE chunksr,   Tkeependss   s   

s   

Nr   endswithr&   r@   r:   r   r   s        r)   r   zSSEDecoder._iter_chunks   s\     E(($(7==!@AJD	 8  J s
   3AAc                  K   | j                  |      2 3 d{   }|j                         D ],  }|j                  d      }| j                  |      }|s(| . I7 D6 yw)r   Nr   )_aiter_chunksr   r   r   s         r)   re   zSSEDecoder.aiter_bytes   s[     --h7 	%!,,.w/kk$'I	 /	7s+   A!AAA8A!A!AA!c                  K   d}|2 3 d{   }|j                  d      D ]   }||z  }|j                  d      s| d}" ?7 :6 |r| yyw)r   r,   NTr   r   r   r   s        r)   r   zSSEDecoder._aiter_chunks   sd     # 	%(($(7==!@AJD	 8	8 J s*   AAAA,A
AA
Ac                z   |s| j                   s%| j                  s| j                  s| j                  y t	        | j                   dj                  | j                        | j                  | j                        }d | _         g | _        d | _        |S |j                  d      ry |j                  d      \  }}}|j                  d      r|dd  }|dk(  r|| _         y |dk(  r| j                  j                  |       y |dk(  rd	|v r	 y || _        y |d
k(  r	 t        |      | _        y 	 y # t        t        f$ r Y y w xY w)N
rp   : r   rq   r:   rr    rs   )rx   rw   r   ry   ro   join
startswith	partitionappendint	TypeError
ValueError)r&   r   rA   	fieldname_values         r)   r   zSSEDecoder.decode   sW    ;;tzz$:M:MRVR]R]Re!kkYYtzz*&&kk	C DKDJDKJ??3"nnS1	1eC !"IEDK   & JJe$  $u}  ',#  '!!%j   z*  s   D( (D:9D:NrW   r@   Iterator[bytes]rQ   rT   )r@   r   rQ   r   r@   AsyncIterator[bytes]rQ   rm   )r@   r   rQ   r   )r   r   rQ   zServerSentEvent | None)
rX   rY   rZ   r\   r*   r6   r   re   r   r   rD   r,   r)   r   r      s8    

.r,   r   c                      e Zd ZddZddZy)r   c                     y)r   NrD   r&   r@   s     r)   r6   zSSEBytesDecoder.iter_bytes(      r,   c                     y)zdGiven an async iterator that yields raw binary data, iterate over it & yield every event encounteredNrD   r   s     r)   re   zSSEBytesDecoder.aiter_bytes,  r   r,   Nr   r   )rX   rY   rZ   r6   re   rD   r,   r)   r   r   &  s    r,   r   c                z    t        |       xs | }t        j                  |      xr t        |t        t
        f      S )zaTypeGuard for determining whether or not the given type is a subclass of `Stream` / `AsyncStream`)r   inspectisclass
issubclassr   r^   )typorigins     r)   is_stream_class_typer   1  s0    _#F??6"Pz&6;:O'PPr,   )failure_messagec          	     H    ddl m}m} t        | dt	        d||f      |      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   )r   r^   r   ztuple[type, ...])indexgeneric_basesr   )_base_clientr   r^   r   r   )
stream_clsr   r   r^   s       r)   extract_stream_chunk_typer   7  s-     2%-/DE'	 r,   )r   typerQ   z;TypeGuard[type[Stream[object]] | type[AsyncStream[object]]])r   r   r   r   rQ   r   )#
__future__r   r=   r   typesr   typingr   r   r   r   r	   r
   r   typing_extensionsr   r   r   r   r   r   httpx_utilsr   r!   r   r   r   r   r^   ro   r   r   r   r   rD   r,   r)   <module>r      s    "    V V V ` `  ., T]<WR[ <~=%'"+ =%@&j &jRf fR h  Q #'   
	r,   