§
    éŽ’j2  ã                   ó\   — d 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Z G d„ d¦  «        ZdS )	a§  Stateful conversation session for the Google Antigravity SDK.

Conversation is the Layer 2 session API. It wraps a Connection with:
- Step history accumulation (with compaction index tracking)
- A chat() convenience method (send + collect in one call)
- State introspection (idle, turn count, last response)

Layer 1 (Agent) delegates to Conversation; power users can use
Conversation directly with any ConnectionStrategy.
é    N)ÚAnyÚAsyncIteratorÚSequence)Útypes)Ú
connectioni'  c                   ód  — e Zd ZdZeddœdej        dedee	j
                 dz  fd„Zeej        dej        d	ed          fd
„¦   «         ¦   «         Zde	j        dz  ded	dfd„Zd	ee	j
                 fd„Zd	ee	j        e	j        z           fd„Zd	edz  fd„Z	 d&de	j        dz  ded	e	j        fd„Zed	ee	j
                 fd„¦   «         Zed	efd„¦   «         Z ed	efd„¦   «         Z!ed	ee         fd„¦   «         Z"d'd„Z#d'd„Z$ed	ej        fd„¦   «         Z%ed	e&fd„¦   «         Z'ed	efd„¦   «         Z(ed	e	j)        fd„¦   «         Z*ed	e+ee	j)        f         fd„¦   «         Z,ed	e	j)        dz  fd„¦   «         Z-ed	e	j)        dz  fd„¦   «         Z.ed	e	j/        fd„¦   «         Z0d'd „Z1d'd!„Z2d(d#e3d	e&fd$„Z4d'd%„Z5dS ))ÚConversationzÖStateful session wrapping a single conversation with the agent.

  Accumulates step history, tracks turn start indices and compaction indices,
  and provides convenience methods for common send/receive patterns.
  N)Úmax_history_sizeÚhistoryÚconnr
   r   c                óV  — || _         g | _        g | _        g | _        || _        d| _        |pg D ]e}| j                             |¦  «         |j        t          j	        j
        k    r/| j                             t          | j        ¦  «        dz
  ¦  «         Œf|                      ¦   «          dS )a2  Initializes the conversation with a connection and empty history.

    Args:
      conn: The established connection to the agent backend.
      max_history_size: Maximum number of steps to retain in history.
        When exceeded, the oldest steps are discarded. Set to 0 to
        disable the limit.
    Né   )Ú_connectionÚ_stepsÚ_turn_start_indicesÚ_compaction_indicesÚ_max_history_sizeÚ_turn_start_usageÚappendÚtyper   ÚStepTypeÚ
COMPACTIONÚlenÚ_enforce_max_history)Úselfr   r
   r   Ústeps        úZC:\Projects\DSCons\.venv\Lib\site-packages\google\antigravity\conversation\conversation.pyÚ__init__zConversation.__init__+   s­   € ð €DÔØ$&€D„KØ*,€DÔØ*,€DÔØ-€DÔØ9=€DÔØ�˜2ð >ð >ˆØ
„k×Ò˜ÑÔÐØ	Œ•e”nÔ/Ò	/Ð	/ØÔ ×'Ò'­¨D¬KÑ(8Ô(8¸1Ñ(<Ñ=Ô=Ð=øØ×ÒÑÔÐÐÐó    ÚstrategyÚreturnc                ó¶   K  — |4 ƒd{V —† |                      ¦   «         } | ||j        ¬¦  «        W V — ddd¦  «        ƒd{V —† dS # 1 ƒd{V —†swxY w Y   dS )z¥Creates a new conversation.

    Args:
      strategy: The connection strategy to use to interact with an agent.

    Yields:
      A new Conversation instance.
    N)r   )ÚconnectÚ_initial_history)Úclsr    r   s      r   ÚcreatezConversation.createF   sþ   è è € ð ð 5ð 5ð 5ð 5ð 5ð 5ð 5ð 5Ø×ÒÑÔ€dØˆC�˜dÔ3Ð4Ñ4Ô4Ð4Ð4Ð4Ð4ð5ð 5ð 5ñ 5ô 5ð 5ð 5ð 5ð 5ð 5ð 5ð 5ð 5ð 5ð 5ð 5ð 5ð 5ð 5ð 5ð 5ð 5ð 5ð 5øøøð 5ð 5ð 5ð 5ð 5ð 5s   ‹*AÁ
AÁAÚpromptÚkwargsc              ‹   ó’  K  — | j         j        du rN	 |                      ¦   «         2 3 d{V —†}Œ6 n/# t          $ r" | j                              ¦   «         ƒ d{V —† Y nw xY w| j                             t          | j        ¦  «        ¦  «         | j         j	         
                    ¦   «         | _         | j         j        |fi |¤Žƒ d{V —† dS )a‹  Sends a message to the agent.

    If a turn is still in progress, drains all remaining steps into
    history before sending the new message.  When another coroutine is
    already iterating ``receive_steps()`` (and therefore recording steps
    into history), falls back to ``wait_for_idle()``.

    Args:
      prompt: The user message to send.
      **kwargs: Strategy-specific options.
    FN)r   Úis_idleÚreceive_stepsÚRuntimeErrorÚwait_for_idler   r   r   r   Úcumulative_usageÚ
model_copyr   Úsend)r   r'   r(   Ú_s       r   r0   zConversation.send\   s  è è € ð  ÔÔ 5Ð(Ð(ð/Ø×)Ò)Ñ+Ô+ð 	ð 	ð 	ð 	ð 	ð 	ð 	�!Ø
ð ,Ð+øåð /ð /ð /ð
 Ô×,Ò,Ñ.Ô.Ð.Ð.Ð.Ð.Ð.Ð.Ð.Ð.Ð.ð/øøøð 	Ô×#Ò#¥C¨¬Ñ$4Ô$4Ñ5Ô5Ð5Ø!Ô-Ô>×IÒIÑKÔK€DÔØ
ˆ$Ô
Ô
 Ð
1Ð
1¨&Ð
1Ð
1Ð1Ð1Ð1Ð1Ð1Ð1Ð1Ð1Ð1s   ’0 ¦.¬0 °)AÁAc                óF  K  — | j                              ¦   «         2 3 d{V —†}| j                             |¦  «         |j        t
          j        j        k    r/| j                             t          | j        ¦  «        dz
  ¦  «         |  
                    ¦   «          |W V — Œ„6 dS )a  Receives steps as they complete, blocks until execution is idle.

    Steps are recorded in history as they arrive. The iterator exits once
    the execution turn is complete.

    Returns:
      An async iterator of Step objects.

    Yields:
      Steps as they complete.
    Nr   )r   r+   r   r   r   r   r   r   r   r   r   ©r   r   s     r   r+   zConversation.receive_stepsz   s·   è è € ð Ô&×4Ò4Ñ6Ô6ð ð ð ð ð ð ð ˆdØ
„k×Ò˜ÑÔÐØ	Œ•e”nÔ/Ò	/Ð	/ØÔ ×'Ò'­¨D¬KÑ(8Ô(8¸1Ñ(<Ñ=Ô=Ð=Ø
×ÒÑ!Ô!Ð!Ø€j€j€j€j€jð 7Ð6Ð6s   œB c                ó  K  — t          ¦   «         }|                      ¦   «         2 3 d{V —†}|j        t          j        j        k    }|j        t          j        j        k    }|rV|rT|j	        r#t          j
        |j        |j	        ¬¦  «        W V — |j        r#t          j        |j        |j        ¬¦  «        W V — |j        r@|j        D ]8}|j        �	|j        |vr&|j        �|                     |j        ¦  «         |W V — Œ9ŒÛ6 dS )z×Receives and yields real-time semantic chunks for the current turn.

    Returns:
      An async iterator of Thought, Text, or ToolCall events.

    Yields:
      Thought, Text, or ToolCall events in real-time.
    N)Ú
step_indexÚtext)Úsetr+   Úsourcer   Ú
StepSourceÚMODELÚtargetÚ
StepTargetÚUSERÚthinking_deltaÚThoughtr5   Úcontent_deltaÚTextÚ
tool_callsÚidÚadd)r   Úseen_tool_idsr   Úis_modelÚis_target_userÚcalls         r   Úreceive_chunkszConversation.receive_chunks�   sN  è è € õ "™eœe€MØ×(Ò(Ñ*Ô*ð ð ð ð ð ð ð ˆdØ”¥Ô 0Ô 6Ò6€hØ”{¥eÔ&6Ô&;Ò;€nà	ð 	P�nð 	PàÔð 	Ý”Øœ¨tÔ/Bðñ ô ð ð ð ð ð
 Ôð 	PÝ”
 d¤o¸DÔ<NÐOÑOÔOÐ
OÐ
OÐ
OÐ
Oð 
Œð Ø”Oð 	ð 	ˆDØŒWˆ_ ¤¨}Ð <Ð <ØŒwÐ"Ø×Ò ¤Ñ(Ô(Ð(ØˆJˆJˆJˆJøøð1 +Ð*Ð*s   ¥D c                 óz   — t          | j        ¦  «        D ]%}|j        t          j        j        k    r	|j        c S Œ&dS )z¡Extracts the structured output payload from the most recent FINISH step.

    Returns:
      The parsed JSON structured output object, or None if not found.
    N)Úreversedr   r   r   r   ÚFINISHÚstructured_outputr3   s     r   Úget_last_structured_outputz'Conversation.get_last_structured_output³   sH   € õ ˜œÑ%Ô%ð &ð &ˆØ	Œ•e”nÔ+Ò	+Ð	+ØÔ%Ð%Ð%Ð%ð 
,àˆ4r   c              ‹   ó~   K  —  | j         |fi |¤Žƒ d{V —† t          j        |                      ¦   «         | ¬¦  «        S )a‘  Sends a prompt and returns a streaming ChatResponse instantly.

    This is a unified entry point supporting real-time text delta streaming
    via iteration, or non-blocking deferred accessors for standard reading.

    Args:
      prompt: The user message to send.
      **kwargs: Strategy-specific options.

    Returns:
      A lazy streaming ChatResponse object wrapping the chunk generator.
    N)Úconversation)r0   r   ÚChatResponserI   )r   r'   r(   s      r   ÚchatzConversation.chat¾   sV   è è € ð ˆ$Œ)�FÐ
%Ð
%˜fÐ
%Ð
%Ð%Ð%Ð%Ð%Ð%Ð%Ð%ÝÔ˜d×1Ò1Ñ3Ô3À$ÐGÑGÔGÐGr   c                 ó*   — t          | j        ¦  «        S )z·Returns all steps received across all turns.

    This is the full, uncompacted transcript. Use compaction_indices to
    identify where the model's context window was compacted.
    )Úlistr   ©r   s    r   r   zConversation.historyÔ   s   € õ �”ÑÔÐr   c                 óT   — t          | j        ¦  «        D ]}|j        r	|j        c S ŒdS )z<Returns the content of the most recent final model response.Ú )rK   r   Úis_complete_responseÚcontentr3   s     r   Úlast_responsezConversation.last_responseÝ   s@   € õ ˜œÑ%Ô%ð ð ˆØ	Ô	"ð ØŒ|ÐÐÐðàˆ2r   c                 ó*   — t          | j        ¦  «        S )z=Returns the number of send() calls made on this conversation.)r   r   rU   s    r   Ú
turn_countzConversation.turn_countå   s   € õ ˆtÔ'Ñ(Ô(Ð(r   c                 ó*   — t          | j        ¦  «        S )a$  Step indices where the model's context was compacted.

    Each index corresponds to the position of a compaction step in
    the history list. Steps before these indices may no longer be in
    the model's context window, but remain in the full history for
    transcript and debugging.
    )rT   r   rU   s    r   Úcompaction_indiceszConversation.compaction_indicesê   s   € õ �Ô(Ñ)Ô)Ð)r   c                 óª   — | j                              ¦   «          | j                             ¦   «          | j                             ¦   «          d| _        dS )uÚ   Clears the accumulated step history, turn indices, and compaction indices.

    Use this to free memory during long-running sessions. The conversation
    remains active â€” only the recorded history is discarded.
    N)r   Úclearr   r   r   rU   s    r   Úclear_historyzConversation.clear_historyõ   sP   € ð 	„K×ÒÑÔÐØÔ×"Ò"Ñ$Ô$Ð$ØÔ×"Ò"Ñ$Ô$Ð$Ø!€DÔÐÐr   c                 ó  ‡— | j         rt          | j        ¦  «        | j         k    rdt          | j        ¦  «        | j         z
  Š| j        ‰d…         | _        ˆfd„| j        D ¦   «         | _        ˆfd„| j        D ¦   «         | _        dS dS dS )z4Trims history to max_history_size if a limit is set.Nc                 ó&   •— g | ]}|‰k    ¯|‰z
  ‘ŒS © rd   ©Ú.0ÚiÚoverflows     €r   ú
<listcomp>z5Conversation._enforce_max_history.<locals>.<listcomp>  ó.   ø€ ð "ð "ð "àØ�(Š]ˆ]ð ˆh‰,àˆ]ˆ]r   c                 ó&   •— g | ]}|‰k    ¯|‰z
  ‘ŒS rd   rd   re   s     €r   ri   z5Conversation._enforce_max_history.<locals>.<listcomp>  rj   r   )r   r   r   r   r   )r   rh   s    @r   r   z!Conversation._enforce_max_history   sÀ   ø€ àÔð ¥# d¤kÑ"2Ô"2°TÔ5KÒ"KÐ"KÝ�T”[Ñ!Ô! DÔ$:Ñ:€hØ”K  	 	Ô*€d„kð"ð "ð "ð "àÔ+ð"ñ "ô "€dÔð
"ð "ð "ð "àÔ+ð"ñ "ô "€dÔÐÐðð Ð"KÐ"Kr   c                 ó   — | j         S )u  Returns the underlying Connection transport.

    Intended for advanced use cases that need direct transport access.
    Prefer Conversation or Agent methods for normal interaction â€”
    bypassing the Conversation layer skips history tracking and hook
    dispatch.
    )r   rU   s    r   r   zConversation.connection  s   € ð ÔÐr   c                 ó   — | j         j        S )z=Returns True if the conversation is idle and ready for input.)r   r*   rU   s    r   r*   zConversation.is_idle  s   € ð ÔÔ#Ð#r   c                 ó   — | j         j        S )z3Returns the conversation identifier, if one exists.)r   Úconversation_idrU   s    r   ro   zConversation.conversation_id!  s   € ð ÔÔ+Ð+r   c                 ó>   — | j         j                             ¦   «         S )z@Returns cumulative token usage across all turns in this session.)r   r.   r/   rU   s    r   Útotal_usagezConversation.total_usage&  s   € ð ÔÔ,×7Ò7Ñ9Ô9Ð9r   c                 óR   — d„ | j         j                             ¦   «         D ¦   «         S )zÃReturns a dictionary mapping trajectory ID to cumulative token usage.

    The main agent's trajectory ID matches conversation_id (or 'main').
    Subagents each have a unique trajectory ID.
    c                 ó>   — i | ]\  }}||                      ¦   «         “ŒS rd   )r/   )rf   ÚkÚvs      r   ú
<dictcomp>z2Conversation.trajectory_usages.<locals>.<dictcomp>2  s3   € ð ð ð Ù"˜a ˆˆ1�<Š<‰>Œ>ðð ð r   )r   Útrajectory_usagesÚitemsrU   s    r   rw   zConversation.trajectory_usages+  s5   € ðð Ø&*Ô&6Ô&H×&NÒ&NÑ&PÔ&Pðñ ô ð r   c                 óZ   — | j         €dS | j        j        | j         z
  }|j        dk    rdS |S )z7Returns token usage from the most recent turn, or None.Nr   )r   r   r.   Útotal_token_count)r   Údiffs     r   Úlast_turn_usagezConversation.last_turn_usage6  s=   € ð ÔÐ%ØˆTØÔÔ,¨tÔ/EÑE€DØÔ Ò"Ð"ØˆTØ€Kr   c                 ó   — | j         S )z#Internal alias for last_turn_usage.)r|   rU   s    r   Ú_last_turn_usagezConversation._last_turn_usage@  s   € ð ÔÐr   c                 ó   — | j         j        S )z0Returns the stop reason of the most recent turn.)r   Ú_last_turn_stop_reasonrU   s    r   r€   z#Conversation._last_turn_stop_reasonE  s   € ð ÔÔ2Ð2r   c              ƒ   óH   K  — | j                              ¦   «         ƒ d{V —† dS )z%Cancels the current turn in progress.N)r   ÚcancelrU   s    r   r‚   zConversation.cancelN  s3   è è € à
Ô
×
!Ò
!Ñ
#Ô
#Ð#Ð#Ð#Ð#Ð#Ð#Ð#Ð#Ð#r   c              ƒ   óH   K  — | j                              ¦   «         ƒ d{V —† dS )zBBlocks until the conversation is idle and ready for the next turn.N)r   r-   rU   s    r   r-   zConversation.wait_for_idleR  s3   è è € à
Ô
×
(Ò
(Ñ
*Ô
*Ð*Ð*Ð*Ð*Ð*Ð*Ð*Ð*Ð*r   ç     Àr@Útimeoutc              ƒ   óF   K  — | j                              |¦  «        ƒ d{V —†S )zÁBlocks until the conversation wakes up or the timeout is reached.

    Args:
      timeout: Maximum seconds to wait.

    Returns:
      True if the conversation woke up, False on timeout.
    N)r   Úwait_for_wakeup)r   r…   s     r   r‡   zConversation.wait_for_wakeupV  s1   è è € ð Ô!×1Ò1°'Ñ:Ô:Ð:Ð:Ð:Ð:Ð:Ð:Ð:r   c              ƒ   óH   K  — | j                              ¦   «         ƒ d{V —† dS )zBCloses the connection transport and releases background resources.N)r   Ú
disconnectrU   s    r   r‰   zConversation.disconnecta  s3   è è € à
Ô
×
%Ò
%Ñ
'Ô
'Ð'Ð'Ð'Ð'Ð'Ð'Ð'Ð'Ð'r   )N)r!   N)r„   )6Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú_DEFAULT_MAX_HISTORY_SIZEÚconnection_libÚ
ConnectionÚintr   r   ÚStepr   ÚclassmethodÚ
contextlibÚasynccontextmanagerÚConnectionStrategyr   r&   ÚContentr   r0   r+   ÚStreamChunkÚToolCallrI   rN   rQ   rR   ÚpropertyrT   r   ÚstrrZ   r\   r^   ra   r   r   Úboolr*   ro   ÚUsageMetadatarq   Údictrw   r|   r~   Ú
StopReasonr€   r‚   r-   Úfloatr‡   r‰   rd   r   r   r	   r	   $   s3  € € € € € ðð ð 8Ø-1ð ð  ð  àÔ%ð ð ð	 ð
 ˜œ
Ô# dÑ*ð ð  ð  ð  ð6 ØÔ!ð5àÔ1ð5ð �^Ô$ð5ð 5ð 5ñ "Ô!ñ „;ð5ð(2àŒm˜dÑ"ð2ð ð2ð ð	2ð 2ð 2ð 2ð< =°´Ô#<ð ð ð ð ð&$à�UÔ&¨¬Ñ7Ô8ð$ð $ð $ð $ðL	¨#°©*ð 	ð 	ð 	ð 	ð ,0ðHð HØ”M DÑ(ðHØ;>ðHàÔðHð Hð Hð Hð, ð�t˜EœJÔ'ð ð ð ñ „8ðð ð˜Sð ð ð ñ „8ðð ð)˜#ð )ð )ð )ñ „8ð)ð ð* $ s¤)ð *ð *ð *ñ „8ð*ð	"ð 	"ð 	"ð 	"ðð ð ð ð" ð˜.Ô3ð ð ð ñ „8ðð ð$�tð $ð $ð $ñ „8ð$ð ð,˜sð ,ð ,ð ,ñ „8ð,ð ð:˜5Ô.ð :ð :ð :ñ „8ð:ð ð  c¨5Ô+>Ð&>Ô!?ð ð ð ñ „8ðð ð˜uÔ2°TÑ9ð ð ð ñ „8ðð ð  Ô 3°dÑ :ð  ð  ð  ñ „8ð ð ð3 eÔ&6ð 3ð 3ð 3ñ „8ð3ð$ð $ð $ð $ð+ð +ð +ð +ð	;ð 	;¨5ð 	;¸Tð 	;ð 	;ð 	;ð 	;ð(ð (ð (ð (ð (ð (r   r	   )r�   r”   Útypingr   r   r   Úgoogle.antigravityr   Úgoogle.antigravity.connectionsr   r�   rŽ   r	   rd   r   r   ú<module>r¤      s£   ðð	ð 	ð Ð Ð Ð Ø /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ð /à $Ð $Ð $Ð $Ð $Ð $Ø GÐ GÐ GÐ GÐ GÐ Gð #Ð ð(ð (ð (ð (ð (ñ (ô (ð (ð (ð (r   