From 632fa770be0590f62f0bb1e9b1252bf381fbb98f Mon Sep 17 00:00:00 2001 From: maver Date: Sun, 2 Jul 2023 21:50:37 +0200 Subject: [PATCH] Create a deep copy of prompt defaults on char select Along other minor fixes and optimizations --- public/css/promptmanager.css | 1 + public/index.html | 8 +++--- public/scripts/PromptManager.js | 46 ++++++++++++--------------------- public/scripts/openai.js | 4 +-- 4 files changed, 24 insertions(+), 35 deletions(-) diff --git a/public/css/promptmanager.css b/public/css/promptmanager.css index 85620c36f..c2b007b1c 100644 --- a/public/css/promptmanager.css +++ b/public/css/promptmanager.css @@ -96,6 +96,7 @@ #completion_prompt_manager_popup #completion_prompt_manager_popup_entry_form_inspect_list { margin-top: 1em; + padding: 1em; } #completion_prompt_manager_popup .completion_prompt_manager_prompt { diff --git a/public/index.html b/public/index.html index 90694e389..b0dae97f7 100644 --- a/public/index.html +++ b/public/index.html @@ -1282,11 +1282,11 @@
-
@@ -3596,7 +3596,7 @@
The list of prompts associated with this marker.
-
+
diff --git a/public/scripts/PromptManager.js b/public/scripts/PromptManager.js index 6a3d3f096..616b87e18 100644 --- a/public/scripts/PromptManager.js +++ b/public/scripts/PromptManager.js @@ -64,46 +64,39 @@ class Prompt { class PromptCollection { collection = []; + constructor(...prompts) { + this.add(...prompts); + } + + checkPromptInstance(...prompts) { for(let prompt of prompts) { if(!(prompt instanceof Prompt)) { throw new Error('Only Prompt instances can be added to PromptCollection'); } } - - this.collection.push(...prompts); } add(...prompts) { - for(let prompt of prompts) { - if(!(prompt instanceof Prompt)) { - throw new Error('Only Prompt instances can be added to PromptCollection'); - } - } - + this.checkPromptInstance(...prompts); this.collection.push(...prompts); } set(prompt, position) { - if(!(prompt instanceof Prompt)) { - throw new Error('Only Prompt instances can be added to PromptCollection'); - } - + this.checkPromptInstance(prompt); this.collection[position] = prompt; } get(identifier) { - const index = this.index(identifier); - if (0 > index) return null; - return this.collection[index]; + return this.collection.find(prompt => prompt.identifier === identifier); } - index (identifier){ + index(identifier) { return this.collection.findIndex(prompt => prompt.identifier === identifier); } has(identifier) { - return this.collection.some(message => message.identifier === identifier); + return this.index(identifier) !== -1; } } @@ -520,12 +513,12 @@ PromptManagerModule.prototype.checkForMissingPrompts = function(prompts) { }; /** - * Check whether a prompt is a marker. + * Check whether a prompt can be inspected. * @param {object} prompt - The prompt to check. * @returns {boolean} True if the prompt is a marker, false otherwise. */ -PromptManagerModule.prototype.isStPromptMarker = function (prompt) { - return true === prompt.marker; +PromptManagerModule.prototype.isPromptInspectionAllowed = function (prompt) { + return true === prompt.marker; } /** @@ -576,10 +569,7 @@ PromptManagerModule.prototype.handleCharacterSelected = function (event) { // ToDo: These should be passed as parameter or attached to the manager as a set of default options. // Set default prompts and order for character. - if (0 === promptList.length) this.addPromptListForCharacter(this.activeCharacter, openAiDefaultPromptList) - // Check whether the referenced prompts are present. - if (0 === this.serviceSettings.prompts.length) this.setPrompts(openAiDefaultPrompts.prompts); - + if (0 === promptList.length) this.addPromptListForCharacter(this.activeCharacter, openAiDefaultPromptList); } PromptManagerModule.prototype.handleGroupSelected = function (event) { @@ -588,8 +578,6 @@ PromptManagerModule.prototype.handleGroupSelected = function (event) { const promptList = this.getPromptListByCharacter(characterDummy); if (0 === promptList.length) this.addPromptListForCharacter(characterDummy, openAiDefaultPromptList) - if (0 === this.serviceSettings.prompts.length) this.setPrompts(openAiDefaultPrompts.prompts); - } PromptManagerModule.prototype.getActiveGroupCharacters = function() { @@ -645,7 +633,7 @@ PromptManagerModule.prototype.removePromptListForCharacter = function (character PromptManagerModule.prototype.addPromptListForCharacter = function (character, promptList) { this.serviceSettings.prompt_lists.push({ character_id: character.id, - list: promptList + list: JSON.parse(JSON.stringify(promptList)) }); } @@ -775,7 +763,7 @@ PromptManagerModule.prototype.loadMessagesIntoInspectForm = function (messages) messages.getCollection().forEach(message => { const truncatedTitle = message.content.length > 32 ? message.content.slice(0, 32) + '...' : message.content; - messageList.append(createInlineDrawer(message.title || truncatedTitle, message.content || 'No Content')); + messageList.append(createInlineDrawer(message.identifier || truncatedTitle, message.content || 'No Content')); }); } @@ -981,7 +969,7 @@ PromptManagerModule.prototype.renderPromptManagerListItems = function () { } let inspectSpanHtml = ''; - if (this.isStPromptMarker(prompt)) { + if (this.isPromptInspectionAllowed(prompt)) { inspectSpanHtml = ` `; diff --git a/public/scripts/openai.js b/public/scripts/openai.js index 4aaa3ff32..05b96cf47 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -454,7 +454,7 @@ function populateDialogueExamples(prompts, chatCompletion) { /** * Populate a chat conversation by adding prompts to the conversation and managing system and user prompts. * - * @param {Map} prompts - Map object containing all prompts where the key is the prompt identifier and the value is the prompt object. + * @param {PromptCollection} prompts - PromptCollection containing all prompts where the key is the prompt identifier and the value is the prompt object. * @param {ChatCompletion} chatCompletion - An instance of ChatCompletion class that will be populated with the prompts. * @param {Object} options - An object with optional settings. * @param {string} options.bias - A bias to be added in the conversation. @@ -496,7 +496,7 @@ function populateChatCompletion (prompts, chatCompletion, {bias, quietPrompt, ty [...systemPrompts, ...userPrompts].forEach(identifier => addToChatCompletion(identifier)); // Add enhance definition instruction - if (prompts.has('scenario')) addToChatCompletion('enhanceDefinitions'); + if (prompts.has('enhanceDefinitions')) addToChatCompletion('enhanceDefinitions'); // Insert nsfw avoidance prompt into main, if no nsfw prompt is present if (false === chatCompletion.has('nsfw') && oai_settings.nsfw_avoidance_prompt)