Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/langchain_google_cloud_sql_pg/chat_message_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
14 changes: 14 additions & 0 deletions tests/test_chatmessagehistory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down