
    g1"                    0   U d dl mZ d dlZd dlZd dlmZ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 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 d d
lmZ d dl m!Z! d dl"m#Z#m$Z$m%Z%m&Z& erddl'm(Z(m)Z)m*Z* ddl+m,Z,  e#d      Z- ed      Z.d&dZ/d'dZ0d(dZ1 ed      Z2e G d dee2                Z3ee3e2   df   Z4de5d<   	  G d d      Z6 e6       Z7d)dZ8 ed      Z9 ed      Z: G d d ee9e:f         Z;e
	 	 	 	 	 	 d*d!       Z<d+d"Z=d,d#Z>d-d$Z?d.d%Z@y)/    )annotationsN)AsyncIterableAsyncIteratorIterator)asynccontextmanagersuppress)	dataclassis_dataclass)datetimetimezone)partial)GenericAlias)TYPE_CHECKINGAnyCallableGenericTypeVarUnioncastoverload)	BaseModel)JsonSchemaValue)	ParamSpec	TypeAlias	TypeGuardis_typeddict   )RetryPromptPartToolCallPartToolReturnPart)ObjectJsonSchema_P_Rc                   K   |r9t        j                         j                  d t        | g|i |       d {   S  t        j                         j                  d | g|  d {   S 7 27 wN)asyncioget_running_looprun_in_executorr   )funcargskwargss      G/var/www/openai/venv/lib/python3.12/site-packages/pydantic_ai/_utils.pyr(   r(      si     --/??gdFdUYFd]cFdeee?W--/??dRTRRR fRs!   7A0A,-A0'A.(A0.A0c                    t        | t              xr= t        | t               xr* t        | t              xs t        |       xs t        |       S )zCheck if something is a pydantic model, dataclass or typedict.

    These should all generate a JSON Schema with `{"type": "object"}` and therefore be usable directly as
    function parameters.
    )
isinstancetyper   
issubclassr   r
   r   )type_s    r,   is_model_liker2   !   sK     	5$ 	Y5,//	Yy)W\%-@WLQVDW    c                J    ddl m} | j                  d      dk(  r| S  |d      )Nr   )	UserErrorr/   objectzSchema must be an object)
exceptionsr5   get)schemar5   s     r,   check_object_json_schemar:   .   s'    %zz&X%233r3   Tc                      e Zd ZU dZded<   y)Somez(Analogous to Rust's `Option::Some` type.r;   valueN)__name__
__module____qualname____doc____annotations__ r3   r,   r=   r=   :   s
    2Hr3   r=   r   Optionc                      e Zd ZdZy)Unsetz(A singleton to represent an unset value.N)r?   r@   rA   rB   rD   r3   r,   rG   rG   E   s    2r3   rG   c                    | t         uS r%   )UNSET)
t_or_unsets    r,   is_setrK   N   s    U""r3   LeftRightc                      e Zd ZdZdZedd       Zedd       ZeefddZedd       Z	edd       Z
ddZdd	Zd
 Zy)EitherzTwo member Union that records which member was set, this is analogous to Rust enums with two variants.

    Usage:

    ```python
    if left_thing := either.left:
        use_left(left_thing.value)
    else:
        use_right(either.right)
    ```
    )_left_rightc                    y r%   rD   selflefts     r,   __init__zEither.__init__e   s    /2r3   c                    y r%   rD   )rT   rights     r,   rV   zEither.__init__h   s    14r3   c                    |t         ur.|t         u sJ d       t        t        t        |            | _        y |t         usJ d       d | _        t        t
        |      | _        y )Nz>`Either` must receive exactly one argument - `left` or `right`)rI   r=   r   rL   rP   rM   rQ   )rT   rU   rX   s      r,   rV   zEither.__init__k   sX    uE>c#cc>'+Dt,<'=DJ%g'gg%DJue,DKr3   c                    | j                   S r%   rP   rT   s    r,   rU   zEither.leftt   s    zzr3   c                    | j                   S r%   )rQ   r\   s    r,   rX   zEither.rightx   s    {{r3   c                    | j                   d uS r%   r[   r\   s    r,   is_leftzEither.is_left|   s    zz%%r3   c                ^    | j                   | j                   j                  S | j                  S r%   rP   r>   rX   r\   s    r,   	whicheverzEither.whichever   s$    #'::#9tzzItzzIr3   c                ^    | j                   x}rd|j                  dS d| j                  dS )NzEither(left=)zEither(right=ra   rS   s     r,   __repr__zEither.__repr__   s7    ::4!$**q11"4::.22r3   N)rU   rL   returnNone)rX   rM   rf   rg   )rU   zLeft | UnsetrX   zRight | Unsetrf   rg   )rf   zOption[Left])rf   rM   )rf   bool)rf   zLeft | Right)r?   r@   rA   rB   	__slots__r   rV   rI   propertyrU   rX   r_   rb   re   rD   r3   r,   rO   rO   V   sl    
 "I2 24 4,1% -    &J3r3   rO   c                  K   d fd} |        ydd fd}	  |        r>j                  d       t        t        j                        5   d{    ddd       yy7 # 1 sw Y   yxY w# rKj                  d       t        t        j                        5   d{  7   ddd       w # 1 sw Y   w xY ww xY ww)a  Group items from an async iterable into lists based on time interval between them.

    Effectively debouncing the iterator.

    This returns a context manager usable as an iterator so any pending tasks can be cancelled if an error occurs
    during iteration.

    Usage:

    ```python
    async with group_by_temporal(yield_groups(), 0.1) as groups_iter:
        async for groups in groups_iter:
            print(groups)
    ```

    Args:
        aiter: The async iterable to group.
        soft_max_interval: Maximum interval over which to group items, this should avoid a trickle of items causing
            a group to never be yielded. It's a soft max in the sense that once we're over this time, we yield items
            as soon as `aiter.__anext__()` returns. If `None`, no grouping/debouncing is performed

    Returns:
        A context manager usable as an iterator async iterable of lists of items from the input async iterable.
    Nc                8   K   2 3 d {   } | g 7 6 y wr%   rD   )itemaiters    r,   async_iter_groups_noopz1group_by_temporal.<locals>.async_iter_groups_noop   s      # dfes   c                  K   dk\  sJ d       g } t        j                         }	 |}nt        j                         |z
  z
  }#t        j                  j	                               t        j
                  f|       d {   \  }}|rI	 |j                         j                         }| j                  |       d | t        j                         }n| r	|  g } d }7 ^# t        $ r | r|  d Y y w xY ww)Nr   z+soft_max_interval must be a positive number)timeout)
time	monotonicr&   create_task	__anext__waitpopresultappendStopAsyncIteration)	buffergroup_start_time	wait_timedone_rm   rn   soft_max_intervaltasks	         r,   async_iter_groupsz,group_by_temporal.<locals>.async_iter_groups   s     !,1Ba1GvIvvG>>+'-	 .1ADT1TU	 | **5??+<= $LL$)DDGD!<88:,,.D MM$'D'/+/>>+;(#' O  E * $Ds6   BDC'		DC) 17D)C>;D=C>>Dz#Cancelling due to error in iterator)rf   zAsyncIterator[list[T]])cancelr   r&   CancelledError)rn   r   ro   r   r   s   ``  @r,   group_by_temporalr      s     8  	 %&& $(D.(`!! KK=>'001

 21   21 KK=>'001

 211 si   C
A= ,CA1 A/!A1%
C/A11A:6C=-C*B?0B3
1B?6	C?CCCc                    | |S || S | |z   S )zAdd two optional strings.rD   )abs     r,   add_optionalr      s    y	
1ur3   c                R    	 t        |       S # t        $ r}t               |d}~ww xY w)zGet the next item from a sync iterator, raising `StopAsyncIteration` if it's exhausted.

    Useful when iterating over a sync iterator in an async context.
    N)nextStopIterationrz   )iteratores     r,   
sync_anextr      s,    
*H~ * ")*s   
 	&!&c                 J    t        j                  t        j                        S )N)tz)r   nowr   utcrD   r3   r,   now_utcr      s    <<8<<((r3   c                J    | j                   J | d|         | j                   S )zWType guard that checks a `tool_call_id` is not None both for static typing and runtime.z$ requires `tool_call_id` to be set: )tool_call_id)tmodel_sources     r,   guard_tool_call_idr     s/    >>%_,7[\][^'__%>>r3   )r)   zCallable[_P, _R]r*   z_P.argsr+   z	_P.kwargsrf   r#   )r1   r   rf   rh   )r9   r   rf   r!   )rJ   z	T | Unsetrf   zTypeGuard[T])rn   zAsyncIterator[T]r   zfloat | Nonerf   z%AsyncIterator[AsyncIterable[list[T]]])r   
str | Noner   r   rf   r   )r   zIterator[T]rf   r;   )rf   r   )r   z/ToolCallPart | ToolReturnPart | RetryPromptPartr   strrf   r   )A
__future__r   _annotationsr&   rr   collections.abcr   r   r   
contextlibr   r   dataclassesr	   r
   r   r   	functoolsr   typesr   typingr   r   r   r   r   r   r   r   pydanticr   pydantic.json_schemar   typing_extensionsr   r   r   r   messagesr   r   r    toolsr!   r"   r#   r(   r2   r:   r;   r=   rE   rC   rG   rI   rK   rL   rM   rO   r   r   r   r   r   rD   r3   r,   <module>r      s5   2   B B 4 / '   X X X  0 K KGG't_T]S
4 CL 71:   $q'4-(	 ( f	 	 	# v03WT5[! 03f ^^0<^*^ ^B*)r3   