Assorted summary improvements
| @@ -32,9 +32,6 @@ export { MODULE_NAME }; | ||
| 32 | 32 | |
| 33 | 33 | const MODULE_NAME = '1_memory'; |
| 34 | 34 | |
| 35 | -let lastCharacterId = null; | |
| 36 | -let lastGroupId = null; | |
| 37 | -let lastChatId = null; | |
| 38 | 35 | let lastMessageHash = null; |
| 39 | 36 | let lastMessageId = null; |
| 40 | 37 | let inApiCall = false; |
| @@ -251,7 +248,7 @@ function onSummarySourceChange(event) { | ||
| 251 | 248 | } |
| 252 | 249 | |
| 253 | 250 | function switchSourceControls(value) { |
| 254 | 251 | $('#summaryExtensionDrawerContents [data-summary-source], #memory_settings [data-summary-source]').each((_, element) => { |
| 255 | 252 | const source = element.dataset.summarySource.split(',').map(s => s.trim()); |
| 256 | 253 | $(element).toggle(source.includes(value)); |
| 257 | 254 | }); |
| @@ -349,15 +346,6 @@ function onMaxMessagesPerRequestInput() { | ||
| 349 | 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 | 349 | function getLatestMemoryFromChat(chat) { |
| 362 | 350 | if (!Array.isArray(chat) || !chat.length) { |
| 363 | 351 | return ''; |
| @@ -390,6 +378,12 @@ function getIndexOfLatestChatSummary(chat) { | ||
| 390 | 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 | 387 | async function onChatEvent() { |
| 394 | 388 | // Module not enabled |
| 395 | 389 | if (extension_settings.memory.source === summary_sources.extras && !modules.includes('summarize')) { |
| @@ -401,32 +395,19 @@ async function onChatEvent() { | ||
| 401 | 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 | 398 | // Streaming in-progress |
| 413 | 399 | if (streamingProcessor && !streamingProcessor.isFinished) { |
| 414 | 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 | 403 | // Currently summarizing or frozen state - skip |
| 426 | 404 | if (inApiCall || extension_settings.memory.memoryFrozen) { |
| 427 | 405 | return; |
| 428 | 406 | } |
| 429 | 407 | |
| 408 | + const context = getContext(); | |
| 409 | + const chat = context.chat; | |
| 410 | + | |
| 430 | 411 | // No new messages - do nothing |
| 431 | 412 | if (chat.length === 0 || (lastMessageId === chat.length && getStringHash(chat[chat.length - 1].mes) === lastMessageHash)) { |
| 432 | 413 | return; |
| @@ -449,7 +430,10 @@ async function onChatEvent() { | ||
| 449 | 430 | |
| 450 | 431 | summarizeChat(context) |
| 451 | 432 | .catch(console.error) |
| 452 | 433 | .finally(saveLastValues(); => { |
| 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 | 450 | const context = getContext(); |
| 467 | - | |
| 468 | 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 | 453 | const toast = quiet ? jQuery() : toastr.info('Summarizing chat...', 'Please wait', { timeOut: 0, extendedTimeOut: 0 }); |
| 476 | 454 | const value = extension_settings.memory.source === summary_sources.main |
| @@ -993,7 +971,7 @@ function doPopout(e) { | ||
| 993 | 971 | .removeClass('zoomed_avatar') |
| 994 | 972 | .addClass('draggable') |
| 995 | 973 | .empty(); |
| 996 | 974 | const prevSummaryBoxContents = $('#memory_contents').val().toString(); //copy summary box before emptying |
| 997 | 975 | originalElement.empty(); |
| 998 | 976 | originalElement.html('<div class="flex-container alignitemscenter justifyCenter wide100p"><small>Currently popped out</small></div>'); |
| 999 | 977 | newElement.append(controlBarHtml).append(originalHTMLClone); |
| @@ -1013,7 +991,7 @@ function doPopout(e) { | ||
| 1013 | 991 | const summaryPopoutHTML = $('#summaryExtensionDrawerContents'); |
| 1014 | 992 | $('#summaryExtensionPopout').fadeOut(animation_duration, () => { |
| 1015 | 993 | originalElement.empty(); |
| 1016 | 994 | originalElement.htmlappend(summaryPopoutHTML); |
| 1017 | 995 | $('#summaryExtensionPopout').remove(); |
| 1018 | 996 | }); |
| 1019 | 997 | loadSettings(); |
| @@ -1027,31 +1005,30 @@ function doPopout(e) { | ||
| 1027 | 1005 | function setupListeners() { |
| 1028 | 1006 | //setup shared listeners for popout and regular ext menu |
| 1029 | 1007 | $('#memory_restore').off('click').on('click', onMemoryRestoreClick); |
| 1030 | 1008 | $('#memory_contents').off('clickinput').on('input', onMemoryContentInput); |
| 1031 | 1009 | $('#memory_frozen').off('clickinput').on('input', onMemoryFrozenInput); |
| 1032 | 1010 | $('#memory_skipWIAN').off('clickinput').on('input', onMemorySkipWIANInput); |
| 1033 | 1011 | $('#summary_source').off('clickchange').on('change', onSummarySourceChange); |
| 1034 | 1012 | $('#memory_prompt_words').off('clickinput').on('input', onMemoryPromptWordsInput); |
| 1035 | 1013 | $('#memory_prompt_interval').off('clickinput').on('input', onMemoryPromptIntervalInput); |
| 1036 | 1014 | $('#memory_prompt').off('clickinput').on('input', onMemoryPromptInput); |
| 1037 | 1015 | $('#memory_force_summarize').off('click').on('click', () => forceSummarizeChat(false)); |
| 1038 | 1016 | $('#memory_template').off('clickinput').on('input', onMemoryTemplateInput); |
| 1039 | 1017 | $('#memory_depth').off('clickinput').on('input', onMemoryDepthInput); |
| 1040 | 1018 | $('#memory_role').off('clickinput').on('input', onMemoryRoleInput); |
| 1041 | 1019 | $('input[name="memory_position"]').off('clickchange').on('change', onMemoryPositionChange); |
| 1042 | 1020 | $('#memory_prompt_words_force').off('clickinput').on('input', onMemoryPromptWordsForceInput); |
| 1043 | 1021 | $('#memory_prompt_builder_default').off('clickinput').on('input', onMemoryPromptBuilderInput); |
| 1044 | 1022 | $('#memory_prompt_builder_raw_blocking').off('clickinput').on('input', onMemoryPromptBuilderInput); |
| 1045 | 1023 | $('#memory_prompt_builder_raw_non_blocking').off('clickinput').on('input', onMemoryPromptBuilderInput); |
| 1046 | 1024 | $('#memory_prompt_restore').off('click').on('click', onMemoryPromptRestoreClick); |
| 1047 | 1025 | $('#memory_prompt_interval_auto').off('click').on('click', onPromptIntervalAutoClick); |
| 1048 | 1026 | $('#memory_prompt_words_auto').off('click').on('click', onPromptForceWordsAutoClick); |
| 1049 | 1027 | $('#memory_override_response_length').off('clickinput').on('input', onOverrideResponseLengthInput); |
| 1050 | 1028 | $('#memory_max_messages_per_request').off('clickinput').on('input', onMaxMessagesPerRequestInput); |
| 1051 | 1029 | $('#memory_include_wi_scan').off('input').on('input', onMemoryIncludeWIScanInput); |
| 1052 | 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 | 1046 | await addExtensionControls(); |
| 1070 | 1047 | loadSettings(); |
| 1048 | + eventSource.on(event_types.CHAT_CHANGED, onChatChanged); | |
| 1071 | 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 | 1051 | eventSource.on(event_types.MESSAGE_EDITEDevent, onChatEvent); |
| 1074 | - eventSource.on(event_types.MESSAGE_SWIPED, onChatEvent); | |
| 1052 | + } | |
| 1075 | - eventSource.on(event_types.CHAT_CHANGED, onChatEvent); | |
| 1076 | 1053 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ |
| 1077 | 1054 | name: 'summarize', |
| 1078 | 1055 | callback: summarizeCallback, |
| @@ -10,16 +10,18 @@ | ||
| 10 | 10 | <div class="inline-drawer-content"> |
| 11 | 11 | <div id="summaryExtensionDrawerContents"> |
| 12 | 12 | <label for="summary_source" data-i18n="ext_sum_with">Summarize with:</label> |
| 13 | 13 | <select id="summary_source" class="text_pole"> |
| 14 | 14 | <option value="main" data-i18n="ext_sum_main_api">Main API</option> |
| 15 | 15 | <option value="extras">Extras API (deprecated)</option> |
| 16 | 16 | <option value="webllm" data-i18n="ext_sum_webllm">WebLLM Extension</option> |
| 17 | 17 | </select><br> |
| 18 | 18 | |
| 19 | 19 | <div class="flex-container justifyspacebetween alignitemscenter"> |
| 20 | 20 | <span class="flex1" 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 | 22 | <span data-i18nclass="ext_sum_restore_previousflex1">Restore Previous </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 | 25 | </div> |
| 24 | 26 | </div> |
| 25 | 27 | |
| @@ -3,6 +3,15 @@ | ||
| 3 | 3 | flex-direction: column; |
| 4 | 4 | } |
| 5 | 5 | |
| 6 | +#memory_contents { | |
| 7 | + field-sizing: content; | |
| 8 | + max-height: 50dvh; | |
| 9 | +} | |
| 10 | + | |
| 11 | +#memory_restore { | |
| 12 | + width: max-content; | |
| 13 | +} | |
| 14 | + | |
| 6 | 15 | #memory_settings textarea { |
| 7 | 16 | font-size: calc(var(--mainFontSize) * 0.9); |
| 8 | 17 | line-height: 1.2; |
| @@ -35,3 +44,9 @@ label[for="memory_frozen"] input { | ||
| 35 | 44 | flex-direction: column; |
| 36 | 45 | row-gap: 5px; |
| 37 | 46 | } |
| 47 | + | |
| 48 | +#summaryExtensionPopout { | |
| 49 | + display: flex; | |
| 50 | + flex-direction: column; | |
| 51 | + padding-top: 25px; | |
| 52 | +} | |