o
    hD                  	   @  s  d dl mZ d dlZd dlZd dlmZ d dlmZ d dlm	Z	m
Z
mZmZmZmZ d dlmZ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 d d
lmZ d dlmZ d dl m!Z! d dl"m#Z#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/m.Z. ddl0m1Z1 ddl2m2Z2 ddl3m4Z4 ddl5m6Z6 ddl7m8Z8 ddl9m:Z:m;Z; ddl<m=Z= ddl>m?Z? ddl@mAZA e	rddlBmCZCmDZD e%dZEeeEe
f ZFee#e4e
 eEf e
f ZGee#e8eEf e
f ZHeeFeE eGeE eHeE f ZIeG dd dZJeG d d! d!ZKeG d"d# d#ZLeG d$d% d%ZMeG d&d' d'ZNeG d(d) d)ZOeG d*d+ d+ZPG d,d- d-e&ZQeePgeAeQ f ZR	 eG d.d/ d/ZSeG d0d1 d1ZTeG d2d3 d3ZUeG d4d5 d5ZVeeVgeAeW f ZX	 eG d6d7 d7ZYeeKeLeMeNeSeYeUeTf ZZ	 dWd>d?Z[ee4e
 e\geAeW f Z]edddd@dd@d@dAdXdPdQZ^edddd@dd@d@dAdYdSdQZ^	dZdddd@e[d@d@dAd[dVdQZ^dS )\    )annotationsN)	Awaitable)	dataclass)TYPE_CHECKINGAnyCallableLiteralUnionoverload)FiltersRankingOptions)PendingSafetyCheckResponseComputerToolCall)LocalShellCallMcpApprovalRequest)CodeInterpreterImageGenerationMcp)r   )UserLocation)ValidationError)ConcatenateNotRequired	ParamSpec	TypedDict   )_debug)AsyncComputerComputer)ModelBehaviorError)DocstringStylefunction_schema)RunItem)logger)RunContextWrapper)ensure_strict_json_schema)ToolContext)ToolInputGuardrailToolOutputGuardrail)	SpanError)_error_tracing)MaybeAwaitable)Agent	AgentBase
ToolParamsc                   @  s*   e Zd ZU ded< 	 ded< 	 ded< dS )FunctionToolResultFunctionTooltoolr   outputr!   run_itemN)__name__
__module____qualname____annotations__ r7   r7   L/var/www/html/openai_agents/venv/lib/python3.10/site-packages/agents/tool.pyr.   3   s   
 r.   c                   @  sz   e Zd ZU dZded< 	 ded< 	 ded< 	 ded< 	 d	Zd
ed< 	 d	Zded< 	 dZded< 	 dZded< 	 dd Z	dS )r/   zA tool that wraps a function. In most cases, you should use  the `function_tool` helpers to
    create a FunctionTool, as they let you easily wrap a Python function.
    strnamedescriptionzdict[str, Any]params_json_schemaz1Callable[[ToolContext[Any], str], Awaitable[Any]]on_invoke_toolTboolstrict_json_schemaJbool | Callable[[RunContextWrapper[Any], AgentBase], MaybeAwaitable[bool]]
is_enabledNz$list[ToolInputGuardrail[Any]] | Nonetool_input_guardrailsz%list[ToolOutputGuardrail[Any]] | Nonetool_output_guardrailsc                 C  s   | j rt| j| _d S d S N)r?   r$   r<   selfr7   r7   r8   __post_init__i   s   zFunctionTool.__post_init__)
r3   r4   r5   __doc__r6   r?   rA   rB   rC   rG   r7   r7   r7   r8   r/   ?   s&   
 
r/   c                   @  s`   e Zd ZU dZded< 	 dZded< 	 dZded	< 	 dZd
ed< 	 dZded< 	 e	dd Z
dS )FileSearchToolzA hosted tool that lets the LLM search through a vector store. Currently only supported with
    OpenAI models, using the Responses API.
    z	list[str]vector_store_idsNz
int | Nonemax_num_resultsFr>   include_search_resultszRankingOptions | Noneranking_optionszFilters | Nonefiltersc                 C     dS )Nfile_searchr7   rE   r7   r7   r8   r:         zFileSearchTool.name)r3   r4   r5   rH   r6   rK   rL   rM   rN   propertyr:   r7   r7   r7   r8   rI   n   s   
 rI   c                   @  sH   e Zd ZU dZdZded< 	 dZded< 	 dZded	< 	 ed
d Z	dS )WebSearchToolz~A hosted tool that lets the LLM search the web. Currently only supported with OpenAI models,
    using the Responses API.
    NzUserLocation | Noneuser_locationzWebSearchToolFilters | NonerN   mediumz Literal['low', 'medium', 'high']search_context_sizec                 C  rO   )N
web_searchr7   rE   r7   r7   r8   r:      rQ   zWebSearchTool.name)
r3   r4   r5   rH   rT   r6   rN   rV   rR   r:   r7   r7   r7   r8   rS      s   
 rS   c                   @  6   e Zd ZU dZded< 	 dZded< 	 edd ZdS )	ComputerToolz3A hosted tool that lets the LLM control a computer.zComputer | AsyncComputercomputerNzDCallable[[ComputerToolSafetyCheckData], MaybeAwaitable[bool]] | Noneon_safety_checkc                 C  rO   )Ncomputer_use_previewr7   rE   r7   r7   r8   r:      rQ   zComputerTool.name)r3   r4   r5   rH   r6   r[   rR   r:   r7   r7   r7   r8   rY      s   
 rY   c                   @  s8   e Zd ZU dZded< 	 ded< 	 ded< 	 ded	< d
S )ComputerToolSafetyCheckDataz/Information about a computer tool safety check.RunContextWrapper[Any]ctx_wrapperz
Agent[Any]agentr   	tool_callr   safety_checkNr3   r4   r5   rH   r6   r7   r7   r7   r8   r]      s   
 r]   c                   @  $   e Zd ZU dZded< 	 ded< dS )MCPToolApprovalRequestz!A request to approve a tool call.r^   r_   r   dataNrc   r7   r7   r7   r8   re         
 re   c                   @  rd   )MCPToolApprovalFunctionResultz,The result of an MCP tool approval function.r>   approvezNotRequired[str]reasonNrc   r7   r7   r7   r8   rh      s   
 rh   c                   @  rX   )	HostedMCPToola  A tool that allows the LLM to use a remote MCP server. The LLM will automatically list and
    call tools, without requiring a round trip back to your code.
    If you want to run MCP servers locally via stdio, in a VPC or other non-publicly-accessible
    environment, or you just prefer to run tool calls locally, then you can instead use the servers
    in `agents.mcp` and pass `Agent(mcp_servers=[...])` to the agent.r   tool_configNzMCPToolApprovalFunction | Noneon_approval_requestc                 C  rO   )N
hosted_mcpr7   rE   r7   r7   r8   r:      rQ   zHostedMCPTool.name)r3   r4   r5   rH   r6   rm   rR   r:   r7   r7   r7   r8   rk      s   
 rk   c                   @  (   e Zd ZU dZded< 	 edd ZdS )CodeInterpreterToolzFA tool that allows the LLM to execute code in a sandboxed environment.r   rl   c                 C  rO   )Ncode_interpreterr7   rE   r7   r7   r8   r:      rQ   zCodeInterpreterTool.nameNr3   r4   r5   rH   r6   rR   r:   r7   r7   r7   r8   rp         
 rp   c                   @  ro   )ImageGenerationToolz.A tool that allows the LLM to generate images.r   rl   c                 C  rO   )Nimage_generationr7   rE   r7   r7   r8   r:     rQ   zImageGenerationTool.nameNrr   r7   r7   r7   r8   rt      rs   rt   c                   @  rd   )LocalShellCommandRequestz*A request to execute a command on a shell.r^   r_   r   rf   Nrc   r7   r7   r7   r8   rv     rg   rv   c                   @  ro   )LocalShellToolzA tool that allows the LLM to execute commands on a shell.

    For more details, see:
    https://platform.openai.com/docs/guides/tools-local-shell
    LocalShellExecutorexecutorc                 C  rO   )Nlocal_shellr7   rE   r7   r7   r8   r:      rQ   zLocalShellTool.nameNrr   r7   r7   r7   r8   rw     s   
 rw   ctxr^   error	Exceptionreturnr9   c                 C  s   dt | S )zLThe default tool error function, which just returns a generic error message.zCAn error occurred while running the tool. Please try again. Error: )r9   )r{   r|   r7   r7   r8   default_tool_error_function2  s   r   Tname_overridedescription_overridedocstring_styleuse_docstring_infofailure_error_functionstrict_moderA   funcToolFunction[...]r   
str | Noner   r   DocstringStyle | Noner   r>   r   ToolErrorFunction | Noner   rA   r@   c                C  rO   )z6Overload for usage as @function_tool (no parentheses).Nr7   )r   r   r   r   r   r   r   rA   r7   r7   r8   function_tool:  s   r   +Callable[[ToolFunction[...]], FunctionTool]c                 C  rO   )z*Overload for usage as @function_tool(...).Nr7   r   r7   r7   r8   r   J  s   ToolFunction[...] | None:FunctionTool | Callable[[ToolFunction[...]], FunctionTool]c          	        s<   dfdd t | r | S d fdd	}|S )a)  
    Decorator to create a FunctionTool from a function. By default, we will:
    1. Parse the function signature to create a JSON schema for the tool's parameters.
    2. Use the function's docstring to populate the tool's description.
    3. Use the function's docstring to populate argument descriptions.
    The docstring style is detected automatically, but you can override it.

    If the function takes a `RunContextWrapper` as the first argument, it *must* match the
    context type of the agent that uses the tool.

    Args:
        func: The function to wrap.
        name_override: If provided, use this name for the tool instead of the function's name.
        description_override: If provided, use this description for the tool instead of the
            function's docstring.
        docstring_style: If provided, use this style for the tool's docstring. If not provided,
            we will attempt to auto-detect the style.
        use_docstring_info: If True, use the function's docstring to populate the tool's
            description and argument descriptions.
        failure_error_function: If provided, use this function to generate an error message when
            the tool call fails. The error message is sent to the LLM. If you pass None, then no
            error message will be sent and instead an Exception will be raised.
        strict_mode: Whether to enable strict mode for the tool's JSON schema. We *strongly*
            recommend setting this to True, as it increases the likelihood of correct JSON input.
            If False, it allows non-strict JSON schemas. For example, if a parameter has a default
            value, it will be optional, additional properties are allowed, etc. See here for more:
            https://platform.openai.com/docs/guides/structured-outputs?api-mode=responses#supported-schemas
        is_enabled: Whether the tool is enabled. Can be a bool or a callable that takes the run
            context and agent and returns whether the tool is enabled. Disabled tools are hidden
            from the LLM at runtime.
    the_funcr   r~   r/   c                   sT   t 	ddfdd	 d fd
d}tjjp"dj|dS )N)r   r   r   r   r   r?   r{   ToolContext[Any]inputr9   r~   r   c              
     s  z|r	t |ni }W n1 ty= } z%tjr!td j  ntd j d|  td j d| |d }~ww tjrKtd j  ntd j d|  z|rb j	d
i |n 	 }W n t
y } ztd j d| |d }~ww  |\}}tjstd| d|  tr jr| g|R i |I d H }n!|i |I d H }n jrƈ| g|R i |}n|i |}tjrtd j d |S td j d	|  |S )NzInvalid JSON input for tool z: zInvoking tool z with input zTool call args: z
, kwargs: zTool z completed.z
 returned r7   )jsonloadsr}   r   DONT_LOG_TOOL_DATAr"   debugr:   r   params_pydantic_modelr   to_call_argsinspectiscoroutinefunctiontakes_context)r{   r   	json_dataeparsedargskwargs_dictresult)schemar   r7   r8   _on_invoke_tool_impl  sR   	
zJfunction_tool.<locals>._create_function_tool.<locals>._on_invoke_tool_implc              
     s   z	 | |I d H W S  t yG } z1d u r | |}t|r,|I d H W  Y d }~S ttdjt|dd |W  Y d }~S d }~ww )NzError running tool (non-fatal))	tool_namer|   )messagerf   )r}   r   isawaitabler)   attach_error_to_current_spanr(   r:   r9   )r{   r   r   r   )r   r   r   r7   r8   _on_invoke_tool  s(   

	zEfunction_tool.<locals>._create_function_tool.<locals>._on_invoke_tool )r:   r;   r<   r=   r?   rA   )r{   r   r   r9   r~   r   )r    r/   r:   r;   r<   )r   r   )r   r   r   rA   r   r   r   )r   r   r   r8   _create_function_tool  s$   	1z,function_tool.<locals>._create_function_tool	real_funcc                   s    | S rD   r7   )r   )r   r7   r8   	decorator  s   z function_tool.<locals>.decoratorN)r   r   r~   r/   )r   r   r~   r/   )callable)	r   r   r   r   r   r   r   rA   r   r7   )r   r   r   r   rA   r   r   r   r8   r   Y  s
   +[)r{   r^   r|   r}   r~   r9   )r   r   r   r   r   r   r   r   r   r>   r   r   r   r>   rA   r@   r~   r/   )r   r   r   r   r   r   r   r>   r   r   r   r>   rA   r@   r~   r   rD   )r   r   r   r   r   r   r   r   r   r>   r   r   r   r>   rA   r@   r~   r   )_
__future__r   r   r   collections.abcr   dataclassesr   typingr   r   r   r   r	   r
   -openai.types.responses.file_search_tool_paramr   r   2openai.types.responses.response_computer_tool_callr   r   +openai.types.responses.response_output_itemr   r   !openai.types.responses.tool_paramr   r   r   &openai.types.responses.web_search_toolWebSearchToolFilters,openai.types.responses.web_search_tool_paramr   pydanticr   typing_extensionsr   r   r   r   r   r   rZ   r   r   
exceptionsr   r    r   itemsr!   r"   run_contextr#   strict_schemar$   tool_contextr%   tool_guardrailsr&   r'   tracingr(   utilr)   util._typesr*   r`   r+   r,   r-   ToolFunctionWithoutContextToolFunctionWithContextToolFunctionWithToolContextToolFunctionr.   r/   rI   rS   rY   r]   re   rh   MCPToolApprovalFunctionrk   rp   rt   rv   r9   rx   rw   Toolr   r}   ToolErrorFunctionr   r7   r7   r7   r8   <module>   s     .




