
    .hK)                       U d dl mZ d dlZd dlZd dlmZ d dlmZmZ d dlm	Z	m
Z
mZmZmZmZ d dlmZ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 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(m)Z) ddl*m+Z+ ddl,m-Z- e	rddl.m/Z/ ddlm0Z0 ddl1m2Z2 e G d d                      Z3ee$e%         e4e'         ge-e3         f         Z5de6d<   	  G d de          Z7 G d de          Z8e G d d ee%                               Z9dS )!    )annotationsN)	Awaitable)	dataclassfield)TYPE_CHECKINGAnyCallableGenericLiteralcast)NotRequired	TypeAlias	TypedDict   )AgentOutputSchemaBase)InputGuardrailOutputGuardrail)Handoff)ItemHelpers)logger)MCPUtil)ModelSettings)Model)RunContextWrapperTContext)FunctionToolResultToolfunction_tool)_transforms)MaybeAwaitable)
AgentHooks)	MCPServer)	RunResultc                  *    e Zd ZU ded<   	 dZded<   dS )ToolsToFinalOutputResultboolis_final_outputNz
Any | Nonefinal_output)__name__
__module____qualname____annotations__r(        I/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/agents/agent.pyr%   r%      s:           $L#### r.   r%   r   ToolsToFinalOutputFunctionc                      e Zd ZU ded<   dS )StopAtToolsz	list[str]stop_at_tool_namesN)r)   r*   r+   r,   r-   r.   r/   r2   r2   4   s         !!!!VVr.   r2   c                      e Zd ZU dZded<   dS )	MCPConfigzConfiguration for MCP servers.zNotRequired[bool]convert_schemas_to_strictN)r)   r*   r+   __doc__r,   r-   r.   r/   r5   r5   9   s(         ((0000 r.   r5   c                     e Zd ZU dZded<   	 dZded<   	 dZded<   	  ee	          Z	d
ed<   	 dZ
ded<   	  ee	          Zded<   	  ee	          Zded<   	  ee	          Zded<   	  ed 	          Zded<   	  ee	          Zded<   	  ee	          Zded<   	 dZded<   	 dZded<   	 dZd ed!<   	 d"Zd#ed$<   	 d5d)Z	 d6d7d/Zd8d2Zd9d3Zd9d4ZdS ):Agenta  An agent is an AI model configured with instructions, tools, guardrails, handoffs and more.

    We strongly recommend passing `instructions`, which is the "system prompt" for the agent. In
    addition, you can pass `handoff_description`, which is a human-readable description of the
    agent, used when the agent is used inside tools/handoffs.

    Agents are generic on the context type. The context is a (mutable) object you create. It is
    passed to tool functions, handoffs, guardrails, etc.
    strnameNzZstr | Callable[[RunContextWrapper[TContext], Agent[TContext]], MaybeAwaitable[str]] | Noneinstructions
str | Nonehandoff_description)default_factoryz$list[Agent[Any] | Handoff[TContext]]handoffszstr | Model | Nonemodelr   model_settings
list[Tool]toolszlist[MCPServer]mcp_serversc                     t                      S N)r5   r-   r.   r/   <lambda>zAgent.<lambda>   s
    )++ r.   r5   
mcp_configzlist[InputGuardrail[TContext]]input_guardrailszlist[OutputGuardrail[TContext]]output_guardrailsz(type[Any] | AgentOutputSchemaBase | Noneoutput_typezAgentHooks[TContext] | Nonehooksrun_llm_againzYLiteral['run_llm_again', 'stop_on_first_tool'] | StopAtTools | ToolsToFinalOutputFunctiontool_use_behaviorTr&   reset_tool_choicekwargsr   returnAgent[TContext]c                &    t          j        | fi |S )zMake a copy of the agent, with the given arguments changed. For example, you could do:
        ```
        new_agent = agent.clone(instructions="New instructions")
        ```
        )dataclassesreplace)selfrQ   s     r/   clonezAgent.clone   s     "4226222r.   	tool_nametool_descriptioncustom_output_extractor,Callable[[RunResult], Awaitable[str]] | Noner   c                |     t          |pt          j         j                  |pd          d	 fd            }|S )
aR  Transform this agent into a tool, callable by other agents.

        This is different from handoffs in two ways:
        1. In handoffs, the new agent receives the conversation history. In this tool, the new agent
           receives generated input.
        2. In handoffs, the new agent takes over the conversation. In this tool, the new agent is
           called as a tool, and the conversation is continued by the original agent.

        Args:
            tool_name: The name of the tool. If not provided, the agent's name will be used.
            tool_description: The description of the tool, which should indicate what it does and
                when to use it.
            custom_output_extractor: A function that extracts the output from the agent. If not
                provided, the last message from the agent will be used.
         )name_overridedescription_overridecontextr   inputr:   rR   c                   K   ddl m} |                     || j                   d {V }r |           d {V S t          j        |j                  S )Nr   )Runner)starting_agentrb   ra   )runrd   ra   r   text_message_outputs	new_items)ra   rb   rd   outputr[   rW   s       r/   	run_agentz Agent.as_tool.<locals>.run_agent   s      
 $#####!::# &        F
 ' =44V<<<<<<<<<3F4DEEEr.   )ra   r   rb   r:   rR   r:   )r   r   transform_string_function_styler;   )rW   rY   rZ   r[   rj   s   `  ` r/   as_toolzAgent.as_tool   ss    , 
#]{'RSWS\']']!1!7R

 

 

	F 	F 	F 	F 	F 	F	

 

	F r.   run_contextRunContextWrapper[TContext]c                  K   t          | j        t                    r| j        S t          | j                  r|t	          j        | j                  r:t          t          t                   |                     ||                      d{V S t          t          |                     ||                     S | j        t          j	        d| j                    dS )z$Get the system prompt for the agent.Nz1Instructions must be a string or a function, got )

isinstancer<   r:   callableinspectiscoroutinefunctionr   r   r   error)rW   rm   s     r/   get_system_promptzAgent.get_system_prompt   s      d'-- 	b$$d'(( 	b*4+<== G!)C.$2C2CKQU2V2VWWWWWWWWWC!2!2;!E!EFFF*L`TM^``aaatr.   c                |   K   | j                             dd          }t          j        | j        |           d{V S )z1Fetches the available tools from the MCP servers.r6   FN)rI   getr   get_all_function_toolsrE   )rW   r6   s     r/   get_mcp_toolszAgent.get_mcp_tools   sI      $(O$7$78SUZ$[$[!3D4DF_`````````r.   c                N   K   |                                   d{V }|| j        z   S )z8All agent tools, including MCP tools and function tools.N)ry   rD   )rW   	mcp_toolss     r/   get_all_toolszAgent.get_all_tools   s6      ,,........	4:%%r.   )rQ   r   rR   rS   rG   )rY   r=   rZ   r=   r[   r\   rR   r   )rm   rn   rR   r=   )rR   rC   )r)   r*   r+   r7   r,   r<   r>   r   listr@   rA   r   rB   rD   rE   rI   rJ   rK   rL   rM   rO   rP   rX   rl   ru   ry   r|   r-   r.   r/   r9   r9   B   sf          III  	      '+**** 6;U45P5P5PHPPPP
 !%E$$$$ %*E-$H$H$HNHHHH d333E33331#(5#>#>#>K>>>> "E2E2EFFFJFFFF(7<uT7R7R7RRRRR :?t9T9T9TTTTT =AK@@@@ *.E----
 	       #""""Z3 3 3 3 QU	' ' ' ' 'R   a a a a
& & & & & &r.   r9   ):
__future__r   rU   rr   collections.abcr   r   r   typingr   r   r	   r
   r   r   typing_extensionsr   r   r   agent_outputr   	guardrailr   r   r@   r   itemsr   r   mcpr   rB   r   models.interfacer   rm   r   r   toolr   r   r   utilr   util._typesr    	lifecycler!   r"   resultr#   r%   r}   r0   r,   r2   r5   r9   r-   r.   r/   <module>r      s   " " " " " " "      % % % % % % ( ( ( ( ( ( ( ( G G G G G G G G G G G G G G G G ? ? ? ? ? ? ? ? ? ? / / / / / / 6 6 6 6 6 6 6 6                         ) ) ) ) ) ) # # # # # # 4 4 4 4 4 4 4 4 9 9 9 9 9 9 9 9 9 9       ' ' ' ' ' ' "%%%%%%!!!!!! 	 	 	 	 	 	 	 	 )1x $'9":;+,.)     
W W W W W) W W W
    	    y& y& y& y& y&GH y& y& y& y& y&r.   