ó
    °"³j"  ã                  ó&  • % S r SSKJr  SSKrSSKJrJr  SSKJr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JrJrJrJrJrJrJrJrJrJr  \(       a  SSKJr  \S   r S\!S'    Sr"S\!S'    SS jr#\ " S S5      5       r$ " S S\%\$   5      r&g)zøInternal helpers for the `RunContext.enqueue` / `AgentRun.enqueue` APIs.

These types live here (rather than in `messages.py`) because they're internal runtime
state for the pending message queue, not part of the wire-serializable message history.
é    )ÚannotationsN)ÚIterableÚSequence)Ú	dataclassÚfield)ÚTYPE_CHECKINGÚLiteralÚSupportsIndexÚ	TypeAliasé   )Úuuid7)Ú	UserError)ÚModelMessageÚModelRequestÚModelRequestPartÚModelResponseÚRetryPromptPartÚ
SpeechPartÚSystemPromptPartÚToolAvailabilityDeltaPartÚToolReturnPartÚToolSearchReturnPartÚUserPromptPart)ÚUserContent)ÚasapÚ	when_idler   ÚPendingMessagePriorityz-UserContent | ModelRequestPart | ModelMessageÚEnqueueContentc                óˆ  ^^^^• / m/ m/ mSUU4S jjmSUUU4S jjnU  H—  n[        U[        [        45      (       a  U" 5         TR                  U5        M8  [        U[        [
        [        [        [        [        [        45      (       a  T" 5         TR                  U5        M†  TR                  U5        M™     U" 5         T$ )aR  Assemble enqueue items into a list of [`ModelMessage`][pydantic_ai.messages.ModelMessage]s.

Adjacent [`UserContent`][pydantic_ai.messages.UserContent] items are gathered into one
[`UserPromptPart`][pydantic_ai.messages.UserPromptPart], and part-style items (user content and
[`ModelRequestPart`][pydantic_ai.messages.ModelRequestPart]s) are coalesced into a single
[`ModelRequest`][pydantic_ai.messages.ModelRequest]; complete `ModelMessage`s are emitted as-is.
Order is preserved, so a `ModelResponse` followed by part-style items produces the response then
a request built from those parts.
c                 óÔ   >• T(       a`  [        T5      S:X  a  [        TS   [        5      (       a  TS   O
[        T5      n TR	                  [        U S95        TR                  5         g g )Nr   r   )Úcontent)ÚlenÚ
isinstanceÚstrÚlistÚappendr   Úclear)Úsingler!   Úpartss    €€ÚQ/home/mande/repo/quber/.venv/lib/python3.13/site-packages/pydantic_ai/_enqueue.pyÚflush_contentÚ._build_enqueue_messages.<locals>.flush_contentT   sU   ø€ Þô $' w£<°1Ó#4¼ÀGÈAÁJÔPS×9TÑ9T�W˜Q’ZÔZ^Ð_fÓZgˆFØ�L‰Lœ°Ñ7Ô8Ø�M‰M�Oð ó    c                 ó†   >• T " 5         T(       a2  TR                  [        [        T5      S95        TR                  5         g g )N)r)   )r&   r   r%   r'   )r+   Úmessagesr)   s   €€€r*   Úflush_requestÚ._build_enqueue_messages.<locals>.flush_request\   s.   ø€ ÙŒÞØ�O‰OœL¬t°E«{Ñ;Ô<Ø�K‰K�Mð r-   ©ÚreturnÚNone)r#   r   r   r&   r   r   r   r   r   r   r   )Úitemsr0   Úitemr!   r+   r/   r)   s      @@@@r*   Ú_build_enqueue_messagesr7   F   s¬   û€ ð $&€HØ$&€EØ!#€G÷ð ÷ñ ó ˆÜ�dœ\¬=Ð9×:Ñ:ÙŒOØ�O‰O˜DÖ!ÜØä ÜÜÜÜ$Ü)Üð÷
ñ 
ñ ŒOØ�L‰L˜Öà�N‰N˜4Ö ñ' ñ( „OØ€Or-   c                  ól   • \ rS rSr% SrS\S'    SrS\S'    \" S S	9rS
\S'    \	SS.SS jj5       r
Srg)ÚPendingMessageéz   ap  One or more [`ModelMessage`][pydantic_ai.messages.ModelMessage]s queued for injection into the agent conversation.

Enqueued via [`RunContext.enqueue`][pydantic_ai.tools.RunContext.enqueue] or
[`AgentRun.enqueue`][pydantic_ai.run.AgentRun.enqueue] and automatically drained
at the appropriate time during the agent run by the internal `PendingMessageDrainCapability`.
úlist[ModelMessage]r/   r   r   Úpriorityc                 ó(   • [        [        5       5      $ ©N)r$   r   © r-   r*   Ú<lambda>ÚPendingMessage.<lambda>�   s
   € ´C¼»´Lr-   )Údefault_factoryr$   Ú
enqueue_id©r<   c               óz   • [        U5      nU(       d  g[        US   [        5      (       d  [        S5      eU " X1S9$ )u‹  Build a `PendingMessage` from `enqueue` arguments, or `None` when there's nothing to send.

Returns `None` for an empty call (enqueueing nothing is a no-op rather than an error).

Raises:
    UserError: If the assembled messages don't end in a
        [`ModelRequest`][pydantic_ai.messages.ModelRequest] â€” e.g. a lone `ModelResponse` â€”
        since the agent needs a request to respond to.
Néÿÿÿÿz•Enqueued content must end with a `ModelRequest` (or user content / `ModelRequestPart` items that form one), so the agent has a request to respond to.)r/   r<   )r7   r#   r   r   )Úclsr<   r!   r/   s       r*   Úfrom_contentÚPendingMessage.from_content”   sE   € ô +¨7Ó3ˆÞØÜ˜( 2™,¬×5Ñ5ÜðRóð ñ ˜HÑ8Ð8r-   r?   N)r!   r   r<   r   r3   zPendingMessage | None)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú__annotations__r<   r   rC   ÚclassmethodrH   Ú__static_attributes__r?   r-   r*   r9   r9   z   sU   ‡ ñð !Ó ð<ð (.€HÐ$Ó-ðñ Ñ,@ÑA€J�ÓAðYð ØW]ö 9ó ó9r-   r9   c                  óv   ^ • \ rS rSrSrSSU 4S jjjrSS jrSU 4S jjrSS jrSS jr	SS jr
SS	 jrS
rU =r$ )ÚPendingMessageQueueéª   zFA run's pending messages with thread-safe append and drain operations.c                óf   >• [         TU ]  U5        SU l        [        R                  " 5       U l        g )NF)ÚsuperÚ__init__Ú_closedÚ	threadingÚLockÚ_lock)Úselfr/   Ú	__class__s     €r*   rW   ÚPendingMessageQueue.__init__­   s%   ø€ Ü‰Ñ˜Ô"ØˆŒÜ—^’^Ó%ˆ�
r-   c                ó&   • [         [        U 5      44$ r>   )rS   r%   )r\   Úprotocols     r*   Ú__reduce_ex__Ú!PendingMessageQueue.__reduce_ex__²   s   € Ü"¤T¨$£Z MÐ1Ð1r-   c                ó¨   >• U R                      U R                  (       a  [        S5      e[        TU ]  U5        S S S 5        g ! , (       d  f       g = f)Nz;`enqueue` is not available because the agent run has ended.)r[   rX   r   rV   r&   )r\   Úpendingr]   s     €r*   r&   ÚPendingMessageQueue.appendµ   s3   ø€ Ø�Z‹ZØ�|�|ÜÐ ]Ó^Ð^Ü‰G‰N˜7Ô#÷ �ZŽZús   Ž,AÁ
Ac                ór   • U R                      U R                  U5      sS S S 5        $ ! , (       d  f       g = fr>   )r[   Ú_pop_priority)r\   r<   s     r*   Úpop_priorityÚ PendingMessageQueue.pop_priority»   s!   € Ø�Z‹ZØ×%Ñ% hÓ/÷ �Z�Zús   �(¨
6c                óÄ   • U R                      U R                  S5      nU R                  S5      nU(       d  U(       d  SU l        X4sSSS5        $ ! , (       d  f       g= f)z:Drain both priorities, or atomically close an empty queue.r   r   TN)r[   rg   rX   )r\   r   r   s      r*   Údrain_at_endÚ PendingMessageQueue.drain_at_end¿   sD   € à�Z‹ZØ×%Ñ% fÓ-ˆDØ×*Ñ*¨;Ó7ˆIÞ¦	Ø#�”Ø�?÷ �Z�Zús   �:AÁ
Ac                ó^   • U R                      SU l        S S S 5        g ! , (       d  f       g = f)NT)r[   rX   )r\   s    r*   ÚcloseÚPendingMessageQueue.closeÈ   s   € Ø�Z‹ZØˆDŒL÷ �ZŽZús   �ž
,c                ó®   • U  Vs/ s H  o"R                   U:X  d  M  UPM     nnU  Vs/ s H  o"R                   U:w  d  M  UPM     snU S S & U$ s  snf s  snf r>   rD   )r\   r<   rd   Úselecteds       r*   rg   Ú!PendingMessageQueue._pop_priorityÌ   sR   € Ù+/ÓPª4 ×3CÑ3CÀxÑ3O—G©4ˆÐPÙ*.ÓOª$˜w×2BÑ2BÀhÑ2N—7©$ÑOˆ‰QˆØˆùò QùÚOs   …AœA¨A¿A)rX   r[   )r?   )r/   zIterable[PendingMessage]r3   r4   )r`   r
   r3   z=tuple[type[PendingMessageQueue], tuple[list[PendingMessage]]])rd   r9   r3   r4   )r<   r   r3   zlist[PendingMessage])r3   z1tuple[list[PendingMessage], list[PendingMessage]]r2   )rJ   rK   rL   rM   rN   rW   ra   r&   rh   rk   rn   rg   rQ   Ú__classcell__)r]   s   @r*   rS   rS   ª   s3   ø† ÙP÷&ñ &ô
2÷$ô0ô#ô ÷ò r-   rS   )r5   zSequence[EnqueueContent]r3   r;   )'rN   Ú
__future__r   rY   Úcollections.abcr   r   Údataclassesr   r   Útypingr   r	   r
   r   Ú_uuidr   Ú
exceptionsr   r/   r   r   r   r   r   r   r   r   r   r   r   r   r   rO   r   r7   r9   r%   rS   r?   r-   r*   Ú<module>rz      s›   ðòõ #ã ß .ß (ß CÓ Cå Ý !÷÷ ÷ ñ ö Ý%ð %,Ð,?Ñ$@Ð ˜	Ó @ðð L€�	Ó Kðô.1ðh ÷,9ð ,9ó ð,9ô^%˜$˜~Ñ.õ %r-   