o
    i#                     @  sP  d dl 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 d dlZd dlmZ d	d
lmZmZ ddlmZ erPd	dlmZ dZededZededZdZed Z	 e	ddG dd deeef Z e	ddG dd deeef Z!e eef e!eef B Z"	 G dd deeeef Z#d$d"d#Z$dS )%    )annotations)ABCabstractmethod)AbstractAsyncContextManager)	dataclassfield)datetime)TYPE_CHECKING	AnnotatedAnyGenericLiteralN)TypeVar   )BaseNodeEnd   )_utils)Graph)StateTNodeSnapshotEndSnapshotSnapshotBaseStatePersistenceSnapshotStatus build_snapshot_list_type_adapterr   )defaultRunEndT	__unset__)createdpendingrunningsuccesserrorT)kw_onlyc                   @  sv   e Zd ZU dZded< 	 ded< 	 dZded< 	 dZd	ed
< 	 dZded< 	 dZded< 	 e	Z
ded< 	 dddZdS )r   z;History step describing the execution of a node in a graph.r   statezDAnnotated[BaseNode[StateT, Any, RunEndT], _utils.CustomNodeSchema()]nodeNzdatetime | Nonestart_tszfloat | Nonedurationr   r   statuszLiteral['node']kindstridreturnNonec                 C     | j tkr| j | _ d S d S Nr,   UNSET_SNAPSHOT_IDr&   get_snapshot_idself r6   i/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/pydantic_graph/persistence/__init__.py__post_init__@      
zNodeSnapshot.__post_init__r-   r.   )__name__
__module____qualname____doc____annotations__r'   r(   r)   r*   r2   r,   r8   r6   r6   r6   r7   r   ,   s"   
 r   c                   @  sp   e Zd ZU dZded< 	 ded< 	 eejdZded< 	 d	Z	d
ed< 	 e
Zded< 	 dddZedddZdS )r   z/History step describing the end of a graph run.r   r%   End[RunEndT]result)default_factoryr   tsendzLiteral['end']r*   r+   r,   r-   r.   c                 C  r/   r0   r1   r4   r6   r6   r7   r8   U   r9   zEndSnapshot.__post_init__c                 C  s   | j S )zShim to get the [`result`][pydantic_graph.persistence.EndSnapshot.result].

        Useful to allow `[snapshot.node for snapshot in persistence.history]`.
        )rA   r4   r6   r6   r7   r&   Y   s   zEndSnapshot.nodeNr:   )r-   r@   )r;   r<   r=   r>   r?   r   r   now_utcrC   r*   r2   r,   r8   propertyr&   r6   r6   r6   r7   r   E   s   
 
r   c                   @  s   e Zd Z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
d/ddZd0d d!Zd1d&d'Zd(S )2r   zAbstract base class for storing the state of a graph run.

    Each instance of a `BaseStatePersistence` subclass should be used for a single graph run.
    r%   r   	next_nodeBaseNode[StateT, Any, RunEndT]r-   r.   c                      t )a  Snapshot the state of a graph, when the next step is to run a node.

        This method should add a [`NodeSnapshot`][pydantic_graph.persistence.NodeSnapshot] to persistence.

        Args:
            state: The state of the graph.
            next_node: The next node to run.
        NotImplementedError)r5   r%   rG   r6   r6   r7   snapshot_nodep      
z"BaseStatePersistence.snapshot_nodesnapshot_idr+   c                   rI   )a  Snapshot the state of a graph if the snapshot ID doesn't already exist in persistence.

        This method will generally call [`snapshot_node`][pydantic_graph.persistence.BaseStatePersistence.snapshot_node]
        but should do so in an atomic way.

        Args:
            snapshot_id: The ID of the snapshot to check.
            state: The state of the graph.
            next_node: The next node to run.
        rJ   )r5   rN   r%   rG   r6   r6   r7   snapshot_node_if_new|   s   z)BaseStatePersistence.snapshot_node_if_newrD   r@   c                   rI   )a  Snapshot the state of a graph when the graph has ended.

        This method should add an [`EndSnapshot`][pydantic_graph.persistence.EndSnapshot] to persistence.

        Args:
            state: The state of the graph.
            end: data from the end of the run.
        rJ   )r5   r%   rD   r6   r6   r7   snapshot_end   rM   z!BaseStatePersistence.snapshot_end!AbstractAsyncContextManager[None]c                 C  s   t )a{  Record the run of the node, or error if the node is already running.

        Args:
            snapshot_id: The ID of the snapshot to record.

        Raises:
            GraphNodeRunningError: if the node status it not `'created'` or `'pending'`.
            LookupError: if the snapshot ID is not found in persistence.

        Returns:
            An async context manager that records the run of the node.

        In particular this should set:

        - [`NodeSnapshot.status`][pydantic_graph.persistence.NodeSnapshot.status] to `'running'` and
          [`NodeSnapshot.start_ts`][pydantic_graph.persistence.NodeSnapshot.start_ts] when the run starts.
        - [`NodeSnapshot.status`][pydantic_graph.persistence.NodeSnapshot.status] to `'success'` or `'error'` and
          [`NodeSnapshot.duration`][pydantic_graph.persistence.NodeSnapshot.duration] when the run finishes.
        rJ   )r5   rN   r6   r6   r7   
record_run   s   zBaseStatePersistence.record_run$NodeSnapshot[StateT, RunEndT] | Nonec                   rI   )aE  Retrieve a node snapshot with status `'created`' and set its status to `'pending'`.

        This is used by [`Graph.iter_from_persistence`][pydantic_graph.graph.Graph.iter_from_persistence]
        to get the next node to run.

        Returns: The snapshot, or `None` if no snapshot with status `'created`' exists.
        rJ   r4   r6   r6   r7   	load_next      	zBaseStatePersistence.load_nextlist[Snapshot[StateT, RunEndT]]c                   rI   )a  Load the entire history of snapshots.

        `load_all` is not used by pydantic-graph itself, instead it's provided to make it convenient to
        get all [snapshots][pydantic_graph.persistence.Snapshot] from persistence.

        Returns: The list of snapshots.
        rJ   r4   r6   r6   r7   load_all   rU   zBaseStatePersistence.load_allgraphGraph[StateT, Any, RunEndT]c                 C  sL   |   r$t|  | j|j  W d   dS 1 sw   Y  dS dS )a@  Set the types of the state and run end from a graph.

        You generally won't need to customise this method, instead implement
        [`set_types`][pydantic_graph.persistence.BaseStatePersistence.set_types] and
        [`should_set_types`][pydantic_graph.persistence.BaseStatePersistence.should_set_types].
        N)should_set_typesr   set_nodes_type_context	get_nodes	set_typesinferred_types)r5   rX   r6   r6   r7   set_graph_types   s
   "z$BaseStatePersistence.set_graph_typesboolc                 C     dS )zWhether types need to be set.

        Implementations should override this method to return `True` when types have not been set if they are needed.
        Fr6   r4   r6   r6   r7   rZ      s   z%BaseStatePersistence.should_set_types
state_typetype[StateT]run_end_typetype[RunEndT]c                 C  ra   )a  Set the types of the state and run end.

        This can be used to create [type adapters][pydantic.TypeAdapter] for serializing and deserializing snapshots,
        e.g. with [`build_snapshot_list_type_adapter`][pydantic_graph.persistence.build_snapshot_list_type_adapter].

        Args:
            state_type: The state type.
            run_end_type: The run end type.
        Nr6   )r5   rb   rd   r6   r6   r7   r]      s   
zBaseStatePersistence.set_typesN)r%   r   rG   rH   r-   r.   )rN   r+   r%   r   rG   rH   r-   r.   )r%   r   rD   r@   r-   r.   )rN   r+   r-   rQ   )r-   rS   )r-   rV   )rX   rY   r-   r.   )r-   r`   )rb   rc   rd   re   r-   r.   )r;   r<   r=   r>   r   rL   rO   rP   rR   rT   rW   r_   rZ   r]   r6   r6   r6   r7   r   j   s"    



r   state_trc   	run_end_tre   r-   5pydantic.TypeAdapter[list[Snapshot[StateT, RunEndT]]]c                 C  s$   t ttt| |f t df  S )aI  Build a type adapter for a list of snapshots.

    This method should be called from within
    [`set_types`][pydantic_graph.persistence.BaseStatePersistence.set_types]
    where context variables will be set such that Pydantic can create a schema for
    [`NodeSnapshot.node`][pydantic_graph.persistence.NodeSnapshot.node].
    r*   )pydanticTypeAdapterlistr
   r   Discriminator)rf   rg   r6   r6   r7   r      s   $
r   )rf   rc   rg   re   r-   rh   )%
__future__r   _annotationsabcr   r   
contextlibr   dataclassesr   r   r   typingr	   r
   r   r   r   ri   typing_extensionsr   nodesr   r    r   r   __all__r   r   r2   r   r   r   r   r   r   r6   r6   r6   r7   <module>   s4    
z