o
    ^۷i<                     @  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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"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e0d< G dd de1Z2ede/f Z3e
de3dZ4edeeee/f f Z5e
de5dZ6G dd deZ7G dd deZ8G dd de	e% Z9G dd  d e9e4 Z:G d!d" d"e9e6 Z;edGd&d'Z<edddddddddd(	dHd8d'Z<edddddddddd(	dId:d'Z<	dJdddddddddd(	dKd=d'Z<edLd?d@Z=edddddddddd(	dMdAd@Z=edddddddddd(	dNdCd@Z=	dJdddddddddd(	dOdFd@Z=ee:e e7f Z>ee;e e8f Z?dS )P    )annotationsN)ABCabstractmethod)	AnyUnionGenericTypeVarCallableIterable	Coroutinecastoverload)iscoroutinefunction)Literal	TypeAliasoverride)	BaseModel   )_compat)is_dict)cached_property)TypeAdapter)BetaToolParamBetaToolUnionParamBetaCacheControlEphemeralParam)	CallableT)InputSchema)Contentr   BetaFunctionToolResultTypec                      s,   e Zd ZU dZded< d fddZ  ZS )		ToolErrora  Error that can be raised from a tool to return structured content with ``is_error: True``.

    When the tool runner catches this error, it will use the :attr:`content`
    property as the tool result instead of ``repr(exc)``.

    Example::

        raise ToolError([
            {"type": "text", "text": "Error details here"},
            {"type": "image", "source": {"type": "base64", "data": "...", "media_type": "image/png"}},
        ])
    r   contentreturnNonec                   s   t |tr|}n.g }|D ] }|d}|d ur|t| q|d|dd d q|r4d|nd}t | || _d S )Ntext[typeunknown] z
Tool error)
isinstancestrgetappendjoinsuper__init__r    )selfr    messagepartsblockr#   	__class__ Y/home/ubuntu/vllm_env/lib/python3.10/site-packages/anthropic/lib/tools/_beta_functions.pyr/   +   s   


zToolError.__init__)r    r   r!   r"   )__name__
__module____qualname____doc____annotations__r/   __classcell__r6   r6   r4   r7   r      s   
 r   .	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 )BetaBuiltinFunctionToolr!   r   c                 C     d S Nr6   r0   r6   r6   r7   to_dictB      zBetaBuiltinFunctionTool.to_dictinputobjectr   c                 C  rC   rD   r6   r0   rH   r6   r6   r7   callE   rG   zBetaBuiltinFunctionTool.callr*   c                 C      |   }d|v r|d S |d S Nmcp_server_namenamerF   r0   rawr6   r6   r7   rO   H      zBetaBuiltinFunctionTool.nameNr!   r   rH   rI   r!   r   r!   r*   r8   r9   r:   r   rF   rK   propertyrO   r6   r6   r6   r7   rB   A       rB   c                   @  rA   )BetaAsyncBuiltinFunctionToolr!   r   c                 C  rC   rD   r6   rE   r6   r6   r7   rF   Q   rG   z$BetaAsyncBuiltinFunctionTool.to_dictrH   rI   r   c                   s   d S rD   r6   rJ   r6   r6   r7   rK   T   s   z!BetaAsyncBuiltinFunctionTool.callr*   c                 C  rL   rM   rP   rQ   r6   r6   r7   rO   W   rS   z!BetaAsyncBuiltinFunctionTool.nameNrT   rU   rV   rW   r6   r6   r6   r7   rZ   P   rY   rZ   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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 )0BaseFunctionToolr   funcr*   rO   descriptionr   input_schemaN	rO   r]   r^   defer_loadingcache_controlallowed_callerseager_input_streaminginput_examplesstrict
str | None$InputSchema | type[BaseModel] | Noner`   bool | Nonera   %BetaCacheControlEphemeralParam | Nonerb   Tlist[Literal['direct', 'code_execution_20250825', 'code_execution_20260120']] | Nonerc   rd   "Iterable[dict[str, object]] | Nonere   r!   r"   c       	         C  s   t jrtd|| _t|| _|p|j| _|| _	|| _
|| _|| _|	| _|
| _|p-|  | _|d urDt|tr?| | _d S || _d S |  | _d S )N2Tool functions are only supported with Pydantic v2)r   PYDANTIC_V1RuntimeErrorr\   pydanticvalidate_call_func_with_validater8   rO   _defer_loading_cache_control_allowed_callers_eager_input_streaming_input_examples_strict_get_description_from_docstringr]   r)   r%   model_json_schemar^   _create_schema_from_function)r0   r\   rO   r]   r^   r`   ra   rb   rc   rd   re   r6   r6   r7   r/   j   s"   

zBaseFunctionTool.__init__c                 C  s   | j S rD   r\   rE   r6   r6   r7   __call__   s   zBaseFunctionTool.__call__r   c                 C  s   | j | j| jd}| jd ur| j|d< | jd ur| j|d< | jd ur'| j|d< | jd ur1| j|d< | jd ur;| j|d< | jd urE| j|d< |S )N)rO   r]   r^   r`   ra   rb   rc   rd   re   )	rO   r]   r^   rr   rs   rt   ru   rv   rw   )r0   defnr6   r6   r7   rF      s"   











zBaseFunctionTool.to_dictdocstring_parser.Docstringc                 C  s   t | jjpdS )N )docstring_parserparser\   r;   rE   r6   r6   r7   _parsed_docstring   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   )r   short_descriptionlong_description)r0   r]   r6   r6   r7   rx      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>.CustomGenerateJsonSchemar\   Callable[..., Any]parsed_docstringr   r!   r"   c                  s   t    || _|| _d S rD   )r.   r/   _funcr   )r0   r\   r   r4   r6   r7   r/      s   

zXBaseFunctionTool._create_schema_from_function.<locals>.CustomGenerateJsonSchema.__init___args_kwds'CustomGenerateJsonSchema'c                 _  s   | S rD   r6   )r0   r   r   r6   r6   r7   r|      rG   zXBaseFunctionTool._create_schema_from_function.<locals>.CustomGenerateJsonSchema.__call__	arguments'list[ArgumentsParameter]'var_kwargs_schemaCoreSchema | Noner   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 )Nr%   rI   
propertiesr]   )r.   kw_arguments_schemar+   r   r   paramsarg_namer]   )r0   r   r   schemar   paramprop_schemar4   r6   r7   r      s   

zcBaseFunctionTool._create_schema_from_function.<locals>.CustomGenerateJsonSchema.kw_arguments_schema)r\   r   r   r   r!   r"   )r   r   r   r   r!   r   )r   r   r   r   r!   r   )r8   r9   r:   r/   r|   r   r   r=   r6   r6   r4   r7   CustomGenerateJsonSchema   s
    
r   )r\   r   )schema_generator)pydantic_corer   pydantic.json_schemar   r   pydantic_core.core_schemar   r\   r   _adapterjson_schema)r0   r   r   r   r   r   r   r6   r6   r7   rz      s   "z-BaseFunctionTool._create_schema_from_functionTypeAdapter[Any]c                 C  s
   t | jS rD   )r   rq   rE   r6   r6   r7   r      s   
zBaseFunctionTool._adapter)r\   r   rO   rf   r]   rf   r^   rg   r`   rh   ra   ri   rb   rj   rc   rh   rd   rk   re   rh   r!   r"   )r!   r   )r!   r   )r!   r~   rV   )r!   r   )r!   r   )r8   r9   r:   r<   r/   rX   r|   rF   r   r   rx   rz   r   r6   r6   r6   r7   r[   _   s4   
 %


	,r[   c                   @     e Zd ZdddZdS )	BetaFunctionToolrH   rI   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 r6   r   r\   rn   r   	TypeErrorr%   r8   rq   r   r   ro   ValidationError
ValueErrorrO   r0   rH   er6   r6   r7   rK      s   
zBetaFunctionTool.callNrU   r8   r9   r:   rK   r6   r6   r6   r7   r          r   c                   @  r   )	BetaAsyncFunctionToolrH   rI   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   r6   r   r   r6   r6   r7   rK      s   
zBetaAsyncFunctionTool.callNrU   r   r6   r6   r6   r7   r      r   r   r\   r!   BetaFunctionTool[FunctionT]c                 C  rC   rD   r6   r{   r6   r6   r7   	beta_tool  rG   r   r_   rO   rf   r]   r^   rg   r`   rh   ra   ri   rb   rj   rc   rd   rk   re   c       	   
      C  rC   rD   r6   
r\   rO   r]   r^   r`   ra   rb   rc   rd   re   r6   r6   r7   r        2Callable[[FunctionT], BetaFunctionTool[FunctionT]]c        	   	      C  rC   rD   r6   r_   r6   r6   r7   r        FunctionT | NonePBetaFunctionTool[FunctionT] | Callable[[FunctionT], BetaFunctionTool[FunctionT]]c       	      
     @   t jrtdd	 f	dd}
| dur|
| S |
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   fnr>   r!   r   c                      t |  d
S Nr_   )r   r   	rb   ra   r`   r]   rc   rd   r^   rO   re   r6   r7   _makeA     zbeta_tool.<locals>._makeN)r   r>   r!   r   r   rm   rn   r\   rO   r]   r^   r`   ra   rb   rc   rd   re   r   r6   r   r7   r   $     %BetaAsyncFunctionTool[AsyncFunctionT]c                 C  rC   rD   r6   r{   r6   r6   r7   beta_async_toolU  rG   r   c       	   
      C  rC   rD   r6   r   r6   r6   r7   r   Y  r   ACallable[[AsyncFunctionT], BetaAsyncFunctionTool[AsyncFunctionT]]c        	   	      C  rC   rD   r6   r_   r6   r6   r7   r   i  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   r   r@   r!   r   c                   r   r   )r   r   r   r6   r7   r     r   zbeta_async_tool.<locals>._makeN)r   r@   r!   r   r   r   r6   r   r7   r   x  r   )r\   r>   r!   r   )r\   r>   rO   rf   r]   rf   r^   rg   r`   rh   ra   ri   rb   rj   rc   rh   rd   rk   re   rh   r!   r   )rO   rf   r]   rf   r^   rg   r`   rh   ra   ri   rb   rj   rc   rh   rd   rk   re   rh   r!   r   rD   )r\   r   rO   rf   r]   rf   r^   rg   r`   rh   ra   ri   rb   rj   rc   rh   rd   rk   re   rh   r!   r   )r\   r@   r!   r   )r\   r@   rO   rf   r]   rf   r^   rg   r`   rh   ra   ri   rb   rj   rc   rh   rd   rk   re   rh   r!   r   )rO   rf   r]   rf   r^   rg   r`   rh   ra   ri   rb   rj   rc   rh   rd   rk   re   rh   r!   r   )r\   r   rO   rf   r]   rf   r^   rg   r`   rh   ra   ri   rb   rj   rc   rh   rd   rk   re   rh   r!   r   )@
__future__r   loggingabcr   r   typingr   r   r   r   r	   r
   r   r   r   inspectr   typing_extensionsr   r   r   ro   r   r   r   r   _utilsr   r   _modelsr   
types.betar   r   r   _utils._utilsr   types.tool_paramr   'types.beta.beta_tool_result_block_paramr   BetaContent	getLoggerr8   logr*   r   r<   	Exceptionr   Functionr>   AsyncFunctionr@   rB   rZ   r[   r   r   r   r   BetaRunnableToolBetaAsyncRunnableToolr6   r6   r6   r7   <module>   s    ,
 11