diff --git a/public/script.js b/public/script.js index 4eaafa840..02626d6ae 100644 --- a/public/script.js +++ b/public/script.js @@ -279,7 +279,8 @@ export const event_types = { CHATCOMPLETION_SOURCE_CHANGED: 'chatcompletion_source_changed', CHATCOMPLETION_MODEL_CHANGED: 'chatcompletion_model_changed', OAI_BEFORE_CHATCOMPLETION: 'oai_before_chatcompletion', - OAI_PRESET_CHANGED: 'oai_preset_changed', + OAI_PRESET_CHANGED_BEFORE: 'oai_preset_changed_before', + OAI_PRESET_CHANGED_AFTER: 'oai_preset_changed_after', WORLDINFO_SETTINGS_UPDATED: 'worldinfo_settings_updated', CHARACTER_EDITED: 'character_edited', USER_MESSAGE_RENDERED: 'user_message_rendered', diff --git a/public/scripts/PromptManager.js b/public/scripts/PromptManager.js index bc1b4624a..7c25aeee4 100644 --- a/public/scripts/PromptManager.js +++ b/public/scripts/PromptManager.js @@ -53,7 +53,7 @@ const registerPromptManagerMigration = () => { }; eventSource.on(event_types.SETTINGS_LOADED_BEFORE, settings => migrate(settings)); - eventSource.on(event_types.OAI_PRESET_CHANGED, event => migrate(event.preset, event.savePreset, event.presetName)); + eventSource.on(event_types.OAI_PRESET_CHANGED_BEFORE, event => migrate(event.preset, event.savePreset, event.presetName)); } /** @@ -604,22 +604,20 @@ 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 => { - // Save configuration and wrap everything up. - this.saveServiceSettings().then(() => { - const mainPrompt = this.getPromptById('main'); - this.updateQuickEdit('main', mainPrompt); + eventSource.on(event_types.OAI_PRESET_CHANGED_AFTER, () => { + this.sanitizeServiceSettings(); + const mainPrompt = this.getPromptById('main'); + this.updateQuickEdit('main', mainPrompt); - const nsfwPrompt = this.getPromptById('nsfw'); - this.updateQuickEdit('nsfw', nsfwPrompt); + const nsfwPrompt = this.getPromptById('nsfw'); + this.updateQuickEdit('nsfw', nsfwPrompt); - const jailbreakPrompt = this.getPromptById('jailbreak'); - this.updateQuickEdit('jailbreak', jailbreakPrompt); + const jailbreakPrompt = this.getPromptById('jailbreak'); + this.updateQuickEdit('jailbreak', jailbreakPrompt); - this.hidePopup(); - this.clearEditForm(); - this.renderDebounced(); - }); + this.hidePopup(); + this.clearEditForm(); + this.renderDebounced(); }); // Re-render prompt manager on world settings update @@ -643,19 +641,13 @@ PromptManagerModule.prototype.render = function (afterTryGenerate = true) { if (true === afterTryGenerate) { // Executed during dry-run for determining context composition this.profileStart('filling context'); - this.tryGenerate().then(() => { + this.tryGenerate().finally(() => { this.profileEnd('filling context'); this.profileStart('render'); this.renderPromptManager(); this.renderPromptManagerListItems() this.makeDraggable(); this.profileEnd('render'); - }).catch(error => { - this.profileEnd('filling context'); - this.log('Error caught during render: ' + error); - this.renderPromptManager(); - this.renderPromptManagerListItems() - this.makeDraggable(); }); } else { // Executed during live communication @@ -1383,6 +1375,9 @@ PromptManagerModule.prototype.renderPromptManagerListItems = function () { `; + console.log(this.activeCharacter) + console.log(this.serviceSettings) +console.log(this.getPromptsForCharacter(this.activeCharacter)) this.getPromptsForCharacter(this.activeCharacter).forEach(prompt => { if (!prompt) return; diff --git a/public/scripts/openai.js b/public/scripts/openai.js index 3986d2b77..52425bbf2 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -354,7 +354,11 @@ function setupChatCompletionPromptManager(openAiSettings) { } promptManager.tryGenerate = () => { - return Generate('normal', {}, true); + if (characters[this_chid]) { + return Generate('normal', {}, true); + } else{ + return Promise.resolve(); + } } promptManager.tokenHandler = tokenHandler; @@ -613,7 +617,7 @@ function populateChatCompletion(prompts, chatCompletion, { bias, quietPrompt, ty // Insert nsfw avoidance prompt into main, if no nsfw prompt is present if (false === chatCompletion.has('nsfw') && oai_settings.nsfw_avoidance_prompt) - if (prompts.has('nsfwAvoidance')) chatCompletion.insert(Message.fromPrompt(prompts.get('nsfwAvoidance')), 'main'); + if (prompts.has('nsfwAvoidance') && prompts.has('main')) chatCompletion.insert(Message.fromPrompt(prompts.get('nsfwAvoidance')), 'main'); // Bias if (bias && bias.trim().length) addToChatCompletion('bias'); @@ -2463,7 +2467,7 @@ function onSettingsPresetChange() { const updateCheckbox = (selector, value) => $(selector).prop('checked', value).trigger('input'); // Allow subscribers to alter the preset before applying deltas - eventSource.emit(event_types.OAI_PRESET_CHANGED, { + eventSource.emit(event_types.OAI_PRESET_CHANGED_BEFORE, { preset: preset, presetName: presetName, settingsToUpdate: settingsToUpdate, @@ -2485,6 +2489,7 @@ function onSettingsPresetChange() { $(`#openai_logit_bias_preset`).trigger('change'); saveSettingsDebounced(); + eventSource.emit(event_types.OAI_PRESET_CHANGED_AFTER); }); }