§
    éŽ’j:.  ã                  óà  — d Z ddlmZ ddlZddlZddl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 ddlmZ dd	lmZ  G d
„ dej        ¦  «        Z G d„ de¦  «        Z G d„ de¦  «        Z G d„ de¦  «        Z e	d¦  «        Z e	d¦  «        Z G d„ dej        ee         ¦  «        Z G d„ dej        ee         ¦  «        Z G d„ dej        eeef         ¦  «        Zeez  ez  Z G d„ ded         ¦  «        Z G d„ ded         ¦  «        Z  G d„ deej!                 ¦  «        Z" G d „ d!ee#         ¦  «        Z$ G d"„ d#eej%                 ¦  «        Z& G d$„ d%eej'                 ¦  «        Z( G d&„ d'ee)ef         ¦  «        Z* G d(„ d)eeef         ¦  «        Z+ G d*„ d+eej,                 ¦  «        Z-d<d0„Z.d1d2œd=d6„Z/ e/e"¦  «        Z0 e/e&¦  «        Z1 e/e+¦  «        Z2 e/e-¦  «        Z3 e/ed7¬2¦  «        Z4 e/e d7¬2¦  «        Z5 e/e$¦  «        Z6 e/e(¦  «        Z7 e/e*¦  «        Z8 G d8„ d9eej,                 ¦  «        Z9 G d:„ d;eej,                 ¦  «        Z: e/e9¦  «        Z; e/e:¦  «        Z<dS )>aO  Base definitions for Antigravity SDK Hooks v2.

This module defines the interface for Hooks and the standard result types
returned by their lifecycle callbacks.

Hooks can be defined as classes or by using decorators (e.g. `@pre_turn`).
Decorators automatically wrap functions into Hook instances. Wrapped functions
can optionally accept the hook's context:
- By naming the parameter `context`
  (e.g. `async def my_hook(context, data)`)
- By type-hinting it as `HookContext`
  (e.g. `async def my_hook(ctx: HookContext, data)`)
The context can be placed in any position (first or second).

é    )ÚannotationsN)ÚAnyÚGenericÚTypeVar)Útypes)ÚAskQuestionInteractionSpec)Ú
HookResult)ÚQuestionHookResult)Ústatec                  ó&   ‡ — e Zd ZdZddˆ fd„Zˆ xZS )ÚHookContextz&Base context for hooks to share state.NÚparentúHookContext | Nonec                óL   •— t          ¦   «                              |¬¦  «         d S ©N)r   ©ÚsuperÚ__init__)Úselfr   Ú	__class__s     €úLC:\Projects\DSCons\.venv\Lib\site-packages\google\antigravity\hooks\hooks.pyr   zHookContext.__init__3   s$   ø€ Ý	�G„G×Ò˜FÐÑ#Ô#Ð#Ð#Ð#ó    ©N)r   r   ©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__classcell__©r   s   @r   r   r   0   sH   ø€ € € € € Ø.Ð.ð$ð $ð $ð $ð $ð $ð $ð $ð $ð $ð $r   r   c                  ó"   ‡ — e Zd ZdZˆ fd„Zˆ xZS )ÚSessionContextz$Context scoped to an entire session.c                óL   •— t          ¦   «                              d ¬¦  «         d S r   r   )r   r   s    €r   r   zSessionContext.__init__:   s$   ø€ Ý	�G„G×Ò˜DÐÑ!Ô!Ð!Ð!Ð!r   r   r    s   @r   r"   r"   7   s>   ø€ € € € € Ø,Ð,ð"ð "ð "ð "ð "ð "ð "ð "ð "r   r"   c                  ó$   ‡ — e Zd ZdZdˆ fd„Zˆ xZS )ÚTurnContextz Context scoped to a single turn.Úsession_contextr"   c                óL   •— t          ¦   «                              |¬¦  «         d S r   r   )r   r&   r   s     €r   r   zTurnContext.__init__A   s$   ø€ Ý	�G„G×Ò˜OÐÑ,Ô,Ð,Ð,Ð,r   )r&   r"   r   r    s   @r   r%   r%   >   sC   ø€ € € € € Ø(Ð(ð-ð -ð -ð -ð -ð -ð -ð -ð -ð -r   r%   c                  ó$   ‡ — e Zd ZdZdˆ fd„Zˆ xZS )ÚOperationContextz8Context scoped to a specific operation (e.g. tool call).Úturn_contextr%   c                óL   •— t          ¦   «                              |¬¦  «         d S r   r   )r   r*   r   s     €r   r   zOperationContext.__init__H   s$   ø€ Ý	�G„G×Ò˜LÐÑ)Ô)Ð)Ð)Ð)r   )r*   r%   r   r    s   @r   r)   r)   E   sC   ø€ € € € € Ø@Ð@ð*ð *ð *ð *ð *ð *ð *ð *ð *ð *r   r)   ÚTÚRc                  ó4   — e Zd ZdZej        d
d„¦   «         Zd	S )ÚInspectHookz/Read-only, non-blocking hook for observability.Úcontextr   Údatar,   ÚreturnÚNonec              ƒ  ó
   K  — dS )zwRuns the inspection hook.

    Args:
      context: The hook context.
      data: The data to inspect (read-only).
    N© ©r   r0   r1   s      r   ÚrunzInspectHook.runV   s   è è € ð 	€Dr   N)r0   r   r1   r,   r2   r3   ©r   r   r   r   ÚabcÚabstractmethodr7   r5   r   r   r/   r/   S   s<   € € € € € Ø7Ð7àÔð	ð 	ð 	ñ Ôð	ð 	ð 	r   r/   c                  ó4   — e Zd ZdZej        d
d„¦   «         Zd	S )Ú
DecideHookz.Read-only, blocking hook for policy decisions.r0   r   r1   r,   r2   r	   c              ƒ  ó
   K  — dS )z¬Runs the decision hook.

    Args:
      context: The hook context.
      data: The data to make a decision on.

    Returns:
      A HookResult indicating allow/deny.
    Nr5   r6   s      r   r7   zDecideHook.rund   ó   è è € ð 	€Dr   N)r0   r   r1   r,   r2   r	   r8   r5   r   r   r<   r<   a   s<   € € € € € Ø6Ð6àÔð
	ð 
	ð 
	ñ Ôð
	ð 
	ð 
	r   r<   c                  ó4   — e Zd ZdZej        d
d„¦   «         Zd	S )ÚTransformHookz1Modifying, blocking hook for data transformation.r0   r   r1   r,   r2   r-   c              ƒ  ó
   K  — dS )z›Runs the transformation hook.

    Args:
      context: The hook context.
      data: The data to transform.

    Returns:
      The transformed data.
    Nr5   r6   s      r   r7   zTransformHook.runu   r>   r   N)r0   r   r1   r,   r2   r-   r8   r5   r   r   r@   r@   r   s<   € € € € € Ø9Ð9àÔð
	ð 
	ð 
	ñ Ôð
	ð 
	ð 
	r   r@   c                  ó   — e Zd ZdZdS )ÚOnSessionStartHookz Invoked when the session starts.N©r   r   r   r   r5   r   r   rC   rC   Š   s   € € € € € Ø(Ð(à€$r   rC   c                  ó   — e Zd ZdZdS )ÚOnSessionEndHookzInvoked when the session ends.NrD   r5   r   r   rF   rF   �   s   € € € € € Ø&Ð&à€$r   rF   c                  ó   — e Zd ZdZdS )ÚPreTurnHookz´Invoked before a turn starts.

  The `data` parameter receives the user's prompt (types.Content), which can
  be a single string/media object or a list of multimodal primitives.
  NrD   r5   r   r   rH   rH   —   ó   € € € € € ðð ð €$r   rH   c                  ó   — e Zd ZdZdS )ÚPostTurnHookzrInvoked after a turn ends.

  The `data` parameter receives the model's response text for the completed
  turn.
  NrD   r5   r   r   rK   rK   ¡   rI   r   rK   c                  ó   — e Zd ZdZdS )ÚPreToolCallDecideHookaz  Invoked before a tool call to decide if it should proceed.

  The `data` parameter receives the `types.ToolCall` object.
  Hooks can return:
  - `HookResult(allow=True)` to allow execution with original arguments.
  - `HookResult(allow=True, modified_args={...})` to allow execution with
    modified arguments.
  - `HookResult(allow=False, message="...")` to deny execution.
  NrD   r5   r   r   rM   rM   ¬   s   € € € € € ðð ð €$r   rM   c                  ó   — e Zd ZdZdS )ÚPostToolCallHookz�Invoked after a tool call completes.

  The `data` parameter receives the `types.ToolResult` object containing the
  tool execution details.
  NrD   r5   r   r   rO   rO   º   rI   r   rO   c                  ó   — e Zd ZdZdS )ÚOnToolErrorHooka±  Invoked when a tool fails, allowing authors to shape the failure message.

  Receives the raised exception and returns an optional custom error string.
  If a non-empty string is returned, it replaces the default stacktrace or
  error message delivered to the model. If None is returned, default error
  formatting is used. Works symmetrically across built-in and custom tools.

  The hook cannot fix or retry the tool call on its own, but it can guide
  the agent toward a specific resolution.

  To customize failure messages or provide recovery fallbacks for your tool,
  you can either return a custom error string from this hook or do so directly
  within your tool implementation.
  NrD   r5   r   r   rQ   rQ   Ä   s   € € € € € ðð ð €$r   rQ   c                  ó   — e Zd ZdZdS )ÚOnInteractionHookzHook invoked when the agent needs user interaction.

  This is a superset of QuestionHook and handles all user interactions.
  NrD   r5   r   r   rS   rS   Ø   s   € € € € € ðð ð
 €$r   rS   c                  ó   — e Zd ZdZdS )ÚOnCompactionHookzõInvoked when a context compaction event occurs.

  Compaction is triggered by the harness when the context window exceeds the
  configured token threshold. This hook provides an observability point for
  logging, metrics, or UI notifications.
  NrD   r5   r   r   rU   rU   ä   s   € € € € € ðð ð €$r   rU   Úparamúinspect.Parameterr2   Úboolc                óâ   — | j         dk    rdS | j        }|t          j        j        u rdS |t
          k    rdS t          |t          ¦  «        r#|                     d¦  «        d         }|dk    rdS dS )z1Checks if a parameter represents the HookContext.r0   TFú.éÿÿÿÿr   )	ÚnameÚ
annotationÚinspectÚ	ParameterÚemptyr   Ú
isinstanceÚstrÚsplit)rV   r]   Úsimple_names      r   Ú_is_context_parameterre   ò   sƒ   € à
„Z�9ÒÐØˆ4ØÔ€*Ø•7Ô$Ô*Ð*Ð*Øˆ5ð •;ÒÐØˆ4Ý�
�CÑ Ô ð Ø×"Ò" 3Ñ'Ô'¨Ô+€KØ�mÒ#Ð#ØˆTØ	ˆr   T)Ú	pass_dataÚhook_clsú	type[Any]rf   c               óR   ‡‡‡—  G d„ d| ¦  «        Š G d„ d| ¦  «        Šˆˆˆfd„}|S )aÉ  Creates a decorator that wraps a function as a Hook subclass.

  Each decorator-created hook delegates its ``run()`` to the wrapped
  function and remains directly callable for convenience. Both synchronous
  and asynchronous functions are supported.

  If the wrapped function accepts `context` (either by name 'context' or
  by type hint matching HookContext), the HookContext will be passed to it.

  Args:
    hook_cls: The concrete Hook class to subclass.
    pass_data: If True, the wrapped function receives the hook's ``data``
      argument.  If False (e.g. session start/end), it is called with no
      arguments by default.

  Returns:
    A decorator that converts a function into a Hook instance.
  c                  ó&   — e Zd ZdZd„ Zdd„Zd	„ Zd
S )ú3_make_hook_decorator.<locals>._FunctionHookWithDataú;Internal hook implementation wrapping a decorated function.c                óL   — || _         || _        t          j        | |¦  «         d S r   ©ÚfÚ_call_fnÚ	functoolsÚupdate_wrapper©r   ro   Úcall_fns      r   r   z<_make_hook_decorator.<locals>._FunctionHookWithData.__init__  ó)   € Ø€d„fØ€d„mÝÔ˜t QÑ'Ô'Ð'Ð'Ð'r   r0   r   r1   r   r2   c              “  ón   K  — |                       ||¦  «        }t          j        |¦  «        r|ƒ d {V —†S |S r   ©rp   r^   Úisawaitable©r   r0   r1   Úress       r   r7   z7_make_hook_decorator.<locals>._FunctionHookWithData.run!  sC   è è € Ø�MŠM˜' 4Ñ(Ô(€cÝ	Ô	˜SÑ	!Ô	!ð ØˆyˆyˆyˆyˆyˆyÐØ€jr   c              Ÿ  ó\   K  —  | j         |i |¤Ž}t          j        |¦  «        r|ƒ d {V —†S |S r   ©ro   r^   rx   ©r   ÚargsÚkwargsrz   s       r   Ú__call__z<_make_hook_decorator.<locals>._FunctionHookWithData.__call__'  óF   è è € ØˆDŒF�DÐ#˜FÐ#Ð#€cÝ	Ô	˜SÑ	!Ô	!ð ØˆyˆyˆyˆyˆyˆyÐØ€jr   N©r0   r   r1   r   r2   r   ©r   r   r   r   r   r7   r€   r5   r   r   Ú_FunctionHookWithDatark     óL   € € € € € ØEÐEð(ð (ð (ð
ð ð ð ðð ð ð ð r   r„   c                  ó&   — e Zd ZdZd„ Zdd„Zd	„ Zd
S )ú1_make_hook_decorator.<locals>._FunctionHookNoDatarl   c                óL   — || _         || _        t          j        | |¦  «         d S r   rn   rs   s      r   r   z:_make_hook_decorator.<locals>._FunctionHookNoData.__init__0  ru   r   r0   r   r1   r   r2   c              “  ól   K  — |                       |¦  «        }t          j        |¦  «        r|ƒ d {V —†S |S r   rw   ry   s       r   r7   z5_make_hook_decorator.<locals>._FunctionHookNoData.run5  sA   è è € Ø�MŠM˜'Ñ"Ô"€cÝ	Ô	˜SÑ	!Ô	!ð ØˆyˆyˆyˆyˆyˆyÐØ€jr   c              Ÿ  ó\   K  —  | j         |i |¤Ž}t          j        |¦  «        r|ƒ d {V —†S |S r   r|   r}   s       r   r€   z:_make_hook_decorator.<locals>._FunctionHookNoData.__call__;  r�   r   Nr‚   rƒ   r5   r   r   Ú_FunctionHookNoDatar‡   -  r…   r   r‹   c                óÞ  •‡ ‡— t          j        ‰ ¦  «        }t          |j                             ¦   «         ¦  «        }t          |¦  «        }d}t          |¦  «        D ]\  }}t          |¦  «        r|} nŒ|dk    }	 ‰rµ|r‘|dk    ri|dk    r/|                     t          ¦   «         t          ¦   «         ¦  «         n° |j        t          ¦   «         fi ||         j
        t          ¦   «         i¤Ž n||                     t          ¦   «         ¦  «         nZ|                     t          ¦   «         ¦  «         n8|r"|                     t          ¦   «         ¦  «         n|                     ¦   «          n-# t          $ r }t          d‰ j        › d|› �¦  «        |‚d }~ww xY w‰r<|r)|dk    r|dk    r‰ }	n||         j
        Šˆˆ fd„}	nˆ fd„}	nˆ fd„}	 ‰‰ |	¦  «        S |r‰ }
nˆ fd	„}
 ‰‰ |
¦  «        S )
Nr[   é   r   zInvalid signature for hook 'z': c                ó   •—  ‰|fi ‰| i¤ŽS r   r5   )ÚctxÚdÚcontext_nameÚfuncs     €€r   ú<lambda>z9_make_hook_decorator.<locals>.decorator.<locals>.<lambda>n  s   ø€ ¨$¨$¨qÐ*HÐ*H°\À3Ð4GÐ*HÐ*H€ r   c                ó   •—  ‰| ¦  «        S r   r5   ©r�   r�   r’   s     €r   r“   z9_make_hook_decorator.<locals>.decorator.<locals>.<lambda>q  s   ø€ ¨¨¨S©	¬	€ r   c                ó   •—  ‰|¦  «        S r   r5   r•   s     €r   r“   z9_make_hook_decorator.<locals>.decorator.<locals>.<lambda>t  s   ø€  d d¨1¡g¤g€ r   c                ó   •—  ‰¦   «         S r   r5   )r�   r’   s    €r   r“   z9_make_hook_decorator.<locals>.decorator.<locals>.<lambda>|  s   ø€  T T¡V¤V€ r   )r^   Ú	signatureÚlistÚ
parametersÚvaluesÚlenÚ	enumeratere   ÚbindÚobjectr\   Ú	TypeErrorr   )r’   ÚsigÚparamsÚ
num_paramsÚcontext_idxÚiÚpÚis_statefulÚeÚcall_fn_dataÚcall_fn_nodatar‘   r‹   r„   rf   s   `          @€€€r   Ú	decoratorz'_make_hook_decorator.<locals>.decoratorA  sb  øøø€ Ý
Ô
˜DÑ
!Ô
!€CÝ�#”.×'Ò'Ñ)Ô)Ñ*Ô*€FÝ�V‘”€Jð €KÝ˜&Ñ!Ô!ð ð ‰ˆˆ1Ý	˜qÑ	!Ô	!ð ØˆØˆðð  Ò#€KðØ	ð Øð 		Ø˜1Š_ˆ_Ø˜aÒÐØ�hŠh•v‘x”x¥¡¤Ñ*Ô*Ð*Ð*àˆcŒh•v‘x”xÐHÐH F¨;Ô$7Ô$<½f¹h¼hÐ#GÐHÐHÐHÐHà�HŠH•V‘X”XÑÔÐÐà
�(Š(•6‘8”8Ñ
Ô
Ð
Ð
àð 	Ø
�(Š(•6‘8”8Ñ
Ô
Ð
Ð
à
�(Š(‰*Œ*ˆ*øøÝð ð ð ÝØ
>¨¬Ð
>Ð
>¸1Ð
>Ð
>ñô àðøøøøðøøøð
 ð 7Ø	ð .Ø˜Š?ˆ?Ø˜AÒÐØˆLˆLð " +Ô.Ô3ˆLØHÐHÐHÐHÐHˆLˆLð 2Ð1Ð1Ð1ˆ,ˆ,ð .Ð-Ð-Ð-ˆà"Ð" 4¨Ñ6Ô6Ð6ð 
ð ,Øˆˆà+Ð+Ð+Ð+ˆà Ð   ~Ñ6Ô6Ð6s   Á>C/E. Å.
FÅ8FÆFr5   )rg   rf   r«   r‹   r„   s    ` @@r   Ú_make_hook_decoratorr¬     s…   øøø€ ð(ð ð ð ð ˜hñ ô ð ð(ð ð ð ð ˜Hñ ô ð ð(=7ð =7ð =7ð =7ð =7ð =7ð =7ð~ 
Ðr   Fc                  ó   — e Zd ZdZdS )Ú_PreStepHookz;Invoked when a step is first seen in the stream (internal).NrD   r5   r   r   r®   r®   ‘  s   € € € € € ØCÐCÐCÐCr   r®   c                  ó   — e Zd ZdZdS )Ú_PostStepHookz)Invoked when a step completes (internal).NrD   r5   r   r   r°   r°   •  s   € € € € € Ø1Ð1Ð1Ð1r   r°   )rV   rW   r2   rX   )rg   rh   rf   rX   )=r   Ú
__future__r   r9   rq   r^   Útypingr   r   r   Úgoogle.antigravityr   Úgoogle.antigravity.typesr   r	   r
   Úgoogle.antigravity.utilsr   Ústate_moduleÚ
StateStorer   r"   r%   r)   r,   r-   ÚABCr/   r<   r@   ÚHookrC   rF   ÚContentrH   rb   rK   ÚToolCallrM   Ú
ToolResultrO   Ú	ExceptionrQ   rS   ÚSteprU   re   r¬   Úpre_turnÚpre_tool_call_decideÚon_interactionÚon_compactionÚon_session_startÚon_session_endÚ	post_turnÚpost_tool_callÚon_tool_errorr®   r°   Ú	_pre_stepÚ
_post_stepr5   r   r   ú<module>rÊ      s[  ððð ð  #Ð "Ð "Ð "Ð "Ð "à 
€
€
€
Ø Ð Ð Ð Ø €€€Ø (Ð (Ð (Ð (Ð (Ð (Ð (Ð (Ð (Ð (à $Ð $Ð $Ð $Ð $Ð $Ø ?Ð ?Ð ?Ð ?Ð ?Ð ?Ø /Ð /Ð /Ð /Ð /Ð /Ø 7Ð 7Ð 7Ð 7Ð 7Ð 7Ø :Ð :Ð :Ð :Ð :Ð :ð$ð $ð $ð $ð $�,Ô)ñ $ô $ð $ð"ð "ð "ð "ð "�[ñ "ô "ð "ð-ð -ð -ð -ð -�+ñ -ô -ð -ð*ð *ð *ð *ð *�{ñ *ô *ð *ð €GˆC�L„L€Ø€GˆC�L„L€ð	ð 	ð 	ð 	ð 	�#”'˜7 1œ:ñ 	ô 	ð 	ð	ð 	ð 	ð 	ð 	�”˜' !œ*ñ 	ô 	ð 	ð"	ð 	ð 	ð 	ð 	�C”G˜W Q¨ Tœ]ñ 	ô 	ð 	ð" �ZÑ -Ñ/€ðð ð ð ð ˜ TÔ*ñ ô ð ðð ð ð ð �{ 4Ô(ñ ô ð ðð ð ð ð �*˜Uœ]Ô+ñ ô ð ðð ð ð ð �;˜sÔ#ñ ô ð ðð ð ð ð ˜J u¤~Ô6ñ ô ð ðð ð ð ð �{ 5Ô#3Ô4ñ ô ð ðð ð ð ð �m I¨s NÔ3ñ ô ð ð(ð ð ð ð ØÐ,Ð.@Ð@ÔAñô ð ðð ð ð ð �{ 5¤:Ô.ñ ô ð ðð ð ð ð& DHð {ð {ð {ð {ð {ð {ð@  Ð Ñ,Ô,€Ø+Ð+Ð,AÑBÔBÐ Ø%Ð%Ð&7Ñ8Ô8€Ø$Ð$Ð%5Ñ6Ô6€Ø'Ð'Ð(:ÀeÐLÑLÔLÐ Ø%Ð%Ð&6À%ÐHÑHÔH€Ø Ð  Ñ.Ô.€	Ø%Ð%Ð&6Ñ7Ô7€Ø$Ð$ _Ñ5Ô5€ðDð Dð Dð Dð D�;˜uœzÔ*ñ Dô Dð Dð2ð 2ð 2ð 2ð 2�K ¤
Ô+ñ 2ô 2ð 2ð !Ð  Ñ.Ô.€	Ø!Ð! -Ñ0Ô0€
€
€
r   