
    .hK$                    T   d dl mZ d dl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 d dlmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZ erddlmZ e G d d                      Ze G d d                      Ze G d d                      Ze G d dee                               Ze G d dee                               Z ede	d          Z e
ee          dee!e"e         f         gef         Z#e
ee          dee!e"e         f         gee         f         Z$ed4d            Z%ed5d!            Z%edd"d6d&            Z%	 d7dd"d8d)Z%e
ee          de	gef         Z&e
ee          de	gee         f         Z'ed9d,            Z(ed:d.            Z(edd"d;d0            Z(	 d7dd"d<d3Z(dS )=    )annotationsN)	Awaitable)	dataclass)TYPE_CHECKINGAnyCallableGenericUnionoverload)TypeVar   )	UserError)TResponseInputItem)RunContextWrapperTContext)MaybeAwaitable)Agentc                  *    e Zd ZU dZded<   	 ded<   dS )GuardrailFunctionOutputz#The output of a guardrail function.r   output_infobooltripwire_triggeredN__name__
__module____qualname____doc____annotations__     M/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/agents/guardrail.pyr   r      s;         --
  r    r   c                  *    e Zd ZU dZded<   	 ded<   dS )InputGuardrailResultThe result of a guardrail run.zInputGuardrail[Any]	guardrailr   outputNr   r   r    r!   r#   r#   #   s7         (("""" $###//r    r#   c                  B    e Zd ZU dZded<   	 ded<   	 ded<   	 ded	<   d
S )OutputGuardrailResultr$   zOutputGuardrail[Any]r%   r   agent_output
Agent[Any]agentr   r&   Nr   r   r    r!   r(   r(   0   s]         ((####   $###//r    r(   c                  @    e Zd ZU dZded<   	 dZded<   	 dd	ZddZdS )InputGuardrailaI  Input guardrails are checks that run in parallel to the agent's execution.
    They can be used to do things like:
    - Check if input messages are off-topic
    - Take over control of the agent's execution if an unexpected input is detected

    You can use the `@input_guardrail()` decorator to turn a function into an `InputGuardrail`, or
    create an `InputGuardrail` manually.

    Guardrails return a `GuardrailResult`. If `result.tripwire_triggered` is `True`, the agent
    execution will immediately stop and a `InputGuardrailTripwireTriggered` exception will be raised
    z|Callable[[RunContextWrapper[TContext], Agent[Any], str | list[TResponseInputItem]], MaybeAwaitable[GuardrailFunctionOutput]]guardrail_functionN
str | Nonenamereturnstrc                6    | j         r| j         S | j        j        S Nr0   r.   r   selfs    r!   get_namezInputGuardrail.get_namec        9 	9&//r    r+   r*   inputstr | list[TResponseInputItem]contextRunContextWrapper[TContext]r#   c                  K   t          | j                  st          d| j                   |                     |||          }t          j        |          rt          | | d {V           S t          | |          S )N)Guardrail function must be callable, got )r%   r&   )callabler.   r   inspectisawaitabler#   )r7   r+   r:   r<   r&   s        r!   runzInputGuardrail.runi   s       /00 	caH_aabbb((%??v&& 	'#||||||   
 $
 
 
 	
r    r1   r2   )r+   r*   r:   r;   r<   r=   r1   r#   r   r   r   r   r   r0   r8   rC   r   r    r!   r-   r-   G   sy         
 
   
 D0 0 0 0
 
 
 
 
 
r    r-   c                  @    e Zd ZU dZded<   	 dZded<   	 dd	ZddZdS )OutputGuardraila  Output guardrails are checks that run on the final output of an agent.
    They can be used to do check if the output passes certain validation criteria

    You can use the `@output_guardrail()` decorator to turn a function into an `OutputGuardrail`,
    or create an `OutputGuardrail` manually.

    Guardrails return a `GuardrailResult`. If `result.tripwire_triggered` is `True`, a
    `OutputGuardrailTripwireTriggered` exception will be raised.
    zaCallable[[RunContextWrapper[TContext], Agent[Any], Any], MaybeAwaitable[GuardrailFunctionOutput]]r.   Nr/   r0   r1   r2   c                6    | j         r| j         S | j        j        S r4   r5   r6   s    r!   r8   zOutputGuardrail.get_name   r9   r    r<   r=   r+   r*   r)   r   r(   c                
  K   t          | j                  st          d| j                   |                     |||          }t          j        |          rt          | ||| d {V           S t          | |||          S )Nr?   )r%   r+   r)   r&   )r@   r.   r   rA   rB   r(   )r7   r<   r+   r)   r&   s        r!   rC   zOutputGuardrail.run   s       /00 	caH_aabbb((%FFv&& 	()#||||||	    %%	
 
 
 	
r    rD   )r<   r=   r+   r*   r)   r   r1   r(   rE   r   r    r!   rG   rG      sy             
 D0 0 0 0
 
 
 
 
 
r    rG   TContext_coT)bound	covariantr*   func$_InputGuardrailFuncSync[TContext_co]r1   InputGuardrail[TContext_co]c                    d S r4   r   rM   s    r!   input_guardrailrR      	     #&#r    %_InputGuardrailFuncAsync[TContext_co]c                    d S r4   r   rQ   s    r!   rR   rR      rS   r    r0   r0   r/   uCallable[[_InputGuardrailFuncSync[TContext_co] | _InputGuardrailFuncAsync[TContext_co]], InputGuardrail[TContext_co]]c                    d S r4   r   rV   s    r!   rR   rR      	     3r    S_InputGuardrailFuncSync[TContext_co] | _InputGuardrailFuncAsync[TContext_co] | NoneInputGuardrail[TContext_co] | Callable[[_InputGuardrailFuncSync[TContext_co] | _InputGuardrailFuncAsync[TContext_co]], InputGuardrail[TContext_co]]c               .    dfd}|  ||           S |S )aA  
    Decorator that transforms a sync or async function into an `InputGuardrail`.
    It can be used directly (no parentheses) or with keyword args, e.g.:

        @input_guardrail
        def my_sync_guardrail(...): ...

        @input_guardrail(name="guardrail_name")
        async def my_async_guardrail(...): ...
    fL_InputGuardrailFuncSync[TContext_co] | _InputGuardrailFuncAsync[TContext_co]r1   rO   c                &    t          |           S N)r.   r0   )r-   r]   r0   s    r!   	decoratorz"input_guardrail.<locals>.decorator   s     >>>>r    N)r]   r^   r1   rO   r   rM   r0   rb   s    ` r!   rR   rR      s?    0? ? ? ? ? ?
 y r    %_OutputGuardrailFuncSync[TContext_co]OutputGuardrail[TContext_co]c                    d S r4   r   rQ   s    r!   output_guardrailrg     	     $'3r    &_OutputGuardrailFuncAsync[TContext_co]c                    d S r4   r   rQ   s    r!   rg   rg     rh   r    xCallable[[_OutputGuardrailFuncSync[TContext_co] | _OutputGuardrailFuncAsync[TContext_co]], OutputGuardrail[TContext_co]]c                    d S r4   r   rV   s    r!   rg   rg     rY   r    U_OutputGuardrailFuncSync[TContext_co] | _OutputGuardrailFuncAsync[TContext_co] | NoneOutputGuardrail[TContext_co] | Callable[[_OutputGuardrailFuncSync[TContext_co] | _OutputGuardrailFuncAsync[TContext_co]], OutputGuardrail[TContext_co]]c               .    dfd}|  ||           S |S )aD  
    Decorator that transforms a sync or async function into an `OutputGuardrail`.
    It can be used directly (no parentheses) or with keyword args, e.g.:

        @output_guardrail
        def my_sync_guardrail(...): ...

        @output_guardrail(name="guardrail_name")
        async def my_async_guardrail(...): ...
    r]   N_OutputGuardrailFuncSync[TContext_co] | _OutputGuardrailFuncAsync[TContext_co]r1   re   c                &    t          |           S r`   )rG   ra   s    r!   rb   z#output_guardrail.<locals>.decorator6  s     !$????r    N)r]   rp   r1   re   r   rc   s    ` r!   rg   rg     sE    0@ @ @ @ @ @
 y r    )rM   rN   r1   rO   )rM   rT   r1   rO   )r0   r/   r1   rW   r4   )rM   rZ   r0   r/   r1   r[   )rM   rd   r1   re   )rM   ri   r1   re   )r0   r/   r1   rk   )rM   rm   r0   r/   r1   rn   ))
__future__r   rA   collections.abcr   dataclassesr   typingr   r   r   r	   r
   r   typing_extensionsr   
exceptionsr   itemsr   run_contextr   r   util._typesr   r+   r   r   r#   r(   r-   rG   rJ   r2   list_InputGuardrailFuncSync_InputGuardrailFuncAsyncrR   _OutputGuardrailFuncSync_OutputGuardrailFuncAsyncrg   r   r    r!   <module>r      s   " " " " " "  % % % % % % ! ! ! ! ! ! I I I I I I I I I I I I I I I I % % % % % % ! ! ! ! ! ! % % % % % % 4 4 4 4 4 4 4 4 ' ' ' ' ' '          	0 	0 	0 	0 	0 	0 	0 	0 0 0 0 0 0 0 0 0, 4
 4
 4
 4
 4
WX& 4
 4
 4
n 3
 3
 3
 3
 3
gh' 3
 3
 3
l gm3$??? #{#\5dCU>V9V3WX  ${#\5dCU>V9V3WX%&(  
& & & 
&
 
& & & 
&
 
      
 "
 " " " " " "J ${#\37  %{#\37%&(  
' ' ' 
'
 
' ' ' 
'
 
      
 "
 " " " " " " " "r    