
    .hY$                       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 d dlmZ ddlmZ dd	l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mZmZ ddl m Z  ddl!m"Z" ddl#m$Z$ ddl%m&Z& ddl'm(Z(m)Z) e
rddlmZ dd	lmZ  ed          Z*e G d dej+                              Z,e G d de,                      Z-e G d de,                      Z.dS )    )annotationsN)AsyncIterator)	dataclassfield)TYPE_CHECKINGAnycast)TypeVar   )QueueCompleteSentinel)Agent)AgentOutputSchemaBase)InputGuardrailTripwireTriggeredMaxTurnsExceeded)InputGuardrailResultOutputGuardrailResult)ItemHelpersModelResponseRunItemTResponseInputItem)logger)RunContextWrapper)StreamEvent)Trace)pretty_print_result!pretty_print_run_result_streamingTc                      e Zd ZU ded<   	 ded<   	 ded<   	 ded<   	 d	ed
<   	 ded<   	 ded<   	 eej        dd                        Zdd dZd!dZ	ed"d            Z
dS )#RunResultBasezstr | list[TResponseInputItem]inputzlist[RunItem]	new_itemszlist[ModelResponse]raw_responsesr   final_outputzlist[InputGuardrailResult]input_guardrail_resultszlist[OutputGuardrailResult]output_guardrail_resultszRunContextWrapper[Any]context_wrapperreturn
Agent[Any]c                    dS )The last agent that was run.N selfs    J/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/agents/result.py
last_agentzRunResultBase.last_agent9   s          Fclstype[T]raise_if_incorrect_typeboolr   c                    |r,t          | j        |          st          d|j                   t	          t
          | j                  S )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 )
isinstancer#   	TypeError__name__r	   r   )r-   r1   r3   s      r.   final_output_aszRunResultBase.final_output_as>   sN     # 	K:d6G+M+M 	KI3<IIJJJAt()))r0   list[TResponseInputItem]c                `    t          j        | j                  }d | j        D             }||z   S )zVCreates a new input list, merging the original input with all the new items generated.c                6    g | ]}|                                 S r+   )to_input_item).0items     r.   
<listcomp>z/RunResultBase.to_input_list.<locals>.<listcomp>S   s$    EEEdT''))EEEr0   )r   input_to_new_input_listr    r!   )r-   original_itemsr!   s      r.   to_input_listzRunResultBase.to_input_listP   s6    3>3VW[Wa3b3bEEdnEEE		))r0   
str | Nonec                8    | j         sdS | j         d         j        S )zEConvenience method to get the response ID of the last model response.N)r"   response_idr,   s    r.   last_response_idzRunResultBase.last_response_idW   s%     ! 	4!"%11r0   Nr'   r(   )F)r1   r2   r3   r4   r'   r   )r'   r:   )r'   rD   )r8   
__module____qualname____annotations__propertyabcabstractmethodr/   r9   rC   rH   r+   r0   r.   r   r      s         ))))  '&&&L'777739999>++++0+ + +  X+* * * * *$* * * * 2 2 2 X2 2 2r0   r   c                  :    e Zd ZU ded<   edd            Zd	dZdS )
	RunResultr(   _last_agentr'   c                    | j         S )r*   )rR   r,   s    r.   r/   zRunResult.last_agentd   s     r0   strc                     t          |           S N)r   r,   s    r.   __str__zRunResult.__str__i   s    "4(((r0   NrI   r'   rT   )r8   rJ   rK   rL   rM   r/   rW   r+   r0   r.   rQ   rQ   `   sT               X ) ) ) ) ) )r0   rQ   c                     e Zd ZU dZded<   	 ded<   	 ded<   	 ded<   	  ed	
          Zded<    ed	
          Zded<   d	Zded<   	  ee	j
        d	          Zded<    ee	j
        d	          Zded<    edd	          Zded<    edd	          Zded<    edd	          Zded<    edd	          Zded<   ed(d            Zd)d!Zd*d#Zd$ Zd% Zd+d'ZdS ),RunResultStreamingaJ  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_agentintcurrent_turn	max_turnsr   r#   F)reprzAgentOutputSchemaBase | None_current_agent_output_schemazTrace | Nonetracer4   is_complete)default_factoryr_   z2asyncio.Queue[StreamEvent | QueueCompleteSentinel]_event_queuez#asyncio.Queue[InputGuardrailResult]_input_guardrail_queueN)defaultr_   zasyncio.Task[Any] | None_run_impl_task_input_guardrails_task_output_guardrails_taskzException | None_stored_exceptionr'   c                    | j         S )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,   s    r.   r/   zRunResultStreaming.last_agent   s    
 !!r0   Nonec                l   |                                   d| _        | j                                        s2| j                                         | j                                        2| j                                        s4| j                                         | j                                        2dS dS )zaCancels the streaming run, stopping all background tasks and marking the run as
        complete.TN)_cleanup_tasksrb   rd   empty
get_nowaitre   r,   s    r.   cancelzRunResultStreaming.cancel   s     	 #))++ 	+((*** #))++ 	+-3355 	5'22444 -3355 	5 	5 	5 	5 	5r0   AsyncIterator[StreamEvent]c               0  K   	 |                                   | j        rt          j        d           d| _        n| j        r| j                                        rn	 | j                                         d{V }n# t          j	        $ r Y nfw xY wt          |t                    r.| j                                         |                                   n|W V  | j                                         |                                  | j        r| j        dS )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.
        Tz Breaking due to stored exceptionN)_check_errorsrj   r   debugrb   rd   ro   getasyncioCancelledErrorr6   r   	task_donern   )r-   r?   s     r.   stream_eventsz RunResultStreaming.stream_events   sN     	*   % ?@@@#'  D$5$;$;$=$= !.2244444444)    $ 566 !++---""$$$JJJJ'')))-	*0 	! 	)((	) 	)s   A= =BBc                P   | j         | j        k    rt          d| j         d          | _        | j                                        sR| j                                        }|j        j        rt          |          | _        | j                                        R| j
        rP| j
                                        r7| j
                                        }|rt          |t                    r|| _        | j        rP| j                                        r7| j                                        }|rt          |t                    r|| _        | j        rR| j                                        r;| j                                        }|r"t          |t                    r|| _        d S d S d S d S d S )NzMax turns (z
) exceeded)r]   r^   r   rj   re   ro   rp   outputtripwire_triggeredr   rg   done	exceptionr6   	Exceptionrh   ri   )r-   guardrail_resultexcs      r.   rt   z RunResultStreaming._check_errors   s   t~--%56^DN6^6^6^%_%_D" -3355 	[#:EEGG&9 [)HIY)Z)Z& -3355 	[  	-4#6#;#;#=#= 	-%//11C -z#y11 -),&& 	-4+F+K+K+M+M 	--7799C -z#y11 -),&' 	-D,H,M,M,O,O 	-.88::C -z#y11 -),&&&	- 	- 	- 	-- - - -r0   c                d   | j         r2| j                                         s| j                                          | j        r2| j                                        s| j                                         | j        r4| j                                        s| j                                         d S d S d S rV   )rg   r~   rq   rh   ri   r,   s    r.   rn   z!RunResultStreaming._cleanup_tasks   s     	)t':'?'?'A'A 	)&&(((& 	1t/J/O/O/Q/Q 	1'..000' 	20L0Q0Q0S0S 	2(//11111	2 	2 	2 	2r0   rT   c                     t          |           S rV   )r   r,   s    r.   rW   zRunResultStreaming.__str__   s    0666r0   rI   )r'   rl   )r'   rr   rX   )r8   rJ   rK   __doc__rL   r   r`   ra   rb   rw   Queuerd   re   rg   rh   ri   rj   rM   r/   rq   rz   rt   rn   rW   r+   r0   r.   rZ   rZ   m   s          ,"NNN<[AFEARARAR RRRR%U+++E++++K1 HMuEH H HL     CH%EC C C    
 05uT/N/N/NNNNNN7<uTPU7V7V7VVVVV8=dQV8W8W8WWWWW*/%5*I*I*IIIII" " " X"
5 
5 
5 
5$) $) $) $)L- - -42 2 27 7 7 7 7 7r0   rZ   )/
__future__r   rN   rw   collections.abcr   dataclassesr   r   typingr   r   r	   typing_extensionsr
   	_run_implr   agentr   agent_outputr   
exceptionsr   r   	guardrailr   r   itemsr   r   r   r   r   run_contextr   rz   r   tracingr   util._pretty_printr   r   r   ABCr   rQ   rZ   r+   r0   r.   <module>r      s_   " " " " " " 



  ) ) ) ) ) ) ( ( ( ( ( ( ( ( + + + + + + + + + + % % % % % % , , , , , ,       / / / / / / I I I I I I I I B B B B B B B B J J J J J J J J J J J J       * * * * * * & & & & & &       V V V V V V V V 000000GCLL >2 >2 >2 >2 >2CG >2 >2 >2B 	) 	) 	) 	) 	) 	) 	) 	) H7 H7 H7 H7 H7 H7 H7 H7 H7 H7r0   