
    @'h?                        d dl mZmZmZmZ d dlmZmZ d dlmZm	Z	m
Z
mZmZmZmZmZmZmZmZ d dlmZ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"m#Z#  ed	e$e%e&      Z'ee&e&ef   Z(d
Z) G d ded      Z* G d de      Z+e,e&ee&e$e%f   f   Z- G d de      Z.de.fdZ/de.de.fdZ0ddde.deee&e"f      de$dee&   de.f
dZ1 G d de      Z2 ede&d dd!d"#      Z3 ed$e&d%d&d!d"#      Z4 ed'ee&   d(d)dd"#      Z5 G d* d+e
e'         Z6 G d, d-e7      Z8d.edee&   fd/Z9d.ed0e*de*fd1Z:	 ed2e!d3ed4e d5iZ;h d6Z<y)7    )AsyncIteratorIteratorMappingSequence)datetimetimezone)AnyDictGenericListLiteral
NamedTupleOptionalTuple	TypedDictTypeVarUnion)ConfigurableFieldSpecRunnableConfig)uuid6)SerializerProtocolmaybe_add_typed_methods)JsonPlusSerializer)ERROR	INTERRUPTRESUME	SCHEDULEDChannelProtocolSendProtocolV   c                   Z    e Zd ZU dZed   ed<   	 eed<   	 eee	f   ed<   	 eeef   ed<   y)CheckpointMetadataz&Metadata associated with a checkpoint.)inputloopupdateforksourcestepwritesparentsN)
__name__
__module____qualname____doc__r   __annotations__intdictstrr	        i/home/kushmeetdev/Regenta/Chatbot/venv/lib/python3.12/site-packages/langgraph/checkpoint/base/__init__.pyr#   r#   &   sL    0566 I cN #s(^r5   r#   F)totalc                       e Zd ZU ed   ed<   y)TaskInfo)	scheduledsuccesserrorstatusN)r,   r-   r.   r   r0   r4   r5   r6   r9   r9   D   s    344r5   r9   c                   ~    e Zd ZU dZeed<   	 eed<   	 eed<   	 eeef   ed<   	 e	ed<   	 eee	f   ed<   	 e
e   ed<   y	)

Checkpointz(State snapshot at a given point in time.vidtschannel_valueschannel_versionsversions_seenpending_sendsN)r,   r-   r.   r/   r1   r0   r3   r2   r	   ChannelVersionsr   r   r4   r5   r6   r?   r?   K   si    2
F<GNG=cN" &% _,-- %%'r5   r?   returnc            	          t        t        t        t        d            t	        j
                  t        j                        j                         i i i g       S )N	clock_seq)r@   rA   rB   rC   rD   rE   rF   )	r?   LATEST_VERSIONr3   r   r   nowr   utc	isoformatr4   r5   r6   empty_checkpointrQ   i   sD    
ur"#<<%//1 r5   
checkpointc                 *   t        | d   | d   | d   | d   j                         | d   j                         | d   j                         D ci c]  \  }}||j                          c}}| j                  dg       j                               S c c}}w )	Nr@   rB   rA   rC   rD   rE   rF   r@   rB   rA   rC   rD   rE   rF   )r?   copyitemsget)rR   kr@   s      r6   copy_checkpointrY   u   s    
S/dd!"2388:#$67<<>/9//J/P/P/RStq!q!&&({S nn_b9>>@  Ts   BN)rA   channelsr)   rA   c                   t        j                  t        j                        j	                         }|| d   }n6i }|j                         D ]!  \  }}|| d   vr	 |j                         ||<   # t        t        ||xs t        t        |            || d   | d   | j                  dg             S # t        $ r Y rw xY w)z+Create a checkpoint for the given channels.rC   rD   rK   rE   rF   rT   )r   rN   r   rO   rP   rV   rR   EmptyChannelErrorr?   rM   r3   r   rW   )rR   rZ   r)   rA   rB   valuesrX   r@   s           r6   create_checkpointr^      s     
hll	#	-	-	/B,-NN$ 	DAq
#566LLNq			 
+UT*+#$67 1 nn_b9  % s   B33	B?>B?c                   ^    e Zd ZU dZeed<   eed<   eed<   dZe	e   ed<   dZ
e	ee      ed<   y)CheckpointTuplez8A tuple containing a checkpoint and its associated data.configrR   metadataNparent_configpending_writes)r,   r-   r.   r/   r   r0   r?   r#   rc   r   rd   r   PendingWriter4   r5   r6   r`   r`      s:    B  .2M8N+237NHT,/07r5   r`   	thread_idz	Thread ID T)rA   
annotationnamedescriptiondefault	is_sharedcheckpoint_nszCheckpoint NSzCheckpoint namespace. Denotes the path to the subgraph node the checkpoint originates from, separated by `|` character, e.g. `"child|grandchild"`. Defaults to "" (root graph).checkpoint_idzCheckpoint IDzHPass to fetch a past checkpoint. If None, fetches the latest checkpoint.c                   (   e Zd ZU dZ e       Zeed<   dddee   ddfdZ	e
dee   fd       Zdedee   fd	Zdedee   fd
Zdddddee   deeeef      dee   dee   dee   f
dZdededededef
dZ	 d#dedeeeef      dededdf
dZdeddfdZdedee   fdZdedee   fdZ dddddee   deeeef      dee   dee   de!e   f
dZ"dededededef
dZ#	 d#dedeeeef      dededdf
dZ$deddfdZ%d ee&   d!e'de&f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.
    serdeN)rq   rH   c                @    t        |xs | j                        | _        y )N)r   rq   )selfrq   s     r6   __init__zBaseCheckpointSaver.__init__   s    
 -U-@djjA
r5   c                 $    t         t        t        gS )zDefine the configuration options for the checkpoint saver.

        Returns:
            list[ConfigurableFieldSpec]: List of configuration field specs.
        )CheckpointThreadIdCheckpointNSCheckpointId)rs   s    r6   config_specsz BaseCheckpointSaver.config_specs   s     #L,??r5   ra   c                 B    | j                  |      x}r|j                  S y)zFetch a checkpoint using the given configuration.

        Args:
            config: Configuration specifying which checkpoint to retrieve.

        Returns:
            Optional[Checkpoint]: The requested checkpoint, or None if not found.
        N)	get_tuplerR   rs   ra   values      r6   rW   zBaseCheckpointSaver.get   s)     NN6**5*### +r5   c                     t         )ar  Fetch a checkpoint tuple using the given configuration.

        Args:
            config: Configuration specifying which checkpoint to retrieve.

        Returns:
            Optional[CheckpointTuple]: The requested checkpoint tuple, or None if not found.

        Raises:
            NotImplementedError: Implement this method in your custom checkpoint saver.
        NotImplementedErrorrs   ra   s     r6   r{   zBaseCheckpointSaver.get_tuple   s
     "!r5   )filterbeforelimitr   r   r   c                    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[CheckpointTuple]: Iterator of matching checkpoint tuples.

        Raises:
            NotImplementedError: Implement this method in your custom checkpoint saver.
        r   rs   ra   r   r   r   s        r6   listzBaseCheckpointSaver.list  s
    , "!r5   rR   rb   new_versionsc                     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.
        r   rs   ra   rR   rb   r   s        r6   putzBaseCheckpointSaver.put  s
    * "!r5   r*   task_id	task_pathc                     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.
        r   rs   ra   r*   r   r   s        r6   
put_writeszBaseCheckpointSaver.put_writes0  s
    $ "!r5   rf   c                     t         zDelete all checkpoints and writes associated with a specific thread ID.

        Args:
            thread_id: The thread ID whose checkpoints should be deleted.
        r   rs   rf   s     r6   delete_threadz!BaseCheckpointSaver.delete_threadD  s
     "!r5   c                 ^   K   | j                  |       d{   x}r|j                  S y7 w)a  Asynchronously fetch a checkpoint using the given configuration.

        Args:
            config: Configuration specifying which checkpoint to retrieve.

        Returns:
            Optional[Checkpoint]: The requested checkpoint, or None if not found.
        N)
aget_tuplerR   r|   s      r6   agetzBaseCheckpointSaver.agetO  s3      //&11151### 21s   -+-c                    K   t         w)a  Asynchronously fetch a checkpoint tuple using the given configuration.

        Args:
            config: Configuration specifying which checkpoint to retrieve.

        Returns:
            Optional[CheckpointTuple]: The requested checkpoint tuple, or None if not found.

        Raises:
            NotImplementedError: Implement this method in your custom checkpoint saver.
        r   r   s     r6   r   zBaseCheckpointSaver.aget_tuple[  s      "!   	c                  K   t         w)a1  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:
            AsyncIterator[CheckpointTuple]: Async iterator of matching checkpoint tuples.

        Raises:
            NotImplementedError: Implement this method in your custom checkpoint saver.
        r   r   s        r6   alistzBaseCheckpointSaver.alisti  s     , "!r   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.
        r   r   s        r6   aputzBaseCheckpointSaver.aput  s     * "!r   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.
        r   r   s        r6   aput_writeszBaseCheckpointSaver.aput_writes  s     $ "!r   c                    K   t         wr   r   r   s     r6   adelete_threadz"BaseCheckpointSaver.adelete_thread  s      "!r   currentchannelc                 >    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: The channel being versioned.

        Returns:
            V: The next version identifier, which must be increasing.
           )
isinstancer3   r   )rs   r   r   s      r6   get_next_versionz$BaseCheckpointSaver.get_next_version  s&     gs#%%_Q;r5   )rg   ))r,   r-   r.   r/   r   rq   r   r0   r   rt   propertyr   r   ry   r   r?   rW   r`   r{   r
   r3   r	   r1   r   r#   rG   r   r   r   r   r   r   r   r   r   r   r   r   r    r   r   r4   r5   r6   rp   rp      s    !3 4E4
 /3B *+B 
	B @d#89 @ @
$. 
$Xj-A 
$" "8O3L "$ ,0+/#"(" c3h(	"
 (" }" 
/	""0"" " %	"
 &" 
"8 "" sCx)" 	"
 " 
"(	"	" 
	"
$ 
$HZ4H 
$"~ "(?:S "$ ,0+/#( c3h(	
 ( } 
	'2"" " %	"
 &" 
"8 "" sCx)" 	"
 " 
"(	"	" 
	" o RS r5   rp   c                       e Zd ZdZy)r\   ziRaised when attempting to get the value of a channel that hasn't been updated
    for the first time yet.N)r,   r-   r.   r/   r4   r5   r6   r\   r\     s     	r5   r\   ra   c                 P    | d   j                  d| d   j                  d            S )zKGet checkpoint ID in a backwards-compatible manner (fallback on thread_ts).configurablern   	thread_ts)rW   )ra   s    r6   get_checkpoint_idr     s/    .!%%/33K@ r5   rb   c           	         |j                         }| j                  d      | j                  d      fD ]U  }|s|D ]K  }||v s|t        v s|j                  d      r!||   }t	        |t
        t        t        t        f      sG|||<   M W |S )z9Get checkpoint metadata in a backwards-compatible manner.rb   r   __)	rU   rW   EXCLUDED_METADATA_KEYS
startswithr   r3   r1   boolfloat)ra   rb   objkeyr@   s        r6   get_checkpoint_metadatar     s     }}H

:&

>(BC " 	"Ch#)?"?3>>RVCWCA!c3e45 !	"" Or5   rJ   >   rn   rm   checkpoint_map)=collections.abcr   r   r   r   r   r   typingr	   r
   r   r   r   r   r   r   r   r   r   langchain_core.runnablesr   r   langgraph.checkpoint.base.idr   langgraph.checkpoint.serde.baser   r   #langgraph.checkpoint.serde.jsonplusr    langgraph.checkpoint.serde.typesr   r   r   r   r   r   r1   r   r3   r    re   rM   r#   r9   r2   rG   r?   rQ   rY   r^   r`   rv   rw   rx   rp   	Exceptionr\   r   r   WRITES_IDX_MAPr   r4   r5   r6   <module>r      s   F F '    K . W B  CeS!S#s]#% <5y 5 sE#sE/223' '<	* 		
 	z 	$ wsO345 
 	 >8j 8 +	  %	 B %}	ZC'!* CL		 	n # &8" YIr62F r5   