o
    iB                  	   @  sR  d Z ddlmZ ddlZ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mZmZmZ ddlmZmZmZmZ dd	lmZmZ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dZ#edddZ$edddZ%eG dd dZ&eddG dd deee f Z'edee"e!ge"f e!e"fdZ(edee'ee f e"e!ge"f ee e!e"fdZ)ede)ee e!e"f e(e!e"f B ee e!e"fdZ*	 d;d#d$Z+d<d&d'Z,d=d)d*Z-d>d-d.Z.G d/d0 d0eZ/ed1e/dd2Z0d?d3d4Z1eG d5d6 d6ee# Z2eddG d7d8 d8eee e!e"f Z3eG d9d: d:eee ef Z4dS )@zJoin operations and reducers for graph execution.

This module provides the core components for joining parallel execution paths
in a graph, including various reducer types that aggregate data from multiple
sources into a single output.
    )annotationsN)abstractmethod)CallableIterableMapping)	dataclass)AnyGenericLiteralcastoverload)ProtocolSelfTypeAliasTypeTypeVar)BaseNodeEndGraphRunContext)ForkID	ForkStackJoinIDStateTT)infer_varianceDepsTInputTOutputTTKVc                   @  s.   e Zd ZU dZded< ded< dZded< d	S )
	JoinStatezOThe state of a join during graph execution associated to a particular fork run.r   currentr   downstream_fork_stackFboolcancelled_sibling_tasksN)__name__
__module____qualname____doc____annotations__r#    r)   r)   ^/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/pydantic_graph/beta/join.pyr      s
   
 r   F)initc                   @  s^   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dd Z	dS )ReducerContexta  Context information passed to reducer functions during graph execution.

    The reducer context provides access to the current graph state and dependencies.

    Type Parameters:
        StateT: The type of the graph state
        DepsT: The type of the dependencies
    r   _stater   _depsr   _join_statestatedeps
join_statec                C  s   || _ || _|| _d S N)r-   r.   r/   )selfr0   r1   r2   r)   r)   r*   __init__:   s   
zReducerContext.__init__returnc                 C     | j S )zThe state of the graph run.)r-   r4   r)   r)   r*   r0   ?      zReducerContext.statec                 C  r7   )zThe deps for the graph run.)r.   r8   r)   r)   r*   r1   D   r9   zReducerContext.depsc                 C  s   d| j _dS )zCancel all sibling tasks created from the same fork.

        You can call this if you want your join to have early-stopping behavior.
        TN)r/   r#   r8   r)   r)   r*   cancel_sibling_tasksI   s   z#ReducerContext.cancel_sibling_tasksN)r0   r   r1   r   r2   r   )r6   r   )r6   r   )
r$   r%   r&   r'   r(   r5   propertyr0   r1   r:   r)   r)   r)   r*   r,   (   s   
 	
r,   PlainReducerFunction)type_paramsContextReducerFunctionReducerFunctionr    Noneinputsr   r6   c                 C  s   dS )z8A reducer that discards all input data and returns None.Nr)   r    rA   r)   r)   r*   reduce_nulle      rC   list[T]c                 C     |  | | S )z!A reducer that appends to a list.)appendrB   r)   r)   r*   reduce_list_appendj      
rH   Iterable[T]c                 C  rF   )zA reducer that extends a list.)extendrB   r)   r)   r*   reduce_list_extendp   rI   rL   
dict[K, V]Mapping[K, V]c                 C  rF   )zA reducer that updates a dict.)updaterB   r)   r)   r*   reduce_dict_updatev   rI   rP   c                   @  s   e Zd ZdZedddZdS )	SupportsSumz5A protocol for a type that supports adding to itself.otherr   r6   c                C     d S r3   r)   )r4   rR   r)   r)   r*   __add__   rD   zSupportsSum.__add__N)rR   r   r6   r   )r$   r%   r&   r'   r   rT   r)   r)   r)   r*   rQ   |   s    rQ   NumericT)boundr   c                 C  s   | | S )zA reducer that sums numbers.r)   rB   r)   r)   r*   
reduce_sum   s   rW   c                   @  s   e Zd ZdZddd	Zd
S )ReduceFirstValuezRA reducer that returns the first value it encounters, and cancels all other tasks.ctxReducerContext[object, object]r    r   rA   r6   c                 C  s   |   |S )zThe reducer function.)r:   )r4   rY   r    rA   r)   r)   r*   __call__   s   zReduceFirstValue.__call__N)rY   rZ   r    r   rA   r   r6   r   )r$   r%   r&   r'   r[   r)   r)   r)   r*   rX      s    rX   c                   @  s   e Zd ZU dZded< ded< ded< ded	< d
ed< dddd(ddZedd Zedd Zd)dd Z	e
d*d+d#d$Ze
d,d%d$Zd*d-d'd$ZdS ).Joina  A join operation that synchronizes and aggregates parallel execution paths.

    A join defines how to combine outputs from multiple parallel execution paths
    using a [`ReducerFunction`][pydantic_graph.beta.join.ReducerFunction]. It specifies which fork
    it joins (if any) and manages the initialization of reducers.

    Type Parameters:
        StateT: The type of the graph state
        DepsT: The type of the dependencies
        InputT: The type of input data to join
        OutputT: The type of the final joined output
    r   id/ReducerFunction[StateT, DepsT, InputT, OutputT]_reducerCallable[[], OutputT]_initial_factoryForkID | Noneparent_fork_idzLiteral['closest', 'farthest']preferred_parent_forkNfarthest)rc   rd   reducerinitial_factoryLiteral['farthest', 'closest']c                C  s"   || _ || _|| _|| _|| _d S r3   )r]   r_   ra   rc   rd   )r4   r]   rf   rg   rc   rd   r)   r)   r*   r5      s
   	
zJoin.__init__c                 C  r7   r3   )r_   r8   r)   r)   r*   rf         zJoin.reducerc                 C  r7   r3   )ra   r8   r)   r)   r*   rg      ri   zJoin.initial_factoryrY   ReducerContext[StateT, DepsT]r    r   rA   r   r6   c                 C  sT   t t| jj}|dkrttttf | j||S tt	t
tttf | j|||S )N   )leninspect	signaturerf   
parametersr   r<   r   r   r>   r   r   )r4   rY   r    rA   n_parametersr)   r)   r*   reduce   s    zJoin.reducer@   JoinNode[StateT, DepsT]c                 C  rS   r3   r)   r4   rA   r)   r)   r*   as_node      zJoin.as_nodec                 C  rS   r3   r)   rs   r)   r)   r*   rt      ru   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
        )JoinNoders   r)   r)   r*   rt      s   
	)
r]   r   rf   r^   rg   r`   rc   rb   rd   rh   )rY   rj   r    r   rA   r   r6   r   r3   )rA   r@   r6   rr   )rA   r   r6   rr   )rA   rv   r6   rr   )r$   r%   r&   r'   r(   r5   r;   rf   rg   rq   r   rt   r)   r)   r)   r*   r\      s(   
 


r\   c                   @  s0   e Zd ZU dZded< 	 ded< 	 dd
dZdS )rw   aN  A base node that represents a join item with bound inputs.

    JoinNode bridges between the v1 and v2 graph execution systems by wrapping
    a [`Join`][pydantic_graph.beta.join.Join] 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Join[StateT, DepsT, Any, Any]joinr   rA   rY   GraphRunContext[StateT, DepsT]r6   'BaseNode[StateT, DepsT, Any] | End[Any]c                   s
   t d)a	  Attempt to run the join 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`JoinNode` is not meant to be run directly, it is meant to be used in `BaseNode` subclasses to indicate a transition to v2-style steps.)NotImplementedError)r4   rY   r)   r)   r*   run   s   zJoinNode.runN)rY   ry   r6   rz   )r$   r%   r&   r'   r(   r|   r)   r)   r)   r*   rw      s   
 rw   )r    r@   rA   r   r6   r@   )r    rE   rA   r   r6   rE   )r    rE   rA   rJ   r6   rE   )r    rM   rA   rN   r6   rM   )r    rU   rA   rU   r6   rU   )5r'   
__future__r   rm   abcr   collections.abcr   r   r   dataclassesr   typingr   r	   r
   r   r   typing_extensionsr   r   r   r   pydantic_graphr   r   r   pydantic_graph.beta.id_typesr   r   r   r   r   r   r   r   r   r   r   r,   r<   r>   r?   rC   rH   rL   rP   rQ   rU   rW   rX   r\   rw   r)   r)   r)   r*   <module>   sd    (






	D 