Refactor context change checks during chat summarization

b6af55134abeccff74bae19b9b7741ceaa0eb89f

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +20 -18Showing whitespace changes
public/scripts/extensions/memory/index.js+20 -18
@@ -378,6 +378,23 @@ function getIndexOfLatestChatSummary(chat) {
378378 return -1;
379379}
380380
381+/**
382+ * Check if something is changed during the summarization process.
383+ * @param {{ groupId: any; chatId: any; characterId: any; }} context
384+ * @returns {boolean} True if the context has changed and the summary should be discarded
385+ */
386+function isContextChanged(context) {
387+ const newContext = getContext();
388+ if (newContext.groupId !== context.groupId
389+ || newContext.chatId !== context.chatId
390+ || (!newContext.groupId && (newContext.characterId !== context.characterId))) {
391+ console.log('Context changed, summary discarded');
392+ return true;
393+ }
394+
395+ return false;
396+}
397+
381398function onChatChanged() {
382399 const context = getContext();
383400 const latestMemory = getLatestMemoryFromChat(context.chat);
@@ -626,7 +643,6 @@ async function summarizeChatWebLLM(context, force) {
626643 try {
627644 inApiCall = true;
628645 const summary = await generateWebLlmChatPrompt(messages, params);
629- const newContext = getContext();
630646
631647 if (!summary) {
632648 console.warn('Empty summary received');
@@ -634,10 +650,7 @@ async function summarizeChatWebLLM(context, force) {
634650 }
635651
636652 // something changed during summarization request
637- if (newContext.groupId !== context.groupId ||
653+ if (isContextChanged(context)) {
638- newContext.chatId !== context.chatId ||
639- (!newContext.groupId && (newContext.characterId !== context.characterId))) {
640- console.log('Context changed, summary discarded');
641654 return;
642655 }
643656
@@ -701,13 +714,7 @@ async function summarizeChatMain(context, force, skipWIAN) {
701714 return;
702715 }
703716
704- const newContext = getContext();
717+ if (isContextChanged(context)) {
705-
706- // something changed during summarization request
707- if (newContext.groupId !== context.groupId
708- || newContext.chatId !== context.chatId
709- || (!newContext.groupId && (newContext.characterId !== context.characterId))) {
710- console.log('Context changed, summary discarded');
711718 return;
712719 }
713720
@@ -833,18 +840,13 @@ async function summarizeChatExtras(context) {
833840 try {
834841 inApiCall = true;
835842 const summary = await callExtrasSummarizeAPI(resultingString);
836- const newContext = getContext();
837843
838844 if (!summary) {
839845 console.warn('Empty summary received');
840846 return;
841847 }
842848
843- // something changed during summarization request
849+ if (isContextChanged(context)) {
844- if (newContext.groupId !== context.groupId
845- || newContext.chatId !== context.chatId
846- || (!newContext.groupId && (newContext.characterId !== context.characterId))) {
847- console.log('Context changed, summary discarded');
848850 return;
849851 }
850852