o
    iG                     @  s  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
mZmZmZ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 e
rdddlmZ ddlmZ ddlmZ ddlm Z  ddl!m"Z" G dd deZ#eddG dd dee Z$G dd deee Z%dS )    )annotations)ABCabstractmethod)Callable)	dataclass)TYPE_CHECKINGAnyGenericLiteralProtocol)SchemaValidator)Self   )
AgentDepsT
RunContext)ToolDefinitionToolsPrepareFunc   ApprovalRequiredToolsetFilteredToolsetPrefixedToolsetPreparedToolsetRenamedToolsetc                   @  s0   e Zd ZdZdddddZdddddZdS )SchemaValidatorProtzsProtocol for a Pydantic Core `SchemaValidator` or `PluggableSchemaValidator` (which is private but API-compatible).F)allow_partialinputstr | bytes | bytearrayr   /bool | Literal['off', 'on', 'trailing-strings']kwargsr   returnc                K     d S N selfr    r   r#   r'   r'   c/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/pydantic_ai/toolsets/abstract.pyvalidate_json   s   z!SchemaValidatorProt.validate_jsonc                K  r%   r&   r'   r(   r'   r'   r*   validate_python!   s   z#SchemaValidatorProt.validate_pythonN)r    r!   r   r"   r#   r   r$   r   )r    r   r   r"   r#   r   r$   r   )__name__
__module____qualname____doc__r+   r,   r'   r'   r'   r*   r      s    	r   T)kw_onlyc                   @  s8   e Zd ZU dZded< 	 ded< 	 ded< 	 ded	< d
S )ToolsetToola:  Definition of a tool available on a toolset.

    This is a wrapper around a plain tool definition that includes information about:

    - the toolset that provided it, for use in error messages
    - the maximum number of retries to attempt if the tool call fails
    - the validator for the tool's arguments
    AbstractToolset[AgentDepsT]toolsetr   tool_defintmax_retriesz%SchemaValidator | SchemaValidatorProtargs_validatorN)r-   r.   r/   r0   __annotations__r'   r'   r'   r*   r2   &   s   
 	r2   c                   @  s   e Zd ZdZeedCddZedDddZedDd	d
ZdEddZ	dFddZ
edGddZedHddZdId"d#ZdJd&d'ZdKd+d,ZdLd/d0ZdMd4d5ZdNd9d:Zd;d< fdOd@dAZdBS )PAbstractToolseta  A toolset is a collection of tools that can be used by an agent.

    It is responsible for:

    - Listing the tools it contains
    - Validating the arguments of the tools
    - Calling the tools

    See [toolset docs](../toolsets.md) for more information.
    r$   
str | Nonec                 C  s   t  )a  An ID for the toolset that is unique among all toolsets registered with the same agent.

        If you're implementing a concrete implementation that users can instantiate more than once, you should let them optionally pass a custom ID to the constructor and return that here.

        A toolset needs to have an ID in order to be used in a durable execution environment like Temporal, in which case the ID will be used to identify the toolset's activities within the workflow.
        NotImplementedErrorr)   r'   r'   r*   idJ   s   	zAbstractToolset.idstrc                 C  s"   | j j}| jr|d| j7 }|S )z2The name of the toolset for use in error messages. )	__class__r-   r?   )r)   labelr'   r'   r*   rC   U   s   zAbstractToolset.labelc                 C     dS )zUA hint for how to avoid name conflicts with other toolsets for use in error messages.zSRename the tool or wrap the toolset in a `PrefixedToolset` to avoid name conflicts.r'   r>   r'   r'   r*   tool_name_conflict_hint]   s   z'AbstractToolset.tool_name_conflict_hintr   c                   s   | S )z{Enter the toolset context.

        This is where you can set up network connections in a concrete implementation.
        r'   r>   r'   r'   r*   
__aenter__b      zAbstractToolset.__aenter__argsr   bool | Nonec                   s   dS )z}Exit the toolset context.

        This is where you can tear down network connections in a concrete implementation.
        Nr'   )r)   rH   r'   r'   r*   	__aexit__i   rG   zAbstractToolset.__aexit__ctxRunContext[AgentDepsT]"dict[str, ToolsetTool[AgentDepsT]]c                      t  )z-The tools that are available in this toolset.r<   )r)   rK   r'   r'   r*   	get_toolsp   s   zAbstractToolset.get_toolsname	tool_argsdict[str, Any]toolToolsetTool[AgentDepsT]c                   rN   )aJ  Call a tool with the given arguments.

        Args:
            name: The name of the tool to call.
            tool_args: The arguments to pass to the tool.
            ctx: The run context.
            tool: The tool definition returned by [`get_tools`][pydantic_ai.toolsets.AbstractToolset.get_tools] that was called.
        r<   )r)   rP   rQ   rK   rS   r'   r'   r*   	call_toolu   s   zAbstractToolset.call_toolvisitor-Callable[[AbstractToolset[AgentDepsT]], None]Nonec                 C  s   ||  dS )zmRun a visitor function on all "leaf" toolsets (i.e. those that implement their own tool listing and calling).Nr'   r)   rV   r'   r'   r*   apply   s   zAbstractToolset.applyDCallable[[AbstractToolset[AgentDepsT]], AbstractToolset[AgentDepsT]]r3   c                 C  s   || S )zRun a visitor function on all "leaf" toolsets (i.e. those that implement their own tool listing and calling) and replace them in the hierarchy with the result of the function.r'   rY   r'   r'   r*   visit_and_replace   s   z!AbstractToolset.visit_and_replacefilter_func8Callable[[RunContext[AgentDepsT], ToolDefinition], bool]FilteredToolset[AgentDepsT]c                 C     ddl m} || |S )zReturns a new toolset that filters this toolset's tools using a filter function that takes the agent context and the tool definition.

        See [toolset docs](../toolsets.md#filtering-tools) for more information.
        r   r   )filteredr   )r)   r]   r   r'   r'   r*   ra      s   
zAbstractToolset.filteredprefixPrefixedToolset[AgentDepsT]c                 C  r`   )zReturns a new toolset that prefixes the names of this toolset's tools.

        See [toolset docs](../toolsets.md#prefixing-tool-names) for more information.
        r   r   )prefixedr   )r)   rb   r   r'   r'   r*   rd         
zAbstractToolset.prefixedprepare_funcToolsPrepareFunc[AgentDepsT]PreparedToolset[AgentDepsT]c                 C  r`   )zReturns a new toolset that prepares this toolset's tools using a prepare function that takes the agent context and the original tool definitions.

        See [toolset docs](../toolsets.md#preparing-tool-definitions) for more information.
        r   r   )preparedr   )r)   rf   r   r'   r'   r*   ri      re   zAbstractToolset.preparedname_mapdict[str, str]RenamedToolset[AgentDepsT]c                 C  r`   )zReturns a new toolset that renames this toolset's tools using a dictionary mapping new names to original names.

        See [toolset docs](../toolsets.md#renaming-tools) for more information.
        r   r   )renamedr   )r)   rj   r   r'   r'   r*   rm      re   zAbstractToolset.renamedc                 C  rD   )NTr'   )rK   r5   rQ   r'   r'   r*   <lambda>   s    zAbstractToolset.<lambda>approval_required_funcHCallable[[RunContext[AgentDepsT], ToolDefinition, dict[str, Any]], bool]#ApprovalRequiredToolset[AgentDepsT]c                 C  r`   )zReturns a new toolset that requires (some) calls to tools it contains to be approved.

        See [toolset docs](../toolsets.md#requiring-tool-approval) for more information.
        r   r   )approval_requiredr   )r)   ro   r   r'   r'   r*   rr      s   

z!AbstractToolset.approval_requiredN)r$   r;   )r$   r@   )r$   r   )rH   r   r$   rI   )rK   rL   r$   rM   )
rP   r@   rQ   rR   rK   rL   rS   rT   r$   r   )rV   rW   r$   rX   )rV   r[   r$   r3   )r]   r^   r$   r_   )rb   r@   r$   rc   )rf   rg   r$   rh   )rj   rk   r$   rl   )ro   rp   r$   rq   )r-   r.   r/   r0   propertyr   r?   rC   rE   rF   rJ   rO   rU   rZ   r\   ra   rd   ri   rm   rr   r'   r'   r'   r*   r:   >   s.    	






	
	r:   N)&
__future__r   abcr   r   collections.abcr   dataclassesr   typingr   r   r	   r
   r   pydantic_corer   typing_extensionsr   _run_contextr   r   toolsr   r   rr   r   ra   r   rd   r   ri   r   rm   r   r   r2   r:   r'   r'   r'   r*   <module>   s&    