
    /j-                       S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKrS SKJ	r	J
r
  S SKJr  S SKJr  S SKJrJrJrJrJrJr  S SKJr  SS	KJr  SS
KJrJr  SSKJrJr  SSKJ r J!r!  SSKJ"r"  SSK#J$r$  \(       a  SSK%J&r&  \'" \RP                  " SS 5      5      r)\" S5      r*\ " S S5      5       r+\ " S S5      5       r,\ " S S5      5       r-\ " S S5      5       r.\+\,-  \--  \.-  r/ " S S\\*   5      r0 " S S5      r1 " S S 5      r2 " S! S"5      r3 " S# S$5      r4 " S% S&5      r5 " S' S(5      r6 " S) S*5      r7\(       a  \8\9\&   \8\:\	4   4   r;\Rx                  S+   " S,5      r=\S0S- j5       r>SS..S1S/ jjr?g)2    )annotationsN)Callable	Generator)contextmanager)	dataclass)TYPE_CHECKINGAnyGenericLiteralTypeVaroverload)trace   )llm)function_toolutils)trace_typestracer)	NOT_GIVEN
NotGivenOr)is_given   )SpeechHandle)AgentLIVEKIT_EVALS_VERBOSERun_Tc                  .    \ rS rSr% S\S'   SrS\S'   Srg)	ChatMessageEvent'   zllm.ChatMessageitemmessageLiteral['message']type N__name__
__module____qualname____firstlineno____annotations__r#   __static_attributes__r$       P/app/agent/.venv/lib/python3.13/site-packages/livekit/agents/voice/run_result.pyr   r   '   s    
(D
(r,   r   c                  .    \ rS rSr% S\S'   SrS\S'   Srg)	FunctionCallEvent-   zllm.FunctionCallr    function_callLiteral['function_call']r#   r$   Nr%   r$   r,   r-   r/   r/   -   s    
%4D
"4r,   r/   c                  .    \ rS rSr% S\S'   SrS\S'   Srg)	FunctionCallOutputEvent3   zllm.FunctionCallOutputr    function_call_outputLiteral['function_call_output']r#   r$   Nr%   r$   r,   r-   r4   r4   3   s    
  ,BD
)Br,   r4   c                  B    \ rS rSr% S\S'   S\S'   S\S'   SrS	\S
'   Srg)AgentHandoffEvent9   llm.AgentHandoffr    Agent | None	old_agentr   	new_agentagent_handoffLiteral['agent_handoff']r#   r$   Nr%   r$   r,   r-   r9   r9   9   s    
%4D
"4r,   r9   c                      \ rS rSrSS.SS jjr\SS j5       r\R                  SS j5       r	\SS j5       r
SS jrSS	 jr        SS
 jrSS jrSS jrSS jrSS jrSS jrSS jrSrg)	RunResultD   N)
user_inputc                   [        5       U l        [        R                  S    " 5       U l        Xl        X l        / U l        S U l        S U l	        g N)
set_handlesasyncioFuture	_done_fut_user_input_output_type_recorded_items_final_output_RunResult__last_speech_handle)selfrD   output_types      r-   __init__RunResult.__init__E   sB    :=% -/%'/1+/9=!r,   c                    U R                   $ )z5List of all recorded events generated during the run.)rN   rQ   s    r-   eventsRunResult.eventsP   s     ###r,   c                    [         (       a@  SR                  [        U R                  5      5      n[	        SU R
                   SU S35        [        U 5      $ )zy
Provides an assertion helper for verifying the run events.

Returns:
    RunAssert: Assertion interface for run events.
z
    z
+ RunResult(
   user_input=`z`
   events:
    z
))lk_evals_verbosejoin_format_eventsrW   printrL   	RunAssert)rQ   
events_strs     r-   expectRunResult.expectU   sZ     !~dkk'BCJ""&"2"2!3 4##-, / r,   c                    U R                   R                  5       (       d  [        S5      eU R                  (       d  [        S5      eU R                  $ )z
Returns the final output of the run after completion.

Raises:
    RuntimeError: If the run is not complete or no output is set.

Returns:
    Run_T: The final result output.
z3cannot retrieve final_output, RunResult is not donezno final output)rK   doneRuntimeErrorrO   rV   s    r-   final_outputRunResult.final_outputi   sD     ~~""$$TUU!!011!!!r,   c                6    U R                   R                  5       $ )z=Indicates whether the run has finished processing all events.)rK   rc   rV   s    r-   rc   RunResult.done|   s    ~~""$$r,   c                >   ^  SU 4S jjnU" 5       R                  5       $ )Nc                 d   >#    [         R                  " T R                  5      I S h  vN   T $  N7frF   )rI   shieldrK   rV   s   r-   _await_impl(RunResult.__await__.<locals>._await_impl   s&     ..000K 1s   $0.0)returnzRunResult[Run_T])	__await__)rQ   rl   s   ` r-   ro   RunResult.__await__   s    	 }&&((r,   c                   U R                   R                  5       (       a  g [        XUS9nU R                  UR                  R
                  S9nU R                  R                  XT5        g )N)r    r=   r>   
created_at)rK   rc   r9   _find_insertion_indexr    rs   rN   insert)rQ   r    r=   r>   eventindexs         r-   _agent_handoffRunResult._agent_handoff   sW     >>  !tIV**ejj6K6K*L##E1r,   c                f   U R                   R                  5       (       a  g S nUR                  S:X  a
  [        US9nO3UR                  S:X  a
  [	        US9nOUR                  S:X  a	  [        US9nUb?  U R                  UR                  R                  S9nU R                  R                  X25        g g )Nr!   )r    r1   r6   rr   )rK   rc   r#   r   r/   r4   rt   r    rs   rN   ru   )rQ   r    rv   rw   s       r-   _item_addedRunResult._item_added   s    >>  !%99	!$$/EYY/)%40EYY00+6E..%**:O:O.PE  ''5 r,   c                   U R                   R                  5       (       a  g U R                  R                  U5        [	        U[
        5      (       a  UR                  U R                  5        UR                  U R                  5        g rF   )
rK   rc   rH   add
isinstancer   _add_item_added_callbackr{   add_done_callback_mark_done_if_neededrQ   handles     r-   _watch_handleRunResult._watch_handle   s_    >>  &!fl++++D,<,<=  !:!:;r,   c                    XR                   ;  a  gU R                   R                  U5        UR                  U R                  5        [	        U[
        5      (       a  UR                  U R                  5        g)NFT)rH   discardremove_done_callbackr   r   r   _remove_item_added_callbackr{   r   s     r-   _unwatch_handleRunResult._unwatch_handle   sY    &f%##D$=$=>fl++..t/?/?@r,   c                    [        U[        5      (       a  Xl        [        S U R                   5       5      (       a  U R                  5         g g )Nc              3  @   #    U  H  oR                  5       v   M     g 7frF   )rc   ).0r   s     r-   	<genexpr>1RunResult._mark_done_if_needed.<locals>.<genexpr>   s     9={{}}=s   )r   r   rP   allrH   
_mark_doner   s     r-   r   RunResult._mark_done_if_needed   s9    fl++(.%94==999OO :r,   c                   [         R                  " [        R                  5         U R                  c%  U R
                  R                  S 5         S S S 5        g U R                  R                  n[        U[        5      (       d  U R                  (       al  [        XR                  5      (       dR  U R
                  R                  [        SU R                  R                   S[        U5      R                   35      5        O=Xl        U R
                  R                  S 5        OU R
                  R                  U5        S S S 5        g ! , (       d  f       g = f)NzExpected output of type , got )
contextlibsuppressrI   InvalidStateErrorrP   rK   
set_result_maybe_run_final_outputr   BaseExceptionrM   set_exceptionrd   r&   r#   rO   )rQ   re   s     r-   r   RunResult._mark_done   s      !:!:;((0))$/ <;
  44LLLlM::$$ZFWFW-X-XNN00$6t7H7H7Q7Q6R S##'#5#>#>"?A *6&NN--d3,,\:% <;;s   *EC%E
Ec                   [        [        [        U R                  5      5      5       H1  nU R                  U   R                  R
                  U::  d  M,  US-   s  $    g)z
Returns the index to insert an item by creation time.

Iterates in reverse, assuming items are sorted by `created_at`.
Finds the position after the last item with `created_at <=` the given timestamp.
r   r   )reversedrangelenrN   r    rs   )rQ   rs   is      r-   rt   RunResult._find_insertion_index   sP     %D$8$8 9:;A##A&++66*D1u < r,   )__last_speech_handlerK   rO   rH   rM   rN   rL   )rD   z
str | NonerR   ztype[Run_T] | Nonern   None)rn   list[RunEvent])rn   r^   )rn   r   )rn   bool)rn   z'Generator[None, None, RunResult[Run_T]])r    r;   r=   r<   r>   r   rn   r   )r    zllm.ChatItemrn   r   )r   SpeechHandle | asyncio.Taskrn   r   )r   r   rn   r   )r   z"SpeechHandle | asyncio.Task | Nonern   r   rn   r   )rs   floatrn   int)r&   r'   r(   r)   rS   propertyrW   	functoolscached_propertyr`   re   rc   ro   rx   r{   r   r   r   r   rt   r+   r$   r,   r-   rB   rB   D   s    37 	> $ $  & " "$%)2'24@2MR2	26 	<	;*r,   rB   c                  N   \ rS rSrS!S jr\S"S j5       r\S#S j5       rS$S jrS%S jrS&S'S jjr\SS	.S(S
 jj5       r	\S)S j5       r	\S*S j5       r	\S+S j5       r	\S,S j5       r	SS	.   S-S jjr	\\
S.     S.S jj5       r\\
\
S.       S/S jj5       r\\
\
S.       S0S jj5       r\\
S.     S1S jj5       r\
\
\
\
\
\
S.               S2S jjrS3S4S jjrS5S jr\
\
S.     S6S jjr\
S.   S7S jjr\
\
S.     S8S jjr\
S.   S9S jjrS rg):r^      c                4    UR                   U l        SU l        g )Nr   )rW   _events_list_current_index)rQ   
run_results     r-   rS   RunAssert.__init__   s    &--r,   c                    g rF   r$   )rQ   rw   s     r-   __getitem__RunAssert.__getitem__   s    69r,   c                    g rF   r$   )rQ   ss     r-   r   r      s    9<r,   c                   [        U[        5      (       a  U R                  U   n[        X U5      $ [        U[        5      (       a  US:  a  U[        U R                  5      -  nSUs=::  a  [        U R                  5      :  d,  O  U R                  SU S[        U R                  5       S3US9  [        U R                  U   X5      $ [        [        U 5      R                   S[        U5      R                   35      e)a  
Access a specific event or range for assertions.

Args:
    key (int | slice): Index or slice of events.

Returns:
    EventAssert: Assertion for a single event when key is int.
    EventRangeAssert: Assertion for a span of events when key is slice.

Raises:
    TypeError: If key is not an int or slice.
    AssertionError: If index is out of range.

Examples:
    # Single event access
    >>> result.expect[0].is_message(role="user")
    >>> result.expect[-1].is_message(role="assistant")

    # Full range access
    >>> result.expect[:].contains_function_call(name="foo")

    # Partial range access
    >>> result.expect[0:2].contains_message(role="assistant")
r   znth(z) out of range (total events: )rw   z# indices must be int or slice, not )r   slicer   EventRangeAssertr   r   _raise_with_debug_infoEventAssert	TypeErrorr#   r&   )rQ   keyrW   s      r-   r   r      s    4 c5!!&&s+F#F#66c3Qws4,,--5s4#4#455++3%=c$BSBS>T=UUVW ,  t005tAADz""##FtCyGYGYFZ[
 	
r,   c                    SnU R                   [        U R                  5      :  a  U R                  S5        X R                      nU$ )NTz&Expected another event, but none left.)r   r   r   r   rQ   __tracebackhide__rv   s      r-   _current_eventRunAssert._current_event  sC     #d&7&7"88''(PQ(()r,   Nc                    SnUc  U R                   OUnSR                  [        U R                  US95      n[	        U S3U-   5      e)NT
selected_indexz
Context around failure:
)r   r[   r\   r   AssertionError)rQ   r!   rw   r   marker_indexr_   s         r-   r    RunAssert._raise_with_debug_info   sM     .3mt**YY~d.?.?P\]^
y(CDzQRRr,   )r#   c                   g rF   r$   rQ   r#   s     r-   
next_eventRunAssert.next_event'  s    ?Br,   c                   g rF   r$   r   s     r-   r   r   *  s    LOr,   c                   g rF   r$   r   s     r-   r   r   -      SVr,   c                   g rF   r$   r   s     r-   r   r   0  s    `cr,   c                   g rF   r$   r   s     r-   r   r   3  r   r,   c               N   Sn U R                  5       nU =R                  S-  sl        Ub  UR                  5       R                  U:X  a  OMI  US:X  a  UR	                  5       $ US:X  a  UR                  5       $ US:X  a  UR                  5       $ US:X  a  UR                  5       $ U$ )a  
Advance to the next event, optionally filtering by type.

Args:
    type (str, optional): Event type to match.

Returns:
    EventAssert or subclass: Assertion object for the matched event.

Example:
    >>> result.expect.next_event(type="function_call").is_function_call(name="foo")
Tr   r!   r1   r6   r?   )r   r   rv   r#   
is_messageis_function_callis_function_call_outputis_agent_handoff)rQ   r#   r   	ev_asserts       r-   r   r   6  s    0 !++-I1$|y055=  9''))_$--//++4466_$--//r,   rolec                   g rF   r$   )rQ   r#   r   s      r-   skip_next_event_ifRunAssert.skip_next_event_ifb  s     $'r,   name	argumentsc                   g rF   r$   )rQ   r#   r   r   s       r-   r   r   g  s     %(r,   outputis_errorc                   g rF   r$   )rQ   r#   r   r   s       r-   r   r   p  s     +.r,   new_agent_typec                   g rF   r$   )rQ   r#   r   s      r-   r   r   y  s     %(r,   )r   r   r   r   r   r   c               x   Sn Sn	US:X  a  U R                  5       R                  US9n	OkUS:X  a  U R                  5       R                  X4S9n	OGUS:X  a  U R                  5       R                  XVS9n	O#US	:X  a  U R                  5       R	                  US
9n	U =R
                  S-  sl        U	$ ! [         a     gf = f)a  
Conditionally skip the next event if it matches criteria.

Args:
    type (str): Type of event to check.
    role (ChatRole, optional): Required role for message events.
    name (str, optional): Required function name for calls.
    arguments (dict, optional): Required args for function calls.
    output (str, optional): Required output for function call outputs.
    is_error (bool, optional): Required error flag for call outputs.
    new_agent_type (type, optional): Required agent class for handoffs.

Returns:
    EventAssert or None: The skipped event assertion if matched.

Example:
    >>> skipped = result.expect.skip_next_event_if(type="message", role="assistant")
TNr!   r   r1   r   r6   r   r?   r   r   )r   r   r   r   r   r   r   rd   )
rQ   r#   r   r   r   r   r   r   r   evs
             r-   r   r   ~  s    F !	   y ((*5545@(((*;;;[//((*BB&Bd(((*;;>;Z1$I 		s   B'B, ,
B98B9c                    Sn[        U5       HS  nU R                  [        U R                  5      :  a  U R	                  SU SU S35        U =R                  S-  sl        MU     U $ )z
Skip a specified number of upcoming events without assertions.

Args:
    count (int): Number of events to skip.

Returns:
    RunAssert: Self for chaining.

Example:
    >>> result.expect.skip_next(2)
TzTried to skip z event(s), but only z were available.r   )r   r   r   r   r   )rQ   countr   r   s       r-   	skip_nextRunAssert.skip_next  sm     !uA""c$*;*;&<<++$UG+?sBRS 1$  r,   c                    SnU R                   [        U R                  5      :  aA  U R                  U R                      nU R                  S[	        U5      R
                   35        gg)z
Assert that there are no further events.

Raises:
    AssertionError: If unexpected events remain.

Example:
    >>> result.expect.no_more_events()
Tz$Expected no more events, but found: N)r   r   r   r   r#   r&   r   s      r-   no_more_eventsRunAssert.no_more_events  sa     !T%6%6!77%%d&9&9:E''6tE{7K7K6LM 8r,   c               *    SnU SS R                  XS9$ )a1  
Assert existence of a function call event matching criteria.

Args:
    name (str, optional): Function name to match.
    arguments (dict, optional): Arguments to match.

Returns:
    FunctionCallAssert: Assertion for the matching call.

Example:
    >>> result.expect.contains_function_call(name="foo")
TNr   )contains_function_call)rQ   r   r   r   s       r-   r    RunAssert.contains_function_call  s"    & !Aw--4-MMr,   c               *    SnU SS R                  US9$ )z
Assert existence of a message event matching criteria.

Args:
    role (ChatRole, optional): Role to match.

Returns:
    ChatMessageAssert: Assertion for the matching message.

Example:
    >>> result.expect.contains_message(role="user")
TNr   )contains_messagerQ   r   r   s      r-   r   RunAssert.contains_message  s"    " !Aw''T'22r,   c               *    SnU SS R                  XS9$ )aL  
Assert existence of a function call output event matching criteria.

Args:
    output (str, optional): Output string to match.
    is_error (bool, optional): Error flag to match.

Returns:
    FunctionCallOutputAssert: Assertion for the matching output.

Example:
    >>> result.expect.contains_function_call_output(is_error=True)
TNr   )contains_function_call_outputrQ   r   r   r   s       r-   r  'RunAssert.contains_function_call_output  s"    & !Aw44F4VVr,   c               *    SnU SS R                  US9$ )a  
Assert existence of an agent handoff event matching criteria.

Args:
    new_agent_type (type, optional): Expected new agent class.

Returns:
    AgentHandoffAssert: Assertion for the matching handoff.

Example:
    >>> result.expect.contains_agent_handoff(new_agent_type=MyAgent)
TNr   )contains_agent_handoffrQ   r   r   s      r-   r   RunAssert.contains_agent_handoff$  s"     !Aw--^-LLr,   )r   r   )r   rB   )rw   r   rn   r   )r   r   rn   r   )r   z[int, slice]rn   zEventAssert | EventRangeAssert)rn   r   rF   )r!   strrw   
int | Nonern   r   )r#   r   rn   r   )r#   r"   rn   ChatMessageAssert)r#   r2   rn   FunctionCallAssert)r#   r7   rn   FunctionCallOutputAssert)r#   r@   rn   AgentHandoffAssert)r#   zSLiteral['message', 'function_call', 'function_call_output', 'agent_handoff'] | Nonern   zdEventAssert | ChatMessageAssert | FunctionCallAssert | FunctionCallOutputAssert | AgentHandoffAssert)r#   r"   r   NotGivenOr[llm.ChatRole]rn   zChatMessageAssert | None)r#   r2   r   NotGivenOr[str]r   NotGivenOr[dict[str, Any]]rn   zFunctionCallAssert | None)r#   r7   r   r  r   NotGivenOr[bool]rn   zFunctionCallOutputAssert | None)r#   r@   r   NotGivenOr[type[Agent]]rn   zAgentHandoffAssert | None)r#   zLLiteral['message', 'function_call', 'function_call_output', 'agent_handoff']r   r  r   r  r   r  r   r  r   r  r   r  rn   z]ChatMessageAssert | AgentHandoffAssert | FunctionCallAssert | FunctionCallOutputAssert | None)r   )r   r   rn   r^   r   r   r  r   r  rn   r  r   r  rn   r
  r   r  r   r  rn   r  r   r  rn   r  )r&   r'   r(   r)   rS   r   r   r   r   r   r   r   r   r   r   r   r  r  r+   r$   r,   r-   r^   r^      s     9 9< <*
XS )-B BO OV Vc cV V 	**	*X LU')'1I'	!' ' 
 !*09( '( 	(
 .( 
#( ( 
 #,%.. ..  	.
 #. 
). . [d(/(AX(	"( ( *3 )09"+%.2;:1 [:1 '	:1
 :1 .:1  :1 #:1 0:1	:1x0* !*09	N N .	N
 
N2 *33 '3 
	3. #,%.	W  W #	W
 
"W. <EM!8M	M Mr,   r^   c                      \ rS rSrSSS jjrSS jrSS jr\\S.     SS jjr\\S.     SS jjr	\S	.SS
 jjr
\S.   SS jjrSrg)r   i7  c                (    Xl         X l        X0l        g rF   _event_parent_indexrQ   rv   parentrw   s       r-   rS   EventAssert.__init__8      r,   c                N    SnU R                   R                  XR                  S9  g NTr   r  r   r  rQ   r!   r   s      r-   _raiseEventAssert._raise=  !     ++G;;+Gr,   c                    U R                   $ rF   r  rV   s    r-   rv   EventAssert.eventA      {{r,   r   c                  Sn[        U R                  [        5      (       d  U R                  S5        [        U R                  [        5      (       d   e[	        U5      (       aZ  U R                  R
                  R                  U:w  a6  U R                  SU SU R                  R
                  R                   S35        [	        U5      (       a  [        R                  " U R                  R
                  R                  5      nUR                  5        H=  u  pVXT;  d
  XE   U:w  d  M  U R                  SU SU SUR                  U5       35        M?     [        U R                  U R                  U R                  5      $ )	a  
Verify this event is a function call with matching details.

Args:
    name (str, optional): Expected function name.
    arguments (dict, optional): Expected call arguments.

Returns:
    FunctionCallAssert: Assertion for the function call.

Raises:
    AssertionError: If the event is not a function call or details mismatch.

Example:
    >>> ev_assert.is_function_call(name="foo", arguments={"x": 1})
TzExpected FunctionCallEventzExpected call name '', got ''z	For key 'z', expected r   )r   r  r/   r%  r   r    r   jsonloadsr   itemsgetr  r  r  )rQ   r   r   r   actualr   values          r-   r   EventAssert.is_function_callD  s    , !$++'899KK45$++'89999D>>dkk..33t;KK.tfHT[[=M=M=R=R<SSTUVIZZ 0 0 : :;F'oo/
$u(<KK)C5UG6&**UX/IZ [\ 0 "$++t||T[[IIr,   r   c                  Sn[        U R                  [        5      (       d  U R                  S5        [        U R                  [        5      (       d   e[	        U5      (       aZ  U R                  R
                  R                  U:w  a6  U R                  SU SU R                  R
                  R                   S35        [	        U5      (       aY  U R                  R
                  R                  U:w  a5  U R                  SU SU R                  R
                  R                   35        [        U R                  U R                  U R                  5      $ )a  
Verify this event is a function call output with matching details.

Args:
    output (str, optional): Expected output text.
    is_error (bool, optional): Expected error flag.

Returns:
    FunctionCallOutputAssert: Assertion for the output.

Raises:
    AssertionError: If the event is not function output or details mismatch.

Example:
    >>> ev_assert.is_function_call_output(output="OK", is_error=False)
Tz Expected FunctionCallOutputEventzExpected output 'r-  r.  zExpected is_error=r   )r   r  r4   r%  r   r    r   r   r  r  r  r  s       r-   r   #EventAssert.is_function_call_outputk  s    & !$++'>??KK:;$++'>????F 0 0 7 76 AKK+F88DKK<L<L<S<S;TTUVWH$++"2"2";";x"GKK,XJfT[[=M=M=V=V<WXY'T\\4;;OOr,   r   c                  Sn[        U R                  [        5      (       d  U R                  S5        [        U R                  [        5      (       d   e[	        U5      (       aZ  U R                  R
                  R                  U:w  a6  U R                  SU SU R                  R
                  R                   S35        [        U R                  U R                  U R                  5      $ )a3  
Verify this event is a message from the given role.

Args:
    role (ChatRole, optional): Expected sender role.

Returns:
    ChatMessageAssert: Assertion for the message.

Raises:
    AssertionError: If the event is not a message or role mismatch.

Example:
    >>> ev_assert.is_message(role="assistant")
TzExpected ChatMessageEventzExpected role 'r-  r.  )
r   r  r   r%  r   r    r   r
  r  r  r   s      r-   r   EventAssert.is_message  s      !$++'788KK34$++'78888D>>dkk..33t;KK/$x8H8H8M8M7NaPQ dllDKKHHr,   r   c                  Sn[        U R                  [        5      (       d  U R                  S5        [        U R                  [        5      (       d   e[	        U5      (       an  [        U R                  R
                  U5      (       dI  U R                  SUR                   S[        U R                  R
                  5      R                   S35        [        U R                  U R                  U R                  5      $ )aD  
Verify this event is an agent handoff.

Args:
    new_agent_type (type, optional): Expected new agent class.

Returns:
    AgentHandoffAssert: Assertion for the handoff.

Raises:
    AssertionError: If the event is not an agent handoff or type mismatch.

Example:
    >>> ev_assert.is_agent_handoff(new_agent_type=MyAgent)
TzExpected AgentHandoffEventzExpected new_agent 'r-  r.  )r   r  r9   r%  r   r>   r&   r#   r  r  r  r  s      r-   r   EventAssert.is_agent_handoff  s    $ !$++'899KK45$++'89999N##Jt{{7L7Ln,],]KK&~'>'>&?xT[[MbMbHcHlHlGmmno "$++t||T[[IIr,   r  r  r  N))rv   RunEventr  r^   rw   r   r!   r  rn   r   )rn   r>  r  r  r  r  )r&   r'   r(   r)   rS   r%  rv   r   r   r   r   r   r+   r$   r,   r-   r   r   7  s    
H !*09	%J %J .	%J
 
%JP ,5S\P(P@PP	!P@ >G I8 <EJ!8J	J Jr,   r   c                      \ rS rSrSS jr\\S.     SS jjr\S.   SS jjr\\S.     SS jjr\S	.   SS
 jjr	Sr
g)r   i  c                (    Xl         X l        X0l        g rF   _eventsr  _rng)rQ   rW   r  rngs       r-   rS   EventRangeAssert.__init__  s    	r,   r   c                  Sn[        U R                  5       Hn  u  pE[        XPR                  U R                  R
                  =(       d    SU-   5      n[        R                  " [        5         UR                  XS9sSSS5        s  $    U R                  R                  SU R                  < 35        [        S5      e! , (       d  f       M  = f)a  
Assert that a function call matching criteria exists in the event range.

Args:
    name (str, optional): Expected function name.
    arguments (dict, optional): Expected call arguments.

Returns:
    FunctionCallAssert: Assertion for the matched function call.

Raises:
    AssertionError: If no matching function call is found in range.

Example:
    >>> result.expect[0:3].contains_function_call(name="foo")
Tr   r   Nz8No FunctionCallEvent satisfying criteria found in range unreachable)	enumeraterC  r   r  rD  startr   r   r   r   r   rd   )rQ   r   r   r   idxr   	candidates          r-   r   'EventRangeAssert.contains_function_call  s    , ! .GC#Btyy7K!s6RSI$$^4 11t1Q 54 /
 	++FtyymT	
 =)) 54   -B>>
C	r   c                  Sn[        U R                  5       Hn  u  p4[        X@R                  U R                  R
                  =(       d    SU-   5      n[        R                  " [        5         UR                  US9sSSS5        s  $    U R                  R                  SU R                  < 35        [        S5      e! , (       d  f       M  = f)aR  
Assert that a message matching criteria exists in the event range.

Args:
    role (ChatRole, optional): Expected sender role.

Returns:
    ChatMessageAssert: Assertion for the matched message.

Raises:
    AssertionError: If no matching message is found in range.

Example:
    >>> result.expect[:2].contains_message(role="assistant")
Tr   r   Nz5No ChatMessageEvent matching criteria found in range rH  )rI  rC  r   r  rD  rJ  r   r   r   r   r   rd   )rQ   r   r   rK  r   rL  s         r-   r   !EventRangeAssert.contains_message  s    ( ! .GC#Btyy7K!s6RSI$$^4 +++6 54 /
 	++CDII=Q	
 =)) 54rN  r   c                  Sn[        U R                  5       Hn  u  pE[        XPR                  U R                  R
                  =(       d    SU-   5      n[        R                  " [        5         UR                  XS9sSSS5        s  $    U R                  R                  SU R                  < 35        [        S5      e! , (       d  f       M  = f)a  
Assert that a function call output matching criteria exists in the event range.

Args:
    output (str, optional): Expected output text.
    is_error (bool, optional): Expected error flag.

Returns:
    FunctionCallOutputAssert: Assertion for the matched output.

Raises:
    AssertionError: If no matching output is found in range.

Example:
    >>> result.expect[1:4].contains_function_call_output(is_error=True)
Tr   r   Nz<No FunctionCallOutputEvent matching criteria found in range rH  )rI  rC  r   r  rD  rJ  r   r   r   r   r   rd   )rQ   r   r   r   rK  r   rL  s          r-   r  .EventRangeAssert.contains_function_call_output  s    , ! .GC#Btyy7K!s6RSI$$^4 888Z 54 /
 	++J499-X	
 =)) 54rN  r   c                  Sn[        U R                  5       Hn  u  p4[        X@R                  U R                  R
                  =(       d    SU-   5      n[        R                  " [        5         UR                  US9sSSS5        s  $    U R                  R                  SU R                  < 35        [        S5      e! , (       d  f       M  = f)aq  
Assert that an agent handoff matching criteria exists in the event range.

Args:
    new_agent_type (type, optional): Expected new agent class.

Returns:
    AgentHandoffAssert: Assertion for the matched handoff.

Raises:
    AssertionError: If no matching handoff is found in range.

Example:
    >>> result.expect[0:3].contains_agent_handoff(new_agent_type=MyAgent)
Tr   r   Nz6No AgentHandoffEvent matching criteria found in range rH  )rI  rC  r   r  rD  rJ  r   r   r   r   r   rd   )rQ   r   r   rK  r   rL  s         r-   r  'EventRangeAssert.contains_agent_handoff0  s    $ ! .GC#Btyy7K!s6RSI$$^4 111P 54 /
 	++DTYYMR	
 =)) 54rN  rB  N)rW   r   r  r^   rE  r   r  r  r  r  )r&   r'   r(   r)   rS   r   r   r   r  r  r+   r$   r,   r-   r   r     s     !*09	 *  * .	 *
 
 *J *3* '* 
	*F #,%.	 *   * #	 *
 
" *F <E*!8*	* *r,   r   c                  f    \ rS rSrS	S jrS
S jrSS jr\R                  " S5      SS j5       r	Sr
g)r
  iO  c                (    Xl         X l        X0l        g rF   r  r  s       r-   rS   ChatMessageAssert.__init__P  r   r,   c                N    SnU R                   R                  XR                  S9  g r"  r#  r$  s      r-   r%  ChatMessageAssert._raiseU  r'  r,   c                    U R                   $ rF   r)  rV   s    r-   rv   ChatMessageAssert.eventY  r+  r,   judge_evaluationc               t  ^#    Sn[         R                  " 5       nU R                  R                  R                  nUR                  [        R                  S5        UR                  [        R                  TR                  5        UR                  [        R                  S5        UR                  [        R                  [        R                  " X%S.5      5        U(       d  U R                  S5        [        S5      eU(       d  U R                  S5        [        S5      e[         S&S j5       n["        R$                  " 5       nUR'                  S	S
S9  UR'                  SSU SU 3S9  SnSn	0 n
S/n[)        U4S jU 5       5      (       d  SU
S'   TR+                  UU/SSS0S.U
S9  Sh  vN nUR,                  b  UR,                  n	UR.                  (       d  M5  UR.                  R0                  (       d  MR  UR.                  R0                  S   nUR2                  nMy   Nt
 U(       d  U R                  S5        [5        U[6        5      (       d   e[8        R:                  " XhS9u  pU" U0 UD6I Sh  vN  u  nnUR                  [        R<                  U(       + 5        UR                  [        R>                  U5        U	(       a  URA                  [        RB                  U	RD                  [        RF                  U	RH                  [        RJ                  U	RD                  [        RL                  U	RH                  [        RN                  U	RP                  05        U(       d  U R                  SU 35        U $ [R        (       a0  SSK*J+n  U" URY                  SS5      S S!S"9n[[        S#U S$U S%35        U $ 7f)'a9  
Evaluate whether the message fulfills the given intent.

Args:
    llm_v (llm.LLM): LLM instance for judgment.
    intent (str): Description of the expected intent.

Returns:
    ChatMessageAssert: Self for chaining further assertions.

Example:
    >>> await msg_assert.judge(llm, intent="should ask for size")
Tjudger\  )intentr!   zThe chat message is empty.rH  z(Intent is required to judge the message.c                   #    X4$ 7f)z
Determines whether the message correctly fulfills the given intent.

Args:
    success: Whether the message satisfies the intent.
    reason: A concise explanation justifying the result.
r$   )successreasons     r-   check_intent-ChatMessageAssert.judge.<locals>.check_intent  s      ?"s   systeman  You are a test evaluator for conversational agents.
You will be shown a message and a target intent. Determine whether the message accomplishes the intent.
Only respond by calling the `check_intent(success: bool, reason: str)` function with your final judgment.
Be strict: if the message does not clearly fulfill the intent, return `success = False` and explain why.)r   contentuserzCCheck if the following message fulfills the given intent.

Intent:
z

Message:
Nzgpt-5c              3  @   >#    U  H  oTR                   ;   v   M     g 7frF   )model)r   excluded_modelllm_vs     r-   r   *ChatMessageAssert.judge.<locals>.<genexpr>  s     cGb^U[[0Gbs   g        temperaturefunctionr   rc  )r#   rn  )chat_ctxtoolstool_choiceextra_kwargsr   z0LLM did not return any arguments for evaluation.)fncjson_argumentszJudgement failed: )shortenr   z\n   z...)widthplaceholderz- Judgment succeeded for `z`: ``)ra  r   rb  r  rn   ztuple[bool, str]).r   get_current_spanr  r    text_contentset_attributer   ATTR_GEN_AI_OPERATION_NAMEATTR_GEN_AI_REQUEST_MODELri  ATTR_FUNCTION_TOOL_NAMEATTR_FUNCTION_TOOL_ARGSr/  dumpsr%  rd   r   r   ChatContextadd_messageanychatusagedelta
tool_callsr   r   r  	llm_utilsprepare_function_argumentsATTR_FUNCTION_TOOL_IS_ERRORATTR_FUNCTION_TOOL_OUTPUTset_attributesATTR_GEN_AI_USAGE_INPUT_TOKENSprompt_tokensATTR_GEN_AI_USAGE_OUTPUT_TOKENScompletion_tokens#ATTR_GEN_AI_USAGE_INPUT_TEXT_TOKENS$ATTR_GEN_AI_USAGE_OUTPUT_TEXT_TOKENS%ATTR_GEN_AI_USAGE_INPUT_CACHED_TOKENSprompt_cached_tokensrZ   textwrapru  replacer]   )rQ   rk  r_  r   current_spanmsg_contentrc  ro  r   r  rr  excluded_models_temperaturechunktoolfnc_args
fnc_kwargsra  rb  ru  	print_msgs    `                  r-   r^  ChatMessageAssert.judge\  sT     !--/kk&&33"";#I#I7S"";#H#H%++V"";#F#FHZ[""//JJ&AB	

 KK45}--KKBC}--		# 
	# ??${ 	 	
 	"8 $(M+ 	 	
 !%	,0'.i#cGbccc*-L' !::.!+&.9QR%	 & 
 	+% {{&;;{{%%%{{--a0 NN		+ 
  KKJK)S))))(CC 
 !-h E* EEE"";#J#JPWKX"";#H#H&Q''>>@S@S??AXAXCCUEXEXDDeF]F]EEuGaGa KK,VH56  ( 3 3D% @X]^I.ykfXQGHs?   F'N8*H%.H#/H%2AN8<'N8#H%%AN86I97EN8r<  N)rv   r   r  r^   rw   r   r?  )rn   r   )rk  zllm.LLMr_  r  rn   r
  )r&   r'   r(   r)   rS   r%  rv   r   start_as_current_spanr^  r+   r$   r,   r-   r
  r
  O  s4    
H !!"45| 6|r,   r
  c                  (    \ rS rSrSS jrSS jrSrg)r  i  c                (    Xl         X l        X0l        g rF   r  r  s       r-   rS   FunctionCallAssert.__init__  r   r,   c                    U R                   $ rF   r)  rV   s    r-   rv   FunctionCallAssert.event  r+  r,   r<  N)rv   r/   r  r^   rw   r   )rn   r/   r&   r'   r(   r)   rS   rv   r+   r$   r,   r-   r  r        
r,   r  c                  (    \ rS rSrSS jrSS jrSrg)r  i  c                (    Xl         X l        X0l        g rF   r  r  s       r-   rS   !FunctionCallOutputAssert.__init__  r   r,   c                    U R                   $ rF   r)  rV   s    r-   rv   FunctionCallOutputAssert.event  r+  r,   r<  N)rv   r4   r  r^   rw   r   )rn   r4   r  r$   r,   r-   r  r    r  r,   r  c                  (    \ rS rSrSS jrSS jrSrg)r  i  c                (    Xl         X l        X0l        g rF   r  r  s       r-   rS   AgentHandoffAssert.__init__  r   r,   c                    U R                   $ rF   r)  rV   s    r-   rv   AgentHandoffAssert.event  r+  r,   r<  N)rv   r9   r  r^   rw   r   )rn   r9   r  r$   r,   r-   r  r    r  r,   r  	MockToolsagents_mock_toolsc              #     #    [         R                  0 5      n0 UEX0En[         R                  U5      n Sv   [         R                  U5        g! [         R                  U5        f = f7f)z
Temporarily assign a set of mock tool callables to a specific Agent type within the current context.

Usage:
    with mock_tools(MyAgentClass, {"tool_name": mock_fn}):
        # inside this block, MyAgentClass will see the given mocks
N)_MockToolsContextVarr2  rG   reset)agentmockscurrentupdatedtokens        r-   
mock_toolsr     s[      #&&r*G''%'G $$W-E*""5)""5)s   2A)A A)A&&A)r   c          	        / n[        U 5       H  u  p4SnUb	  X1:X  a  SOSn[        U[        [        [        45      (       a@  UR
                  R                  SS1 SkS9nU SU SUR                  R                   S	U S
3nOB[        U[        5      (       a#  U SU SUR                   SUR                   S
3nO
U SU SU 3nUR                  U5        M     U$ )N z>>>z   T>   idr#   call_idrs   )exclude_noneexclude_defaultsexcludez [z] z(item=r   z] AgentHandoffEvent(old_agent=z, new_agent=)rI  r   r   r/   r4   r    
model_dump	__class__r&   r9   r=   r>   append)rW   r   linesr   rv   prefix	item_reprlines           r-   r\   r\     s    Ef%%1UuFe.0ACZ[\\

--!!%? . I
 XRs"U__%=%=$>fYKqQD011("QC  "__-\%//9J!M 
 XRs"UG,DT) &, Lr,   )r  ztype[Agent]r  zdict[str, Callable]rn   zGenerator[None, None, None])rW   r   r   r	  rn   z	list[str])@
__future__r   rI   r   contextvarsr   r/  oscollections.abcr   r   r   dataclassesr   typingr   r	   r
   r   r   r   opentelemetryr   r  r   r   r   r  	telemetryr   r   typesr   r   r   speech_handler   r  r   r   getenvrZ   r   r   r/   r4   r9   r>  rB   r^   r   r   r
  r  r  r  dictr#   r  r  
ContextVarr  r  r\   r$   r,   r-   <module>r     s   "      	 / % !     3 + )  ' ryy!8!<=  ) ) )
 5 5 5
 C C C
 5 5 5 //2IIL]]Z ZzSM SMl
LJ LJ^F* F*RJ JZ    T%[$sH}"556I"--k:;NO  * *" LP  r,   