
    g3fi<                       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mZ d dlm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  ed	eee      Z e!eeef   Z" G d
 ded      Z#e$eeez  ez  f   Z% G d de      Z&d$dZ' G d de      Z( G d de	e          Z) G d de*      Z+d%dZ,	 	 	 	 	 	 d&dZ-	 	 	 	 	 	 d&dZ.	 edededediZ/h dZ0dZ1d'd Z2d!d"	 	 	 	 	 	 	 	 	 d(d#Z3y!))    )annotations)AsyncIteratorIteratorMappingSequence)AnyGenericLiteral
NamedTuple	TypedDictTypeVar)RunnableConfig)uuid6)SerializerProtocolmaybe_add_typed_methods)JsonPlusSerializer)ERROR	INTERRUPTRESUME	SCHEDULEDChannelProtocolVc                  4    e Zd ZU dZded<   	 ded<   	 ded<   y)	CheckpointMetadataz&Metadata associated with a checkpoint.z*Literal['input', 'loop', 'update', 'fork']sourceintstepzdict[str, str]parentsN__name__
__module____qualname____doc____annotations__     `/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langgraph/checkpoint/base/__init__.pyr   r      s)    066 I r&   r   F)totalc                  d    e Zd ZU dZded<   	 ded<   	 ded<   	 ded<   	 d	ed
<   	 ded<   	 ded<   y)
Checkpointz(State snapshot at a given point in time.r   vstridtszdict[str, Any]channel_valuesChannelVersionschannel_versionszdict[str, ChannelVersions]versions_seenzlist[str] | Noneupdated_channelsNr   r%   r&   r'   r*   r*   ;   sS    2
F>G' 	G="" &%
 .-
 '&r&   r*   c                L   t        | d   | d   | d   | d   j                         | d   j                         | d   j                         D ci c]  \  }}||j                          c}}| j                  dg       j                         | j                  dd       	      S c c}}w )
Nr+   r.   r-   r/   r1   r2   pending_sendsr3   r+   r.   r-   r/   r1   r2   r5   r3   )r*   copyitemsget)
checkpointkr+   s      r'   copy_checkpointr<   ]   s    
S/dd!"2388:#$67<<>/9//J/P/P/RStq!q!&&({S nn_b9>>@#(:DA	 	 Ts   B c                  L    e Zd ZU dZded<   ded<   ded<   dZd	ed
<   dZded<   y)CheckpointTuplez8A tuple containing a checkpoint and its associated data.r   configr*   r:   r   metadataNRunnableConfig | Noneparent_configzlist[PendingWrite] | Nonepending_writes)r    r!   r"   r#   r$   rB   rC   r%   r&   r'   r>   r>   j   s-    B  +/M(/04N-4r&   r>   c                  T   e Zd ZU dZ e       Zded<   dd	 	 	 ddZedd       Z	ddZ
dd	Zdddd
	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 ddZ	 d	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 ddZddZddZdddd
	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 ddZ	 d	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 ddZd dZy)!BaseCheckpointSavera  Base class for creating a graph checkpointer.

    Checkpointers allow LangGraph agents to persist their state
    within and across multiple interactions.

    Attributes:
        serde (SerializerProtocol): Serializer for encoding/decoding checkpoints.

    Note:
        When creating a custom checkpoint saver, consider implementing async
        versions to avoid blocking the main thread.
    r   serdeN)rF   c               @    t        |xs | j                        | _        y )N)r   rF   )selfrF   s     r'   __init__zBaseCheckpointSaver.__init__   s    
 -U-@djjA
r&   c                    g S )zDefine the configuration options for the checkpoint saver.

        Returns:
            list: List of configuration field specs.
        r%   )rH   s    r'   config_specsz BaseCheckpointSaver.config_specs   s	     	r&   c                B    | j                  |      x}r|j                  S y)zFetch a checkpoint using the given configuration.

        Args:
            config: Configuration specifying which checkpoint to retrieve.

        Returns:
            The requested checkpoint, or `None` if not found.
        N)	get_tupler:   rH   r?   values      r'   r9   zBaseCheckpointSaver.get   s)     NN6**5*### +r&   c                    t         )aY  Fetch a checkpoint tuple using the given configuration.

        Args:
            config: Configuration specifying which checkpoint to retrieve.

        Returns:
            The requested checkpoint tuple, or `None` if not found.

        Raises:
            NotImplementedError: Implement this method in your custom checkpoint saver.
        NotImplementedErrorrH   r?   s     r'   rM   zBaseCheckpointSaver.get_tuple   s
     "!r&   )filterbeforelimitc                   t         )a  List checkpoints that match the given criteria.

        Args:
            config: Base configuration for filtering checkpoints.
            filter: Additional filtering criteria.
            before: List checkpoints created before this configuration.
            limit: Maximum number of checkpoints to return.

        Returns:
            Iterator of matching checkpoint tuples.

        Raises:
            NotImplementedError: Implement this method in your custom checkpoint saver.
        rQ   rH   r?   rT   rU   rV   s        r'   listzBaseCheckpointSaver.list   s
    , "!r&   c                    t         )a   Store a checkpoint with its configuration and metadata.

        Args:
            config: Configuration for the checkpoint.
            checkpoint: The checkpoint to store.
            metadata: Additional metadata for the checkpoint.
            new_versions: New channel versions as of this write.

        Returns:
            RunnableConfig: Updated configuration after storing the checkpoint.

        Raises:
            NotImplementedError: Implement this method in your custom checkpoint saver.
        rQ   rH   r?   r:   r@   new_versionss        r'   putzBaseCheckpointSaver.put   s
    * "!r&   c                    t         )a  Store intermediate writes linked to a checkpoint.

        Args:
            config: Configuration of the related checkpoint.
            writes: List of writes to store.
            task_id: Identifier for the task creating the writes.
            task_path: Path of the task creating the writes.

        Raises:
            NotImplementedError: Implement this method in your custom checkpoint saver.
        rQ   rH   r?   writestask_id	task_paths        r'   
put_writeszBaseCheckpointSaver.put_writes   s
    $ "!r&   c                    t         zDelete all checkpoints and writes associated with a specific thread ID.

        Args:
            thread_id: The thread ID whose checkpoints should be deleted.
        rQ   rH   	thread_ids     r'   delete_threadz!BaseCheckpointSaver.delete_thread   s
     "!r&   c                ^   K   | j                  |       d{   x}r|j                  S y7 w)zAsynchronously fetch a checkpoint using the given configuration.

        Args:
            config: Configuration specifying which checkpoint to retrieve.

        Returns:
            The requested checkpoint, or `None` if not found.
        N)
aget_tupler:   rN   s      r'   agetzBaseCheckpointSaver.aget   s3      //&11151### 21s   -+-c                   K   t         w)ah  Asynchronously fetch a checkpoint tuple using the given configuration.

        Args:
            config: Configuration specifying which checkpoint to retrieve.

        Returns:
            The requested checkpoint tuple, or `None` if not found.

        Raises:
            NotImplementedError: Implement this method in your custom checkpoint saver.
        rQ   rS   s     r'   rj   zBaseCheckpointSaver.aget_tuple  s      "!   	c                 K   t         w)a  Asynchronously list checkpoints that match the given criteria.

        Args:
            config: Base configuration for filtering checkpoints.
            filter: Additional filtering criteria for metadata.
            before: List checkpoints created before this configuration.
            limit: Maximum number of checkpoints to return.

        Returns:
            Async iterator of matching checkpoint tuples.

        Raises:
            NotImplementedError: Implement this method in your custom checkpoint saver.
        rQ   rX   s        r'   alistzBaseCheckpointSaver.alist  s     , "!rm   c                   K   t         w)a  Asynchronously store a checkpoint with its configuration and metadata.

        Args:
            config: Configuration for the checkpoint.
            checkpoint: The checkpoint to store.
            metadata: Additional metadata for the checkpoint.
            new_versions: New channel versions as of this write.

        Returns:
            RunnableConfig: Updated configuration after storing the checkpoint.

        Raises:
            NotImplementedError: Implement this method in your custom checkpoint saver.
        rQ   r[   s        r'   aputzBaseCheckpointSaver.aput/  s     * "!rm   c                   K   t         w)a  Asynchronously store intermediate writes linked to a checkpoint.

        Args:
            config: Configuration of the related checkpoint.
            writes: List of writes to store.
            task_id: Identifier for the task creating the writes.
            task_path: Path of the task creating the writes.

        Raises:
            NotImplementedError: Implement this method in your custom checkpoint saver.
        rQ   r_   s        r'   aput_writeszBaseCheckpointSaver.aput_writesF  s     $ "!rm   c                   K   t         wre   rQ   rf   s     r'   adelete_threadz"BaseCheckpointSaver.adelete_threadZ  s      "!rm   c                >    t        |t              rt        |y|dz   S )a  Generate the next version ID for a channel.

        Default is to use integer versions, incrementing by `1`.

        If you override, you can use `str`/`int`/`float` versions, as long as they are monotonically increasing.

        Args:
            current: The current version identifier (`int`, `float`, or `str`).
            channel: Deprecated argument, kept for backwards compatibility.

        Returns:
            V: The next version identifier, which must be increasing.
           )
isinstancer,   rR   )rH   currentchannels      r'   get_next_versionz$BaseCheckpointSaver.get_next_versione  s&     gs#%%_Q;r&   )rF   zSerializerProtocol | NonereturnNone)r|   rY   )r?   r   r|   zCheckpoint | None)r?   r   r|   zCheckpointTuple | None)
r?   rA   rT   dict[str, Any] | NonerU   rA   rV   
int | Noner|   zIterator[CheckpointTuple])
r?   r   r:   r*   r@   r   r\   r0   r|   r   ) )
r?   r   r`   zSequence[tuple[str, Any]]ra   r,   rb   r,   r|   r}   )rg   r,   r|   r}   )
r?   rA   rT   r~   rU   rA   rV   r   r|   zAsyncIterator[CheckpointTuple])ry   zV | Nonerz   r}   r|   r   )r    r!   r"   r#   r   rF   r$   rI   propertyrK   r9   rM   rY   r]   rc   rh   rk   rj   ro   rq   rs   ru   r{   r%   r&   r'   rE   rE   t   s    !3 4E4
 ,0B )B 
	B  
$"$ )-(, "%" &	"
 &" " 
#"0"" " %	"
 &" 
"8 "" *" 	"
 " 
"(	"	" 
	"
$"$ )-(, % &	
 &  
(2"" " %	"
 &" 
"8 "" *" 	"
 " 
"(	"	" 
	"r&   rE   c                      e Zd ZdZy)EmptyChannelErrorziRaised when attempting to get the value of a channel that hasn't been updated
    for the first time yet.N)r    r!   r"   r#   r%   r&   r'   r   r   {  s     	r&   r   c                *    | d   j                  d      S )zGet checkpoint ID.configurablecheckpoint_id)r9   )r?   s    r'   get_checkpoint_idr     s    .!%%o66r&   c           	        |j                         D ci c]*  \  }}|t        |t              r|j                  dd      n|, }}}| j	                  d      | j	                  d      fD ]  }|s|j                         D ]j  \  }}||v s|t
        v s|j                  d      r$t        |t              r|j                  dd      ||<   Jt        |t        t        t        f      sf|||<   l  |S c c}}w )9Get checkpoint metadata in a backwards-compatible manner. r   r@   r   __)
r8   rx   r,   replacer9   EXCLUDED_METADATA_KEYS
startswithr   boolfloat)r?   r@   r;   r+   objkeys         r'   get_checkpoint_metadatar     s     NN$Aq 	
jC&8199Xr"a?H  

:&

>(BC 	"iik 	"FCh#)?"?3>>RVCWAs# !		(B 7AT512 !	"	" Os   /C/c                H    t        | |      }d|v r|j                  d       |S )r   r`   )r   pop)r?   r@   checkpoint_metadatas      r'   $get_serializable_checkpoint_metadatar     s.     2&(C&&)r&   >	   rg   r   checkpoint_nscheckpoint_maplanggraph_nodelanggraph_pathlanggraph_steplanggraph_triggerslanggraph_checkpoint_ns   c            
         ddl m } m} t        t        t	        t        d            | j                  |j                        j                         i i i g d       S )Nr   datetimetimezoner   	clock_seq)r+   r-   r.   r/   r1   r2   r5   r3   )	r   r   r*   LATEST_VERSIONr,   r   nowutc	isoformatr   s     r'   empty_checkpointr     sJ    +
ur"#<<%//1	 	r&   N)r-   c                  ddl m }m} |j                  |j                        j	                         }|| d   }n6i }|j                         D ]!  \  }}	|| d   vr	 |	j                         ||<   # t        t        ||xs t        t        |            || d   | d   | j                  dg       d	      S # t        $ r Y sw xY w)
z+Create a checkpoint for the given channels.r   r   Nr/   r1   r   r2   r5   r6   )r   r   r   r   r   r8   r:   r   r*   r   r,   r   r9   )
r:   channelsr   r-   r   r   r.   valuesr;   r+   s
             r'   create_checkpointr     s     ,	hll	#	-	-	/B,-NN$ 	DAq
#566LLNq			 
+UT*+#$67 1 nn_b9	 	 % s   B44	C ?C )r:   r*   r|   r*   )r?   r   r|   
str | None)r?   r   r@   r   r|   r   )r|   r*   )
r:   r*   r   z$Mapping[str, ChannelProtocol] | Noner   r   r-   r   r|   r*   )4
__future__r   collections.abcr   r   r   r   typingr   r	   r
   r   r   r   langchain_core.runnablesr   langgraph.checkpoint.base.idr   langgraph.checkpoint.serde.baser   r   #langgraph.checkpoint.serde.jsonplusr    langgraph.checkpoint.serde.typesr   r   r   r   r   r   r   r,   r   tuplePendingWriter   dictr0   r*   r<   r>   rE   	Exceptionr   r   r   r   WRITES_IDX_MAPr   r   r   r   r%   r&   r'   <module>r      s_   " F F  4 . W B  CeS!S#s]#% 2 sC#I--. D
5j 5D'!* DN		 	7
&8*&8 YIr62F
  ( 2 
 	 r&   