Merge pull request #3542 from SillyTavern/summary-improvements Assorted summary improvements
Signed| @@ -32,9 +32,6 @@ export { MODULE_NAME }; | |||
| 32 | 32 | ||
| 33 | const MODULE_NAME = '1_memory'; | 33 | const MODULE_NAME = '1_memory'; |
| 34 | 34 | ||
| 35 | let lastCharacterId = null; | ||
| 36 | let lastGroupId = null; | ||
| 37 | let lastChatId = null; | ||
| 38 | let lastMessageHash = null; | 35 | let lastMessageHash = null; |
| 39 | let lastMessageId = null; | 36 | let lastMessageId = null; |
| 40 | let inApiCall = false; | 37 | let inApiCall = false; |
| @@ -251,7 +248,7 @@ function onSummarySourceChange(event) { | |||
| 251 | } | 248 | } |
| 252 | 249 | ||
| 253 | function switchSourceControls(value) { | 250 | function switchSourceControls(value) { |
| 254 | $('#memory_settings [data-summary-source]').each((_, element) => { | 251 | $('#summaryExtensionDrawerContents [data-summary-source], #memory_settings [data-summary-source]').each((_, element) => { |
| 255 | const source = element.dataset.summarySource.split(',').map(s => s.trim()); | 252 | const source = element.dataset.summarySource.split(',').map(s => s.trim()); |
| 256 | $(element).toggle(source.includes(value)); | 253 | $(element).toggle(source.includes(value)); |
| 257 | }); | 254 | }); |
| @@ -349,15 +346,6 @@ function onMaxMessagesPerRequestInput() { | |||
| 349 | saveSettingsDebounced(); | 346 | saveSettingsDebounced(); |
| 350 | } | 347 | } |
| 351 | 348 | ||
| 352 | function saveLastValues() { | ||
| 353 | const context = getContext(); | ||
| 354 | lastGroupId = context.groupId; | ||
| 355 | lastCharacterId = context.characterId; | ||
| 356 | lastChatId = context.chatId; | ||
| 357 | lastMessageId = context.chat?.length ?? null; | ||
| 358 | lastMessageHash = getStringHash((context.chat.length && context.chat[context.chat.length - 1]['mes']) ?? ''); | ||
| 359 | } | ||
| 360 | |||
| 361 | function getLatestMemoryFromChat(chat) { | 349 | function getLatestMemoryFromChat(chat) { |
| 362 | if (!Array.isArray(chat) || !chat.length) { | 350 | if (!Array.isArray(chat) || !chat.length) { |
| 363 | return ''; | 351 | return ''; |
| @@ -390,6 +378,12 @@ function getIndexOfLatestChatSummary(chat) { | |||
| 390 | return -1; | 378 | return -1; |
| 391 | } | 379 | } |
| 392 | 380 | ||
| 381 | function onChatChanged() { | ||
| 382 | const context = getContext(); | ||
| 383 | const latestMemory = getLatestMemoryFromChat(context.chat); | ||
| 384 | setMemoryContext(latestMemory, false); | ||
| 385 | } | ||
| 386 | |||
| 393 | async function onChatEvent() { | 387 | async function onChatEvent() { |
| 394 | // Module not enabled | 388 | // Module not enabled |
| 395 | if (extension_settings.memory.source === summary_sources.extras && !modules.includes('summarize')) { | 389 | if (extension_settings.memory.source === summary_sources.extras && !modules.includes('summarize')) { |
| @@ -401,32 +395,19 @@ async function onChatEvent() { | |||
| 401 | return; | 395 | return; |
| 402 | } | 396 | } |
| 403 | 397 | ||
| 404 | const context = getContext(); | ||
| 405 | const chat = context.chat; | ||
| 406 | |||
| 407 | // no characters or group selected | ||
| 408 | if (!context.groupId && context.characterId === undefined) { | ||
| 409 | return; | ||
| 410 | } | ||
| 411 | |||
| 412 | // Streaming in-progress | 398 | // Streaming in-progress |
| 413 | if (streamingProcessor && !streamingProcessor.isFinished) { | 399 | if (streamingProcessor && !streamingProcessor.isFinished) { |
| 414 | return; | 400 | return; |
| 415 | } | 401 | } |
| 416 | 402 | ||
| 417 | // Chat/character/group changed | ||
| 418 | if ((context.groupId && lastGroupId !== context.groupId) || (context.characterId !== lastCharacterId) || (context.chatId !== lastChatId)) { | ||
| 419 | const latestMemory = getLatestMemoryFromChat(chat); | ||
| 420 | setMemoryContext(latestMemory, false); | ||
| 421 | saveLastValues(); | ||
| 422 | return; | ||
| 423 | } | ||
| 424 | |||
| 425 | // Currently summarizing or frozen state - skip | 403 | // Currently summarizing or frozen state - skip |
| 426 | if (inApiCall || extension_settings.memory.memoryFrozen) { | 404 | if (inApiCall || extension_settings.memory.memoryFrozen) { |
| 427 | return; | 405 | return; |
| 428 | } | 406 | } |
| 429 | 407 | ||
| 408 | const context = getContext(); | ||
| 409 | const chat = context.chat; | ||
| 410 | |||
| 430 | // No new messages - do nothing | 411 | // No new messages - do nothing |
| 431 | if (chat.length === 0 || (lastMessageId === chat.length && getStringHash(chat[chat.length - 1].mes) === lastMessageHash)) { | 412 | if (chat.length === 0 || (lastMessageId === chat.length && getStringHash(chat[chat.length - 1].mes) === lastMessageHash)) { |
| 432 | return; | 413 | return; |
| @@ -449,7 +430,10 @@ async function onChatEvent() { | |||
| 449 | 430 | ||
| 450 | summarizeChat(context) | 431 | summarizeChat(context) |
| 451 | .catch(console.error) | 432 | .catch(console.error) |
| 452 | .finally(saveLastValues); | 433 | .finally(() => { |
| 434 | lastMessageId = context.chat?.length ?? null; | ||
| 435 | lastMessageHash = getStringHash((context.chat.length && context.chat[context.chat.length - 1]['mes']) ?? ''); | ||
| 436 | }); | ||
| 453 | } | 437 | } |
| 454 | 438 | ||
| 455 | /** | 439 | /** |
| @@ -464,13 +448,7 @@ async function forceSummarizeChat(quiet) { | |||
| 464 | } | 448 | } |
| 465 | 449 | ||
| 466 | const context = getContext(); | 450 | const context = getContext(); |
| 467 | |||
| 468 | const skipWIAN = extension_settings.memory.SkipWIAN; | 451 | const skipWIAN = extension_settings.memory.SkipWIAN; |
| 469 | console.log(`Skipping WIAN? ${skipWIAN}`); | ||
| 470 | if (!context.chatId) { | ||
| 471 | toastr.warning('No chat selected'); | ||
| 472 | return ''; | ||
| 473 | } | ||
| 474 | 452 | ||
| 475 | const toast = quiet ? jQuery() : toastr.info('Summarizing chat...', 'Please wait', { timeOut: 0, extendedTimeOut: 0 }); | 453 | const toast = quiet ? jQuery() : toastr.info('Summarizing chat...', 'Please wait', { timeOut: 0, extendedTimeOut: 0 }); |
| 476 | const value = extension_settings.memory.source === summary_sources.main | 454 | const value = extension_settings.memory.source === summary_sources.main |
| @@ -993,7 +971,7 @@ function doPopout(e) { | |||
| 993 | .removeClass('zoomed_avatar') | 971 | .removeClass('zoomed_avatar') |
| 994 | .addClass('draggable') | 972 | .addClass('draggable') |
| 995 | .empty(); | 973 | .empty(); |
| 996 | const prevSummaryBoxContents = $('#memory_contents').val(); //copy summary box before emptying | 974 | const prevSummaryBoxContents = $('#memory_contents').val().toString(); //copy summary box before emptying |
| 997 | originalElement.empty(); | 975 | originalElement.empty(); |
| 998 | originalElement.html('<div class="flex-container alignitemscenter justifyCenter wide100p"><small>Currently popped out</small></div>'); | 976 | originalElement.html('<div class="flex-container alignitemscenter justifyCenter wide100p"><small>Currently popped out</small></div>'); |
| 999 | newElement.append(controlBarHtml).append(originalHTMLClone); | 977 | newElement.append(controlBarHtml).append(originalHTMLClone); |
| @@ -1013,7 +991,7 @@ function doPopout(e) { | |||
| 1013 | const summaryPopoutHTML = $('#summaryExtensionDrawerContents'); | 991 | const summaryPopoutHTML = $('#summaryExtensionDrawerContents'); |
| 1014 | $('#summaryExtensionPopout').fadeOut(animation_duration, () => { | 992 | $('#summaryExtensionPopout').fadeOut(animation_duration, () => { |
| 1015 | originalElement.empty(); | 993 | originalElement.empty(); |
| 1016 | originalElement.html(summaryPopoutHTML); | 994 | originalElement.append(summaryPopoutHTML); |
| 1017 | $('#summaryExtensionPopout').remove(); | 995 | $('#summaryExtensionPopout').remove(); |
| 1018 | }); | 996 | }); |
| 1019 | loadSettings(); | 997 | loadSettings(); |
| @@ -1027,31 +1005,30 @@ function doPopout(e) { | |||
| 1027 | function setupListeners() { | 1005 | function setupListeners() { |
| 1028 | //setup shared listeners for popout and regular ext menu | 1006 | //setup shared listeners for popout and regular ext menu |
| 1029 | $('#memory_restore').off('click').on('click', onMemoryRestoreClick); | 1007 | $('#memory_restore').off('click').on('click', onMemoryRestoreClick); |
| 1030 | $('#memory_contents').off('click').on('input', onMemoryContentInput); | 1008 | $('#memory_contents').off('input').on('input', onMemoryContentInput); |
| 1031 | $('#memory_frozen').off('click').on('input', onMemoryFrozenInput); | 1009 | $('#memory_frozen').off('input').on('input', onMemoryFrozenInput); |
| 1032 | $('#memory_skipWIAN').off('click').on('input', onMemorySkipWIANInput); | 1010 | $('#memory_skipWIAN').off('input').on('input', onMemorySkipWIANInput); |
| 1033 | $('#summary_source').off('click').on('change', onSummarySourceChange); | 1011 | $('#summary_source').off('change').on('change', onSummarySourceChange); |
| 1034 | $('#memory_prompt_words').off('click').on('input', onMemoryPromptWordsInput); | 1012 | $('#memory_prompt_words').off('input').on('input', onMemoryPromptWordsInput); |
| 1035 | $('#memory_prompt_interval').off('click').on('input', onMemoryPromptIntervalInput); | 1013 | $('#memory_prompt_interval').off('input').on('input', onMemoryPromptIntervalInput); |
| 1036 | $('#memory_prompt').off('click').on('input', onMemoryPromptInput); | 1014 | $('#memory_prompt').off('input').on('input', onMemoryPromptInput); |
| 1037 | $('#memory_force_summarize').off('click').on('click', () => forceSummarizeChat(false)); | 1015 | $('#memory_force_summarize').off('click').on('click', () => forceSummarizeChat(false)); |
| 1038 | $('#memory_template').off('click').on('input', onMemoryTemplateInput); | 1016 | $('#memory_template').off('input').on('input', onMemoryTemplateInput); |
| 1039 | $('#memory_depth').off('click').on('input', onMemoryDepthInput); | 1017 | $('#memory_depth').off('input').on('input', onMemoryDepthInput); |
| 1040 | $('#memory_role').off('click').on('input', onMemoryRoleInput); | 1018 | $('#memory_role').off('input').on('input', onMemoryRoleInput); |
| 1041 | $('input[name="memory_position"]').off('click').on('change', onMemoryPositionChange); | 1019 | $('input[name="memory_position"]').off('change').on('change', onMemoryPositionChange); |
| 1042 | $('#memory_prompt_words_force').off('click').on('input', onMemoryPromptWordsForceInput); | 1020 | $('#memory_prompt_words_force').off('input').on('input', onMemoryPromptWordsForceInput); |
| 1043 | $('#memory_prompt_builder_default').off('click').on('input', onMemoryPromptBuilderInput); | 1021 | $('#memory_prompt_builder_default').off('input').on('input', onMemoryPromptBuilderInput); |
| 1044 | $('#memory_prompt_builder_raw_blocking').off('click').on('input', onMemoryPromptBuilderInput); | 1022 | $('#memory_prompt_builder_raw_blocking').off('input').on('input', onMemoryPromptBuilderInput); |
| 1045 | $('#memory_prompt_builder_raw_non_blocking').off('click').on('input', onMemoryPromptBuilderInput); | 1023 | $('#memory_prompt_builder_raw_non_blocking').off('input').on('input', onMemoryPromptBuilderInput); |
| 1046 | $('#memory_prompt_restore').off('click').on('click', onMemoryPromptRestoreClick); | 1024 | $('#memory_prompt_restore').off('click').on('click', onMemoryPromptRestoreClick); |
| 1047 | $('#memory_prompt_interval_auto').off('click').on('click', onPromptIntervalAutoClick); | 1025 | $('#memory_prompt_interval_auto').off('click').on('click', onPromptIntervalAutoClick); |
| 1048 | $('#memory_prompt_words_auto').off('click').on('click', onPromptForceWordsAutoClick); | 1026 | $('#memory_prompt_words_auto').off('click').on('click', onPromptForceWordsAutoClick); |
| 1049 | $('#memory_override_response_length').off('click').on('input', onOverrideResponseLengthInput); | 1027 | $('#memory_override_response_length').off('input').on('input', onOverrideResponseLengthInput); |
| 1050 | $('#memory_max_messages_per_request').off('click').on('input', onMaxMessagesPerRequestInput); | 1028 | $('#memory_max_messages_per_request').off('input').on('input', onMaxMessagesPerRequestInput); |
| 1051 | $('#memory_include_wi_scan').off('input').on('input', onMemoryIncludeWIScanInput); | 1029 | $('#memory_include_wi_scan').off('input').on('input', onMemoryIncludeWIScanInput); |
| 1052 | $('#summarySettingsBlockToggle').off('click').on('click', function () { | 1030 | $('#summarySettingsBlockToggle').off('click').on('click', function () { |
| 1053 | console.log('saw settings button click'); | 1031 | $('#summarySettingsBlock').slideToggle(200, 'swing'); |
| 1054 | $('#summarySettingsBlock').slideToggle(200, 'swing'); //toggleClass("hidden"); | ||
| 1055 | }); | 1032 | }); |
| 1056 | } | 1033 | } |
| 1057 | 1034 | ||
| @@ -1068,11 +1045,11 @@ jQuery(async function () { | |||
| 1068 | 1045 | ||
| 1069 | await addExtensionControls(); | 1046 | await addExtensionControls(); |
| 1070 | loadSettings(); | 1047 | loadSettings(); |
| 1048 | eventSource.on(event_types.CHAT_CHANGED, onChatChanged); | ||
| 1071 | eventSource.makeLast(event_types.CHARACTER_MESSAGE_RENDERED, onChatEvent); | 1049 | eventSource.makeLast(event_types.CHARACTER_MESSAGE_RENDERED, onChatEvent); |
| 1072 | eventSource.on(event_types.MESSAGE_DELETED, onChatEvent); | 1050 | for (const event of [event_types.MESSAGE_DELETED, event_types.MESSAGE_UPDATED, event_types.MESSAGE_SWIPED]) { |
| 1073 | eventSource.on(event_types.MESSAGE_EDITED, onChatEvent); | 1051 | eventSource.on(event, onChatEvent); |
| 1074 | eventSource.on(event_types.MESSAGE_SWIPED, onChatEvent); | 1052 | } |
| 1075 | eventSource.on(event_types.CHAT_CHANGED, onChatEvent); | ||
| 1076 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ | 1053 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ |
| 1077 | name: 'summarize', | 1054 | name: 'summarize', |
| 1078 | callback: summarizeCallback, | 1055 | callback: summarizeCallback, |
| @@ -10,16 +10,18 @@ | |||
| 10 | <div class="inline-drawer-content"> | 10 | <div class="inline-drawer-content"> |
| 11 | <div id="summaryExtensionDrawerContents"> | 11 | <div id="summaryExtensionDrawerContents"> |
| 12 | <label for="summary_source" data-i18n="ext_sum_with">Summarize with:</label> | 12 | <label for="summary_source" data-i18n="ext_sum_with">Summarize with:</label> |
| 13 | <select id="summary_source"> | 13 | <select id="summary_source" class="text_pole"> |
| 14 | <option value="main" data-i18n="ext_sum_main_api">Main API</option> | 14 | <option value="main" data-i18n="ext_sum_main_api">Main API</option> |
| 15 | <option value="extras">Extras API (deprecated)</option> | 15 | <option value="extras">Extras API (deprecated)</option> |
| 16 | <option value="webllm" data-i18n="ext_sum_webllm">WebLLM Extension</option> | 16 | <option value="webllm" data-i18n="ext_sum_webllm">WebLLM Extension</option> |
| 17 | </select><br> | 17 | </select><br> |
| 18 | 18 | ||
| 19 | <div class="flex-container justifyspacebetween alignitemscenter"> | 19 | <div class="flex-container justifyspacebetween alignitemscenter"> |
| 20 | <span class="flex1" data-i18n="ext_sum_current_summary">Current summary:</span> | 20 | <span data-i18n="ext_sum_current_summary">Current summary:</span> |
| 21 | <div id="memory_restore" class="menu_button flex1 margin0" data-i18n="[title]ext_sum_restore_tip" title="Restore a previous summary; use repeatedly to clear summarization state for this chat."> | 21 | <i class="editor_maximize fa-solid fa-maximize right_menu_button" data-for="memory_contents" title="Expand the editor" data-i18n="[title]Expand the editor"></i> |
| 22 | <span data-i18n="ext_sum_restore_previous">Restore Previous</span> | 22 | <span class="flex1"> </span> |
| 23 | <div id="memory_restore" class="menu_button margin0" data-i18n="[title]ext_sum_restore_tip" title="Restore a previous summary; use repeatedly to clear summarization state for this chat."> | ||
| 24 | <small data-i18n="ext_sum_restore_previous">Restore Previous</small> | ||
| 23 | </div> | 25 | </div> |
| 24 | </div> | 26 | </div> |
| 25 | 27 | ||
| @@ -3,9 +3,13 @@ | |||
| 3 | flex-direction: column; | 3 | flex-direction: column; |
| 4 | } | 4 | } |
| 5 | 5 | ||
| 6 | #memory_settings textarea { | 6 | #memory_contents { |
| 7 | font-size: calc(var(--mainFontSize) * 0.9); | 7 | field-sizing: content; |
| 8 | line-height: 1.2; | 8 | max-height: 50dvh; |
| 9 | } | ||
| 10 | |||
| 11 | #memory_restore { | ||
| 12 | width: max-content; | ||
| 9 | } | 13 | } |
| 10 | 14 | ||
| 11 | label[for="memory_frozen"], | 15 | label[for="memory_frozen"], |
| @@ -35,3 +39,9 @@ label[for="memory_frozen"] input { | |||
| 35 | flex-direction: column; | 39 | flex-direction: column; |
| 36 | row-gap: 5px; | 40 | row-gap: 5px; |
| 37 | } | 41 | } |
| 42 | |||
| 43 | #summaryExtensionPopout { | ||
| 44 | display: flex; | ||
| 45 | flex-direction: column; | ||
| 46 | padding-top: 25px; | ||
| 47 | } | ||