
    Cj/                       S SK Jr  S SKrS SKrS SKrS SKrS SKJr  S SKJ	r	J
r
Jr  S SK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  SS
KJr  SSKJrJrJrJr  SSKJ r J!r!  SSK"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,  SSK-J.r.J/r/J0r0  SSK1J2r2  SSK3J4r4  SSK5J6r6J7r7  SSK8J9r9  SSK:J;r;  SSK<J=r=J>r>  \(       a  S SKJ?r?J@r@  SSKAJBrB  \" S5      rC\
" SS9 " S S5      5       rDSSSS .                   S+S" jjrE\S#   rF        S,S$ jrGS-S% jrH\
 " S& S!\R                  5      5       rJ\
 " S' S(\J5      5       rK\
 " S) S*\J5      5       rLg).    )annotationsN)AsyncIterator)InitVar	dataclassfield)TYPE_CHECKINGAnyLiteralTypeVarcast)GetCoreSchemaHandler)core_schema   )Agent)AgentOutputSchemaBase)AgentsExceptionInputGuardrailTripwireTriggeredMaxTurnsExceededRunErrorDetails)InputGuardrailResultOutputGuardrailResult)ItemHelpersModelResponseRunItemToolApprovalItemTResponseInputItem)logger)RunContextWrapper)run_items_to_input_items)NextStepInterruptionProcessedResponseQueueCompleteSentinel)RunState)StreamEvent)ToolInputGuardrailResultToolOutputGuardrailResult)Trace)
TraceState)pretty_print_result!pretty_print_run_result_streaming)	AwaitableCallable)BaseSandboxSessionTT)frozenc                  <    \ rS rSr% SrS\S'    S\S'    S\S'   Srg)	AgentToolInvocation8   z8Immutable metadata about a nested agent-tool invocation.str	tool_nametool_call_idtool_arguments N)__name__
__module____qualname____firstlineno____doc____annotations____static_attributes__r7       _/home/rurouni/.local/share/pipx/venvs/strix-agent/lib/python3.13/site-packages/agents/result.pyr1   r1   8   s    BN45;r?   r1   F)conversation_idprevious_response_idauto_previous_response_idRunResultBasec               4   UR                   U l        [        USS5      n	[        U	[        5      (       a  [	        U	5      U l        OUR                  U l        [	        UR                  5      U l        UR                  U l	        UR                  U l        UR                  U l        UR                  U l        UR                   U l        X0l        X l        X@l        U R+                  U5        X`l        Xpl        Xl        [        USS5      n
[        U
[2        5      (       a  U
R4                  U l        O[        USS5      U l        [        USS5      U l        [	        [        US/ 5      5      nU(       a  [9        US9U l        [        USS5      nUc!  [<        R>                  " [        US	S5      5      nU(       a  [@        RB                  " U5      OSU l"        [        US
S5      n[        U[F        5      (       a  [@        RB                  " U5      U l$        U $ SU l$        U $ )z8Populate a RunState with common fields from a RunResult._model_input_itemsN_state_generated_prompt_cache_key_reasoning_item_id_policyinterruptions)rJ   _trace_statetrace_sandbox_resume_state)%
last_agent_current_agentgetattr
isinstancelist_generated_items	new_items_session_itemsraw_responses_model_responsesinput_guardrail_results_input_guardrail_resultsoutput_guardrail_results_output_guardrail_resultstool_input_guardrail_results_tool_input_guardrail_resultstool_output_guardrail_results_tool_output_guardrail_results_last_processed_response_current_turn"_current_turn_persisted_item_countset_tool_use_tracker_snapshot_conversation_id_previous_response_id_auto_previous_response_idr#   rH   rI   r    _current_stepr(   
from_tracecopydeepcopyrK   dict_sandbox)stateresultcurrent_turnlast_processed_response!current_turn_persisted_item_counttool_use_tracker_snapshotrA   rB   rC   model_input_itemssource_staterJ   trace_statesandbox_resume_states                 r@   _populate_state_from_resultrw   F   s    ",,E(<dC#T**!%&7!8!'!1!1 0 01E#11E%+%C%CE"&,&E&EE#*0*M*ME'+1+O+OE(%<"&/P,	''(AB,"6'@$68T2L,)),8,T,T),3F<Y[_,`)&-f6QSW&XE#"=>M2O&.$7K ++GFGT,JK7B{3E"6+BDI&--';< L Lr?   )preserve_all
normalizedc                   [        U R                  U5      nUS:X  a  U$ US:w  a  [        SU 35      e[        U SS5      (       d  U$ [        U SS5      n[	        U[
        5      (       d  U$ [        XB5      $ )a  Return input items for the requested result view.

``preserve_all`` keeps the full converted history from ``new_items``. ``normalized`` returns
the canonical continuation input when handoff filtering rewrote model history, otherwise it
falls back to the same converted history.
rx   ry   z Unsupported to_input_list mode: _replay_from_model_input_itemsFrF   N)r   rT   
ValueErrorrP   rQ   rR   )rn   modereasoning_item_id_policysession_itemsrs   s        r@   _input_items_for_resultr      s     -V-=-=?WXM~|;D6BCC6;UCC (<dC'.. $$5PPr?   c                    [        U SS5      n[        USS5      n[        U[        5      (       a  U$ [        U SS5      n[        U[        5      (       a  U$ U R                  $ )zJReturn the root agent graph that should seed RunState identity resolution.rG   N_starting_agent_starting_agent_for_state)rP   rQ   r   rN   )rn   rm   starting_agentstored_starting_agents       r@   r   r      sb    FHd+EU$5t<N.%((#F,GN'//$$r?   c                     \ rS rSr% S\S'    S\S'    S\S'    S\S	'    S
\S'    S\S'    S\S'    S\S'    S\S'    \" SSSS9rS\S'    \" SSSS9rS\S'    \" SSSS9rS\S'    \" SSSS9r	S\S'    \" SSSS9r
S\S '    \" SSSS9rS!\S"'    \      S1S# j5       r\\R                   S2S$ j5       5       rS%S&.S3S' jjrS4S( jr\R                   S4S) j5       rS5S6S* jjrS+S,.   S7S- jjr\S8S. j5       r\S9S/ j5       rS0rg):rD      zstr | list[TResponseInputItem]inputlist[RunItem]rT   zlist[ModelResponse]rV   r	   final_outputzlist[InputGuardrailResult]rX   zlist[OutputGuardrailResult]rZ   zlist[ToolInputGuardrailResult]r\   zlist[ToolOutputGuardrailResult]r^   zRunContextWrapper[Any]context_wrapperNFdefaultinitreprzTraceState | NonerK   boolr{   zdict[str, object] | NonerM   zBaseSandboxSession | None_sandbox_sessionAgent[Any] | Noner   
str | NonerH   c                .    [         R                  " U 5      $ N)r   is_instance_schema)cls_source_type_handlers      r@   __get_pydantic_core_schema__*RunResultBase.__get_pydantic_core_schema__   s     --c22r?   c                    g)The last agent that was run.Nr7   selfs    r@   rN   RunResultBase.last_agent       r?   Trelease_new_itemsc                   U(       a9  U R                    H)  n[        USS5      n[        U5      (       d  M"  U" 5         M+     U R                  5         g)a*  
Release strong references to agents held by this result. After calling this method,
accessing `item.agent` or `last_agent` may return `None` if the agent has been garbage
collected. Callers can use this when they are done inspecting the result and want to
eagerly drop any associated agent graph.
release_agentN)rT   rP   callable_release_last_agent_reference)r   r   itemreleases       r@   release_agentsRunResultBase.release_agents   sA     !$>G$$I ' 	**,r?   c                D     U R                  SS9  g ! [         a     g f = f)NFr   )r   	Exceptionr   s    r@   __del__RunResultBase.__del__   s,    	
 %8 		s    
c                    g)zDRelease stored agent reference specific to the concrete result type.Nr7   r   s    r@   r   +RunResultBase._release_last_agent_reference  r   r?   c                    U(       a3  [        U R                  U5      (       d  [        SUR                   35      e[	        [
        U R                  5      $ )a  A convenience method to cast the final output to a specific type. By default, the cast
is only for the typechecker. If you set `raise_if_incorrect_type` to True, we'll raise a
TypeError if the final output is not of the given type.

Args:
    cls: The type to cast the final output to.
    raise_if_incorrect_type: If True, we'll raise a TypeError if the final output is not of
        the given type.

Returns:
    The final output casted to the given type.
zFinal output is not of type )rQ   r   	TypeErrorr8   r   r.   )r   r   raise_if_incorrect_types      r@   final_output_asRunResultBase.final_output_as  sC     #:d6G6G+M+M:3<<.IJJAt(())r?   rx   )r}   c               z    [         R                  " U R                  5      n[        U SS5      n[	        U UUS9nX$-   $ )a4  Create an input-item view of this run.

``mode="preserve_all"`` keeps the historical behavior of converting ``new_items`` into a
full plain-item history. ``mode="normalized"`` prefers the canonical continuation input
when handoff filtering rewrote model history, while remaining identical for ordinary runs.
rI   N)r}   r~   )r   input_to_new_input_listr   rP   r   )r   r}   original_itemsr~   replay_itemss        r@   to_input_listRunResultBase.to_input_list  sI     4?3V3VW[WaWa3b#*41Ld#S .%=

 ,,r?   c                    SSK Jn  [        U R                  U5      (       d  g[	        U R                  R
                  U R                  R                  U R                  R                  S9$ )zkImmutable metadata for results produced by `Agent.as_tool()`.

Returns `None` for ordinary top-level runs.
r   )ToolContextN)r4   r5   r6   )tool_contextr   rQ   r   r1   r4   r5   r6   )r   r   s     r@   agent_tool_invocation#RunResultBase.agent_tool_invocation2  sW     	.$..<<"**44--:://>>
 	
r?   c                X    U R                   (       d  gU R                   S   R                  $ )zEConvenience method to get the response ID of the last model response.N)rV   response_idr   s    r@   last_response_idRunResultBase.last_response_idC  s'     !!!!"%111r?   r7   )r   r	   r   r   returnzcore_schema.CoreSchemar   
Agent[Any])r   r   r   Noner   r   )F)r   ztype[T]r   r   r   r.   )r}   ToInputListModer   list[TResponseInputItem])r   zAgentToolInvocation | None)r   r   )r8   r9   r:   r;   r=   r   rK   r{   rM   r   r   rH   classmethodr   propertyabcabstractmethodrN   r   r   r   r   r   r   r   r>   r7   r?   r@   rD   rD      s   ))  '&L'77399>"@@N#BBO++0&+Du5&QL#Q<+0UQV+W"DW
 7<Du[`6a3aC275W\2]/]]38EX]3^0^N.3DuSX.YYA33 '3 
 	3 3 +  + ;? -	 	S S** !/- - 
"	-( 
 
  2 2r?   c                     \ rS rSr% S\S'   \" SSSS9rS\S'   \" SSS	9rS
\S'    \" \SS9r	S\S'   Sr
S\S'    SrS\S'    \" \SS9rS\S'    \" SSS	9rS\S'    \" SSS	9rS\S'    \" SSS	9rS\S'    \" SSS	9rS\S'    \" SSSS9rS\S'    SrS\S '    \" \S!9rS"\S#'    S*S$ jr\S+S% j5       rS*S& jrS,S' jrS-S( jrS)rg).	RunResultiL  r   _last_agentFNr   r   r   (weakref.ReferenceType[Agent[Any]] | None_last_agent_refr   r   ProcessedResponse | Noner`   default_factoryr   dict[str, list[str]]_tool_use_tracker_snapshotr   intrb   ra   r   rF   %str | list[TResponseInputItem] | None_original_inputr   rd   re   r   rf   r   "Literal['preserve', 'omit'] | NonerI   
   	max_turnsr   list[ToolApprovalItem]rJ   c                N    [         R                  " U R                  5      U l        g r   )weakrefrefr   r   r   s    r@   __post_init__RunResult.__post_init__q  s    &{{4+;+;<r?   c                    [        SU R                  R                  S5      5      nUb  U$ U R                  (       a  U R                  5       nUb  U$ [	        S5      e)r   r   r   ,Last agent reference is no longer available.)r   __dict__getr   r   r   agents     r@   rN   RunResult.last_agentt  sY     ($--*;*;M*JKL((*E LMMr?   c                    [        SU R                  R                  S5      5      nUc  g [        R                  " U5      U l        S U R                  S'   g )Nr   r   )r   r   r   r   r   r   r   s     r@   r   'RunResult._release_last_agent_reference  sE    ($--*;*;M*JK=&{{51'+m$r?   c                :   [        U SS5      n[        U R                  Ub  UOU R                  [	        U 5      U R
                  S9n[        UU U R                  U R                  U R                  U R                  U R                  U R                  U R                  S9	$ )a  Create a RunState from this result to resume execution.

This is useful when the run was interrupted (e.g., for tool approval). You can
approve or reject the tool calls on the returned state, then pass it back to
`Runner.run()` to continue execution.

Returns:
    A RunState that can be used to resume the run.

Example:
    ```python
    # Run agent until it needs approval
    result = await Runner.run(agent, "Use the delete_file tool")

    if result.interruptions:
        # Approve the tool call
        state = result.to_state()
        state.approve(result.interruptions[0])

        # Resume the run
        result = await Runner.run(agent, state)
    ```
r   Ncontextoriginal_inputr   r   ro   rp   rq   rr   rA   rB   rC   )rP   r#   r   r   r   r   rw   ra   r`   rb   r   rd   re   rf   )r   original_input_for_staterm   s      r@   to_stateRunResult.to_state  s    2 $+41BD#I (('3 44T:nn
 +++$($A$A.2.U.U&*&E&E 11!%!;!;&*&E&E

 
	
r?   c                    [        U 5      $ r   )r)   r   s    r@   __str__RunResult.__str__  s    "4((r?   )r   r   r   r   RunState[Any]r   r3   )r8   r9   r:   r;   r=   r   r   r`   rk   r   rb   ra   rR   rF   r   rd   re   rf   rI   r   rJ   r   r   rN   r   r   r   r>   r7   r?   r@   r   r   L  sI   @EAO= 
 :?tRW9X6X\7<TX]7^ 4^./&/M3Q(-d(OOF=B4V[=\O:\E $)E#BjB:(-d(G:GG',U'GGCDI5uEA  TIs;,1$,GM)GF= 	N 	N,-
^)r?   r   c                     \ rS rSr% SrS\S'    S\S'    S\S'    S\S	'    \" S
S9rS\S'   \" S
S9rS\S'   S
r	S\S'    \" S
S
SS9r
S\S'   \" \S
S9rS\S'    \" \R                  S
S9rS\S'   \" \R                  S
S9rS\S'   \" SS
S9rS\S'   \" SS
S9rS\S '   \" SS
S9rS!\S"'   \" SS
S9rS\S#'   \" SS
S9rS$\S%'   \" S&S
S9rS'\S('   \" SS
S9rS)\S*'    \" \S+9rS,\S-'    \" S
S
S9rS\S.'   S/rS\S0'    S
rS\S1'    SrS2\S3'    \" S
S
S9rS\S4'   \" SS
S9rS5\S6'    \" \S
S9r S7\S8'   \" SS
S9r!S\S9'    \" SS
S9r"S:\S;'    \" SS
S9r#S:\S<'    \" S
S
S9r$S\S='    \" SS
S
S>9r%S?\S@'    Sr&SA\SB'   \" SS
S
S>9r'SC\SD'   \" SS
S
S>9r(SE\SF'   \" S
S
S
S>9r)S\SG'   SYSH jr*\+SZSI j5       r,S[SJ jr-S[SK jr.S[SL jr/\+S\SM j5       r0S]S^SN jjr1S_SO jr2S`SP jr3SQ r4SR r5SaSS jr6SbST jr7S[SU jr8S[SV jr9ScSW jr:SXr;g)dRunResultStreamingi  a6  The result of an agent run in streaming mode. You can use the `stream_events` method to
receive semantic events as they are generated.

The streaming method will raise:
- A MaxTurnsExceeded exception if the agent exceeds the max_turns limit.
- A GuardrailTripwireTriggered exception if a guardrail is tripped.
r   current_agentr   ro   r   r	   r   F)r   zAgentOutputSchemaBase | None_current_agent_output_schemazTrace | NonerL   r   is_completeNr   r   _current_agent_refr   r   rF   z2asyncio.Queue[StreamEvent | QueueCompleteSentinel]_event_queuez#asyncio.Queue[InputGuardrailResult]_input_guardrail_queuer   asyncio.Task[Any] | Nonerun_loop_task_input_guardrails_taskzInputGuardrailResult | None!_triggered_input_guardrail_result_output_guardrails_taskzException | None_stored_exceptionnonez*Literal['none', 'immediate', 'after_turn']_cancel_moder   r`   r   r   rJ   _waiting_on_event_queuer   rb   _stream_input_persistedzlist[TResponseInputItem] | None_original_input_for_persistence_max_turns_handledr   r   r   r   rG   r   rd   re   rf   r   r   rI   z!InitVar[asyncio.Task[Any] | None]_run_impl_taskz$Callable[[], Awaitable[None]] | None_sandbox_cleanupzasyncio.Task[None] | None_sandbox_cleanup_task$_sandbox_cleanup_callback_registeredc                    [         R                  " U R                  5      U l        U R                  c  U R
                  U l        U R                  c  Ub  Xl        g g g r   )r   r   r  r  r   r   r  )r   r  s     r@   r    RunResultStreaming.__post_init__  sQ    ")++d.@.@"A'#'::D %.*D!/ +E%r?   c                    [        SU R                  R                  S5      5      nUb  U$ U R                  (       a  U R                  5       nUb  U$ [	        S5      e)zThe last agent that was run. Updates as the agent run progresses, so the true last agent
is only available after the agent run is complete.
r   r  r   )r   r   r   r  r   r   s     r@   rN   RunResultStreaming.last_agent%  sY    
 ($--*;*;O*LML""++-E LMMr?   c                    [        SU R                  R                  S5      5      nUc  g [        R                  " U5      U l        S U R                  S'   g )Nr   r  )r   r   r   r   r   r  r   s     r@   r   0RunResultStreaming._release_last_agent_reference3  sE    ($--*;*;O*LM=")++e"4)-o&r?   c                   ^#    U R                   mTc  g U R                  nUc)  SU4S jjn[        R                  " U" 5       5      nXl        UI S h  vN   g  N7f)Nc                    >#     T" 5       I S h  vN   g  N! [          a!  n [        R                  " SU 5         S n A g S n A ff = f7f)Nz;Failed to clean up sandbox resources after streamed run: %s)r   r   warning)errorsandbox_cleanups    r@   _cleanup_once>RunResultStreaming._run_sandbox_cleanup.<locals>._cleanup_onceC  s=     )+++  NNUW\ s3   A
  A 
A=AAAr   )r  r  asynciocreate_task)r   taskr%  r$  s      @r@   _run_sandbox_cleanup'RunResultStreaming._run_sandbox_cleanup;  sP     //"))< &&}7D)-&

s   AAAAc                  ^ ^ T R                   b  T R                  b  T R                  (       a  g T R                  mST l        TR                  U 4S j5        SUU 4S jjn[        R
                  " U" 5       5      T l        g )NTc                L   > [         R                  " TR                  5       5      $ r   )r'  r(  r*  )_taskr   s    r@   <lambda>IRunResultStreaming.ensure_sandbox_cleanup_on_completion.<locals>.<lambda>[  s    '--d.G.G.IJr?   c                    >#     TI S h  vN n TR                  5       I S h  vN   U $  N! [         R                   a'    TR                  5       (       d  TR                  5         e [         a    TR                  5       I S h  vN    e f = f Ni7fr   )r'  CancelledErrordonecancelr   r*  )rn   original_taskr   s    r@   _await_run_and_cleanupWRunResultStreaming.ensure_sandbox_cleanup_on_completion.<locals>._await_run_and_cleanup^  s     ,, ++---M -)) $))++!((* //111 .s<   B+ )+ BBB+ AB	BB		B)r   r	   )r  r  r  add_done_callbackr'  r(  )r   r6  r5  s   ` @r@   $ensure_sandbox_cleanup_on_completion7RunResultStreaming.ensure_sandbox_cleanup_on_completionP  sn    !!)!!)88**481''J	
	 	 %001G1IJr?   c                    U R                   nUb*  UR                  5       (       a  UR                  5       (       a  gUR                  5       $ )a}  The exception raised by the background run loop, if any.

When the run loop fails before producing stream events (for example during early
sandbox initialisation), the exception may not be re-raised through
:meth:`stream_events`. This property gives callers a reliable way to check for
silent failures after consuming the stream:

.. code-block:: python

    result = Runner.run_streamed(agent, "hello")
    async for event in result.stream_events():
        pass
    if result.run_loop_exception:
        raise result.run_loop_exception

Returns ``None`` if the run loop completed without error, has not yet finished,
or was cancelled.
N)r  r3  	cancelled	exceptionr   r)  s     r@   run_loop_exception%RunResultStreaming.run_loop_exceptionn  s:    ( !!<tyy{{dnn.>.>~~r?   c                   Xl         US:X  a  U R                  5         SU l        U R                  R	                  5       (       d;  U R                  R                  5         U R                  R	                  5       (       d  M;  U R                  R                  [        5       5        U R                  (       d  U R                  5         ggUS:X  a  gg)aK  Cancel the streaming run.

Args:
    mode: Cancellation strategy:
        - "immediate": Stop immediately, cancel all tasks, clear queues (default)
        - "after_turn": Complete current turn gracefully before stopping
            * Allows LLM response to finish
            * Executes pending tool calls
            * Saves session state properly
            * Tracks usage accurately
            * Stops before next turn begins

Example:
    ```python
    result = Runner.run_streamed(agent, "Task", session=session)

    async for event in result.stream_events():
        if user_interrupted():
            result.cancel(mode="after_turn")  # Graceful
            # result.cancel()  # Immediate (default)
    ```

Note: After calling cancel(), you should continue consuming stream_events()
to allow the cancellation to complete properly.
	immediateT
after_turnN)r  _cleanup_tasksr  r	  empty
get_nowaitr  
put_nowaitr"   r  _drain_event_queue)r   r}   s     r@   r4  RunResultStreaming.cancel  s    6 !;!#D117799++668 117799 (()>)@A//'') 0 \! 	 "r?   c                 #    Sn  U R                  5         [        U R                  [        5      nU R                  (       aD  U(       a  U R                  R                  5       (       a  [        R                  " S5        SU l        OU R                  (       a   U R                  R                  5       (       a  O SU l	        U R                  R                  5       I Sh  vN n SU l	        [        U[        5      (       aN  U R                  U R                   5      I Sh  vN   U R                  R#                  5         U R                  5         O"U7v   U R                  R#                  5         GMg   U(       a  U R%                  5         OCU R                  U R&                  5      I Sh  vN   U R                  5         U R%                  5         U(       d  U R)                  5       I Sh  vN   [        R*                  " S5      I Sh  vN   U R-                  5         U R/                  5         U R                  (       a  U R                  eg GNi! [        R                   a    SnU R                  5         e f = f! SU l	        f = f GNc N N N! [        R*                  " S5      I Sh  vN    U R-                  5         U R/                  5         f = f!  U(       a  U R%                  5         ODU R                  U R&                  5      I Sh  vN    U R                  5         U R%                  5         U(       d  U R)                  5       I Sh  vN    [        R*                  " S5      I Sh  vN    U R-                  5         U R/                  5         f ! [        R*                  " S5      I Sh  vN    U R-                  5         U R/                  5         f = f= f7f)a  Stream deltas for new items as they are generated. We're using the types from the
OpenAI Responses API, so these are semantic events: each event has a `type` field that
describes the type of the event, along with the data for that event.

This will raise:
- A MaxTurnsExceeded exception if the agent exceeds the max_turns limit.
- A GuardrailTripwireTriggered exception if a guardrail is tripped.
FTz Breaking due to stored exceptionNr   )_check_errorsrQ   r  r   r  rE  r   debugr  r  r   r'  r2  r4  r"   _await_task_safelyr  	task_donerD  r  r*  sleeprH  _drain_input_guardrail_queue)r   r<  should_drain_queued_eventsr   s       r@   stream_events RunResultStreaming.stream_events  s     	A	4""$-78N8NP`-a*))2d6G6G6M6M6O6OLL!CD'+D$##(9(9(?(?(A(A937D0!%!2!2!6!6!88D 49D0d$9:: 11$2M2MNNN%%//1 &&(
!!++-I L4 '') 11$2D2DEEE
 &&('') 33555 mmA&&& '')113!!((( "g 9--  $IKKM
 49D0
 O( F 6 'gmmA&&& '')11354 '') 11$2D2DEEE
 &&('') 33555 mmA&&& '')113	 mmA&&& '')113s   OB1K 8$I II ":K I?AK /6J %J&>J $J%J )OJAOI (I00I3 3	I<<K J J OK	"J%#&K		OO6N
L?N
MN

O#M&$&O
O$N'%&OOOc           
         [        U R                  U R                  U R                  U R                  U R
                  U R                  U R                  S9$ )zRReturn a `RunErrorDetails` object considering the current attributes of the class.)r   rT   rV   rN   r   rX   rZ   )r   r   rT   rV   r  r   rX   rZ   r   s    r@   _create_error_details(RunResultStreaming._create_error_details  sJ    **nn,,)) 00$($@$@%)%B%B
 	
r?   c                   U R                   U R                  :  aE  U R                  (       d4  [        SU R                   S35      nU R	                  5       Ul        Xl        U R                  R                  5       (       d|  U R                  R                  5       nUR                  R                  (       a&  [        U5      nU R	                  5       Ul        X0l        U R                  R                  5       (       d  M|  U R                  (       a  U R                  R                  5       (       a  U R                  R                  5       (       ds  U R                  R!                  5       nU(       aR  [#        U[$        5      (       a=  [#        U[&        5      (       a"  UR
                  c  U R	                  5       Ul        X@l        U R(                  (       a  U R(                  R                  5       (       a  U R(                  R                  5       (       ds  U R(                  R!                  5       nU(       aR  [#        U[$        5      (       a=  [#        U[&        5      (       a"  UR
                  c  U R	                  5       Ul        XPl        U R*                  (       a  U R*                  R                  5       (       a  U R*                  R                  5       (       dv  U R*                  R!                  5       nU(       aT  [#        U[$        5      (       a>  [#        U[&        5      (       a"  UR
                  c  U R	                  5       Ul        X`l        g g g g g g )NzMax turns (z
) exceeded)ro   r   r  r   rU  run_datar  r	  rE  rF  outputtripwire_triggeredr   r  r3  r<  r=  rQ   r   r   r  r  )r   max_turns_excguardrail_resulttripwire_excrun_impl_excin_guard_excout_guard_excs          r@   rK   RunResultStreaming._check_errors  sd   t~~-d6M6M,{4>>:J*-UVM%)%?%?%AM"%2" --3355#::EEG&&99>?OP(,(B(B(D%)5& --3355 $"4"4"9"9";";%%//11#11;;=J|Y$G$G!,@@\EZEZEb040J0J0L--9*&&4+F+F+K+K+M+M..88::#::DDFJ|Y$G$G!,@@\EZEZEb040J0J0L--9*''D,H,H,M,M,O,O//99;; $ < < F F H Zy%I%I"=/BB)22:151K1K1M.-:* &J= < -P'r?   c                   U R                   (       a9  U R                   R                  5       (       d  U R                   R                  5         U R                  (       a9  U R                  R                  5       (       d  U R                  R                  5         U R                  (       a;  U R                  R                  5       (       d  U R                  R                  5         g g g r   )r  r3  r4  r  r  r   s    r@   rD  !RunResultStreaming._cleanup_tasks=  s    d&8&8&=&=&?&?%%'&&t/J/J/O/O/Q/Q''..0''0L0L0Q0Q0S0S((//1 1T'r?   c                    [        U 5      $ r   )r*   r   s    r@   r   RunResultStreaming.__str__G  s    066r?   c                   #    U(       a"  UR                  5       (       d   UI Sh  vN   ggg N! [        R                   a     g[         a     gf = f7f)zAwait a task if present, ignoring cancellation and storing exceptions elsewhere.

This ensures we do not lose late guardrail exceptions while not surfacing
CancelledError to callers of stream_events.
N)r3  r'  r2  r   r>  s     r@   rM  %RunResultStreaming._await_task_safelyJ  sL      		

 $4))  s=   A/ -/ A/ AA	AAAAc                :   U R                   R                  5       (       dW   U R                   R                  5         U R                   R                  5         U R                   R                  5       (       d  MV  gg! [        R
                   a     g[         a     gf = f)zARemove any pending items from the event queue and mark them done.N)r  rE  rF  rN  r'  
QueueEmptyr|   r   s    r@   rH  %RunResultStreaming._drain_event_queueZ  s|    ##))++!!,,.!!++- ##))++ %%  s   4A8 8B	BBc                    U R                   R                  5       (       d=   U R                   R                  5         U R                   R                  5       (       d  M<  gg! [        R                   a     gf = f)z8Remove any pending items from the input guardrail queue.N)r	  rE  rF  r'  ri  r   s    r@   rP  /RunResultStreaming._drain_input_guardrail_queuef  s_    --3355++668 --3355 %% s   A A54A5c                H   [        U R                  U R                  b  U R                  OU R                  [	        U 5      U R
                  S9n[        UU U R                  U R                  U R                  U R                  U R                  U R                  U R                  S9	$ )a3  Create a RunState from this streaming result to resume execution.

This is useful when the run was interrupted (e.g., for tool approval). You can
approve or reject the tool calls on the returned state, then pass it back to
`Runner.run_streamed()` to continue execution.

Returns:
    A RunState that can be used to resume the run.

Example:
    ```python
    # Run agent until it needs approval
    result = Runner.run_streamed(agent, "Use the delete_file tool")
    async for event in result.stream_events():
        pass

    if result.interruptions:
        # Approve the tool call
        state = result.to_state()
        state.approve(result.interruptions[0])

        # Resume the run
        result = Runner.run_streamed(agent, state)
        async for event in result.stream_events():
            pass
    ```
r   r   )r#   r   r   r   r   r   rw   ro   r`   rb   r   rd   re   rf   )r   rm   s     r@   r   RunResultStreaming.to_staten  s    > ((373G3G3S4//Y]YcYc4T:nn	
 +**$($A$A.2.U.U&*&E&E 11!%!;!;&*&E&E

 
	
r?   )	r  r  r   r  r  r  r  r  r  )r  r
  r   r   r   r   )r   zBaseException | None)rB  )r}   z"Literal['immediate', 'after_turn']r   r   )r   zAsyncIterator[StreamEvent])r   r   r  )r)  r
  r   r   r   )<r8   r9   r:   r;   r<   r=   r   r  rL   r  r  rR   rF   r'  Queuer  r	  r  r  r  r  r  r  r`   rJ   r  rb   r  r  r  r   rk   r   rG   rd   re   rf   rI   r  r  r  r  r   r   rN   r   r*  r9  r?  r4  rR  rU  rK  rD  r   rM  rH  rP  r   r>   r7   r?   r@   r  r    s1    ,"N<[AFEAR ">RU+E<+K1CHD@  ).d(OOK HMEHLD  CHEC? 
 /4Du.MM+M7<TPU7V4VEJSW^cEd%'Bd8=dQV8W5W*/5*I'I?DVZ_?`L<`9>tRW9X6X\,1$,GM)GF$)%e$DTD./&/ %*T)VGK#%DK>  %U??=B4V[=\O:\X7<TX]7^ 4^51FC1C#(E#BjB:(-d(G:GG',U'GGCDI5uEA  T8<N5<=B>: 
 8=TPU\a7b4b16u5W\1]($]0 N N.*K<    0.`N)`

(;T27 
0
r?   r  )rm   r   rn   rD   ro   r   rp   r   rq   r   rr   r   rA   r   rB   r   rC   r   r   r   )rn   rD   r}   r   r~   r   r   r   )rn   rD   r   r   )M
__future__r   r   r'  ri   r   collections.abcr   dataclassesr   r   r   typingr   r	   r
   r   r   pydanticr   pydantic_corer   r   r   agent_outputr   
exceptionsr   r   r   r   	guardrailr   r   itemsr   r   r   r   r   r   run_contextr   run_internal.itemsr   run_internal.run_stepsr    r!   r"   	run_stater#   rR  r$   tool_guardrailsr%   r&   tracingr'   tracing.tracesr(   util._pretty_printr)   r*   r+   r,   $sandbox.session.base_sandbox_sessionr-   r.   r1   rw   r   r   r   ABCrD   r   r  r7   r?   r@   <module>r     s   " 
    ) 1 1 = = ) %  /  C   * 8 
   & P  &
 3HCL $
< 
< 
<* #''+&+555 	5
 65 (+5  45  5 %5  $5 5p 67QQ Q A	Q
 Q> Z2CGG Z2 Z2z k) k) k)\ b
 b
 b
r?   