
    3fi                        d Z ddlmZ ddlZddlZddlmZmZmZm	Z	 ddl
mZmZ erddlmZmZ ddlmZ ddlmZ dd	lmZmZmZ d
Z eddd       G d de             Zy)z8Astra DB - based chat message history, based on astrapy.    )annotationsN)TYPE_CHECKINGListOptionalSequence)	SetupMode_AstraDBCollectionEnvironment)AstraDBAsyncAstraDB)
deprecated)BaseChatMessageHistory)BaseMessagemessage_to_dictmessages_from_dictlangchain_message_storez0.0.25z1.0z+langchain_astradb.AstraDBChatMessageHistory)sinceremovalalternative_importc            	          e Zd Zedddddej
                  dd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZedd       Zej                  dd       ZddZ
ddZdd	Zdd
ZddZy)AstraDBChatMessageHistoryNFcollection_nametokenapi_endpointastra_db_clientasync_astra_db_client	namespace
setup_modepre_delete_collectionc       	   
         t        ||||||||	      | _        | j                  j                  | _        | j                  j                  | _        || _        || _        y)a  Chat message history that stores history in Astra DB.

        Args:
            session_id: arbitrary key that is used to store the messages
                of a single chat session.
            collection_name: name of the Astra DB collection to create/use.
            token: API token for Astra DB usage.
            api_endpoint: full URL to the API endpoint,
                such as "https://<DB-ID>-us-east1.apps.astra.datastax.com".
            astra_db_client: *alternative to token+api_endpoint*,
                you can pass an already-created 'astrapy.db.AstraDB' instance.
            async_astra_db_client: *alternative to token+api_endpoint*,
                you can pass an already-created 'astrapy.db.AsyncAstraDB' instance.
            namespace: namespace (aka keyspace) where the
                collection is created. Defaults to the database's "default namespace".
            setup_mode: mode used to create the Astra DB collection (SYNC, ASYNC or
                OFF).
            pre_delete_collection: whether to delete the collection
                before creating it. If False and the collection already exists,
                the collection will be used as is.
        r   N)r	   	astra_env
collectionasync_collection
session_idr   )
selfr$   r   r   r   r   r   r   r   r   s
             p/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_community/chat_message_histories/astradb.py__init__z"AstraDBChatMessageHistory.__init__"   s]    D 7+%+"7!"7	
 ..33 $ ? ?$.    c                8   | j                   j                          t        | j                  j	                  d| j
                  iddd      d       D cg c]  }|d   	 }}|D cg c]  }t        j                  |       }}t        |      }|S c c}w c c}w )z%Retrieve all session messages from DBr$      	timestamp	body_blobfilter
projectionc                    | d   S Nr,    _docs    r&   <lambda>z4AstraDBChatMessageHistory.messages.<locals>.<lambda>e   s    k!2 r(   keyr-   )	r!   ensure_db_setupsortedr"   paginated_findr$   jsonloadsr   )r%   docmessage_blobsmessage_blobitemsmessagess         r&   rB   z"AstraDBChatMessageHistory.messagesU   s     	&&( ..$doo &'%& 	 /  3
 
 
 ?LLlL)LL%e,#
 Ms   B'Bc                    t        d      )NzUse add_messages instead)NotImplementedError)r%   rB   s     r&   rB   z"AstraDBChatMessageHistory.messagesl   s    !"<==r(   c                  K   | j                   j                          d {    | j                  j                  d| j                  iddd      }t        |2 cg c3 d {   }|7 G7 	6 nc c}w c}d       }|D cg c]  }|d   	 nc c}w }}|D cg c]  }t        j                  |       nc c}w }}t        |      }|S w)Nr$   r*   r+   r.   c                    | d   S r2   r3   r4   s    r&   r6   z9AstraDBChatMessageHistory.aget_messages.<locals>.<lambda>}   s    T+. r(   r7   r-   )	r!   aensure_db_setupr#   r;   r$   r:   r<   r=   r   )r%   docsr>   sorted_docsr?   r@   rA   rB   s           r&   aget_messagesz'AstraDBChatMessageHistory.aget_messagesp   s     nn--///$$33doo 	 4 
 "&''3S 	0 (''.
 6AAc[)AAA>KLlL)LLL%e,s]   C
A(9C
A.A,
 A*!A,
$A.(C
*A,
,A.-C
BC
B65C
c           
        | j                   j                          |D cg c]@  }t        j                         | j                  t	        j
                  t        |            dB }}| j                  j                  |       y c c}w N)r,   r$   r-   )	r!   r9   timer$   r<   dumpsr   r"   chunked_insert_manyr%   rB   messagerH   s       r&   add_messagesz&AstraDBChatMessageHistory.add_messages   so    &&( $
 	 "YY["oo!ZZ(@A
 
 	++D1
s   ABc           
     >  K   | j                   j                          d {    |D cg c]@  }t        j                         | j                  t	        j
                  t        |            dB }}| j                  j                  |       d {    y 7 sc c}w 7 wrL   )	r!   rG   rM   r$   r<   rN   r   r#   rO   rP   s       r&   aadd_messagesz'AstraDBChatMessageHistory.aadd_messages   s     nn--/// $
 	 "YY["oo!ZZ(@A
 
 ##77=== 	0
 	>s-   BBBAB. BBBBc                    | j                   j                          | j                  j                  d| j                  i       y Nr$   )r/   )r!   r9   r"   delete_manyr$   r%   s    r&   clearzAstraDBChatMessageHistory.clear   s0    &&(##L$//+J#Kr(   c                   K   | j                   j                          d {    | j                  j                  d| j                  i       d {    y 7 57 wrV   )r!   rG   r#   rW   r$   rX   s    r&   aclearz AstraDBChatMessageHistory.aclear   sK     nn--///##//doo7V/WWW 	0Ws!   AA/AAAA)r$   strr   r\   r   Optional[str]r   r]   r   zOptional[AstraDB]r   zOptional[AsyncAstraDB]r   r]   r   r   r   boolreturnNone)r_   List[BaseMessage])rB   ra   r_   r`   )rB   zSequence[BaseMessage]r_   r`   )r_   r`   )__name__
__module____qualname__DEFAULT_COLLECTION_NAMEr   SYNCr'   propertyrB   setterrJ   rR   rT   rY   r[   r3   r(   r&   r   r      s      7#&*-18<#' )&+1/ 1/ 	1/
 1/ $1/ +1/  61/ !1/ 1/  $1/ 
1/f  , __> >(
2
>LXr(   r   )__doc__
__future__r   r<   rM   typingr   r   r   r   %langchain_community.utilities.astradbr   r	   
astrapy.dbr
   r   langchain_core._api.deprecationr   langchain_core.chat_historyr   langchain_core.messagesr   r   r   re   r   r3   r(   r&   <module>rq      sl    > "   : :
 0 6 >  4  
D
AX 6 AX
AXr(   