
    ÁjH                       d Z ddlmZ ddl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 ddlmZmZ ddlmZ g dZ G d de
      Z G d de
      Z ede      ZddZddZddZ	 	 	 	 	 	 ddZddZ d dZ!d!dZ"y)"a=  Shared tool-dispatch helpers for the tool runners.

Both ``client.beta.messages.tool_runner`` (the Messages tool runner) and
``client.beta.sessions.events.tool_runner`` (the sessions-side
:class:`~anthropic.lib.tools._beta_session_runner.SessionToolRunner`) do the
same three small things: index the supplied tools by name, run a runnable tool
over a JSON input, and turn an exception raised by a tool into tool-result
content. Those steps are factored out here so the two runners stay consistent
instead of each carrying its own copy. Consumed by the runner helpers only.
    )annotationsN)UnionTypeVarIterable	Awaitable)Protocol   )	ToolErrorBetaFunctionToolResultType   )BetaMessageParam)BetaContentBlockParam)Tool BetaRequestToolRemovalBlockParam)!BetaRequestToolAdditionBlockParam)tool_registrytool_error_contentrun_runnable_toolavailable_tool_namesc                  "    e Zd ZdZedd       Zy)
_NamedTooluH   Anything with a ``name`` — the shape :func:`tool_registry` indexes on.c                     y N )selfs    I/tmp/pip-target-k_scjv0h/lib/python/anthropic/lib/tools/_tool_dispatch.pynamez_NamedTool.name!   s        N)returnstr)__name__
__module____qualname____doc__propertyr   r   r   r   r   r      s    R r   r   c                      e Zd ZdZddZy)_CallableToolzhA runnable tool: ``call`` may be sync or async (it returns either the
    result or an awaitable of it).c                     y r   r   )r   inputs     r   callz_CallableTool.call)   s    r   N)r)   objectr   zHUnion[BetaFunctionToolResultType, Awaitable[BetaFunctionToolResultType]])r!   r"   r#   r$   r*   r   r   r   r'   r'   %   s    & sr   r'   
NamedToolT)boundc                B    | D ci c]  }|j                   | c}S c c}w )zIndex ``tools`` by their ``name`` for O(1) dispatch lookup.

    On a duplicate name the later tool wins, matching a plain dict comprehension.
    )r   )toolstools     r   r   r   /   s     
 )..DIItO...s   c                    t        |      }| D ]3  }|d   }|d   dk7  st        |t              r!|D ]  }t        ||        5 |S )uv  Fold mid-conversation ``tool_removal`` / ``tool_addition`` blocks over
    the locally runnable ``tool_names``.

    Only ``role: "system"`` messages carry these blocks, and only a
    ``tool_reference`` can name a locally runnable tool — MCP references are
    executed server-side, so they (and any unknown block/reference type) are
    ignored rather than raising.
    contentrolesystem)set
isinstancer    _apply_tool_change)messages
tool_names	availablemessager2   blocks         r   r   r   7   s_     JI 1)$6?h&*Wc*B 	1Eui0	1	1 r   c                    t        | t              sy| d   dk(  s| d   dk(  rt        | |       y| d   dk(  r(| d   D ]  }|d   dk(  s	|d   dk(  st        ||       ! yy)a6  Apply a single ``tool_removal`` / ``tool_addition`` block to ``available``.

    A ``mid_conv_system`` block's ``content`` is limited by the API schema to
    ``text`` / ``tool_addition`` / ``tool_removal``, so exactly one level is
    walked (no deeper nesting exists); every other block type is a no-op.
    Ntypetool_removaltool_additionmid_conv_systemr2   )r6   dict_apply_tool_reference_change)r<   r:   inners      r   r7   r7   J   s{     eT" 	V}&%-?*J$UI6	v+	+9% 	?EV}.%-?2R,UI>	?
 	r   c                |    t        | d         }|y| d   dk(  r|j                  |       y|j                  |       y)zGFold one ``tool_removal`` / ``tool_addition`` block into ``available``.r0   Nr>   r?   )_referenced_tool_namediscardadd)r<   r:   r   s      r   rC   rC   a   s@     !v/D|V}&$dr   c                    | d   dk(  r| d   S y)a1  The locally runnable tool name a tool-change reference resolves to.

    Only ``tool_reference`` names a runnable tool; ``mcp_tool_reference`` /
    ``mcp_toolset_reference`` execute server-side and unknown reference types
    are ignored (forward compatibility), so all of those resolve to ``None``.
    r>   tool_referencer   Nr   )refs    r   rF   rF   n   s     6{&&6{r   c                P    t        | t              r| j                  S t        |       S )a  Render an exception raised by a tool as tool-result content.

    A :class:`ToolError` carries its own structured content; anything else is
    rendered with ``repr`` (which, unlike ``str``, keeps the exception type).
    The caller owns the ``is_error`` flag and any logging.
    )r6   r
   r2   repr)excs    r   r   r   z   s"     #y!{{9r   c                r   K   | j                  |      }t        j                  |      r
| d{   S |S 7 w)a  Call ``tool`` with ``input``, awaiting the result if the tool is async.

    Bridges the sync (:class:`~anthropic.lib.tools.BetaFunctionTool`) and async
    (:class:`~anthropic.lib.tools.BetaAsyncFunctionTool`) runnable-tool shapes
    behind a single ``await``.
    N)r*   inspectisawaitable)r0   r)   results      r   r   r      s6      YYuF6"|M s   ,757)r/   zIterable[NamedToolT]r   zdict[str, NamedToolT])r8   zIterable[BetaMessageParam]r9   zIterable[str]r   set[str])r<   r   r:   rS   r   None)r<   zJUnion[BetaRequestToolRemovalBlockParam, BetaRequestToolAdditionBlockParam]r:   rS   r   rT   )rK   _ToolChangeReferencer   z
str | None)rN   BaseExceptionr   r   )r0   r'   r)   zdict[str, object]r   r   )#r$   
__future__r   rP   typingr   r   r   r   typing_extensionsr   _beta_functionsr
   r   types.beta.beta_message_paramr   #types.beta.beta_content_block_paramr   0types.beta.beta_request_tool_removal_block_paramr   rU   r   1types.beta.beta_request_tool_addition_block_paramr   __all__r   r'   r,   r   r   r7   rC   rF   r   r   r   r   r   <module>r`      s   	 #  6 6 & B = H c
^ sH s \4
/&.
U
bj
	
		
r   