feat(history): Implement get_messages_by_window in HistoryManager
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user