o
    +iV,                     @  s  U d dl mZ d dlZd dlmZmZ d dlmZmZm	Z	m
Z
mZmZmZmZmZ d dlmZ d dlmZ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 ddl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( e)e*Z+ee,ee( f Z-de.d< ede-f Z/e
de/dZ0edeeee-f f Z1e
de1dZ2G dd deZ3G dd deZ4G dd de	e# Z5G dd de5e0 Z6G dd  d e5e2 Z7ed>d$d%Z8edddd&d?d,d%Z8eddddd-d@d1d%Z8	dAddddd-dBd4d%Z8edCd6d7Z9eddddd-dDd8d7Z9eddddd-dEd:d7Z9	dAddddd-dFd=d7Z9ee6e e3f Z:ee7e e4f Z;dS )G    )annotationsN)ABCabstractmethod)	AnyUnionGenericTypeVarCallableIterable	Coroutinecastoverload)iscoroutinefunction)	TypeAliasoverride)	BaseModel   )_compat)is_dict)cached_property)TypeAdapter)BetaToolParamBetaToolUnionParam)	CallableT)InputSchema)Contentr   BetaFunctionToolResultType.	FunctionT)boundAsyncFunctionTc                   @  6   e Zd ZedddZeddd	ZedddZdS )BetaBuiltinFunctionToolreturnr   c                 C     d S N selfr%   r%   `/var/www/html/psymed-ai/venv/lib/python3.10/site-packages/anthropic/lib/tools/_beta_functions.pyto_dict"      zBetaBuiltinFunctionTool.to_dictinputobjectr   c                 C  r#   r$   r%   r'   r+   r%   r%   r(   call%   r*   zBetaBuiltinFunctionTool.callstrc                 C      |   }d|v r|d S |d S Nmcp_server_namenamer)   r'   rawr%   r%   r(   r3   (      zBetaBuiltinFunctionTool.nameNr"   r   r+   r,   r"   r   r"   r/   __name__
__module____qualname__r   r)   r.   propertyr3   r%   r%   r%   r(   r!   !       r!   c                   @  r    )BetaAsyncBuiltinFunctionToolr"   r   c                 C  r#   r$   r%   r&   r%   r%   r(   r)   1   r*   z$BetaAsyncBuiltinFunctionTool.to_dictr+   r,   r   c                   s   d S r$   r%   r-   r%   r%   r(   r.   4   s   z!BetaAsyncBuiltinFunctionTool.callr/   c                 C  r0   r1   r4   r5   r%   r%   r(   r3   7   r7   z!BetaAsyncBuiltinFunctionTool.nameNr8   r9   r:   r;   r%   r%   r%   r(   rA   0   r@   rA   c                   @  s   e Zd ZU ded< 	 ded< 	 ded< ded< ddddd	d!ddZed"ddZd#ddZed$ddZ	d%ddZ
d&ddZed'dd ZdS )(BaseFunctionToolr   funcr/   r3   descriptionr   input_schemaNr3   rD   rE   defer_loading
str | None$InputSchema | type[BaseModel] | NonerG   bool | Noner"   Nonec                C  sx   t jrtd|| _t|| _|p|j| _|| _	|p| 
 | _|d ur5t|tr0| | _d S || _d S |  | _d S )N2Tool functions are only supported with Pydantic v2)r   PYDANTIC_V1RuntimeErrorrC   pydanticvalidate_call_func_with_validater<   r3   _defer_loading_get_description_from_docstringrD   
isinstancetypemodel_json_schemarE   _create_schema_from_function)r'   rC   r3   rD   rE   rG   r%   r%   r(   __init__J   s   	

zBaseFunctionTool.__init__c                 C  s   | j S r$   rC   r&   r%   r%   r(   __call__e   s   zBaseFunctionTool.__call__r   c                 C  s*   | j | j| jd}| jd ur| j|d< |S )Nr3   rD   rE   rG   )r3   rD   rE   rR   )r'   defnr%   r%   r(   r)   i   s   

zBaseFunctionTool.to_dictdocstring_parser.Docstringc                 C  s   t | jjpdS )N )docstring_parserparserC   __doc__r&   r%   r%   r(   _parsed_docstrings   s   z"BaseFunctionTool._parsed_docstringc                 C  s2   | j jr| j j}| j jr|d| j j 7 }|S dS )z*Extract description from parsed docstring.z

r^   )rb   short_descriptionlong_description)r'   rD   r%   r%   r(   rS   w   s   z0BaseFunctionTool._get_description_from_docstringc                 C  sV   ddl m} ddlm}m} ddlm} G dd d|}|| j| jd}| j	j
|dS )	z:Create JSON schema from function signature using pydantic.r   )
CoreSchema)JsonSchemaValueGenerateJsonSchema)ArgumentsParameterc                      s:   e Zd Zd fddZdddZed fddZ  ZS )zOBaseFunctionTool._create_schema_from_function.<locals>.CustomGenerateJsonSchemarC   Callable[..., Any]parsed_docstringr   r"   rK   c                  s   t    || _|| _d S r$   )superrX   _funcrb   )r'   rC   rj   	__class__r%   r(   rX      s   

zXBaseFunctionTool._create_schema_from_function.<locals>.CustomGenerateJsonSchema.__init___args_kwds'CustomGenerateJsonSchema'c                 _  s   | S r$   r%   )r'   ro   rp   r%   r%   r(   rZ      r*   zXBaseFunctionTool._create_schema_from_function.<locals>.CustomGenerateJsonSchema.__call__	arguments'list[ArgumentsParameter]'var_kwargs_schemaCoreSchema | Nonerf   c                   s~   t  ||}|ddkr|S |d}|rt|s|S | jjD ]}||j}|r/t|s0q!|jr<d|vr<|j|d< q!|S )NrU   r,   
propertiesrD   )rk   kw_arguments_schemagetr   rb   paramsarg_namerD   )r'   rr   rt   schemarv   paramprop_schemarm   r%   r(   rw      s   

zcBaseFunctionTool._create_schema_from_function.<locals>.CustomGenerateJsonSchema.kw_arguments_schema)rC   ri   rj   r   r"   rK   )ro   r   rp   r   r"   rq   )rr   rs   rt   ru   r"   rf   )r<   r=   r>   rX   rZ   r   rw   __classcell__r%   r%   rm   r(   CustomGenerateJsonSchema   s
    
r   )rC   rj   )schema_generator)pydantic_corere   pydantic.json_schemarf   rg   pydantic_core.core_schemarh   rC   rb   _adapterjson_schema)r'   re   rf   rg   rh   r   r   r%   r%   r(   rW      s   "z-BaseFunctionTool._create_schema_from_functionTypeAdapter[Any]c                 C  s
   t | jS r$   )r   rQ   r&   r%   r%   r(   r      s   
zBaseFunctionTool._adapter)rC   r   r3   rH   rD   rH   rE   rI   rG   rJ   r"   rK   )r"   r   )r"   r   )r"   r]   r:   )r"   r   )r"   r   )r<   r=   r>   __annotations__rX   r?   rZ   r)   r   rb   rS   rW   r   r%   r%   r%   r(   rB   ?   s*   
 



	,rB   c                   @     e Zd ZdddZdS )	BetaFunctionToolr+   r,   r"   r   c              
   C  st   t | jr	tdt|stdt|j z| jdi tt	|W S  t
jy9 } z	td| j |d }~ww )NzJCannot call a coroutine function synchronously. Use `@async_tool` instead. Input must be a dictionary, got Invalid arguments for function r%   r   rC   rN   r   	TypeErrorrU   r<   rQ   r   r   rO   ValidationError
ValueErrorr3   r'   r+   er%   r%   r(   r.      s   
zBetaFunctionTool.callNr9   r<   r=   r>   r.   r%   r%   r%   r(   r          r   c                   @  r   )	BetaAsyncFunctionToolr+   r,   r"   r   c              
     s|   t | js
tdt|stdt|j z| jdi tt	|I d H W S  t
jy= } z	td| j |d }~ww )NzGCannot call a synchronous function asynchronously. Use `@tool` instead.r   r   r%   r   r   r%   r%   r(   r.      s   
zBetaAsyncFunctionTool.callNr9   r   r%   r%   r%   r(   r      r   r   rC   r"   BetaFunctionTool[FunctionT]c                 C  r#   r$   r%   rY   r%   r%   r(   	beta_tool   r*   r   r[   r3   rH   rD   rE   rI   c                C  r#   r$   r%   )rC   r3   rD   rE   r%   r%   r(   r         rF   rG   rJ   2Callable[[FunctionT], BetaFunctionTool[FunctionT]]c                 C  r#   r$   r%   rF   r%   r%   r(   r      r   FunctionT | NonePBetaFunctionTool[FunctionT] | Callable[[FunctionT], BetaFunctionTool[FunctionT]]c                  @   t jrtd| durt|  dS d
 fdd	}|S )aF  Create a FunctionTool from a function with automatic schema inference.

    Can be used as a decorator with or without parentheses:

    @function_tool
    def my_func(x: int) -> str: ...

    @function_tool()
    def my_func(x: int) -> str: ...

    @function_tool(name="custom_name")
    def my_func(x: int) -> str: ...
    rL   NrC   r3   rD   rE   rG   rC   r   r"   r   c                      t |  dS Nr   )r   rY   rG   rD   rE   r3   r%   r(   	decorator  s   
zbeta_tool.<locals>.decoratorrC   r   r"   r   )r   rM   rN   r   rC   r3   rD   rE   rG   r   r%   r   r(   r      s   
%BetaAsyncFunctionTool[AsyncFunctionT]c                 C  r#   r$   r%   rY   r%   r%   r(   beta_async_tool  r*   r   c                C  r#   r$   r%   r   r%   r%   r(   r     s   ACallable[[AsyncFunctionT], BetaAsyncFunctionTool[AsyncFunctionT]]c                 C  r#   r$   r%   rF   r%   r%   r(   r     r   AsyncFunctionT | NoneiBetaAsyncFunctionTool[AsyncFunctionT] | Callable[[AsyncFunctionT], BetaAsyncFunctionTool[AsyncFunctionT]]c                  r   )aU  Create an AsyncFunctionTool from a function with automatic schema inference.

    Can be used as a decorator with or without parentheses:

    @async_tool
    async def my_func(x: int) -> str: ...

    @async_tool()
    async def my_func(x: int) -> str: ...

    @async_tool(name="custom_name")
    async def my_func(x: int) -> str: ...
    rL   Nr   rC   r   r"   r   c                   r   r   )r   rY   r   r%   r(   r   H  s   z"beta_async_tool.<locals>.decoratorrC   r   r"   r   )r   rM   rN   r   r   r%   r   r(   r   %  s   		r   )
rC   r   r3   rH   rD   rH   rE   rI   r"   r   )
r3   rH   rD   rH   rE   rI   rG   rJ   r"   r   r$   )rC   r   r3   rH   rD   rH   rE   rI   rG   rJ   r"   r   r   )rC   r   r3   rH   rD   rH   rE   rI   rG   rJ   r"   r   )
r3   rH   rD   rH   rE   rI   rG   rJ   r"   r   )rC   r   r3   rH   rD   rH   rE   rI   rG   rJ   r"   r   )<
__future__r   loggingabcr   r   typingr   r   r   r   r	   r
   r   r   r   inspectr   typing_extensionsr   r   rO   r_   r   r^   r   _utilsr   r   _modelsr   
types.betar   r   _utils._utilsr   types.tool_paramr   'types.beta.beta_tool_result_block_paramr   BetaContent	getLoggerr<   logr/   r   r   Functionr   AsyncFunctionr   r!   rA   rB   r   r   r   r   BetaRunnableToolBetaAsyncRunnableToolr%   r%   r%   r(   <module>   s    ,
r	
'

/