o
    h,                     @  s  U 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	 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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& ddl'm(Z( eryddl)m*Z*m+Z+ ededZ,eddddZ-eee e,gef Z.eee gef Z/eddG dd dZ0ee0ge(e0 f Z1de2d< 	 eG dd deee-f Z3edddddd9d+d,Z4eddddd-d:d2d,Z4eddddd-d;d4d,Z4						d<d=d8d,Z4dS )>    )annotationsN)	Awaitable)	dataclassreplace)TYPE_CHECKINGAnyCallableGenericcastoverload)TypeAdapter)	TypeAliasTypeVar   )ModelBehaviorError	UserError)RunItemTResponseInputItem)RunContextWrapperTContext)ensure_strict_json_schema)	SpanError)_error_tracing_json_transforms)MaybeAwaitable)Agent	AgentBaseTHandoffInput)defaultTAgentAgentBase[Any]z
Agent[Any])boundr   T)frozenc                   @  sD   e Zd ZU ded< 	 ded< 	 ded< 	 dZded< 	 dddZdS )HandoffInputDataz$str | tuple[TResponseInputItem, ...]input_historyztuple[RunItem, ...]pre_handoff_items	new_itemsNzRunContextWrapper[Any] | Nonerun_contextkwargsr   returnc                 K  s   t | fi |S )z
        Make a copy of the handoff input data, with the given arguments changed. For example, you
        could do:
        ```
        new_handoff_input_data = handoff_input_data.clone(new_items=())
        ```
        )dataclasses_replace)selfr)    r-   P/var/www/html/openai_agents/venv/lib/python3.10/site-packages/agents/handoffs.pyclone:   s   zHandoffInputData.clone)r)   r   r*   r$   )__name__
__module____qualname____annotations__r(   r/   r-   r-   r-   r.   r$   "   s   
 r$   r   HandoffInputFilterc                   @  s   e Zd ZU dZded< 	 ded< 	 ded< 	 ded< 	 ded	< 	 d
Zded< 	 dZded< 	 dZded< 	 dddZe	dddZ
e	dddZd
S )Handoffa1  A handoff is when an agent delegates a task to another agent.
    For example, in a customer support scenario you might have a "triage agent" that determines
    which agent should handle the user's request, and sub-agents that specialize in different
    areas like billing, account management, etc.
    str	tool_nametool_descriptionzdict[str, Any]input_json_schemaz:Callable[[RunContextWrapper[Any], str], Awaitable[TAgent]]on_invoke_handoff
agent_nameNzHandoffInputFilter | Noneinput_filterTboolstrict_json_schemazObool | Callable[[RunContextWrapper[Any], AgentBase[Any]], MaybeAwaitable[bool]]
is_enabledagentr!   r*   c                 C  s   t d|jiS )N	assistant)jsondumpsname)r,   r@   r-   r-   r.   get_transfer_message   s   zHandoff.get_transfer_messagec                 C  s   t d|j S )Ntransfer_to_)r   transform_string_function_stylerD   clsr@   r-   r-   r.   default_tool_name   s   zHandoff.default_tool_namec                 C  s   d|j  d|jp	d S )NzHandoff to the z agent to handle the request.  )rD   handoff_descriptionrH   r-   r-   r.   default_tool_description   s   
z Handoff.default_tool_description)r@   r!   r*   r6   )r0   r1   r2   __doc__r3   r<   r>   r?   rE   classmethodrJ   rM   r-   r-   r-   r.   r5   I   s0   
 

r5   )tool_name_overridetool_description_overrider<   r?   r@   Agent[TContext]rP   
str | NonerQ   r<   5Callable[[HandoffInputData], HandoffInputData] | Noner?   Kbool | Callable[[RunContextWrapper[Any], Agent[Any]], MaybeAwaitable[bool]]r*   "Handoff[TContext, Agent[TContext]]c                C     d S Nr-   )r@   rP   rQ   r<   r?   r-   r-   r.   handoff   s   rY   )rQ   rP   r<   r?   
on_handoff!OnHandoffWithInput[THandoffInput]
input_typetype[THandoffInput]c                C  rW   rX   r-   )r@   rZ   r\   rQ   rP   r<   r?   r-   r-   r.   rY      s   
OnHandoffWithoutInputc                C  rW   rX   r-   )r@   rZ   rQ   rP   r<   r?   r-   r-   r.   rY      s   	@OnHandoffWithInput[THandoffInput] | OnHandoffWithoutInput | Nonetype[THandoffInput] | NonePbool | Callable[[RunContextWrapper[Any], Agent[TContext]], MaybeAwaitable[bool]]c              	     s  rsrrJ ddur1t sJ dt}t|jdkr(tdt }ndi }durIt}t|jdkrItd	dd fdd}	|p[t	 }
|pbt
 }t|}dfdd}t|
|||	| jt r~|dS dS )a  Create a handoff from an agent.

    Args:
        agent: The agent to handoff to, or a function that returns an agent.
        tool_name_override: Optional override for the name of the tool that represents the handoff.
        tool_description_override: Optional override for the description of the tool that
            represents the handoff.
        on_handoff: A function that runs when the handoff is invoked.
        input_type: the type of the input to the handoff. If provided, the input will be validated
            against this type. Only relevant if you pass a function that takes an input.
        input_filter: a function that filters the inputs that are passed to the next agent.
        is_enabled: Whether the handoff is enabled. Can be a bool or a callable that takes the run
            context and agent and returns whether the handoff is enabled. Disabled handoffs are
            hidden from the LLM at runtime.
    zBYou must provide either both on_handoff and input_type, or neitherNzon_handoff must be callable   z5on_handoff must take two arguments: context and inputr   z*on_handoff must take one argument: contextctxRunContextWrapper[Any]
input_jsonrS   r*   rR   c                   s   d urAd urA|d u rt tdddid tdtj|dd}ttt }t	
|r:|| |I d H   S || |  S d ur\tt}t	
|rX|| I d H   S ||   S )Nz6Handoff function expected non-null input, but got Nonedetailszinput_json is None)messagedataF)json_strtype_adapterpartial)r   attach_error_to_current_spanr   r   r   validate_jsonr
   OnHandoffWithInputr   inspectiscoroutinefunctionr^   )rc   re   validated_input
input_funcno_input_func)r@   r\   rZ   rj   r-   r.   _invoke_handoff   s8   




z handoff.<locals>._invoke_handoff
agent_baser!   r=   c                   sR   ddl m} t sJ dt||sJ d | |}t|r'|I d H S |S )Nr   )r   z is_enabled must be callable herezCan't handoff to a non-Agent)r@   r   callable
isinstancero   isawaitable)rc   ru   r   result)r?   r-   r.   _is_enabled  s   


zhandoff.<locals>._is_enabled)r7   r8   r9   r:   r<   r;   r?   rX   )rc   rd   re   rS   r*   rR   )rc   rd   ru   r!   r*   r=   )rv   ro   	signaturelen
parametersr   r   json_schemar5   rJ   rM   r   rD   )r@   rP   rQ   rZ   r\   r<   r?   sigr9   rt   r7   r8   rz   r-   )r@   r\   r?   rZ   rj   r.   rY      sB   


 
)r@   rR   rP   rS   rQ   rS   r<   rT   r?   rU   r*   rV   )r@   rR   rZ   r[   r\   r]   rQ   rS   rP   rS   r<   rT   r?   rU   r*   rV   )r@   rR   rZ   r^   rQ   rS   rP   rS   r<   rT   r?   rU   r*   rV   )NNNNNT)r@   rR   rP   rS   rQ   rS   rZ   r_   r\   r`   r<   rT   r?   ra   r*   rV   )5
__future__r   ro   rB   collections.abcr   dataclassesr   r   r+   typingr   r   r   r	   r
   r   pydanticr   typing_extensionsr   r   
exceptionsr   r   itemsr   r   r(   r   r   strict_schemar   tracing.spansr   utilr   r   r   util._typesr   r@   r   r   r   r    rn   r^   r$   r4   r3   r5   rY   r-   r-   r-   r.   <module>   sh     "F
