o
    i+                  
   @  s  d Z ddlmZ ddlZddlmZmZmZmZ ddl	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 dd	lmZ dd
lmZmZmZ ddlmZ ddlmZ ddl m!Z! erfddl"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)edddZ*	 e
G dd dee* Z+edddZ,	 edddZ-	 e
ddG dd  d ee%e&e,e*e'f Z.dS )!a:  Decision node implementation for conditional branching in graph execution.

This module provides the Decision node type and related classes for implementing
conditional branching logic in parallel control flow graphs. Decision nodes allow the graph
to choose different execution paths based on runtime conditions.
    )annotationsN)AsyncIterableCallableIterableSequence)	dataclass)TYPE_CHECKINGAnyGeneric
get_origin)NeverSelfTypeVar)BaseNode)NodeID)PathPathBuilderTransformFunction)NodeStep)TypeOrTypeExpression)GraphBuildingError)AnyDestinationNodeDestinationNodeStateTT)infer_varianceDepsTHandledTT)kw_onlyc                   @  sD   e Zd ZU dZded< 	 ded< 	 ded< 	 dddZdddZdS )DecisionzDecision node for conditional branching in graph execution.

    A Decision node evaluates conditions and routes execution to different
    branches based on the input data type or custom matching logic.
    r   idzlist[DecisionBranch[Any]]branches
str | NonenotebranchDecisionBranch[T]return%Decision[StateT, DepsT, HandledT | T]c                 C  s   t | j| j|g | jdS )zAdd a new branch to this decision.

        Args:
            branch: The branch to add to this decision.

        Returns:
            A new Decision with the additional branch.
        )r    r!   r#   )r   r    r!   r#   )selfr$    r)   b/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/pydantic_graph/beta/decision.pyr$   9   s   	zDecision.branchinputsr   r   c                 C  s   t d)aq  Forces this type to be contravariant in the HandledT type variable.

        This is an implementation detail of how we can type-check that all possible input types have
        been exhaustively covered.

        Args:
            inputs: Input data of handled types.

        Raises:
            RuntimeError: Always, as this method should never be executed.
        zKThis method should never be called, it is just defined for typing purposes.)RuntimeError)r(   r+   r)   r)   r*   _force_handled_contravariantD   s   z%Decision._force_handled_contravariantN)r$   r%   r&   r'   )r+   r   r&   r   )__name__
__module____qualname____doc____annotations__r$   r-   r)   r)   r)   r*   r   (   s   
 
r   SourceTc                   @  s8   e Zd ZU dZded< 	 ded< 	 ded< 	 ded	< d
S )DecisionBrancha  Represents a single branch within a decision node.

    Each branch defines the conditions under which it should be taken
    and the path to follow when those conditions are met.

    Note: with the current design, it is actually _critical_ that this class is invariant in SourceT for the sake
    of type-checking that inputs to a Decision are actually handled. See the `# type: ignore` comment in
    `tests.graph.beta.test_graph_edge_cases.test_decision_no_matching_branch` for an example of how this works.
    TypeOrTypeExpression[SourceT]sourceCallable[[Any], bool] | Nonematchesr   pathzlist[AnyDestinationNode]destinationsN)r.   r/   r0   r1   r2   r)   r)   r)   r*   r4   W   s   
 
r4   OutputT
NewOutputTF)initc                   @  s   e Zd ZU dZded< 	 ded< 	 ded< 	 ded	< 	 d0ddZddd1ddZddd2ddZd3d"d#Zddd$d4d)d*Z	d5d.d/Z
dS )6DecisionBranchBuilderaY  Builder for constructing decision branches with fluent API.

    This builder provides methods to configure branches with destinations,
    forks, and transformations in a type-safe manner.

    Instances of this class should be created using [`GraphBuilder.match`][pydantic_graph.beta.graph_builder.GraphBuilder],
    not created directly.
    !Decision[StateT, DepsT, HandledT]	_decisionr5   _sourcer7   _matches#PathBuilder[StateT, DepsT, OutputT]_path_builderdecisionr6   r8   path_builderc                C  s   || _ || _|| _|| _d S N)r@   rA   rB   rD   )r(   rE   r6   r8   rF   r)   r)   r*   __init__   s   	
zDecisionBranchBuilder.__init__Nfork_iddestinationLDestinationNode[StateT, DepsT, OutputT] | type[BaseNode[StateT, DepsT, Any]]extra_destinationsrJ   r"   r&   DecisionBranch[SourceT]c               G  sV   t |p|}tdd |D }dd |g|R D }t| j| j| jj|d|i|dS )a  Set the destination(s) for this branch.

        Args:
            destination: The primary destination node.
            *extra_destinations: Additional destination nodes.
            fork_id: Optional node ID to use for the resulting broadcast fork if multiple destinations are provided.

        Returns:
            A completed DecisionBranch with the specified destinations.
        c                 s  s    | ]	}t |p	|V  qd S rG   )r   .0dr)   r)   r*   	<genexpr>   s    z+DecisionBranchBuilder.to.<locals>.<genexpr>c                 S  s"   g | ]}t |rt|n|qS r)   )inspectisclassr   rO   r)   r)   r*   
<listcomp>   s   " z,DecisionBranchBuilder.to.<locals>.<listcomp>rJ   r6   r8   r9   r:   )r   tupler4   rA   rB   rD   to)r(   rK   rJ   rM   r:   r)   r)   r*   rX      s   zDecisionBranchBuilder.to	get_forks3Callable[[Self], Sequence[DecisionBranch[SourceT]]]c               C  s\   || }dd |D }|st d| d| jj||d}dd |D }t| j| j||dS )aa  Broadcast this decision branch into multiple destinations.

        Args:
            get_forks: The callback that will return a sequence of decision branches to broadcast to.
            fork_id: Optional node ID to use for the resulting broadcast fork.

        Returns:
            A completed DecisionBranch with the specified destinations.
        c                 S  s   g | ]}|j qS r)   )r9   )rP   br)   r)   r*   rU      s    z3DecisionBranchBuilder.broadcast.<locals>.<listcomp>zThe call to z4 returned no branches, but must return at least one.rI   c                 S  s   g | ]
}|j D ]}|qqS r)   )r:   )rP   fdprQ   r)   r)   r*   rU      s    rV   )r   rD   	broadcastr4   rA   rB   )r(   rY   rJ   fork_decision_branches	new_pathsr9   r:   r)   r)   r*   r]      s   zDecisionBranchBuilder.broadcastfunc5TransformFunction[StateT, DepsT, OutputT, NewOutputT]CDecisionBranchBuilder[StateT, DepsT, NewOutputT, SourceT, HandledT]c                C     t | j| j| j| j|dS )a  Apply a transformation to the branch's output.

        Args:
            func: Transformation function to apply.

        Returns:
            A new DecisionBranchBuilder where the provided transform is applied prior to generating the final output.
        rE   r6   r8   rF   )r>   r@   rA   rB   rD   	transform)r(   r`   r)   r)   r*   re         
zDecisionBranchBuilder.transformrJ   downstream_join_idr(   DecisionBranchBuilder[StateT, DepsT, Iterable[T], SourceT, HandledT] | DecisionBranchBuilder[StateT, DepsT, AsyncIterable[T], SourceT, HandledT]rh   :DecisionBranchBuilder[StateT, DepsT, T, SourceT, HandledT]c                C  s"   t | j| j| j| jj||ddS )a3  Spread the branch's output.

        To do this, the current output must be iterable, and any subsequent steps in the path being built for this
        branch will be applied to each item of the current output in parallel.

        Args:
            fork_id: Optional ID for the fork, defaults to a generated value
            downstream_join_id: Optional ID of a downstream join node which is involved when mapping empty iterables

        Returns:
            A new DecisionBranchBuilder where mapping is performed prior to generating the final output.
        rg   rd   )r>   r@   rA   rB   rD   map)r(   rJ   rh   r)   r)   r*   rk      s   zDecisionBranchBuilder.maplabelstr@DecisionBranchBuilder[StateT, DepsT, OutputT, SourceT, HandledT]c                 C  rc   )aR  Apply a label to the branch at the current point in the path being built.

        These labels are only used in generated mermaid diagrams.

        Args:
            label: The label to apply.

        Returns:
            A new DecisionBranchBuilder where the label has been applied at the end of the current path being built.
        rd   )r>   r@   rA   rB   rD   rl   )r(   rl   r)   r)   r*   rl     rf   zDecisionBranchBuilder.label)rE   r?   r6   r5   r8   r7   rF   rC   )rK   rL   rM   rL   rJ   r"   r&   rN   )rY   rZ   rJ   r"   r&   rN   )r`   ra   r&   rb   )r(   ri   rJ   r"   rh   r"   r&   rj   )rl   rm   r&   rn   )r.   r/   r0   r1   r2   rH   rX   r]   re   rk   rl   r)   r)   r)   r*   r>      s(   
 	

r>   )/r1   
__future__r   rS   collections.abcr   r   r   r   dataclassesr   typingr   r	   r
   r   typing_extensionsr   r   r   pydantic_graphr   pydantic_graph.beta.id_typesr   pydantic_graph.beta.pathsr   r   r   pydantic_graph.beta.stepr   pydantic_graph.beta.utilr   pydantic_graph.exceptionsr   pydantic_graph.beta.node_typesr   r   r   r   r   r   r   r3   r4   r;   r<   r>   r)   r)   r)   r*   <module>   sF    *'$