
    ij                        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Jr  \(       a  SSKJr   " S	 S
\R"                  \   5      rg)    )annotationsN)AnyAsyncGeneratorTYPE_CHECKING   )WorkflowRuntimeError)Event	StopEventInternalDispatchEvent)
RunResultT)Contextc                     ^  \ rS rSr% SrS\S'   S\S'   S\S'   S	\S
'   SSS.           SU 4S jjjr\SS j5       rSS jr	SS jr
SS jrSS jrSS jr S   SS jjrS S jrSrU =r$ )!WorkflowHandler   a  
Handle a running workflow: await results, stream events, access context, or cancel.

Instances are returned by [Workflow.run][workflows.workflow.Workflow.run].
They can be awaited for the final result and support streaming intermediate
events via [stream_events][workflows.handler.WorkflowHandler.stream_events].

See Also:
    - [Context][workflows.context.context.Context]
    - [StopEvent][workflows.events.StopEvent]
r   _ctxasyncio.Task[None] | None	_run_taskbool_all_events_consumedStopEvent | None_stop_eventN)run_idrun_taskc               X   > [         TU ]  " U0 UD6  X l        Xl        X0l        SU l        g )NF)super__init__r   r   r   r   )selfctxr   r   argskwargs	__class__s         K/home/james-whalen/.local/lib/python3.13/site-packages/workflows/handler.pyr   WorkflowHandler.__init__%   s.     	$)&)	!$)!    c                    U R                   $ )zGThe workflow [Context][workflows.context.context.Context] for this run.)r   r   s    r"   r   WorkflowHandler.ctx3   s     yyr$   c                    U R                   $ )zThe stop event for this run. Always defined once the future is done. In a future major release, this will be removed, and the result will be the stop event itself.)r   r&   s    r"   get_stop_eventWorkflowHandler.get_stop_event8   s    r$   c                ~   #    U R                  5       I Sh  vN   U R                  c   S5       eU R                  $  N$7f)zGet the stop event for this run. Always defined once the future is done. In a future major release, this will be removed, and the result will be the stop event itself.Nz3Stop event must be defined once the future is done.)resultr   r&   s    r"   stop_event_result!WorkflowHandler.stop_event_result<   sC     kkm+ 	
A	
+ 	 	s   =;%=c                x    Xl         U R                  [        U5      [        L a  UR                  5        g U5        g N)r   
set_resulttyper
   r,   )r   
stop_events     r"   _set_stop_eventWorkflowHandler._set_stop_eventD   s3    % 	!%j!1Y!>J	
DN	
r$   c                4    [        U R                  5       5      $ r0   )strr,   r&   s    r"   __str__WorkflowHandler.__str__M   s    4;;=!!r$   c                "    U R                  5       $ )z,Return True when the workflow has completed.)doner&   s    r"   is_doneWorkflowHandler.is_doneP   s    yy{r$   c                 #    U R                   (       a  Sn[        U5      eU R                  R                  5         Sh  vN n[	        U[
        5      (       a	  U(       d  M'  U7v   [	        U[        5      (       d  MC  SU l           g NG
 g7f)a{  
Stream events from the workflow execution as they occur.

This method provides real-time access to events generated during workflow
execution, allowing for monitoring and processing of intermediate results.
Events are yielded in the order they are generated by the workflow.

The stream includes all events written to the context's streaming queue,
and terminates when a [StopEvent][workflows.events.StopEvent] is
encountered, indicating the workflow has completed.

Args:
    expose_internal (bool): Whether to expose internal events.

Returns:
    AsyncGenerator[Event, None]: An async generator that yields Event objects
        as they are produced by the workflow.

Raises:
    ValueError: If the context is not set on the handler.
    WorkflowRuntimeError: If all events have already been consumed by a
        previous call to `stream_events()` on the same handler instance.

Examples:
    ```python
    handler = workflow.run()

    # Stream and process events in real-time
    async for event in handler.stream_events():
        if isinstance(event, StopEvent):
            print(f"Workflow completed with result: {event.result}")
        else:
            print(f"Received event: {event}")

    # Get final result
    result = await handler
    ```

Note:
    Events can only be streamed once per handler instance. Subsequent
    calls to `stream_events()` will raise a WorkflowRuntimeError.
z3All the streamed events have already been consumed.NT)r   r   r   stream_events
isinstancer   r
   )r   expose_internalmsgevs       r"   r?   WorkflowHandler.stream_eventsT   sp     ^ $$GC&s++..0 	""344_H"i((,0)	0s-   9BB	B B	7B>	BB		Bc                   #    U R                   (       a>  U R                   R                  5         U R                  b   U R                  I Sh  vN   ggg N! [         a     gf = f7f)a)  Cancel the running workflow.

Signals the underlying context to raise
[WorkflowCancelledByUser][workflows.errors.WorkflowCancelledByUser],
which will be caught by the workflow and gracefully end the run.

Examples:
    ```python
    handler = workflow.run()
    await handler.cancel_run()
    ```
N)r   _workflow_cancel_runr   	Exceptionr&   s    r"   
cancel_runWorkflowHandler.cancel_run   sZ      88HH))+~~)..(( *  )  s:   9A%A AA A%A 
A"A%!A""A%)r   r   r   r   r   )r   r   r   r   r   z
str | Noner   r   r    r   returnNone)rJ   r   )rJ   r   )rJ   r
   )r3   r
   rJ   rK   )rJ   r7   )rJ   r   )F)rA   r   rJ   zAsyncGenerator[Event, None])rJ   rK   )__name__
__module____qualname____firstlineno____doc____annotations__r   propertyr   r)   r-   r4   r8   r<   r?   rH   __static_attributes____classcell__)r!   s   @r"   r   r      s    
 M((!! ".2** * 	*
 ,* * 
* *    
"
 ',:#:	$:x r$   r   )
__future__r   asynciotypingr   r   r   errorsr   eventsr	   r
   r   typesr   contextr   Futurer    r$   r"   <module>r^      s@    #  5 5 ) ; ;   PgnnZ0 Pr$   