o
    h                      @  s*  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	rJddlmZ eG dd dZeG dd dZG dd deZG dd deZG dd deZeG dd dZeG dd dZ eG dd de Z!ede
ddZ"eG dd  d ee" Z#eG d!d" d"ee" Z$ee gef Z%ee gee f Z&ed<d%d&Z'ed=d(d&Z'edd)d>d.d&Z'	d?dd)d@d1d&Z'ee!gef Z(ee!gee f Z)edAd3d4Z*edBd6d4Z*edd)dCd8d4Z*	d?dd)dDd;d4Z*dS )E    )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< dS )ToolInputGuardrailResultz)The result of a tool input guardrail run.ToolInputGuardrail[Any]	guardrailToolGuardrailFunctionOutputoutputN__name__
__module____qualname____doc____annotations__ r   r   W/var/www/html/openai_agents/venv/lib/python3.10/site-packages/agents/tool_guardrails.pyr         
 r   c                   @  r   )ToolOutputGuardrailResultz*The result of a tool output guardrail run.ToolOutputGuardrail[Any]r   r   r   Nr   r   r   r   r    r"      r!   r"   c                   @  s"   e Zd ZU dZded< ded< dS )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   
 r$   c                   @     e Zd ZU dZded< dS )RaiseExceptionBehaviorz&Raises an exception to halt execution.zLiteral['raise_exception']r%   Nr   r   r   r   r    r)   /      
 r)   c                   @  r(   )AllowBehaviorz)Allows normal tool execution to continue.zLiteral['allow']r%   Nr   r   r   r   r    r+   5   r*   r+   c                   @  sd   e Zd ZU dZded< 	 edd dZded< 	 eddddZeddddZ	eddddZ
d	S )r   z(The output of a tool guardrail function.r   output_infoc                   C  s
   t ddS )Nallowr%   r+   r   r   r   r    <lambda>F   s   
 z$ToolGuardrailFunctionOutput.<lambda>)default_factoryz>RejectContentBehavior | RaiseExceptionBehavior | AllowBehaviorbehaviorNreturnc                 C     | |t dd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.
        r-   r.   r,   r2   r/   clsr,   r   r   r    r-   O      
z!ToolGuardrailFunctionOutput.allowr'   r&   c                 C  s   | |t d|d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'   r5   )r$   )r7   r'   r,   r   r   r    r9   [   s   
z*ToolGuardrailFunctionOutput.reject_contentc                 C  r4   )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_exceptionr.   r5   )r)   r6   r   r   r    r:   k   r8   z+ToolGuardrailFunctionOutput.raise_exceptionN)r,   r   r3   r   )r'   r&   r,   r   r3   r   )r   r   r   r   r   r   r2   classmethodr-   r9   r:   r   r   r   r    r   ;   s   
 r   c                   @  r   )ToolInputGuardrailDataz5Input data passed to a tool input guardrail function.zToolContext[Any]contextz
Agent[Any]agentNr   r   r   r   r    r=   x   s   
 r=   c                   @  r(   )ToolOutputGuardrailDatazkInput data passed to a tool output guardrail function.

    Extends input data with the tool's output.
    r   r   Nr   r   r   r   r    r@      s   
 r@   TContext_coT)bound	covariantc                   @  >   e Zd ZU dZded< 	 dZded< 	 dd	d
ZdddZdS )ToolInputGuardrailz8A guardrail that runs before a function tool is invoked.zOCallable[[ToolInputGuardrailData], MaybeAwaitable[ToolGuardrailFunctionOutput]]guardrail_functionN
str | Nonenamer3   r&   c                 C     | j p| jjS r;   rH   rF   r   selfr   r   r    get_name      zToolInputGuardrail.get_namedatar=   r   c                   >   t | jstd| j | |}t|r|I d H S |S Nz)Guardrail function must be callable, got callablerF   r   inspectisawaitablerL   rO   resultr   r   r    run      



zToolInputGuardrail.runr3   r&   )rO   r=   r3   r   r   r   r   r   r   rH   rM   rX   r   r   r   r    rE         
 
rE   c                   @  rD   )ToolOutputGuardrailz7A guardrail that runs after a function tool is invoked.zPCallable[[ToolOutputGuardrailData], MaybeAwaitable[ToolGuardrailFunctionOutput]]rF   NrG   rH   r3   r&   c                 C  rI   r;   rJ   rK   r   r   r    rM      rN   zToolOutputGuardrail.get_namerO   r@   r   c                   rP   rQ   rR   rV   r   r   r    rX      rY   zToolOutputGuardrail.runrZ   )rO   r@   r3   r   r[   r   r   r   r    r]      r\   r]   func_ToolInputFuncSyncc                 C     d S r;   r   r^   r   r   r    tool_input_guardrail      rb   _ToolInputFuncAsyncc                 C  r`   r;   r   ra   r   r   r    rb      rc   rH   rH   rG   r3   MCallable[[_ToolInputFuncSync | _ToolInputFuncAsync], ToolInputGuardrail[Any]]c                 C  r`   r;   r   re   r   r   r    rb         /_ToolInputFuncSync | _ToolInputFuncAsync | NonegToolInputGuardrail[Any] | Callable[[_ToolInputFuncSync | _ToolInputFuncAsync], ToolInputGuardrail[Any]]c                  "   d fdd}| dur|| S |S )	z9Decorator to create a ToolInputGuardrail from a function.f(_ToolInputFuncSync | _ToolInputFuncAsyncr3   r   c                      t |  p| jdS N)rF   rH   )rE   r   rk   re   r   r    	decorator      z'tool_input_guardrail.<locals>.decoratorN)rk   rl   r3   r   r   r^   rH   rp   r   re   r    rb         
_ToolOutputFuncSyncc                 C  r`   r;   r   ra   r   r   r    tool_output_guardrail   rc   ru   _ToolOutputFuncAsyncc                 C  r`   r;   r   ra   r   r   r    ru      rc   PCallable[[_ToolOutputFuncSync | _ToolOutputFuncAsync], ToolOutputGuardrail[Any]]c                 C  r`   r;   r   re   r   r   r    ru     rg   1_ToolOutputFuncSync | _ToolOutputFuncAsync | NonekToolOutputGuardrail[Any] | Callable[[_ToolOutputFuncSync | _ToolOutputFuncAsync], ToolOutputGuardrail[Any]]c                  rj   )	z:Decorator to create a ToolOutputGuardrail from a function.rk   *_ToolOutputFuncSync | _ToolOutputFuncAsyncr3   r#   c                   rm   rn   )r]   r   ro   re   r   r    rp     rq   z(tool_output_guardrail.<locals>.decoratorN)rk   rz   r3   r#   r   rr   r   re   r    ru     rs   )r^   r_   )r^   rd   )rH   rG   r3   rf   r;   )r^   rh   rH   rG   r3   ri   )r^   rt   )r^   rv   )rH   rG   r3   rw   )r^   rx   rH   rG   r3   ry   )+
__future__r   rT   collections.abcr   dataclassesr   r   typingr   r   r   r	   r
   r   typing_extensionsr   r   
exceptionsr   tool_contextr   util._typesr   r?   r   r   r"   r$   r)   r+   r   r=   r@   rA   rE   r]   r_   rd   rb   rt   rv   ru   r   r   r   r    <module>   sn     

<