o
    i                     @  s   d Z ddlmZ ddl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 ddlZd	d
l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 e
G dd deeef Ze
G dd deeef ZdS )zbIn memory state persistence.

This module provides simple in memory state persistence for graphs.
    )annotationsN)AsyncIterator)asynccontextmanager)	dataclassfield)perf_counter)Any   )
exceptions)BaseNodeEnd   )BaseStatePersistenceEndSnapshotNodeSnapshotRunEndTSnapshotStateT_utils build_snapshot_list_type_adapterc                   @  s`   e Zd ZU dZdZded< 	 dddZdddZd ddZe	d!ddZ
d"ddZd#ddZdS )$SimpleStatePersistencezSimple in memory state persistence that just hold the latest snapshot.

    If no state persistence implementation is provided when running a graph, this is used by default.
    Nz Snapshot[StateT, RunEndT] | Nonelast_snapshotstater   	next_nodeBaseNode[StateT, Any, RunEndT]returnNonec                      t ||d| _d S N)r   node)r   r   )selfr   r    r!   g/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/pydantic_graph/persistence/in_mem.pysnapshot_node)      z$SimpleStatePersistence.snapshot_nodesnapshot_idstrc                   s.   | j r| j j|krd S | ||I d H  d S N)r   idr#   r    r%   r   r   r!   r!   r"   snapshot_node_if_new,   s   z+SimpleStatePersistence.snapshot_node_if_newendEnd[RunEndT]c                   r   N)r   result)r   r   )r    r   r+   r!   r!   r"   snapshot_end4   r$   z#SimpleStatePersistence.snapshot_endAsyncIterator[None]c                 C s   | j d u s|| j jkrtd|t| j tsJ dtj| j j d| j _t	
 | j _t }zd V  W n tyJ   t | | j _d| j _ w t | | j _d| j _d S )NNo snapshot found with id=!Only NodeSnapshot can be recordedrunningerrorsuccess)r   r(   LookupError
isinstancer   r
   GraphNodeStatusErrorcheckstatusr   now_utcstart_tsr   	Exceptionduration)r    r%   startr!   r!   r"   
record_run7   s"   
z!SimpleStatePersistence.record_run$NodeSnapshot[StateT, RunEndT] | Nonec                   s6   t | jtr| jjdkrd| j_t| jS d S d S )Ncreatedpending)r7   r   r   r:   copydeepcopyr    r!   r!   r"   	load_nextL   s
   z SimpleStatePersistence.load_nextlist[Snapshot[StateT, RunEndT]]c                   s
   t d)Nz0load is not supported for SimpleStatePersistence)NotImplementedErrorrF   r!   r!   r"   load_allQ   s   zSimpleStatePersistence.load_allr   r   r   r   r   r   r%   r&   r   r   r   r   r   r   r   r   r+   r,   r   r   r%   r&   r   r0   r   rA   r   rH   )__name__
__module____qualname____doc__r   __annotations__r#   r*   r/   r   r@   rG   rJ   r!   r!   r!   r"   r      s   
 



r   c                   @  s   e Zd ZU dZdZded< 	 eedZded< 	 edd	d	d
Z	ded< d9ddZ
d:ddZd;ddZed<ddZd=d!d"Zd>d#d$Zd?d%d&Zd@d+d,Zdd-dAd1d2ZdBd5d6ZdCd7d8ZdS )DFullStatePersistencez:In memory state persistence that hold a list of snapshots.Tbool	deep_copy)default_factoryrH   historyNF)defaultinitreprz<pydantic.TypeAdapter[list[Snapshot[StateT, RunEndT]]] | None_snapshots_type_adapterr   r   r   r   r   r   c                   s2   t | || jr| n|d}| j| d S r   )r   _prep_staterX   rZ   append)r    r   r   snapshotr!   r!   r"   r#   e      z"FullStatePersistence.snapshot_noder%   r&   c                   s4   t  fdd| jD s| ||I d H  d S d S )Nc                 3  s    | ]}|j  kV  qd S r'   r(   .0sr%   r!   r"   	<genexpr>o   s    z<FullStatePersistence.snapshot_node_if_new.<locals>.<genexpr>)anyrZ   r#   r)   r!   rg   r"   r*   l   s   z)FullStatePersistence.snapshot_node_if_newr+   r,   c                   s2   t | || jr| n|d}| j| d S r-   )r   r_   rX   deep_copy_datarZ   r`   )r    r   r+   ra   r!   r!   r"   r/   r   rb   z!FullStatePersistence.snapshot_endr0   c              
    s   zt  fdd| jD }W n ty# } ztd |d }~ww t|ts-J dtj|j	 d|_	t
 |_t }zd V  W n tyU   t | |_d|_	 w t | |_d|_	d S )Nc                 3  s    | ]
}|j  kr|V  qd S r'   rc   rd   rg   r!   r"   rh   |   s    z2FullStatePersistence.record_run.<locals>.<genexpr>r1   r2   r3   r4   r5   )nextrZ   StopIterationr6   r7   r   r
   r8   r9   r:   r   r;   r<   r   r=   r>   )r    r%   ra   er?   r!   rg   r"   r@   y   s*   


zFullStatePersistence.record_runrA   c                   s0   t dd | jD d  }rd|_t|S d S )Nc                 s  s(    | ]}t |tr|jd kr|V  qdS )rB   N)r7   r   r:   rd   r!   r!   r"   rh      s   & z1FullStatePersistence.load_next.<locals>.<genexpr>rC   )rk   rZ   r:   rD   rE   )r    ra   r!   r!   r"   rG      s
   
zFullStatePersistence.load_nextc                   s   | j S r'   )rZ   rF   r!   r!   r"   rJ      s   zFullStatePersistence.load_allc                 C  s
   | j d u S r'   )r^   rF   r!   r!   r"   should_set_types   s   
z%FullStatePersistence.should_set_types
state_typetype[StateT]run_end_typetype[RunEndT]c                 C  s   t ||| _d S r'   )r   r^   )r    ro   rq   r!   r!   r"   	set_types   s   zFullStatePersistence.set_typesindentru   
int | Nonebytesc                C  s$   | j dus	J d| j j| j|dS )zDump the history to JSON bytes.Nz+type adapter must be set to use `dump_json`rt   )r^   	dump_jsonrZ   )r    ru   r!   r!   r"   rx         zFullStatePersistence.dump_json	json_datastr | bytes | bytearrayc                 C  s$   | j dus	J d| j || _dS )zLoad the history from JSON.Nz+type adapter must be set to use `load_json`)r^   validate_jsonrZ   )r    rz   r!   r!   r"   	load_json   ry   zFullStatePersistence.load_jsonc                 C  s   | j r|du r	|S t|S )zMPrepare state for snapshot, uses [`copy.deepcopy`][copy.deepcopy] by default.N)rX   rD   rE   )r    r   r!   r!   r"   r_      s   
z FullStatePersistence._prep_staterK   rL   rM   rN   rO   rP   )r   rW   )ro   rp   rq   rr   r   r   )ru   rv   r   rw   )rz   r{   r   r   )r   r   r   r   )rQ   rR   rS   rT   rX   rU   r   listrZ   r^   r#   r*   r/   r   r@   rG   rJ   rn   rs   rx   r}   r_   r!   r!   r!   r"   rV   U   s*   
 







rV   ) rT   
__future__r   _annotationsrD   collections.abcr   
contextlibr   dataclassesr   r   timer   typingr   pydantic r
   nodesr   r   r   r   r   r   r   r   r   r   r   rV   r!   r!   r!   r"   <module>   s     (5