feat(history): Implement get_messages_by_window in HistoryManager

This commit is contained in:
Yunxiao Xu
2026-02-15 03:20:12 -08:00
parent 68c0985482
commit be687d0fa5
2 changed files with 69 additions and 1 deletions

View File

@@ -185,4 +185,28 @@ class HistoryManager:
# Pre-load plots for each message
for m in messages:
_ = m.plots
return messages
return messages
def get_messages_by_window(self, conversation_id: str, window_size: int = 10) -> List[Message]:
"""
Get the last N messages for a conversation, ordered by creation time (ascending).
"""
with self.get_session() as session:
# 1. Get the last N messages in descending order
stmt = (
select(Message)
.where(Message.conversation_id == conversation_id)
.order_by(Message.created_at.desc())
.limit(window_size)
)
result = session.execute(stmt)
messages = list(result.scalars().all())
# 2. Reverse to get ascending chronological order
messages.reverse()
# Pre-load plots if needed (though graph usually only needs text)
for m in messages:
_ = m.plots
return messages