
    Ji'                    ,   d dl mZ d dlmZmZmZ d dlmZ d dlm	Z	m
Z
 d dlmZmZmZmZmZ  G d de      Z G d d	e      Z G d
 de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Ze
e   Zy)    )annotations)
dict_items	dict_keysdict_values)Enum)AnyType)	BaseModel
ConfigDictFieldPrivateAttrmodel_serializerc                       e Zd ZU dZ ed      Z ee      Zde	d<   d fdZ
d fdZd fd	Zdd
ZddZdddZddZd dZd!dZd"dZd#dZd$dZd%dZd&dZ ed      d'd       Z xZS )(DictLikeModela  
    Base Pydantic model class that mimics a dict interface for dynamic fields.

    Known, typed fields behave like regular Pydantic attributes. Any extra
    keyword arguments are stored in an internal dict and can be accessed through
    both attribute and mapping semantics. This hybrid model enables flexible
    event payloads while preserving validation for declared fields.

    PrivateAttr:
        _data (dict[str, Any]): Underlying Python dict for dynamic fields.
    T)arbitrary_types_allowed)default_factorydict[str, Any]_datac                Z   i }i }i }|j                         D ]<  \  }}|| j                  j                  v r|||<   $|| j                  v r|||<   8|||<   > t	        	|   di | |j                         D ]  \  }}t        	|   ||        |r| j                  j                  |       yy)zc
        __init__.

        NOTE: fields and private_attrs are pulled from params by name.
        N )	items	__class__model_fields__private_attributes__super__init____setattr__r   update)
selfparamsfieldsprivate_attrsdatakvprivate_attrvaluer   s
            b/var/www/html/BankruptcyAI-uat/bankruptcy-ai/venv/lib/python3.12/site-packages/workflows/events.pyr   zDictLikeModel.__init__#   s     LLN 	DAqDNN///q	d111#$a Q	 	"6"#0#6#6#8 	5L%Ge4	5JJd#     c                    || j                   v s|| j                  j                  v rt        |   |      S || j
                  vr&t        d| j                  j                   d| d      | j
                  |   S )N'z' object has no attribute ')r   r   r   r   __getattr__r   AttributeError__name__)r   _DictLikeModel__namer   s     r(   r,   zDictLikeModel.__getattr__:   s|    d1114447&v..TZZ'$//00KF8STU  ::f%%r)   c                    || j                   v s|| j                  j                  v rt        |   ||       y | j
                  j                  ||       y N)r   r   r   r   r   r   __setitem__)r   namer'   r   s      r(   r   zDictLikeModel.__setattr__G   sE    4...$$..:U:U2UGe,JJ""4/r)   c                     | j                   |   S r1   r   r   keys     r(   __getitem__zDictLikeModel.__getitem__M   s    zz#r)   c                "    || j                   |<   y r1   r5   )r   r7   r'   s      r(   r2   zDictLikeModel.__setitem__P   s    

3r)   c                :    | j                   j                  ||      S r1   )r   get)r   r7   defaults      r(   r;   zDictLikeModel.getS   s    zz~~c7++r)   c                    || j                   v S r1   r5   r6   s     r(   __contains__zDictLikeModel.__contains__V   s    djj  r)   c                6    | j                   j                         S r1   )r   keysr   s    r(   r@   zDictLikeModel.keysY   s    zz  r)   c                6    | j                   j                         S r1   )r   valuesrA   s    r(   rC   zDictLikeModel.values\   s    zz  ""r)   c                6    | j                   j                         S r1   )r   r   rA   s    r(   r   zDictLikeModel.items_   s    zz!!r)   c                ,    t        | j                        S r1   )lenr   rA   s    r(   __len__zDictLikeModel.__len__b   s    4::r)   c                ,    t        | j                        S r1   )iterr   rA   s    r(   __iter__zDictLikeModel.__iter__e   s    DJJr)   c                    | j                   S r1   r5   )r   argskwargss      r(   to_dictzDictLikeModel.to_dicth   s    zzr)   c                     y)z.Make test `if event:` pass on Event instances.Tr   rA   s    r(   __bool__zDictLikeModel.__bool__k   s    r)   wrapmodec                L     ||       }| j                   r| j                   |d<   |S )Nr   r5   r   handlerr#   s      r(   custom_model_dumpzDictLikeModel.custom_model_dumpo   s$    t}:: JJDMr)   r    r   )r/   strreturnr   )r3   rY   r'   r   rZ   None)r7   rY   rZ   r   )r7   rY   r'   r   rZ   r[   r1   )r7   rY   r<   r   rZ   r   )r7   rY   rZ   bool)rZ   z'dict_keys[str, Any]')rZ   z'dict_values[str, Any]')rZ   z'dict_items[str, Any]')rZ   intrZ   r   )rL   r   rM   r   rZ   r   )rZ   r\   rV   r   rZ   r   )r.   
__module____qualname____doc__r   model_configr   dictr   __annotations__r   r,   r   r8   r2   r;   r>   r@   rC   r   rG   rJ   rN   rP   r   rW   __classcell__r   s   @r(   r   r      s    
 d;L'=E>=$.&0 ,!!#"  6" #r)   r   c                  $     e Zd ZdZd fdZ xZS )Eventa  
    Base class for all workflow events.

    Events are light-weight, serializable payloads passed between steps.
    They support both attribute and mapping access to dynamic fields.

    Examples:
        Subclassing with typed fields:

        ```python
        from pydantic import Field

        class CustomEv(Event):
            score: int = Field(ge=0)

        e = CustomEv(score=10)
        print(e.score)
        ```

    See Also:
        - [StartEvent][workflows.events.StartEvent]
        - [StopEvent][workflows.events.StopEvent]
        - [InputRequiredEvent][workflows.events.InputRequiredEvent]
        - [HumanResponseEvent][workflows.events.HumanResponseEvent]
    c                $    t        |   di | y )Nr   r   r   )r   r    r   s     r(   r   zEvent.__init__   s    "6"r)   rX   )r.   r`   ra   rb   r   rf   rg   s   @r(   ri   ri   x   s    4# #r)   ri   c                      e Zd ZdZy)
StartEventz9Implicit entry event sent to kick off a `Workflow.run()`.Nr.   r`   ra   rb   r   r)   r(   rm   rm      s    Cr)   rm   c                       e Zd ZU dZ ed      Zded<   dd fdZddZe	dd       Z
 ed	
      dd       ZddZddZ xZS )	StopEventa  Terminal event that signals the workflow has completed.

    The `result` property contains the return value of the workflow run. When a
    custom stop event subclass is used, the workflow result is that event
    instance itself.

    Examples:
        ```python
        # default stop event: result holds the value
        return StopEvent(result={"answer": 42})
        ```

        Subclassing to provide a custom result:

        ```python
        class MyStopEv(StopEvent):
            pass

        @step
        async def my_step(self, ctx: Context, ev: StartEvent) -> MyStopEv:
            return MyStopEv(result={"answer": 42})
    N)r<   r   _resultc                (    t        |   dd|i| y )Nrq   r   rk   )r   resultrM   r   s      r(   r   zStopEvent.__init__   s    2262r)   c                    | j                   S )zBThis can be overridden by subclasses to return the desired result.rq   rA   s    r(   _get_resultzStopEvent._get_result   s    ||r)   c                "    | j                         S r1   )rv   rA   s    r(   rs   zStopEvent.result   s    !!r)   rQ   rR   c                L     ||       }| j                   | j                   |d<   |S )Nrs   ru   rU   s      r(   rW   zStopEvent.custom_model_dump   s'    t}<<#!\\DNr)   c                    i | j                   | j                         } |j                         D cg c]  \  }}| d| }}}dj                  |      }| j                  j
                   d| dS c c}}w )N=z, ())r   
model_dumpr   joinr   r.   )r   r   r$   r%   partsdict_strs         r(   __repr__zStopEvent.__repr__   sy    8

8doo&78
*:**:*:*<=$!QA3au==99U#..))*!H:Q77 >s   A4c                ,    t        | j                        S r1   )rY   rq   rA   s    r(   __str__zStopEvent.__str__   s    4<<  r)   r1   )rs   r   rM   r   rZ   r[   r^   r_   )rZ   rY   )r.   r`   ra   rb   r   rq   re   r   rv   propertyrs   r   rW   r   r   rf   rg   s   @r(   rp   rp      sZ    . t,GS,3 " " 6" #8!r)   rp   c                      e Zd ZdZy)InputRequiredEventa  Emitted when human input is required to proceed.

    Automatically written to the event stream if returned from a step.

    If returned from a step, it does not need to be consumed by other steps and will pass validation.
    It's expected that the caller will respond to this event and send back a [HumanResponseEvent][workflows.events.HumanResponseEvent].

    Use this directly or subclass it.

    Typical flow: a step returns `InputRequiredEvent`, callers consume it from
    the stream and send back a [HumanResponseEvent][workflows.events.HumanResponseEvent].

    Examples:
        ```python
        from workflows.events import InputRequiredEvent, HumanResponseEvent

        class HITLWorkflow(Workflow):
            @step
            async def my_step(self, ev: StartEvent) -> InputRequiredEvent:
                return InputRequiredEvent(prefix="What's your name? ")

            @step
            async def my_step(self, ev: HumanResponseEvent) -> StopEvent:
                return StopEvent(result=ev.response)
        ```
    Nrn   r   r)   r(   r   r      s    r)   r   c                      e Zd ZdZy)HumanResponseEventag  Carries a human's response for a prior input request.

    If consumed by a step and not returned by another, it will still pass validation.

    Examples:
        ```python
        from workflows.events import InputRequiredEvent, HumanResponseEvent

        class HITLWorkflow(Workflow):
            @step
            async def my_step(self, ev: StartEvent) -> InputRequiredEvent:
                return InputRequiredEvent(prefix="What's your name? ")

            @step
            async def my_step(self, ev: HumanResponseEvent) -> StopEvent:
                return StopEvent(result=ev.response)
        ```
    Nrn   r   r)   r(   r   r      s    r)   r   c                      e Zd ZdZy)InternalDispatchEventa  
    InternalDispatchEvent is a special event type that exposes processes running inside workflow, even if the user did not explicitly expose them by setting, e.g., `ctx.write_event_to_stream(`.

    Examples:
        ```python
        wf = ExampleWorkflow()
        handler = wf.run(message="Hello, who are you?")

        async for ev in handler.stream_event(expose_internal=True):
            if isinstance(ev, InternalDispatchEvent):
                print(type(ev), ev)
        ```
    Nrn   r   r)   r(   r   r     s     	r)   r   c                      e Zd ZdZdZdZy)	StepState	preparingrunningnot_runningN)r.   r`   ra   	PREPARINGRUNNINGNOT_RUNNINGr   r)   r(   r   r     s    IGKr)   r   c                      e Zd ZU dZ ed      Zded<    ed      Zded<    ed	      Zded
<    ed      Z	ded<    edd      Z
ded<   y)StepStateChangeda  
    StepStateChanged is a special event type that exposes internal changes in the state of the event, including whether the step is running or in progress, what worker it is running on and what events it takes as input and output, as well as changes in the workflow state.

    Attributes:
        name (str): Name of the step
        step_state (StepState): State of the step ("running", "not_running", "in_progress", "not_in_progress", "exited")
        worker_id (str): ID of the worker that the step is running on
        input_event_name (str): Name of the input event
        output_event_name (Optional[str]): Name of the output event
        context_state (dict[str, Any]): Snapshot of the current workflow state
    zName of the step)descriptionrY   r3   zXState of the step ('running', 'not_running', 'in_progress', 'not_in_progress', 'exited')r   
step_statez,ID of the worker that the step is running on	worker_idzName of the input eventinput_event_namezName of the output eventN)r   r<   z
str | Noneoutput_event_name)r.   r`   ra   rb   r   r3   re   r   r   r   r   r   r)   r(   r   r   !  sg    
 "45D#5!nJ	  'UVIsV!.GHcH$).%z r)   r   N)
__future__r   _collections_abcr   r   r   enumr   typingr   r	   pydanticr
   r   r   r   r   r   ri   rm   rp   r   r   r   r   r   	EventTyper   r)   r(   <module>r      s    # ? ?   bI bJ#M #>D D6! 6!r : *	E 	$   , 0 K	r)   