ó
    ±"³jù&  ã                  óÜ   • S SK Jr  S SKrS SKJrJrJr  S SKJr  S SK	J
r
JrJr  S SKrS SKJrJr  S SKJr  S SKJr  S S	KJrJr  S
SKJr  \
(       a  S SKJrJr  \ " S S\\   5      5       rg)é    )ÚannotationsN)ÚAsyncIterableÚAsyncIteratorÚ	Coroutine)Ú	dataclass)ÚTYPE_CHECKINGÚAnyÚcast)ÚMemoryObjectReceiveStreamÚMemoryObjectSendStream)Ú_utils)ÚAgentStreamEvent)Ú
AgentDepsTÚ
RunContexté   )ÚAbstractCapability)ÚEventStreamHandlerÚEventStreamProcessorc                  ób   • \ rS rSr% SrS\S'         S
S jr\SS j5       r\	SS j5       r
Srg	)ÚProcessEventStreamé   u‚  A capability that forwards the agent's event stream to a user-provided async handler.

The handler receives the stream of [`AgentStreamEvent`][pydantic_ai.messages.AgentStreamEvent]s
emitted during classic model streaming and tool execution, or the shared and realtime-only
events emitted by a realtime session. Two forms are supported:

- An [`EventStreamHandler`][pydantic_ai.agent.EventStreamHandler] â€” an `async def`
  returning `None`. Events are forwarded to the handler while also being passed
  through unchanged to the rest of the capability chain, so multiple handlers (and
  the top-level `event_stream_handler` argument) can all see the same stream without
  changing each other's view. A handler that returns early stops receiving events
  but does not affect downstream consumers; a handler that raises propagates the
  exception to the rest of the run. Events are delivered synchronously, so a slow
  handler back-pressures the rest of the stream.
- An `EventStreamProcessor` â€” an async
  generator yielding [`AgentStreamEvent`][pydantic_ai.messages.AgentStreamEvent]s.
  The events it yields replace the inner stream for downstream wrappers and consumers,
  so it can modify, drop, or add events.

  This replacement is global, not a private view for event-stream handlers: the run has one
  event stream and a processor shapes all of it. Dropping or rewriting a
  [`PartDeltaEvent`][pydantic_ai.messages.PartDeltaEvent] therefore also changes what
  [`stream_text()`][pydantic_ai.result.StreamedRunResult.stream_text] yields to a
  `run_stream()` caller.

  Some events are also control signals:
  [`FinalResultEvent`][pydantic_ai.messages.FinalResultEvent] is what tells
  [`agent.run_stream()`][pydantic_ai.agent.AbstractAgent.run_stream] that the final output has
  started, so dropping it makes `run_stream()` wait for the whole model response before handing
  back the result instead of streaming it. Filter deliberately.

  None of this changes the run's output: the
  [`ModelResponse`][pydantic_ai.messages.ModelResponse] is accumulated from the raw model
  stream before a processor sees the events, so
  [`stream_output()`][pydantic_ai.result.StreamedRunResult.stream_output] and the final
  validated output are unaffected (dropping events can only change when a partial snapshot is
  emitted, not its content). Use the observer form if you only want to watch events.

  In a realtime session, this is likewise only a consumer-facing view. Transforming or dropping
  events does not affect session history or tool execution.

When this capability is registered, `agent.run()` and
[`AgentRun.next()`][pydantic_ai.run.AgentRun.next] automatically enable streaming so the
handler fires without requiring an explicit `event_stream_handler` argument. The handler
sees the same events however the run is driven, including under
[`agent.iter()`][pydantic_ai.agent.Agent.iter] and when you stream a node yourself with
`node.stream()`.

!!! note "Durable execution"

    Under the durable-execution capabilities
    ([`TemporalDurability`][pydantic_ai.durable_exec.temporal.TemporalDurability],
    [`DBOSDurability`][pydantic_ai.durable_exec.dbos.DBOSDurability],
    [`PrefectDurability`][pydantic_ai.durable_exec.prefect.PrefectDurability]),
    this capability's handler always runs in workflow or flow code and must be
    deterministic because it re-runs on workflow replay. Tool-call and final-output
    events arrive live; model events are the real captured events replayed after each
    model-request activity, step, or task completes. For handler I/O that must run
    exactly once inside a durable boundary, pass `event_stream_handler=` to the
    durability capability instead.
zIEventStreamHandlerFunc[AgentDepsT] | EventStreamProcessorFunc[AgentDepsT]Úhandlerc              ó–  ^^^^#   • U R                  TU5      n[        U[        5      (       a  U  S h  v•N nU7v •  M  [        SU5      R	                  5         [        SU R                   5      m[
        R                  " 5       u  nmSUUU4S jjn[        R                  " U" 5       5      nS n[        U5      m U IS h  v•N   Sn	S	U4S jjn
 [        R                  " U
" 5       5      nU	(       a±  [        R                  " X‡4[        R                  S9I S h  v•N   UR                  5       (       ap  UR                  5       (       d  UR                  5       c  Sn	OG[        R                   " U5      I S h  v•N    [        R"                  " T5      I S h  v•N   UI S h  v•N    UI S h  v•N nU	(       a   UR'                  U5      I S h  v•N   U7v •  GM   GN¤
 g  GN NÅ Nk NN NF! UI S h  v•N    f = f NM! [$         a     O2f = f N>! [
        R(                  [
        R*                  4 a    Sn	 Ndf = fS S S 5      IS h  v•N    Os! , IS h  v•N  (       d  f       O\= f! [,         aL    [        R                   " U/[/        S U45      Q76 I S h  v•N    [        R"                  " T5      I S h  v•N    e f = fUI S h  v•N    g 7f)
NzCoroutine[Any, Any, None]z"EventStreamHandlerFunc[AgentDepsT]c               “  ó¦   >#   • T IS h  v•N   T" T T5      I S h  v•N   S S S 5      IS h  v•N   g  N& N N	! , IS h  v•N  (       d  f       g = f7f©N© )ÚctxÚobserverÚreceive_streams   €€€Új/home/mande/repo/quber/.venv/lib/python3.13/site-packages/pydantic_ai/capabilities/process_event_stream.pyÚrun_handlerÚ=ProcessEventStream.wrap_run_event_stream.<locals>.run_handlerr   s2   øé € ß%’~Ù˜s NÓ3×3Ð3÷ &—~’~Ù3÷ &—~—~�~üsF   ƒAŠ1‹AŽ7›3œ7 A«5¬A³7µA·A½A ¾AÁ
ATc               “  ó6   >#   • [        T 5      I S h  v•N $  N7fr   )Úanext)Ústream_iterators   €r    Ú	pull_nextÚ;ProcessEventStream.wrap_run_event_stream.<locals>.pull_next‹   s   øé € Ü!& Ó!7×7Ð7Ñ7ùs   ƒ’“)Úreturn_whenF)ÚreturnÚNone)r)   r   )r   Ú
isinstancer   r
   ÚcloseÚanyioÚcreate_memory_object_streamÚasyncioÚcreate_taskÚaiterÚwaitÚFIRST_COMPLETEDÚdoneÚ	cancelledÚ	exceptionr   Úcancel_and_drainÚaclose_if_supportedÚStopAsyncIterationÚsendÚBrokenResourceErrorÚClosedResourceErrorÚBaseExceptionÚfilter)Úselfr   ÚstreamÚprobeÚeventÚsend_streamr!   Úhandler_taskÚ	next_taskÚhandler_aliver&   r   r   r%   s    `         @@@r    Úwrap_run_event_streamÚ(ProcessEventStream.wrap_run_event_streamZ   sb  ûé € ð —‘˜S &Ó)ˆÜ�eœ]×+Ñ+Ù$÷ �eØ•ô
 	Ð(¨%Ó0×6Ñ6Ô8äÐ<¸d¿l¹lÓKˆô ',×&GÒ&GÓ&IÑ#ˆ�^÷	4ñ 	4ô$ ×*Ò*©;«=Ó9ˆØ;?ˆ	Ü ›-ˆð,	ß"’{Ø $�÷8ð Ü '× 3Ò 3±I³KÓ @�IÞ$Ü%ŸlšlØ&Ð5Ü(/×(?Ñ(?ñ÷ ð ð (×,Ñ,×.Ñ.Ø#/×#9Ñ#9×#;Ñ#;À×@VÑ@VÓ@XÑ@`Ø05¡ä&,×&=Ò&=¸iÓ&H× HÐ Hð!7Ü*0×*DÒ*DÀ_Ó*U×$UÐ$Uà*6×$6Ð$6ðØ&/§˜ö %ð2Ø"-×"2Ñ"2°5Ó"9×9Ð9ð  “Kò9 òS˜uàòB #ññ !Iá$Uá$6ø¨,×$6Ò$6úñ !0øÜ-ó Ùðúñ
 :øÜ %× 9Ñ 9¼5×;TÑ;TÐUó 2à,1šMð2ú÷? #—{—{—{—{�{ûôF ó 	ô ×)Ò)¨,ÐT¼ÀÀyÀlÓ9SÒT×TÑTÜ×,Ò,¨_Ó=×=Ñ=Øð	úð ×ÒùsŠ  †*K	°G´GµG¸BK	Â;I$ ÃGÃI$ ÃAI
ÄG ÄAI
Å6G"Å7I
Å<G(ÆG$ÆG(ÆI
ÆG&Æ I
Æ%G:Æ*G8Æ+G:Æ/I
Æ7HÇH
ÇHÇI
ÇGÇK	ÇI$ Ç I
Ç"I
Ç$G(Ç&I
Ç(G5Ç.G1
Ç/G5Ç5I
Ç8G:Ç:
HÈI
ÈHÈI
È
HÈ&H5È2I
È4H5È5I
È8I$ ÉIÉI$ É	K	É
I!ÉIÉI!ÉI$ É K	É!I$ É$/J:ÊJÊJ:Ê2J5Ê3J:Ê:K	ËKËK	c                ó   • g r   r   )Úclss    r    Úget_serialization_nameÚ)ProcessEventStream.get_serialization_name¸   ó   € àó    c                ó   • g)NTr   )r?   s    r    Ú_emits_app_eventsÚ$ProcessEventStream._emits_app_events¼   rM   rN   r   N)r   zRunContext[AgentDepsT]r@   úAsyncIterable[AgentStreamEvent]r)   rR   )r)   z
str | None)r)   Úbool)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú__annotations__rG   ÚclassmethodrK   ÚpropertyrP   Ú__static_attributes__r   rN   r    r   r      s_   ‡ ñ<ð| WÓVð\à#ð\ð 0ð	\ð
 
)ô\ð| óó ðð óó órN   r   ) Ú
__future__r   r/   Úcollections.abcr   r   r   Údataclassesr   Útypingr   r	   r
   r-   Úanyio.streams.memoryr   r   Úpydantic_air   Úpydantic_ai.messagesr   Úpydantic_ai.toolsr   r   Úabstractr   Úpydantic_ai.agent.abstractr   ÚEventStreamHandlerFuncr   ÚEventStreamProcessorFuncr   r   rN   r    Ú<module>ri      sX   ðÝ "ã ß CÑ CÝ !ß +Ñ +ã ß Rå Ý 1ß 4å (æ÷ð ôeÐ+¨JÑ7ó eó ñerN   