From 54ee4da2949c37fb636519b9ea8ab8d866948889 Mon Sep 17 00:00:00 2001 From: Nico Arqueros <1622112+nicarq@users.noreply.github.com> Date: Fri, 8 Sep 2023 21:41:06 +0800 Subject: [PATCH] fixed frontend --- shinkai-app/src/components/ChatMessages.tsx | 16 ++++++++++++---- src/db/db_inbox.rs | 5 ++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/shinkai-app/src/components/ChatMessages.tsx b/shinkai-app/src/components/ChatMessages.tsx index cec689684..5598ed961 100644 --- a/shinkai-app/src/components/ChatMessages.tsx +++ b/shinkai-app/src/components/ChatMessages.tsx @@ -24,6 +24,7 @@ const ChatMessages: React.FC = ({ deserializedId }) => { ); const [lastKey, setLastKey] = useState(undefined); + const [mostRecentKey, setMostRecentKey] = useState(undefined); const [prevMessagesLength, setPrevMessagesLength] = useState(0); const [hasMoreMessages, setHasMoreMessages] = useState(true); const [messages, setMessages] = useState([]); @@ -42,13 +43,13 @@ const ChatMessages: React.FC = ({ deserializedId }) => { console.log("Debug> Last Message:", lastMessage); console.log("Debug> Last Message Content:", extractContent(lastMessage.body)); console.log("Debug> Hash Key from above:", hashKey); - console.log("Debug> Last key (stored):", lastKey); + console.log("Debug> Last key (stored):", mostRecentKey); dispatch( - getLastUnreadMessagesFromInbox(deserializedId, 10, lastKey, setupDetailsState) + getLastUnreadMessagesFromInbox(deserializedId, 10, mostRecentKey, setupDetailsState) ); }, 5000); // 2000 milliseconds = 2 seconds return () => clearInterval(interval); - }, [dispatch, deserializedId, lastKey, setupDetailsState, reduxMessages]); + }, [dispatch, deserializedId, mostRecentKey, setupDetailsState, reduxMessages]); useEffect(() => { if (reduxMessages && reduxMessages.length > 0) { @@ -57,8 +58,15 @@ const ChatMessages: React.FC = ({ deserializedId }) => { console.log("Last Message:", lastMessage); const timeKey = lastMessage.external_metadata.scheduled_time; const hashKey = calculateMessageHash(lastMessage); - const lastMessageKey = `${timeKey}:${hashKey}`; + const lastMessageKey = `${timeKey}:::${hashKey}`; setLastKey(lastMessageKey); + + const mostRecentMessage = reduxMessages[0]; + const mostRecentTimeKey = mostRecentMessage.external_metadata.scheduled_time; + const mostRecentHashKey = calculateMessageHash(mostRecentMessage); + const mostRecentMessageKey = `${mostRecentTimeKey}:::${mostRecentHashKey}`; + setMostRecentKey(mostRecentMessageKey); + setMessages(reduxMessages); if (reduxMessages.length - prevMessagesLength < 10) { diff --git a/src/db/db_inbox.rs b/src/db/db_inbox.rs index 9a651d1d7..663350525 100644 --- a/src/db/db_inbox.rs +++ b/src/db/db_inbox.rs @@ -263,11 +263,14 @@ impl ShinkaiDB { let offset_hash = match &from_offset_key { Some(offset_key) => { let split: Vec<&str> = offset_key.split(":::").collect(); + if split.len() < 2 { + return Err(ShinkaiDBError::SomeError("Invalid offset key format".to_string())); + } Some(split[1].to_string()) } None => None, }; - + let mut messages = Vec::new(); let mut first_message = true; for item in iter.take(n) {