Refactor context change checks during chat summarization

b6af55134abeccff74bae19b9b7741ceaa0eb89f

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

1 files changed, +20 -18Ignore whitespace
public/scripts/extensions/memory/index.js+20 -18
@@ -378,6 +378,23 @@ function getIndexOfLatestChatSummary(chat) {
378 return -1;378 return -1;
379}379}
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 */
386function 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
381function onChatChanged() {398function onChatChanged() {
382 const context = getContext();399 const context = getContext();
383 const latestMemory = getLatestMemoryFromChat(context.chat);400 const latestMemory = getLatestMemoryFromChat(context.chat);
@@ -626,7 +643,6 @@ async function summarizeChatWebLLM(context, force) {
626 try {643 try {
627 inApiCall = true;644 inApiCall = true;
628 const summary = await generateWebLlmChatPrompt(messages, params);645 const summary = await generateWebLlmChatPrompt(messages, params);
629 const newContext = getContext();
630646
631 if (!summary) {647 if (!summary) {
632 console.warn('Empty summary received');648 console.warn('Empty summary received');
@@ -634,10 +650,7 @@ async function summarizeChatWebLLM(context, force) {
634 }650 }
635651
636 // something changed during summarization request652 // 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');
641 return;654 return;
642 }655 }
643656
@@ -701,13 +714,7 @@ async function summarizeChatMain(context, force, skipWIAN) {
701 return;714 return;
702 }715 }
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');
711 return;718 return;
712 }719 }
713720
@@ -833,18 +840,13 @@ async function summarizeChatExtras(context) {
833 try {840 try {
834 inApiCall = true;841 inApiCall = true;
835 const summary = await callExtrasSummarizeAPI(resultingString);842 const summary = await callExtrasSummarizeAPI(resultingString);
836 const newContext = getContext();
837843
838 if (!summary) {844 if (!summary) {
839 console.warn('Empty summary received');845 console.warn('Empty summary received');
840 return;846 return;
841 }847 }
842848
843 // something changed during summarization request849 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');
848 return;850 return;
849 }851 }
850852