
    3fi+                         d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZ  ej                  e      ZdZdZ edd	d
       G d de             Zy)    N)List)
deprecated)BaseChatMessageHistory)BaseMessagemessage_to_dictmessages_from_dictchat_historymessage_storez0.0.25z1.0z+langchain_mongodb.MongoDBChatMessageHistory)sinceremovalalternative_importc                   h    e Zd ZdZeedfdededededef
dZe	d	e
e   fd
       Zded	dfdZddZy)MongoDBChatMessageHistorya  Chat message history that stores history in MongoDB.

    Args:
        connection_string: connection string to connect to MongoDB
        session_id: arbitrary key that is used to store the messages
            of a single chat session.
        database_name: name of the database to use
        collection_name: name of the collection to use
        create_index: whether to create an index with name SessionId. Set to False if
            such an index already exists.
    Tconnection_string
session_iddatabase_namecollection_namecreate_indexc                 V   ddl m}m} || _        || _        || _        || _        	  ||      | _        | j                  |   | _        | j                  |   | _        |r| j                  j                  d       y y # |j                  $ r}t        j                  |       Y d }~pd }~ww xY w)Nr   )MongoClienterrors	SessionId)pymongor   r   r   r   r   r   clientConnectionFailureloggererrordb
collectionr   )	selfr   r   r   r   r   r   r   r   s	            p/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_community/chat_message_histories/mongodb.py__init__z"MongoDBChatMessageHistory.__init__%   s     	0!2$*.	 '23D'EDK ++m,''/2OO((5  '' 	 LL	 s   A: :B(	B##B(returnc                 8   ddl m} 	 | j                  j                  d| j                  i      }r&|D cg c]  }t        j                  |d          }}ng }t        |      }|S # |j
                  $ r}t        j                  |       Y d}~`d}~ww xY wc c}w )z"Retrieve the messages from MongoDBr   r   r   NHistory)r   r   r   findr   OperationFailurer   r   jsonloadsr   )r    r   cursorr   documentitemsmessagess          r!   r.   z"MongoDBChatMessageHistory.messages>   s     	#	 __));*HIF EKLTZZ 34LELE%e, && 	 LL	  Ms   'A& B&B5BBmessageNc                     ddl m} 	 | j                  j                  | j                  t        j                  t        |            d       y# |j                  $ r}t        j                  |       Y d}~yd}~ww xY w)z+Append the message to the record in MongoDBr   r%   )r   r&   N)r   r   r   
insert_oner   r)   dumpsr   
WriteErrorr   r   )r    r/   r   errs       r!   add_messagez%MongoDBChatMessageHistory.add_messageP   s`    "	OO&&!%#zz/'*BC    	LL	s   AA A;A66A;c                     ddl m} 	 | j                  j                  d| j                  i       y# |j
                  $ r}t        j                  |       Y d}~yd}~ww xY w)z!Clear session memory from MongoDBr   r%   r   N)r   r   r   delete_manyr   r3   r   r   )r    r   r4   s      r!   clearzMongoDBChatMessageHistory.clear^   sJ    "	OO''doo(FG   	LL	s   '0 AAA)r#   N)__name__
__module____qualname____doc__DEFAULT_DBNAMEDEFAULT_COLLECTION_NAMEstrboolr"   propertyr   r   r.   r5   r8        r!   r   r      s    
  ,6!66 6 	6
 6 62 ${+  "; 4 rC   r   )r)   loggingtypingr   langchain_core._api.deprecationr   langchain_core.chat_historyr   langchain_core.messagesr   r   r   	getLoggerr9   r   r=   r>   r   rB   rC   r!   <module>rJ      sj       6 >  
		8	$)  
D
M 6 M
MrC   