From 9dd35aac99ce96af46be3d3a1d1213529cc5469e Mon Sep 17 00:00:00 2001 From: Mic Neale Date: Mon, 23 Dec 2024 11:32:23 +1100 Subject: [PATCH] limit message count --- ui/desktop/src/ChatWindow.tsx | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/ui/desktop/src/ChatWindow.tsx b/ui/desktop/src/ChatWindow.tsx index 39abdfb4a..d1afcb3e3 100644 --- a/ui/desktop/src/ChatWindow.tsx +++ b/ui/desktop/src/ChatWindow.tsx @@ -20,6 +20,9 @@ import FlappyGoose from './components/FlappyGoose'; // update this when you want to show the welcome screen again - doesn't have to be an actual version, just anything woudln't have been seen before const CURRENT_VERSION = '0.0.0'; +// Maximum number of messages to keep in memory +const MAX_MESSAGES = 200; + // Get the last version from localStorage const getLastSeenVersion = () => localStorage.getItem('lastSeenVersion'); const setLastSeenVersion = (version: string) => localStorage.setItem('lastSeenVersion', version); @@ -114,8 +117,23 @@ function ChatContent({ }, }); - // Update chat messages when they change + // Update chat messages when they change and trim if exceeding limit useEffect(() => { + if (messages.length > MAX_MESSAGES) { + // Keep the most recent messages + const trimmedMessages = messages.slice(-MAX_MESSAGES); + setMessages(trimmedMessages); + + // Also trim the metadata for old messages + const newMetadata: Record = {}; + trimmedMessages.forEach(msg => { + if (messageMetadata[msg.id]) { + newMetadata[msg.id] = messageMetadata[msg.id]; + } + }); + setMessageMetadata(newMetadata); + } + const updatedChats = chats.map((c) => c.id === selectedChatId ? { ...c, messages } : c );