§
    éŽ’jh3  ã                   ó   — d Z ddlZddlZddlZddlZddlZddlmZmZ ddl	Z	ddl
mZ ddlmZ dedef         dedz  fd	„Zdedef         d
ededef         fd„Z G d„ d¦  «        Zdedefd„Z G d„ d¦  «        ZdS )uÊ  In-process tool runner for the Google Antigravity SDK.

Tools are Python callables that run directly in the SDK process. The
ToolRunner is a registry and executor â€” it holds references to tool
functions and invokes them by name when requested.

HOW tool calls reach the runner (callback server, direct invocation,
RPC bridge) is a connection strategy concern, not a tool runner concern.

ToolContext injection is handled here: at registration time the runner
inspects each tool's signature for a ``ToolContext``-typed parameter
and caches the result. At execution time, the context is injected
automatically. Schema generation (``get_public_callable``) strips
injectable parameters so the model never sees them.
é    N)ÚAnyÚCallable)Útypes)Útool_contextÚfn.Úreturnc                 óâ  — | }t          |t          ¦  «        r|j        }t          |t          ¦  «        °	 t          j        |¦  «        }n# t
          t          t          f$ r Y dS w xY w|                     ¦   «         D ]r\  }}|dk    rŒ|t          j
        u r|c S t          j        |¦  «        }|t          j        u s|t          j        u r$t          j
        t          j        |¦  «        v r|c S ŒsdS )u½  Returns the name of the ToolContext-typed parameter, if any.

  Uses ``typing.get_type_hints`` to resolve annotations â€” including
  stringified ones from ``from __future__ import annotations`` â€” and
  checks for an exact match against ``ToolContext``.

  Args:
    fn: The callable to inspect. If it's a ``ToolWithSchema``, the inner ``.fn``
      is used.

  Returns:
    The parameter name, or None if no ToolContext parameter is found.
  Nr   )Ú
isinstanceÚToolWithSchemar   ÚtypingÚget_type_hintsÚ	TypeErrorÚ	NameErrorÚAttributeErrorÚitemsÚtool_context_moduleÚToolContextÚ
get_originÚUnionÚ	std_typesÚ	UnionTypeÚget_args)r   ÚtargetÚhintsÚnameÚannÚorigins         úRC:\Projects\DSCons\.venv\Lib\site-packages\google\antigravity\tools\tool_runner.pyÚ_find_context_paramr   +   s
  € ð €&Ý�6�>Ñ*Ô*ð ØŒY€Fõ 	�6�>Ñ*Ô*ð ðÝÔ! &Ñ)Ô)€E€EøÝ
•Y¥Ð	/ð ð ð Øˆ4ˆ4ðøøøð —;’;‘=”=ð 	ð 	�i€dˆCØˆxÒÐØØ
Õ!Ô-Ð-Ð-Ø€k€k€kåÔ˜sÑ#Ô#€FØ•”ÐÐ ­9Ô+>Ð!>Ð!>Ý	Ô	(­F¬O¸CÑ,@Ô,@Ð	@Ð	@ØˆˆˆøØ	ˆs   µA
 Á
A%Á$A%Úcontext_paramc                 óà  ‡ ‡— t          ‰ t          ¦  «        r(t          t          ‰ j        ‰¦  «        ‰ j        ¦  «        S t          j        ‰ ¦  «        }ˆfd„|j                             ¦   «         D ¦   «         }| 	                    |¬¦  «        }t          ‰ ¦  «        r t          j        ‰ ¦  «        ˆ fd„¦   «         }nt          j        ‰ ¦  «        ˆ fd„¦   «         }t          |d|¦  «         |S )a«  Returns a proxy callable with the injectable parameter removed.

  The proxy has the same ``__name__``, ``__doc__``, and a modified
  ``__signature__`` that excludes the ``context_param``. This is used
  for schema generation so the model never sees the injected parameter.

  Args:
    fn: The original callable.
    context_param: Name of the parameter to hide.

  Returns:
    A wrapper callable with a cleaned signature.
  c                 ó&   •— g | ]\  }}|‰k    ¯|‘ŒS © r#   )Ú.0ÚnÚpr    s      €r   ú
<listcomp>z)_make_public_callable.<locals>.<listcomp>d   s'   ø€ ÐKÐKÐK‘d�a˜¸¸]Ò8JÐ8J�Ð8JÐ8JÐ8Jó    )Ú
parametersc               ›   ó$   •K  —  ‰di | ¤Žƒ d {V —†S ©Nr#   r#   ©Úkwargsr   s    €r   Ú_proxyz%_make_public_callable.<locals>._proxyi   s+   øè è € à�2�<�<˜�<�<ÐÐÐÐÐÐÐr(   c                  ó   •—  ‰di | ¤ŽS r+   r#   r,   s    €r   r.   z%_make_public_callable.<locals>._proxyo   s   ø€ àˆRˆ\ˆ\�&ˆ\ˆ\Ðr(   Ú__signature__)r
   r   Ú_make_public_callabler   Úinput_schemaÚinspectÚ	signaturer)   r   ÚreplaceÚ	_is_asyncÚ	functoolsÚwrapsÚsetattr)r   r    ÚsigÚ
new_paramsÚ
public_sigr.   s   ``    r   r1   r1   N   s  øø€ õ  �•NÑ#Ô#ð ÝÝ˜bœe ]Ñ3Ô3°R´_ñô ð õ 	Ô˜"ÑÔ€#ØKÐKÐKÐK˜cœn×2Ò2Ñ4Ô4ÐKÑKÔK€*Ø�{Š{ jˆ{Ñ1Ô1€*åˆr�]„]ð 
å„_�RÑÔð ð  ð  ð  ñ Ôð ð  õ
 „_�RÑÔðð ð ð ñ Ôðõ 
ˆ&�/ :Ñ.Ô.Ð.Ø	€-r(   c                   óR   — e Zd ZdZdedef         deeef         fd„Zdedefd„Z	d	S )
r   z3Wrapper for callables with an explicit JSON Schema.r   .r2   c                 ó¢   — || _         || _        t          |dd ¦  «        pt          |¦  «        j        | _        t          |dd ¦  «        | _        d S )NÚ__name__Ú__doc__)r   r2   ÚgetattrÚtyper?   r@   )Úselfr   r2   s      r   Ú__init__zToolWithSchema.__init__z   sI   € Ø€D„GØ$€DÔÝ˜B 
¨DÑ1Ô1ÐFµT¸"±X´XÔ5F€D„MÝ˜2˜y¨$Ñ/Ô/€D„L€L€Lr(   r-   r   c                 ó   —  | j         di |¤ŽS r+   )r   )rC   r-   s     r   Ú__call__zToolWithSchema.__call__€   s   € Øˆ4Œ7ÐÐ�VÐÐÐr(   N)
r?   Ú
__module__Ú__qualname__r@   r   r   ÚdictÚstrrD   rF   r#   r(   r   r   r   w   so   € € € € € Ø;Ð;ð0˜ # s (Ô+ð 0¸4ÀÀSÀ¼>ð 0ð 0ð 0ð 0ð˜sð  sð ð ð ð ð ð r(   r   Úcallable_objc                 óÎ   — t          | t          ¦  «        rt          | j        ¦  «        S t	          j        | ¦  «        p(t          | d¦  «        ot	          j        | j        ¦  «        S )zGReturns True if the callable is async (coroutine function or __call__).rF   )r
   r   r6   r   r3   ÚiscoroutinefunctionÚhasattrrF   )rK   s    r   r6   r6   „   s_   € å��nÑ-Ô-ð &Ý�\”_Ñ%Ô%Ð%Ý	Ô	$ \Ñ	2Ô	2ð 
Ýˆl˜JÑ'Ô'ð =Ý
Ô
% lÔ&;Ñ
<Ô
<ðr(   c                   óö  — e Zd ZdZddeej                 dz  fd„Zdej	        ddfd„Z
ddej        d	edz  ddfd
„Zd	eddfd„Zedee         fd„¦   «         Zedeeej        f         fd„¦   «         Zdededef         fd„Zdedef         dedefd„Zdedeeef         deeef         fd„Zdedef         deeef         deeef         fd„Zdededefd„Zdeej                 deej                 fd„ZdS )Ú
ToolRunnera  Registry and executor for in-process Python tools.

  Tools are registered by name and executed on demand. Both sync and async
  tools are supported. Tools that declare a ``ToolContext``-typed parameter
  receive the context automatically at execution time.
  NÚtoolsc                 ól   — i | _         d | _        i | _        |r|D ]}|                      |¦  «         Œd S d S ©N)Ú_toolsÚ_contextÚ_context_paramsÚregister)rC   rQ   Útools      r   rD   zToolRunner.__init__–   sZ   € Ø/1€D„KØ<@€D„Mð ,.€DÔØð Øð ð ˆ$Ø�Š�dÑÔÐÐðð ðð r(   Úctxr   c                 ó   — || _         dS )ztSets the ToolContext for injection into tools that request it.

    Args:
      ctx: The ToolContext to inject.
    N)rU   )rC   rY   s     r   Úset_contextzToolRunner.set_context    s   € ð €D„M€M€Mr(   rX   r   c                 óÚ   — |p$t          |dd¦  «        pt          |¦  «        j        }|| j        v rt	          d|› d�¦  «        ‚|| j        |<   t          |¦  «        }|�|| j        |<   dS dS )a•  Registers a tool by name.

    At registration time, the tool's signature is inspected for a
    ``ToolContext``-typed parameter. If found, the parameter name is
    cached for injection at execution time.

    Args:
      tool: The callable to register.
      name: Optional name override. Defaults to tool.__name__.

    Raises:
      ValueError: If a tool with the same name is already registered.
    r?   NúTool 'z' is already registered.)rA   rB   r?   rT   Ú
ValueErrorr   rV   )rC   rX   r   Ú	tool_nameÚ	ctx_params        r   rW   zToolRunner.register¨   s‹   € ð ÐN�  j°$Ñ7Ô7ÐN½4À¹:¼:Ô;N€IØ�D”KÐÐÝÐC 	ÐCÐCÐCÑDÔDÐDØ!€D„K�	Ñõ $ DÑ)Ô)€IØÐØ(1€dÔ˜9Ñ%Ð%Ð%ð Ðr(   c                 ó„   — || j         vrt          d|› d�¦  «        ‚| j         |= | j                             |d¦  «         dS )zžRemoves a tool by name.

    Args:
      name: The name of the tool to remove.

    Raises:
      KeyError: If no tool with the given name is registered.
    r]   ú' is not registered.N)rT   ÚKeyErrorrV   Úpop)rC   r   s     r   Ú
unregisterzToolRunner.unregisterÀ   sT   € ð �4”;ÐÐÝÐ8˜dÐ8Ð8Ð8Ñ9Ô9Ð9ØŒ�DÐØÔ×Ò˜T 4Ñ(Ô(Ð(Ð(Ð(r(   c                 óN   — t          | j                             ¦   «         ¦  «        S )z"The names of all registered tools.)ÚlistrT   Úkeys©rC   s    r   Ú
tool_nameszToolRunner.tool_namesÎ   s    € õ �”× Ò Ñ"Ô"Ñ#Ô#Ð#r(   c                 ó*   — t          | j        ¦  «        S )z*A copy of the registered tools dictionary.)rI   rT   ri   s    r   rQ   zToolRunner.toolsÓ   s   € õ �”ÑÔÐr(   r_   .c                 ó°   — || j         vrt          d|› d�¦  «        ‚| j         |         }| j                             |¦  «        }|€|S t	          ||¦  «        S )añ  Returns a callable with injectable params hidden from the signature.

    Connections use this to generate tool schemas for the model without
    exposing internal-only parameters like ``ToolContext``.

    Args:
      tool_name: The name of the registered tool.

    Returns:
      The original callable if no context parameter, otherwise a proxy
      callable with the context parameter removed from the signature.

    Raises:
      KeyError: If no tool with the given name is registered.
    r]   rb   )rT   rc   rV   Úgetr1   )rC   r_   rX   r`   s       r   Úget_public_callablezToolRunner.get_public_callableØ   sh   € ð  ˜œÐ#Ð#ÝÐ=˜iÐ=Ð=Ð=Ñ>Ô>Ð>ØŒ;�yÔ!€DØÔ$×(Ò(¨Ñ3Ô3€IØÐØ€kÝ   yÑ1Ô1Ð1r(   r   r-   c              ‹   ó¢   K  — t          |¦  «        st          j        |fi |¤Žƒ d{V —†}n |di |¤Ž}t          j        |¦  «        r|ƒ d{V —†S |S )zAExecutes a callable, running sync functions in a separate thread.Nr#   )r6   ÚasyncioÚ	to_threadÚiscoroutine)rC   r   r-   Úresults       r   Ú_execute_fnzToolRunner._execute_fnð   s{   è è € å�R‰=Œ=ð ÝÔ& rÐ4Ð4¨VÐ4Ð4Ð4Ð4Ð4Ð4Ð4Ð4€f€fàˆrˆ|ˆ|�Fˆ|ˆ|€fåÔ˜6Ñ"Ô"ð Øˆ\ˆ\ˆ\ˆ\ˆ\ˆ\ÐØ€Mr(   c                 ón   — | j                              |¦  «        }|�| j        �||vri |¥|| j        i¥S |S )av  Returns kwargs augmented with ToolContext if the tool requests it.

    Returns a new dict when injection occurs; returns the original
    dict unchanged otherwise (no unnecessary copies).

    Args:
      tool_name: The registered tool name.
      kwargs: The original keyword arguments.

    Returns:
      The kwargs dict, potentially augmented with the ToolContext.
    )rV   rm   rU   )rC   r_   r-   r`   s       r   Ú_inject_contextzToolRunner._inject_contextû   sN   € ð Ô$×(Ò(¨Ñ3Ô3€IØÐ ¤Ð!:Ø	˜&Ð	 Ð	 Ø3�&Ð3˜) T¤]Ð3Ð3Ð3Ø€Mr(   c                 ó0  — |}t          |t          ¦  «        r|j        }t          |t          ¦  «        °	 t          j        |¦  «        }n# t
          t          f$ r |cY S w xY w	 t          j        |¦  «        }n# t          t          t          f$ r i }Y nw xY wi }|j                             ¦   «         D ]¿\  }}||vrŒ
|j        t          j        j        t          j        j        fv r||         ||<   Œ?||         }	|                     ||j        ¦  «        }
|
t          j        j        u s|	€|	||<   Œ}	 t)          j        |
¦  «        }|                     |	¦  «        ||<   Œ«# t.          $ r |	||<   Y Œ¼w xY w|                     ¦   «         D ]\  }}||vr|||<   Œ|S )zLCoerces keyword arguments to match function type annotations using pydantic.)r
   r   r   r3   r4   r^   r   r   r   r   r   r)   r   ÚkindÚ	ParameterÚVAR_POSITIONALÚVAR_KEYWORDrm   Ú
annotationÚemptyÚpydanticÚTypeAdapterÚvalidate_pythonÚ	Exception)rC   r   r-   r   r:   r   Úcoercedr   ÚparamÚvalr   ÚadapterÚkÚvs                 r   Ú_coerce_argszToolRunner._coerce_args  só  € ð €FÝ
�V�^Ñ
,Ô
,ð ØŒy€fõ �V�^Ñ
,Ô
,ð ðÝÔ˜fÑ%Ô%€c€cøÝ�	Ð"ð ð ð Ø€m€m€mðøøøðÝÔ# FÑ+Ô+€e€eøÝ•y¥.Ð1ð ð ð Ø€e€e€eðøøøð €GØ”~×+Ò+Ñ-Ô-ð ð ‰ˆˆeØ	�VÐ	Ð	ØØ	ŒÝ
Ô
Ô
*Ý
Ô
Ô
'ðð 
ð 
ð ˜tœˆ�‰ØØ�4ŒL€cØ�IŠI�d˜EÔ,Ñ-Ô-€cØ	•Ô!Ô'Ð	'Ð	'¨3¨;Øˆ�‰ØðÝÔ& sÑ+Ô+ˆØ×/Ò/°Ñ4Ô4ˆ�‰ˆøÝð ð ð Øˆ�‰ˆˆðøøøð —’‘”ð ð ‰ˆˆ1Ø	
�'Ð	Ð	Øˆ�‰
øà€Ns5   µA
 Á
A ÁA Á$A9 Á9BÂBÄ1,EÅE0Å/E0c              ‹   óØ   K  — || j         vrt          d|› d�¦  «        ‚| j         |         }|                      ||¦  «        }|                      ||¦  «        } | j        |fi |¤Žƒ d{V —†S )aŸ  Executes a registered tool by name.

    If the tool declares a ``ToolContext`` parameter and a context has
    been set via ``set_context()``, the context is injected automatically.

    Args:
      tool_name: The name of the tool to execute.
      **kwargs: Arguments to pass to the tool.

    Returns:
      The tool's return value.

    Raises:
      KeyError: If no tool with the given name is registered.
    r]   rb   N)rT   rc   rˆ   rv   rt   )rC   r_   r-   Útool_fns       r   ÚexecutezToolRunner.execute>  s’   è è € ð  ˜œÐ#Ð#ÝÐ=˜iÐ=Ð=Ð=Ñ>Ô>Ð>àŒk˜)Ô$€GØ×Ò˜w¨Ñ/Ô/€FØ×!Ò! )¨VÑ4Ô4€FØ!�Ô! 'Ð4Ð4¨VÐ4Ð4Ð4Ð4Ð4Ð4Ð4Ð4Ð4r(   Ú
tool_callsc              ƒ   ó    ‡ ‡K  — dt           j        dt           j        fˆ fd„Št          t	          j        ˆfd„|D ¦   «         Ž ƒ d{V —†¦  «        S )añ  Executes a batch of tool calls concurrently and returns structured results.

    Tool calls are executed in parallel via ``asyncio.gather``.  Unknown
    tools and execution failures produce ToolResult with an error message
    rather than raising.

    Note: tools execute concurrently; callers must not depend on
    sequential side-effect ordering.

    Args:
      tool_calls: List of ToolCall objects.

    Returns:
      A list of ToolResult, one per input tool call, in the same order.
    Útcr   c           
   “   óÌ  •K  — 	 | j         ‰j        vr6t          j        | j        | j        | j        | j         d| j         › d�¬¦  «        S  ‰j        | j         fi | j        ¤Žƒ d {V —†}t          j        | j        | j        | j        | j         |¬¦  «        S # t          $ rE}t          j        | j        | j        | j        | j         t          |¦  «        |¬¦  «        cY d }~S d }~ww xY w)NzUnknown tool: 'ú')ÚidÚstep_idÚserver_namer   Úerror)r‘   r’   r“   r   rs   )r‘   r’   r“   r   r”   Ú	exception)r   rT   r   Ú
ToolResultr‘   r’   r“   r‹   Úargsr�   rJ   )rŽ   rs   ÚerC   s      €r   Ú_execute_onez3ToolRunner.process_tool_calls.<locals>._execute_onej  s/  øè è € ð
ØŒ7˜$œ+Ð%Ð%ÝÔ!Ø”Ø”jØœ.Ø”7Ø0 b¤gÐ0Ð0Ð0ðñ ô ð ð $�t”| B¤GÐ7Ð7¨r¬wÐ7Ð7Ð7Ð7Ð7Ð7Ð7Ð7ˆÝÔØŒuØ”JØœØ”Øð
ñ 
ô 
ð 	
øõ ð 
ð 
ð 
ÝÔØŒuØ”JØœØ”Ý�a‘&”&Øð
ñ 
ô 
ð 	
ð 	
ð 	
ð 	
ð 	
ð 	
øøøøð
øøøs%   …AB Á	A
B Â
C#Â:CÃC#ÃC#c                 ó&   •— g | ]} ‰|¦  «        ‘ŒS r#   r#   )r$   rŽ   r™   s     €r   r'   z1ToolRunner.process_tool_calls.<locals>.<listcomp>ˆ  s#   ø€ Ð&MÐ&MÐ&M¸B | |°BÑ'7Ô'7Ð&MÐ&MÐ&Mr(   N)r   ÚToolCallr–   rg   rp   Úgather)rC   rŒ   r™   s   ` @r   Úprocess_tool_callszToolRunner.process_tool_callsV  sw   øøè è € ð(
�uœ~ð 
µ%Ô2Bð 
ð 
ð 
ð 
ð 
ð 
õ< •g”nÐ&MÐ&MÐ&MÐ&MÀ*Ð&MÑ&MÔ&MÐNÐNÐNÐNÐNÐNÐNÑOÔOÐOr(   rS   )r?   rG   rH   r@   rg   r   Ú
PythonToolrD   r   r   r[   rJ   rW   re   Úpropertyrj   rI   rQ   r   r   rn   rt   rv   rˆ   r‹   r›   r–   r�   r#   r(   r   rP   rP   Ž   sl  € € € € € ðð ðð ˜D Ô!1Ô2°TÑ9ð ð ð ð ðÐ0Ô<ð Àð ð ð ð ð2ð 2˜5Ô+ð 2°3¸±:ð 2Èð 2ð 2ð 2ð 2ð0)˜Sð ) Tð )ð )ð )ð )ð ð$˜$˜sœ)ð $ð $ð $ñ „8ð$ð ð�T˜#˜uÔ/Ð/Ô0ð ð ð ñ „8ðð2¨3ð 2°8¸CÀ¸HÔ3Eð 2ð 2ð 2ð 2ð0	 (¨3°¨8Ô"4ð 	Àð 	Èð 	ð 	ð 	ð 	ðØðØ$(¨¨c¨¤NðàˆC�ˆH„~ðð ð ð ð*,Ø˜˜c˜Ô"ð,Ø,0°°c°¬Nð,àˆC�ˆH„~ð,ð ,ð ,ð ,ð\5 Sð 5°Cð 5¸Cð 5ð 5ð 5ð 5ð02Pà�u”~Ô&ð2Pð ˆEÔÔð2Pð 2Pð 2Pð 2Pð 2Pð 2Pr(   rP   )r@   rp   r7   r3   r   r   r   r   r   r~   Úgoogle.antigravityÚgoogle.antigravity.toolsr   r   rJ   r   r1   r   Úboolr6   rP   r#   r(   r   ú<module>r£      sˆ  ððð ð  €€€Ø Ð Ð Ð Ø €€€Ø Ð Ð Ð Ø €€€Ø  Ð  Ð  Ð  Ð  Ð  Ð  Ð  à €€€Ø $Ð $Ð $Ð $Ð $Ð $Ø HÐ HÐ HÐ HÐ HÐ Hð ˜H S¨# XÔ.ð  °3¸±:ð  ð  ð  ð  ðF&Ø��c�Ôð&Ø+.ð&àˆc�3ˆhÔð&ð &ð &ð &ðR
ð 
ð 
ð 
ð 
ñ 
ô 
ð 
ð˜Cð  Dð ð ð ð ðzPð zPð zPð zPð zPñ zPô zPð zPð zPð zPr(   