
    .Ui,                       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rddl)m*Z*m+Z+  ede      Z, eddd      Z-eee   e,gef   Z.eee   gef   Z/ ed       G d d             Z0ee0ge(e0   f   Z1de2d<   	 e G d deee-f                Z3eddddd	 	 	 	 	 	 	 	 	 	 	 d%d        Z4eddddd!	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d&d"       Z4eddddd!	 	 	 	 	 	 	 	 	 	 	 	 	 d'd#       Z4	 	 	 	 	 	 d(	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d)d$Z4y)*    )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                  J    e Zd ZU ded<   	 ded<   	 ded<   	 dZded<   	 d
d	Zy)HandoffInputDataz$str | tuple[TResponseInputItem, ...]input_historyztuple[RunItem, ...]pre_handoff_items	new_itemsNzRunContextWrapper[Any] | Nonerun_contextc                    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)selfkwargss     k/home/jay/workspace/tools/ai-image-gen/jaaz-app/server/venv/lib/python3.12/site-packages/agents/handoffs.pyclonezHandoffInputData.clone:   s     #42622    )r.   r   returnr&   )__name__
__module____qualname____annotations__r*   r0    r1   r/   r&   r&   "   s>    77 +* #"
 26K.5
3r1   r&   r   HandoffInputFilterc                      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Ze	dd       Z
e	dd       Zy
)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_enabledc                D    t        j                  d|j                  i      S )N	assistant)jsondumpsname)r-   agents     r/   get_transfer_messagezHandoff.get_transfer_message   s    zz;

344r1   c                F    t        j                  d|j                         S )Ntransfer_to_)r   transform_string_function_stylerI   clsrJ   s     r/   default_tool_namezHandoff.default_tool_name   s    ::\%**;VWWr1   c                B    d|j                    d|j                  xs d S )NzHandoff to the z agent to handle the request.  )rI   handoff_descriptionrO   s     r/   default_tool_descriptionz Handoff.default_tool_description   s-     ejj\)G((.B/1	
r1   )rJ   r"   r2   r;   )r3   r4   r5   __doc__r6   rA   rC   rD   rK   classmethodrQ   rU   r7   r1   r/   r:   r:   I   s     N;B%% RQ O<.2L+2  $#
 	 _ .5 X X 
 
r1   r:   )tool_name_overridetool_description_overriderA   rD   c                    y Nr7   )rJ   rX   rY   rA   rD   s        r/   handoffr\      s     *-r1   )rY   rX   rA   rD   c                    y r[   r7   )rJ   
on_handoff
input_typerY   rX   rA   rD   s          r/   r\   r\      s     *-r1   c                    y r[   r7   )rJ   r^   rY   rX   rA   rD   s         r/   r\   r\      s     *-r1   c                x    rsr	rJ d       ft              sJ d       t        j                        }t        |j                        dk7  rt        d      t              j                         }n>di }8t        j                        }t        |j                        dk7  rt        d      	 d	 	 	 	 	 d fd}	|xs t        j                         }
|xs t        j                         }t        |      }dfd	}t        |
|||	| j                  t              r|
      S 
      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: contextc                  K   |-t        j                  t        dddi             t        d      t	        j
                  |d      }t        t        t                 }t        j                  |      r || |       d {    S  || |       S ?t        t              }t        j                  |      r ||        d {    S  ||        S 7 T7 w)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iscoroutinefunctionOnHandoffWithoutInput)	ctx
input_jsonvalidated_input
input_funcno_input_funcrJ   r_   r^   rh   s	        r/   _invoke_handoffz handoff.<locals>._invoke_handoff   s     !l&>!;; X')=> ))abb#11#)O
 0?LJ**:6 o666  30  # !6
CM**=9#C(((  c" 7 )s%   BC CAC CC C c                   K   ddl m} t              sJ d       t        ||      sJ d        | |      }t	        j
                  |      r
| d {   S |S 7 w)Nr   )r   z is_enabled must be callable herezCan't handoff to a non-Agent)rJ   r   callable
isinstancerm   isawaitable)rp   
agent_baser   resultrD   s       r/   _is_enabledzhandoff.<locals>._is_enabled  s`      
#G%GG#*e,L.LL,C,v&<  s   AAAA)r<   r=   r>   r?   rA   r@   rD   r[   )rp   RunContextWrapper[Any]rq   
str | Noner2   Agent[TContext])rp   r}   rz   r"   r2   rB   )rw   rm   	signaturelen
parametersr   r   json_schemar:   rQ   rU   r   rI   )rJ   rX   rY   r^   r_   rA   rD   sigr>   ru   r<   r=   r|   rh   s   `  `` `      @r/   r\   r\      sg   2 :zj LI 
#B%BB#
+s~~!#STT":.(446!##J/C3>>"a' LMM ?C#1;	 @ #Fg&?&?&FI0[G4T4TUZ4[ 22CD
 )+)!::"*:"6;  =G r1   )rJ   r   rX   r~   rY   r~   rA   5Callable[[HandoffInputData], HandoffInputData] | NonerD   Kbool | Callable[[RunContextWrapper[Any], Agent[Any]], MaybeAwaitable[bool]]r2   "Handoff[TContext, Agent[TContext]])rJ   r   r^   z!OnHandoffWithInput[THandoffInput]r_   ztype[THandoffInput]rY   r~   rX   r~   rA   r   rD   r   r2   r   )rJ   r   r^   ro   rY   r~   rX   r~   rA   r   rD   r   r2   r   )NNNNNT)rJ   r   rX   r~   rY   r~   r^   z@OnHandoffWithInput[THandoffInput] | OnHandoffWithoutInput | Noner_   ztype[THandoffInput] | NonerA   r   rD   zPbool | Callable[[RunContextWrapper[Any], Agent[TContext]], MaybeAwaitable[bool]]r2   r   )5
__future__r   rm   rG   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   rJ   r   r   r   r!   rl   ro   r&   r8   r6   r:   r\   r7   r1   r/   <module>r      s   "   % A H H   0 5 . 4 4 $ 4 4 '' 5 
!1<	H05}EsJK  "3C"8!93!>?  $3 3 3D !)*:);^L\=])] ^ I ^ F C
gh&' C
 C
L 
 &*,0JN^b-- #-  *	-
 H- \- (- 
- 
 -1%)JN^b	-	- 2	- $		-
  *	- #	- H	- \	- (	- 
	- 

 -1%)JN^b-- &-  *	-
 #- H- \- (- 
- &*,0SW-1JNRVhh"h  *h Q	h
 +h HhPh (hr1   