o
    i!                     @  s  d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	m
Z
mZmZmZmZ ddlmZ ddlmZ ddlmZmZmZ ed	d
dZedd
dZedd
dZedd
dZeddG dd de
eeef ZG dd deeeeef ZG dd deeeeef Zee	e	e	e	f Z	 eddG dd de
eeeef ZeG dd deeee	f Z G dd deeee	eeee	f ee	 B f Z!dS )zStep-based graph execution components.

This module provides the core abstractions for step-based graph execution,
including step contexts, step functions, and step nodes that bridge between
the v1 and v2 graph execution systems.
    )annotations)AsyncIterator	Awaitable)	dataclass)AnyGenericProtocolcast
get_originoverload)TypeVar)NodeID)BaseNodeEndGraphRunContextStateTT)infer_varianceDepsTInputTOutputTF)initc                   @  sd   e Zd ZU dZded< 	 ded< 	 ded< 	 dddZedddZedddZedddZ	dS )StepContextaM  Context information passed to step functions during graph execution.

    The step context provides access to the current graph state, dependencies, and input data for a step.

    Type Parameters:
        StateT: The type of the graph state
        DepsT: The type of the dependencies
        InputT: The type of the input data
    r   _stater   _depsr   _inputsstatedepsinputsc                C     || _ || _|| _d S N)r   r   r   )selfr   r   r    r!   ^/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/pydantic_graph/beta/step.py__init__,      
zStepContext.__init__returnc                 C     | j S r   )r   r    r!   r!   r"   r   1      zStepContext.statec                 C  r&   r   )r   r'   r!   r!   r"   r   5   r(   zStepContext.depsc                 C  r&   )zkThe input data for this step.

        This must be a property to ensure correct variance behavior
        )r   r'   r!   r!   r"   r   9   s   zStepContext.inputsN)r   r   r   r   r   r   )r%   r   )r%   r   )r%   r   )
__name__
__module____qualname____doc____annotations__r#   propertyr   r   r   r!   r!   r!   r"   r      s   
 

r   c                   @     e Zd ZdZd	ddZdS )
StepFunctionab  Protocol for step functions that can be executed in the graph.

    Step functions are async callables that receive a step context and return a result.

    Type Parameters:
        StateT: The type of the graph state
        DepsT: The type of the dependencies
        InputT: The type of the input data
        OutputT: The type of the output data
    ctx"StepContext[StateT, DepsT, InputT]r%   Awaitable[OutputT]c                 C  s   t )zExecute the step function with the given context.

        Args:
            ctx: The step context containing state, dependencies, and inputs

        Returns:
            An awaitable that resolves to the step's output
        NotImplementedErrorr    r1   r!   r!   r"   __call__N   s   	zStepFunction.__call__N)r1   r2   r%   r3   r)   r*   r+   r,   r7   r!   r!   r!   r"   r0   B       r0   c                   @  r/   )
StreamFunctionao  Protocol for stream functions that can be executed in the graph.

    Stream functions are async callables that receive a step context and return an async iterator.

    Type Parameters:
        StateT: The type of the graph state
        DepsT: The type of the dependencies
        InputT: The type of the input data
        OutputT: The type of the output data
    r1   r2   r%   AsyncIterator[OutputT]c                 c  s    t )zExecute the stream function with the given context.

        Args:
            ctx: The step context containing state, dependencies, and inputs

        Returns:
            An async iterator yielding the streamed output
        r4   r6   r!   r!   r"   r7   f   s   	zStreamFunction.__call__N)r1   r2   r%   r;   r8   r!   r!   r!   r"   r:   Z   r9   r:   c                   @  sx   e Zd ZU dZded< 	 ded< 	 ded< 	 dd	dddZedddZeddddZ	edddZ	ddddZ	dS )Stepa  A step in the graph execution that wraps a step function.

    Steps represent individual units of execution in the graph, encapsulating
    a step function along with metadata like ID and label.

    Type Parameters:
        StateT: The type of the graph state
        DepsT: The type of the dependencies
        InputT: The type of the input data
        OutputT: The type of the output data
    r   id,StepFunction[StateT, DepsT, InputT, OutputT]_call
str | NonelabelN)rA   callc                C  r   r   )r=   r?   rA   )r    r=   rB   rA   r!   r!   r"   r#      r$   zStep.__init__r%   c                 C  r&   )zXThe step function to execute. This needs to be a property for proper variance inference.)r?   r'   r!   r!   r"   rB      s   z	Step.callr   NoneStepNode[StateT, DepsT]c                 C     d S r   r!   r    r   r!   r!   r"   as_node      zStep.as_noder   c                 C  rE   r   r!   rF   r!   r!   r"   rG      rH   InputT | Nonec                 C  s
   t | |S )zCreate a step node with bound inputs.

        Args:
            inputs: The input data to bind to this step, or None

        Returns:
            A [`StepNode`][pydantic_graph.beta.step.StepNode] with this step and the bound inputs
        )StepNoderF   r!   r!   r"   rG      s   
	)r=   r   rB   r>   rA   r@   )r%   r>   r   )r   rC   r%   rD   )r   r   r%   rD   )r   rI   r%   rD   )
r)   r*   r+   r,   r-   r#   r.   rB   r   rG   r!   r!   r!   r"   r<   w   s    
 r<   c                   @  s0   e Zd ZU dZded< 	 ded< 	 dd
dZdS )rJ   aI  A base node that represents a step with bound inputs.

    StepNode bridges between the v1 and v2 graph execution systems by wrapping
    a [`Step`][pydantic_graph.beta.step.Step] with bound inputs in a BaseNode interface.
    It is not meant to be run directly but rather used to indicate transitions
    to v2-style steps.
    zStep[StateT, DepsT, Any, Any]stepr   r   r1   GraphRunContext[StateT, DepsT]r%   'BaseNode[StateT, DepsT, Any] | End[Any]c                   s
   t d)a	  Attempt to run the step node.

        Args:
            ctx: The graph execution context

        Returns:
            The result of step execution

        Raises:
            NotImplementedError: Always raised as StepNode is not meant to be run directly
        z`StepNode` is not meant to be run directly, it is meant to be used in `BaseNode` subclasses to indicate a transition to v2-style steps.r4   r6   r!   r!   r"   run   s   zStepNode.runN)r1   rL   r%   rM   )r)   r*   r+   r,   r-   rN   r!   r!   r!   r"   rJ      s   
 rJ   c                      s@   e Zd ZU dZded< 	 dddd fd
dZdddZ  ZS )NodeStepa  A step that wraps a BaseNode type for execution.

    NodeStep allows v1-style BaseNode classes to be used as steps in the
    v2 graph execution system. It validates that the input is of the expected
    node type and runs it with the appropriate graph context.
    "type[BaseNode[StateT, DepsT, Any]]	node_typeN)r=   rA   r=   NodeID | NonerA   r@   c                  s2   t  j|p
t| | j|d t|p|| _dS )a   Initialize a node step.

        Args:
            node_type: The BaseNode class this step will execute
            id: Optional unique identifier, defaults to the node's get_node_id()
            label: Optional human-readable label for this step
        )r=   rB   rA   N)superr#   r   get_node_id
_call_noder
   rQ   )r    rQ   r=   rA   	__class__r!   r"   r#      s   zNodeStep.__init__r1   StepContext[StateT, DepsT, Any]r%   rM   c                   sZ   |j }t|| jstd| d| j tttttf |}|	t
|j|jdI dH S )aA  Execute the wrapped node with the step context.

        Args:
            ctx: The step context containing the node instance to run

        Returns:
            The result of running the node, either another BaseNode or End

        Raises:
            ValueError: If the input node is not of the expected type
        zNode z is not of type )r   r   N)r   
isinstancerQ   
ValueErrorr	   r   r   r   r   rN   r   r   r   )r    r1   noder!   r!   r"   rU      s   zNodeStep._call_node)rQ   rP   r=   rR   rA   r@   )r1   rX   r%   rM   )r)   r*   r+   r,   r-   r#   rU   __classcell__r!   r!   rV   r"   rO      s   
 rO   N)"r,   
__future__r   collections.abcr   r   dataclassesr   typingr   r   r   r	   r
   r   typing_extensionsr   pydantic_graph.beta.id_typesr   pydantic_graph.nodesr   r   r   r   r   r   r   r   r0   r:   AnyStepFunctionr<   rJ   rO   r!   r!   r!   r"   <module>   s.     (02"