Skip to content

Commit f526d6f

Browse files
authored
Merge pull request ChatGPTNextWeb#5774 from ConnectAI-E/feature/update-target-session
fix: updateCurrentSession => updateTargetSession
2 parents f3603e5 + 106461a commit f526d6f

File tree

2 files changed

+56
-53
lines changed

2 files changed

+56
-53
lines changed

app/components/chat.tsx

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ export function SessionConfigModel(props: { onClose: () => void }) {
149149
text={Locale.Chat.Config.Reset}
150150
onClick={async () => {
151151
if (await showConfirm(Locale.Memory.ResetConfirm)) {
152-
chatStore.updateCurrentSession(
152+
chatStore.updateTargetSession(
153+
session,
153154
(session) => (session.memoryPrompt = ""),
154155
);
155156
}
@@ -174,7 +175,10 @@ export function SessionConfigModel(props: { onClose: () => void }) {
174175
updateMask={(updater) => {
175176
const mask = { ...session.mask };
176177
updater(mask);
177-
chatStore.updateCurrentSession((session) => (session.mask = mask));
178+
chatStore.updateTargetSession(
179+
session,
180+
(session) => (session.mask = mask),
181+
);
178182
}}
179183
shouldSyncFromGlobal
180184
extraListItems={
@@ -346,12 +350,14 @@ export function PromptHints(props: {
346350

347351
function ClearContextDivider() {
348352
const chatStore = useChatStore();
353+
const session = chatStore.currentSession();
349354

350355
return (
351356
<div
352357
className={styles["clear-context"]}
353358
onClick={() =>
354-
chatStore.updateCurrentSession(
359+
chatStore.updateTargetSession(
360+
session,
355361
(session) => (session.clearContextIndex = undefined),
356362
)
357363
}
@@ -461,6 +467,7 @@ export function ChatActions(props: {
461467
const navigate = useNavigate();
462468
const chatStore = useChatStore();
463469
const pluginStore = usePluginStore();
470+
const session = chatStore.currentSession();
464471

465472
// switch themes
466473
const theme = config.theme;
@@ -477,10 +484,9 @@ export function ChatActions(props: {
477484
const stopAll = () => ChatControllerPool.stopAll();
478485

479486
// switch model
480-
const currentModel = chatStore.currentSession().mask.modelConfig.model;
487+
const currentModel = session.mask.modelConfig.model;
481488
const currentProviderName =
482-
chatStore.currentSession().mask.modelConfig?.providerName ||
483-
ServiceProvider.OpenAI;
489+
session.mask.modelConfig?.providerName || ServiceProvider.OpenAI;
484490
const allModels = useAllModels();
485491
const models = useMemo(() => {
486492
const filteredModels = allModels.filter((m) => m.available);
@@ -514,12 +520,9 @@ export function ChatActions(props: {
514520
const dalle3Sizes: DalleSize[] = ["1024x1024", "1792x1024", "1024x1792"];
515521
const dalle3Qualitys: DalleQuality[] = ["standard", "hd"];
516522
const dalle3Styles: DalleStyle[] = ["vivid", "natural"];
517-
const currentSize =
518-
chatStore.currentSession().mask.modelConfig?.size ?? "1024x1024";
519-
const currentQuality =
520-
chatStore.currentSession().mask.modelConfig?.quality ?? "standard";
521-
const currentStyle =
522-
chatStore.currentSession().mask.modelConfig?.style ?? "vivid";
523+
const currentSize = session.mask.modelConfig?.size ?? "1024x1024";
524+
const currentQuality = session.mask.modelConfig?.quality ?? "standard";
525+
const currentStyle = session.mask.modelConfig?.style ?? "vivid";
523526

524527
const isMobileScreen = useMobileScreen();
525528

@@ -537,7 +540,7 @@ export function ChatActions(props: {
537540
if (isUnavailableModel && models.length > 0) {
538541
// show next model to default model if exist
539542
let nextModel = models.find((model) => model.isDefault) || models[0];
540-
chatStore.updateCurrentSession((session) => {
543+
chatStore.updateTargetSession(session, (session) => {
541544
session.mask.modelConfig.model = nextModel.name;
542545
session.mask.modelConfig.providerName = nextModel?.provider
543546
?.providerName as ServiceProvider;
@@ -548,7 +551,7 @@ export function ChatActions(props: {
548551
: nextModel.name,
549552
);
550553
}
551-
}, [chatStore, currentModel, models]);
554+
}, [chatStore, currentModel, models, session]);
552555

553556
return (
554557
<div className={styles["chat-input-actions"]}>
@@ -615,7 +618,7 @@ export function ChatActions(props: {
615618
text={Locale.Chat.InputActions.Clear}
616619
icon={<BreakIcon />}
617620
onClick={() => {
618-
chatStore.updateCurrentSession((session) => {
621+
chatStore.updateTargetSession(session, (session) => {
619622
if (session.clearContextIndex === session.messages.length) {
620623
session.clearContextIndex = undefined;
621624
} else {
@@ -647,7 +650,7 @@ export function ChatActions(props: {
647650
onSelection={(s) => {
648651
if (s.length === 0) return;
649652
const [model, providerName] = getModelProvider(s[0]);
650-
chatStore.updateCurrentSession((session) => {
653+
chatStore.updateTargetSession(session, (session) => {
651654
session.mask.modelConfig.model = model as ModelType;
652655
session.mask.modelConfig.providerName =
653656
providerName as ServiceProvider;
@@ -685,7 +688,7 @@ export function ChatActions(props: {
685688
onSelection={(s) => {
686689
if (s.length === 0) return;
687690
const size = s[0];
688-
chatStore.updateCurrentSession((session) => {
691+
chatStore.updateTargetSession(session, (session) => {
689692
session.mask.modelConfig.size = size;
690693
});
691694
showToast(size);
@@ -712,7 +715,7 @@ export function ChatActions(props: {
712715
onSelection={(q) => {
713716
if (q.length === 0) return;
714717
const quality = q[0];
715-
chatStore.updateCurrentSession((session) => {
718+
chatStore.updateTargetSession(session, (session) => {
716719
session.mask.modelConfig.quality = quality;
717720
});
718721
showToast(quality);
@@ -739,7 +742,7 @@ export function ChatActions(props: {
739742
onSelection={(s) => {
740743
if (s.length === 0) return;
741744
const style = s[0];
742-
chatStore.updateCurrentSession((session) => {
745+
chatStore.updateTargetSession(session, (session) => {
743746
session.mask.modelConfig.style = style;
744747
});
745748
showToast(style);
@@ -770,7 +773,7 @@ export function ChatActions(props: {
770773
}))}
771774
onClose={() => setShowPluginSelector(false)}
772775
onSelection={(s) => {
773-
chatStore.updateCurrentSession((session) => {
776+
chatStore.updateTargetSession(session, (session) => {
774777
session.mask.plugin = s as string[];
775778
});
776779
}}
@@ -813,7 +816,8 @@ export function EditMessageModal(props: { onClose: () => void }) {
813816
icon={<ConfirmIcon />}
814817
key="ok"
815818
onClick={() => {
816-
chatStore.updateCurrentSession(
819+
chatStore.updateTargetSession(
820+
session,
817821
(session) => (session.messages = messages),
818822
);
819823
props.onClose();
@@ -830,7 +834,8 @@ export function EditMessageModal(props: { onClose: () => void }) {
830834
type="text"
831835
value={session.topic}
832836
onInput={(e) =>
833-
chatStore.updateCurrentSession(
837+
chatStore.updateTargetSession(
838+
session,
834839
(session) => (session.topic = e.currentTarget.value),
835840
)
836841
}
@@ -991,7 +996,8 @@ function _Chat() {
991996
prev: () => chatStore.nextSession(-1),
992997
next: () => chatStore.nextSession(1),
993998
clear: () =>
994-
chatStore.updateCurrentSession(
999+
chatStore.updateTargetSession(
1000+
session,
9951001
(session) => (session.clearContextIndex = session.messages.length),
9961002
),
9971003
fork: () => chatStore.forkSession(),
@@ -1062,7 +1068,7 @@ function _Chat() {
10621068
};
10631069

10641070
useEffect(() => {
1065-
chatStore.updateCurrentSession((session) => {
1071+
chatStore.updateTargetSession(session, (session) => {
10661072
const stopTiming = Date.now() - REQUEST_TIMEOUT_MS;
10671073
session.messages.forEach((m) => {
10681074
// check if should stop all stale messages
@@ -1088,7 +1094,7 @@ function _Chat() {
10881094
}
10891095
});
10901096
// eslint-disable-next-line react-hooks/exhaustive-deps
1091-
}, []);
1097+
}, [session]);
10921098

10931099
// check if should send message
10941100
const onInputKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
@@ -1119,7 +1125,8 @@ function _Chat() {
11191125
};
11201126

11211127
const deleteMessage = (msgId?: string) => {
1122-
chatStore.updateCurrentSession(
1128+
chatStore.updateTargetSession(
1129+
session,
11231130
(session) =>
11241131
(session.messages = session.messages.filter((m) => m.id !== msgId)),
11251132
);
@@ -1186,7 +1193,7 @@ function _Chat() {
11861193
};
11871194

11881195
const onPinMessage = (message: ChatMessage) => {
1189-
chatStore.updateCurrentSession((session) =>
1196+
chatStore.updateTargetSession(session, (session) =>
11901197
session.mask.context.push(message),
11911198
);
11921199

@@ -1712,14 +1719,17 @@ function _Chat() {
17121719
});
17131720
}
17141721
}
1715-
chatStore.updateCurrentSession((session) => {
1716-
const m = session.mask.context
1717-
.concat(session.messages)
1718-
.find((m) => m.id === message.id);
1719-
if (m) {
1720-
m.content = newContent;
1721-
}
1722-
});
1722+
chatStore.updateTargetSession(
1723+
session,
1724+
(session) => {
1725+
const m = session.mask.context
1726+
.concat(session.messages)
1727+
.find((m) => m.id === message.id);
1728+
if (m) {
1729+
m.content = newContent;
1730+
}
1731+
},
1732+
);
17231733
}}
17241734
></IconButton>
17251735
</div>

app/store/chat.ts

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ export const useChatStore = createPersistStore(
357357
session.messages = session.messages.concat();
358358
session.lastUpdate = Date.now();
359359
});
360-
get().updateStat(message);
360+
get().updateStat(message, targetSession);
361361
get().summarizeSession(false, targetSession);
362362
},
363363

@@ -396,10 +396,10 @@ export const useChatStore = createPersistStore(
396396
// get recent messages
397397
const recentMessages = get().getMessagesWithMemory();
398398
const sendMessages = recentMessages.concat(userMessage);
399-
const messageIndex = get().currentSession().messages.length + 1;
399+
const messageIndex = session.messages.length + 1;
400400

401401
// save user's and bot's message
402-
get().updateCurrentSession((session) => {
402+
get().updateTargetSession(session, (session) => {
403403
const savedUserMessage = {
404404
...userMessage,
405405
content: mContent,
@@ -420,7 +420,7 @@ export const useChatStore = createPersistStore(
420420
if (message) {
421421
botMessage.content = message;
422422
}
423-
get().updateCurrentSession((session) => {
423+
get().updateTargetSession(session, (session) => {
424424
session.messages = session.messages.concat();
425425
});
426426
},
@@ -434,7 +434,7 @@ export const useChatStore = createPersistStore(
434434
},
435435
onBeforeTool(tool: ChatMessageTool) {
436436
(botMessage.tools = botMessage?.tools || []).push(tool);
437-
get().updateCurrentSession((session) => {
437+
get().updateTargetSession(session, (session) => {
438438
session.messages = session.messages.concat();
439439
});
440440
},
@@ -444,7 +444,7 @@ export const useChatStore = createPersistStore(
444444
tools[i] = { ...tool };
445445
}
446446
});
447-
get().updateCurrentSession((session) => {
447+
get().updateTargetSession(session, (session) => {
448448
session.messages = session.messages.concat();
449449
});
450450
},
@@ -459,7 +459,7 @@ export const useChatStore = createPersistStore(
459459
botMessage.streaming = false;
460460
userMessage.isError = !isAborted;
461461
botMessage.isError = !isAborted;
462-
get().updateCurrentSession((session) => {
462+
get().updateTargetSession(session, (session) => {
463463
session.messages = session.messages.concat();
464464
});
465465
ChatControllerPool.remove(
@@ -591,8 +591,8 @@ export const useChatStore = createPersistStore(
591591
set(() => ({ sessions }));
592592
},
593593

594-
resetSession() {
595-
get().updateCurrentSession((session) => {
594+
resetSession(session: ChatSession) {
595+
get().updateTargetSession(session, (session) => {
596596
session.messages = [];
597597
session.memoryPrompt = "";
598598
});
@@ -736,19 +736,12 @@ export const useChatStore = createPersistStore(
736736
}
737737
},
738738

739-
updateStat(message: ChatMessage) {
740-
get().updateCurrentSession((session) => {
739+
updateStat(message: ChatMessage, session: ChatSession) {
740+
get().updateTargetSession(session, (session) => {
741741
session.stat.charCount += message.content.length;
742742
// TODO: should update chat count and word count
743743
});
744744
},
745-
746-
updateCurrentSession(updater: (session: ChatSession) => void) {
747-
const sessions = get().sessions;
748-
const index = get().currentSessionIndex;
749-
updater(sessions[index]);
750-
set(() => ({ sessions }));
751-
},
752745
updateTargetSession(
753746
targetSession: ChatSession,
754747
updater: (session: ChatSession) => void,

0 commit comments

Comments
 (0)