
    .Ui.                       d dl mZ d dlZd dl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mZ ddl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 ddlmZ erddlmZ ddl m!Z!  ede      Z"eee   e"gef   Z#eee   gef   Z$edddd	 	 	 	 	 	 	 	 	 dd       Z%edddd	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Z%edddd	 	 	 	 	 	 	 	 	 	 	 dd       Z%	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ%y)    )annotationsN)TYPE_CHECKINGAnyCallablecastoverload)TypeAdapter)TypeVar   )ModelBehaviorError	UserError)Handoff)RunContextWrapperTContext)ensure_strict_json_schema)	SpanError)_error_tracing_json)MaybeAwaitable)	AgentBase   )RealtimeAgentTHandoffInput)defaultT)tool_name_overridetool_description_override
is_enabledc                    y N )agentr   r   r   s       t/home/jay/workspace/tools/ai-image-gen/jaaz-app/server/venv/lib/python3.12/site-packages/agents/realtime/handoffs.pyrealtime_handoffr#      s     25    )r   r   r   c                    y r   r    )r!   
on_handoff
input_typer   r   r   s         r"   r#   r#   (   s     25r$   c                    y r   r    )r!   r&   r   r   r   s        r"   r#   r#   5   s     25r$   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        |	|
||d j                  t              r|
      S 
      S )a  Create a handoff from a RealtimeAgent.

    Args:
        agent: The RealtimeAgent to handoff to, or a function that returns a RealtimeAgent.
        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.
        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.

    Note: input_filter is not supported for RealtimeAgent handoffs.
    zBYou must provide either both on_handoff and input_type, or neitherNzon_handoff must be callabler   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_funcr!   r'   r&   r/   s	        r"   _invoke_handoffz)realtime_handoff.<locals>._invoke_handoffn   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   t              sJ d       t        |t              sJ d        | |      }t        j                  |      r
| d {   S |S 7 w)Nz is_enabled must be non-null herez$Can't handoff to a non-RealtimeAgent)callable
isinstancer   r4   isawaitable)r7   
agent_baseresultr   s      r"   _is_enabledz%realtime_handoff.<locals>._is_enabled   s]     
#G%GG#*m4\6\\4C,v&<  s   AAAA)	tool_nametool_descriptioninput_json_schemaon_invoke_handoffinput_filter
agent_namer   r   )r7   RunContextWrapper[Any]r8   
str | NonereturnRealtimeAgent[TContext])r7   rJ   rA   zAgentBase[Any]rL   bool)r>   r4   	signaturelen
parametersr   r	   json_schemar   default_tool_namedefault_tool_descriptionr   name)r!   r   r   r&   r'   r   sigrF   r<   rD   rE   rC   r/   s   `  ```      @r"   r#   r#   A   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 r$   )
r!   rM   r   rK   r   rK   r   Sbool | Callable[[RunContextWrapper[Any], RealtimeAgent[Any]], MaybeAwaitable[bool]]rL   *Handoff[TContext, RealtimeAgent[TContext]])r!   rM   r&   z!OnHandoffWithInput[THandoffInput]r'   ztype[THandoffInput]r   rK   r   rK   r   rW   rL   rX   )r!   rM   r&   r6   r   rK   r   rK   r   rW   rL   rX   )NNNNT)r!   rM   r   rK   r   rK   r&   z@OnHandoffWithInput[THandoffInput] | OnHandoffWithoutInput | Noner'   ztype[THandoffInput] | Noner   rW   rL   rX   )&
__future__r   r4   typingr   r   r   r   r   pydanticr	   typing_extensionsr
   
exceptionsr   r   handoffsr   run_contextr   r   strict_schemar   tracing.spansr   utilr   r   util._typesr   r!   r    r   r   r3   r6   r#   r    r$   r"   <module>re      s   "  ? ?   % 6  5 5 % ( (! 505}EsJK  "3C"8!93!>?  
 &*,0UY5"5 #5  *	5
S5 05 
5 
 -1%)UY	5"	5 2	5 $		5
  *	5 #	5S	5 0	5 
	5 

 -1%)UY5"5 &5  *	5
 #5S5 05 
5 &*,0SW-1UYd"d"d  *d Q	d
 +dSd 0dr$   