From 61b645f5ff44f892d2669729e95ce7aecaaeeff5 Mon Sep 17 00:00:00 2001 From: maver Date: Mon, 14 Aug 2023 23:19:39 +0200 Subject: [PATCH 1/4] Fix responsive popup not taking all available width on desktop --- public/css/promptmanager.css | 1 - 1 file changed, 1 deletion(-) diff --git a/public/css/promptmanager.css b/public/css/promptmanager.css index a964e9fd6..74dac59f3 100644 --- a/public/css/promptmanager.css +++ b/public/css/promptmanager.css @@ -250,7 +250,6 @@ } #completion_prompt_manager_popup { - max-width: var(--sheldWidth); overflow-y: auto; height: calc(100% - 40px); position: absolute; From 8f830a09282f920e9f83e9815b59c228651fa33a Mon Sep 17 00:00:00 2001 From: maver Date: Mon, 14 Aug 2023 23:36:06 +0200 Subject: [PATCH 2/4] Persist settings before applying preset --- public/scripts/PromptManager.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/public/scripts/PromptManager.js b/public/scripts/PromptManager.js index 2c3da56aa..25b30b2fb 100644 --- a/public/scripts/PromptManager.js +++ b/public/scripts/PromptManager.js @@ -571,12 +571,13 @@ PromptManagerModule.prototype.init = function (moduleConfiguration, serviceSetti document.getElementById(this.configuration.prefix + 'prompt_manager_popup_close_button').addEventListener('click', closeAndClearPopup); // Re-render prompt manager on openai preset change - eventSource.on(event_types.OAI_PRESET_CHANGED, settings => this.renderDebounced()); - - // Close popup on preset change - eventSource.on(event_types.OAI_PRESET_CHANGED, () => { - this.hidePopup(); - this.clearEditForm(); + eventSource.on(event_types.OAI_PRESET_CHANGED, settings => { + // Save configuration and wrap everything up. + this.saveServiceSettings().then(() => { + this.hidePopup(); + this.clearEditForm(); + this.renderDebounced() + }); }); // Re-render prompt manager on world settings update From 2bb8fa5ea176ad71c3e2b23cd52938847ec41abd Mon Sep 17 00:00:00 2001 From: maver Date: Tue, 15 Aug 2023 01:20:42 +0200 Subject: [PATCH 3/4] Update quick edit textareas when prompt manager re-renders --- public/index.html | 19 +----- public/scripts/PromptManager.js | 106 +++++++++++++++++++------------- public/scripts/openai.js | 5 -- 3 files changed, 65 insertions(+), 65 deletions(-) diff --git a/public/index.html b/public/index.html index ef2334182..b5bf83ec3 100644 --- a/public/index.html +++ b/public/index.html @@ -1350,22 +1350,9 @@
-
-
Main
-
- -
-
-
-
NSFW
-
- -
-
-
-
Jailbreak
-
- +
+
+ Select a character to show quick edit options.
diff --git a/public/scripts/PromptManager.js b/public/scripts/PromptManager.js index 25b30b2fb..e763a3952 100644 --- a/public/scripts/PromptManager.js +++ b/public/scripts/PromptManager.js @@ -1,8 +1,23 @@ -import {callPopup, event_types, eventSource, is_send_press, main_api, saveSettingsDebounced, substituteParams} from "../script.js"; +import {callPopup, event_types, eventSource, is_send_press, main_api, substituteParams} from "../script.js"; import {TokenHandler} from "./openai.js"; import {power_user} from "./power-user.js"; import { debounce, waitUntilCondition } from "./utils.js"; +function debouncePromise(func, delay) { + let timeoutId; + + return (...args) => { + clearTimeout(timeoutId); + + return new Promise((resolve) => { + timeoutId = setTimeout(() => { + const result = func(...args); + resolve(result); + }, delay); + }); + }; +} + /** * Register migrations for the prompt manager when settings are loaded or an Open AI preset is loaded. */ @@ -158,11 +173,6 @@ function PromptManagerModule() { nsfw: '', jailbreak: '', enhanceDefinitions: '' - }, - quickEdit: { - main: '', - nsfw: '', - jailbreak: '' } }; @@ -307,41 +317,6 @@ PromptManagerModule.prototype.init = function (moduleConfiguration, serviceSetti this.saveServiceSettings().then(() => this.render()); }; - // Factory function for creating quick edit elements - const createQuickEdit = function() { - return { - element: null, - prompt: null, - - from(element, prompt) { - this.element = element; - element.value = prompt.content ?? ''; - element.addEventListener('input', () => { - prompt.content = element.value; - saveSettingsDebounced() - }); - - return this; - }, - - update(value) { - this.element.value = value; - } - } - } - - const mainPrompt = this.getPromptById('main'); - const mainPromptTextarea = document.getElementById(this.configuration.quickEdit.main); - const mainQuickEdit = createQuickEdit().from(mainPromptTextarea, mainPrompt); - - const nsfwPrompt = this.getPromptById('nsfw'); - const nsfwPromptTextarea = document.getElementById(this.configuration.quickEdit.nsfw); - const nsfwQuickEdit = createQuickEdit().from(nsfwPromptTextarea, nsfwPrompt); - - const jailbreakPrompt = this.getPromptById('jailbreak'); - const jailbreakPromptTextarea = document.getElementById(this.configuration.quickEdit.jailbreak); - const jailbreakQuickEdit = createQuickEdit().from(jailbreakPromptTextarea, jailbreakPrompt); - // Save prompt edit form to settings and close form. this.handleSavePrompt = (event) => { const promptId = event.target.dataset.pmPrompt; @@ -355,9 +330,9 @@ PromptManagerModule.prototype.init = function (moduleConfiguration, serviceSetti this.updatePromptWithPromptEditForm(prompt); } - if ('main' === promptId) mainQuickEdit.update(prompt.content); - if ('nsfw' === promptId) nsfwQuickEdit.update(prompt.content); - if ('jailbreak' === promptId) jailbreakQuickEdit.update(prompt.content); + if ('main' === promptId) this.updateQuickEdit('main', prompt); + if ('nsfw' === promptId) this.updateQuickEdit('nsfw', prompt); + if ('jailbreak' === promptId) this.updateQuickEdit('jailbreak', prompt); this.log('Saved prompt: ' + promptId); @@ -952,6 +927,42 @@ PromptManagerModule.prototype.preparePrompt = function (prompt, original = null) return preparedPrompt; } +/** + * Factory function for creating a QuickEdit object associated with a prompt element. + * + * The QuickEdit object provides methods to synchronize an input element's value with a prompt's content + * and handle input events to update the prompt content. + * + */ +PromptManagerModule.prototype.createQuickEdit = function(identifier, title) { + const prompt = this.getPromptById(identifier); + const textareaIdentifier = `${identifier}_prompt_quick_edit_textarea`; + const html = `
+
${title}
+
+ +
+
`; + + const quickEditContainer = document.getElementById('quick-edit-container'); + quickEditContainer.insertAdjacentHTML('afterbegin', html); + + const debouncedSaveServiceSettings = debouncePromise(() => this.saveServiceSettings(), 300); + + const textarea = document.getElementById(textareaIdentifier); + textarea.addEventListener('blur', () => { + prompt.content = textarea.value; + this.updatePromptByIdentifier(identifier, prompt); + debouncedSaveServiceSettings().then(() => this.render()); + }); + +} + +PromptManagerModule.prototype.updateQuickEdit = function(identifier, prompt) { + const textarea = document.getElementById(`${identifier}_prompt_quick_edit_textarea`); + textarea.value = prompt.content; +} + /** * Checks if a given name is accepted by OpenAi API * @link https://platform.openai.com/docs/api-reference/chat/create @@ -1241,6 +1252,13 @@ PromptManagerModule.prototype.renderPromptManager = function () { footerDiv.querySelector('#prompt-manager-export').addEventListener('click', showExportSelection); rangeBlockDiv.querySelector('.export-promptmanager-prompts-full').addEventListener('click', this.handleFullExport); rangeBlockDiv.querySelector('.export-promptmanager-prompts-character').addEventListener('click', this.handleCharacterExport); + + const quickEditContainer = document.getElementById('quick-edit-container'); + quickEditContainer.innerHTML = ''; + + this.createQuickEdit('jailbreak', 'Jailbreak'); + this.createQuickEdit('nsfw', 'NSFW'); + this.createQuickEdit('main', 'Main'); } }; diff --git a/public/scripts/openai.js b/public/scripts/openai.js index fb3f6a580..db8f8c028 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -347,11 +347,6 @@ function setupChatCompletionPromptManager(openAiSettings) { nsfw: default_nsfw_prompt, jailbreak: default_jailbreak_prompt, enhanceDefinitions: default_enhance_definitions_prompt - }, - quickEdit: { - main: 'main_prompt_quick_edit_textarea', - nsfw: 'nsfw_prompt_quick_edit_textarea', - jailbreak: 'jailbreak_prompt_quick_edit_textarea' } }; From 9fb38af838f34f95c44046fa5ef3436abfb40107 Mon Sep 17 00:00:00 2001 From: maver Date: Tue, 15 Aug 2023 01:28:07 +0200 Subject: [PATCH 4/4] Fix defaults getting applied when switching back to Default preset --- public/scripts/PromptManager.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/public/scripts/PromptManager.js b/public/scripts/PromptManager.js index e763a3952..33753b0a7 100644 --- a/public/scripts/PromptManager.js +++ b/public/scripts/PromptManager.js @@ -23,6 +23,8 @@ function debouncePromise(func, delay) { */ const registerPromptManagerMigration = () => { const migrate = (settings, savePreset = null, presetName = null) => { + if ('Default' === presetName) return; + if (settings.main_prompt || settings.nsfw_prompt || settings.jailbreak_prompt) { console.log('Running prompt manager configuration migration'); if (settings.prompts === undefined || settings.prompts.length === 0) settings.prompts = structuredClone(chatCompletionDefaultPrompts.prompts);