From cc01ee6c1c6055c867c2923546e47a0afab5f94f Mon Sep 17 00:00:00 2001 From: maver Date: Tue, 15 Aug 2023 19:40:22 +0200 Subject: [PATCH 1/3] Print stack to console on chatcompletion error --- public/scripts/openai.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/public/scripts/openai.js b/public/scripts/openai.js index 71b83b9f2..931cc4c38 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -806,8 +806,10 @@ function prepareOpenAIMessages({ promptManager.error = 'The name of at least one character contained whitespaces or special characters. Please check your user and character name.'; } else { toastr.error('An unknown error occurred while counting tokens. Further information may be available in console.') - chatCompletion.log('Unexpected error:'); + chatCompletion.log('----- Unexpected error while preparing prompts -----'); chatCompletion.log(error); + chatCompletion.log(error.stack); + chatCompletion.log('----------------------------------------------------'); } } finally { // Pass chat completion to prompt manager for inspection From b0158bd72dfe1d519bb81175d4fa18050279084f Mon Sep 17 00:00:00 2001 From: maver Date: Tue, 15 Aug 2023 19:41:34 +0200 Subject: [PATCH 2/3] Implement configurable prompt order strategy for prompt manager global and character --- public/scripts/PromptManager.js | 102 +++++++++++++++++++++++++------- public/scripts/openai.js | 6 +- 2 files changed, 85 insertions(+), 23 deletions(-) diff --git a/public/scripts/PromptManager.js b/public/scripts/PromptManager.js index cdd771358..78d095e82 100644 --- a/public/scripts/PromptManager.js +++ b/public/scripts/PromptManager.js @@ -168,6 +168,10 @@ function PromptManagerModule() { listIdentifier: '', listItemTemplateIdentifier: '', toggleDisabled: [], + promptOrder: { + strategy: 'global', + dummyId: 100000 + }, draggable: true, warningTokenThreshold: 1500, dangerTokenThreshold: 500, @@ -416,12 +420,26 @@ PromptManagerModule.prototype.init = function (moduleConfiguration, serviceSetti // Export all user prompts this.handleFullExport = () => { - const exportPrompts = this.serviceSettings.prompts.reduce((userPrompts, prompt) => { + const prompts = this.serviceSettings.prompts.reduce((userPrompts, prompt) => { if (false === prompt.system_prompt && false === prompt.marker) userPrompts.push(prompt); return userPrompts; }, []); - this.export({prompts: exportPrompts}, 'full', 'st-prompts'); + let promptOrder = []; + if ('global' === this.configuration.promptOrder.strategy) { + promptOrder = this.getPromptOrderForCharacter({id: this.configuration.promptOrder.dummyId}); + } else if ('character' === this.configuration.promptOrder.strategy) { + promptOrder = []; + } else { + throw new Error('Prompt order strategy not supported.') + } + + const exportPrompts = { + prompts: prompts, + prompt_order: promptOrder + } + + this.export(exportPrompts, 'full', 'st-prompts'); } // Export user prompts and order for this character @@ -554,7 +572,7 @@ PromptManagerModule.prototype.init = function (moduleConfiguration, serviceSetti this.saveServiceSettings().then(() => { this.hidePopup(); this.clearEditForm(); - this.renderDebounced() + this.renderDebounced(); }); }); @@ -694,6 +712,13 @@ PromptManagerModule.prototype.sanitizeServiceSettings = function () { this.serviceSettings.prompts = this.serviceSettings.prompts ?? []; this.serviceSettings.prompt_order = this.serviceSettings.prompt_order ?? []; + if ('global' === this.configuration.promptOrder.strategy) { + const dummyCharacter = {id: this.configuration.promptOrder.dummyId}; + const promptOrder = this.getPromptOrderForCharacter(dummyCharacter); + + if (0 === promptOrder.length) this.addPromptOrderForCharacter(dummyCharacter, promptManagerDefaultPromptOrder); + } + // Check whether the referenced prompts are present. this.serviceSettings.prompts.length === 0 ? this.setPrompts(chatCompletionDefaultPrompts.prompts) @@ -775,9 +800,10 @@ PromptManagerModule.prototype.isPromptToggleAllowed = function (prompt) { /** * Handle the deletion of a character by removing their prompt list and nullifying the active character if it was the one deleted. * @param {object} event - The event object containing the character's ID. - * @returns boolean + * @returns void */ PromptManagerModule.prototype.handleCharacterDeleted = function (event) { + if ('global' === this.configuration.promptOrder.strategy) return; this.removePromptOrderForCharacter(this.activeCharacter); if (this.activeCharacter.id === event.detail.id) this.activeCharacter = null; } @@ -788,12 +814,19 @@ PromptManagerModule.prototype.handleCharacterDeleted = function (event) { * @returns {void} */ PromptManagerModule.prototype.handleCharacterSelected = function (event) { - this.activeCharacter = {id: event.detail.id, ...event.detail.character}; - const promptOrder = this.getPromptOrderForCharacter(this.activeCharacter); + if ('global' === this.configuration.promptOrder.strategy) { + this.activeCharacter = {id: this.configuration.promptOrder.dummyId}; + } else if ('character' === this.configuration.promptOrder.strategy) { + console.log('FOO') + this.activeCharacter = {id: event.detail.id, ...event.detail.character}; + const promptOrder = this.getPromptOrderForCharacter(this.activeCharacter); - // 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 === promptOrder.length) this.addPromptOrderForCharacter(this.activeCharacter, promptManagerDefaultPromptOrder); + // 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 === promptOrder.length) this.addPromptOrderForCharacter(this.activeCharacter, promptManagerDefaultPromptOrder); + } else { + throw new Error('Unsupported prompt order mode.'); + } } /** @@ -802,7 +835,13 @@ PromptManagerModule.prototype.handleCharacterSelected = function (event) { * @param event */ PromptManagerModule.prototype.handleCharacterUpdated = function (event) { - this.activeCharacter = {id: event.detail.id, ...event.detail.character}; + if ('global' === this.configuration.promptOrder.strategy) { + this.activeCharacter = {id: this.configuration.promptOrder.dummyId}; + } else if ('character' === this.configuration.promptOrder.strategy) { + this.activeCharacter = {id: event.detail.id, ...event.detail.character}; + } else { + throw new Error ('Prompt order strategy not supported.') + } } /** @@ -811,11 +850,17 @@ PromptManagerModule.prototype.handleCharacterUpdated = function (event) { * @param event */ PromptManagerModule.prototype.handleGroupSelected = function (event) { - const characterDummy = {id: event.detail.id, group: event.detail.group}; - this.activeCharacter = characterDummy; - const promptOrder = this.getPromptOrderForCharacter(characterDummy); + if ('global' === this.configuration.promptOrder.strategy) { + this.activeCharacter = {id: this.configuration.promptOrder.dummyId}; + } else if ('character' === this.configuration.promptOrder.strategy) { + const characterDummy = {id: event.detail.id, group: event.detail.group}; + this.activeCharacter = characterDummy; + const promptOrder = this.getPromptOrderForCharacter(characterDummy); - if (0 === promptOrder.length) this.addPromptOrderForCharacter(characterDummy, promptManagerDefaultPromptOrder) + if (0 === promptOrder.length) this.addPromptOrderForCharacter(characterDummy, promptManagerDefaultPromptOrder) + } else { + throw new Error ('Prompt order strategy not supported.') + } } /** @@ -1225,10 +1270,14 @@ PromptManagerModule.prototype.renderPromptManager = function () { Export all - + ${ 'global' === this.configuration.promptOrder.strategy + ? '' + : `` } `; @@ -1254,7 +1303,7 @@ PromptManagerModule.prototype.renderPromptManager = function () { footerDiv.querySelector('#prompt-manager-import').addEventListener('click', this.handleImport); 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); + rangeBlockDiv.querySelector('.export-promptmanager-prompts-character')?.addEventListener('click', this.handleCharacterExport); const quickEditContainer = document.getElementById('quick-edit-container'); quickEditContainer.innerHTML = ''; @@ -1451,10 +1500,19 @@ PromptManagerModule.prototype.import = function (importData) { this.setPrompts(prompts); this.log('Prompt import succeeded'); - if ('character' === importData.type) { - const promptOrder = this.getPromptOrderForCharacter(this.activeCharacter); + let promptOrder = []; + if ('global' === this.configuration.promptOrder.strategy) { + const promptOrder = this.getPromptOrderForCharacter({id: this.configuration.promptOrder.dummyId}); Object.assign(promptOrder, importData.data.prompt_order); - this.log(`Prompt order import for character ${this.activeCharacter.name} completed`); + this.log(`Prompt order import succeeded`); + } else if ('character' === this.configuration.promptOrder.strategy) { + if ('character' === importData.type) { + const promptOrder = this.getPromptOrderForCharacter(this.activeCharacter); + Object.assign(promptOrder, importData.data.prompt_order); + this.log(`Prompt order import for character ${this.activeCharacter.name} succeeded`); + } + } else { + throw new Error('Prompt order strategy not supported.') } toastr.success('Prompt import complete.'); diff --git a/public/scripts/openai.js b/public/scripts/openai.js index 931cc4c38..8fdaf1f36 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -347,7 +347,11 @@ function setupChatCompletionPromptManager(openAiSettings) { nsfw: default_nsfw_prompt, jailbreak: default_jailbreak_prompt, enhanceDefinitions: default_enhance_definitions_prompt - } + }, + promptOrder: { + strategy: 'global', + dummyId: 100000 + }, }; promptManager.saveServiceSettings = () => { From 0084ea2461d0ceb5809dda3fa954c579c53b5ffc Mon Sep 17 00:00:00 2001 From: maver Date: Sat, 19 Aug 2023 14:59:48 +0200 Subject: [PATCH 3/3] Move sortable delay into configuration --- public/scripts/PromptManager.js | 6 +++--- public/scripts/openai.js | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/public/scripts/PromptManager.js b/public/scripts/PromptManager.js index 78d095e82..29d71e28e 100644 --- a/public/scripts/PromptManager.js +++ b/public/scripts/PromptManager.js @@ -2,7 +2,7 @@ import {callPopup, event_types, eventSource, is_send_press, main_api, substitute import { is_group_generating } from "./group-chats.js"; import {TokenHandler} from "./openai.js"; import {power_user} from "./power-user.js"; -import { debounce, getSortableDelay, waitUntilCondition } from "./utils.js"; +import { debounce, waitUntilCondition } from "./utils.js"; function debouncePromise(func, delay) { let timeoutId; @@ -172,7 +172,7 @@ function PromptManagerModule() { strategy: 'global', dummyId: 100000 }, - draggable: true, + sortableDelay: 30, warningTokenThreshold: 1500, dangerTokenThreshold: 500, defaultPrompts: { @@ -1569,7 +1569,7 @@ PromptManagerModule.prototype.getFormattedDate = function() { */ PromptManagerModule.prototype.makeDraggable = function () { $(`#${this.configuration.prefix}prompt_manager_list`).sortable({ - delay: getSortableDelay(), + delay: this.configuration.sortableDelay, items: `.${this.configuration.prefix}prompt_manager_prompt_draggable`, update: ( event, ui ) => { const promptOrder = this.getPromptOrderForCharacter(this.activeCharacter); diff --git a/public/scripts/openai.js b/public/scripts/openai.js index 8fdaf1f36..e11aecf8a 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -47,7 +47,7 @@ import { import { delay, download, - getFileText, + getFileText, getSortableDelay, getStringHash, parseJsonFile, stringFormat, @@ -341,7 +341,7 @@ function setupChatCompletionPromptManager(openAiSettings) { containerIdentifier: 'completion_prompt_manager', listIdentifier: 'completion_prompt_manager_list', toggleDisabled: ['main'], - draggable: true, + sortableDelay: getSortableDelay(), defaultPrompts: { main: default_main_prompt, nsfw: default_nsfw_prompt,