From e636d486f5b08fd14473f31b6c9e33ad92fe723a Mon Sep 17 00:00:00 2001 From: Jiacheng Dong <43509711+Jiacheng787@users.noreply.github.com> Date: Thu, 15 Jun 2023 10:23:01 +0800 Subject: [PATCH 1/3] fix: updating the array using push in zustand does not actually trigger component updates --- app/store/chat.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/app/store/chat.ts b/app/store/chat.ts index f56d9b9d..455399b8 100644 --- a/app/store/chat.ts +++ b/app/store/chat.ts @@ -227,6 +227,7 @@ export const useChatStore = create()( onNewMessage(message) { get().updateCurrentSession((session) => { + session.messages = session.messages.concat(); session.lastUpdate = Date.now(); }); get().updateStat(message); @@ -273,8 +274,9 @@ export const useChatStore = create()( // save user's and bot's message get().updateCurrentSession((session) => { - session.messages.push(userMessage); - session.messages.push(botMessage); + // session.messages.push(userMessage); + // session.messages.push(botMessage); + session.messages = session.messages.concat([userMessage, botMessage]); }); // make request @@ -287,7 +289,10 @@ export const useChatStore = create()( if (message) { botMessage.content = message; } - set(() => ({})); + // set(() => ({})); + get().updateCurrentSession((session) => { + session.messages = session.messages.concat(); + }); }, onFinish(message) { botMessage.streaming = false; @@ -299,7 +304,7 @@ export const useChatStore = create()( sessionIndex, botMessage.id ?? messageIndex, ); - set(() => ({})); + // set(() => ({})); }, onError(error) { const isAborted = error.message.includes("aborted"); From 1d42e955fc60365477ec9e4b38077dc5c6676924 Mon Sep 17 00:00:00 2001 From: Jiacheng Dong <43509711+Jiacheng787@users.noreply.github.com> Date: Thu, 15 Jun 2023 10:55:25 +0800 Subject: [PATCH 2/3] fix: updating the array using push in zustand does not actually trigger component updates --- app/store/chat.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/store/chat.ts b/app/store/chat.ts index 455399b8..335e4be7 100644 --- a/app/store/chat.ts +++ b/app/store/chat.ts @@ -317,8 +317,10 @@ export const useChatStore = create()( botMessage.streaming = false; userMessage.isError = !isAborted; botMessage.isError = !isAborted; - - set(() => ({})); + get().updateCurrentSession((session) => { + session.messages = session.messages.concat(); + }); + // set(() => ({})); ChatControllerPool.remove( sessionIndex, botMessage.id ?? messageIndex, From 4dd5bf71ea89a889ee0fc19fbeefe0c224f0e93a Mon Sep 17 00:00:00 2001 From: Jiacheng Dong <43509711+Jiacheng787@users.noreply.github.com> Date: Thu, 15 Jun 2023 13:59:38 +0800 Subject: [PATCH 3/3] Update chat.ts --- app/store/chat.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/app/store/chat.ts b/app/store/chat.ts index 335e4be7..e1bd2eb4 100644 --- a/app/store/chat.ts +++ b/app/store/chat.ts @@ -274,8 +274,6 @@ export const useChatStore = create()( // save user's and bot's message get().updateCurrentSession((session) => { - // session.messages.push(userMessage); - // session.messages.push(botMessage); session.messages = session.messages.concat([userMessage, botMessage]); }); @@ -289,7 +287,6 @@ export const useChatStore = create()( if (message) { botMessage.content = message; } - // set(() => ({})); get().updateCurrentSession((session) => { session.messages = session.messages.concat(); }); @@ -304,7 +301,6 @@ export const useChatStore = create()( sessionIndex, botMessage.id ?? messageIndex, ); - // set(() => ({})); }, onError(error) { const isAborted = error.message.includes("aborted"); @@ -320,7 +316,6 @@ export const useChatStore = create()( get().updateCurrentSession((session) => { session.messages = session.messages.concat(); }); - // set(() => ({})); ChatControllerPool.remove( sessionIndex, botMessage.id ?? messageIndex,