
    .Ui                        d dl mZ d dlZd dlmZ d dlmZmZ d dlm	Z	m
Z
mZmZ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 e	rddlmZ e G d d             Ze G d d             Z G d de      Z G d de      Z G d de      Ze G d d             Ze G d d             Z e G d de              Z! ede
d      Z"e G d d ee"                Z#e G d! d"ee"                Z$ee gef   Z%ee gee   f   Z&ed,d#       Z'ed-d$       Z'edd%	 	 	 d.d&       Z'	 d/dd%	 	 	 	 	 d0d'Z'ee!gef   Z(ee!gee   f   Z)ed1d(       Z*ed2d)       Z*edd%	 	 	 d3d*       Z*	 d/dd%	 	 	 	 	 d4d+Z*y)5    )annotationsN)	Awaitable)	dataclassfield)TYPE_CHECKINGAnyCallableGenericLiteraloverload)	TypedDictTypeVar   )	UserError)ToolContext)MaybeAwaitable)Agentc                  (    e Zd ZU dZded<   	 ded<   y)ToolInputGuardrailResultz)The result of a tool input guardrail run.ToolInputGuardrail[Any]	guardrailToolGuardrailFunctionOutputoutputN__name__
__module____qualname____doc____annotations__     r/home/jay/workspace/tools/ai-image-gen/jaaz-app/server/venv/lib/python3.12/site-packages/agents/tool_guardrails.pyr   r      s    3&&%''/r!   r   c                  (    e Zd ZU dZded<   	 ded<   y)ToolOutputGuardrailResultz*The result of a tool output guardrail run.ToolOutputGuardrail[Any]r   r   r   Nr   r    r!   r"   r$   r$      s    4''%''/r!   r$   c                  &    e Zd ZU dZded<   ded<   y)RejectContentBehaviorzQRejects the tool call/output but continues execution with a message to the model.zLiteral['reject_content']typestrmessageNr   r    r!   r"   r'   r'   (   s    [
##Lr!   r'   c                      e Zd ZU dZded<   y)RaiseExceptionBehaviorz&Raises an exception to halt execution.zLiteral['raise_exception']r(   Nr   r    r!   r"   r,   r,   /   s    0
$$r!   r,   c                      e Zd ZU dZded<   y)AllowBehaviorz)Allows normal tool execution to continue.zLiteral['allow']r(   Nr   r    r!   r"   r.   r.   5   s    3
r!   r.   c                  z    e Zd ZU dZded<   	  ed       Zded<   	 eddd	       Zeddd
       Z	eddd       Z
y)r   z(The output of a tool guardrail function.r   output_infoc                     t        d      S )Nallowr(   r.   r    r!   r"   <lambda>z$ToolGuardrailFunctionOutput.<lambda>F   s    7 ; r!   )default_factoryz>RejectContentBehavior | RaiseExceptionBehavior | AllowBehaviorbehaviorNc                *     | |t        d            S )a  Create a guardrail output that allows the tool execution to continue normally.

        Args:
            output_info: Optional data about checks performed.

        Returns:
            ToolGuardrailFunctionOutput configured to allow normal execution.
        r2   r3   r0   r7   r4   clsr0   s     r"   r2   z!ToolGuardrailFunctionOutput.allowO   s     {]5PQQr!   c                ,     | |t        d|            S )aU  Create a guardrail output that rejects the tool call/output but continues execution.

        Args:
            message: Message to send to the model instead of the tool result.
            output_info: Optional data about checks performed.

        Returns:
            ToolGuardrailFunctionOutput configured to reject the content.
        reject_content)r(   r*   r9   )r'   )r;   r*   r0   s      r"   r=   z*ToolGuardrailFunctionOutput.reject_content[   s     #*0@'R
 	
r!   c                *     | |t        d            S )zCreate a guardrail output that raises an exception to halt execution.

        Args:
            output_info: Optional data about checks performed.

        Returns:
            ToolGuardrailFunctionOutput configured to raise an exception.
        raise_exceptionr3   r9   )r,   r:   s     r"   r?   z+ToolGuardrailFunctionOutput.raise_exceptionk   s     {5KQb5cddr!   N)r0   r   returnr   )r*   r)   r0   r   rA   r   )r   r   r   r   r   r   r7   classmethodr2   r=   r?   r    r!   r"   r   r   ;   so    2
 PU;PHL  	R 	R 
 
 	e 	er!   r   c                  (    e Zd ZU dZded<   	 ded<   y)ToolInputGuardrailDataz5Input data passed to a tool input guardrail function.zToolContext[Any]contextz
Agent[Any]agentNr   r    r!   r"   rD   rD   x   s    ? r!   rD   c                      e Zd ZU dZded<   y)ToolOutputGuardrailDatazkInput data passed to a tool output guardrail function.

    Extends input data with the tool's output.
    r   r   Nr   r    r!   r"   rH   rH      s    
 Kr!   rH   TContext_coT)bound	covariantc                  >    e Zd ZU dZded<   	 dZded<   	 d	dZd
dZy)ToolInputGuardrailz8A guardrail that runs before a function tool is invoked.zOCallable[[ToolInputGuardrailData], MaybeAwaitable[ToolGuardrailFunctionOutput]]guardrail_functionN
str | Nonenamec                J    | j                   xs | j                  j                  S r@   rP   rN   r   selfs    r"   get_namezToolInputGuardrail.get_name       yy<D33<<<r!   c                   K   t        | j                        st        d| j                         | j                  |      }t        j                  |      r
| d {   S |S 7 wNz)Guardrail function must be callable, got callablerN   r   inspectisawaitablerT   dataresults      r"   runzToolInputGuardrail.run   ^     //0GH_H_G`abb((.v&<     AA$A"A$rA   r)   )r^   rD   rA   r   r   r   r   r   r   rP   rU   r`   r    r!   r"   rM   rM      s/    B  D*=r!   rM   c                  >    e Zd ZU dZded<   	 dZded<   	 d	dZd
dZy)ToolOutputGuardrailz7A guardrail that runs after a function tool is invoked.zPCallable[[ToolOutputGuardrailData], MaybeAwaitable[ToolGuardrailFunctionOutput]]rN   NrO   rP   c                J    | j                   xs | j                  j                  S r@   rR   rS   s    r"   rU   zToolOutputGuardrail.get_name   rV   r!   c                   K   t        | j                        st        d| j                         | j                  |      }t        j                  |      r
| d {   S |S 7 wrX   rY   r]   s      r"   r`   zToolOutputGuardrail.run   ra   rb   rc   )r^   rH   rA   r   rd   r    r!   r"   rf   rf      s/    A  D*=r!   rf   c                     y r@   r    funcs    r"   tool_input_guardrailrl      s    47r!   c                     y r@   r    rj   s    r"   rl   rl      s    58r!   rP   c                     y r@   r    rn   s    r"   rl   rl      s	     UXr!   c               (    dfd}|  ||       S |S )z9Decorator to create a ToolInputGuardrail from a function.c                :    t        | xs | j                        S N)rN   rP   )rM   r   frP   s    r"   	decoratorz'tool_input_guardrail.<locals>.decorator   s    !QT=OQZZPPr!   )rt   z(_ToolInputFuncSync | _ToolInputFuncAsyncrA   r   r    rk   rP   ru   s    ` r"   rl   rl      s!    Q r!   c                     y r@   r    rj   s    r"   tool_output_guardrailrx      s    69r!   c                     y r@   r    rj   s    r"   rx   rx      s    7:r!   c                     y r@   r    rn   s    r"   rx   rx     s	     X[r!   c               (    dfd}|  ||       S |S )z:Decorator to create a ToolOutputGuardrail from a function.c                :    t        | xs | j                        S rr   )rf   r   rs   s    r"   ru   z(tool_output_guardrail.<locals>.decorator  s    "ad>PajjQQr!   )rt   z*_ToolOutputFuncSync | _ToolOutputFuncAsyncrA   r%   r    rv   s    ` r"   rx   rx     s!    R r!   )rk   _ToolInputFuncSync)rk   _ToolInputFuncAsync)rP   rO   rA   zMCallable[[_ToolInputFuncSync | _ToolInputFuncAsync], ToolInputGuardrail[Any]]r@   )rk   z/_ToolInputFuncSync | _ToolInputFuncAsync | NonerP   rO   rA   zgToolInputGuardrail[Any] | Callable[[_ToolInputFuncSync | _ToolInputFuncAsync], ToolInputGuardrail[Any]])rk   _ToolOutputFuncSync)rk   _ToolOutputFuncAsync)rP   rO   rA   zPCallable[[_ToolOutputFuncSync | _ToolOutputFuncAsync], ToolOutputGuardrail[Any]])rk   z1_ToolOutputFuncSync | _ToolOutputFuncAsync | NonerP   rO   rA   zkToolOutputGuardrail[Any] | Callable[[_ToolOutputFuncSync | _ToolOutputFuncAsync], ToolOutputGuardrail[Any]])+
__future__r   r[   collections.abcr   dataclassesr   r   typingr   r   r	   r
   r   r   typing_extensionsr   r   
exceptionsr   tool_contextr   util._typesr   rF   r   r   r$   r'   r,   r.   r   rD   rH   rI   rM   rf   r}   r~   rl   r   r   rx   r    r!   r"   <module>r      s}   "  % ( K K 0 ! % ' 0 0 0 0 0 0I %Y %I  9e 9e 9ex    	4 	 	 m3$? -  8 '+.  : 568SST  67C^9__`  
 7 
 7 
 8 
 8 
XXRX 
X =A 
9 
T$  78:UUV !8 99E`;a ab  
 9 
 9 
 : 
 : 
[[U[ 
[ ?C 
; 
Wr!   