Refactor context change checks during chat summarization

This commit is contained in:
Cohee
2025-02-28 12:54:52 +00:00
parent 1aae08be5b
commit b6af55134a

View File

@ -378,6 +378,23 @@ function getIndexOfLatestChatSummary(chat) {
return -1; 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() { function onChatChanged() {
const context = getContext(); const context = getContext();
const latestMemory = getLatestMemoryFromChat(context.chat); const latestMemory = getLatestMemoryFromChat(context.chat);
@ -626,7 +643,6 @@ async function summarizeChatWebLLM(context, force) {
try { try {
inApiCall = true; inApiCall = true;
const summary = await generateWebLlmChatPrompt(messages, params); const summary = await generateWebLlmChatPrompt(messages, params);
const newContext = getContext();
if (!summary) { if (!summary) {
console.warn('Empty summary received'); console.warn('Empty summary received');
@ -634,10 +650,7 @@ async function summarizeChatWebLLM(context, force) {
} }
// something changed during summarization request // something changed during summarization request
if (newContext.groupId !== context.groupId || if (isContextChanged(context)) {
newContext.chatId !== context.chatId ||
(!newContext.groupId && (newContext.characterId !== context.characterId))) {
console.log('Context changed, summary discarded');
return; return;
} }
@ -701,13 +714,7 @@ async function summarizeChatMain(context, force, skipWIAN) {
return; return;
} }
const newContext = getContext(); if (isContextChanged(context)) {
// 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');
return; return;
} }
@ -833,18 +840,13 @@ async function summarizeChatExtras(context) {
try { try {
inApiCall = true; inApiCall = true;
const summary = await callExtrasSummarizeAPI(resultingString); const summary = await callExtrasSummarizeAPI(resultingString);
const newContext = getContext();
if (!summary) { if (!summary) {
console.warn('Empty summary received'); console.warn('Empty summary received');
return; return;
} }
// something changed during summarization request if (isContextChanged(context)) {
if (newContext.groupId !== context.groupId
|| newContext.chatId !== context.chatId
|| (!newContext.groupId && (newContext.characterId !== context.characterId))) {
console.log('Context changed, summary discarded');
return; return;
} }