From b6af55134abeccff74bae19b9b7741ceaa0eb89f Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Fri, 28 Feb 2025 12:54:52 +0000 Subject: [PATCH] Refactor context change checks during chat summarization --- public/scripts/extensions/memory/index.js | 38 ++++++++++++----------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/public/scripts/extensions/memory/index.js b/public/scripts/extensions/memory/index.js index 241bff4e5..dd3178f9c 100644 --- a/public/scripts/extensions/memory/index.js +++ b/public/scripts/extensions/memory/index.js @@ -378,6 +378,23 @@ function getIndexOfLatestChatSummary(chat) { return -1; } +/** + * Check if something is changed during the summarization process. + * @param {{ groupId: any; chatId: any; characterId: any; }} context + * @returns {boolean} True if the context has changed and the summary should be discarded + */ +function isContextChanged(context) { + const newContext = getContext(); + if (newContext.groupId !== context.groupId + || newContext.chatId !== context.chatId + || (!newContext.groupId && (newContext.characterId !== context.characterId))) { + console.log('Context changed, summary discarded'); + return true; + } + + return false; +} + function onChatChanged() { const context = getContext(); const latestMemory = getLatestMemoryFromChat(context.chat); @@ -626,7 +643,6 @@ async function summarizeChatWebLLM(context, force) { try { inApiCall = true; const summary = await generateWebLlmChatPrompt(messages, params); - const newContext = getContext(); if (!summary) { console.warn('Empty summary received'); @@ -634,10 +650,7 @@ async function summarizeChatWebLLM(context, force) { } // something changed during summarization request - if (newContext.groupId !== context.groupId || - newContext.chatId !== context.chatId || - (!newContext.groupId && (newContext.characterId !== context.characterId))) { - console.log('Context changed, summary discarded'); + if (isContextChanged(context)) { return; } @@ -701,13 +714,7 @@ async function summarizeChatMain(context, force, skipWIAN) { return; } - const newContext = getContext(); - - // something changed during summarization request - if (newContext.groupId !== context.groupId - || newContext.chatId !== context.chatId - || (!newContext.groupId && (newContext.characterId !== context.characterId))) { - console.log('Context changed, summary discarded'); + if (isContextChanged(context)) { return; } @@ -833,18 +840,13 @@ async function summarizeChatExtras(context) { try { inApiCall = true; const summary = await callExtrasSummarizeAPI(resultingString); - const newContext = getContext(); if (!summary) { console.warn('Empty summary received'); return; } - // something changed during summarization request - if (newContext.groupId !== context.groupId - || newContext.chatId !== context.chatId - || (!newContext.groupId && (newContext.characterId !== context.characterId))) { - console.log('Context changed, summary discarded'); + if (isContextChanged(context)) { return; }