Skip to content

Commit e49fe97

Browse files
authored
Merge pull request #5765 from ConnectAI-E/feature/onfinish
feat: update real 'currentSession'
2 parents 14f7519 + e49466f commit e49fe97

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

app/components/chat.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1607,7 +1607,7 @@ function _Chat() {
16071607
title={Locale.Chat.Actions.RefreshTitle}
16081608
onClick={() => {
16091609
showToast(Locale.Chat.Actions.RefreshToast);
1610-
chatStore.summarizeSession(true);
1610+
chatStore.summarizeSession(true, session);
16111611
}}
16121612
/>
16131613
</div>

app/store/chat.ts

+22-9
Original file line numberDiff line numberDiff line change
@@ -352,13 +352,13 @@ export const useChatStore = createPersistStore(
352352
return session;
353353
},
354354

355-
onNewMessage(message: ChatMessage) {
356-
get().updateCurrentSession((session) => {
355+
onNewMessage(message: ChatMessage, targetSession: ChatSession) {
356+
get().updateTargetSession(targetSession, (session) => {
357357
session.messages = session.messages.concat();
358358
session.lastUpdate = Date.now();
359359
});
360360
get().updateStat(message);
361-
get().summarizeSession();
361+
get().summarizeSession(false, targetSession);
362362
},
363363

364364
async onUserInput(content: string, attachImages?: string[]) {
@@ -428,7 +428,7 @@ export const useChatStore = createPersistStore(
428428
botMessage.streaming = false;
429429
if (message) {
430430
botMessage.content = message;
431-
get().onNewMessage(botMessage);
431+
get().onNewMessage(botMessage, session);
432432
}
433433
ChatControllerPool.remove(session.id, botMessage.id);
434434
},
@@ -598,9 +598,12 @@ export const useChatStore = createPersistStore(
598598
});
599599
},
600600

601-
summarizeSession(refreshTitle: boolean = false) {
601+
summarizeSession(
602+
refreshTitle: boolean = false,
603+
targetSession: ChatSession,
604+
) {
602605
const config = useAppConfig.getState();
603-
const session = get().currentSession();
606+
const session = targetSession;
604607
const modelConfig = session.mask.modelConfig;
605608
// skip summarize when using dalle3?
606609
if (isDalle3(modelConfig.model)) {
@@ -651,7 +654,8 @@ export const useChatStore = createPersistStore(
651654
},
652655
onFinish(message, responseRes) {
653656
if (responseRes?.status === 200) {
654-
get().updateCurrentSession(
657+
get().updateTargetSession(
658+
session,
655659
(session) =>
656660
(session.topic =
657661
message.length > 0 ? trimTopic(message) : DEFAULT_TOPIC),
@@ -719,7 +723,7 @@ export const useChatStore = createPersistStore(
719723
onFinish(message, responseRes) {
720724
if (responseRes?.status === 200) {
721725
console.log("[Memory] ", message);
722-
get().updateCurrentSession((session) => {
726+
get().updateTargetSession(session, (session) => {
723727
session.lastSummarizeIndex = lastSummarizeIndex;
724728
session.memoryPrompt = message; // Update the memory prompt for stored it in local storage
725729
});
@@ -745,7 +749,16 @@ export const useChatStore = createPersistStore(
745749
updater(sessions[index]);
746750
set(() => ({ sessions }));
747751
},
748-
752+
updateTargetSession(
753+
targetSession: ChatSession,
754+
updater: (session: ChatSession) => void,
755+
) {
756+
const sessions = get().sessions;
757+
const index = sessions.findIndex((s) => s.id === targetSession.id);
758+
if (index < 0) return;
759+
updater(sessions[index]);
760+
set(() => ({ sessions }));
761+
},
749762
async clearAllData() {
750763
await indexedDBStorage.clear();
751764
localStorage.clear();

0 commit comments

Comments
 (0)