ó
    °"³j%  ã                  óF  • S r SSKJr  SSKrSSKJrJrJrJrJ	r	  SSK
Jr  SSKJr  SSKJrJrJrJr  S	S
KJr  S	SKJr  S	SKJr  S	SKJr  S	SKJrJr  / SQr " S S\5      r  " S S\5      r!\" S\ S9r"SS jr#SS jr$S S jr%S!S jr&      S"S jr'S#S jr(S$S jr)S%S jr*g)&a1  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
`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Úcast)ÚProtocol)Úrun_syncé   )Ú	ToolErrorÚBetaFunctionToolÚBetaBuiltinFunctionToolÚBetaFunctionToolResultTypeé   )ÚBetaMessageParam)ÚBetaCompactionBlock)ÚBetaContentBlockParam)Ú BetaRequestToolRemovalBlockParam)ÚToolÚ!BetaRequestToolAdditionBlockParam)Útool_registryÚtool_error_contentÚrun_runnable_toolÚavailable_tool_namesc                  ó,   • \ rS rSrSr\SS j5       rSrg)Ú
_NamedToolé!   u@   Anything with a `name` â€” the shape `tool_registry` indexes on.c                ó   • g ©N© )Úselfs    Ú_/home/mande/repo/quber/.venv/lib/python3.13/site-packages/anthropic/lib/tools/_tool_dispatch.pyÚnameÚ_NamedTool.name$   s   € Øó    r    N)ÚreturnÚstr)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Úpropertyr#   Ú__static_attributes__r    r%   r"   r   r   !   s   † ÙJàÛó Ûr%   r   c                  ó"   • \ rS rSrSrSS jrSrg)Ú_CallableToolé(   zbA runnable tool: `call` may be sync or async (it returns either the
result or an awaitable of it).c                ó   • g r   r    )r!   Úinputs     r"   ÚcallÚ_CallableTool.call,   s   € Ðorr%   r    N)r3   Úobjectr&   zHUnion[BetaFunctionToolResultType, Awaitable[BetaFunctionToolResultType]])r(   r)   r*   r+   r,   r4   r.   r    r%   r"   r0   r0   (   s   † ñ&÷ sr%   r0   Ú
NamedToolT)Úboundc                óF   • U  Vs0 s H  oR                   U_M     sn$ s  snf )z‡Index `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   2   s"   € ñ
 ).Ó.ª �I‰I�tŠO©Ñ.Ð.ùÒ.s   …c                óô   • [        U5      nU  Hf  nUS   n[        U[        5      (       a  M  U HA  nUS   S:X  a  [        XR5        M  US   S:X  d  M$  [	        U5       H  n[        Xb5        M     MC     Mh     U$ )a×  Fold mid-conversation `tool_removal` / `tool_addition` blocks over
the locally runnable `tool_names`.

These blocks arrive in `role: "system"` messages, and in the
`tool_changes` of a `compaction` block, which stands in for the system
messages of the turns it summarized. A `tool_reference` or a by-value
`tool_definition` can name a locally runnable tool; MCP references
execute server-side, so they (and any unknown block or tool type) are
ignored rather than raising.
ÚcontentÚroleÚsystemÚ	assistant)ÚsetÚ
isinstancer'   Ú_apply_tool_changeÚ_compaction_tool_changes)ÚmessagesÚ
tool_namesÚ	availableÚmessager=   ÚblockÚchanges          r"   r   r   :   sz   € ô �J“€IÛˆØ˜)Ñ$ˆÜ�gœs×#Ñ#ÙÛˆEØ�v‰ (Ó*Ü" 5Ö4Ø˜‘ KÕ/Ü6°uÖ=�FÜ& vÖ9ó >ó	 ñ	 ð Ðr%   c                ó,  • [        U [        5      (       aB  U R                  =(       d    S Vs/ s H!  n[        [        UR                  5       5      PM#     sn$ [        U [        5      (       a#  U S   S:X  a  U R                  S5      =(       d    S$ gs  snf )zÖThe ``tool_changes`` of a ``compaction`` block, whichever shape it has in history.

A caller-written block is a dict; one the runner echoed from a response is
still the response model, with response-model entries.
r    ÚtypeÚ
compactionÚtool_changes)rB   r   rN   r   r   Úto_dictÚdictÚget)rI   rJ   s     r"   rD   rD   S   s|   € ô �%Ô,×-Ñ-ØLQ×L^ÑL^×LdÐbdÐLdÓeÒLdÀ&”Ô*¨F¯N©NÓ,<Ö=ÑLdÑeÐeÜ�%œ×Ñ 5¨¡=°LÓ#@Ø�y‰y˜Ó(×.¨BÐ.Øùò fs   ­(Bc                ól   • [        U [        5      (       d  gU S   S:X  d	  U S   S:X  a  [        X5        gg)zEApply a single `tool_removal` / `tool_addition` block to `available`.NrL   Útool_removalÚtool_addition)rB   rP   Ú_apply_tool_reference_change)rI   rG   s     r"   rC   rC   `   s9   € ä�eœT×"Ñ"ð 	ØˆV�}˜Ó&¨%°©-¸?Ó*JÜ$ UÕ6ð +Kr%   c                ó€   • [        U S   5      nUc  gU S   S:X  a  UR                  U5        gUR                  U5        g)zAFold one `tool_removal` / `tool_addition` block into `available`.r;   NrL   rS   )Ú_changed_tool_nameÚdiscardÚadd)rI   rG   r#   s      r"   rU   rU   k   s@   € ô ˜e F™mÓ,€DØ�|ØØˆV�}˜Ó&Ø×Ñ˜$Õà�‰�dÕr%   c                ó    • U S   S:X  a  U S   $ U S   S:X  a7  [        SU S   5      R                  S5      n[        U[        5      (       a  U$ S$ g)a  The locally runnable tool name a tool-change ``tool`` resolves to.

`tool_reference` names one directly and `tool_definition` carries one
by value; MCP references execute server-side, and unknown types are
ignored (forward compatibility), so those resolve to `None`.
rL   Útool_referencer#   Útool_definitionúdict[str, object]Ú
definitionN)r   rQ   rB   r'   )r;   r#   s     r"   rW   rW   x   s_   € ð ˆF�|Ð'Ó'Ø�F‰|ÐØˆF�|Ð(Ó(äÐ'¨¨lÑ);Ó<×@Ñ@ÀÓHˆÜ! $¬×,Ñ,ˆtÐ6°$Ð6Ør%   c                óZ   • [        U [        5      (       a  U R                  $ [        U 5      $ )züRender an exception raised by a tool as tool-result content.

A `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.
)rB   r   r=   Úrepr)Úexcs    r"   r   r   ˆ   s%   € ô �#”y×!Ñ!Ø�{‰{ÐÜ�‹9Ðr%   c              ƒ  óò   #   • [        U [        [        45      (       a  [        U R                  USS9I Sh  v•N $ U R	                  U5      n[
        R                  " U5      (       a
  UI Sh  v•N $ U$  N< N7f)zÝCall `tool` with `input`, awaiting the result if the tool is async.

Sync tools run on a worker thread. If the caller cancels (for example on a
timeout), the thread is left to finish on its own and its result is
dropped.
T)Úabandon_on_cancelN)rB   r   r   r
   r4   ÚinspectÚisawaitable)r;   r3   Úresults      r"   r   r   ”   sh   é € ô �$Ô)Ô+BÐC×DÑDÜ˜dŸi™i¨À$ÑG×GÐGØ�Y‰Y�uÓ€FÜ×Ò˜6×"Ñ"Ø�|ÐØ€Mñ	 Hñ ùs!   ‚4A7¶A3·5A7Á,A5Á-A7Á5A7)r:   zIterable[NamedToolT]r&   zdict[str, NamedToolT])rE   zIterable[BetaMessageParam]rF   zIterable[str]r&   úset[str])rI   r   r&   zIterable[BetaContentBlockParam])rI   r   rG   rg   r&   ÚNone)rI   zJUnion[BetaRequestToolRemovalBlockParam, BetaRequestToolAdditionBlockParam]rG   rg   r&   rh   )r;   Ú_ToolChangeToolr&   z
str | None)ra   ÚBaseExceptionr&   r   )r;   r0   r3   r]   r&   r   )+r,   Ú
__future__r   rd   Útypingr   r   r   r   r   Útyping_extensionsr	   Úanyio.to_threadr
   Ú_beta_functionsr   r   r   r   Útypes.beta.beta_message_paramr   Ú types.beta.beta_compaction_blockr   Ú#types.beta.beta_content_block_paramr   Ú0types.beta.beta_request_tool_removal_block_paramr   Ú1types.beta.beta_request_tool_addition_block_paramr   ri   r   Ú__all__r   r0   r7   r   r   rD   rC   rU   rW   r   r   r    r%   r"   Ú<module>rv      s¥   ðñ	õ #ã ß <Õ <Ý &å $ç mÓ mÝ =Ý CÝ HÝ `÷ò
 _€ô�ô ôs�Hô sñ �\¨Ñ4€
ô/ôô2
ô7ð
ØUð
Øbjð
à	ô
ôô 	õr%   