Turn PromptManagerModule into a class
This commit is contained in:
parent
676cc7731e
commit
5c175bc647
|
@ -178,7 +178,8 @@ class PromptCollection {
|
|||
}
|
||||
}
|
||||
|
||||
function PromptManagerModule() {
|
||||
class PromptManagerModule {
|
||||
constructor() {
|
||||
this.systemPrompts = [
|
||||
'main',
|
||||
'nsfw',
|
||||
|
@ -281,6 +282,7 @@ function PromptManagerModule() {
|
|||
this.renderDebounced = debounce(this.render.bind(this), 1000);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Initializes the PromptManagerModule with provided configuration and service settings.
|
||||
*
|
||||
|
@ -290,7 +292,7 @@ function PromptManagerModule() {
|
|||
* @param {Object} moduleConfiguration - Configuration object for the PromptManagerModule.
|
||||
* @param {Object} serviceSettings - Service settings object for the PromptManagerModule.
|
||||
*/
|
||||
PromptManagerModule.prototype.init = function (moduleConfiguration, serviceSettings) {
|
||||
init(moduleConfiguration, serviceSettings) {
|
||||
this.configuration = Object.assign(this.configuration, moduleConfiguration);
|
||||
this.tokenHandler = this.tokenHandler || new TokenHandler();
|
||||
this.serviceSettings = serviceSettings;
|
||||
|
@ -654,14 +656,14 @@ PromptManagerModule.prototype.init = function (moduleConfiguration, serviceSetti
|
|||
eventSource.on(event_types.WORLDINFO_SETTINGS_UPDATED, () => this.renderDebounced());
|
||||
|
||||
this.log('Initialized');
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Main rendering function
|
||||
*
|
||||
* @param afterTryGenerate - Whether a dry run should be attempted before rendering
|
||||
*/
|
||||
PromptManagerModule.prototype.render = function (afterTryGenerate = true) {
|
||||
render(afterTryGenerate = true) {
|
||||
if (main_api !== 'openai') return;
|
||||
|
||||
if ('character' === this.configuration.promptOrder.strategy && null === this.activeCharacter) return;
|
||||
|
@ -690,20 +692,20 @@ PromptManagerModule.prototype.render = function (afterTryGenerate = true) {
|
|||
}).catch(() => {
|
||||
console.log('Timeout while waiting for send press to be false');
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a prompt with the values from the HTML form.
|
||||
* @param {object} prompt - The prompt to be updated.
|
||||
* @returns {void}
|
||||
*/
|
||||
PromptManagerModule.prototype.updatePromptWithPromptEditForm = function (prompt) {
|
||||
updatePromptWithPromptEditForm(prompt) {
|
||||
prompt.name = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_name').value;
|
||||
prompt.role = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_role').value;
|
||||
prompt.content = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_prompt').value;
|
||||
prompt.injection_position = Number(document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_position').value);
|
||||
prompt.injection_depth = Number(document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_depth').value);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a prompt by its identifier and update it with the provided object.
|
||||
|
@ -711,32 +713,32 @@ PromptManagerModule.prototype.updatePromptWithPromptEditForm = function (prompt)
|
|||
* @param {object} updatePrompt - An object with properties to be updated in the prompt.
|
||||
* @returns {void}
|
||||
*/
|
||||
PromptManagerModule.prototype.updatePromptByIdentifier = function (identifier, updatePrompt) {
|
||||
updatePromptByIdentifier(identifier, updatePrompt) {
|
||||
let prompt = this.serviceSettings.prompts.find((item) => identifier === item.identifier);
|
||||
if (prompt) prompt = Object.assign(prompt, updatePrompt);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterate over an array of prompts, find each one by its identifier, and update them with the provided data.
|
||||
* @param {object[]} prompts - An array of prompt updates.
|
||||
* @returns {void}
|
||||
*/
|
||||
PromptManagerModule.prototype.updatePrompts = function (prompts) {
|
||||
updatePrompts(prompts) {
|
||||
prompts.forEach((update) => {
|
||||
let prompt = this.getPromptById(update.identifier);
|
||||
if (prompt) Object.assign(prompt, update);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
PromptManagerModule.prototype.getTokenHandler = function () {
|
||||
getTokenHandler() {
|
||||
return this.tokenHandler;
|
||||
};
|
||||
}
|
||||
|
||||
PromptManagerModule.prototype.isPromptDisabledForActiveCharacter = function (identifier) {
|
||||
isPromptDisabledForActiveCharacter(identifier) {
|
||||
const promptOrderEntry = this.getPromptOrderEntry(this.activeCharacter, identifier);
|
||||
if (promptOrderEntry) return !promptOrderEntry.enabled;
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a prompt to the current character's prompt list.
|
||||
|
@ -744,12 +746,12 @@ PromptManagerModule.prototype.isPromptDisabledForActiveCharacter = function (ide
|
|||
* @param {object} character - The character whose prompt list will be updated.
|
||||
* @returns {void}
|
||||
*/
|
||||
PromptManagerModule.prototype.appendPrompt = function (prompt, character) {
|
||||
appendPrompt(prompt, character) {
|
||||
const promptOrder = this.getPromptOrderForCharacter(character);
|
||||
const index = promptOrder.findIndex(entry => entry.identifier === prompt.identifier);
|
||||
|
||||
if (-1 === index) promptOrder.push({ identifier: prompt.identifier, enabled: false });
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a prompt from the current character's prompt list.
|
||||
|
@ -758,12 +760,12 @@ PromptManagerModule.prototype.appendPrompt = function (prompt, character) {
|
|||
* @returns {void}
|
||||
*/
|
||||
// Remove a prompt from the current characters prompt list
|
||||
PromptManagerModule.prototype.detachPrompt = function (prompt, character) {
|
||||
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);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new prompt and add it to the list of prompts.
|
||||
|
@ -771,7 +773,7 @@ PromptManagerModule.prototype.detachPrompt = function (prompt, character) {
|
|||
* @param {string} identifier - The identifier for the new prompt.
|
||||
* @returns {void}
|
||||
*/
|
||||
PromptManagerModule.prototype.addPrompt = function (prompt, identifier) {
|
||||
addPrompt(prompt, identifier) {
|
||||
|
||||
if (typeof prompt !== 'object' || prompt === null) throw new Error('Object is not a prompt');
|
||||
|
||||
|
@ -784,13 +786,13 @@ PromptManagerModule.prototype.addPrompt = function (prompt, identifier) {
|
|||
};
|
||||
|
||||
this.serviceSettings.prompts.push(newPrompt);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize the service settings, ensuring each prompt has a unique identifier.
|
||||
* @returns {void}
|
||||
*/
|
||||
PromptManagerModule.prototype.sanitizeServiceSettings = function () {
|
||||
sanitizeServiceSettings() {
|
||||
this.serviceSettings.prompts = this.serviceSettings.prompts ?? [];
|
||||
this.serviceSettings.prompt_order = this.serviceSettings.prompt_order ?? [];
|
||||
|
||||
|
@ -819,7 +821,7 @@ PromptManagerModule.prototype.sanitizeServiceSettings = function () {
|
|||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether entries of a characters prompt order are orphaned
|
||||
|
@ -827,7 +829,7 @@ PromptManagerModule.prototype.sanitizeServiceSettings = function () {
|
|||
*
|
||||
* @param prompts
|
||||
*/
|
||||
PromptManagerModule.prototype.checkForMissingPrompts = function (prompts) {
|
||||
checkForMissingPrompts(prompts) {
|
||||
const defaultPromptIdentifiers = chatCompletionDefaultPrompts.prompts.reduce((list, prompt) => { list.push(prompt.identifier); return list; }, []);
|
||||
|
||||
const missingIdentifiers = defaultPromptIdentifiers.filter(identifier =>
|
||||
|
@ -841,62 +843,62 @@ PromptManagerModule.prototype.checkForMissingPrompts = function (prompts) {
|
|||
this.log(`Missing system prompt: ${defaultPrompt.identifier}. Added default.`);
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.isPromptInspectionAllowed = function (prompt) {
|
||||
isPromptInspectionAllowed(prompt) {
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a prompt can be deleted. System prompts cannot be deleted.
|
||||
* @param {object} prompt - The prompt to check.
|
||||
* @returns {boolean} True if the prompt can be deleted, false otherwise.
|
||||
*/
|
||||
PromptManagerModule.prototype.isPromptDeletionAllowed = function (prompt) {
|
||||
isPromptDeletionAllowed(prompt) {
|
||||
return false === prompt.system_prompt;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a prompt can be edited.
|
||||
* @param {object} prompt - The prompt to check.
|
||||
* @returns {boolean} True if the prompt can be edited, false otherwise.
|
||||
*/
|
||||
PromptManagerModule.prototype.isPromptEditAllowed = function (prompt) {
|
||||
isPromptEditAllowed(prompt) {
|
||||
return !prompt.marker;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a prompt can be toggled on or off.
|
||||
* @param {object} prompt - The prompt to check.
|
||||
* @returns {boolean} True if the prompt can be deleted, false otherwise.
|
||||
*/
|
||||
PromptManagerModule.prototype.isPromptToggleAllowed = function (prompt) {
|
||||
isPromptToggleAllowed(prompt) {
|
||||
const forceTogglePrompts = ['charDescription', 'charPersonality', 'scenario', 'personaDescription', 'worldInfoBefore', 'worldInfoAfter'];
|
||||
return prompt.marker && !forceTogglePrompts.includes(prompt.identifier) ? false : !this.configuration.toggleDisabled.includes(prompt.identifier);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 void
|
||||
*/
|
||||
PromptManagerModule.prototype.handleCharacterDeleted = function (event) {
|
||||
handleCharacterDeleted(event) {
|
||||
if ('global' === this.configuration.promptOrder.strategy) return;
|
||||
this.removePromptOrderForCharacter(this.activeCharacter);
|
||||
if (this.activeCharacter.id === event.detail.id) this.activeCharacter = null;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the selection of a character by setting them as the active character and setting up their prompt list if necessary.
|
||||
* @param {object} event - The event object containing the character's ID and character data.
|
||||
* @returns {void}
|
||||
*/
|
||||
PromptManagerModule.prototype.handleCharacterSelected = function (event) {
|
||||
handleCharacterSelected(event) {
|
||||
if ('global' === this.configuration.promptOrder.strategy) {
|
||||
this.activeCharacter = { id: this.configuration.promptOrder.dummyId };
|
||||
} else if ('character' === this.configuration.promptOrder.strategy) {
|
||||
|
@ -910,14 +912,14 @@ PromptManagerModule.prototype.handleCharacterSelected = function (event) {
|
|||
} else {
|
||||
throw new Error('Unsupported prompt order mode.');
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the most recently selected character
|
||||
*
|
||||
* @param event
|
||||
*/
|
||||
PromptManagerModule.prototype.handleCharacterUpdated = function (event) {
|
||||
handleCharacterUpdated(event) {
|
||||
if ('global' === this.configuration.promptOrder.strategy) {
|
||||
this.activeCharacter = { id: this.configuration.promptOrder.dummyId };
|
||||
} else if ('character' === this.configuration.promptOrder.strategy) {
|
||||
|
@ -925,14 +927,14 @@ PromptManagerModule.prototype.handleCharacterUpdated = function (event) {
|
|||
} else {
|
||||
throw new Error('Prompt order strategy not supported.');
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the most recently selected character group
|
||||
*
|
||||
* @param event
|
||||
*/
|
||||
PromptManagerModule.prototype.handleGroupSelected = function (event) {
|
||||
handleGroupSelected(event) {
|
||||
if ('global' === this.configuration.promptOrder.strategy) {
|
||||
this.activeCharacter = { id: this.configuration.promptOrder.dummyId };
|
||||
} else if ('character' === this.configuration.promptOrder.strategy) {
|
||||
|
@ -944,17 +946,17 @@ PromptManagerModule.prototype.handleGroupSelected = function (event) {
|
|||
} else {
|
||||
throw new Error('Prompt order strategy not supported.');
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of group characters, regardless of whether they are active or not.
|
||||
*
|
||||
* @returns {string[]}
|
||||
*/
|
||||
PromptManagerModule.prototype.getActiveGroupCharacters = function () {
|
||||
getActiveGroupCharacters() {
|
||||
// ToDo: Ideally, this should return the actual characters.
|
||||
return (this.activeCharacter?.group?.members || []).map(member => member && member.substring(0, member.lastIndexOf('.')));
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the prompts for a specific character. Can be filtered to only include enabled prompts.
|
||||
|
@ -962,51 +964,51 @@ PromptManagerModule.prototype.getActiveGroupCharacters = function () {
|
|||
* @param character
|
||||
* @param onlyEnabled
|
||||
*/
|
||||
PromptManagerModule.prototype.getPromptsForCharacter = function (character, onlyEnabled = false) {
|
||||
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);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the order of prompts for a specific character. If no character is specified or the character doesn't have a prompt list, an empty array is returned.
|
||||
* @param {object|null} character - The character to get the prompt list for.
|
||||
* @returns {object[]} The prompt list for the character, or an empty array.
|
||||
*/
|
||||
PromptManagerModule.prototype.getPromptOrderForCharacter = function (character) {
|
||||
getPromptOrderForCharacter(character) {
|
||||
return !character ? [] : (this.serviceSettings.prompt_order.find(list => String(list.character_id) === String(character.id))?.order ?? []);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the prompts for the manager.
|
||||
* @param {object[]} prompts - The prompts to be set.
|
||||
* @returns {void}
|
||||
*/
|
||||
PromptManagerModule.prototype.setPrompts = function (prompts) {
|
||||
setPrompts(prompts) {
|
||||
this.serviceSettings.prompts = prompts;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the prompt list for a specific character.
|
||||
* @param {object} character - The character whose prompt list will be removed.
|
||||
* @returns {void}
|
||||
*/
|
||||
PromptManagerModule.prototype.removePromptOrderForCharacter = function (character) {
|
||||
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);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new prompt list for a specific character.
|
||||
* @param {Object} character - Object with at least an `id` property
|
||||
* @param {Array<Object>} promptOrder - Array of prompt objects
|
||||
*/
|
||||
PromptManagerModule.prototype.addPromptOrderForCharacter = function (character, promptOrder) {
|
||||
addPromptOrderForCharacter(character, promptOrder) {
|
||||
this.serviceSettings.prompt_order.push({
|
||||
character_id: character.id,
|
||||
order: JSON.parse(JSON.stringify(promptOrder)),
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches for a prompt list entry for a given character and identifier.
|
||||
|
@ -1014,27 +1016,27 @@ PromptManagerModule.prototype.addPromptOrderForCharacter = function (character,
|
|||
* @param {string} identifier - Identifier of the prompt list entry
|
||||
* @returns {Object|null} The prompt list entry object, or null if not found
|
||||
*/
|
||||
PromptManagerModule.prototype.getPromptOrderEntry = function (character, identifier) {
|
||||
getPromptOrderEntry(character, identifier) {
|
||||
return this.getPromptOrderForCharacter(character).find(entry => entry.identifier === identifier) ?? null;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds and returns a prompt by its identifier.
|
||||
* @param {string} identifier - Identifier of the prompt
|
||||
* @returns {Object|null} The prompt object, or null if not found
|
||||
*/
|
||||
PromptManagerModule.prototype.getPromptById = function (identifier) {
|
||||
getPromptById(identifier) {
|
||||
return this.serviceSettings.prompts.find(item => item && item.identifier === identifier) ?? null;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds and returns the index of a prompt by its identifier.
|
||||
* @param {string} identifier - Identifier of the prompt
|
||||
* @returns {number|null} Index of the prompt, or null if not found
|
||||
*/
|
||||
PromptManagerModule.prototype.getPromptIndexById = function (identifier) {
|
||||
getPromptIndexById(identifier) {
|
||||
return this.serviceSettings.prompts.findIndex(item => item.identifier === identifier) ?? null;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Enriches a generic object, creating a new prompt object in the process
|
||||
|
@ -1043,7 +1045,7 @@ PromptManagerModule.prototype.getPromptIndexById = function (identifier) {
|
|||
* @param original
|
||||
* @returns {Object} An object with "role" and "content" properties
|
||||
*/
|
||||
PromptManagerModule.prototype.preparePrompt = function (prompt, original = null) {
|
||||
preparePrompt(prompt, original = null) {
|
||||
const groupMembers = this.getActiveGroupCharacters();
|
||||
const preparedPrompt = new Prompt(prompt);
|
||||
|
||||
|
@ -1056,7 +1058,7 @@ PromptManagerModule.prototype.preparePrompt = function (prompt, original = null)
|
|||
}
|
||||
|
||||
return preparedPrompt;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory function for creating a QuickEdit object associated with a prompt element.
|
||||
|
@ -1065,7 +1067,7 @@ PromptManagerModule.prototype.preparePrompt = function (prompt, original = null)
|
|||
* and handle input events to update the prompt content.
|
||||
*
|
||||
*/
|
||||
PromptManagerModule.prototype.createQuickEdit = function (identifier, title) {
|
||||
createQuickEdit(identifier, title) {
|
||||
const prompt = this.getPromptById(identifier);
|
||||
const textareaIdentifier = `${identifier}_prompt_quick_edit_textarea`;
|
||||
const html = `<div class="range-block m-t-1">
|
||||
|
@ -1087,15 +1089,15 @@ PromptManagerModule.prototype.createQuickEdit = function (identifier, title) {
|
|||
debouncedSaveServiceSettings().then(() => this.render());
|
||||
});
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
PromptManagerModule.prototype.updateQuickEdit = function (identifier, prompt) {
|
||||
updateQuickEdit(identifier, prompt) {
|
||||
const elementId = `${identifier}_prompt_quick_edit_textarea`;
|
||||
const textarea = document.getElementById(elementId);
|
||||
textarea.value = prompt.content;
|
||||
|
||||
return elementId;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a given name is accepted by OpenAi API
|
||||
|
@ -1104,21 +1106,21 @@ PromptManagerModule.prototype.updateQuickEdit = function (identifier, prompt) {
|
|||
* @param name
|
||||
* @returns {boolean}
|
||||
*/
|
||||
PromptManagerModule.prototype.isValidName = function (name) {
|
||||
isValidName(name) {
|
||||
const regex = /^[a-zA-Z0-9_]{1,64}$/;
|
||||
|
||||
return regex.test(name);
|
||||
};
|
||||
}
|
||||
|
||||
PromptManagerModule.prototype.sanitizeName = function (name) {
|
||||
sanitizeName(name) {
|
||||
return name.replace(/[^a-zA-Z0-9_]/g, '_').substring(0, 64);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a given prompt into the edit form fields.
|
||||
* @param {Object} prompt - Prompt object with properties 'name', 'role', 'content', and 'system_prompt'
|
||||
*/
|
||||
PromptManagerModule.prototype.loadPromptIntoEditForm = function (prompt) {
|
||||
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');
|
||||
|
@ -1151,9 +1153,9 @@ PromptManagerModule.prototype.loadPromptIntoEditForm = function (prompt) {
|
|||
|
||||
const savePromptButton = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_save');
|
||||
savePromptButton.dataset.pmPrompt = prompt.identifier;
|
||||
};
|
||||
}
|
||||
|
||||
PromptManagerModule.prototype.handleInjectionPositionChange = function (event) {
|
||||
handleInjectionPositionChange(event) {
|
||||
const injectionDepthBlock = document.getElementById(this.configuration.prefix + 'prompt_manager_depth_block');
|
||||
const injectionPosition = Number(event.target.value);
|
||||
if (injectionPosition === INJECTION_POSITION.ABSOLUTE) {
|
||||
|
@ -1161,13 +1163,13 @@ PromptManagerModule.prototype.handleInjectionPositionChange = function (event) {
|
|||
} else {
|
||||
injectionDepthBlock.style.visibility = 'hidden';
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a given prompt into the inspect form
|
||||
* @param {MessageCollection} messages - Prompt object with properties 'name', 'role', 'content', and 'system_prompt'
|
||||
*/
|
||||
PromptManagerModule.prototype.loadMessagesIntoInspectForm = function (messages) {
|
||||
loadMessagesIntoInspectForm(messages) {
|
||||
if (!messages) return;
|
||||
|
||||
const createInlineDrawer = (message) => {
|
||||
|
@ -1201,12 +1203,12 @@ PromptManagerModule.prototype.loadMessagesIntoInspectForm = function (messages)
|
|||
messagesCollection.forEach(message => {
|
||||
messageList.append(createInlineDrawer(message));
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears all input fields in the edit form.
|
||||
*/
|
||||
PromptManagerModule.prototype.clearEditForm = function () {
|
||||
clearEditForm() {
|
||||
const editArea = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_edit');
|
||||
editArea.style.display = 'none';
|
||||
|
||||
|
@ -1226,20 +1228,20 @@ PromptManagerModule.prototype.clearEditForm = function () {
|
|||
injectionDepthBlock.style.visibility = 'unset';
|
||||
|
||||
roleField.disabled = false;
|
||||
};
|
||||
}
|
||||
|
||||
PromptManagerModule.prototype.clearInspectForm = function () {
|
||||
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 = '';
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a full list of prompts whose content markers have been substituted.
|
||||
* @returns {PromptCollection} A PromptCollection object
|
||||
*/
|
||||
PromptManagerModule.prototype.getPromptCollection = function () {
|
||||
getPromptCollection() {
|
||||
const promptOrder = this.getPromptOrderForCharacter(this.activeCharacter);
|
||||
|
||||
const promptCollection = new PromptCollection();
|
||||
|
@ -1251,35 +1253,35 @@ PromptManagerModule.prototype.getPromptCollection = function () {
|
|||
});
|
||||
|
||||
return promptCollection;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for messages property
|
||||
*
|
||||
* @param {MessageCollection} messages
|
||||
*/
|
||||
PromptManagerModule.prototype.setMessages = function (messages) {
|
||||
setMessages(messages) {
|
||||
this.messages = messages;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Set and process a finished chat completion object
|
||||
*
|
||||
* @param {ChatCompletion} chatCompletion
|
||||
*/
|
||||
PromptManagerModule.prototype.setChatCompletion = function (chatCompletion) {
|
||||
setChatCompletion(chatCompletion) {
|
||||
const messages = chatCompletion.getMessages();
|
||||
|
||||
this.setMessages(messages);
|
||||
this.populateTokenCounts(messages);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Populates the token handler
|
||||
*
|
||||
* @param {MessageCollection} messages
|
||||
*/
|
||||
PromptManagerModule.prototype.populateTokenCounts = function (messages) {
|
||||
populateTokenCounts(messages) {
|
||||
this.tokenHandler.resetCounts();
|
||||
const counts = this.tokenHandler.getCounts();
|
||||
messages.getCollection().forEach(message => {
|
||||
|
@ -1289,7 +1291,7 @@ PromptManagerModule.prototype.populateTokenCounts = function (messages) {
|
|||
this.tokenUsage = this.tokenHandler.getTotal();
|
||||
|
||||
this.log('Updated token usage with ' + this.tokenUsage);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Populates legacy token counts
|
||||
|
@ -1298,7 +1300,7 @@ PromptManagerModule.prototype.populateTokenCounts = function (messages) {
|
|||
*
|
||||
* @param {MessageCollection} messages
|
||||
*/
|
||||
PromptManagerModule.prototype.populateLegacyTokenCounts = function (messages) {
|
||||
populateLegacyTokenCounts(messages) {
|
||||
// Update general token counts
|
||||
const chatHistory = messages.getItemByIdentifier('chatHistory');
|
||||
const startChat = chatHistory?.getCollection()[0]?.getTokens() || 0;
|
||||
|
@ -1317,12 +1319,12 @@ PromptManagerModule.prototype.populateLegacyTokenCounts = function (messages) {
|
|||
'conversation': this.tokenHandler.counts.chatHistory ?? 0,
|
||||
},
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Empties, then re-assembles the container containing the prompt list.
|
||||
*/
|
||||
PromptManagerModule.prototype.renderPromptManager = function () {
|
||||
renderPromptManager() {
|
||||
const promptManagerDiv = this.containerElement;
|
||||
promptManagerDiv.innerHTML = '';
|
||||
|
||||
|
@ -1419,12 +1421,12 @@ PromptManagerModule.prototype.renderPromptManager = function () {
|
|||
rangeBlockDiv.querySelector('.export-promptmanager-prompts-full').addEventListener('click', this.handleFullExport);
|
||||
rangeBlockDiv.querySelector('.export-promptmanager-prompts-character')?.addEventListener('click', this.handleCharacterExport);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Empties, then re-assembles the prompt list
|
||||
*/
|
||||
PromptManagerModule.prototype.renderPromptManagerListItems = function () {
|
||||
renderPromptManagerListItems() {
|
||||
if (!this.serviceSettings.prompts) return;
|
||||
|
||||
const promptManagerList = this.listElement;
|
||||
|
@ -1545,7 +1547,7 @@ PromptManagerModule.prototype.renderPromptManagerListItems = function () {
|
|||
Array.from(promptManagerList.querySelectorAll('.prompt-manager-toggle-action')).forEach(el => {
|
||||
el.addEventListener('click', this.handleToggle);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the passed data to a json file
|
||||
|
@ -1554,7 +1556,7 @@ PromptManagerModule.prototype.renderPromptManagerListItems = function () {
|
|||
* @param type
|
||||
* @param name
|
||||
*/
|
||||
PromptManagerModule.prototype.export = function (data, type, name = 'export') {
|
||||
export(data, type, name = 'export') {
|
||||
const promptExport = {
|
||||
version: this.configuration.version,
|
||||
type: type,
|
||||
|
@ -1573,14 +1575,14 @@ PromptManagerModule.prototype.export = function (data, type, name = 'export') {
|
|||
downloadLink.click();
|
||||
|
||||
URL.revokeObjectURL(url);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Imports a json file with prompts and an optional prompt list for the active character
|
||||
*
|
||||
* @param importData
|
||||
*/
|
||||
PromptManagerModule.prototype.import = function (importData) {
|
||||
import(importData) {
|
||||
const mergeKeepNewer = (prompts, newPrompts) => {
|
||||
let merged = [...prompts, ...newPrompts];
|
||||
|
||||
|
@ -1629,7 +1631,7 @@ PromptManagerModule.prototype.import = function (importData) {
|
|||
|
||||
toastr.success('Prompt import complete.');
|
||||
this.saveServiceSettings().then(() => this.render());
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to check whether the structure of object matches controlObj
|
||||
|
@ -1638,7 +1640,7 @@ PromptManagerModule.prototype.import = function (importData) {
|
|||
* @param object
|
||||
* @returns {boolean}
|
||||
*/
|
||||
PromptManagerModule.prototype.validateObject = function (controlObj, object) {
|
||||
validateObject(controlObj, object) {
|
||||
for (let key in controlObj) {
|
||||
if (!Object.hasOwn(object, key)) {
|
||||
if (controlObj[key] === null) continue;
|
||||
|
@ -1654,14 +1656,14 @@ PromptManagerModule.prototype.validateObject = function (controlObj, object) {
|
|||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current date as mm/dd/YYYY
|
||||
*
|
||||
* @returns {`${string}_${string}_${string}`}
|
||||
*/
|
||||
PromptManagerModule.prototype.getFormattedDate = function () {
|
||||
getFormattedDate() {
|
||||
const date = new Date();
|
||||
let month = String(date.getMonth() + 1);
|
||||
let day = String(date.getDate());
|
||||
|
@ -1671,7 +1673,7 @@ PromptManagerModule.prototype.getFormattedDate = function () {
|
|||
if (day.length < 2) day = '0' + day;
|
||||
|
||||
return `${month}_${day}_${year}`;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the prompt list draggable and handles swapping of two entries in the list.
|
||||
|
@ -1679,7 +1681,7 @@ PromptManagerModule.prototype.getFormattedDate = function () {
|
|||
* @property {string} identifier
|
||||
* @returns {void}
|
||||
*/
|
||||
PromptManagerModule.prototype.makeDraggable = function () {
|
||||
makeDraggable() {
|
||||
$(`#${this.configuration.prefix}prompt_manager_list`).sortable({
|
||||
delay: this.configuration.sortableDelay,
|
||||
items: `.${this.configuration.prefix}prompt_manager_prompt_draggable`,
|
||||
|
@ -1697,72 +1699,73 @@ PromptManagerModule.prototype.makeDraggable = function () {
|
|||
this.saveServiceSettings();
|
||||
},
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Slides down the edit form and adds the class 'openDrawer' to the first element of '#openai_prompt_manager_popup'.
|
||||
* @returns {void}
|
||||
*/
|
||||
PromptManagerModule.prototype.showPopup = function (area = 'edit') {
|
||||
showPopup(area = 'edit') {
|
||||
const areaElement = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_' + area);
|
||||
areaElement.style.display = 'block';
|
||||
|
||||
$('#' + this.configuration.prefix + 'prompt_manager_popup').first()
|
||||
.slideDown(200, 'swing')
|
||||
.addClass('openDrawer');
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Slides up the edit form and removes the class 'openDrawer' from the first element of '#openai_prompt_manager_popup'.
|
||||
* @returns {void}
|
||||
*/
|
||||
PromptManagerModule.prototype.hidePopup = function () {
|
||||
hidePopup() {
|
||||
$('#' + this.configuration.prefix + 'prompt_manager_popup').first()
|
||||
.slideUp(200, 'swing')
|
||||
.removeClass('openDrawer');
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Quick uuid4 implementation
|
||||
* @returns {string} A string representation of an uuid4
|
||||
*/
|
||||
PromptManagerModule.prototype.getUuidv4 = function () {
|
||||
getUuidv4() {
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
||||
let r = Math.random() * 16 | 0,
|
||||
v = c === 'x' ? r : (r & 0x3 | 0x8);
|
||||
return v.toString(16);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Write to console with prefix
|
||||
*
|
||||
* @param output
|
||||
*/
|
||||
PromptManagerModule.prototype.log = function (output) {
|
||||
log(output) {
|
||||
if (power_user.console_log_prompts) console.log('[PromptManager] ' + output);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Start a profiling task
|
||||
*
|
||||
* @param identifier
|
||||
*/
|
||||
PromptManagerModule.prototype.profileStart = function (identifier) {
|
||||
profileStart(identifier) {
|
||||
if (power_user.console_log_prompts) console.time(identifier);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* End a profiling task
|
||||
*
|
||||
* @param identifier
|
||||
*/
|
||||
PromptManagerModule.prototype.profileEnd = function (identifier) {
|
||||
profileEnd(identifier) {
|
||||
if (power_user.console_log_prompts) {
|
||||
this.log('Profiling of "' + identifier + '" finished. Result below.');
|
||||
console.timeEnd(identifier);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const chatCompletionDefaultPrompts = {
|
||||
'prompts': [
|
||||
|
|
Loading…
Reference in New Issue