mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Merge pull request #1439 from valadaptive/prompt-manager-class
Convert PromptManagerModule to a class
This commit is contained in:
@ -178,7 +178,8 @@ class PromptCollection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function PromptManagerModule() {
|
class PromptManager {
|
||||||
|
constructor() {
|
||||||
this.systemPrompts = [
|
this.systemPrompts = [
|
||||||
'main',
|
'main',
|
||||||
'nsfw',
|
'nsfw',
|
||||||
@ -279,18 +280,19 @@ function PromptManagerModule() {
|
|||||||
|
|
||||||
/** Debounced version of render */
|
/** Debounced version of render */
|
||||||
this.renderDebounced = debounce(this.render.bind(this), 1000);
|
this.renderDebounced = debounce(this.render.bind(this), 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes the PromptManagerModule with provided configuration and service settings.
|
/**
|
||||||
|
* Initializes the PromptManager with provided configuration and service settings.
|
||||||
*
|
*
|
||||||
* Sets up various handlers for user interactions, event listeners and initial rendering of prompts.
|
* Sets up various handlers for user interactions, event listeners and initial rendering of prompts.
|
||||||
* It is also responsible for preparing prompt edit form buttons, managing popup form close and clear actions.
|
* It is also responsible for preparing prompt edit form buttons, managing popup form close and clear actions.
|
||||||
*
|
*
|
||||||
* @param {Object} moduleConfiguration - Configuration object for the PromptManagerModule.
|
* @param {Object} moduleConfiguration - Configuration object for the PromptManager.
|
||||||
* @param {Object} serviceSettings - Service settings object for the PromptManagerModule.
|
* @param {Object} serviceSettings - Service settings object for the PromptManager.
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.init = function (moduleConfiguration, serviceSettings) {
|
init(moduleConfiguration, serviceSettings) {
|
||||||
this.configuration = Object.assign(this.configuration, moduleConfiguration);
|
this.configuration = Object.assign(this.configuration, moduleConfiguration);
|
||||||
this.tokenHandler = this.tokenHandler || new TokenHandler();
|
this.tokenHandler = this.tokenHandler || new TokenHandler();
|
||||||
this.serviceSettings = serviceSettings;
|
this.serviceSettings = serviceSettings;
|
||||||
@ -654,14 +656,14 @@ PromptManagerModule.prototype.init = function (moduleConfiguration, serviceSetti
|
|||||||
eventSource.on(event_types.WORLDINFO_SETTINGS_UPDATED, () => this.renderDebounced());
|
eventSource.on(event_types.WORLDINFO_SETTINGS_UPDATED, () => this.renderDebounced());
|
||||||
|
|
||||||
this.log('Initialized');
|
this.log('Initialized');
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main rendering function
|
* Main rendering function
|
||||||
*
|
*
|
||||||
* @param afterTryGenerate - Whether a dry run should be attempted before rendering
|
* @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 (main_api !== 'openai') return;
|
||||||
|
|
||||||
if ('character' === this.configuration.promptOrder.strategy && null === this.activeCharacter) return;
|
if ('character' === this.configuration.promptOrder.strategy && null === this.activeCharacter) return;
|
||||||
@ -690,88 +692,88 @@ PromptManagerModule.prototype.render = function (afterTryGenerate = true) {
|
|||||||
}).catch(() => {
|
}).catch(() => {
|
||||||
console.log('Timeout while waiting for send press to be false');
|
console.log('Timeout while waiting for send press to be false');
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update a prompt with the values from the HTML form.
|
* Update a prompt with the values from the HTML form.
|
||||||
* @param {object} prompt - The prompt to be updated.
|
* @param {object} prompt - The prompt to be updated.
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.updatePromptWithPromptEditForm = function (prompt) {
|
updatePromptWithPromptEditForm(prompt) {
|
||||||
prompt.name = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_name').value;
|
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.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.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_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);
|
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.
|
* Find a prompt by its identifier and update it with the provided object.
|
||||||
* @param {string} identifier - The identifier of the prompt.
|
* @param {string} identifier - The identifier of the prompt.
|
||||||
* @param {object} updatePrompt - An object with properties to be updated in the prompt.
|
* @param {object} updatePrompt - An object with properties to be updated in the prompt.
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.updatePromptByIdentifier = function (identifier, updatePrompt) {
|
updatePromptByIdentifier(identifier, updatePrompt) {
|
||||||
let prompt = this.serviceSettings.prompts.find((item) => identifier === item.identifier);
|
let prompt = this.serviceSettings.prompts.find((item) => identifier === item.identifier);
|
||||||
if (prompt) prompt = Object.assign(prompt, updatePrompt);
|
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.
|
* 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.
|
* @param {object[]} prompts - An array of prompt updates.
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.updatePrompts = function (prompts) {
|
updatePrompts(prompts) {
|
||||||
prompts.forEach((update) => {
|
prompts.forEach((update) => {
|
||||||
let prompt = this.getPromptById(update.identifier);
|
let prompt = this.getPromptById(update.identifier);
|
||||||
if (prompt) Object.assign(prompt, update);
|
if (prompt) Object.assign(prompt, update);
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
PromptManagerModule.prototype.getTokenHandler = function () {
|
getTokenHandler() {
|
||||||
return this.tokenHandler;
|
return this.tokenHandler;
|
||||||
};
|
}
|
||||||
|
|
||||||
PromptManagerModule.prototype.isPromptDisabledForActiveCharacter = function (identifier) {
|
isPromptDisabledForActiveCharacter(identifier) {
|
||||||
const promptOrderEntry = this.getPromptOrderEntry(this.activeCharacter, identifier);
|
const promptOrderEntry = this.getPromptOrderEntry(this.activeCharacter, identifier);
|
||||||
if (promptOrderEntry) return !promptOrderEntry.enabled;
|
if (promptOrderEntry) return !promptOrderEntry.enabled;
|
||||||
return false;
|
return false;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a prompt to the current character's prompt list.
|
* Add a prompt to the current character's prompt list.
|
||||||
* @param {object} prompt - The prompt to be added.
|
* @param {object} prompt - The prompt to be added.
|
||||||
* @param {object} character - The character whose prompt list will be updated.
|
* @param {object} character - The character whose prompt list will be updated.
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.appendPrompt = function (prompt, character) {
|
appendPrompt(prompt, character) {
|
||||||
const promptOrder = this.getPromptOrderForCharacter(character);
|
const promptOrder = this.getPromptOrderForCharacter(character);
|
||||||
const index = promptOrder.findIndex(entry => entry.identifier === prompt.identifier);
|
const index = promptOrder.findIndex(entry => entry.identifier === prompt.identifier);
|
||||||
|
|
||||||
if (-1 === index) promptOrder.push({ identifier: prompt.identifier, enabled: false });
|
if (-1 === index) promptOrder.push({ identifier: prompt.identifier, enabled: false });
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove a prompt from the current character's prompt list.
|
* Remove a prompt from the current character's prompt list.
|
||||||
* @param {object} prompt - The prompt to be removed.
|
* @param {object} prompt - The prompt to be removed.
|
||||||
* @param {object} character - The character whose prompt list will be updated.
|
* @param {object} character - The character whose prompt list will be updated.
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
// Remove a prompt from the current characters prompt list
|
// Remove a prompt from the current characters prompt list
|
||||||
PromptManagerModule.prototype.detachPrompt = function (prompt, character) {
|
detachPrompt(prompt, character) {
|
||||||
const promptOrder = this.getPromptOrderForCharacter(character);
|
const promptOrder = this.getPromptOrderForCharacter(character);
|
||||||
const index = promptOrder.findIndex(entry => entry.identifier === prompt.identifier);
|
const index = promptOrder.findIndex(entry => entry.identifier === prompt.identifier);
|
||||||
if (-1 === index) return;
|
if (-1 === index) return;
|
||||||
promptOrder.splice(index, 1);
|
promptOrder.splice(index, 1);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new prompt and add it to the list of prompts.
|
* Create a new prompt and add it to the list of prompts.
|
||||||
* @param {object} prompt - The prompt to be added.
|
* @param {object} prompt - The prompt to be added.
|
||||||
* @param {string} identifier - The identifier for the new prompt.
|
* @param {string} identifier - The identifier for the new prompt.
|
||||||
* @returns {void}
|
* @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');
|
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);
|
this.serviceSettings.prompts.push(newPrompt);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sanitize the service settings, ensuring each prompt has a unique identifier.
|
* Sanitize the service settings, ensuring each prompt has a unique identifier.
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.sanitizeServiceSettings = function () {
|
sanitizeServiceSettings() {
|
||||||
this.serviceSettings.prompts = this.serviceSettings.prompts ?? [];
|
this.serviceSettings.prompts = this.serviceSettings.prompts ?? [];
|
||||||
this.serviceSettings.prompt_order = this.serviceSettings.prompt_order ?? [];
|
this.serviceSettings.prompt_order = this.serviceSettings.prompt_order ?? [];
|
||||||
|
|
||||||
@ -819,15 +821,15 @@ PromptManagerModule.prototype.sanitizeServiceSettings = function () {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks whether entries of a characters prompt order are orphaned
|
* Checks whether entries of a characters prompt order are orphaned
|
||||||
* and if all mandatory system prompts for a character are present.
|
* and if all mandatory system prompts for a character are present.
|
||||||
*
|
*
|
||||||
* @param prompts
|
* @param prompts
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.checkForMissingPrompts = function (prompts) {
|
checkForMissingPrompts(prompts) {
|
||||||
const defaultPromptIdentifiers = chatCompletionDefaultPrompts.prompts.reduce((list, prompt) => { list.push(prompt.identifier); return list; }, []);
|
const defaultPromptIdentifiers = chatCompletionDefaultPrompts.prompts.reduce((list, prompt) => { list.push(prompt.identifier); return list; }, []);
|
||||||
|
|
||||||
const missingIdentifiers = defaultPromptIdentifiers.filter(identifier =>
|
const missingIdentifiers = defaultPromptIdentifiers.filter(identifier =>
|
||||||
@ -841,62 +843,62 @@ PromptManagerModule.prototype.checkForMissingPrompts = function (prompts) {
|
|||||||
this.log(`Missing system prompt: ${defaultPrompt.identifier}. Added default.`);
|
this.log(`Missing system prompt: ${defaultPrompt.identifier}. Added default.`);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check whether a prompt can be inspected.
|
* Check whether a prompt can be inspected.
|
||||||
* @param {object} prompt - The prompt to check.
|
* @param {object} prompt - The prompt to check.
|
||||||
* @returns {boolean} True if the prompt is a marker, false otherwise.
|
* @returns {boolean} True if the prompt is a marker, false otherwise.
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.isPromptInspectionAllowed = function (prompt) {
|
isPromptInspectionAllowed(prompt) {
|
||||||
return true;
|
return true;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check whether a prompt can be deleted. System prompts cannot be deleted.
|
* Check whether a prompt can be deleted. System prompts cannot be deleted.
|
||||||
* @param {object} prompt - The prompt to check.
|
* @param {object} prompt - The prompt to check.
|
||||||
* @returns {boolean} True if the prompt can be deleted, false otherwise.
|
* @returns {boolean} True if the prompt can be deleted, false otherwise.
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.isPromptDeletionAllowed = function (prompt) {
|
isPromptDeletionAllowed(prompt) {
|
||||||
return false === prompt.system_prompt;
|
return false === prompt.system_prompt;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check whether a prompt can be edited.
|
* Check whether a prompt can be edited.
|
||||||
* @param {object} prompt - The prompt to check.
|
* @param {object} prompt - The prompt to check.
|
||||||
* @returns {boolean} True if the prompt can be edited, false otherwise.
|
* @returns {boolean} True if the prompt can be edited, false otherwise.
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.isPromptEditAllowed = function (prompt) {
|
isPromptEditAllowed(prompt) {
|
||||||
return !prompt.marker;
|
return !prompt.marker;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check whether a prompt can be toggled on or off.
|
* Check whether a prompt can be toggled on or off.
|
||||||
* @param {object} prompt - The prompt to check.
|
* @param {object} prompt - The prompt to check.
|
||||||
* @returns {boolean} True if the prompt can be deleted, false otherwise.
|
* @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'];
|
const forceTogglePrompts = ['charDescription', 'charPersonality', 'scenario', 'personaDescription', 'worldInfoBefore', 'worldInfoAfter'];
|
||||||
return prompt.marker && !forceTogglePrompts.includes(prompt.identifier) ? false : !this.configuration.toggleDisabled.includes(prompt.identifier);
|
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.
|
* 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.
|
* @param {object} event - The event object containing the character's ID.
|
||||||
* @returns void
|
* @returns void
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.handleCharacterDeleted = function (event) {
|
handleCharacterDeleted(event) {
|
||||||
if ('global' === this.configuration.promptOrder.strategy) return;
|
if ('global' === this.configuration.promptOrder.strategy) return;
|
||||||
this.removePromptOrderForCharacter(this.activeCharacter);
|
this.removePromptOrderForCharacter(this.activeCharacter);
|
||||||
if (this.activeCharacter.id === event.detail.id) this.activeCharacter = null;
|
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.
|
* 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.
|
* @param {object} event - The event object containing the character's ID and character data.
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.handleCharacterSelected = function (event) {
|
handleCharacterSelected(event) {
|
||||||
if ('global' === this.configuration.promptOrder.strategy) {
|
if ('global' === this.configuration.promptOrder.strategy) {
|
||||||
this.activeCharacter = { id: this.configuration.promptOrder.dummyId };
|
this.activeCharacter = { id: this.configuration.promptOrder.dummyId };
|
||||||
} else if ('character' === this.configuration.promptOrder.strategy) {
|
} else if ('character' === this.configuration.promptOrder.strategy) {
|
||||||
@ -910,14 +912,14 @@ PromptManagerModule.prototype.handleCharacterSelected = function (event) {
|
|||||||
} else {
|
} else {
|
||||||
throw new Error('Unsupported prompt order mode.');
|
throw new Error('Unsupported prompt order mode.');
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the most recently selected character
|
* Set the most recently selected character
|
||||||
*
|
*
|
||||||
* @param event
|
* @param event
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.handleCharacterUpdated = function (event) {
|
handleCharacterUpdated(event) {
|
||||||
if ('global' === this.configuration.promptOrder.strategy) {
|
if ('global' === this.configuration.promptOrder.strategy) {
|
||||||
this.activeCharacter = { id: this.configuration.promptOrder.dummyId };
|
this.activeCharacter = { id: this.configuration.promptOrder.dummyId };
|
||||||
} else if ('character' === this.configuration.promptOrder.strategy) {
|
} else if ('character' === this.configuration.promptOrder.strategy) {
|
||||||
@ -925,14 +927,14 @@ PromptManagerModule.prototype.handleCharacterUpdated = function (event) {
|
|||||||
} else {
|
} else {
|
||||||
throw new Error('Prompt order strategy not supported.');
|
throw new Error('Prompt order strategy not supported.');
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the most recently selected character group
|
* Set the most recently selected character group
|
||||||
*
|
*
|
||||||
* @param event
|
* @param event
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.handleGroupSelected = function (event) {
|
handleGroupSelected(event) {
|
||||||
if ('global' === this.configuration.promptOrder.strategy) {
|
if ('global' === this.configuration.promptOrder.strategy) {
|
||||||
this.activeCharacter = { id: this.configuration.promptOrder.dummyId };
|
this.activeCharacter = { id: this.configuration.promptOrder.dummyId };
|
||||||
} else if ('character' === this.configuration.promptOrder.strategy) {
|
} else if ('character' === this.configuration.promptOrder.strategy) {
|
||||||
@ -944,106 +946,106 @@ PromptManagerModule.prototype.handleGroupSelected = function (event) {
|
|||||||
} else {
|
} else {
|
||||||
throw new Error('Prompt order strategy not supported.');
|
throw new Error('Prompt order strategy not supported.');
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a list of group characters, regardless of whether they are active or not.
|
* Get a list of group characters, regardless of whether they are active or not.
|
||||||
*
|
*
|
||||||
* @returns {string[]}
|
* @returns {string[]}
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.getActiveGroupCharacters = function () {
|
getActiveGroupCharacters() {
|
||||||
// ToDo: Ideally, this should return the actual characters.
|
// ToDo: Ideally, this should return the actual characters.
|
||||||
return (this.activeCharacter?.group?.members || []).map(member => member && member.substring(0, member.lastIndexOf('.')));
|
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.
|
* Get the prompts for a specific character. Can be filtered to only include enabled prompts.
|
||||||
* @returns {object[]} The prompts for the character.
|
* @returns {object[]} The prompts for the character.
|
||||||
* @param character
|
* @param character
|
||||||
* @param onlyEnabled
|
* @param onlyEnabled
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.getPromptsForCharacter = function (character, onlyEnabled = false) {
|
getPromptsForCharacter(character, onlyEnabled = false) {
|
||||||
return this.getPromptOrderForCharacter(character)
|
return this.getPromptOrderForCharacter(character)
|
||||||
.map(item => true === onlyEnabled ? (true === item.enabled ? this.getPromptById(item.identifier) : null) : this.getPromptById(item.identifier))
|
.map(item => true === onlyEnabled ? (true === item.enabled ? this.getPromptById(item.identifier) : null) : this.getPromptById(item.identifier))
|
||||||
.filter(prompt => null !== prompt);
|
.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.
|
* 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.
|
* @param {object|null} character - The character to get the prompt list for.
|
||||||
* @returns {object[]} The prompt list for the character, or an empty array.
|
* @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 ?? []);
|
return !character ? [] : (this.serviceSettings.prompt_order.find(list => String(list.character_id) === String(character.id))?.order ?? []);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the prompts for the manager.
|
* Set the prompts for the manager.
|
||||||
* @param {object[]} prompts - The prompts to be set.
|
* @param {object[]} prompts - The prompts to be set.
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.setPrompts = function (prompts) {
|
setPrompts(prompts) {
|
||||||
this.serviceSettings.prompts = prompts;
|
this.serviceSettings.prompts = prompts;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove the prompt list for a specific character.
|
* Remove the prompt list for a specific character.
|
||||||
* @param {object} character - The character whose prompt list will be removed.
|
* @param {object} character - The character whose prompt list will be removed.
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.removePromptOrderForCharacter = function (character) {
|
removePromptOrderForCharacter(character) {
|
||||||
const index = this.serviceSettings.prompt_order.findIndex(list => String(list.character_id) === String(character.id));
|
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);
|
if (-1 !== index) this.serviceSettings.prompt_order.splice(index, 1);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a new prompt list for a specific character.
|
* Adds a new prompt list for a specific character.
|
||||||
* @param {Object} character - Object with at least an `id` property
|
* @param {Object} character - Object with at least an `id` property
|
||||||
* @param {Array<Object>} promptOrder - Array of prompt objects
|
* @param {Array<Object>} promptOrder - Array of prompt objects
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.addPromptOrderForCharacter = function (character, promptOrder) {
|
addPromptOrderForCharacter(character, promptOrder) {
|
||||||
this.serviceSettings.prompt_order.push({
|
this.serviceSettings.prompt_order.push({
|
||||||
character_id: character.id,
|
character_id: character.id,
|
||||||
order: JSON.parse(JSON.stringify(promptOrder)),
|
order: JSON.parse(JSON.stringify(promptOrder)),
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Searches for a prompt list entry for a given character and identifier.
|
* Searches for a prompt list entry for a given character and identifier.
|
||||||
* @param {Object} character - Character object
|
* @param {Object} character - Character object
|
||||||
* @param {string} identifier - Identifier of the prompt list entry
|
* @param {string} identifier - Identifier of the prompt list entry
|
||||||
* @returns {Object|null} The prompt list entry object, or null if not found
|
* @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;
|
return this.getPromptOrderForCharacter(character).find(entry => entry.identifier === identifier) ?? null;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds and returns a prompt by its identifier.
|
* Finds and returns a prompt by its identifier.
|
||||||
* @param {string} identifier - Identifier of the prompt
|
* @param {string} identifier - Identifier of the prompt
|
||||||
* @returns {Object|null} The prompt object, or null if not found
|
* @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;
|
return this.serviceSettings.prompts.find(item => item && item.identifier === identifier) ?? null;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds and returns the index of a prompt by its identifier.
|
* Finds and returns the index of a prompt by its identifier.
|
||||||
* @param {string} identifier - Identifier of the prompt
|
* @param {string} identifier - Identifier of the prompt
|
||||||
* @returns {number|null} Index of the prompt, or null if not found
|
* @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;
|
return this.serviceSettings.prompts.findIndex(item => item.identifier === identifier) ?? null;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enriches a generic object, creating a new prompt object in the process
|
* Enriches a generic object, creating a new prompt object in the process
|
||||||
*
|
*
|
||||||
* @param {Object} prompt - Prompt object
|
* @param {Object} prompt - Prompt object
|
||||||
* @param original
|
* @param original
|
||||||
* @returns {Object} An object with "role" and "content" properties
|
* @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 groupMembers = this.getActiveGroupCharacters();
|
||||||
const preparedPrompt = new Prompt(prompt);
|
const preparedPrompt = new Prompt(prompt);
|
||||||
|
|
||||||
@ -1056,16 +1058,16 @@ PromptManagerModule.prototype.preparePrompt = function (prompt, original = null)
|
|||||||
}
|
}
|
||||||
|
|
||||||
return preparedPrompt;
|
return preparedPrompt;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Factory function for creating a QuickEdit object associated with a prompt element.
|
* Factory function for creating a QuickEdit object associated with a prompt element.
|
||||||
*
|
*
|
||||||
* The QuickEdit object provides methods to synchronize an input element's value with a prompt's content
|
* The QuickEdit object provides methods to synchronize an input element's value with a prompt's content
|
||||||
* and handle input events to update the prompt content.
|
* and handle input events to update the prompt content.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.createQuickEdit = function (identifier, title) {
|
createQuickEdit(identifier, title) {
|
||||||
const prompt = this.getPromptById(identifier);
|
const prompt = this.getPromptById(identifier);
|
||||||
const textareaIdentifier = `${identifier}_prompt_quick_edit_textarea`;
|
const textareaIdentifier = `${identifier}_prompt_quick_edit_textarea`;
|
||||||
const html = `<div class="range-block m-t-1">
|
const html = `<div class="range-block m-t-1">
|
||||||
@ -1087,38 +1089,38 @@ PromptManagerModule.prototype.createQuickEdit = function (identifier, title) {
|
|||||||
debouncedSaveServiceSettings().then(() => this.render());
|
debouncedSaveServiceSettings().then(() => this.render());
|
||||||
});
|
});
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
PromptManagerModule.prototype.updateQuickEdit = function (identifier, prompt) {
|
updateQuickEdit(identifier, prompt) {
|
||||||
const elementId = `${identifier}_prompt_quick_edit_textarea`;
|
const elementId = `${identifier}_prompt_quick_edit_textarea`;
|
||||||
const textarea = document.getElementById(elementId);
|
const textarea = document.getElementById(elementId);
|
||||||
textarea.value = prompt.content;
|
textarea.value = prompt.content;
|
||||||
|
|
||||||
return elementId;
|
return elementId;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if a given name is accepted by OpenAi API
|
* Checks if a given name is accepted by OpenAi API
|
||||||
* @link https://platform.openai.com/docs/api-reference/chat/create
|
* @link https://platform.openai.com/docs/api-reference/chat/create
|
||||||
*
|
*
|
||||||
* @param name
|
* @param name
|
||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.isValidName = function (name) {
|
isValidName(name) {
|
||||||
const regex = /^[a-zA-Z0-9_]{1,64}$/;
|
const regex = /^[a-zA-Z0-9_]{1,64}$/;
|
||||||
|
|
||||||
return regex.test(name);
|
return regex.test(name);
|
||||||
};
|
}
|
||||||
|
|
||||||
PromptManagerModule.prototype.sanitizeName = function (name) {
|
sanitizeName(name) {
|
||||||
return name.replace(/[^a-zA-Z0-9_]/g, '_').substring(0, 64);
|
return name.replace(/[^a-zA-Z0-9_]/g, '_').substring(0, 64);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads a given prompt into the edit form fields.
|
* Loads a given prompt into the edit form fields.
|
||||||
* @param {Object} prompt - Prompt object with properties 'name', 'role', 'content', and 'system_prompt'
|
* @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 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 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 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');
|
const savePromptButton = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_save');
|
||||||
savePromptButton.dataset.pmPrompt = prompt.identifier;
|
savePromptButton.dataset.pmPrompt = prompt.identifier;
|
||||||
};
|
}
|
||||||
|
|
||||||
PromptManagerModule.prototype.handleInjectionPositionChange = function (event) {
|
handleInjectionPositionChange(event) {
|
||||||
const injectionDepthBlock = document.getElementById(this.configuration.prefix + 'prompt_manager_depth_block');
|
const injectionDepthBlock = document.getElementById(this.configuration.prefix + 'prompt_manager_depth_block');
|
||||||
const injectionPosition = Number(event.target.value);
|
const injectionPosition = Number(event.target.value);
|
||||||
if (injectionPosition === INJECTION_POSITION.ABSOLUTE) {
|
if (injectionPosition === INJECTION_POSITION.ABSOLUTE) {
|
||||||
@ -1161,13 +1163,13 @@ PromptManagerModule.prototype.handleInjectionPositionChange = function (event) {
|
|||||||
} else {
|
} else {
|
||||||
injectionDepthBlock.style.visibility = 'hidden';
|
injectionDepthBlock.style.visibility = 'hidden';
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads a given prompt into the inspect form
|
* Loads a given prompt into the inspect form
|
||||||
* @param {MessageCollection} messages - Prompt object with properties 'name', 'role', 'content', and 'system_prompt'
|
* @param {MessageCollection} messages - Prompt object with properties 'name', 'role', 'content', and 'system_prompt'
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.loadMessagesIntoInspectForm = function (messages) {
|
loadMessagesIntoInspectForm(messages) {
|
||||||
if (!messages) return;
|
if (!messages) return;
|
||||||
|
|
||||||
const createInlineDrawer = (message) => {
|
const createInlineDrawer = (message) => {
|
||||||
@ -1201,12 +1203,12 @@ PromptManagerModule.prototype.loadMessagesIntoInspectForm = function (messages)
|
|||||||
messagesCollection.forEach(message => {
|
messagesCollection.forEach(message => {
|
||||||
messageList.append(createInlineDrawer(message));
|
messageList.append(createInlineDrawer(message));
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clears all input fields in the edit form.
|
* Clears all input fields in the edit form.
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.clearEditForm = function () {
|
clearEditForm() {
|
||||||
const editArea = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_edit');
|
const editArea = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_edit');
|
||||||
editArea.style.display = 'none';
|
editArea.style.display = 'none';
|
||||||
|
|
||||||
@ -1226,20 +1228,20 @@ PromptManagerModule.prototype.clearEditForm = function () {
|
|||||||
injectionDepthBlock.style.visibility = 'unset';
|
injectionDepthBlock.style.visibility = 'unset';
|
||||||
|
|
||||||
roleField.disabled = false;
|
roleField.disabled = false;
|
||||||
};
|
}
|
||||||
|
|
||||||
PromptManagerModule.prototype.clearInspectForm = function () {
|
clearInspectForm() {
|
||||||
const inspectArea = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_inspect');
|
const inspectArea = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_inspect');
|
||||||
inspectArea.style.display = 'none';
|
inspectArea.style.display = 'none';
|
||||||
const messageList = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_inspect_list');
|
const messageList = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_inspect_list');
|
||||||
messageList.innerHTML = '';
|
messageList.innerHTML = '';
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a full list of prompts whose content markers have been substituted.
|
* Returns a full list of prompts whose content markers have been substituted.
|
||||||
* @returns {PromptCollection} A PromptCollection object
|
* @returns {PromptCollection} A PromptCollection object
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.getPromptCollection = function () {
|
getPromptCollection() {
|
||||||
const promptOrder = this.getPromptOrderForCharacter(this.activeCharacter);
|
const promptOrder = this.getPromptOrderForCharacter(this.activeCharacter);
|
||||||
|
|
||||||
const promptCollection = new PromptCollection();
|
const promptCollection = new PromptCollection();
|
||||||
@ -1251,35 +1253,35 @@ PromptManagerModule.prototype.getPromptCollection = function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
return promptCollection;
|
return promptCollection;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setter for messages property
|
* Setter for messages property
|
||||||
*
|
*
|
||||||
* @param {MessageCollection} messages
|
* @param {MessageCollection} messages
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.setMessages = function (messages) {
|
setMessages(messages) {
|
||||||
this.messages = messages;
|
this.messages = messages;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set and process a finished chat completion object
|
* Set and process a finished chat completion object
|
||||||
*
|
*
|
||||||
* @param {ChatCompletion} chatCompletion
|
* @param {ChatCompletion} chatCompletion
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.setChatCompletion = function (chatCompletion) {
|
setChatCompletion(chatCompletion) {
|
||||||
const messages = chatCompletion.getMessages();
|
const messages = chatCompletion.getMessages();
|
||||||
|
|
||||||
this.setMessages(messages);
|
this.setMessages(messages);
|
||||||
this.populateTokenCounts(messages);
|
this.populateTokenCounts(messages);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Populates the token handler
|
* Populates the token handler
|
||||||
*
|
*
|
||||||
* @param {MessageCollection} messages
|
* @param {MessageCollection} messages
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.populateTokenCounts = function (messages) {
|
populateTokenCounts(messages) {
|
||||||
this.tokenHandler.resetCounts();
|
this.tokenHandler.resetCounts();
|
||||||
const counts = this.tokenHandler.getCounts();
|
const counts = this.tokenHandler.getCounts();
|
||||||
messages.getCollection().forEach(message => {
|
messages.getCollection().forEach(message => {
|
||||||
@ -1289,16 +1291,16 @@ PromptManagerModule.prototype.populateTokenCounts = function (messages) {
|
|||||||
this.tokenUsage = this.tokenHandler.getTotal();
|
this.tokenUsage = this.tokenHandler.getTotal();
|
||||||
|
|
||||||
this.log('Updated token usage with ' + this.tokenUsage);
|
this.log('Updated token usage with ' + this.tokenUsage);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Populates legacy token counts
|
* Populates legacy token counts
|
||||||
*
|
*
|
||||||
* @deprecated This might serve no purpose and should be evaluated for removal
|
* @deprecated This might serve no purpose and should be evaluated for removal
|
||||||
*
|
*
|
||||||
* @param {MessageCollection} messages
|
* @param {MessageCollection} messages
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.populateLegacyTokenCounts = function (messages) {
|
populateLegacyTokenCounts(messages) {
|
||||||
// Update general token counts
|
// Update general token counts
|
||||||
const chatHistory = messages.getItemByIdentifier('chatHistory');
|
const chatHistory = messages.getItemByIdentifier('chatHistory');
|
||||||
const startChat = chatHistory?.getCollection()[0]?.getTokens() || 0;
|
const startChat = chatHistory?.getCollection()[0]?.getTokens() || 0;
|
||||||
@ -1317,12 +1319,12 @@ PromptManagerModule.prototype.populateLegacyTokenCounts = function (messages) {
|
|||||||
'conversation': this.tokenHandler.counts.chatHistory ?? 0,
|
'conversation': this.tokenHandler.counts.chatHistory ?? 0,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Empties, then re-assembles the container containing the prompt list.
|
* Empties, then re-assembles the container containing the prompt list.
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.renderPromptManager = function () {
|
renderPromptManager() {
|
||||||
const promptManagerDiv = this.containerElement;
|
const promptManagerDiv = this.containerElement;
|
||||||
promptManagerDiv.innerHTML = '';
|
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-full').addEventListener('click', this.handleFullExport);
|
||||||
rangeBlockDiv.querySelector('.export-promptmanager-prompts-character')?.addEventListener('click', this.handleCharacterExport);
|
rangeBlockDiv.querySelector('.export-promptmanager-prompts-character')?.addEventListener('click', this.handleCharacterExport);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Empties, then re-assembles the prompt list
|
* Empties, then re-assembles the prompt list
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.renderPromptManagerListItems = function () {
|
renderPromptManagerListItems() {
|
||||||
if (!this.serviceSettings.prompts) return;
|
if (!this.serviceSettings.prompts) return;
|
||||||
|
|
||||||
const promptManagerList = this.listElement;
|
const promptManagerList = this.listElement;
|
||||||
@ -1545,16 +1547,16 @@ PromptManagerModule.prototype.renderPromptManagerListItems = function () {
|
|||||||
Array.from(promptManagerList.querySelectorAll('.prompt-manager-toggle-action')).forEach(el => {
|
Array.from(promptManagerList.querySelectorAll('.prompt-manager-toggle-action')).forEach(el => {
|
||||||
el.addEventListener('click', this.handleToggle);
|
el.addEventListener('click', this.handleToggle);
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes the passed data to a json file
|
* Writes the passed data to a json file
|
||||||
*
|
*
|
||||||
* @param data
|
* @param data
|
||||||
* @param type
|
* @param type
|
||||||
* @param name
|
* @param name
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.export = function (data, type, name = 'export') {
|
export(data, type, name = 'export') {
|
||||||
const promptExport = {
|
const promptExport = {
|
||||||
version: this.configuration.version,
|
version: this.configuration.version,
|
||||||
type: type,
|
type: type,
|
||||||
@ -1573,14 +1575,14 @@ PromptManagerModule.prototype.export = function (data, type, name = 'export') {
|
|||||||
downloadLink.click();
|
downloadLink.click();
|
||||||
|
|
||||||
URL.revokeObjectURL(url);
|
URL.revokeObjectURL(url);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Imports a json file with prompts and an optional prompt list for the active character
|
* Imports a json file with prompts and an optional prompt list for the active character
|
||||||
*
|
*
|
||||||
* @param importData
|
* @param importData
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.import = function (importData) {
|
import(importData) {
|
||||||
const mergeKeepNewer = (prompts, newPrompts) => {
|
const mergeKeepNewer = (prompts, newPrompts) => {
|
||||||
let merged = [...prompts, ...newPrompts];
|
let merged = [...prompts, ...newPrompts];
|
||||||
|
|
||||||
@ -1629,16 +1631,16 @@ PromptManagerModule.prototype.import = function (importData) {
|
|||||||
|
|
||||||
toastr.success('Prompt import complete.');
|
toastr.success('Prompt import complete.');
|
||||||
this.saveServiceSettings().then(() => this.render());
|
this.saveServiceSettings().then(() => this.render());
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper function to check whether the structure of object matches controlObj
|
* Helper function to check whether the structure of object matches controlObj
|
||||||
*
|
*
|
||||||
* @param controlObj
|
* @param controlObj
|
||||||
* @param object
|
* @param object
|
||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.validateObject = function (controlObj, object) {
|
validateObject(controlObj, object) {
|
||||||
for (let key in controlObj) {
|
for (let key in controlObj) {
|
||||||
if (!Object.hasOwn(object, key)) {
|
if (!Object.hasOwn(object, key)) {
|
||||||
if (controlObj[key] === null) continue;
|
if (controlObj[key] === null) continue;
|
||||||
@ -1654,14 +1656,14 @@ PromptManagerModule.prototype.validateObject = function (controlObj, object) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get current date as mm/dd/YYYY
|
* Get current date as mm/dd/YYYY
|
||||||
*
|
*
|
||||||
* @returns {`${string}_${string}_${string}`}
|
* @returns {`${string}_${string}_${string}`}
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.getFormattedDate = function () {
|
getFormattedDate() {
|
||||||
const date = new Date();
|
const date = new Date();
|
||||||
let month = String(date.getMonth() + 1);
|
let month = String(date.getMonth() + 1);
|
||||||
let day = String(date.getDate());
|
let day = String(date.getDate());
|
||||||
@ -1671,15 +1673,15 @@ PromptManagerModule.prototype.getFormattedDate = function () {
|
|||||||
if (day.length < 2) day = '0' + day;
|
if (day.length < 2) day = '0' + day;
|
||||||
|
|
||||||
return `${month}_${day}_${year}`;
|
return `${month}_${day}_${year}`;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Makes the prompt list draggable and handles swapping of two entries in the list.
|
* Makes the prompt list draggable and handles swapping of two entries in the list.
|
||||||
* @typedef {Object} Entry
|
* @typedef {Object} Entry
|
||||||
* @property {string} identifier
|
* @property {string} identifier
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.makeDraggable = function () {
|
makeDraggable() {
|
||||||
$(`#${this.configuration.prefix}prompt_manager_list`).sortable({
|
$(`#${this.configuration.prefix}prompt_manager_list`).sortable({
|
||||||
delay: this.configuration.sortableDelay,
|
delay: this.configuration.sortableDelay,
|
||||||
items: `.${this.configuration.prefix}prompt_manager_prompt_draggable`,
|
items: `.${this.configuration.prefix}prompt_manager_prompt_draggable`,
|
||||||
@ -1697,72 +1699,73 @@ PromptManagerModule.prototype.makeDraggable = function () {
|
|||||||
this.saveServiceSettings();
|
this.saveServiceSettings();
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Slides down the edit form and adds the class 'openDrawer' to the first element of '#openai_prompt_manager_popup'.
|
* Slides down the edit form and adds the class 'openDrawer' to the first element of '#openai_prompt_manager_popup'.
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.showPopup = function (area = 'edit') {
|
showPopup(area = 'edit') {
|
||||||
const areaElement = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_' + area);
|
const areaElement = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_' + area);
|
||||||
areaElement.style.display = 'block';
|
areaElement.style.display = 'block';
|
||||||
|
|
||||||
$('#' + this.configuration.prefix + 'prompt_manager_popup').first()
|
$('#' + this.configuration.prefix + 'prompt_manager_popup').first()
|
||||||
.slideDown(200, 'swing')
|
.slideDown(200, 'swing')
|
||||||
.addClass('openDrawer');
|
.addClass('openDrawer');
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Slides up the edit form and removes the class 'openDrawer' from the first element of '#openai_prompt_manager_popup'.
|
* Slides up the edit form and removes the class 'openDrawer' from the first element of '#openai_prompt_manager_popup'.
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.hidePopup = function () {
|
hidePopup() {
|
||||||
$('#' + this.configuration.prefix + 'prompt_manager_popup').first()
|
$('#' + this.configuration.prefix + 'prompt_manager_popup').first()
|
||||||
.slideUp(200, 'swing')
|
.slideUp(200, 'swing')
|
||||||
.removeClass('openDrawer');
|
.removeClass('openDrawer');
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Quick uuid4 implementation
|
* Quick uuid4 implementation
|
||||||
* @returns {string} A string representation of an uuid4
|
* @returns {string} A string representation of an uuid4
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.getUuidv4 = function () {
|
getUuidv4() {
|
||||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
||||||
let r = Math.random() * 16 | 0,
|
let r = Math.random() * 16 | 0,
|
||||||
v = c === 'x' ? r : (r & 0x3 | 0x8);
|
v = c === 'x' ? r : (r & 0x3 | 0x8);
|
||||||
return v.toString(16);
|
return v.toString(16);
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write to console with prefix
|
* Write to console with prefix
|
||||||
*
|
*
|
||||||
* @param output
|
* @param output
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.log = function (output) {
|
log(output) {
|
||||||
if (power_user.console_log_prompts) console.log('[PromptManager] ' + output);
|
if (power_user.console_log_prompts) console.log('[PromptManager] ' + output);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start a profiling task
|
* Start a profiling task
|
||||||
*
|
*
|
||||||
* @param identifier
|
* @param identifier
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.profileStart = function (identifier) {
|
profileStart(identifier) {
|
||||||
if (power_user.console_log_prompts) console.time(identifier);
|
if (power_user.console_log_prompts) console.time(identifier);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* End a profiling task
|
* End a profiling task
|
||||||
*
|
*
|
||||||
* @param identifier
|
* @param identifier
|
||||||
*/
|
*/
|
||||||
PromptManagerModule.prototype.profileEnd = function (identifier) {
|
profileEnd(identifier) {
|
||||||
if (power_user.console_log_prompts) {
|
if (power_user.console_log_prompts) {
|
||||||
this.log('Profiling of "' + identifier + '" finished. Result below.');
|
this.log('Profiling of "' + identifier + '" finished. Result below.');
|
||||||
console.timeEnd(identifier);
|
console.timeEnd(identifier);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const chatCompletionDefaultPrompts = {
|
const chatCompletionDefaultPrompts = {
|
||||||
'prompts': [
|
'prompts': [
|
||||||
@ -1902,7 +1905,7 @@ const promptManagerDefaultPromptOrder = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
export {
|
export {
|
||||||
PromptManagerModule,
|
PromptManager,
|
||||||
registerPromptManagerMigration,
|
registerPromptManagerMigration,
|
||||||
chatCompletionDefaultPrompts,
|
chatCompletionDefaultPrompts,
|
||||||
promptManagerDefaultPromptOrders,
|
promptManagerDefaultPromptOrders,
|
||||||
|
@ -38,7 +38,7 @@ import {
|
|||||||
INJECTION_POSITION,
|
INJECTION_POSITION,
|
||||||
Prompt,
|
Prompt,
|
||||||
promptManagerDefaultPromptOrders,
|
promptManagerDefaultPromptOrders,
|
||||||
PromptManagerModule as PromptManager,
|
PromptManager,
|
||||||
} from './PromptManager.js';
|
} from './PromptManager.js';
|
||||||
|
|
||||||
import { getCustomStoppingStrings, persona_description_positions, power_user } from './power-user.js';
|
import { getCustomStoppingStrings, persona_description_positions, power_user } from './power-user.js';
|
||||||
@ -458,7 +458,7 @@ function setOpenAIMessageExamples(mesExamplesArray) {
|
|||||||
* One-time setup for prompt manager module.
|
* One-time setup for prompt manager module.
|
||||||
*
|
*
|
||||||
* @param openAiSettings
|
* @param openAiSettings
|
||||||
* @returns {PromptManagerModule|null}
|
* @returns {PromptManager|null}
|
||||||
*/
|
*/
|
||||||
function setupChatCompletionPromptManager(openAiSettings) {
|
function setupChatCompletionPromptManager(openAiSettings) {
|
||||||
// Do not set up prompt manager more than once
|
// Do not set up prompt manager more than once
|
||||||
|
Reference in New Issue
Block a user