mirror of
https://github.com/Yidadaa/ChatGPT-Next-Web.git
synced 2025-08-08 23:20:28 +08:00
feat: close #1415 clear context button
This commit is contained in:
@@ -45,6 +45,7 @@ export interface ChatSession {
|
||||
stat: ChatStat;
|
||||
lastUpdate: number;
|
||||
lastSummarizeIndex: number;
|
||||
clearContextIndex?: number;
|
||||
|
||||
mask: Mask;
|
||||
}
|
||||
@@ -341,7 +342,12 @@ export const useChatStore = create<ChatStore>()(
|
||||
getMessagesWithMemory() {
|
||||
const session = get().currentSession();
|
||||
const modelConfig = session.mask.modelConfig;
|
||||
const messages = session.messages.filter((msg) => !msg.isError);
|
||||
|
||||
// wont send cleared context messages
|
||||
const clearedContextMessages = session.messages.slice(
|
||||
(session.clearContextIndex ?? -1) + 1,
|
||||
);
|
||||
const messages = clearedContextMessages.filter((msg) => !msg.isError);
|
||||
const n = messages.length;
|
||||
|
||||
const context = session.mask.context.slice();
|
||||
@@ -362,17 +368,17 @@ export const useChatStore = create<ChatStore>()(
|
||||
n - modelConfig.historyMessageCount,
|
||||
);
|
||||
const longTermMemoryMessageIndex = session.lastSummarizeIndex;
|
||||
const oldestIndex = Math.max(
|
||||
const mostRecentIndex = Math.max(
|
||||
shortTermMemoryMessageIndex,
|
||||
longTermMemoryMessageIndex,
|
||||
);
|
||||
const threshold = modelConfig.compressMessageLengthThreshold;
|
||||
const threshold = modelConfig.compressMessageLengthThreshold * 2;
|
||||
|
||||
// get recent messages as many as possible
|
||||
const reversedRecentMessages = [];
|
||||
for (
|
||||
let i = n - 1, count = 0;
|
||||
i >= oldestIndex && count < threshold;
|
||||
i >= mostRecentIndex && count < threshold;
|
||||
i -= 1
|
||||
) {
|
||||
const msg = messages[i];
|
||||
@@ -410,15 +416,15 @@ export const useChatStore = create<ChatStore>()(
|
||||
const session = get().currentSession();
|
||||
|
||||
// remove error messages if any
|
||||
const cleanMessages = session.messages.filter((msg) => !msg.isError);
|
||||
const messages = session.messages;
|
||||
|
||||
// should summarize topic after chating more than 50 words
|
||||
const SUMMARIZE_MIN_LEN = 50;
|
||||
if (
|
||||
session.topic === DEFAULT_TOPIC &&
|
||||
countMessages(cleanMessages) >= SUMMARIZE_MIN_LEN
|
||||
countMessages(messages) >= SUMMARIZE_MIN_LEN
|
||||
) {
|
||||
const topicMessages = cleanMessages.concat(
|
||||
const topicMessages = messages.concat(
|
||||
createMessage({
|
||||
role: "user",
|
||||
content: Locale.Store.Prompt.Topic,
|
||||
@@ -440,9 +446,13 @@ export const useChatStore = create<ChatStore>()(
|
||||
}
|
||||
|
||||
const modelConfig = session.mask.modelConfig;
|
||||
let toBeSummarizedMsgs = cleanMessages.slice(
|
||||
const summarizeIndex = Math.max(
|
||||
session.lastSummarizeIndex,
|
||||
session.clearContextIndex ?? 0,
|
||||
);
|
||||
let toBeSummarizedMsgs = messages
|
||||
.filter((msg) => !msg.isError)
|
||||
.slice(summarizeIndex);
|
||||
|
||||
const historyMsgLength = countMessages(toBeSummarizedMsgs);
|
||||
|
||||
|
Reference in New Issue
Block a user