diff --git a/src/langchain_google_cloud_sql_pg/chat_message_history.py b/src/langchain_google_cloud_sql_pg/chat_message_history.py index 10f3540d..e4b6fb8d 100644 --- a/src/langchain_google_cloud_sql_pg/chat_message_history.py +++ b/src/langchain_google_cloud_sql_pg/chat_message_history.py @@ -106,11 +106,17 @@ def create_sync( history = engine._run_as_sync(coro) return cls(cls.__create_key, engine, history) - @property # type: ignore[override] + @property def messages(self) -> list[BaseMessage]: - """The abstraction required a property.""" + """Fetches all messages stored in AlloyDB.""" return self._engine._run_as_sync(self._history._aget_messages()) + @messages.setter + def messages(self, value: list[BaseMessage]) -> None: + """Clear the stored messages and appends a list of messages to the record in AlloyDB.""" + self.clear() + self.add_messages(value) + async def aadd_message(self, message: BaseMessage) -> None: """Append the message to the record in PostgreSQL""" await self._engine._run_as_async(self._history.aadd_message(message)) diff --git a/tests/test_chatmessagehistory.py b/tests/test_chatmessagehistory.py index 5ccedd39..206e9c3f 100644 --- a/tests/test_chatmessagehistory.py +++ b/tests/test_chatmessagehistory.py @@ -163,6 +163,20 @@ async def test_chat_message_history_sync_messages( assert len(history2.messages) == 0 +@pytest.mark.asyncio +async def test_chat_message_history_set_messages( + async_engine: PostgresEngine, +) -> None: + history = await PostgresChatMessageHistory.create( + engine=async_engine, session_id="test", table_name=table_name_async + ) + msg1 = HumanMessage(content="hi!") + msg2 = AIMessage(content="bye -_-") + # verify setting messages property adds to message history + history.messages = [msg1, msg2] + assert len(history.messages) == 2 + + @pytest.mark.asyncio async def test_chat_table_async(async_engine): with pytest.raises(ValueError):