Assorted summary improvements

7a54b0cad76210d80ed877e40dedfee86d44c0c3

Cohee <18619528+Cohee1207@users.noreply.github.com>

3 files changed, +59 -65Ignore whitespace
public/scripts/extensions/memory/index.js+38 -61
@@ -32,9 +32,6 @@ export { MODULE_NAME };
3232
3333const MODULE_NAME = '1_memory';
3434
35-let lastCharacterId = null;
36-let lastGroupId = null;
37-let lastChatId = null;
3835let lastMessageHash = null;
3936let lastMessageId = null;
4037let inApiCall = false;
@@ -251,7 +248,7 @@ function onSummarySourceChange(event) {
251248}
252249
253250function switchSourceControls(value) {
254251 $('#summaryExtensionDrawerContents [data-summary-source], #memory_settings [data-summary-source]').each((_, element) => {
255252 const source = element.dataset.summarySource.split(',').map(s => s.trim());
256253 $(element).toggle(source.includes(value));
257254 });
@@ -349,15 +346,6 @@ function onMaxMessagesPerRequestInput() {
349346 saveSettingsDebounced();
350347}
351348
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-
361349function getLatestMemoryFromChat(chat) {
362350 if (!Array.isArray(chat) || !chat.length) {
363351 return '';
@@ -390,6 +378,12 @@ function getIndexOfLatestChatSummary(chat) {
390378 return -1;
391379}
392380
381+function onChatChanged() {
382+ const context = getContext();
383+ const latestMemory = getLatestMemoryFromChat(context.chat);
384+ setMemoryContext(latestMemory, false);
385+}
386+
393387async function onChatEvent() {
394388 // Module not enabled
395389 if (extension_settings.memory.source === summary_sources.extras && !modules.includes('summarize')) {
@@ -401,32 +395,19 @@ async function onChatEvent() {
401395 return;
402396 }
403397
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-
412398 // Streaming in-progress
413399 if (streamingProcessor && !streamingProcessor.isFinished) {
414400 return;
415401 }
416402
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-
425403 // Currently summarizing or frozen state - skip
426404 if (inApiCall || extension_settings.memory.memoryFrozen) {
427405 return;
428406 }
429407
408+ const context = getContext();
409+ const chat = context.chat;
410+
430411 // No new messages - do nothing
431412 if (chat.length === 0 || (lastMessageId === chat.length && getStringHash(chat[chat.length - 1].mes) === lastMessageHash)) {
432413 return;
@@ -449,7 +430,10 @@ async function onChatEvent() {
449430
450431 summarizeChat(context)
451432 .catch(console.error)
452433 .finally(saveLastValues(); => {
434+ lastMessageId = context.chat?.length ?? null;
435+ lastMessageHash = getStringHash((context.chat.length && context.chat[context.chat.length - 1]['mes']) ?? '');
436+ });
453437}
454438
455439/**
@@ -464,13 +448,7 @@ async function forceSummarizeChat(quiet) {
464448 }
465449
466450 const context = getContext();
467-
468451 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- }
474452
475453 const toast = quiet ? jQuery() : toastr.info('Summarizing chat...', 'Please wait', { timeOut: 0, extendedTimeOut: 0 });
476454 const value = extension_settings.memory.source === summary_sources.main
@@ -993,7 +971,7 @@ function doPopout(e) {
993971 .removeClass('zoomed_avatar')
994972 .addClass('draggable')
995973 .empty();
996974 const prevSummaryBoxContents = $('#memory_contents').val().toString(); //copy summary box before emptying
997975 originalElement.empty();
998976 originalElement.html('<div class="flex-container alignitemscenter justifyCenter wide100p"><small>Currently popped out</small></div>');
999977 newElement.append(controlBarHtml).append(originalHTMLClone);
@@ -1013,7 +991,7 @@ function doPopout(e) {
1013991 const summaryPopoutHTML = $('#summaryExtensionDrawerContents');
1014992 $('#summaryExtensionPopout').fadeOut(animation_duration, () => {
1015993 originalElement.empty();
1016994 originalElement.htmlappend(summaryPopoutHTML);
1017995 $('#summaryExtensionPopout').remove();
1018996 });
1019997 loadSettings();
@@ -1027,31 +1005,30 @@ function doPopout(e) {
10271005function setupListeners() {
10281006 //setup shared listeners for popout and regular ext menu
10291007 $('#memory_restore').off('click').on('click', onMemoryRestoreClick);
10301008 $('#memory_contents').off('clickinput').on('input', onMemoryContentInput);
10311009 $('#memory_frozen').off('clickinput').on('input', onMemoryFrozenInput);
10321010 $('#memory_skipWIAN').off('clickinput').on('input', onMemorySkipWIANInput);
10331011 $('#summary_source').off('clickchange').on('change', onSummarySourceChange);
10341012 $('#memory_prompt_words').off('clickinput').on('input', onMemoryPromptWordsInput);
10351013 $('#memory_prompt_interval').off('clickinput').on('input', onMemoryPromptIntervalInput);
10361014 $('#memory_prompt').off('clickinput').on('input', onMemoryPromptInput);
10371015 $('#memory_force_summarize').off('click').on('click', () => forceSummarizeChat(false));
10381016 $('#memory_template').off('clickinput').on('input', onMemoryTemplateInput);
10391017 $('#memory_depth').off('clickinput').on('input', onMemoryDepthInput);
10401018 $('#memory_role').off('clickinput').on('input', onMemoryRoleInput);
10411019 $('input[name="memory_position"]').off('clickchange').on('change', onMemoryPositionChange);
10421020 $('#memory_prompt_words_force').off('clickinput').on('input', onMemoryPromptWordsForceInput);
10431021 $('#memory_prompt_builder_default').off('clickinput').on('input', onMemoryPromptBuilderInput);
10441022 $('#memory_prompt_builder_raw_blocking').off('clickinput').on('input', onMemoryPromptBuilderInput);
10451023 $('#memory_prompt_builder_raw_non_blocking').off('clickinput').on('input', onMemoryPromptBuilderInput);
10461024 $('#memory_prompt_restore').off('click').on('click', onMemoryPromptRestoreClick);
10471025 $('#memory_prompt_interval_auto').off('click').on('click', onPromptIntervalAutoClick);
10481026 $('#memory_prompt_words_auto').off('click').on('click', onPromptForceWordsAutoClick);
10491027 $('#memory_override_response_length').off('clickinput').on('input', onOverrideResponseLengthInput);
10501028 $('#memory_max_messages_per_request').off('clickinput').on('input', onMaxMessagesPerRequestInput);
10511029 $('#memory_include_wi_scan').off('input').on('input', onMemoryIncludeWIScanInput);
10521030 $('#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");
10551032 });
10561033}
10571034
@@ -1068,11 +1045,11 @@ jQuery(async function () {
10681045
10691046 await addExtensionControls();
10701047 loadSettings();
1048+ eventSource.on(event_types.CHAT_CHANGED, onChatChanged);
10711049 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]) {
10731051 eventSource.on(event_types.MESSAGE_EDITEDevent, onChatEvent);
1074- eventSource.on(event_types.MESSAGE_SWIPED, onChatEvent);
1052+ }
1075- eventSource.on(event_types.CHAT_CHANGED, onChatEvent);
10761053 SlashCommandParser.addCommandObject(SlashCommand.fromProps({
10771054 name: 'summarize',
10781055 callback: summarizeCallback,
public/scripts/extensions/memory/settings.html+6 -4
@@ -10,16 +10,18 @@
1010 <div class="inline-drawer-content">
1111 <div id="summaryExtensionDrawerContents">
1212 <label for="summary_source" data-i18n="ext_sum_with">Summarize with:</label>
1313 <select id="summary_source" class="text_pole">
1414 <option value="main" data-i18n="ext_sum_main_api">Main API</option>
1515 <option value="extras">Extras API (deprecated)</option>
1616 <option value="webllm" data-i18n="ext_sum_webllm">WebLLM Extension</option>
1717 </select><br>
1818
1919 <div class="flex-container justifyspacebetween alignitemscenter">
2020 <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>
2222 <span data-i18nclass="ext_sum_restore_previousflex1">Restore Previous&nbsp;</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>
2325 </div>
2426 </div>
2527
public/scripts/extensions/memory/style.css+15 -0
@@ -3,6 +3,15 @@
33 flex-direction: column;
44}
55
6+#memory_contents {
7+ field-sizing: content;
8+ max-height: 50dvh;
9+}
10+
11+#memory_restore {
12+ width: max-content;
13+}
14+
615#memory_settings textarea {
716 font-size: calc(var(--mainFontSize) * 0.9);
817 line-height: 1.2;
@@ -35,3 +44,9 @@ label[for="memory_frozen"] input {
3544 flex-direction: column;
3645 row-gap: 5px;
3746}
47+
48+#summaryExtensionPopout {
49+ display: flex;
50+ flex-direction: column;
51+ padding-top: 25px;
52+}