
    g3fi=                         d Z ddlmZmZmZmZmZmZ ddlm	Z	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 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	Z  G d
 deeef         Z!y)z?Runnable that selects which branch to run based on a condition.    )AsyncIterator	AwaitableCallableIteratorMappingSequence)Anycast)	BaseModel
ConfigDict)override)RunnableRunnableLikeRunnableSerializablecoerce_to_runnable)RunnableConfigensure_config%get_async_callback_manager_for_configget_callback_manager_for_configpatch_config)ConfigurableFieldSpecInputOutputget_unique_config_specs   c                        e Zd ZU dZeeeeef   eee	f   f      e
d<   	 eee	f   e
d<   	 deeeef   eegef   z  eegee   f   z  ef   ez  ddf fdZ ed      Zedefd	       Zeedee   fd
              Zeddedz  dee   f fd       Zeedee   fd              Ze	 ddededz  dede	fd       Ze	 ddededz  dede	fd       Z e	 ddededz  dedz  de!e	   fd       Z"e	 ddededz  dedz  de#e	   fd       Z$ xZ%S )RunnableBrancha\  `Runnable` that selects which branch to run based on a condition.

    The `Runnable` is initialized with a list of `(condition, Runnable)` pairs and
    a default branch.

    When operating on an input, the first condition that evaluates to True is
    selected, and the corresponding `Runnable` is run on the input.

    If no condition evaluates to `True`, the default branch is run on the input.

    Examples:
        ```python
        from langchain_core.runnables import RunnableBranch

        branch = RunnableBranch(
            (lambda x: isinstance(x, str), lambda x: x.upper()),
            (lambda x: isinstance(x, int), lambda x: x + 1),
            (lambda x: isinstance(x, float), lambda x: x * 2),
            lambda x: "goodbye",
        )

        branch.invoke("hello")  # "HELLO"
        branch.invoke(None)  # "goodbye"
        ```
    branchesdefaultreturnNc           	      :   t        |      t        k  rd}t        |      |d   }t        |t        t
        t        f      sd}t        |      t        dt        t        d|                  }g }|dd D ]  }t        |t        t        f      sdt        |       }t        |      t        |      t        k7  rdt        |       }t        |      |\  }}t        d	t        |            }t        |      }|j                  ||f        t        	| =  ||
       y)a  A `Runnable` that runs one of two branches based on a condition.

        Args:
            *branches: A list of `(condition, Runnable)` pairs.
                Defaults a `Runnable` to run if no condition is met.

        Raises:
            ValueError: If the number of branches is less than `2`.
            TypeError: If the default branch is not `Runnable`, `Callable` or `Mapping`.
            TypeError: If a branch is not a `tuple` or `list`.
            ValueError: If a branch is not of length `2`.
        z-RunnableBranch requires at least two branchesz=RunnableBranch default must be Runnable, callable or mapping.zRunnable[Input, Output]r   Nz5RunnableBranch branches must be tuples or lists, not zARunnableBranch branches must be tuples or lists of length 2, not zRunnable[Input, bool])r   r   )len_MIN_BRANCHES
ValueError
isinstancer   r   r   	TypeErrorr
   r   tuplelisttypeappendsuper__init__)
selfr   msgr   default_	branches_branch	conditionrunnable	__class__s
            ]/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_core/runnables/branch.pyr-   zRunnableBranch.__init__J   s?   , x==(ACS/!2,x)
 RCC. %'9$~w:W'X
 	sm 	4Ffudm4,,0L>;   n$6{m+88;F}F  !o%"(Ix46H6STI)(3Hi23#	4& 	 	 	
    T)arbitrary_types_allowedc                      y)z,Return `True` as this class is serializable.T clss    r6   is_lc_serializablez!RunnableBranch.is_lc_serializable   s     r7   c                 
    g dS )zwGet the namespace of the LangChain object.

        Returns:
            `["langchain", "schema", "runnable"]`
        )	langchainschemar4   r:   r;   s    r6   get_lc_namespacezRunnableBranch.get_lc_namespace   s
     32r7   configc                 f   | j                   g| j                  D cg c]  \  }}|	 c}}z   | j                  D cg c]  \  }}|	 c}}z   }|D ]D  }|j                  |      j                         j	                  d      	 3|j                  |      c S  t
        | 	  |      S c c}}w c c}}w )Nr*   )r   r   get_input_schemamodel_json_schemagetr,   )r.   rB   _r	runnablesr4   r5   s         r6   rD   zRunnableBranch.get_input_schema   s     \\N!]]+TQq+,!]]+TQq+, 	 " 	9H))&1CCEII&Q  0088	9 w'// ,+s
   B'B-c           
          t        d | j                  g| j                  D cg c]  \  }}|	 c}}z   | j                  D cg c]  \  }}|	 c}}z   D              S c c}}w c c}}w )Nc              3   B   K   | ]  }|j                   D ]  }|   y wN)config_specs).0stepspecs      r6   	<genexpr>z.RunnableBranch.config_specs.<locals>.<genexpr>   s5      '

 ))'
  '
'
s   )r   r   r   )r.   rG   rH   s      r6   rM   zRunnableBranch.config_specs   sd     ' '
 !%/A1/0!%/A1/0'
 
 	
 0/s   AA$inputkwargsc                    t        |      }t        |      }|j                  d||j                  d      xs | j	                         |j                  dd            }	 t        | j                        D ]x  \  }}|\  }}	|j                  |t        ||j                  d|dz                	      }
|
sB |	j                  |fd
t        ||j                  d|dz                i|} n;  | j                  j                  |fd
t        ||j                  d            i|}|j                  |       |S # t        $ r}|j                  |        d}~ww xY w)a^  First evaluates the condition, then delegate to `True` or `False` branch.

        Args:
            input: The input to the `Runnable`.
            config: The configuration for the `Runnable`.
            **kwargs: Additional keyword arguments to pass to the `Runnable`.

        Returns:
            The output of the branch that was run.
        Nrun_namerun_idnamerV   
condition:   tag	callbacksrB   rB   branch:branch:default)r   r   on_chain_startrF   get_namepop	enumerater   invoker   	get_childr   BaseExceptionon_chain_erroron_chain_endr.   rR   rB   rS   callback_managerrun_manageridxr2   r3   r4   expression_valueoutputes                r6   rf   zRunnableBranch.invoke   s    v&:6B&55J':4==?::h-	 6 
 	(7 V&,#	8#,#3#3'"-"7"7jq	<R"7"S $4 $  $,X__+"&1&;&;'#PQ'@S&;&T  !F ), -,,'+*?*?DT*?*U
  	  (	  	&&q)	s    AD; 5A3D; ;	EEEc                 <  K   t        |      }t        |      }|j                  d ||j                  d      xs | j	                         |j                  dd              d {   }	 t        | j                        D ]  \  }}|\  }}	|j                  |t        ||j                  d|dz                       d {   }
|
sJ |	j                  |fd	t        ||j                  d
|dz                i| d {   } nC  | j                  j                  |fd	t        ||j                  d            i| d {   }|j                  |       d {    |S 7 7 7 i7 &# t        $ r }|j                  |       d {  7    d }~ww xY w7 ;w)NrU   rV   rW   rY   rZ   r[   r]   r_   rB   r`   ra   )r   r   rb   rF   rc   rd   re   r   ainvoker   rg   r   rh   ri   rj   rk   s                r6   rs   zRunnableBranch.ainvoke   s     v&@H,;;J':4==?::h-	 < 
 
 	(7 V&,#	8)2):):'"-"7"7jq	<R"7"S *; * $  $#38#3#3$+"&1&;&;'#PQ'@S&;&T $ !$ F ),  4t||33 '+*?*?DT*?*U 
    &&v...Q
$  	,,Q///	 	/s   A F"E%#F(AE. ?E( E. 9E.  E*AE. E,E. 
FFF(E. *E. ,E. .	F7FFFFFc              +   r  K   t        |      }t        |      }|j                  d||j                  d      xs | j	                         |j                  dd            }d}d}	 t        | j                        D ]  \  }}	|	\  }
}|
j                  |t        ||j                  d|dz          	      
      }|sB |j                  |fdt        ||j                  d|dz          	      i|D ]  }| |s
||}	 ||z   }  nR  | j                  j                  |fdt        ||j                  d      	      i|D ]  }| |s
||}	 ||z   } |j!                  |       y# t        $ r d}d}Y w xY w# t        $ r d}d}Y Kw xY w# t        $ r}|j                  |        d}~ww xY wwa]  First evaluates the condition, then delegate to `True` or `False` branch.

        Args:
            input: The input to the `Runnable`.
            config: The configuration for the `Runnable`.
            **kwargs: Additional keyword arguments to pass to the `Runnable`.

        Yields:
            The output of the branch that was run.
        NrU   rV   rW   TrY   rZ   r[   r]   r_   rB   r`   Fra   )r   r   rb   rF   rc   rd   re   r   rf   r   rg   streamr'   r   rh   ri   rj   r.   rR   rB   rS   rl   rm   final_outputfinal_output_supportedrn   r2   r3   r4   ro   chunkrq   s                  r6   rv   zRunnableBranch.stream'  s:    " v&:6B&55J':4==?::h-	 6 
 '+!%5	(7 1?V&,#	8#,#3#3'"-"7"7jq	<R"7"S $4 $  $!0"+"&1&;&;'#PQ'@S&;&T " !" C $1+3/4!C3?%3GLC$ =1?@ 1T\\00'"-"7"7<L"7"M  ?E  K-'/+0L?/;e/C?* 	  .5 (1 !C37L=B$:!C( $- ?/39> 6?  	&&q)	s   A!F7$AF ;?F ;F E.AF F FF F7.E>;F =E>>F FF FF 	F4F//F44F7c                  K   t        |      }t        |      }|j                  d||j                  d      xs | j	                         |j                  dd             d{   }d}d}	 t        | j                        D ]  \  }}	|	\  }
}|
j                  |t        ||j                  d|dz          	      
       d{   }|sJ |j                  |fdt        ||j                  d|dz          	      i|2 3 d{   }| |s||}	 ||z   }  | j                  j                  |fdt        ||j                  d      	      i|2 3 d{   }| |s||}	 ||z   }7 7 7 u# t        $ r d}d}Y w xY w6  n7 5# t        $ r d}d}Y Kw xY w6 n,# t        $ r }|j                  |       d{  7    d}~ww xY w|j!                  |       d{  7   ywru   )r   r   rb   rF   rc   rd   re   r   rs   r   rg   astreamr'   r   rh   ri   rj   rw   s                  r6   r|   zRunnableBranch.astream{  sw    " v&@H,;;J':4==?::h-	 < 
 
 '+!%5	(7 1?V&,#	8)2):):'"-"7"7jq	<R"7"S *; * $  $'7x'7'7(+"&1&;&;'#PQ'@S&;&T ( !( C Ce $1+3/4!C3?%3GL51?@ $84<<#7#7$'"-"7"7<L"7"M$ $ ? ?%  K-'/+0L?/;e/Cq
$C (1 !C37L=B$:!C($ ? $- ?/39> 6?$$  	,,Q///	 &&|444s   A G1"E6#G1,AF* E9F* 6F* FE;F	F* F* E=<F* F(FF(!F* *F* 0F5F* 6G19F* ;F=F
F* FF* F(F%"F* $F%%F* )G1*	G3GG
GGG1*G-+G1rL   )&__name__
__module____qualname____doc__r   r(   r   r   boolr   __annotations__r   r   r   r-   r   model_configclassmethodr=   r   r)   strrA   r   r*   r   rD   propertyr   rM   r	   rf   rs   r   rv   r   r|   __classcell__)r5   s   @r6   r   r   *   st   4 uXeTk2HUF]4KKLMM2eVm$$5?
UD[!w}%&w	$/01 
 ?
 
?
B  $L 4   3c 3  3 0~'< 0Y 0 0  	
d#89 	
  	
 <@88$2T$98LO8	8 8t <@--$2T$9-LO-	- -^  )-Q/Q/ %Q/ *	Q/
 
&	Q/ Q/f  )-Q5Q5 %Q5 *	Q5
 
v	Q5 Q5r7   r   N)"r   collections.abcr   r   r   r   r   r   typingr	   r
   pydanticr   r   typing_extensionsr   langchain_core.runnables.baser   r   r   r   langchain_core.runnables.configr   r   r   r   r   langchain_core.runnables.utilsr   r   r   r   r$   r   r:   r7   r6   <module>r      s\    E 
 + &    c5)%-8 c5r7   