ó
    °"³jþš  ã                  óø   • S r SSKJr  SSKJrJr  SSKJrJ	r	J
r
  SSKJrJrJr  SSKJr  SSKJrJrJrJrJrJrJrJrJrJrJrJrJr  SS	KJ r!  \(       a  SS
K"J#r#  \r$ \\-  r% \" S\%S9r&\ " S S5      5       r'g)aá  This module provides functionality to manage and update parts of a model's streamed response.

The manager tracks which parts (in particular, text and tool calls) correspond to which
vendor-specific identifiers (e.g., `index`, `tool_call_id`, etc., as appropriate for a given model),
and produces Pydantic AI-format events as appropriate for consumers of the streaming APIs.

The "vendor-specific identifiers" to use depend on the semantics of the responses of the responses from the vendor,
and are tightly coupled to the specific model being used, and the Pydantic AI Model subclass implementation.

This `ModelResponsePartsManager` is used in each of the subclasses of `StreamedResponse` as a way to consolidate
event-emitting logic.
é    )Úannotations)ÚHashableÚIterator)Ú	dataclassÚfieldÚreplace)ÚTYPE_CHECKINGÚAnyÚTypeVar)ÚUnexpectedModelBehavior)ÚModelResponsePartÚModelResponseStreamEventÚNativeToolCallPartÚPartDeltaEventÚPartStartEventÚProviderDetailsDeltaÚTextPartÚTextPartDeltaÚThinkingPartÚThinkingPartDeltaÚToolCallPartÚToolCallPartDeltaÚToolPartKindé   )Úgenerate_tool_call_id)ÚModelRequestParametersÚPartT)Úboundc                  ó  • \ rS rSr% SrS\S'    \" \\   SS9r	S\S'    \" \
\\4   SS9rS	\S
'    \" \
\\\   4   SSSS9rS\S'    \" \
\\4   SSS9rS\S'    S4S jrS5S jrS6S jrS7S jrS8S jrS9S jrS:S jrSSSSSS.               S;S jjrSSSSSS.             S<S jjrSSSSSS.             S=S jjrSSSSS.               S>S  jjr      S?S! jrS@S" jrSASBS# jjr        SCS$ jr           SDS% jr!          SES& jr"        SFS' jr#SGS( jr$SHS) jr%SHS* jr&SIS+ jr'SJS, jr(SKS- jr)        SLS. jr*            SMS/ jr+SNS0 jr,      SOS1 jr-SPS2 jr.S3r/g)QÚModelResponsePartsManageré9   z¯Manages a sequence of parts that make up a model's streamed response.

Parts are generally added and/or updated by providing deltas, which are tracked by vendor-specific IDs.
r   Úmodel_request_parametersF)Údefault_factoryÚinitúlist[ManagedPart]Ú_partszdict[VendorId, int]Ú_vendor_id_to_part_index)r#   r$   ÚreprÚcomparezdict[int, list[str]]Ú_string_buffers)r#   r$   r(   zdict[str, ToolPartKind]Ú_tool_kind_by_namec                ó¬   • U R                   R                   Vs0 s H(  oR                  c  M  UR                  UR                  _M*     snU l        g s  snf ©N)r"   Úfunction_toolsÚ	tool_kindÚnamer+   )ÚselfÚtds     ÚW/home/mande/repo/quber/.venv/lib/python3.13/site-packages/pydantic_ai/_parts_manager.pyÚ__post_init__Ú'ModelResponsePartsManager.__post_init__R   sF   € à,0×,IÑ,I×,XÒ,Xó#
Ú,X b×\hÑ\hÓ!ˆB�G‰G�R—\‘\Ò!Ñ,Xñ#
ˆÕùò #
s
   ™A­Ac                óŽ   • [        U 5      R                   SU R                  < SU R                  5       < SU R                  < S3$ )Nz(model_request_parameters=z	, _parts=z, _vendor_id_to_part_index=Ú))ÚtypeÚ__qualname__r"   Ú_materialized_partsr'   )r1   s    r3   Ú__repr__Ú"ModelResponsePartsManager.__repr__W   sV   € ä�D‹z×&Ñ&Ð'ð ((Ø(,×(EÑ(EÑ'Hð IØ×.Ñ.Ó0Ñ3ð 4(Ø(,×(EÑ(EÑ'HÈðKð	
ó    c                ó8  • UR                   U R                   L a|  [        U[        5      (       d   eU R                  U R	                  5       U R
                  U R                  4UR                  UR	                  5       UR
                  UR                  4:H  $ [        $ r-   )Ú	__class__Ú
isinstancer    r"   r:   r'   r+   ÚNotImplemented)r1   Úothers     r3   Ú__eq__Ú ModelResponsePartsManager.__eq___   s’   € Ø�?‰?˜dŸn™nÒ,Ü˜eÔ%>×?Ñ?Ð?Ð?à×-Ñ-Ø×(Ñ(Ó*Ø×-Ñ-Ø×'Ñ'ð	ð ×.Ñ.Ø×)Ñ)Ó+Ø×.Ñ.Ø×(Ñ(ð	ñ
ð 
ô Ðr=   c                ó8   • U R                   R                  U5      $ r-   )r+   Úget)r1   Ú	tool_names     r3   Ú_tool_kind_forÚ(ModelResponsePartsManager._tool_kind_foro   s   € Ø×&Ñ&×*Ñ*¨9Ó5Ð5r=   c                óˆ   • UR                   b  U$ U R                  UR                  5      nUc  U$ [        R                  " XS9$ )zµPromote a base `ToolCallPart` to a typed subclass via `ToolDefinition.tool_kind`.

Safe no-op for unknown tool names (model hallucinations) and for tool defs
without a `tool_kind`.
)r/   )r/   rH   rG   r   Únarrow_type)r1   ÚpartÚkinds      r3   Ú_typed_call_partÚ*ModelResponsePartsManager._typed_call_partr   sB   € ð �>‰>Ñ%ØˆKØ×"Ñ" 4§>¡>Ó2ˆØ‰<ØˆKÜ×'Ò'¨Ñ=Ð=r=   c                ó  • [        U R                  5       H8  n[        U R                  U   [        5      (       a  M'  U R                  U5        M:     U R                   Vs/ s H  n[        U[        5      (       a  M  UPM     sn$ s  snf )z¯Return only model response parts that are complete (i.e., not ToolCallPartDelta's).

Returns:
    A list of ModelResponsePart objects. ToolCallPartDelta objects are excluded.
)Útupler*   r@   r&   r   Ú_materialize_and_cache_part)r1   Ú
part_indexÚps      r3   Ú	get_partsÚ#ModelResponsePartsManager.get_parts   sf   € ô   × 4Ñ 4Ö5ˆJÜ˜dŸk™k¨*Ñ5Ô7H×IÓIØ×0Ñ0°Ö<ñ 6ð  Ÿ;š;ÓOš;�a¬j¸Ô<M×.N—™;ÑOÐOùÒOs   Á BÁ=Bc                ób   • U R                   R                  U5      nUb  U R                  U5      $ g)z§Return a part by its vendor ID.

Args:
    vendor_id: The vendor-specific ID of the part.

Returns:
    The part corresponding to the vendor ID, or None if not found.
N)r'   rF   rR   )r1   Ú	vendor_idrS   s      r3   Úget_part_by_vendor_idÚ/ModelResponsePartsManager.get_part_by_vendor_idŠ   s4   € ð ×2Ñ2×6Ñ6°yÓAˆ
ØÑ!Ø×3Ñ3°JÓ?Ð?Ør=   N)ÚidÚprovider_nameÚprovider_detailsÚthinking_tagsÚignore_leading_whitespacec             #  ó  #   • SnUc  U R                  [        5      nO¸U R                  R                  U5      n	U	bš  U R                  U	   n
U(       aK  [        U
[        5      (       a6  X&S   :X  a  U R                  U5        gU R                  X©X$U5       Sh  v•N   g[        U
[        5      (       a  X©4nO U R                  U	5      n
[        SU
< 35      eU(       a#  X&S   :X  a  U R                  XU5       Sh  v•N   gUcS  U(       a%  [        U5      S:X  d  UR                  5       (       a  g[        X#XES9nU R                  X±5      n[        XËS9v •  gUu  pÙ[!        UU R#                  XÔ5      US9nUR$                  SL=(       d$    UR&                  SL=(       d    UR&                  0 :H  nU R)                  XÞUS9nU(       a  U R+                  X�R,                  U5        UU R                  U	'   [/        XžS	9v •  g GNd GN7f)
a  Handle incoming text content, creating or updating a TextPart in the manager as appropriate.

When `vendor_part_id` is None, the latest part is updated if it exists and is a TextPart;
otherwise, a new TextPart is created. When a non-None ID is specified, the TextPart corresponding
to that vendor ID is either created or updated.

Args:
    vendor_part_id: The ID the vendor uses to identify this piece
        of text. If None, a new part will be created unless the latest part is already
        a TextPart.
    content: The text content to append to the appropriate TextPart.
    id: An optional id for the text part.
    provider_name: An optional provider name for the text part.
    provider_details: An optional dictionary of provider-specific details for the text part.
    thinking_tags: If provided, will handle content between the thinking tags as thinking parts.
    ignore_leading_whitespace: If True, will ignore leading whitespace in the content.

Yields:
    A `PartStartEvent` if a new part was created, or a `PartDeltaEvent` if an existing part was updated.
    Yields nothing if no event should be emitted (e.g., the first text part was all whitespace).

Raises:
    UnexpectedModelBehavior: If attempting to apply text content to a part that is not a TextPart.
Nr   z+Cannot apply a text delta to existing_part=r   )Úcontentr[   r\   r]   ©ÚindexrL   ©Úcontent_deltar\   r]   ©Úapply_metadata©rc   Údelta)Ú_latest_part_if_of_typer   r'   rF   r&   r@   r   Ú_handle_embedded_thinking_endÚ!_handle_embedded_thinking_contentrR   r   Ú_handle_embedded_thinking_startÚlenÚisspaceÚ_append_partr   r   Ú_resolve_provider_namer\   r]   Ú(_apply_metadata_or_copy_provider_detailsÚ_buffer_string_deltara   r   )r1   Úvendor_part_idra   r[   r\   r]   r^   r_   Úexisting_text_part_and_indexrS   Úexisting_partrL   Únew_part_indexÚexisting_text_partÚ
part_deltarg   Úupdated_parts                    r3   Úhandle_text_deltaÚ+ModelResponsePartsManager.handle_text_delta˜   s  é € ðF EIÐ$àÑ!à+/×+GÑ+GÌÓ+QÑ(ð ×6Ñ6×:Ñ:¸>ÓJˆJØÑ%Ø $§¡¨JÑ 7�æ ¤Z°¼|×%LÑ%Là°Ñ"2Ó2à×:Ñ:¸>ÔJØØ#×EÑEØ%°7ÐK[ó ÷ ð ð Ü ¬x×8Ñ8Ø3@Ð3LÑ0à$(×$DÑ$DÀZÓ$P�MÜ1Ð4`ÐR_ÑQaÐ2bÓcÐcæ˜W°aÑ(8Ó8à×;Ñ;¸NÐ[kÓl×lÐlØà'Ñ/ö )¬c°'«l¸aÓ.?À7Ç?Á?×CTÑCTØô  GÀ-ÑsˆDØ!×.Ñ.¨tÓDˆNÜ  ~ÑAÓAð .JÑ*Ðä&Ø%Ø"×9Ñ9Ð:LÓ\Ø!1ñˆJð ×(Ñ(°Ð4÷ =Ø×.Ñ.°dÐ:÷=à%×6Ñ6¸"Ñ<ð ð
  ×HÑHØ"¸~ð Ið ˆLö Ø×)Ñ)¨*×6PÑ6PÐRYÔZØ&2ˆD�K‰K˜
Ñ#Ü  zÑDÓDò[ò mùs&   ‚BHÂG;ÂA"HÃ:G>Ã;DHÇ>H©ra   r[   Ú	signaturer\   r]   c             #  ó†  #   • SnUc  U R                  [        5      nOeU R                  R                  U5      nUbG  U R                  U   n	[        U	[        5      (       d   U R                  U5      n	[        SU	< 35      eX˜4nUca  Uc  Uc  UbM  [        U5      (       a  U" S5      OUn
[        U=(       d    SUUUU
S9nU R                  X±5      n[        XËS9v •  g[        S5      eUu  pØUc
  Uc  Uc  Uc  g[        UUU R                  XÕ5      US9nUSL=(       d/    UR                  SL=(       d    USL=(       d    UR                  0 :H  nU(       až  [        U5      (       aŽ  U R                  R                  U5      nUb  [!        U5      OSnU" UR                  5      n
[#        USU
S	9nUR%                  U5      nUc  U R                  R'                  US5        O%UUS2	 UU R                  U'   OU R)                  XÞUS
9nU(       a  U R+                  UUR,                  U5        UU R                  U'   [/        XŽS9v •  g7f)a  Handle incoming thinking content, creating or updating a ThinkingPart in the manager as appropriate.

When `vendor_part_id` is None, the latest part is updated if it exists and is a ThinkingPart;
otherwise, a new ThinkingPart is created. When a non-None ID is specified, the ThinkingPart corresponding
to that vendor ID is either created or updated.

Args:
    vendor_part_id: The ID the vendor uses to identify this piece
        of thinking. If None, a new part will be created unless the latest part is already
        a ThinkingPart.
    content: The thinking content to append to the appropriate ThinkingPart.
    id: An optional id for the thinking part.
    signature: An optional signature for the thinking content.
    provider_name: An optional provider name for the thinking part.
    provider_details: Either a dict of provider-specific details, or a callable that takes
        the existing part's `provider_details` and returns the updated details. Callables
        allow provider-specific update logic without the parts manager knowing the details.

Yields:
    A `PartStartEvent` if a new part was created, or a `PartDeltaEvent` if an existing part was updated.

Raises:
    UnexpectedModelBehavior: If attempting to apply a thinking delta to a part that is not a ThinkingPart.
Nz/Cannot apply a thinking delta to existing_part=Ú r}   rb   zLCannot create a ThinkingPart with no content, signature, or provider_details)re   Úsignature_deltar\   r]   r   )re   r]   rf   rh   )rj   r   r'   rF   r&   r@   rR   r   Úcallablerp   r   r   rq   r\   r]   r*   rn   r   ÚapplyÚpoprr   rs   ra   r   )r1   rt   ra   r[   r~   r\   r]   Ú existing_thinking_part_and_indexrS   rv   Úresolved_detailsrL   rw   Úexisting_thinking_partry   rg   ÚbufferÚbuffer_lengthÚmetadata_deltarz   s                       r3   Úhandle_thinking_deltaÚ/ModelResponsePartsManager.handle_thinking_deltaû   s‰  é € ðD MQÐ(àÑ!à/3×/KÑ/KÌLÓ/YÑ,ð ×6Ñ6×:Ñ:¸>ÓJˆJØÑ%Ø $§¡¨JÑ 7�Ü! -´×>Ñ>Ø$(×$DÑ$DÀZÓ$P�MÜ1Ð4dÐVcÑUeÐ2fÓgÐgØ3@Ð3LÐ0à+Ñ3ØÑ" iÑ&;Ð?OÑ?[ô >FÐFV×=WÑ=WÑ#3°DÔ#9Ð]mÐ Ü#Ø#ŸM rØØ'Ø"/Ø%5ñ�ð "&×!2Ñ!2°4Ó!H�Ü$¨>ÑEÓEä-Øbóð ð 2RÑ.Ð"ð ‰ 9Ñ#4¸Ñ9NÐScÑSkØä*Ø%Ø )Ø"×9Ñ9Ð:PÓ`Ø!1ñ	ˆJð  Ð%÷ AØ×+Ñ+°4Ð7÷Aà#¨4Ð/÷Að *×:Ñ:¸bÑ@ð	 ö ¤(Ð+;×"<Ñ"<Ø×-Ñ-×1Ñ1°*Ó=�Ø/5Ñ/A¤ F¤Àq�Ù#3Ð4J×4[Ñ4[Ó#\Ð Ü!(¨À4ÐZjÑ!k�Ø-×3Ñ3Ð4JÓK�Ø‘>Ø×(Ñ(×,Ñ,¨Z¸Õ>à˜}™~Ð.Ø7=�D×(Ñ(¨Ò4à#×LÑLØ*À~ð  Mð  �ö Ø×)Ñ)¨*°l×6JÑ6JÈGÔTØ&2ˆD�K‰K˜
Ñ#Ü  zÑDÓDùs   ‚H?I)rG   ÚargsÚtool_call_idr\   r]   c          	     óø  • SnUc#  Uc  U R                  [        [        [        5      nOsU R                  R                  U5      nUbU  U R                  U   n	[        U	[        [        -  [        -  5      (       d   U R                  U5      n	[        SU	< 35      eX˜4nUcƒ  [        UUUUUS9n
U
R                  5       =(       d    U
n[        U[        5      (       a  U R                  U5      nU R                  X±5      n[        U[        [        -  5      (       a	  [        XËS9$ gUu  p˜[        UUUU R                  X•5      US9n
U R                  R                  U5      nUb  [!        U5      OSn U R#                  X‰U
5      n[        U[        5      (       a  U R                  U5      nXðR                  U'   [        U[        [        -  5      (       a\  [        U	[        5      (       a	  [        X�S9$ UR(                  (       a$  U
R(                  (       d  [+        X¯R(                  S9n
[-        XŠS9$ g! [$         aB    X�R                  U'   Uc  U R                  R'                  US5        e XÞS2	 XÐR                  U'   e f = f)a€  Handle or update a tool call, creating or updating a `ToolCallPart`, `NativeToolCallPart`, or `ToolCallPartDelta`.

Managed items remain as `ToolCallPartDelta`s until they have at least a tool_name, at which
point they are upgraded to `ToolCallPart`s.

If `vendor_part_id` is None, updates the latest matching ToolCallPart (or ToolCallPartDelta)
if any. Otherwise, a new part (or delta) may be created.

Args:
    vendor_part_id: The ID the vendor uses for this tool call.
        If None, the latest matching tool call may be updated.
    tool_name: The name of the tool. If None, the manager does not enforce
        a name match when `vendor_part_id` is None.
    args: Arguments for the tool call, either as a string, a dictionary of key-value pairs, or None.
    tool_call_id: An optional string representing an identifier for this tool call.
    provider_name: An optional provider name for the tool call part.
    provider_details: An optional dictionary of provider-specific details for the tool call part.

Returns:
    - A `PartStartEvent` if a new ToolCallPart or NativeToolCallPart is created.
    - A `PartDeltaEvent` if an existing part is updated.
    - `None` if no new event is emitted (e.g., the part is still incomplete).

Raises:
    UnexpectedModelBehavior: If attempting to apply a tool call delta to a part that is not
        a ToolCallPart, NativeToolCallPart, or ToolCallPartDelta.
Nz0Cannot apply a tool call delta to existing_part=)Útool_name_deltaÚ
args_deltarŽ   r\   r]   rb   r   )rŽ   rh   )rj   r   r   r   r'   rF   r&   r@   rR   r   Úas_partrN   rp   r   rq   r*   rn   Ú_apply_tool_call_deltaÚ	Exceptionr„   rŽ   r   r   )r1   rt   rG   r�   rŽ   r\   r]   Ú existing_matching_part_and_indexrS   rv   ri   rL   rw   rˆ   r‰   rz   s                   r3   Úhandle_tool_call_deltaÚ0ModelResponsePartsManager.handle_tool_call_deltaf  s  € ðL ð 	)ð Ñ!ð Ñ Ø37×3OÑ3OÜ Ô"4Ô6Gó4Ð0øð
 ×6Ñ6×:Ñ:¸>ÓJˆJØÑ%Ø $§¡¨JÑ 7�Ü! -Ô1BÄ\Ñ1QÔTfÑ1f×gÑgØ$(×$DÑ$DÀZÓ$P�MÜ1Ð4eÐWdÑVfÐ2gÓhÐhØ3@Ð3LÐ0à+Ñ3ä%Ø )ØØ)Ø+Ø!1ñˆEð —=‘=“?×+ eˆDÜ˜$¤×-Ñ-Ø×,Ñ,¨TÓ2�Ø!×.Ñ.¨tÓDˆNä˜$¤Ô/AÑ A×BÑBÜ%¨NÑFÐFð Cð )IÑ%ˆMÜ%Ø )ØØ)Ø"×9Ñ9¸-ÓWØ!1ñˆEð ×)Ñ)×-Ñ-¨jÓ9ˆFØ+1Ñ+=œC œKÀ1ˆMðØ#×:Ñ:¸:ÐV[Ó\�Ü˜l¬L×9Ñ9Ø#'×#8Ñ#8¸Ó#F�Lð '3�K‰K˜
Ñ#Ü˜,¬Ô7IÑ(I×JÑJÜ˜mÔ->×?Ñ?ä)°
ÑNÐNð $×0×0¸×9K×9KÜ '¨×<UÑ<UÑ V˜Ü)°
ÑHÐHð Køô ó Ø*7—‘˜JÑ'Ø‘>Ø×(Ñ(×,Ñ,¨Z¸Ô>ð ð ™~Ð.Ø7=×(Ñ(¨Ñ4Øðús   Å.8H- È-AI9)rŽ   r[   r\   r]   c          	     ó’  • [        UUU=(       d
    [        5       UUUS9nU R                  U5      nUc  U R                  U5      n	OzU R                  R                  U5      n
U
b  U R                  U
   OSnU
b)  [        U[         5      (       a  U
n	U R                  X˜5        OU R                  U5      n	X�R                  U'   [        X˜S9$ )a{  Immediately create or fully-overwrite a ToolCallPart with the given information.

This does not apply a delta; it directly sets the tool call part contents.

Args:
    vendor_part_id: The vendor's ID for this tool call part. If not
        None and an existing part is found, that part is overwritten.
    tool_name: The name of the tool being invoked.
    args: The arguments for the tool call, either as a string, a dictionary, or None.
    tool_call_id: An optional string identifier for this tool call.
    id: An optional identifier for this tool call part.
    provider_name: An optional provider name for the tool call part.
    provider_details: An optional dictionary of provider-specific details for the tool call part.

Returns:
    ModelResponseStreamEvent: A `PartStartEvent` indicating that a new tool call part
    has been added to the manager, or replaced an existing part.
)rG   r�   rŽ   r[   r\   r]   Nrb   )
r   Ú_generate_tool_call_idrN   rp   r'   rF   r&   r@   Ú_replace_partr   )r1   rt   rG   r�   rŽ   r[   r\   r]   Únew_partrw   Úmaybe_part_indexrv   s               r3   Úhandle_tool_call_partÚ/ModelResponsePartsManager.handle_tool_call_partÔ  sÏ   € ô:  ØØØ%×AÔ)?Ó)AØØ'Ø-ñ
ˆð ×(Ñ(¨Ó2ˆØÑ!à!×.Ñ.¨xÓ8‰Nð  $×<Ñ<×@Ñ@ÀÓPÐØ=MÑ=Y˜DŸK™KÐ(8Ò9Ð_cˆMØÑ+´
¸=Ì,×0WÑ0WØ!1�Ø×"Ñ" >Õ<à!%×!2Ñ!2°8Ó!<�Ø<J×)Ñ)¨.Ñ9Ü NÑBÐBr=   c               ó<  • Uc  U R                  U5      nOU R                  R                  U5      nUb  U R                  U   OSnUb.  [	        U[        U5      5      (       a  UnU R                  X25        OU R                  U5      nX0R                  U'   [        X2S9$ )ar  Create or overwrite a ModelResponsePart.

Args:
    vendor_part_id: The vendor's ID for this tool call part. If not
        None and an existing part is found, that part is overwritten.
    part: The ModelResponsePart.

Returns:
    ModelResponseStreamEvent: A `PartStartEvent` indicating that a new part
    has been added to the manager, or replaced an existing part.
Nrb   )rp   r'   rF   r&   r@   r8   rš   r   )r1   rt   rL   rw   rœ   rv   s         r3   Úhandle_partÚ%ModelResponsePartsManager.handle_part	  sœ   € ð" Ñ!à!×.Ñ.¨tÓ4‰Nð  $×<Ñ<×@Ñ@ÀÓPÐØ=MÑ=Y˜DŸK™KÐ(8Ò9Ð_cˆMØÑ+´
¸=Ì$ÈtË*×0UÑ0UØ!1�Ø×"Ñ" >Õ8à!%×!2Ñ!2°4Ó!8�Ø<J×)Ñ)¨.Ñ9Ü NÑ>Ð>r=   c                óD   • Ub  U R                   R                  US5        gg)z>Stop tracking a vendor_part_id (no-op if None or not tracked).N)r'   r„   ©r1   rt   s     r3   Ú_stop_tracking_vendor_idÚ2ModelResponsePartsManager._stop_tracking_vendor_id)  s#   € àÑ%Ø×)Ñ)×-Ñ-¨n¸dÕCð &r=   c                óˆ   • [        U R                  5      nU R                  R                  U5        Ub  X0R                  U'   U$ )zAAppend a part, optionally track vendor_part_id, return new index.)rn   r&   Úappendr'   )r1   rL   rt   Ú	new_indexs       r3   rp   Ú&ModelResponsePartsManager._append_part.  s<   € ä˜Ÿ™Ó$ˆ	Ø�‰×Ñ˜4Ô ØÑ%Ø<E×)Ñ)¨.Ñ9ØÐr=   c                óþ  • UR                   n[        U[        5      (       d¤  [        U[        5      =(       d%    [        U[        5      =(       a    UR
                  SLnU(       aP  XR                  ;   aA  U R                  U5      n[        U[        [        -  [        -  5      (       d   SU< 35       eUnUR                  U5      $ [        U[        5      (       a  U R                  XX45      $ U R                  XX45      $ )z9Apply a tool call delta while buffering string arguments.NzExpected a tool call, got )r‘   r@   ÚstrÚdictr   r�   r*   rR   r   r   rƒ   Ú+_apply_string_delta_to_incomplete_tool_callÚ _apply_string_delta_to_tool_call)r1   rS   rv   ri   r�   Úshould_materializeÚmaterialized_parts          r3   r“   Ú0ModelResponsePartsManager._apply_tool_call_delta6  sì   € ð ×ÑˆÜ˜$¤×$Ñ$Ü!+¨D´$Ó!7÷ "Ü˜=Ô*;Ó<×bÀ×AVÑAVÐ^bÐAbð ö " j×4HÑ4HÓ&HØ$(×$DÑ$DÀZÓ$PÐ!Ü!Ð"3Ô5FÌÑ5UÔXjÑ5j×kÑkð Ø0Ð1BÑ0EÐFóÐkð !2�Ø—;‘;˜}Ó-Ð-ä�mÔ%6×7Ñ7Ø×CÑCÀJÐ_dÓkÐkØ×4Ñ4°ZÐPUÓ\Ð\r=   c                ó¸  • UR                   n[        U[        5      (       a  UR                  U5      $ UR                  bB  U R                  U5      n[        U[        5      (       d   SU< 35       eUR                  U5      $ U(       d   XR                  ;  a  UR                  U5      $ UnUR                  (       d"  UR                  (       d  UR                  (       a<  [        USS9nUR                  U5      n[        U[        5      (       d   SU< 35       eO.UR                  b!  [        XwR                  R                  5       S9nU R                  XU5        U$ )z9Apply a buffered string delta to an incomplete tool call.Nz&Expected an incomplete tool call, got ©r‘   ©r]   )r‘   r@   r¬   rƒ   r�   rR   r   r*   rŽ   r\   r]   r   Úcopyrs   )	r1   rS   rv   ri   r�   Úcurrent_argsr°   rz   rŠ   s	            r3   r­   ÚEModelResponsePartsManager._apply_string_delta_to_incomplete_tool_callN  s<  € ð %×/Ñ/ˆÜ�l¤D×)Ñ)Ø—;‘;˜}Ó-Ð-à× Ñ Ñ,Ø $× @Ñ @ÀÓ LÐÜÐ/Ô1B×CÑCð Ø8Ð9JÑ8MÐNóÐCð —;‘;Ð0Ó1Ð1æ˜
×*>Ñ*>Ó>Ø—;‘;˜}Ó-Ð-à$ˆØ×× ×!4×!4¸×8N×8NÜ$ U°tÑ<ˆNØ)×/Ñ/°Ó>ˆLÜ˜lÔ,=×>Ñ>ð Ø8¸Ñ8HÐIóÑ>ð ×*Ñ*Ñ6Ü" <×B_ÑB_×BdÑBdÓBfÑgˆLØ×!Ñ! *¸DÔAØÐr=   c                óü  • UR                   n[        U[        5      (       a  UR                  U5      $ U(       d   XR                  ;  a  UR                  U5      $ UnUR
                  (       d3  UR                  (       d"  UR                  (       d  UR                  (       a  [        USS9nUR                  U5      nO.UR                  b!  [        XfR                  R                  5       S9nU R                  XU5        U$ )z6Apply a buffered string delta to a complete tool call.Nr³   r´   )r�   r@   r¬   rƒ   r*   r�   rŽ   r\   r]   r   rµ   rs   )r1   rS   rv   ri   r�   r¶   rz   rŠ   s           r3   r®   Ú:ModelResponsePartsManager._apply_string_delta_to_tool_calll  sÆ   € ð %×)Ñ)ˆÜ�l¤D×)Ñ)Ø—;‘;˜}Ó-Ð-æ˜
×*>Ñ*>Ó>Ø—;‘;˜}Ó-Ð-à$ˆØ× ×  E×$6×$6¸%×:M×:MÐQV×Qg×QgÜ$ U°tÑ<ˆNØ)×/Ñ/°Ó>‰LØ×*Ñ*Ñ6Ü" <×B_ÑB_×BdÑBdÓBfÑgˆLØ×!Ñ! *¸DÔAØÐr=   c               óÞ   • U(       a3  [        U[        5      (       a  SOSn[        X$S9nUR                  U5      $ UR                  (       a!  [        XR                  R                  5       S9$ U$ )ap  Apply a metadata-only delta to `existing_part`, else defensively copy its `provider_details`.

The content delta is reset so only provider metadata is applied; the string content is carried
separately by the buffer. When there is no metadata to apply, `provider_details` is copied so a
previously-emitted snapshot of the part cannot alias the manager's mutable state.
r€   N)re   r´   )r@   r   r   rƒ   r]   rµ   )r1   rv   ry   rg   Úcontent_resetrŠ   s         r3   rr   ÚBModelResponsePartsManager._apply_metadata_or_copy_provider_details€  s^   € ö Ü",¨Z¼×"GÑ"G™BÈTˆMÜ$ ZÑMˆNØ!×'Ñ'¨Ó6Ð6Ø×)×)Ü˜=×;YÑ;Y×;^Ñ;^Ó;`ÑaÐaØÐr=   c                ó¦   • U(       d  gU R                   R                  U5      nUc  U=(       d    S/=o@R                   U'   UR                  U5        g)a˜  Buffer a string append while preserving a `None`-to-empty transition.

Invariant: `''.join(buffer)` in `_materialized_part` must equal the result of applying each
buffered delta in turn via `TextPartDelta.apply`/`ThinkingPartDelta.apply`/`ToolCallPartDelta.apply`,
which combine string content by plain concatenation (`part.content + delta`). This buffered
assembly duplicates that concatenation logic from `messages.py`, and no test would catch the two
copies diverging: if a `*Delta.apply` ever combined content by anything other than `a + b`
(normalization, trimming, dedup), the buffered path would silently produce different output. Keep
the two in lockstep.
Nr€   )r*   rF   r§   )r1   rS   Úcurrent_valueri   rˆ   s        r3   rs   Ú.ModelResponsePartsManager._buffer_string_delta•  sL   € ö ØØ×%Ñ%×)Ñ)¨*Ó5ˆØ‰>Ø9F×9LÈ"Ð8MÐMˆF×)Ñ)¨*Ñ5Ø�‰�eÕr=   c                óX  • U R                   U   nU R                  R                  U5      nUc  U$ SR                  U5      n[	        U[
        [        -  5      (       a	  [        X$S9$ [	        U[        5      (       a/  [	        UR                  [        5      (       a   S5       e[        X$S9$ [	        U[        [        -  5      (       aW  [	        UR                  [        5      (       a   S5       e[        X$S9n[	        U[        5      (       a  U R                  U5      nU$ [        SU< 35      e)zLCompute the materialized form of a part without mutating any buffered state.r€   )ra   z=Cannot materialize string arguments onto dictionary argumentsr³   )r�   z%Cannot materialize string deltas for )r&   r*   rF   Újoinr@   r   r   r   r   r‘   r¬   r   r   r�   rN   ÚAssertionError)r1   rS   rL   rˆ   ÚvalueÚmaterializeds         r3   Ú_materialized_partÚ,ModelResponsePartsManager._materialized_part§  s  € à�{‰{˜:Ñ&ˆØ×%Ñ%×)Ñ)¨*Ó5ˆØ‰>ØˆKà—‘˜“ˆÜ�dœH¤|Ñ3×4Ñ4Ü˜4Ñ/Ð/Ü�dÔ-×.Ñ.Ü! $§/¡/´4×8Ñ8ð ØOóÐ8ô ˜4Ñ2Ð2Ü�dœLÔ+=Ñ=×>Ñ>Ü! $§)¡)¬T×2Ñ2ÐsÐ4sÓsÐ2Ü" 4Ñ4ˆLÜ˜,¬×5Ñ5Ø#×4Ñ4°\ÓB�ØÐÜÐDÀTÁHÐMÓNÐNr=   c                ó|   • U R                  U5      nXR                  ;   a  U R                  U	 X R                  U'   U$ )zPMaterialize buffered string deltas for one part, caching the result in `_parts`.)rÅ   r*   r&   ©r1   rS   rL   s      r3   rR   Ú5ModelResponsePartsManager._materialize_and_cache_part¾  s=   € à×&Ñ& zÓ2ˆØ×-Ñ-Ó-Ø×$Ñ$ ZÐ0Ø&*�K‰K˜
Ñ#Øˆr=   c                óÀ   • U R                   (       d  U R                  $ [        [        U R                  5      5       Vs/ s H  oR	                  U5      PM     sn$ s  snf )zªReturn the fully materialized parts without mutating buffered state.

Used by `__eq__`/`__repr__` so that reading the manager never flushes its buffers as a
side effect.
)r*   r&   Úrangern   rÅ   )r1   rc   s     r3   r:   Ú-ModelResponsePartsManager._materialized_partsÆ  sH   € ð ×#×#Ø—;‘;ÐÜ<AÄ#ÀdÇkÁkÓBRÔ<SÓTÒ<S°5×'Ñ'¨Ö.Ñ<SÑTÐTùÒTs   ¾Ac                óX   • U R                   R                  US5        X R                  U'   g)z5Fully replace a part and discard any buffered deltas.N)r*   r„   r&   rÈ   s      r3   rš   Ú'ModelResponsePartsManager._replace_partÐ  s$   € à×Ñ× Ñ  ¨TÔ2Ø"&�‰�JÒr=   c                óš   • U R                   (       a:  [        U R                   5      S-
  nU R                   U   n[        X15      (       a  X24$ g)zKGet the latest part and its index if it's an instance of the given type(s).r   N)r&   rn   r@   )r1   Ú
part_typesrS   Úlatest_parts       r3   rj   Ú1ModelResponsePartsManager._latest_part_if_of_typeÕ  sA   € à�;�;Ü˜TŸ[™[Ó)¨AÑ-ˆJØŸ+™+ jÑ1ˆKÜ˜+×2Ñ2Ø"Ð.Ð.Ør=   c              #  óz   #   • U R                  U5        [        SX#S9nU R                  XA5      n[        XTS9v •  g7f)z-Handle <think> tag - create new ThinkingPart.r€   )ra   r\   r]   rb   N)r¤   r   rp   r   )r1   rt   r\   r]   rL   r¨   s         r3   rm   Ú9ModelResponsePartsManager._handle_embedded_thinking_startÞ  s<   é € ð 	×%Ñ% nÔ5Ü B°mÑgˆØ×%Ñ% dÓ;ˆ	Ü 9Ñ8Ó8ùs   ‚9;c              #  óJ  #   • [        UU R                  X5      US9nUR                  SL=(       d$    UR                  SL=(       d    UR                  0 :H  nU R	                  XUS9nU(       a  U R                  X!R                  U5        X€R                  U'   [        X&S9v •  g7f)z)Handle content inside <think>...</think>.rd   Nrf   rh   )	r   rq   r\   r]   rr   rs   ra   r&   r   )	r1   rv   rS   ra   r\   r]   ry   rg   rz   s	            r3   rl   Ú;ModelResponsePartsManager._handle_embedded_thinking_contentç  s¯   é € ô 'Ø!Ø×5Ñ5°mÓSØ-ñ
ˆ
ð ×$Ñ$¨DÐ0÷ 4Ø×*Ñ*°$Ð6÷4à×-Ñ-°Ñ3ð 	ð
 ×DÑDØ°nð Eð 
ˆö Ø×%Ñ% j×2GÑ2GÈÔQØ".�‰�JÑÜ :Ñ@Ó@ùs   ‚B!B#c                ó&   • U R                  U5        g)zCHandle </think> tag - stop tracking so next delta creates new part.N)r¤   r£   s     r3   rk   Ú7ModelResponsePartsManager._handle_embedded_thinking_end  s   € à×%Ñ% nÕ5r=   c                ó@   • UR                   b  X!R                   :w  a  U$ g)zBReturn the provider name if it has not been set on previous parts.N)r\   )r1   rv   r\   s      r3   rq   Ú0ModelResponsePartsManager._resolve_provider_name  s$   € ð ×&Ñ&Ñ.°-×C^ÑC^Ó2^Ø Ð Ør=   c                óx  • [        U[        5      (       a%  U R                  UR                  UR                  S9  g[        U[
        5      (       ak  U R                  UR                  5      nUc   e[        U[        5      (       a   eU R                  UR                  UR                  R                  U5      S9  gg)zQApply a replayed stream event to the managed parts, so `get_parts()` reflects it.)rt   rL   N)
r@   r   r    rc   rL   r   rY   r   ri   rƒ   )r1   ÚeventrL   s      r3   Úapply_eventÚ%ModelResponsePartsManager.apply_event  s—   € ä�eœ^×,Ñ,Ø×Ñ¨E¯K©K¸e¿j¹jÐÒIÜ˜œ~×.Ñ.Ø×-Ñ-¨e¯k©kÓ:ˆDØÑ#Ð#Ð#Ü! $Ô(9×:Ñ:Ð:Ð:Ø×Ñ¨E¯K©K¸e¿k¹k×>OÑ>OÐPTÓ>UÐÒVð	 /r=   )r+   )ÚreturnÚNone)rß   r«   )rB   Úobjectrß   Úbool)rG   r«   rß   zToolPartKind | None)rL   r   rß   r   )rß   zlist[ModelResponsePart])rX   ÚVendorIdrß   zManagedPart | None)rt   úVendorId | Nonera   r«   r[   ú
str | Noner\   rå   r]   údict[str, Any] | Noner^   ztuple[str, str] | Noner_   râ   rß   ú"Iterator[ModelResponseStreamEvent])rt   úHashable | Nonera   rå   r[   rå   r~   rå   r\   rå   r]   r   rß   rç   )rt   rè   rG   rå   r�   ústr | dict[str, Any] | NonerŽ   rå   r\   rå   r]   ræ   rß   zModelResponseStreamEvent | None)rt   rè   rG   r«   r�   ré   rŽ   rå   r[   rå   r\   rå   r]   ræ   rß   r   )rt   rè   rL   r   rß   r   )rt   rä   rß   rà   r-   )rL   ÚManagedPartrt   rä   rß   Úint)rS   rë   rv   ú5ToolCallPartDelta | ToolCallPart | NativeToolCallPartri   r   rß   rì   )
rS   rë   rv   r   ri   r   r�   r«   rß   rì   )
rS   rë   rv   ú!ToolCallPart | NativeToolCallPartri   r   r�   r«   rß   rí   )rv   úTextPart | ThinkingPartry   z!TextPartDelta | ThinkingPartDeltarg   râ   rß   rî   )rS   rë   r¾   rå   ri   r«   rß   rà   )rS   rë   rß   rê   )rß   r%   )rS   rë   rL   rê   rß   rà   )rÐ   ztype[PartT]rß   ztuple[PartT, int] | None)rt   rã   r\   rå   r]   ræ   rß   rç   )rv   r   rS   rë   ra   r«   r\   rå   r]   ræ   rß   rç   )rt   rã   rß   rà   )rv   z%ModelResponsePart | ToolCallPartDeltar\   rå   rß   rå   )rÜ   r   rß   rà   )0Ú__name__Ú
__module__r9   Ú__firstlineno__Ú__doc__Ú__annotations__r   Úlistrê   r&   r¬   rã   rë   r'   r«   r*   r   r+   r4   r;   rC   rH   rN   rU   rY   r{   r‹   r–   r�   r    r¤   rp   r“   r­   r®   rr   rs   rÅ   rR   r:   rš   rj   rm   rl   rk   rq   rÝ   Ú__static_attributes__© r=   r3   r    r    9   s  ‡ ñð
 5Ó4ðñ !&°d¸;Ñ6GÈeÑ T€FÐÓTØfÙ49È$ÈxÐY\È}ÑJ]ÐdiÑ4jÐÐ1ÓjØcÙ,1Ø˜S $ s¡)˜^Ñ,°5¸uÈeñ-€OÐ)ó ð =Ù27ÈÈSÐR^ÐM^ÑH_ÐfkÐrwÑ2xÐÐ/ÓxØ[ô
ô

ôô 6ô>ô	Pôð& Ø$(Ø26Ø04Ø*/ñaEð (ðaEð ð	aEð
 ðaEð "ðaEð 0ðaEð .ðaEð $(ðaEð 
,õaEðN #ØØ $Ø$(Ø15ñiEð (ðiEð ð	iEð
 ðiEð ðiEð "ðiEð /ðiEð 
,õiEð^ !%Ø,0Ø#'Ø$(Ø26ñlIð (ðlIð ð	lIð
 *ðlIð !ðlIð "ðlIð 0ðlIð 
)õlIðh $(ØØ$(Ø26ñ3Cð (ð3Cð ð	3Cð
 *ð3Cð !ð3Cð ð3Cð "ð3Cð 0ð3Cð 
"õ3Cðj?ð (ð?ð  ð	?ð
 
"ô?ô@Dö
ð]àð]ð Mð]ð !ð	]ð
 
?ô]ð0ØðØ.?ðØHYðØadðà	>ôð<ØðØ.OðØXiðØqtðà	*ôð(à.ðð 6ðð
 ðð 
!ôô*ô$Oô.ôUô'ô
ð9Ø&ð9Ø7Að9ØUjð9à	+ô9ðAà#ðAð ðAð ð	Að
 "ðAð 0ðAð 
,ôAô66ðØBðØS]ðà	ô÷Wr=   r    N)(rò   Ú
__future__r   Ú_annotationsÚcollections.abcr   r   Údataclassesr   r   r   Útypingr	   r
   r   Úpydantic_ai.exceptionsr   Úpydantic_ai.messagesr   r   r   r   r   r   r   r   r   r   r   r   r   Ú_utilsr   r™   Úmodelsr   rã   rê   r   r    rö   r=   r3   Ú<module>r      sŠ   ðñõ 3ç .ß 1Ñ 1ß .Ñ .å :÷÷ ÷ õ õ  DæÝ.à€ðð  Ð"3Ñ3€ðñ 	�˜{Ñ+€ð ÷\Wð \Wó ñ\Wr=   