Prompt manager configuration fixes (#1078)

* Refactor oai preset change event into before and after

* Simplify and reinforce prompt manager render without character

* Check if main prompt exists before adding nsfwAvoidance

* Sanitize prompt manager configuration on preset loading

---------

Co-authored-by: maver <kentucky@posteo.de>
This commit is contained in:
Cohee 2023-09-01 23:23:03 +03:00 committed by GitHub
parent 428c851c9b
commit 4a6705cea8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 25 deletions

View File

@ -279,7 +279,8 @@ export const event_types = {
CHATCOMPLETION_SOURCE_CHANGED: 'chatcompletion_source_changed', CHATCOMPLETION_SOURCE_CHANGED: 'chatcompletion_source_changed',
CHATCOMPLETION_MODEL_CHANGED: 'chatcompletion_model_changed', CHATCOMPLETION_MODEL_CHANGED: 'chatcompletion_model_changed',
OAI_BEFORE_CHATCOMPLETION: 'oai_before_chatcompletion', 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', WORLDINFO_SETTINGS_UPDATED: 'worldinfo_settings_updated',
CHARACTER_EDITED: 'character_edited', CHARACTER_EDITED: 'character_edited',
USER_MESSAGE_RENDERED: 'user_message_rendered', USER_MESSAGE_RENDERED: 'user_message_rendered',

View File

@ -53,7 +53,7 @@ const registerPromptManagerMigration = () => {
}; };
eventSource.on(event_types.SETTINGS_LOADED_BEFORE, settings => migrate(settings)); 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,9 +604,8 @@ PromptManagerModule.prototype.init = function (moduleConfiguration, serviceSetti
document.getElementById(this.configuration.prefix + 'prompt_manager_popup_close_button').addEventListener('click', closeAndClearPopup); document.getElementById(this.configuration.prefix + 'prompt_manager_popup_close_button').addEventListener('click', closeAndClearPopup);
// Re-render prompt manager on openai preset change // Re-render prompt manager on openai preset change
eventSource.on(event_types.OAI_PRESET_CHANGED, settings => { eventSource.on(event_types.OAI_PRESET_CHANGED_AFTER, () => {
// Save configuration and wrap everything up. this.sanitizeServiceSettings();
this.saveServiceSettings().then(() => {
const mainPrompt = this.getPromptById('main'); const mainPrompt = this.getPromptById('main');
this.updateQuickEdit('main', mainPrompt); this.updateQuickEdit('main', mainPrompt);
@ -620,7 +619,6 @@ PromptManagerModule.prototype.init = function (moduleConfiguration, serviceSetti
this.clearEditForm(); this.clearEditForm();
this.renderDebounced(); this.renderDebounced();
}); });
});
// Re-render prompt manager on world settings update // Re-render prompt manager on world settings update
eventSource.on(event_types.WORLDINFO_SETTINGS_UPDATED, () => this.renderDebounced()); eventSource.on(event_types.WORLDINFO_SETTINGS_UPDATED, () => this.renderDebounced());
@ -643,19 +641,13 @@ PromptManagerModule.prototype.render = function (afterTryGenerate = true) {
if (true === afterTryGenerate) { if (true === afterTryGenerate) {
// Executed during dry-run for determining context composition // Executed during dry-run for determining context composition
this.profileStart('filling context'); this.profileStart('filling context');
this.tryGenerate().then(() => { this.tryGenerate().finally(() => {
this.profileEnd('filling context'); this.profileEnd('filling context');
this.profileStart('render'); this.profileStart('render');
this.renderPromptManager(); this.renderPromptManager();
this.renderPromptManagerListItems() this.renderPromptManagerListItems()
this.makeDraggable(); this.makeDraggable();
this.profileEnd('render'); this.profileEnd('render');
}).catch(error => {
this.profileEnd('filling context');
this.log('Error caught during render: ' + error);
this.renderPromptManager();
this.renderPromptManagerListItems()
this.makeDraggable();
}); });
} else { } else {
// Executed during live communication // Executed during live communication
@ -1383,6 +1375,9 @@ PromptManagerModule.prototype.renderPromptManagerListItems = function () {
</li> </li>
`; `;
console.log(this.activeCharacter)
console.log(this.serviceSettings)
console.log(this.getPromptsForCharacter(this.activeCharacter))
this.getPromptsForCharacter(this.activeCharacter).forEach(prompt => { this.getPromptsForCharacter(this.activeCharacter).forEach(prompt => {
if (!prompt) return; if (!prompt) return;

View File

@ -354,7 +354,11 @@ function setupChatCompletionPromptManager(openAiSettings) {
} }
promptManager.tryGenerate = () => { promptManager.tryGenerate = () => {
if (characters[this_chid]) {
return Generate('normal', {}, true); return Generate('normal', {}, true);
} else{
return Promise.resolve();
}
} }
promptManager.tokenHandler = tokenHandler; 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 // Insert nsfw avoidance prompt into main, if no nsfw prompt is present
if (false === chatCompletion.has('nsfw') && oai_settings.nsfw_avoidance_prompt) 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 // Bias
if (bias && bias.trim().length) addToChatCompletion('bias'); if (bias && bias.trim().length) addToChatCompletion('bias');
@ -2463,7 +2467,7 @@ function onSettingsPresetChange() {
const updateCheckbox = (selector, value) => $(selector).prop('checked', value).trigger('input'); const updateCheckbox = (selector, value) => $(selector).prop('checked', value).trigger('input');
// Allow subscribers to alter the preset before applying deltas // 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, preset: preset,
presetName: presetName, presetName: presetName,
settingsToUpdate: settingsToUpdate, settingsToUpdate: settingsToUpdate,
@ -2485,6 +2489,7 @@ function onSettingsPresetChange() {
$(`#openai_logit_bias_preset`).trigger('change'); $(`#openai_logit_bias_preset`).trigger('change');
saveSettingsDebounced(); saveSettingsDebounced();
eventSource.emit(event_types.OAI_PRESET_CHANGED_AFTER);
}); });
} }