Refactor context change checks during chat summarization
| @@ -378,6 +378,23 @@ function getIndexOfLatestChatSummary(chat) { | ||
| 378 | 378 | return -1; |
| 379 | 379 | } |
| 380 | 380 | |
| 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 | + | |
| 381 | 398 | function onChatChanged() { |
| 382 | 399 | const context = getContext(); |
| 383 | 400 | const latestMemory = getLatestMemoryFromChat(context.chat); |
| @@ -626,7 +643,6 @@ async function summarizeChatWebLLM(context, force) { | ||
| 626 | 643 | try { |
| 627 | 644 | inApiCall = true; |
| 628 | 645 | const summary = await generateWebLlmChatPrompt(messages, params); |
| 629 | - const newContext = getContext(); | |
| 630 | 646 | |
| 631 | 647 | if (!summary) { |
| 632 | 648 | console.warn('Empty summary received'); |
| @@ -634,10 +650,7 @@ async function summarizeChatWebLLM(context, force) { | ||
| 634 | 650 | } |
| 635 | 651 | |
| 636 | 652 | // 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 | 654 | return; |
| 642 | 655 | } |
| 643 | 656 | |
| @@ -701,13 +714,7 @@ async function summarizeChatMain(context, force, skipWIAN) { | ||
| 701 | 714 | return; |
| 702 | 715 | } |
| 703 | 716 | |
| 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 | 718 | return; |
| 712 | 719 | } |
| 713 | 720 | |
| @@ -833,18 +840,13 @@ async function summarizeChatExtras(context) { | ||
| 833 | 840 | try { |
| 834 | 841 | inApiCall = true; |
| 835 | 842 | const summary = await callExtrasSummarizeAPI(resultingString); |
| 836 | - const newContext = getContext(); | |
| 837 | 843 | |
| 838 | 844 | if (!summary) { |
| 839 | 845 | console.warn('Empty summary received'); |
| 840 | 846 | return; |
| 841 | 847 | } |
| 842 | 848 | |
| 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'); | |
| 848 | 850 | return; |
| 849 | 851 | } |
| 850 | 852 | |