
    3fi                     ^    d dl 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  G d de      Zy)    N)Path)ListOptional)BaseChatMessageHistory)BaseMessagemessages_from_dictmessages_to_dictc            	       j    e Zd ZdZddddedee   deddfd	Zede	e
   fd
       Zde
ddfdZddZy)FileChatMessageHistoryz9Chat message history that stores history in a local file.NT)encodingensure_ascii	file_pathr   r   returnc                6   t        |      | _        || _        || _        | j                  j	                         sa| j                  j                          | j                  j                  t        j                  g | j                        | j                         yy)a2  Initialize the file path for the chat history.
        Args:
            file_path: The path to the local file to store the chat history.
            encoding: The encoding to use for file operations. Defaults to None.
            ensure_ascii: If True, escape non-ASCII in JSON. Defaults to True.
        r   r   N)	r   r   r   r   existstouch
write_textjsondumps)selfr   r   r   s       m/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_community/chat_message_histories/file.py__init__zFileChatMessageHistory.__init__   st     i (~~$$&NN  "NN%%

2D,=,=> &  '    c                     t        j                  | j                  j                  | j                              }t        |      }|S )z)Retrieve the messages from the local filer   )r   loadsr   	read_textr   r   )r   itemsmessagess      r   r    zFileChatMessageHistory.messages%   s7     

4>>33T]]3KL%e,r   messagec                     t        | j                        }|j                  t        |g      d          | j                  j	                  t        j                  || j                        | j                         y)z2Append the message to the record in the local filer   r   r   N)	r	   r    appendr   r   r   r   r   r   )r   r!   r    s      r   add_messagez"FileChatMessageHistory.add_message,   sZ    #DMM2('3A67!!JJxd.?.?@4== 	" 	
r   c                     | j                   j                  t        j                  g | j                        | j
                         y)z(Clear session memory from the local filer   r   N)r   r   r   r   r   r   )r   s    r   clearzFileChatMessageHistory.clear4   s3    !!JJr(9(9:T]] 	" 	
r   )r   N)__name__
__module____qualname____doc__strr   boolr   propertyr   r   r    r$   r&    r   r   r   r      st    C #'! 3-	
  
. ${+  
; 
4 

r   r   )r   pathlibr   typingr   r   langchain_core.chat_historyr   langchain_core.messagesr   r   r	   r   r.   r   r   <module>r3      s)      ! V U-
3 -
r   