| 'use strict'; |
|
|
| import { DOMPurify } from '../lib.js'; |
|
|
| import { event_types, eventSource, is_send_press, main_api, substituteParams } from '../script.js'; |
| import { is_group_generating } from './group-chats.js'; |
| import { Message, MessageCollection, TokenHandler } from './openai.js'; |
| import { power_user } from './power-user.js'; |
| import { debounce, waitUntilCondition, escapeHtml, uuidv4 } from './utils.js'; |
| import { debounce_timeout } from './constants.js'; |
| import { renderTemplateAsync } from './templates.js'; |
| import { Popup } from './popup.js'; |
| import { t } from './i18n.js'; |
| import { isMobile } from './RossAscends-mods.js'; |
|
|
| function debouncePromise(func, delay) { |
| let timeoutId; |
|
|
| return (...args) => { |
| clearTimeout(timeoutId); |
|
|
| return new Promise((resolve) => { |
| timeoutId = setTimeout(() => { |
| const result = func(...args); |
| resolve(result); |
| }, delay); |
| }); |
| }; |
| } |
|
|
| const DEFAULT_DEPTH = 4; |
| const DEFAULT_ORDER = 100; |
|
|
| |
| |
| |
| export const INJECTION_POSITION = { |
| RELATIVE: 0, |
| ABSOLUTE: 1, |
| }; |
|
|
| |
| |
| |
| 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); |
|
|
| const findPrompt = (identifier) => settings.prompts.find(prompt => identifier === prompt.identifier); |
| if (settings.main_prompt) { |
| findPrompt('main').content = settings.main_prompt; |
| delete settings.main_prompt; |
| } |
|
|
| if (settings.nsfw_prompt) { |
| findPrompt('nsfw').content = settings.nsfw_prompt; |
| delete settings.nsfw_prompt; |
| } |
|
|
| if (settings.jailbreak_prompt) { |
| findPrompt('jailbreak').content = settings.jailbreak_prompt; |
| delete settings.jailbreak_prompt; |
| } |
|
|
| if (savePreset && presetName) savePreset(presetName, settings, false); |
| } |
| }; |
|
|
| eventSource.on(event_types.SETTINGS_LOADED_BEFORE, settings => migrate(settings)); |
| eventSource.on(event_types.OAI_PRESET_CHANGED_BEFORE, event => migrate(event.preset, event.savePreset, event.presetName)); |
| }; |
|
|
| |
| |
| |
| class Prompt { |
| |
| |
| |
| |
| enabled; |
|
|
| |
| |
| |
| |
| identifier; |
|
|
| |
| |
| |
| |
| role; |
|
|
| |
| |
| |
| |
| content; |
|
|
| |
| |
| |
| |
| name; |
|
|
| |
| |
| |
| |
| system_prompt; |
|
|
| |
| |
| |
| |
| position; |
|
|
| |
| |
| |
| |
| injection_position; |
|
|
| |
| |
| |
| |
| injection_depth; |
|
|
| |
| |
| |
| |
| injection_order; |
|
|
| |
| |
| |
| |
| forbid_overrides; |
|
|
| |
| |
| |
| |
| extension; |
|
|
| |
| |
| |
| |
| injection_trigger; |
|
|
| |
| |
| |
| |
| marker; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| constructor({ identifier, role, content, name, system_prompt, position, injection_depth, injection_position, forbid_overrides, extension, injection_order, injection_trigger } = {}) { |
| this.identifier = identifier; |
| this.role = role; |
| this.content = content; |
| this.name = name; |
| this.system_prompt = system_prompt; |
| this.position = position; |
| this.injection_depth = injection_depth; |
| this.injection_position = injection_position; |
| this.forbid_overrides = forbid_overrides; |
| this.extension = extension ?? false; |
| this.injection_order = injection_order ?? DEFAULT_ORDER; |
| this.injection_trigger = injection_trigger ?? []; |
| } |
| } |
|
|
| |
| |
| |
| export class PromptCollection { |
| |
| |
| |
| |
| collection = []; |
|
|
| |
| |
| |
| |
| overriddenPrompts = []; |
|
|
| |
| |
| |
| |
| |
| 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'); |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| add(...prompts) { |
| this.checkPromptInstance(...prompts); |
| this.collection.push(...prompts); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| set(prompt, position) { |
| this.checkPromptInstance(prompt); |
| this.collection[position] = prompt; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| get(identifier) { |
| return this.collection.find(prompt => prompt.identifier === identifier); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| index(identifier) { |
| return this.collection.findIndex(prompt => prompt.identifier === identifier); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| has(identifier) { |
| return this.index(identifier) !== -1; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| override(prompt, position) { |
| this.set(prompt, position); |
| this.overriddenPrompts.push(prompt.identifier); |
| } |
| } |
|
|
| class PromptManager { |
| get promptSources() { |
| return { |
| charDescription: t`Character Description`, |
| charPersonality: t`Character Personality`, |
| scenario: t`Character Scenario`, |
| personaDescription: t`Persona Description`, |
| worldInfoBefore: t`World Info (↑Char)`, |
| worldInfoAfter: t`World Info (↓Char)`, |
| }; |
| } |
|
|
| constructor() { |
| this.systemPrompts = [ |
| 'main', |
| 'nsfw', |
| 'jailbreak', |
| 'enhanceDefinitions', |
| ]; |
|
|
| this.overridablePrompts = [ |
| 'main', |
| 'jailbreak', |
| ]; |
|
|
| this.overriddenPrompts = []; |
|
|
| this.configuration = { |
| version: 1, |
| prefix: '', |
| containerIdentifier: '', |
| listIdentifier: '', |
| listItemTemplateIdentifier: '', |
| toggleDisabled: [], |
| promptOrder: { |
| strategy: 'global', |
| dummyId: 100000, |
| }, |
| sortableDelay: 30, |
| warningTokenThreshold: 1500, |
| dangerTokenThreshold: 500, |
| defaultPrompts: { |
| main: '', |
| nsfw: '', |
| jailbreak: '', |
| enhanceDefinitions: '', |
| }, |
| }; |
|
|
| |
| this.serviceSettings = null; |
|
|
| |
| this.containerElement = null; |
|
|
| |
| this.listElement = null; |
|
|
| |
| this.activeCharacter = null; |
|
|
| |
| this.messages = null; |
|
|
| |
| this.tokenHandler = null; |
|
|
| |
| this.tokenUsage = 0; |
|
|
| |
| this.error = null; |
|
|
| |
| this.tryGenerate = async () => { }; |
|
|
| |
| this.saveServiceSettings = () => { return Promise.resolve(); }; |
|
|
| |
| this.handleToggle = () => { }; |
|
|
| |
| this.handleInspect = () => { }; |
|
|
| |
| this.handleEdit = () => { }; |
|
|
| |
| this.handleDetach = () => { }; |
|
|
| |
| this.handleSavePrompt = () => { }; |
|
|
| |
| this.handleResetPrompt = () => { }; |
|
|
| |
| this.handleNewPrompt = () => { }; |
|
|
| |
| this.handleDeletePrompt = () => { }; |
|
|
| |
| this.handleAppendPrompt = () => { }; |
|
|
| |
| this.handleImport = () => { }; |
|
|
| |
| this.handleFullExport = () => { }; |
|
|
| |
| this.handleCharacterExport = () => { }; |
|
|
| |
| this.handleCharacterReset = () => { }; |
|
|
| |
| this.renderDebounced = debounce(this.render.bind(this), debounce_timeout.relaxed); |
| } |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| init(moduleConfiguration, serviceSettings) { |
| this.configuration = Object.assign(this.configuration, moduleConfiguration); |
| this.tokenHandler = this.tokenHandler || new TokenHandler(() => { throw new Error('Token handler not set'); }); |
| this.serviceSettings = serviceSettings; |
| this.containerElement = document.getElementById(this.configuration.containerIdentifier); |
|
|
| if ('global' === this.configuration.promptOrder.strategy) this.activeCharacter = { id: this.configuration.promptOrder.dummyId }; |
|
|
| this.sanitizeServiceSettings(); |
|
|
| |
| this.handleToggle = (event) => { |
| const promptID = event.target.closest('.' + this.configuration.prefix + 'prompt_manager_prompt').dataset.pmIdentifier; |
| const promptOrderEntry = this.getPromptOrderEntry(this.activeCharacter, promptID); |
| const counts = this.tokenHandler.getCounts(); |
|
|
| counts[promptID] = null; |
| promptOrderEntry.enabled = !promptOrderEntry.enabled; |
| this.render(); |
| this.saveServiceSettings(); |
| }; |
|
|
| |
| this.handleEdit = (event) => { |
| this.clearEditForm(); |
| this.clearInspectForm(); |
|
|
| const promptID = event.target.closest('.' + this.configuration.prefix + 'prompt_manager_prompt').dataset.pmIdentifier; |
| const prompt = this.getPromptById(promptID); |
|
|
| this.loadPromptIntoEditForm(prompt); |
|
|
| this.showPopup(); |
| }; |
|
|
| |
| this.handleInspect = (event) => { |
| this.clearEditForm(); |
| this.clearInspectForm(); |
|
|
| const promptID = event.target.closest('.' + this.configuration.prefix + 'prompt_manager_prompt').dataset.pmIdentifier; |
| if (true === this.messages.hasItemWithIdentifier(promptID)) { |
| const messages = this.messages.getItemByIdentifier(promptID); |
|
|
| this.loadMessagesIntoInspectForm(messages); |
|
|
| this.showPopup('inspect'); |
| } |
| }; |
|
|
| |
| this.handleDetach = (event) => { |
| if (null === this.activeCharacter) return; |
| const promptID = event.target.closest('.' + this.configuration.prefix + 'prompt_manager_prompt').dataset.pmIdentifier; |
| const prompt = this.getPromptById(promptID); |
|
|
| this.detachPrompt(prompt, this.activeCharacter); |
| this.hidePopup(); |
| this.clearEditForm(); |
| this.render(); |
| this.saveServiceSettings(); |
| }; |
|
|
| |
| this.handleSavePrompt = (event) => { |
| const promptId = event.target.dataset.pmPrompt; |
| const prompt = this.getPromptById(promptId); |
|
|
| if (null === prompt) { |
| const newPrompt = {}; |
| this.updatePromptWithPromptEditForm(newPrompt); |
| this.addPrompt(newPrompt, promptId); |
| } else { |
| this.updatePromptWithPromptEditForm(prompt); |
| } |
|
|
| 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); |
|
|
| this.hidePopup(); |
| this.clearEditForm(); |
| this.render(); |
| this.saveServiceSettings(); |
| }; |
|
|
| |
| this.handleResetPrompt = (event) => { |
| const promptId = event.target.dataset.pmPrompt; |
| const prompt = this.getPromptById(promptId); |
| const isPulledPrompt = Object.keys(this.promptSources).includes(promptId); |
|
|
| switch (promptId) { |
| case 'main': |
| prompt.name = 'Main Prompt'; |
| prompt.content = this.configuration.defaultPrompts.main; |
| prompt.forbid_overrides = false; |
| break; |
| case 'nsfw': |
| prompt.name = 'Nsfw Prompt'; |
| prompt.content = this.configuration.defaultPrompts.nsfw; |
| break; |
| case 'jailbreak': |
| prompt.name = 'Jailbreak Prompt'; |
| prompt.content = this.configuration.defaultPrompts.jailbreak; |
| prompt.forbid_overrides = false; |
| break; |
| case 'enhanceDefinitions': |
| prompt.name = 'Enhance Definitions'; |
| prompt.content = this.configuration.defaultPrompts.enhanceDefinitions; |
| break; |
| } |
|
|
| const nameField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_name')); |
| const roleField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_role')); |
| const promptField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_prompt')); |
| const injectionPositionField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_position')); |
| const injectionDepthField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_depth')); |
| const injectionOrderField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_order')); |
| const injectionTriggerField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_trigger')); |
| const depthBlock = (document.getElementById(this.configuration.prefix + 'prompt_manager_depth_block')); |
| const orderBlock = (document.getElementById(this.configuration.prefix + 'prompt_manager_order_block')); |
| const forbidOverridesField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_forbid_overrides')); |
| const forbidOverridesBlock = (document.getElementById(this.configuration.prefix + 'prompt_manager_forbid_overrides_block')); |
| const entrySourceBlock = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_source_block')); |
| const entrySource = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_source')); |
|
|
| nameField.value = prompt.name; |
| roleField.value = 'system'; |
| promptField.value = prompt.content ?? ''; |
| injectionPositionField.value = (prompt.injection_position ?? 0).toString(); |
| injectionDepthField.value = (prompt.injection_depth ?? DEFAULT_DEPTH).toString(); |
| injectionOrderField.value = (prompt.injection_order ?? DEFAULT_ORDER).toString(); |
| Array.from(injectionTriggerField.options).forEach(option => { |
| option.selected = false; |
| }); |
| injectionTriggerField.dispatchEvent(new Event('change', { bubbles: true })); |
| depthBlock.style.visibility = prompt.injection_position === INJECTION_POSITION.ABSOLUTE ? 'visible' : 'hidden'; |
| orderBlock.style.visibility = prompt.injection_position === INJECTION_POSITION.ABSOLUTE ? 'visible' : 'hidden'; |
| forbidOverridesField.checked = prompt.forbid_overrides ?? false; |
| forbidOverridesBlock.style.visibility = this.overridablePrompts.includes(prompt.identifier) ? 'visible' : 'hidden'; |
| promptField.disabled = prompt.marker ?? false; |
| entrySourceBlock.style.display = isPulledPrompt ? '' : 'none'; |
|
|
| if (isPulledPrompt) { |
| const sourceName = this.promptSources[promptId]; |
| entrySource.textContent = sourceName; |
| } |
| }; |
|
|
| |
| this.handleAppendPrompt = (event) => { |
| const appendPromptFooter = (document.getElementById(this.configuration.prefix + 'prompt_manager_footer_append_prompt')); |
| const promptID = appendPromptFooter.value; |
| const prompt = this.getPromptById(promptID); |
|
|
| if (prompt) { |
| this.appendPrompt(prompt, this.activeCharacter); |
| this.render(); |
| this.saveServiceSettings(); |
| } |
| }; |
|
|
| |
| this.handleDeletePrompt = async (event) => { |
| Popup.show.confirm(t`Are you sure you want to delete this prompt?`, null).then((userChoice) => { |
| if (!userChoice) return; |
| const appendPromptFooter = (document.getElementById(this.configuration.prefix + 'prompt_manager_footer_append_prompt')); |
| const promptID = appendPromptFooter.value; |
| const prompt = this.getPromptById(promptID); |
|
|
| if (prompt && true === this.isPromptDeletionAllowed(prompt)) { |
| const promptIndex = this.getPromptIndexById(promptID); |
| this.serviceSettings.prompts.splice(Number(promptIndex), 1); |
|
|
| this.log('Deleted prompt: ' + prompt.identifier); |
|
|
| this.hidePopup(); |
| this.clearEditForm(); |
| this.render(); |
| this.saveServiceSettings(); |
| } |
| }); |
| }; |
|
|
| |
| this.handleNewPrompt = (event) => { |
| const prompt = { |
| identifier: this.getUuidv4(), |
| name: '', |
| role: 'system', |
| content: '', |
| }; |
|
|
| this.loadPromptIntoEditForm(prompt); |
| this.showPopup(); |
| }; |
|
|
| |
| this.handleFullExport = () => { |
| const prompts = this.serviceSettings.prompts.reduce((userPrompts, prompt) => { |
| if (false === prompt.system_prompt && false === prompt.marker) userPrompts.push(prompt); |
| return userPrompts; |
| }, []); |
|
|
| 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'); |
| }; |
|
|
| |
| this.handleCharacterExport = () => { |
| const characterPrompts = this.getPromptsForCharacter(this.activeCharacter).reduce((userPrompts, prompt) => { |
| if (false === prompt.system_prompt && !prompt.marker) userPrompts.push(prompt); |
| return userPrompts; |
| }, []); |
|
|
| const characterList = this.getPromptOrderForCharacter(this.activeCharacter); |
|
|
| const exportPrompts = { |
| prompts: characterPrompts, |
| prompt_order: characterList, |
| }; |
|
|
| const name = this.activeCharacter.name + '-prompts'; |
| this.export(exportPrompts, 'character', name); |
| }; |
|
|
| |
| this.handleImport = () => { |
| Popup.show.confirm(t`Existing prompts with the same ID will be overridden. Do you want to proceed?`, null) |
| .then(userChoice => { |
| if (!userChoice) return; |
|
|
| const fileOpener = document.createElement('input'); |
| fileOpener.type = 'file'; |
| fileOpener.accept = '.json'; |
|
|
| fileOpener.addEventListener('change', (event) => { |
| if (!(event.target instanceof HTMLInputElement)) return; |
| const file = event.target.files[0]; |
| if (!file) return; |
|
|
| const reader = new FileReader(); |
|
|
| reader.onload = (event) => { |
| const fileContent = event.target.result; |
|
|
| try { |
| const data = JSON.parse(fileContent.toString()); |
| this.import(data); |
| } catch (err) { |
| toastr.error(t`An error occurred while importing prompts. More info available in console.`); |
| console.log('An error occurred while importing prompts'); |
| console.log(err.toString()); |
| } |
| }; |
|
|
| reader.readAsText(file); |
| }); |
|
|
| fileOpener.click(); |
| }); |
| }; |
|
|
| |
| this.handleCharacterReset = () => { |
| Popup.show.confirm(t`This will reset the prompt order for this character. You will not lose any prompts.`, null) |
| .then(userChoice => { |
| if (!userChoice) return; |
|
|
| this.removePromptOrderForCharacter(this.activeCharacter); |
| this.addPromptOrderForCharacter(this.activeCharacter, promptManagerDefaultPromptOrder); |
|
|
| this.render(); |
| this.saveServiceSettings(); |
| }); |
| }; |
|
|
| |
| if ('global' === this.configuration.promptOrder.strategy) { |
| const handleQuickEditSave = (event) => { |
| const promptId = event.target.dataset.pmPrompt; |
| const prompt = this.getPromptById(promptId); |
|
|
| prompt.content = event.target.value; |
|
|
| |
| |
| const popupEditFormPrompt = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_prompt')); |
| if (popupEditFormPrompt.offsetParent) { |
| popupEditFormPrompt.value = prompt.content; |
| } |
|
|
| this.log('Saved prompt: ' + promptId); |
| this.saveServiceSettings().then(() => this.render()); |
| }; |
|
|
| const mainPrompt = this.getPromptById('main'); |
| const mainElementId = this.updateQuickEdit('main', mainPrompt); |
| document.getElementById(mainElementId).addEventListener('blur', handleQuickEditSave); |
|
|
| const nsfwPrompt = this.getPromptById('nsfw'); |
| const nsfwElementId = this.updateQuickEdit('nsfw', nsfwPrompt); |
| document.getElementById(nsfwElementId).addEventListener('blur', handleQuickEditSave); |
|
|
| const jailbreakPrompt = this.getPromptById('jailbreak'); |
| const jailbreakElementId = this.updateQuickEdit('jailbreak', jailbreakPrompt); |
| document.getElementById(jailbreakElementId).addEventListener('blur', handleQuickEditSave); |
| } |
|
|
| |
| eventSource.on(event_types.MESSAGE_DELETED, () => this.renderDebounced()); |
| eventSource.on(event_types.MESSAGE_EDITED, () => this.renderDebounced()); |
| eventSource.on(event_types.MESSAGE_RECEIVED, () => this.renderDebounced()); |
|
|
| |
| eventSource.on(event_types.CHATCOMPLETION_SOURCE_CHANGED, () => this.renderDebounced()); |
|
|
| eventSource.on(event_types.CHATCOMPLETION_MODEL_CHANGED, () => this.renderDebounced()); |
|
|
| |
| eventSource.on('chatLoaded', (event) => { |
| this.handleCharacterSelected(event); |
| this.saveServiceSettings().then(() => this.renderDebounced()); |
| }); |
|
|
| |
| eventSource.on(event_types.CHARACTER_EDITED, (event) => { |
| this.handleCharacterUpdated(event); |
| this.saveServiceSettings().then(() => this.renderDebounced()); |
| }); |
|
|
| |
| eventSource.on('groupSelected', (event) => { |
| this.handleGroupSelected(event); |
| this.saveServiceSettings().then(() => this.renderDebounced()); |
| }); |
|
|
| |
| eventSource.on(event_types.CHARACTER_DELETED, (event) => { |
| this.handleCharacterDeleted(event); |
| this.saveServiceSettings().then(() => this.renderDebounced()); |
| }); |
|
|
| |
| document.getElementById('openai_max_context').addEventListener('change', (event) => { |
| if (!(event.target instanceof HTMLInputElement)) return; |
| this.serviceSettings.openai_max_context = event.target.value; |
| if (this.activeCharacter) this.renderDebounced(); |
| }); |
|
|
| document.getElementById('openai_max_tokens').addEventListener('change', (event) => { |
| if (this.activeCharacter) this.renderDebounced(); |
| }); |
|
|
| |
| document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_save').addEventListener('click', this.handleSavePrompt); |
| document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_reset').addEventListener('click', this.handleResetPrompt); |
|
|
| const closeAndClearPopup = () => { |
| this.hidePopup(); |
| this.clearEditForm(); |
| this.clearInspectForm(); |
| }; |
|
|
| |
| document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_close').addEventListener('click', closeAndClearPopup); |
| document.getElementById(this.configuration.prefix + 'prompt_manager_popup_close_button').addEventListener('click', closeAndClearPopup); |
| closeAndClearPopup(); |
|
|
| |
| 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 jailbreakPrompt = this.getPromptById('jailbreak'); |
| this.updateQuickEdit('jailbreak', jailbreakPrompt); |
|
|
| this.hidePopup(); |
| this.clearEditForm(); |
| this.renderDebounced(); |
| }); |
|
|
| |
| eventSource.on(event_types.WORLDINFO_SETTINGS_UPDATED, () => this.renderDebounced()); |
|
|
| this.log('Initialized'); |
| } |
|
|
| |
| |
| |
| |
| #getScrollPosition() { |
| return document.getElementById(this.configuration.prefix + 'prompt_manager')?.closest('.scrollableInner')?.scrollTop; |
| } |
|
|
| |
| |
| |
| |
| #setScrollPosition(scrollPosition) { |
| if (scrollPosition === undefined || scrollPosition === null) return; |
| document.getElementById(this.configuration.prefix + 'prompt_manager')?.closest('.scrollableInner')?.scrollTo(0, scrollPosition); |
| } |
|
|
| |
| |
| |
| |
| |
| render(afterTryGenerate = true) { |
| if (main_api !== 'openai') return; |
|
|
| if ('character' === this.configuration.promptOrder.strategy && null === this.activeCharacter) return; |
| this.error = null; |
|
|
| waitUntilCondition(() => !is_send_press && !is_group_generating, 1024 * 1024, 100).then(async () => { |
| if (true === afterTryGenerate) { |
| |
| this.profileStart('filling context'); |
| this.tryGenerate().finally(async () => { |
| this.profileEnd('filling context'); |
| this.profileStart('render'); |
| const scrollPosition = this.#getScrollPosition(); |
| await this.renderPromptManager(); |
| await this.renderPromptManagerListItems(); |
| this.makeDraggable(); |
| this.#setScrollPosition(scrollPosition); |
| this.profileEnd('render'); |
| }); |
| } else { |
| |
| this.profileStart('render'); |
| const scrollPosition = this.#getScrollPosition(); |
| await this.renderPromptManager(); |
| await this.renderPromptManagerListItems(); |
| this.makeDraggable(); |
| this.#setScrollPosition(scrollPosition); |
| this.profileEnd('render'); |
| } |
| }).catch(() => { |
| console.log('Timeout while waiting for send press to be false'); |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| updatePromptWithPromptEditForm(prompt) { |
| const nameField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_name')); |
| const roleField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_role')); |
| const promptField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_prompt')); |
| const injectionPositionField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_position')); |
| const injectionDepthField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_depth')); |
| const injectionOrderField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_order')); |
| const injectionTriggerField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_trigger')); |
| const forbidOverridesField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_forbid_overrides')); |
|
|
| prompt.name = nameField.value; |
| prompt.role = roleField.value; |
| prompt.content = promptField.value; |
| prompt.injection_position = Number(injectionPositionField.value); |
| prompt.injection_depth = Number(injectionDepthField.value); |
| prompt.injection_order = Number(injectionOrderField.value); |
| prompt.injection_trigger = Array.from(injectionTriggerField.selectedOptions).map(option => option.value); |
| prompt.forbid_overrides = forbidOverridesField.checked; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| updatePromptByIdentifier(identifier, updatePrompt) { |
| let prompt = this.serviceSettings.prompts.find((item) => identifier === item.identifier); |
| if (prompt) prompt = Object.assign(prompt, updatePrompt); |
| } |
|
|
| |
| |
| |
| |
| |
| updatePrompts(prompts) { |
| prompts.forEach((update) => { |
| let prompt = this.getPromptById(update.identifier); |
| if (prompt) Object.assign(prompt, update); |
| }); |
| } |
|
|
| getTokenHandler() { |
| return this.tokenHandler; |
| } |
|
|
| isPromptDisabledForActiveCharacter(identifier) { |
| const promptOrderEntry = this.getPromptOrderEntry(this.activeCharacter, identifier); |
| if (promptOrderEntry) return !promptOrderEntry.enabled; |
| return false; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| appendPrompt(prompt, character) { |
| const promptOrder = this.getPromptOrderForCharacter(character); |
| const index = promptOrder.findIndex(entry => entry.identifier === prompt.identifier); |
|
|
| if (-1 === index) promptOrder.unshift({ identifier: prompt.identifier, enabled: false }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| detachPrompt(prompt, character) { |
| const promptOrder = this.getPromptOrderForCharacter(character); |
| const index = promptOrder.findIndex(entry => entry.identifier === prompt.identifier); |
| if (-1 === index) return; |
| promptOrder.splice(index, 1); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| addPrompt(prompt, identifier) { |
|
|
| if (typeof prompt !== 'object' || prompt === null) throw new Error('Object is not a prompt'); |
|
|
| const newPrompt = { |
| identifier: identifier, |
| system_prompt: false, |
| enabled: false, |
| marker: false, |
| ...prompt, |
| }; |
|
|
| this.serviceSettings.prompts.push(newPrompt); |
| } |
|
|
| |
| |
| |
| |
| sanitizeServiceSettings() { |
| 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); |
| } |
|
|
| |
| this.serviceSettings.prompts.length === 0 |
| ? this.setPrompts(chatCompletionDefaultPrompts.prompts) |
| : this.checkForMissingPrompts(this.serviceSettings.prompts); |
|
|
| |
| this.serviceSettings.prompts.forEach(prompt => prompt && (prompt.identifier = prompt.identifier ?? this.getUuidv4())); |
|
|
| if (this.activeCharacter) { |
| const promptReferences = this.getPromptOrderForCharacter(this.activeCharacter); |
| for (let i = promptReferences.length - 1; i >= 0; i--) { |
| const reference = promptReferences[i]; |
| if (reference && -1 === this.serviceSettings.prompts.findIndex(prompt => prompt.identifier === reference.identifier)) { |
| promptReferences.splice(i, 1); |
| this.log('Removed unused reference: ' + reference.identifier); |
| } |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| checkForMissingPrompts(prompts) { |
| const defaultPromptIdentifiers = chatCompletionDefaultPrompts.prompts.reduce((list, prompt) => { list.push(prompt.identifier); return list; }, []); |
|
|
| const missingIdentifiers = defaultPromptIdentifiers.filter(identifier => |
| !prompts.some(prompt => prompt.identifier === identifier), |
| ); |
|
|
| missingIdentifiers.forEach(identifier => { |
| const defaultPrompt = chatCompletionDefaultPrompts.prompts.find(prompt => prompt?.identifier === identifier); |
| if (defaultPrompt) { |
| prompts.push(defaultPrompt); |
| this.log(`Missing system prompt: ${defaultPrompt.identifier}. Added default.`); |
| } |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| isPromptInspectionAllowed(prompt) { |
| return true; |
| } |
|
|
| |
| |
| |
| |
| |
| isPromptDeletionAllowed(prompt) { |
| return false === prompt.system_prompt; |
| } |
|
|
| |
| |
| |
| |
| |
| isPromptEditAllowed(prompt) { |
| const forceEditPrompts = [ |
| 'charDescription', |
| 'charPersonality', |
| 'scenario', |
| 'personaDescription', |
| 'worldInfoBefore', |
| 'worldInfoAfter', |
| ]; |
| return forceEditPrompts.includes(prompt.identifier) || !prompt.marker; |
| } |
|
|
| |
| |
| |
| |
| |
| isPromptToggleAllowed(prompt) { |
| const forceTogglePrompts = [ |
| 'charDescription', |
| 'charPersonality', |
| 'scenario', |
| 'personaDescription', |
| 'worldInfoBefore', |
| 'worldInfoAfter', |
| 'main', |
| 'chatHistory', |
| 'dialogueExamples', |
| ]; |
| return prompt.marker && !forceTogglePrompts.includes(prompt.identifier) ? false : !this.configuration.toggleDisabled.includes(prompt.identifier); |
| } |
|
|
| |
| |
| |
| |
| |
| handleCharacterDeleted(event) { |
| if ('global' === this.configuration.promptOrder.strategy) return; |
| this.removePromptOrderForCharacter(this.activeCharacter); |
| if (this.activeCharacter.id === event.detail.id) this.activeCharacter = null; |
| } |
|
|
| |
| |
| |
| |
| |
| handleCharacterSelected(event) { |
| 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); |
|
|
| |
| |
| if (0 === promptOrder.length) this.addPromptOrderForCharacter(this.activeCharacter, promptManagerDefaultPromptOrder); |
| } else { |
| throw new Error('Unsupported prompt order mode.'); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| handleCharacterUpdated(event) { |
| 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.'); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| handleGroupSelected(event) { |
| 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); |
| } else { |
| throw new Error('Prompt order strategy not supported.'); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| getActiveGroupCharacters() { |
| |
| return (this.activeCharacter?.group?.members || []).map(member => member && member.substring(0, member.lastIndexOf('.'))); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| getPromptsForCharacter(character, onlyEnabled = false) { |
| return this.getPromptOrderForCharacter(character) |
| .map(item => true === onlyEnabled ? (true === item.enabled ? this.getPromptById(item.identifier) : null) : this.getPromptById(item.identifier)) |
| .filter(prompt => null !== prompt); |
| } |
|
|
| |
| |
| |
| |
| |
| getPromptOrderForCharacter(character) { |
| return !character ? [] : (this.serviceSettings.prompt_order.find(list => String(list.character_id) === String(character.id))?.order ?? []); |
| } |
|
|
| |
| |
| |
| |
| |
| setPrompts(prompts) { |
| this.serviceSettings.prompts = prompts; |
| } |
|
|
| |
| |
| |
| |
| |
| removePromptOrderForCharacter(character) { |
| const index = this.serviceSettings.prompt_order.findIndex(list => String(list.character_id) === String(character.id)); |
| if (-1 !== index) this.serviceSettings.prompt_order.splice(index, 1); |
| } |
|
|
| |
| |
| |
| |
| |
| addPromptOrderForCharacter(character, promptOrder) { |
| this.serviceSettings.prompt_order.push({ |
| character_id: character.id, |
| order: JSON.parse(JSON.stringify(promptOrder)), |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| getPromptOrderEntry(character, identifier) { |
| return this.getPromptOrderForCharacter(character).find(entry => entry.identifier === identifier) ?? null; |
| } |
|
|
| |
| |
| |
| |
| |
| getPromptById(identifier) { |
| return this.serviceSettings.prompts.find(item => item && item.identifier === identifier) ?? null; |
| } |
|
|
| |
| |
| |
| |
| |
| getPromptIndexById(identifier) { |
| return this.serviceSettings.prompts.findIndex(item => item.identifier === identifier) ?? null; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| preparePrompt(prompt, original = null) { |
| const groupMembers = this.getActiveGroupCharacters(); |
| const preparedPrompt = new Prompt(prompt); |
|
|
| if (typeof original === 'string') { |
| if (0 < groupMembers.length) preparedPrompt.content = substituteParams(prompt.content ?? '', { original, groupOverride: groupMembers.join(', ') }); |
| else preparedPrompt.content = substituteParams(prompt.content, { original }); |
| } else { |
| if (0 < groupMembers.length) preparedPrompt.content = substituteParams(prompt.content ?? '', { groupOverride: groupMembers.join(', ') }); |
| else preparedPrompt.content = substituteParams(prompt.content); |
| } |
|
|
| return preparedPrompt; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| createQuickEdit(identifier, title) { |
| const prompt = this.getPromptById(identifier); |
| const textareaIdentifier = `${identifier}_prompt_quick_edit_textarea`; |
| const html = `<div class="range-block m-t-1"> |
| <div class="justifyLeft" data-i18n="${title}">${title}</div> |
| <div class="wide100p"> |
| <textarea id="${textareaIdentifier}" class="text_pole textarea_compact" rows="6" placeholder="">${prompt.content}</textarea> |
| </div> |
| </div>`; |
|
|
| 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()); |
| }); |
|
|
| } |
|
|
| |
| |
| |
| |
| |
| |
| updateQuickEdit(identifier, prompt) { |
| const elementId = `${identifier}_prompt_quick_edit_textarea`; |
| const textarea = (document.getElementById(elementId)); |
| textarea.value = prompt.content; |
|
|
| return elementId; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| isValidName(name) { |
| const regex = /^[a-zA-Z0-9_]{1,64}$/; |
|
|
| return regex.test(name); |
| } |
|
|
| sanitizeName(name) { |
| return name.replace(/[^a-zA-Z0-9_]/g, '_').substring(0, 64); |
| } |
|
|
| |
| |
| |
| |
| loadPromptIntoEditForm(prompt) { |
| const nameField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_name')); |
| const roleField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_role')); |
| const promptField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_prompt')); |
| const injectionPositionField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_position')); |
| const injectionDepthField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_depth')); |
| const injectionOrderField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_order')); |
| const injectionTriggerField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_trigger')); |
| const injectionDepthBlock = (document.getElementById(this.configuration.prefix + 'prompt_manager_depth_block')); |
| const injectionOrderBlock = (document.getElementById(this.configuration.prefix + 'prompt_manager_order_block')); |
| const forbidOverridesField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_forbid_overrides')); |
| const forbidOverridesBlock = (document.getElementById(this.configuration.prefix + 'prompt_manager_forbid_overrides_block')); |
| const entrySourceBlock = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_source_block')); |
| const entrySource = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_source')); |
| const isPulledPrompt = Object.keys(this.promptSources).includes(prompt.identifier); |
|
|
| nameField.value = prompt.name ?? ''; |
| roleField.value = prompt.role || 'system'; |
| promptField.value = prompt.content ?? ''; |
| promptField.disabled = prompt.marker ?? false; |
| injectionPositionField.value = (prompt.injection_position ?? INJECTION_POSITION.RELATIVE).toString(); |
| injectionDepthField.value = (prompt.injection_depth ?? DEFAULT_DEPTH).toString(); |
| injectionOrderField.value = (prompt.injection_order ?? DEFAULT_ORDER).toString(); |
| Array.from(injectionTriggerField.options).forEach(option => { |
| option.selected = Array.isArray(prompt.injection_trigger) && prompt.injection_trigger.includes(option.value); |
| }); |
| injectionTriggerField.dispatchEvent(new Event('change', { bubbles: true })); |
| injectionDepthBlock.style.visibility = prompt.injection_position === INJECTION_POSITION.ABSOLUTE ? 'visible' : 'hidden'; |
| injectionOrderBlock.style.visibility = prompt.injection_position === INJECTION_POSITION.ABSOLUTE ? 'visible' : 'hidden'; |
| injectionPositionField.removeAttribute('disabled'); |
| forbidOverridesField.checked = prompt.forbid_overrides ?? false; |
| forbidOverridesBlock.style.visibility = this.overridablePrompts.includes(prompt.identifier) ? 'visible' : 'hidden'; |
| entrySourceBlock.style.display = isPulledPrompt ? '' : 'none'; |
|
|
| if (isPulledPrompt) { |
| const sourceName = this.promptSources[prompt.identifier]; |
| entrySource.textContent = sourceName; |
| } |
|
|
| const resetPromptButton = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_reset'); |
| if (true === prompt.system_prompt) { |
| resetPromptButton.style.display = 'block'; |
| resetPromptButton.dataset.pmPrompt = prompt.identifier; |
| } else { |
| resetPromptButton.style.display = 'none'; |
| } |
|
|
| injectionPositionField.removeEventListener('change', (e) => this.handleInjectionPositionChange(e)); |
| injectionPositionField.addEventListener('change', (e) => this.handleInjectionPositionChange(e)); |
|
|
| const savePromptButton = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_save'); |
| savePromptButton.dataset.pmPrompt = prompt.identifier; |
| } |
|
|
| handleInjectionPositionChange(event) { |
| const injectionDepthBlock = document.getElementById(this.configuration.prefix + 'prompt_manager_depth_block'); |
| const injectionOrderBlock = document.getElementById(this.configuration.prefix + 'prompt_manager_order_block'); |
| const injectionPosition = Number(event.target.value); |
| if (injectionPosition === INJECTION_POSITION.ABSOLUTE) { |
| injectionDepthBlock.style.visibility = 'visible'; |
| injectionOrderBlock.style.visibility = 'visible'; |
| } else { |
| injectionDepthBlock.style.visibility = 'hidden'; |
| injectionOrderBlock.style.visibility = 'hidden'; |
| } |
| } |
|
|
| |
| |
| |
| |
| loadMessagesIntoInspectForm(messages) { |
| if (!messages) return; |
|
|
| const createInlineDrawer = (message) => { |
| const truncatedTitle = message.content.length > 32 ? message.content.slice(0, 32) + '...' : message.content; |
| const title = message.identifier || truncatedTitle; |
| const role = message.role; |
| const content = message.content || 'No Content'; |
| const tokens = message.getTokens(); |
|
|
| let drawerHTML = ` |
| <div class="inline-drawer ${this.configuration.prefix}prompt_manager_prompt"> |
| <div class="inline-drawer-toggle inline-drawer-header"> |
| <span>Name: ${escapeHtml(title)}, Role: ${role}, Tokens: ${tokens}</span> |
| <div class="fa-solid fa-circle-chevron-down inline-drawer-icon down"></div> |
| </div> |
| <div class="inline-drawer-content" style="white-space: pre-wrap;">${escapeHtml(content)}</div> |
| </div> |
| `; |
|
|
| let template = document.createElement('template'); |
| template.innerHTML = drawerHTML.trim(); |
| return template.content.firstChild; |
| }; |
|
|
| const messageList = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_inspect_list'); |
|
|
| const messagesCollection = messages instanceof Message ? [messages] : messages.getCollection(); |
|
|
| if (0 === messagesCollection.length) messageList.innerHTML = '<span>This marker does not contain any prompts.</span>'; |
|
|
| messagesCollection.forEach(message => { |
| messageList.append(createInlineDrawer(message)); |
| }); |
| } |
|
|
| |
| |
| |
| clearEditForm() { |
| const editArea = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_edit'); |
| editArea.style.display = 'none'; |
|
|
| const nameField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_name')); |
| const roleField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_role')); |
| const promptField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_prompt')); |
| const injectionPositionField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_position')); |
| const injectionDepthField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_depth')); |
| const injectionDepthBlock = (document.getElementById(this.configuration.prefix + 'prompt_manager_depth_block')); |
| const injectionOrderBlock = (document.getElementById(this.configuration.prefix + 'prompt_manager_order_block')); |
| const injectionOrderField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_order')); |
| const injectionTriggerField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_trigger')); |
| const forbidOverridesField = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_forbid_overrides')); |
| const forbidOverridesBlock = (document.getElementById(this.configuration.prefix + 'prompt_manager_forbid_overrides_block')); |
| const entrySourceBlock = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_source_block')); |
| const entrySource = (document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_source')); |
|
|
| nameField.value = ''; |
| roleField.selectedIndex = 0; |
| promptField.value = ''; |
| promptField.disabled = false; |
| injectionPositionField.selectedIndex = 0; |
| injectionPositionField.removeAttribute('disabled'); |
| injectionDepthField.value = DEFAULT_DEPTH.toString(); |
| injectionOrderField.value = DEFAULT_ORDER.toString(); |
| injectionTriggerField.value = ''; |
| injectionDepthBlock.style.visibility = 'unset'; |
| injectionOrderBlock.style.visibility = 'unset'; |
| forbidOverridesBlock.style.visibility = 'unset'; |
| forbidOverridesField.checked = false; |
| entrySourceBlock.style.display = 'none'; |
| entrySource.textContent = ''; |
|
|
| roleField.disabled = false; |
| } |
|
|
| clearInspectForm() { |
| const inspectArea = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_inspect'); |
| inspectArea.style.display = 'none'; |
| const messageList = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_inspect_list'); |
| messageList.innerHTML = ''; |
| } |
|
|
| |
| |
| |
| |
| |
| getPromptCollection(generationType) { |
| generationType = String(generationType || 'normal').toLowerCase().trim(); |
| const promptCollection = new PromptCollection(); |
| const promptOrder = this.getPromptOrderForCharacter(this.activeCharacter); |
|
|
| promptOrder.forEach(entry => { |
| const prompt = this.getPromptById(entry.identifier); |
| const allowedTrigger = entry.enabled && this.shouldTrigger(prompt, generationType); |
|
|
| if (!prompt) { |
| return; |
| } |
|
|
| if (allowedTrigger) { |
| promptCollection.add(this.preparePrompt(prompt)); |
| } else if (entry.identifier === 'main') { |
| |
| |
| const replacementPrompt = structuredClone(prompt); |
| replacementPrompt.content = ''; |
| promptCollection.add(this.preparePrompt(replacementPrompt)); |
| } |
| }); |
|
|
| return promptCollection; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| shouldTrigger(prompt, generationType) { |
| if (!Array.isArray(prompt?.injection_trigger)) return true; |
| if (!prompt.injection_trigger.length) return true; |
| return prompt.injection_trigger.includes(generationType); |
| } |
|
|
| |
| |
| |
| |
| |
| setMessages(messages) { |
| this.messages = messages; |
| } |
|
|
| |
| |
| |
| |
| |
| setChatCompletion(chatCompletion) { |
| const messages = chatCompletion.getMessages(); |
|
|
| this.setMessages(messages); |
| this.populateTokenCounts(messages); |
| this.overriddenPrompts = chatCompletion.getOverriddenPrompts(); |
| } |
|
|
| |
| |
| |
| |
| |
| populateTokenCounts(messages) { |
| this.tokenHandler.resetCounts(); |
| const counts = this.tokenHandler.getCounts(); |
| messages.getCollection().forEach(message => { |
| counts[message.identifier] = message.getTokens(); |
| }); |
|
|
| this.tokenUsage = this.tokenHandler.getTotal(); |
|
|
| this.log('Updated token usage with ' + this.tokenUsage); |
| } |
|
|
| |
| |
| |
| async renderPromptManager() { |
| let selectedPromptIndex = 0; |
| const existingAppendSelect = document.getElementById(`${this.configuration.prefix}prompt_manager_footer_append_prompt`); |
| if (existingAppendSelect instanceof HTMLSelectElement) { |
| selectedPromptIndex = existingAppendSelect.selectedIndex; |
| } |
| const promptManagerDiv = this.containerElement; |
| promptManagerDiv.innerHTML = ''; |
|
|
| const errorDiv = this.error ? ` |
| <div class="${this.configuration.prefix}prompt_manager_error"> |
| <span class="fa-solid tooltip fa-triangle-exclamation text_danger"></span> ${DOMPurify.sanitize(this.error)} |
| </div> |
| ` : ''; |
|
|
| const totalActiveTokens = this.tokenUsage; |
|
|
| const headerHtml = await renderTemplateAsync('promptManagerHeader', { error: this.error, errorDiv, prefix: this.configuration.prefix, totalActiveTokens }); |
| promptManagerDiv.insertAdjacentHTML('beforeend', headerHtml); |
|
|
| this.listElement = promptManagerDiv.querySelector(`#${this.configuration.prefix}prompt_manager_list`); |
|
|
| if (null !== this.activeCharacter) { |
| const prompts = [...this.serviceSettings.prompts] |
| .filter(prompt => prompt && !prompt?.system_prompt) |
| .sort((promptA, promptB) => promptA.name.localeCompare(promptB.name)); |
| const promptsHtml = prompts.reduce((acc, prompt) => acc + `<option value="${prompt.identifier}">${escapeHtml(prompt.name)}</option>`, ''); |
|
|
| if (selectedPromptIndex > 0) { |
| selectedPromptIndex = Math.min(selectedPromptIndex, prompts.length - 1); |
| } |
|
|
| if (selectedPromptIndex === -1 && prompts.length) { |
| selectedPromptIndex = 0; |
| } |
|
|
| const rangeBlockDiv = promptManagerDiv.querySelector('.range-block'); |
| const headerDiv = promptManagerDiv.querySelector('.completion_prompt_manager_header'); |
| const footerHtml = await renderTemplateAsync('promptManagerFooter', { promptsHtml, prefix: this.configuration.prefix }); |
| headerDiv.insertAdjacentHTML('afterend', footerHtml); |
| rangeBlockDiv.querySelector('#prompt-manager-reset-character').addEventListener('click', this.handleCharacterReset); |
|
|
| const footerDiv = rangeBlockDiv.querySelector(`.${this.configuration.prefix}prompt_manager_footer`); |
| footerDiv.querySelector('.menu_button:nth-child(2)').addEventListener('click', this.handleAppendPrompt); |
| footerDiv.querySelector('.caution').addEventListener('click', this.handleDeletePrompt); |
| footerDiv.querySelector('.menu_button:last-child').addEventListener('click', this.handleNewPrompt); |
| footerDiv.querySelector('select').selectedIndex = selectedPromptIndex; |
|
|
| |
| footerDiv.querySelector('#prompt-manager-import').addEventListener('click', this.handleImport); |
| footerDiv.querySelector('#prompt-manager-export').addEventListener('click', this.handleFullExport); |
| } |
| } |
|
|
| |
| |
| |
| async renderPromptManagerListItems() { |
| if (!this.serviceSettings.prompts) return; |
|
|
| const promptManagerList = this.listElement; |
| promptManagerList.innerHTML = ''; |
|
|
| const { prefix } = this.configuration; |
|
|
| let listItemHtml = await renderTemplateAsync('promptManagerListHeader', { prefix }); |
|
|
| this.getPromptsForCharacter(this.activeCharacter).forEach(prompt => { |
| if (!prompt) return; |
|
|
| const listEntry = this.getPromptOrderEntry(this.activeCharacter, prompt.identifier); |
| const enabledClass = listEntry.enabled ? '' : `${prefix}prompt_manager_prompt_disabled`; |
| const draggableClass = `${prefix}prompt_manager_prompt_draggable`; |
| const markerClass = prompt.marker ? `${prefix}prompt_manager_marker` : ''; |
| const tokens = this.tokenHandler?.getCounts()[prompt.identifier] ?? 0; |
|
|
| |
| let warningClass = ''; |
| let warningTitle = ''; |
|
|
| const tokenBudget = this.serviceSettings.openai_max_context - this.serviceSettings.openai_max_tokens; |
| if (this.tokenUsage > tokenBudget * 0.8 && |
| 'chatHistory' === prompt.identifier) { |
| const warningThreshold = this.configuration.warningTokenThreshold; |
| const dangerThreshold = this.configuration.dangerTokenThreshold; |
|
|
| if (tokens <= dangerThreshold) { |
| warningClass = 'fa-solid tooltip fa-triangle-exclamation text_danger'; |
| warningTitle = 'Very little of your chat history is being sent, consider deactivating some other prompts.'; |
| } else if (tokens <= warningThreshold) { |
| warningClass = 'fa-solid tooltip fa-triangle-exclamation text_warning'; |
| warningTitle = 'Only a few messages worth chat history are being sent.'; |
| } |
| } |
|
|
| const calculatedTokens = tokens ? tokens : '-'; |
|
|
| let detachSpanHtml = ''; |
| if (this.isPromptDeletionAllowed(prompt)) { |
| detachSpanHtml = ` |
| <span title="Remove" class="prompt-manager-detach-action caution fa-solid fa-chain-broken fa-xs"></span> |
| `; |
| } else { |
| detachSpanHtml = '<span class="fa-solid"></span>'; |
| } |
|
|
| let editSpanHtml = ''; |
| if (this.isPromptEditAllowed(prompt)) { |
| editSpanHtml = ` |
| <span title="edit" class="prompt-manager-edit-action fa-solid fa-pencil fa-xs"></span> |
| `; |
| } else { |
| editSpanHtml = '<span class="fa-solid"></span>'; |
| } |
|
|
| let toggleSpanHtml = ''; |
| if (this.isPromptToggleAllowed(prompt)) { |
| toggleSpanHtml = ` |
| <span class="prompt-manager-toggle-action ${listEntry.enabled ? 'fa-solid fa-toggle-on' : 'fa-solid fa-toggle-off'}"></span> |
| `; |
| } else { |
| toggleSpanHtml = '<span class="fa-solid"></span>'; |
| } |
|
|
| const encodedName = escapeHtml(prompt.name); |
| const isMarkerPrompt = prompt.marker && prompt.injection_position !== INJECTION_POSITION.ABSOLUTE; |
| const isSystemPrompt = !prompt.marker && prompt.system_prompt && prompt.injection_position !== INJECTION_POSITION.ABSOLUTE && !prompt.forbid_overrides; |
| const isImportantPrompt = !prompt.marker && prompt.system_prompt && prompt.injection_position !== INJECTION_POSITION.ABSOLUTE && prompt.forbid_overrides; |
| const isUserPrompt = !prompt.marker && !prompt.system_prompt && prompt.injection_position !== INJECTION_POSITION.ABSOLUTE; |
| const isInjectionPrompt = prompt.injection_position === INJECTION_POSITION.ABSOLUTE; |
| const isOverriddenPrompt = Array.isArray(this.overriddenPrompts) && this.overriddenPrompts.includes(prompt.identifier); |
| const importantClass = isImportantPrompt ? `${prefix}prompt_manager_important` : ''; |
| const iconLookup = prompt.role === 'system' && (prompt.marker || prompt.system_prompt) ? '' : prompt.role; |
|
|
| |
| const promptRoles = { |
| assistant: { roleIcon: 'fa-robot', roleTitle: 'Prompt will be sent as Assistant' }, |
| user: { roleIcon: 'fa-user', roleTitle: 'Prompt will be sent as User' }, |
| }; |
| const roleIcon = promptRoles[iconLookup]?.roleIcon || ''; |
| const roleTitle = promptRoles[iconLookup]?.roleTitle || ''; |
|
|
| listItemHtml += ` |
| <li class="${prefix}prompt_manager_prompt ${draggableClass} ${enabledClass} ${markerClass} ${importantClass}" data-pm-identifier="${escapeHtml(prompt.identifier)}"> |
| <span class="drag-handle">☰</span> |
| <span class="${prefix}prompt_manager_prompt_name" data-pm-name="${encodedName}"> |
| ${isMarkerPrompt ? '<span class="fa-fw fa-solid fa-thumb-tack" title="Marker"></span>' : ''} |
| ${isSystemPrompt ? '<span class="fa-fw fa-solid fa-square-poll-horizontal" title="Global Prompt"></span>' : ''} |
| ${isImportantPrompt ? '<span class="fa-fw fa-solid fa-star" title="Important Prompt"></span>' : ''} |
| ${isUserPrompt ? '<span class="fa-fw fa-solid fa-asterisk" title="Preset Prompt"></span>' : ''} |
| ${isInjectionPrompt ? '<span class="fa-fw fa-solid fa-syringe" title="In-Chat Injection"></span>' : ''} |
| ${this.isPromptInspectionAllowed(prompt) ? `<a title="${encodedName}" class="prompt-manager-inspect-action">${encodedName}</a>` : `<span title="${encodedName}">${encodedName}</span>`} |
| ${roleIcon ? `<span data-role="${escapeHtml(prompt.role)}" class="fa-xs fa-solid ${roleIcon}" title="${roleTitle}"></span>` : ''} |
| ${isInjectionPrompt ? `<small class="prompt-manager-injection-depth">@ ${escapeHtml(prompt.injection_depth.toString())}</small>` : ''} |
| ${isOverriddenPrompt ? '<small class="fa-solid fa-address-card prompt-manager-overridden" title="Pulled from a character card"></small>' : ''} |
| </span> |
| <span> |
| <span class="prompt_manager_prompt_controls"> |
| ${detachSpanHtml} |
| ${editSpanHtml} |
| ${toggleSpanHtml} |
| </span> |
| </span> |
| |
| <span class="prompt_manager_prompt_tokens" data-pm-tokens="${calculatedTokens}"><span class="${warningClass}" title="${warningTitle}"> </span>${calculatedTokens}</span> |
| </li> |
| `; |
| }); |
|
|
| promptManagerList.insertAdjacentHTML('beforeend', listItemHtml); |
|
|
| |
| Array.from(promptManagerList.getElementsByClassName('prompt-manager-detach-action')).forEach(el => { |
| el.addEventListener('click', this.handleDetach); |
| }); |
|
|
| Array.from(promptManagerList.getElementsByClassName('prompt-manager-inspect-action')).forEach(el => { |
| el.addEventListener('click', this.handleInspect); |
| }); |
|
|
| Array.from(promptManagerList.getElementsByClassName('prompt-manager-edit-action')).forEach(el => { |
| el.addEventListener('click', this.handleEdit); |
| }); |
|
|
| Array.from(promptManagerList.querySelectorAll('.prompt-manager-toggle-action')).forEach(el => { |
| el.addEventListener('click', this.handleToggle); |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export(data, type, name = 'export') { |
| const promptExport = { |
| version: this.configuration.version, |
| type: type, |
| data: data, |
| }; |
|
|
| const serializedObject = JSON.stringify(promptExport, null, 4); |
| const blob = new Blob([serializedObject], { type: 'application/json' }); |
| const url = URL.createObjectURL(blob); |
| const downloadLink = document.createElement('a'); |
| downloadLink.href = url; |
|
|
| const dateString = this.getFormattedDate(); |
| downloadLink.download = `${name}-${dateString}.json`; |
|
|
| downloadLink.click(); |
|
|
| URL.revokeObjectURL(url); |
| } |
|
|
| |
| |
| |
| |
| |
| import(importData) { |
| const mergeKeepNewer = (prompts, newPrompts) => { |
| let merged = [...prompts, ...newPrompts]; |
|
|
| let map = new Map(); |
| for (let obj of merged) { |
| map.set(obj.identifier, obj); |
| } |
|
|
| merged = Array.from(map.values()); |
|
|
| return merged; |
| }; |
|
|
| const controlObj = { |
| version: 1, |
| type: '', |
| data: { |
| prompts: [], |
| prompt_order: null, |
| }, |
| }; |
|
|
| if (false === this.validateObject(controlObj, importData)) { |
| toastr.warning(t`Could not import prompts. Export failed validation.`); |
| return; |
| } |
|
|
| const prompts = mergeKeepNewer(this.serviceSettings.prompts, importData.data.prompts); |
|
|
| this.setPrompts(prompts); |
| this.log('Prompt import succeeded'); |
|
|
| 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 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(t`Prompt import complete.`); |
| this.saveServiceSettings().then(() => this.render()); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| validateObject(controlObj, object) { |
| for (let key in controlObj) { |
| if (!Object.hasOwn(object, key)) { |
| if (controlObj[key] === null) continue; |
| else return false; |
| } |
|
|
| if (typeof controlObj[key] === 'object' && controlObj[key] !== null) { |
| if (typeof object[key] !== 'object') return false; |
| if (!this.validateObject(controlObj[key], object[key])) return false; |
| } else { |
| if (typeof object[key] !== typeof controlObj[key]) return false; |
| } |
| } |
|
|
| return true; |
| } |
|
|
| |
| |
| |
| |
| |
| getFormattedDate() { |
| const date = new Date(); |
| let month = String(date.getMonth() + 1); |
| let day = String(date.getDate()); |
| const year = String(date.getFullYear()); |
|
|
| if (month.length < 2) month = '0' + month; |
| if (day.length < 2) day = '0' + day; |
|
|
| return `${month}_${day}_${year}`; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| makeDraggable() { |
| $(`#${this.configuration.prefix}prompt_manager_list`).sortable({ |
| delay: this.configuration.sortableDelay, |
| handle: isMobile() ? '.drag-handle' : null, |
| items: `.${this.configuration.prefix}prompt_manager_prompt_draggable`, |
| update: (event, ui) => { |
| const promptOrder = this.getPromptOrderForCharacter(this.activeCharacter); |
| const promptListElement = $(`#${this.configuration.prefix}prompt_manager_list`).sortable('toArray', { attribute: 'data-pm-identifier' }); |
| const idToObjectMap = new Map(promptOrder.map(prompt => [prompt.identifier, prompt])); |
| const updatedPromptOrder = promptListElement.map(identifier => idToObjectMap.get(identifier)); |
|
|
| this.removePromptOrderForCharacter(this.activeCharacter); |
| this.addPromptOrderForCharacter(this.activeCharacter, updatedPromptOrder); |
|
|
| this.log(`Prompt order updated for ${this.activeCharacter.name}.`); |
|
|
| this.saveServiceSettings(); |
| }, |
| }); |
| } |
|
|
| |
| |
| |
| |
| showPopup(area = 'edit') { |
| const areaElement = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_' + area); |
| areaElement.style.display = 'flex'; |
|
|
| $('#' + this.configuration.prefix + 'prompt_manager_popup').first() |
| .slideDown(200, 'swing') |
| .addClass('openDrawer'); |
| } |
|
|
| |
| |
| |
| |
| hidePopup() { |
| $('#' + this.configuration.prefix + 'prompt_manager_popup').first() |
| .slideUp(200, 'swing') |
| .removeClass('openDrawer'); |
| } |
|
|
| |
| |
| |
| |
| getUuidv4() { |
| return uuidv4(); |
| } |
|
|
| |
| |
| |
| |
| |
| log(output) { |
| if (power_user.console_log_prompts) console.log('[PromptManager] ' + output); |
| } |
|
|
| |
| |
| |
| |
| |
| profileStart(identifier) { |
| if (power_user.console_log_prompts) console.time(identifier); |
| } |
|
|
| |
| |
| |
| |
| |
| profileEnd(identifier) { |
| if (power_user.console_log_prompts) { |
| this.log('Profiling of "' + identifier + '" finished. Result below.'); |
| console.timeEnd(identifier); |
| } |
| } |
| } |
|
|
| const chatCompletionDefaultPrompts = { |
| 'prompts': [ |
| { |
| 'name': 'Main Prompt', |
| 'system_prompt': true, |
| 'role': 'system', |
| 'content': 'Write {{char}}\'s next reply in a fictional chat between {{charIfNotGroup}} and {{user}}.', |
| 'identifier': 'main', |
| }, |
| { |
| 'name': 'Auxiliary Prompt', |
| 'system_prompt': true, |
| 'role': 'system', |
| 'content': '', |
| 'identifier': 'nsfw', |
| }, |
| { |
| 'identifier': 'dialogueExamples', |
| 'name': 'Chat Examples', |
| 'system_prompt': true, |
| 'marker': true, |
| }, |
| { |
| 'name': 'Post-History Instructions', |
| 'system_prompt': true, |
| 'role': 'system', |
| 'content': '', |
| 'identifier': 'jailbreak', |
| }, |
| { |
| 'identifier': 'chatHistory', |
| 'name': 'Chat History', |
| 'system_prompt': true, |
| 'marker': true, |
| }, |
| { |
| 'identifier': 'worldInfoAfter', |
| 'name': 'World Info (after)', |
| 'system_prompt': true, |
| 'marker': true, |
| }, |
| { |
| 'identifier': 'worldInfoBefore', |
| 'name': 'World Info (before)', |
| 'system_prompt': true, |
| 'marker': true, |
| }, |
| { |
| 'identifier': 'enhanceDefinitions', |
| 'role': 'system', |
| 'name': 'Enhance Definitions', |
| 'content': 'If you have more knowledge of {{char}}, add to the character\'s lore and personality to enhance them but keep the Character Sheet\'s definitions absolute.', |
| 'system_prompt': true, |
| 'marker': false, |
| }, |
| { |
| 'identifier': 'charDescription', |
| 'name': 'Char Description', |
| 'system_prompt': true, |
| 'marker': true, |
| }, |
| { |
| 'identifier': 'charPersonality', |
| 'name': 'Char Personality', |
| 'system_prompt': true, |
| 'marker': true, |
| }, |
| { |
| 'identifier': 'scenario', |
| 'name': 'Scenario', |
| 'system_prompt': true, |
| 'marker': true, |
| }, |
| { |
| 'identifier': 'personaDescription', |
| 'name': 'Persona Description', |
| 'system_prompt': true, |
| 'marker': true, |
| }, |
| ], |
| }; |
|
|
| const promptManagerDefaultPromptOrders = { |
| 'prompt_order': [], |
| }; |
|
|
| const promptManagerDefaultPromptOrder = [ |
| { |
| 'identifier': 'main', |
| 'enabled': true, |
| }, |
| { |
| 'identifier': 'worldInfoBefore', |
| 'enabled': true, |
| }, |
| { |
| 'identifier': 'personaDescription', |
| 'enabled': true, |
| }, |
| { |
| 'identifier': 'charDescription', |
| 'enabled': true, |
| }, |
| { |
| 'identifier': 'charPersonality', |
| 'enabled': true, |
| }, |
| { |
| 'identifier': 'scenario', |
| 'enabled': true, |
| }, |
| { |
| 'identifier': 'enhanceDefinitions', |
| 'enabled': false, |
| }, |
| { |
| 'identifier': 'nsfw', |
| 'enabled': true, |
| }, |
| { |
| 'identifier': 'worldInfoAfter', |
| 'enabled': true, |
| }, |
| { |
| 'identifier': 'dialogueExamples', |
| 'enabled': true, |
| }, |
| { |
| 'identifier': 'chatHistory', |
| 'enabled': true, |
| }, |
| { |
| 'identifier': 'jailbreak', |
| 'enabled': true, |
| }, |
| ]; |
|
|
| export { |
| PromptManager, |
| registerPromptManagerMigration, |
| chatCompletionDefaultPrompts, |
| promptManagerDefaultPromptOrders, |
| Prompt, |
| }; |
|
|