
    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 eddd	d
       G d de             Zy)    N)List)
deprecated)BaseChatMessageHistory)BaseMessagemessage_to_dictmessages_from_dictz7postgresql://postgres:mypassword@localhost/chat_historyz0.0.31a  This class is deprecated and will be removed in a future version. You can swap to using the `PostgresChatMessageHistory` implementation in `langchain_postgres`. Please do not submit further PRs to this class.See <https://github.com/langchain-ai/langchain-postgres>z:from langchain_postgres import PostgresChatMessageHistory;T)sincemessagealternativependingc                   n    e Zd ZdZedfdededefdZdd	Zede	e
   fd
       Zde
ddfdZddZddZy)PostgresChatMessageHistoryzChat message history stored in a Postgres database.

    **DEPRECATED**: This class is deprecated and will be removed in a future version.

    Use the `PostgresChatMessageHistory` implementation in `langchain_postgres`.
    message_store
session_idconnection_string
table_namec                 &   dd l }ddlm} 	 |j                  |      | _        | j                  j                  |      | _        || _	        || _
        | j                          y # |j                  $ r}t        j                  |       Y d }~Hd }~ww xY w)Nr   )dict_row)row_factory)psycopgpsycopg.rowsr   connect
connectioncursorOperationalErrorloggererrorr   r   _create_table_if_not_exists)selfr   r   r   r   r   r   s          q/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_community/chat_message_histories/postgres.py__init__z#PostgresChatMessageHistory.__init__&   s|     	)	 %oo.?@DO//00X0FDK %$((* '' 	 LL	 s   7A" "B1BBreturnNc                     d| j                    d}| j                  j                  |       | j                  j	                          y )NzCREATE TABLE IF NOT EXISTS zy (
            id SERIAL PRIMARY KEY,
            session_id TEXT NOT NULL,
            message JSONB NOT NULL
        );)r   r   executer   commit)r   create_table_querys     r    r   z6PostgresChatMessageHistory._create_table_if_not_exists:   sA    !<T__<M N 
 	./     c                     d| j                    d}| j                  j                  || j                  f       | j                  j	                         D cg c]  }|d   	 }}t        |      }|S c c}w )z%Retrieve the messages from PostgreSQLzSELECT message FROM z# WHERE session_id = %s ORDER BY id;r
   )r   r   r$   r   fetchallr   )r   queryrecorditemsmessagess        r    r-   z#PostgresChatMessageHistory.messagesC   sr     #4??"33VW 	 	EDOO#56151E1E1GHv	"HH%e, Is   A/r
   c           	      >   ddl m} |j                  d      j                  |j	                  | j
                              }| j                  j                  || j                  t        j                  t        |            f       | j                  j                          y)z.Append the message to the record in PostgreSQLr   )sqlz5INSERT INTO {} (session_id, message) VALUES (%s, %s);N)r   r/   SQLformat
Identifierr   r   r$   r   jsondumpsr   r   r%   )r   r
   r/   r*   s       r    add_messagez&PostgresChatMessageHistory.add_messageN   sq    OPWWNN4??+
 	DOOTZZ0H%IJ	
 	 r'   c                     d| j                    d}| j                  j                  || j                  f       | j                  j                          y)z$Clear session memory from PostgreSQLzDELETE FROM z WHERE session_id = %s;N)r   r   r$   r   r   r%   )r   r*   s     r    clearz PostgresChatMessageHistory.clearZ   sA    t//FGEDOO#56 r'   c                     | j                   r| j                   j                          | j                  r| j                  j                          y y )N)r   closer   )r   s    r    __del__z"PostgresChatMessageHistory.__del__`   s5    ;;KK??OO!!# r'   )r"   N)__name__
__module____qualname____doc__DEFAULT_CONNECTION_STRINGstrr!   r   propertyr   r   r-   r5   r7   r:    r'   r    r   r      sr     ";)	++ + 	+(! ${+  
!; 
!4 
!!$r'   r   )r3   loggingtypingr   langchain_core._apir   langchain_core.chat_historyr   langchain_core.messagesr   r   r   	getLoggerr;   r   r?   r   rB   r'   r    <module>rI      sn       * >  
		8	$U  
	C MF$!7 F$F$r'   