Rename prompt lists to prompt order

Includes renaming of configuration, no functional changes
This commit is contained in:
maver
2023-07-27 17:49:49 +02:00
parent 63d224d8af
commit bc4befeb22
2 changed files with 54 additions and 58 deletions

View File

@ -234,11 +234,11 @@ PromptManagerModule.prototype.init = function (moduleConfiguration, serviceSetti
// Enable and disable prompts // Enable and disable prompts
this.handleToggle = (event) => { this.handleToggle = (event) => {
const promptID = event.target.closest('.' + this.configuration.prefix + 'prompt_manager_prompt').dataset.pmIdentifier; const promptID = event.target.closest('.' + this.configuration.prefix + 'prompt_manager_prompt').dataset.pmIdentifier;
const promptListEntry = this.getPromptListEntry(this.activeCharacter, promptID); const promptOrderEntry = this.getPromptOrderEntry(this.activeCharacter, promptID);
const counts = this.tokenHandler.getCounts(); const counts = this.tokenHandler.getCounts();
counts[promptID] = null; counts[promptID] = null;
promptListEntry.enabled = !promptListEntry.enabled; promptOrderEntry.enabled = !promptOrderEntry.enabled;
this.saveServiceSettings().then(() => this.render()); this.saveServiceSettings().then(() => this.render());
}; };
@ -389,11 +389,11 @@ PromptManagerModule.prototype.init = function (moduleConfiguration, serviceSetti
return userPrompts; return userPrompts;
}, []); }, []);
const characterList = this.getPromptListForCharacter(this.activeCharacter); const characterList = this.getPromptOrderForCharacter(this.activeCharacter);
const exportPrompts = { const exportPrompts = {
prompts: characterPrompts, prompts: characterPrompts,
promptList: characterList prompt_order: characterList
} }
const name = this.activeCharacter.name + '-prompts'; const name = this.activeCharacter.name + '-prompts';
@ -441,8 +441,8 @@ PromptManagerModule.prototype.init = function (moduleConfiguration, serviceSetti
.then(userChoice => { .then(userChoice => {
if (false === userChoice) return; if (false === userChoice) return;
this.removePromptListForCharacter(this.activeCharacter); this.removePromptOrderForCharacter(this.activeCharacter);
this.addPromptListForCharacter(this.activeCharacter, promptManagerDefaultPromptList); this.addPromptOrderForCharacter(this.activeCharacter, promptManagerDefaultPromptOrder);
this.saveServiceSettings().then(() => this.render()); this.saveServiceSettings().then(() => this.render());
}); });
@ -580,10 +580,10 @@ PromptManagerModule.prototype.getTokenHandler = function() {
* @returns {void} * @returns {void}
*/ */
PromptManagerModule.prototype.appendPrompt = function (prompt, character) { PromptManagerModule.prototype.appendPrompt = function (prompt, character) {
const promptList = this.getPromptListForCharacter(character); const promptOrder = this.getPromptOrderForCharacter(character);
const index = promptList.findIndex(entry => entry.identifier === prompt.identifier); const index = promptOrder.findIndex(entry => entry.identifier === prompt.identifier);
if (-1 === index) promptList.push({identifier: prompt.identifier, enabled: false}); if (-1 === index) promptOrder.push({identifier: prompt.identifier, enabled: false});
} }
/** /**
@ -594,10 +594,10 @@ PromptManagerModule.prototype.appendPrompt = function (prompt, character) {
*/ */
// Remove a prompt from the current characters prompt list // Remove a prompt from the current characters prompt list
PromptManagerModule.prototype.detachPrompt = function (prompt, character) { PromptManagerModule.prototype.detachPrompt = function (prompt, character) {
const promptList = this.getPromptListForCharacter(character); const promptOrder = this.getPromptOrderForCharacter(character);
const index = promptList.findIndex(entry => entry.identifier === prompt.identifier); const index = promptOrder.findIndex(entry => entry.identifier === prompt.identifier);
if (-1 === index) return; if (-1 === index) return;
promptList.splice(index, 1) promptOrder.splice(index, 1)
} }
/** /**
@ -627,7 +627,7 @@ PromptManagerModule.prototype.addPrompt = function (prompt, identifier) {
*/ */
PromptManagerModule.prototype.sanitizeServiceSettings = function () { PromptManagerModule.prototype.sanitizeServiceSettings = function () {
this.serviceSettings.prompts = this.serviceSettings.prompts ?? []; this.serviceSettings.prompts = this.serviceSettings.prompts ?? [];
this.serviceSettings.prompt_lists = this.serviceSettings.prompt_lists ?? []; this.serviceSettings.prompt_order = this.serviceSettings.prompt_order ?? [];
// Check whether the referenced prompts are present. // Check whether the referenced prompts are present.
this.serviceSettings.prompts.length === 0 this.serviceSettings.prompts.length === 0
@ -641,7 +641,7 @@ PromptManagerModule.prototype.sanitizeServiceSettings = function () {
this.serviceSettings.prompts.forEach(prompt => prompt && (prompt.identifier = prompt.identifier ?? this.getUuidv4())); this.serviceSettings.prompts.forEach(prompt => prompt && (prompt.identifier = prompt.identifier ?? this.getUuidv4()));
if (this.activeCharacter) { if (this.activeCharacter) {
const promptReferences = this.getPromptListForCharacter(this.activeCharacter); const promptReferences = this.getPromptOrderForCharacter(this.activeCharacter);
for(let i = promptReferences.length - 1; i >= 0; i--) { for(let i = promptReferences.length - 1; i >= 0; i--) {
const reference = promptReferences[i]; const reference = promptReferences[i];
if(-1 === this.serviceSettings.prompts.findIndex(prompt => prompt.identifier === reference.identifier)) { if(-1 === this.serviceSettings.prompts.findIndex(prompt => prompt.identifier === reference.identifier)) {
@ -712,7 +712,7 @@ PromptManagerModule.prototype.isPromptToggleAllowed = function (prompt) {
* @returns boolean * @returns boolean
*/ */
PromptManagerModule.prototype.handleCharacterDeleted = function (event) { PromptManagerModule.prototype.handleCharacterDeleted = function (event) {
this.removePromptListForCharacter(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;
} }
@ -723,11 +723,11 @@ PromptManagerModule.prototype.handleCharacterDeleted = function (event) {
*/ */
PromptManagerModule.prototype.handleCharacterSelected = function (event) { PromptManagerModule.prototype.handleCharacterSelected = function (event) {
this.activeCharacter = {id: event.detail.id, ...event.detail.character}; this.activeCharacter = {id: event.detail.id, ...event.detail.character};
const promptList = this.getPromptListForCharacter(this.activeCharacter); const promptOrder = this.getPromptOrderForCharacter(this.activeCharacter);
// ToDo: These should be passed as parameter or attached to the manager as a set of default options. // ToDo: These should be passed as parameter or attached to the manager as a set of default options.
// Set default prompts and order for character. // Set default prompts and order for character.
if (0 === promptList.length) this.addPromptListForCharacter(this.activeCharacter, promptManagerDefaultPromptList); if (0 === promptOrder.length) this.addPromptOrderForCharacter(this.activeCharacter, promptManagerDefaultPromptOrder);
} }
PromptManagerModule.prototype.handleCharacterUpdated = function (event) { PromptManagerModule.prototype.handleCharacterUpdated = function (event) {
@ -737,9 +737,9 @@ PromptManagerModule.prototype.handleCharacterUpdated = function (event) {
PromptManagerModule.prototype.handleGroupSelected = function (event) { PromptManagerModule.prototype.handleGroupSelected = function (event) {
const characterDummy = {id: event.detail.id, group: event.detail.group}; const characterDummy = {id: event.detail.id, group: event.detail.group};
this.activeCharacter = characterDummy; this.activeCharacter = characterDummy;
const promptList = this.getPromptListForCharacter(characterDummy); const promptOrder = this.getPromptOrderForCharacter(characterDummy);
if (0 === promptList.length) this.addPromptListForCharacter(characterDummy, promptManagerDefaultPromptList) if (0 === promptOrder.length) this.addPromptOrderForCharacter(characterDummy, promptManagerDefaultPromptOrder)
} }
PromptManagerModule.prototype.getActiveGroupCharacters = function() { PromptManagerModule.prototype.getActiveGroupCharacters = function() {
@ -754,7 +754,7 @@ PromptManagerModule.prototype.getActiveGroupCharacters = function() {
* @param onlyEnabled * @param onlyEnabled
*/ */
PromptManagerModule.prototype.getPromptsForCharacter = function (character, onlyEnabled = false) { PromptManagerModule.prototype.getPromptsForCharacter = function (character, onlyEnabled = false) {
return this.getPromptListForCharacter(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);
} }
@ -764,8 +764,8 @@ PromptManagerModule.prototype.getPromptsForCharacter = function (character, only
* @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.getPromptListForCharacter = function (character) { PromptManagerModule.prototype.getPromptOrderForCharacter = function (character) {
return !character ? [] : (this.serviceSettings.prompt_lists.find(list => String(list.character_id) === String(character.id))?.list ?? []); return !character ? [] : (this.serviceSettings.prompt_order.find(list => String(list.character_id) === String(character.id))?.order ?? []);
} }
/** /**
@ -782,20 +782,20 @@ PromptManagerModule.prototype.setPrompts = function (prompts) {
* @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.removePromptListForCharacter = function (character) { PromptManagerModule.prototype.removePromptOrderForCharacter = function (character) {
const index = this.serviceSettings.prompt_lists.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_lists.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>} promptList - Array of prompt objects * @param {Array<Object>} promptOrder - Array of prompt objects
*/ */
PromptManagerModule.prototype.addPromptListForCharacter = function (character, promptList) { PromptManagerModule.prototype.addPromptOrderForCharacter = function (character, promptOrder) {
this.serviceSettings.prompt_lists.push({ this.serviceSettings.prompt_order.push({
character_id: character.id, character_id: character.id,
list: JSON.parse(JSON.stringify(promptList)) order: JSON.parse(JSON.stringify(promptOrder))
}); });
} }
@ -805,8 +805,8 @@ PromptManagerModule.prototype.addPromptListForCharacter = function (character, p
* @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.getPromptListEntry = function (character, identifier) { PromptManagerModule.prototype.getPromptOrderEntry = function (character, identifier) {
return this.getPromptListForCharacter(character).find(entry => entry.identifier === identifier) ?? null; return this.getPromptOrderForCharacter(character).find(entry => entry.identifier === identifier) ?? null;
} }
/** /**
@ -827,10 +827,6 @@ PromptManagerModule.prototype.getPromptIndexById = function (identifier) {
return this.serviceSettings.prompts.findIndex(item => item.identifier === identifier) ?? null; return this.serviceSettings.prompts.findIndex(item => item.identifier === identifier) ?? null;
} }
PromptManagerModule.prototype.getSettings = function(settings) {
return this.serviceSettings.prompt_manager_settings[settings] ?? null;
}
/** /**
* Prepares a prompt by creating a new object with its role and content. * Prepares a prompt by creating a new object with its role and content.
* @param {Object} prompt - Prompt object * @param {Object} prompt - Prompt object
@ -960,10 +956,10 @@ PromptManagerModule.prototype.clearInspectForm = function() {
* @returns {PromptCollection} A PromptCollection object * @returns {PromptCollection} A PromptCollection object
*/ */
PromptManagerModule.prototype.getPromptCollection = function () { PromptManagerModule.prototype.getPromptCollection = function () {
const promptList = this.getPromptListForCharacter(this.activeCharacter); const promptOrder = this.getPromptOrderForCharacter(this.activeCharacter);
const promptCollection = new PromptCollection(); const promptCollection = new PromptCollection();
promptList.forEach(entry => { promptOrder.forEach(entry => {
if (true === entry.enabled) { if (true === entry.enabled) {
const prompt = this.getPromptById(entry.identifier); const prompt = this.getPromptById(entry.identifier);
if (prompt) promptCollection.add(this.preparePrompt(prompt)); if (prompt) promptCollection.add(this.preparePrompt(prompt));
@ -1191,7 +1187,7 @@ PromptManagerModule.prototype.renderPromptManagerListItems = function () {
prompt.identifier !== 'characterInfo' && prompt.identifier !== 'characterInfo' &&
!advancedEnabled) visibleClass = `${prefix}prompt_manager_prompt_invisible`; !advancedEnabled) visibleClass = `${prefix}prompt_manager_prompt_invisible`;
const listEntry = this.getPromptListEntry(this.activeCharacter, prompt.identifier); const listEntry = this.getPromptOrderEntry(this.activeCharacter, prompt.identifier);
const enabledClass = listEntry.enabled ? '' : `${prefix}prompt_manager_prompt_disabled`; const enabledClass = listEntry.enabled ? '' : `${prefix}prompt_manager_prompt_disabled`;
const draggableClass = `${prefix}prompt_manager_prompt_draggable`; const draggableClass = `${prefix}prompt_manager_prompt_draggable`;
const markerClass = prompt.marker ? `${prefix}prompt_manager_marker` : ''; const markerClass = prompt.marker ? `${prefix}prompt_manager_marker` : '';
@ -1347,7 +1343,7 @@ PromptManagerModule.prototype.import = function (importData) {
type: '', type: '',
data: { data: {
prompts: [], prompts: [],
promptList: null prompt_order: null
} }
} }
@ -1362,8 +1358,8 @@ PromptManagerModule.prototype.import = function (importData) {
this.log('Prompt import succeeded'); this.log('Prompt import succeeded');
if ('character' === importData.type) { if ('character' === importData.type) {
const promptList = this.getPromptListForCharacter(this.activeCharacter); const promptOrder = this.getPromptOrderForCharacter(this.activeCharacter);
Object.assign(promptList, importData.data.promptList); Object.assign(promptOrder, importData.data.prompt_order);
this.log(`Prompt order import for character ${this.activeCharacter.name} completed`); this.log(`Prompt order import for character ${this.activeCharacter.name} completed`);
} }
@ -1411,15 +1407,15 @@ PromptManagerModule.prototype.makeDraggable = function () {
$(`#${this.configuration.prefix}prompt_manager_list`).sortable({ $(`#${this.configuration.prefix}prompt_manager_list`).sortable({
items: `.${this.configuration.prefix}prompt_manager_prompt_draggable`, items: `.${this.configuration.prefix}prompt_manager_prompt_draggable`,
update: ( event, ui ) => { update: ( event, ui ) => {
const promptList = this.getPromptListForCharacter(this.activeCharacter); const promptOrder = this.getPromptOrderForCharacter(this.activeCharacter);
const promptOrder = $(`#${this.configuration.prefix}prompt_manager_list`).sortable('toArray', {attribute: 'data-pm-identifier'}); const promptListElement = $(`#${this.configuration.prefix}prompt_manager_list`).sortable('toArray', {attribute: 'data-pm-identifier'});
const idToObjectMap = new Map(promptList.map(prompt => [prompt.identifier, prompt])); const idToObjectMap = new Map(promptOrder.map(prompt => [prompt.identifier, prompt]));
const newPromptList = promptOrder.map(identifier => idToObjectMap.get(identifier)); const updatedPromptOrder = promptListElement.map(identifier => idToObjectMap.get(identifier));
this.removePromptListForCharacter(this.activeCharacter); this.removePromptOrderForCharacter(this.activeCharacter);
this.addPromptListForCharacter(this.activeCharacter, newPromptList); this.addPromptOrderForCharacter(this.activeCharacter, updatedPromptOrder);
this.log('Prompt order updated.') this.log(`Prompt order updated for ${this.activeCharacter.name}.`);
this.saveServiceSettings(); this.saveServiceSettings();
}}); }});
@ -1567,11 +1563,11 @@ const chatCompletionDefaultPrompts = {
] ]
}; };
const promptManagerDefaultPromptLists = { const promptManagerDefaultPromptOrders = {
"prompt_lists": [] "prompt_order": []
}; };
const promptManagerDefaultPromptList = [ const promptManagerDefaultPromptOrder = [
{ {
"identifier": "main", "identifier": "main",
"enabled": true "enabled": true
@ -1628,7 +1624,7 @@ export {
PromptManagerModule, PromptManagerModule,
registerPromptManagerMigration, registerPromptManagerMigration,
chatCompletionDefaultPrompts, chatCompletionDefaultPrompts,
promptManagerDefaultPromptLists, promptManagerDefaultPromptOrders,
promptManagerDefaultSettings, promptManagerDefaultSettings,
Prompt Prompt
}; };

View File

@ -29,7 +29,7 @@ import {groups, selected_group} from "./group-chats.js";
import { import {
promptManagerDefaultSettings, promptManagerDefaultSettings,
promptManagerDefaultPromptLists, promptManagerDefaultPromptOrders,
chatCompletionDefaultPrompts, Prompt, chatCompletionDefaultPrompts, Prompt,
PromptManagerModule as PromptManager PromptManagerModule as PromptManager
} from "./PromptManager.js"; } from "./PromptManager.js";
@ -143,7 +143,7 @@ const default_settings = {
wrap_in_quotes: false, wrap_in_quotes: false,
names_in_completion: false, names_in_completion: false,
...chatCompletionDefaultPrompts, ...chatCompletionDefaultPrompts,
...promptManagerDefaultPromptLists, ...promptManagerDefaultPromptOrders,
...promptManagerDefaultSettings, ...promptManagerDefaultSettings,
send_if_empty: '', send_if_empty: '',
impersonation_prompt: default_impersonation_prompt, impersonation_prompt: default_impersonation_prompt,
@ -182,7 +182,7 @@ const oai_settings = {
wrap_in_quotes: false, wrap_in_quotes: false,
names_in_completion: false, names_in_completion: false,
...chatCompletionDefaultPrompts, ...chatCompletionDefaultPrompts,
...promptManagerDefaultPromptLists, ...promptManagerDefaultPromptOrders,
...promptManagerDefaultSettings, ...promptManagerDefaultSettings,
send_if_empty: '', send_if_empty: '',
impersonation_prompt: default_impersonation_prompt, impersonation_prompt: default_impersonation_prompt,
@ -1824,7 +1824,7 @@ function loadOpenAISettings(data, settings) {
oai_settings.assistant_prefill = settings.assistant_prefill ?? default_settings.assistant_prefill; oai_settings.assistant_prefill = settings.assistant_prefill ?? default_settings.assistant_prefill;
oai_settings.prompts = settings.prompts ?? default_settings.prompts; oai_settings.prompts = settings.prompts ?? default_settings.prompts;
oai_settings.prompt_lists = settings.prompt_lists ?? default_settings.prompt_lists; oai_settings.prompt_order = settings.prompt_order ?? default_settings.prompt_order;
oai_settings.prompt_manager_settings = settings.prompt_manager_settings ?? default_settings.prompt_manager_settings; oai_settings.prompt_manager_settings = settings.prompt_manager_settings ?? default_settings.prompt_manager_settings;
oai_settings.new_chat_prompt = settings.new_chat_prompt ?? default_settings.new_chat_prompt; oai_settings.new_chat_prompt = settings.new_chat_prompt ?? default_settings.new_chat_prompt;
@ -2045,7 +2045,7 @@ async function saveOpenAIPreset(name, settings) {
wi_format: settings.wi_format, wi_format: settings.wi_format,
stream_openai: settings.stream_openai, stream_openai: settings.stream_openai,
prompts: settings.prompts, prompts: settings.prompts,
prompt_lists: settings.prompt_lists, prompt_order: settings.prompt_order,
prompt_manager_settings: settings.prompt_manager_settings, prompt_manager_settings: settings.prompt_manager_settings,
api_url_scale: settings.api_url_scale, api_url_scale: settings.api_url_scale,
show_external_models: settings.show_external_models, show_external_models: settings.show_external_models,
@ -2379,7 +2379,7 @@ function onSettingsPresetChange() {
wi_format: ['#wi_format_textarea', 'wi_format', false], wi_format: ['#wi_format_textarea', 'wi_format', false],
stream_openai: ['#stream_toggle', 'stream_openai', true], stream_openai: ['#stream_toggle', 'stream_openai', true],
prompts: ['', 'prompts', false], prompts: ['', 'prompts', false],
prompt_lists: ['', 'prompt_lists', false], prompt_order: ['', 'prompt_order', false],
prompt_manager_settings: ['', 'prompt_manager_settings', false], prompt_manager_settings: ['', 'prompt_manager_settings', false],
use_openrouter: ['#use_openrouter', 'use_openrouter', true], use_openrouter: ['#use_openrouter', 'use_openrouter', true],
api_url_scale: ['#api_url_scale', 'api_url_scale', false], api_url_scale: ['#api_url_scale', 'api_url_scale', false],