Refactor context change checks during chat summarization
| @@ -378,6 +378,23 @@ function getIndexOfLatestChatSummary(chat) { | |||
| 378 | return -1; | 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 | function onChatChanged() { | 398 | function 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(); | ||
| 630 | 646 | ||
| 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 | } |
| 635 | 651 | ||
| 636 | // something changed during summarization request | 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 | return; | 654 | return; |
| 642 | } | 655 | } |
| 643 | 656 | ||
| @@ -701,13 +714,7 @@ async function summarizeChatMain(context, force, skipWIAN) { | |||
| 701 | return; | 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 | return; | 718 | return; |
| 712 | } | 719 | } |
| 713 | 720 | ||
| @@ -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(); | ||
| 837 | 843 | ||
| 838 | if (!summary) { | 844 | if (!summary) { |
| 839 | console.warn('Empty summary received'); | 845 | console.warn('Empty summary received'); |
| 840 | return; | 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 | return; | 850 | return; |
| 849 | } | 851 | } |
| 850 | 852 | ||