2023-07-23 22:52:31 +02:00
|
|
|
import {
|
|
|
|
amount_gen,
|
|
|
|
characters,
|
|
|
|
eventSource,
|
|
|
|
event_types,
|
|
|
|
getRequestHeaders,
|
|
|
|
koboldai_setting_names,
|
|
|
|
koboldai_settings,
|
|
|
|
main_api,
|
|
|
|
max_context,
|
|
|
|
nai_settings,
|
|
|
|
novelai_setting_names,
|
|
|
|
novelai_settings,
|
2023-12-12 18:14:17 +01:00
|
|
|
online_status,
|
2023-07-23 22:52:31 +02:00
|
|
|
saveSettingsDebounced,
|
|
|
|
this_chid,
|
2023-12-02 19:04:51 +01:00
|
|
|
} from '../script.js';
|
|
|
|
import { groups, selected_group } from './group-chats.js';
|
|
|
|
import { instruct_presets } from './instruct-mode.js';
|
|
|
|
import { kai_settings } from './kai-settings.js';
|
2024-09-21 22:21:19 +02:00
|
|
|
import { Popup, POPUP_RESULT, POPUP_TYPE } from './popup.js';
|
2023-12-02 19:04:51 +01:00
|
|
|
import { context_presets, getContextSettings, power_user } from './power-user.js';
|
2024-05-12 21:15:05 +02:00
|
|
|
import { SlashCommand } from './slash-commands/SlashCommand.js';
|
|
|
|
import { ARGUMENT_TYPE, SlashCommandArgument } from './slash-commands/SlashCommandArgument.js';
|
2024-06-20 20:33:45 +02:00
|
|
|
import { enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js';
|
|
|
|
import { SlashCommandEnumValue, enumTypes } from './slash-commands/SlashCommandEnumValue.js';
|
2024-05-12 21:15:05 +02:00
|
|
|
import { SlashCommandParser } from './slash-commands/SlashCommandParser.js';
|
2024-09-17 23:38:15 +02:00
|
|
|
import { checkForSystemPromptInInstructTemplate, system_prompts } from './sysprompt.js';
|
2024-09-21 22:21:19 +02:00
|
|
|
import { renderTemplateAsync } from './templates.js';
|
2023-07-23 22:52:31 +02:00
|
|
|
import {
|
|
|
|
textgenerationwebui_preset_names,
|
|
|
|
textgenerationwebui_presets,
|
2023-12-03 16:03:19 +01:00
|
|
|
textgenerationwebui_settings as textgen_settings,
|
2023-12-02 19:04:51 +01:00
|
|
|
} from './textgen-settings.js';
|
|
|
|
import { download, parseJsonFile, waitUntilCondition } from './utils.js';
|
2023-07-23 22:52:31 +02:00
|
|
|
|
|
|
|
const presetManagers = {};
|
|
|
|
|
2023-12-12 18:14:17 +01:00
|
|
|
/**
|
|
|
|
* Automatically select a preset for current API based on character or group name.
|
|
|
|
*/
|
2023-07-23 22:52:31 +02:00
|
|
|
function autoSelectPreset() {
|
|
|
|
const presetManager = getPresetManager();
|
|
|
|
|
|
|
|
if (!presetManager) {
|
|
|
|
console.debug(`Preset Manager not found for API: ${main_api}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const name = selected_group ? groups.find(x => x.id == selected_group)?.name : characters[this_chid]?.name;
|
|
|
|
|
|
|
|
if (!name) {
|
|
|
|
console.debug(`Preset candidate not found for API: ${main_api}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const preset = presetManager.findPreset(name);
|
|
|
|
const selectedPreset = presetManager.getSelectedPreset();
|
|
|
|
|
|
|
|
if (preset === selectedPreset) {
|
|
|
|
console.debug(`Preset already selected for API: ${main_api}, name: ${name}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preset !== undefined && preset !== null) {
|
|
|
|
console.log(`Preset found for API: ${main_api}, name: ${name}`);
|
|
|
|
presetManager.selectPreset(preset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-12 18:14:17 +01:00
|
|
|
/**
|
|
|
|
* Gets a preset manager by API id.
|
|
|
|
* @param {string} apiId API id
|
|
|
|
* @returns {PresetManager} Preset manager
|
|
|
|
*/
|
2024-09-17 23:38:15 +02:00
|
|
|
export function getPresetManager(apiId = '') {
|
2023-08-20 22:29:43 +02:00
|
|
|
if (!apiId) {
|
|
|
|
apiId = main_api == 'koboldhorde' ? 'kobold' : main_api;
|
|
|
|
}
|
2023-07-23 22:52:31 +02:00
|
|
|
|
|
|
|
if (!Object.keys(presetManagers).includes(apiId)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return presetManagers[apiId];
|
|
|
|
}
|
|
|
|
|
2023-12-12 18:14:17 +01:00
|
|
|
/**
|
|
|
|
* Registers preset managers for all select elements with data-preset-manager-for attribute.
|
|
|
|
*/
|
2023-07-23 22:52:31 +02:00
|
|
|
function registerPresetManagers() {
|
|
|
|
$('select[data-preset-manager-for]').each((_, e) => {
|
2023-12-02 19:04:51 +01:00
|
|
|
const forData = $(e).data('preset-manager-for');
|
|
|
|
for (const apiId of forData.split(',')) {
|
2023-07-23 22:52:31 +02:00
|
|
|
console.debug(`Registering preset manager for API: ${apiId}`);
|
|
|
|
presetManagers[apiId] = new PresetManager($(e), apiId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class PresetManager {
|
|
|
|
constructor(select, apiId) {
|
|
|
|
this.select = select;
|
|
|
|
this.apiId = apiId;
|
|
|
|
}
|
|
|
|
|
2024-09-21 22:21:19 +02:00
|
|
|
static masterSections = {
|
|
|
|
'instruct': {
|
|
|
|
name: 'Instruct Template',
|
|
|
|
getData: () => {
|
|
|
|
const manager = getPresetManager('instruct');
|
|
|
|
const name = manager.getSelectedPresetName();
|
|
|
|
return manager.getPresetSettings(name);
|
|
|
|
},
|
|
|
|
setData: (data) => {
|
|
|
|
const manager = getPresetManager('instruct');
|
|
|
|
const name = data.name;
|
|
|
|
return manager.savePreset(name, data);
|
|
|
|
},
|
|
|
|
isValid: (data) => PresetManager.isPossiblyInstructData(data),
|
|
|
|
},
|
|
|
|
'context': {
|
|
|
|
name: 'Context Template',
|
|
|
|
getData: () => {
|
|
|
|
const manager = getPresetManager('context');
|
|
|
|
const name = manager.getSelectedPresetName();
|
|
|
|
return manager.getPresetSettings(name);
|
|
|
|
},
|
|
|
|
setData: (data) => {
|
|
|
|
const manager = getPresetManager('context');
|
|
|
|
const name = data.name;
|
|
|
|
return manager.savePreset(name, data);
|
|
|
|
},
|
|
|
|
isValid: (data) => PresetManager.isPossiblyContextData(data),
|
|
|
|
},
|
|
|
|
'sysprompt': {
|
|
|
|
name: 'System Prompt',
|
|
|
|
getData: () => {
|
|
|
|
const manager = getPresetManager('sysprompt');
|
|
|
|
const name = manager.getSelectedPresetName();
|
|
|
|
return manager.getPresetSettings(name);
|
|
|
|
},
|
|
|
|
setData: (data) => {
|
|
|
|
const manager = getPresetManager('sysprompt');
|
|
|
|
const name = data.name;
|
|
|
|
return manager.savePreset(name, data);
|
|
|
|
},
|
|
|
|
isValid: (data) => PresetManager.isPossiblySystemPromptData(data),
|
|
|
|
},
|
|
|
|
'preset': {
|
2024-09-21 22:28:38 +02:00
|
|
|
name: 'Text Completion Preset',
|
2024-09-21 22:21:19 +02:00
|
|
|
getData: () => {
|
|
|
|
const manager = getPresetManager('textgenerationwebui');
|
|
|
|
const name = manager.getSelectedPresetName();
|
|
|
|
const data = manager.getPresetSettings(name);
|
|
|
|
data['name'] = name;
|
|
|
|
return data;
|
|
|
|
},
|
|
|
|
setData: (data) => {
|
|
|
|
const manager = getPresetManager('textgenerationwebui');
|
|
|
|
const name = data.name;
|
|
|
|
return manager.savePreset(name, data);
|
|
|
|
},
|
|
|
|
isValid: (data) => PresetManager.isPossiblyTextCompletionData(data),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static isPossiblyInstructData(data) {
|
|
|
|
const instructProps = ['name', 'input_sequence', 'output_sequence'];
|
|
|
|
return data && instructProps.every(prop => Object.keys(data).includes(prop));
|
|
|
|
}
|
|
|
|
|
|
|
|
static isPossiblyContextData(data) {
|
|
|
|
const contextProps = ['name', 'story_string'];
|
|
|
|
return data && contextProps.every(prop => Object.keys(data).includes(prop));
|
|
|
|
}
|
|
|
|
|
|
|
|
static isPossiblySystemPromptData(data) {
|
|
|
|
const sysPromptProps = ['name', 'content'];
|
|
|
|
return data && sysPromptProps.every(prop => Object.keys(data).includes(prop));
|
|
|
|
}
|
|
|
|
|
|
|
|
static isPossiblyTextCompletionData(data) {
|
|
|
|
const textCompletionProps = ['temp', 'top_k', 'top_p', 'rep_pen'];
|
|
|
|
return data && textCompletionProps.every(prop => Object.keys(data).includes(prop));
|
|
|
|
}
|
|
|
|
|
2024-09-21 22:28:38 +02:00
|
|
|
/**
|
|
|
|
* Imports master settings from JSON data.
|
|
|
|
* @param {object} data Data to import
|
|
|
|
* @param {string} fileName File name
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
static async performMasterImport(data, fileName) {
|
2024-09-21 22:21:19 +02:00
|
|
|
if (!data || typeof data !== 'object') {
|
|
|
|
toastr.error('Invalid data provided for master import');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for legacy file imports
|
|
|
|
// 1. Instruct Template
|
|
|
|
if (this.isPossiblyInstructData(data)) {
|
|
|
|
toastr.info('Importing instruct template...', 'Instruct template detected');
|
|
|
|
return await getPresetManager('instruct').savePreset(data.name, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Context Template
|
|
|
|
if (this.isPossiblyContextData(data)) {
|
|
|
|
toastr.info('Importing as context template...', 'Context template detected');
|
|
|
|
return await getPresetManager('context').savePreset(data.name, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3. System Prompt
|
|
|
|
if (this.isPossiblySystemPromptData(data)) {
|
|
|
|
toastr.info('Importing as system prompt...', 'System prompt detected');
|
|
|
|
return await getPresetManager('sysprompt').savePreset(data.name, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4. Text Completion settings
|
|
|
|
if (this.isPossiblyTextCompletionData(data)) {
|
|
|
|
toastr.info('Importing as settings preset...', 'Text Completion settings detected');
|
2024-09-21 22:28:38 +02:00
|
|
|
return await getPresetManager('textgenerationwebui').savePreset(fileName, data);
|
2024-09-21 22:21:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const validSections = [];
|
|
|
|
for (const [key, section] of Object.entries(this.masterSections)) {
|
|
|
|
if (key in data && section.isValid(data[key])) {
|
|
|
|
validSections.push(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (validSections.length === 0) {
|
|
|
|
toastr.error('No valid sections found in imported data');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sectionNames = validSections.reduce((acc, key) => {
|
2024-09-22 19:50:36 +02:00
|
|
|
acc[key] = { key: key, name: this.masterSections[key].name, preset: data[key]?.name || '' };
|
2024-09-21 22:21:19 +02:00
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
const html = $(await renderTemplateAsync('masterImport', { sections: sectionNames }));
|
|
|
|
const popup = new Popup(html, POPUP_TYPE.CONFIRM, '', {
|
2024-09-21 22:30:31 +02:00
|
|
|
okButton: 'Import',
|
2024-09-21 22:21:19 +02:00
|
|
|
cancelButton: 'Cancel',
|
|
|
|
});
|
|
|
|
|
|
|
|
const result = await popup.show();
|
|
|
|
|
|
|
|
// Import cancelled
|
|
|
|
if (result !== POPUP_RESULT.AFFIRMATIVE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const importedSections = [];
|
|
|
|
const confirmedSections = html.find('input:checked').map((_, el) => el instanceof HTMLInputElement && el.value).get();
|
|
|
|
|
|
|
|
if (confirmedSections.length === 0) {
|
|
|
|
toastr.info('No sections selected for import');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const section of confirmedSections) {
|
|
|
|
const sectionData = data[section];
|
|
|
|
const masterSection = this.masterSections[section];
|
|
|
|
if (sectionData && masterSection) {
|
|
|
|
await masterSection.setData(sectionData);
|
|
|
|
importedSections.push(masterSection.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toastr.success(`Imported ${importedSections.length} settings: ${importedSections.join(', ')}`);
|
|
|
|
}
|
|
|
|
|
2024-09-21 22:28:38 +02:00
|
|
|
/**
|
|
|
|
* Exports master settings to JSON data.
|
|
|
|
* @returns {Promise<string>} JSON data
|
|
|
|
*/
|
2024-09-21 22:21:19 +02:00
|
|
|
static async performMasterExport() {
|
|
|
|
const sectionNames = Object.entries(this.masterSections).reduce((acc, [key, section]) => {
|
2024-09-22 19:47:05 +02:00
|
|
|
acc[key] = { key: key, name: section.name, checked: key !== 'preset' };
|
2024-09-21 22:21:19 +02:00
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
const html = $(await renderTemplateAsync('masterExport', { sections: sectionNames }));
|
|
|
|
|
|
|
|
const popup = new Popup(html, POPUP_TYPE.CONFIRM, '', {
|
|
|
|
okButton: 'Export',
|
|
|
|
cancelButton: 'Cancel',
|
|
|
|
});
|
|
|
|
|
|
|
|
const result = await popup.show();
|
|
|
|
|
|
|
|
// Export cancelled
|
|
|
|
if (result !== POPUP_RESULT.AFFIRMATIVE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const confirmedSections = html.find('input:checked').map((_, el) => el instanceof HTMLInputElement && el.value).get();
|
|
|
|
const data = {};
|
|
|
|
|
|
|
|
if (confirmedSections.length === 0) {
|
|
|
|
toastr.info('No sections selected for export');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const section of confirmedSections) {
|
|
|
|
const masterSection = this.masterSections[section];
|
|
|
|
if (masterSection) {
|
|
|
|
data[section] = masterSection.getData();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSON.stringify(data, null, 4);
|
|
|
|
}
|
|
|
|
|
2023-12-12 18:14:17 +01:00
|
|
|
/**
|
|
|
|
* Gets all preset names.
|
|
|
|
* @returns {string[]} List of preset names
|
|
|
|
*/
|
|
|
|
getAllPresets() {
|
|
|
|
return $(this.select).find('option').map((_, el) => el.text).toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds a preset by name.
|
|
|
|
* @param {string} name Preset name
|
|
|
|
* @returns {any} Preset value
|
|
|
|
*/
|
2023-07-23 22:52:31 +02:00
|
|
|
findPreset(name) {
|
2024-05-18 15:46:41 +02:00
|
|
|
return $(this.select).find('option').filter(function() {
|
|
|
|
return $(this).text() === name;
|
|
|
|
}).val();
|
2023-07-23 22:52:31 +02:00
|
|
|
}
|
|
|
|
|
2023-12-12 18:14:17 +01:00
|
|
|
/**
|
|
|
|
* Gets the selected preset value.
|
|
|
|
* @returns {any} Selected preset value
|
|
|
|
*/
|
2023-07-23 22:52:31 +02:00
|
|
|
getSelectedPreset() {
|
2023-12-02 19:04:51 +01:00
|
|
|
return $(this.select).find('option:selected').val();
|
2023-07-23 22:52:31 +02:00
|
|
|
}
|
|
|
|
|
2023-12-12 18:14:17 +01:00
|
|
|
/**
|
|
|
|
* Gets the selected preset name.
|
|
|
|
* @returns {string} Selected preset name
|
|
|
|
*/
|
2023-07-23 22:52:31 +02:00
|
|
|
getSelectedPresetName() {
|
2023-12-02 19:04:51 +01:00
|
|
|
return $(this.select).find('option:selected').text();
|
2023-07-23 22:52:31 +02:00
|
|
|
}
|
|
|
|
|
2023-12-12 18:14:17 +01:00
|
|
|
/**
|
|
|
|
* Selects a preset by option value.
|
|
|
|
* @param {string} value Preset option value
|
|
|
|
*/
|
|
|
|
selectPreset(value) {
|
2024-06-22 16:44:08 +02:00
|
|
|
const option = $(this.select).filter(function() {
|
|
|
|
return $(this).val() === value;
|
|
|
|
});
|
|
|
|
option.prop('selected', true);
|
2023-12-12 18:14:17 +01:00
|
|
|
$(this.select).val(value).trigger('change');
|
2023-07-23 22:52:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async updatePreset() {
|
2023-12-02 19:04:51 +01:00
|
|
|
const selected = $(this.select).find('option:selected');
|
2023-12-02 20:11:06 +01:00
|
|
|
console.log(selected);
|
2023-07-23 22:52:31 +02:00
|
|
|
|
|
|
|
if (selected.val() == 'gui') {
|
|
|
|
toastr.info('Cannot update GUI preset');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const name = selected.text();
|
|
|
|
await this.savePreset(name);
|
2024-09-03 21:59:41 +02:00
|
|
|
|
|
|
|
const successToast = !this.isAdvancedFormatting() ? 'Preset updated' : 'Template updated';
|
|
|
|
toastr.success(successToast);
|
2023-07-23 22:52:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async savePresetAs() {
|
2023-12-03 16:16:33 +01:00
|
|
|
const inputValue = this.getSelectedPresetName();
|
2024-09-03 21:59:41 +02:00
|
|
|
const popupText = !this.isAdvancedFormatting() ? '<h4>Hint: Use a character/group name to bind preset to a specific chat.</h4>' : '';
|
|
|
|
const headerText = !this.isAdvancedFormatting() ? 'Preset name:' : 'Template name:';
|
|
|
|
const name = await Popup.show.input(headerText, popupText, inputValue);
|
2023-08-16 20:43:38 +02:00
|
|
|
if (!name) {
|
|
|
|
console.log('Preset name not provided');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-23 22:52:31 +02:00
|
|
|
await this.savePreset(name);
|
2024-09-03 21:59:41 +02:00
|
|
|
|
|
|
|
const successToast = !this.isAdvancedFormatting() ? 'Preset saved' : 'Template saved';
|
|
|
|
toastr.success(successToast);
|
2023-07-23 22:52:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async savePreset(name, settings) {
|
2024-09-19 22:49:23 +02:00
|
|
|
if (this.apiId === 'instruct' && settings) {
|
2024-09-17 23:38:15 +02:00
|
|
|
await checkForSystemPromptInInstructTemplate(name, settings);
|
|
|
|
}
|
|
|
|
|
2023-08-22 22:03:55 +02:00
|
|
|
const preset = settings ?? this.getPresetSettings(name);
|
|
|
|
|
2024-06-28 03:28:16 +02:00
|
|
|
const response = await fetch('/api/presets/save', {
|
2023-12-02 19:04:51 +01:00
|
|
|
method: 'POST',
|
2023-07-23 22:52:31 +02:00
|
|
|
headers: getRequestHeaders(),
|
2023-12-02 21:06:57 +01:00
|
|
|
body: JSON.stringify({ preset, name, apiId: this.apiId }),
|
2023-07-23 22:52:31 +02:00
|
|
|
});
|
|
|
|
|
2024-06-28 03:28:16 +02:00
|
|
|
if (!response.ok) {
|
|
|
|
toastr.error('Check the server connection and reload the page to prevent data loss.', 'Preset could not be saved');
|
|
|
|
console.error('Preset could not be saved', response);
|
2024-06-28 10:09:22 +02:00
|
|
|
throw new Error('Preset could not be saved');
|
2023-07-23 22:52:31 +02:00
|
|
|
}
|
|
|
|
|
2024-06-28 03:28:16 +02:00
|
|
|
const data = await response.json();
|
2023-07-23 22:52:31 +02:00
|
|
|
name = data.name;
|
|
|
|
|
|
|
|
this.updateList(name, preset);
|
|
|
|
}
|
|
|
|
|
|
|
|
getPresetList() {
|
|
|
|
let presets = [];
|
|
|
|
let preset_names = {};
|
|
|
|
|
|
|
|
switch (this.apiId) {
|
2023-12-02 19:04:51 +01:00
|
|
|
case 'koboldhorde':
|
|
|
|
case 'kobold':
|
2023-07-23 22:52:31 +02:00
|
|
|
presets = koboldai_settings;
|
|
|
|
preset_names = koboldai_setting_names;
|
|
|
|
break;
|
2023-12-02 19:04:51 +01:00
|
|
|
case 'novel':
|
2023-07-23 22:52:31 +02:00
|
|
|
presets = novelai_settings;
|
|
|
|
preset_names = novelai_setting_names;
|
|
|
|
break;
|
2023-12-02 19:04:51 +01:00
|
|
|
case 'textgenerationwebui':
|
2023-07-23 22:52:31 +02:00
|
|
|
presets = textgenerationwebui_presets;
|
|
|
|
preset_names = textgenerationwebui_preset_names;
|
|
|
|
break;
|
2023-12-02 19:04:51 +01:00
|
|
|
case 'context':
|
2023-08-26 12:09:47 +02:00
|
|
|
presets = context_presets;
|
|
|
|
preset_names = context_presets.map(x => x.name);
|
|
|
|
break;
|
2023-12-02 19:04:51 +01:00
|
|
|
case 'instruct':
|
2023-08-20 23:56:53 +02:00
|
|
|
presets = instruct_presets;
|
|
|
|
preset_names = instruct_presets.map(x => x.name);
|
|
|
|
break;
|
2024-09-17 12:37:36 +02:00
|
|
|
case 'sysprompt':
|
|
|
|
presets = system_prompts;
|
|
|
|
preset_names = system_prompts.map(x => x.name);
|
|
|
|
break;
|
2023-07-23 22:52:31 +02:00
|
|
|
default:
|
|
|
|
console.warn(`Unknown API ID ${this.apiId}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return { presets, preset_names };
|
|
|
|
}
|
|
|
|
|
2023-08-20 23:56:53 +02:00
|
|
|
isKeyedApi() {
|
2024-09-22 19:06:26 +02:00
|
|
|
return this.apiId == 'textgenerationwebui' || this.isAdvancedFormatting();
|
2023-08-20 23:56:53 +02:00
|
|
|
}
|
|
|
|
|
2024-09-03 21:59:41 +02:00
|
|
|
isAdvancedFormatting() {
|
2024-09-17 12:37:36 +02:00
|
|
|
return this.apiId == 'context' || this.apiId == 'instruct' || this.apiId == 'sysprompt';
|
2023-08-20 23:56:53 +02:00
|
|
|
}
|
|
|
|
|
2023-07-23 22:52:31 +02:00
|
|
|
updateList(name, preset) {
|
|
|
|
const { presets, preset_names } = this.getPresetList();
|
2023-08-20 23:56:53 +02:00
|
|
|
const presetExists = this.isKeyedApi() ? preset_names.includes(name) : Object.keys(preset_names).includes(name);
|
2023-07-23 22:52:31 +02:00
|
|
|
|
|
|
|
if (presetExists) {
|
2023-08-20 23:56:53 +02:00
|
|
|
if (this.isKeyedApi()) {
|
2023-07-23 22:52:31 +02:00
|
|
|
presets[preset_names.indexOf(name)] = preset;
|
|
|
|
$(this.select).find(`option[value="${name}"]`).prop('selected', true);
|
2023-12-02 19:04:51 +01:00
|
|
|
$(this.select).val(name).trigger('change');
|
2023-07-23 22:52:31 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
const value = preset_names[name];
|
|
|
|
presets[value] = preset;
|
|
|
|
$(this.select).find(`option[value="${value}"]`).prop('selected', true);
|
2023-12-02 19:04:51 +01:00
|
|
|
$(this.select).val(value).trigger('change');
|
2023-07-23 22:52:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
presets.push(preset);
|
|
|
|
const value = presets.length - 1;
|
2023-08-20 23:56:53 +02:00
|
|
|
|
|
|
|
if (this.isKeyedApi()) {
|
2023-07-23 22:52:31 +02:00
|
|
|
preset_names[value] = name;
|
|
|
|
const option = $('<option></option>', { value: name, text: name, selected: true });
|
|
|
|
$(this.select).append(option);
|
2023-12-02 19:04:51 +01:00
|
|
|
$(this.select).val(name).trigger('change');
|
2023-07-23 22:52:31 +02:00
|
|
|
} else {
|
|
|
|
preset_names[name] = value;
|
|
|
|
const option = $('<option></option>', { value: value, text: name, selected: true });
|
|
|
|
$(this.select).append(option);
|
2023-12-02 19:04:51 +01:00
|
|
|
$(this.select).val(value).trigger('change');
|
2023-07-23 22:52:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-22 22:03:55 +02:00
|
|
|
getPresetSettings(name) {
|
2023-07-23 22:52:31 +02:00
|
|
|
function getSettingsByApiId(apiId) {
|
|
|
|
switch (apiId) {
|
2023-12-02 19:04:51 +01:00
|
|
|
case 'koboldhorde':
|
|
|
|
case 'kobold':
|
2023-07-23 22:52:31 +02:00
|
|
|
return kai_settings;
|
2023-12-02 19:04:51 +01:00
|
|
|
case 'novel':
|
2023-07-23 22:52:31 +02:00
|
|
|
return nai_settings;
|
2023-12-02 19:04:51 +01:00
|
|
|
case 'textgenerationwebui':
|
2023-12-03 16:03:19 +01:00
|
|
|
return textgen_settings;
|
2023-12-02 19:04:51 +01:00
|
|
|
case 'context': {
|
2023-10-20 07:56:17 +02:00
|
|
|
const context_preset = getContextSettings();
|
2023-08-26 12:09:47 +02:00
|
|
|
context_preset['name'] = name || power_user.context.preset;
|
|
|
|
return context_preset;
|
2023-12-02 16:14:06 +01:00
|
|
|
}
|
2023-12-02 19:04:51 +01:00
|
|
|
case 'instruct': {
|
2023-09-26 08:53:04 +02:00
|
|
|
const instruct_preset = structuredClone(power_user.instruct);
|
2023-08-26 12:09:47 +02:00
|
|
|
instruct_preset['name'] = name || power_user.instruct.preset;
|
|
|
|
return instruct_preset;
|
2023-12-02 16:14:06 +01:00
|
|
|
}
|
2024-09-17 12:37:36 +02:00
|
|
|
case 'sysprompt': {
|
|
|
|
const sysprompt_preset = structuredClone(power_user.sysprompt);
|
|
|
|
sysprompt_preset['name'] = name || power_user.sysprompt.preset;
|
|
|
|
return sysprompt_preset;
|
|
|
|
}
|
2023-07-23 22:52:31 +02:00
|
|
|
default:
|
|
|
|
console.warn(`Unknown API ID ${apiId}`);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-16 20:43:38 +02:00
|
|
|
const filteredKeys = [
|
|
|
|
'preset',
|
2024-07-12 22:43:21 +02:00
|
|
|
'streaming',
|
|
|
|
'truncation_length',
|
|
|
|
'n',
|
2023-08-16 20:43:38 +02:00
|
|
|
'streaming_url',
|
|
|
|
'stopping_strings',
|
2023-08-24 20:23:35 +02:00
|
|
|
'can_use_tokenization',
|
|
|
|
'can_use_streaming',
|
2023-08-16 20:43:38 +02:00
|
|
|
'preset_settings_novel',
|
|
|
|
'streaming_novel',
|
|
|
|
'nai_preamble',
|
|
|
|
'model_novel',
|
2023-10-07 18:25:36 +02:00
|
|
|
'streaming_kobold',
|
2023-12-02 19:04:51 +01:00
|
|
|
'enabled',
|
2023-12-21 14:12:30 +01:00
|
|
|
'bind_to_context',
|
2023-10-26 20:22:00 +02:00
|
|
|
'seed',
|
2023-12-20 14:51:00 +01:00
|
|
|
'legacy_api',
|
2023-11-07 23:17:13 +01:00
|
|
|
'mancer_model',
|
2023-12-20 14:51:00 +01:00
|
|
|
'togetherai_model',
|
|
|
|
'ollama_model',
|
2024-05-03 00:40:40 +02:00
|
|
|
'vllm_model',
|
2024-03-01 22:02:43 +01:00
|
|
|
'aphrodite_model',
|
2023-12-20 14:51:00 +01:00
|
|
|
'server_urls',
|
|
|
|
'type',
|
2024-01-05 18:15:07 +01:00
|
|
|
'custom_model',
|
|
|
|
'bypass_status_check',
|
2024-02-25 21:47:07 +01:00
|
|
|
'infermaticai_model',
|
2024-03-07 10:55:08 +01:00
|
|
|
'dreamgen_model',
|
2024-02-25 21:47:07 +01:00
|
|
|
'openrouter_model',
|
2024-06-28 14:20:15 +02:00
|
|
|
'featherless_model',
|
2024-02-29 10:33:47 +01:00
|
|
|
'max_tokens_second',
|
2024-05-06 18:26:20 +02:00
|
|
|
'openrouter_providers',
|
2024-07-19 22:34:16 +02:00
|
|
|
'openrouter_allow_fallbacks',
|
2024-09-08 21:23:25 +02:00
|
|
|
'tabby_model',
|
2023-08-16 20:43:38 +02:00
|
|
|
];
|
2023-07-23 22:52:31 +02:00
|
|
|
const settings = Object.assign({}, getSettingsByApiId(this.apiId));
|
|
|
|
|
|
|
|
for (const key of filteredKeys) {
|
2023-12-02 16:21:57 +01:00
|
|
|
if (Object.hasOwn(settings, key)) {
|
2023-07-23 22:52:31 +02:00
|
|
|
delete settings[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-03 21:59:41 +02:00
|
|
|
if (!this.isAdvancedFormatting()) {
|
2023-08-20 23:56:53 +02:00
|
|
|
settings['genamt'] = amount_gen;
|
|
|
|
settings['max_length'] = max_context;
|
|
|
|
}
|
2023-07-23 22:52:31 +02:00
|
|
|
|
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteCurrentPreset() {
|
2023-12-17 20:02:32 +01:00
|
|
|
const { preset_names, presets } = this.getPresetList();
|
2023-07-23 22:52:31 +02:00
|
|
|
const value = this.getSelectedPreset();
|
|
|
|
const nameToDelete = this.getSelectedPresetName();
|
|
|
|
|
|
|
|
if (value == 'gui') {
|
|
|
|
toastr.info('Cannot delete GUI preset');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$(this.select).find(`option[value="${value}"]`).remove();
|
|
|
|
|
2023-08-20 23:56:53 +02:00
|
|
|
if (this.isKeyedApi()) {
|
2023-12-17 20:02:32 +01:00
|
|
|
const index = preset_names.indexOf(nameToDelete);
|
|
|
|
preset_names.splice(index, 1);
|
|
|
|
presets.splice(index, 1);
|
2023-07-23 22:52:31 +02:00
|
|
|
} else {
|
|
|
|
delete preset_names[nameToDelete];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.keys(preset_names).length) {
|
|
|
|
const nextPresetName = Object.keys(preset_names)[0];
|
|
|
|
const newValue = preset_names[nextPresetName];
|
2024-07-11 01:38:28 +02:00
|
|
|
$(this.select).find(`option[value="${newValue}"]`).attr('selected', 'true');
|
2023-07-23 22:52:31 +02:00
|
|
|
$(this.select).trigger('change');
|
|
|
|
}
|
|
|
|
|
2023-09-16 16:36:54 +02:00
|
|
|
const response = await fetch('/api/presets/delete', {
|
2023-07-23 22:52:31 +02:00
|
|
|
method: 'POST',
|
|
|
|
headers: getRequestHeaders(),
|
|
|
|
body: JSON.stringify({ name: nameToDelete, apiId: this.apiId }),
|
|
|
|
});
|
|
|
|
|
2023-12-03 16:16:33 +01:00
|
|
|
return response.ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getDefaultPreset(name) {
|
|
|
|
const response = await fetch('/api/presets/restore', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: getRequestHeaders(),
|
|
|
|
body: JSON.stringify({ name, apiId: this.apiId }),
|
|
|
|
});
|
|
|
|
|
2023-07-23 22:52:31 +02:00
|
|
|
if (!response.ok) {
|
2024-09-03 21:59:41 +02:00
|
|
|
const errorToast = !this.isAdvancedFormatting() ? 'Failed to restore default preset' : 'Failed to restore default template';
|
|
|
|
toastr.error(errorToast);
|
2023-12-03 16:16:33 +01:00
|
|
|
return;
|
2023-07-23 22:52:31 +02:00
|
|
|
}
|
2023-12-03 16:16:33 +01:00
|
|
|
|
|
|
|
return await response.json();
|
2023-07-23 22:52:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-12 18:14:17 +01:00
|
|
|
/**
|
|
|
|
* Selects a preset by name for current API.
|
|
|
|
* @param {any} _ Named arguments
|
|
|
|
* @param {string} name Unnamed arguments
|
|
|
|
* @returns {Promise<string>} Selected or current preset name
|
|
|
|
*/
|
|
|
|
async function presetCommandCallback(_, name) {
|
|
|
|
const shouldReconnect = online_status !== 'no_connection';
|
|
|
|
const presetManager = getPresetManager();
|
|
|
|
const allPresets = presetManager.getAllPresets();
|
|
|
|
const currentPreset = presetManager.getSelectedPresetName();
|
|
|
|
|
|
|
|
if (!presetManager) {
|
|
|
|
console.debug(`Preset Manager not found for API: ${main_api}`);
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!name) {
|
|
|
|
console.log('No name provided for /preset command, using current preset');
|
|
|
|
return currentPreset;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.isArray(allPresets) || allPresets.length === 0) {
|
|
|
|
console.log(`No presets found for API: ${main_api}`);
|
|
|
|
return currentPreset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find exact match
|
|
|
|
const exactMatch = allPresets.find(p => p.toLowerCase().trim() === name.toLowerCase().trim());
|
|
|
|
|
|
|
|
if (exactMatch) {
|
|
|
|
console.log('Found exact preset match', exactMatch);
|
|
|
|
|
|
|
|
if (currentPreset !== exactMatch) {
|
|
|
|
const presetValue = presetManager.findPreset(exactMatch);
|
|
|
|
|
|
|
|
if (presetValue) {
|
|
|
|
presetManager.selectPreset(presetValue);
|
|
|
|
shouldReconnect && await waitForConnection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return exactMatch;
|
|
|
|
} else {
|
|
|
|
// Find fuzzy match
|
|
|
|
const fuse = new Fuse(allPresets);
|
|
|
|
const fuzzyMatch = fuse.search(name);
|
|
|
|
|
|
|
|
if (!fuzzyMatch.length) {
|
|
|
|
console.warn(`WARN: Preset found with name ${name}`);
|
|
|
|
return currentPreset;
|
|
|
|
}
|
|
|
|
|
|
|
|
const fuzzyPresetName = fuzzyMatch[0].item;
|
|
|
|
const fuzzyPresetValue = presetManager.findPreset(fuzzyPresetName);
|
|
|
|
|
|
|
|
if (fuzzyPresetValue) {
|
|
|
|
console.log('Found fuzzy preset match', fuzzyPresetName);
|
|
|
|
|
|
|
|
if (currentPreset !== fuzzyPresetName) {
|
|
|
|
presetManager.selectPreset(fuzzyPresetValue);
|
|
|
|
shouldReconnect && await waitForConnection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fuzzyPresetName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Waits for API connection to be established.
|
|
|
|
*/
|
|
|
|
async function waitForConnection() {
|
|
|
|
try {
|
2024-01-18 15:36:26 +01:00
|
|
|
await waitUntilCondition(() => online_status !== 'no_connection', 10000, 100);
|
2023-12-12 18:14:17 +01:00
|
|
|
} catch {
|
|
|
|
console.log('Timeout waiting for API to connect');
|
|
|
|
}
|
|
|
|
}
|
2023-07-23 22:52:31 +02:00
|
|
|
|
2023-12-12 18:14:17 +01:00
|
|
|
export async function initPresetManager() {
|
2023-07-23 22:52:31 +02:00
|
|
|
eventSource.on(event_types.CHAT_CHANGED, autoSelectPreset);
|
|
|
|
registerPresetManagers();
|
2024-05-12 21:15:05 +02:00
|
|
|
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'preset',
|
|
|
|
callback: presetCommandCallback,
|
|
|
|
returns: 'current preset',
|
|
|
|
unnamedArgumentList: [
|
2024-06-17 03:30:52 +02:00
|
|
|
SlashCommandArgument.fromProps({
|
|
|
|
description: 'name',
|
|
|
|
typeList: [ARGUMENT_TYPE.STRING],
|
2024-06-20 20:33:45 +02:00
|
|
|
enumProvider: () => getPresetManager().getAllPresets().map(preset => new SlashCommandEnumValue(preset, null, enumTypes.enum, enumIcons.preset)),
|
2024-06-17 03:30:52 +02:00
|
|
|
}),
|
2024-05-12 21:15:05 +02:00
|
|
|
],
|
|
|
|
helpString: `
|
|
|
|
<div>
|
|
|
|
Sets a preset by name for the current API. Gets the current preset if no name is provided.
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<strong>Example:</strong>
|
|
|
|
<ul>
|
|
|
|
<li>
|
|
|
|
<pre><code>/preset myPreset</code></pre>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<pre><code>/preset</code></pre>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
`,
|
|
|
|
}));
|
|
|
|
|
2023-12-12 18:14:17 +01:00
|
|
|
|
2023-12-02 19:04:51 +01:00
|
|
|
$(document).on('click', '[data-preset-manager-update]', async function () {
|
|
|
|
const apiId = $(this).data('preset-manager-update');
|
2023-08-20 22:29:43 +02:00
|
|
|
const presetManager = getPresetManager(apiId);
|
2023-07-23 22:52:31 +02:00
|
|
|
|
|
|
|
if (!presetManager) {
|
2023-08-20 22:29:43 +02:00
|
|
|
console.warn(`Preset Manager not found for API: ${apiId}`);
|
2023-07-23 22:52:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await presetManager.updatePreset();
|
|
|
|
});
|
|
|
|
|
2023-12-02 19:04:51 +01:00
|
|
|
$(document).on('click', '[data-preset-manager-new]', async function () {
|
|
|
|
const apiId = $(this).data('preset-manager-new');
|
2023-08-20 22:29:43 +02:00
|
|
|
const presetManager = getPresetManager(apiId);
|
2023-07-23 22:52:31 +02:00
|
|
|
|
|
|
|
if (!presetManager) {
|
2023-08-20 22:29:43 +02:00
|
|
|
console.warn(`Preset Manager not found for API: ${apiId}`);
|
2023-07-23 22:52:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await presetManager.savePresetAs();
|
|
|
|
});
|
|
|
|
|
2023-12-02 19:04:51 +01:00
|
|
|
$(document).on('click', '[data-preset-manager-export]', async function () {
|
|
|
|
const apiId = $(this).data('preset-manager-export');
|
2023-08-20 22:29:43 +02:00
|
|
|
const presetManager = getPresetManager(apiId);
|
2023-07-23 22:52:31 +02:00
|
|
|
|
|
|
|
if (!presetManager) {
|
2023-08-20 22:29:43 +02:00
|
|
|
console.warn(`Preset Manager not found for API: ${apiId}`);
|
2023-07-23 22:52:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-02 19:04:51 +01:00
|
|
|
const selected = $(presetManager.select).find('option:selected');
|
2023-07-23 22:52:31 +02:00
|
|
|
const name = selected.text();
|
2023-08-22 22:03:55 +02:00
|
|
|
const preset = presetManager.getPresetSettings(name);
|
2023-07-23 22:52:31 +02:00
|
|
|
const data = JSON.stringify(preset, null, 4);
|
2023-12-02 19:04:51 +01:00
|
|
|
download(data, `${name}.json`, 'application/json');
|
2023-07-23 22:52:31 +02:00
|
|
|
});
|
|
|
|
|
2023-12-02 19:04:51 +01:00
|
|
|
$(document).on('click', '[data-preset-manager-import]', async function () {
|
|
|
|
const apiId = $(this).data('preset-manager-import');
|
2023-08-20 23:56:53 +02:00
|
|
|
$(`[data-preset-manager-file="${apiId}"]`).trigger('click');
|
2023-07-23 22:52:31 +02:00
|
|
|
});
|
|
|
|
|
2023-12-02 19:04:51 +01:00
|
|
|
$(document).on('change', '[data-preset-manager-file]', async function (e) {
|
|
|
|
const apiId = $(this).data('preset-manager-file');
|
2023-08-20 23:56:53 +02:00
|
|
|
const presetManager = getPresetManager(apiId);
|
2023-07-23 22:52:31 +02:00
|
|
|
|
|
|
|
if (!presetManager) {
|
2023-08-20 23:56:53 +02:00
|
|
|
console.warn(`Preset Manager not found for API: ${apiId}`);
|
2023-07-23 22:52:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const file = e.target.files[0];
|
|
|
|
|
|
|
|
if (!file) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-16 19:01:45 +02:00
|
|
|
const fileName = file.name.replace('.json', '').replace('.settings', '');
|
2023-07-23 22:52:31 +02:00
|
|
|
const data = await parseJsonFile(file);
|
2023-10-16 19:01:45 +02:00
|
|
|
const name = data?.name ?? fileName;
|
|
|
|
data['name'] = name;
|
2023-07-23 22:52:31 +02:00
|
|
|
|
|
|
|
await presetManager.savePreset(name, data);
|
2024-09-03 21:59:41 +02:00
|
|
|
const successToast = !presetManager.isAdvancedFormatting() ? 'Preset imported' : 'Template imported';
|
|
|
|
toastr.success(successToast);
|
2023-07-23 22:52:31 +02:00
|
|
|
e.target.value = null;
|
|
|
|
});
|
|
|
|
|
2023-12-02 19:04:51 +01:00
|
|
|
$(document).on('click', '[data-preset-manager-delete]', async function () {
|
|
|
|
const apiId = $(this).data('preset-manager-delete');
|
2023-08-20 22:29:43 +02:00
|
|
|
const presetManager = getPresetManager(apiId);
|
2023-07-23 22:52:31 +02:00
|
|
|
|
|
|
|
if (!presetManager) {
|
2023-08-20 22:29:43 +02:00
|
|
|
console.warn(`Preset Manager not found for API: ${apiId}`);
|
2023-07-23 22:52:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-03 21:59:41 +02:00
|
|
|
const headerText = !presetManager.isAdvancedFormatting() ? 'Delete this preset?' : 'Delete this template?';
|
|
|
|
const confirm = await Popup.show.confirm(headerText, 'This action is irreversible and your current settings will be overwritten.');
|
2023-07-23 22:52:31 +02:00
|
|
|
if (!confirm) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-03 16:16:33 +01:00
|
|
|
const result = await presetManager.deleteCurrentPreset();
|
|
|
|
|
|
|
|
if (result) {
|
2024-09-03 21:59:41 +02:00
|
|
|
const successToast = !presetManager.isAdvancedFormatting() ? 'Preset deleted' : 'Template deleted';
|
|
|
|
toastr.success(successToast);
|
2023-12-03 16:16:33 +01:00
|
|
|
} else {
|
2024-09-03 21:59:41 +02:00
|
|
|
const warningToast = !presetManager.isAdvancedFormatting() ? 'Preset was not deleted from server' : 'Template was not deleted from server';
|
|
|
|
toastr.warning(warningToast);
|
2023-12-03 16:16:33 +01:00
|
|
|
}
|
|
|
|
|
2023-07-23 22:52:31 +02:00
|
|
|
saveSettingsDebounced();
|
|
|
|
});
|
2023-12-03 16:16:33 +01:00
|
|
|
|
2023-12-12 18:14:17 +01:00
|
|
|
$(document).on('click', '[data-preset-manager-restore]', async function () {
|
2023-12-03 16:16:33 +01:00
|
|
|
const apiId = $(this).data('preset-manager-restore');
|
|
|
|
const presetManager = getPresetManager(apiId);
|
|
|
|
|
|
|
|
if (!presetManager) {
|
|
|
|
console.warn(`Preset Manager not found for API: ${apiId}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const name = presetManager.getSelectedPresetName();
|
|
|
|
const data = await presetManager.getDefaultPreset(name);
|
|
|
|
|
|
|
|
if (name == 'gui') {
|
|
|
|
toastr.info('Cannot restore GUI preset');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.isDefault) {
|
|
|
|
if (Object.keys(data.preset).length === 0) {
|
2024-09-03 21:59:41 +02:00
|
|
|
const errorToast = !presetManager.isAdvancedFormatting() ? 'Default preset cannot be restored' : 'Default template cannot be restored';
|
|
|
|
toastr.error(errorToast);
|
2023-12-03 16:16:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-03 21:59:41 +02:00
|
|
|
const confirmText = !presetManager.isAdvancedFormatting()
|
|
|
|
? 'Resetting a <b>default preset</b> will restore the default settings.'
|
|
|
|
: 'Resetting a <b>default template</b> will restore the default settings.';
|
|
|
|
const confirm = await Popup.show.confirm('Are you sure?', confirmText);
|
2023-12-03 16:16:33 +01:00
|
|
|
if (!confirm) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await presetManager.deleteCurrentPreset();
|
|
|
|
await presetManager.savePreset(name, data.preset);
|
|
|
|
const option = presetManager.findPreset(name);
|
|
|
|
presetManager.selectPreset(option);
|
2024-09-03 21:59:41 +02:00
|
|
|
const successToast = !presetManager.isAdvancedFormatting() ? 'Default preset restored' : 'Default template restored';
|
|
|
|
toastr.success(successToast);
|
2023-12-03 16:16:33 +01:00
|
|
|
} else {
|
2024-09-03 21:59:41 +02:00
|
|
|
const confirmText = !presetManager.isAdvancedFormatting()
|
|
|
|
? 'Resetting a <b>custom preset</b> will restore to the last saved state.'
|
|
|
|
: 'Resetting a <b>custom template</b> will restore to the last saved state.';
|
|
|
|
const confirm = await Popup.show.confirm('Are you sure?', confirmText);
|
2023-12-03 16:16:33 +01:00
|
|
|
if (!confirm) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const option = presetManager.findPreset(name);
|
|
|
|
presetManager.selectPreset(option);
|
2024-09-03 21:59:41 +02:00
|
|
|
const successToast = !presetManager.isAdvancedFormatting() ? 'Preset restored' : 'Template restored';
|
|
|
|
toastr.success(successToast);
|
2023-12-03 16:16:33 +01:00
|
|
|
}
|
|
|
|
});
|
2024-09-21 22:21:19 +02:00
|
|
|
|
|
|
|
$('#af_master_import').on('click', () => {
|
|
|
|
$('#af_master_import_file').trigger('click');
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#af_master_import_file').on('change', async function (e) {
|
|
|
|
if (!(e.target instanceof HTMLInputElement)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const file = e.target.files[0];
|
|
|
|
|
|
|
|
if (!file) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = await parseJsonFile(file);
|
2024-09-21 22:28:38 +02:00
|
|
|
const fileName = file.name.replace('.json', '');
|
|
|
|
await PresetManager.performMasterImport(data, fileName);
|
2024-09-22 21:47:28 +02:00
|
|
|
e.target.value = null;
|
2024-09-21 22:21:19 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$('#af_master_export').on('click', async () => {
|
|
|
|
const data = await PresetManager.performMasterExport();
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const shortDate = new Date().toISOString().split('T')[0];
|
|
|
|
download(data, `ST-formatting-${shortDate}.json`, 'application/json');
|
|
|
|
});
|
2023-12-12 18:14:17 +01:00
|
|
|
}
|