mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Add preset restoration
This commit is contained in:
@ -135,18 +135,21 @@
|
||||
<span class="fa-solid fa-circle-question note-link-span"></span>
|
||||
</a>
|
||||
</h4>
|
||||
<div class="flex-container">
|
||||
<select id="settings_preset" data-preset-manager-for="kobold" class="flex1 text_pole">
|
||||
<div class="flex-container flexNoGap">
|
||||
<select id="settings_preset" data-preset-manager-for="kobold" class="flex1 flexBasis100p text_pole">
|
||||
<option value="gui" data-i18n="guikoboldaisettings">GUI KoboldAI Settings</option>
|
||||
</select>
|
||||
<div class="flex-container flexBasis100p justifyCenter">
|
||||
<input type="file" hidden data-preset-manager-file="kobold" accept=".json, .settings">
|
||||
<i data-newbie-hidden data-preset-manager-update="kobold" class="menu_button fa-solid fa-save" title="Update current preset" data-i18n="[title]Update current preset"></i>
|
||||
<i data-newbie-hidden data-preset-manager-new="kobold" class="menu_button fa-solid fa-plus" title="Create new preset" data-i18n="[title]Create new preset"></i>
|
||||
<i data-newbie-hidden data-preset-manager-new="kobold" class="menu_button fa-solid fa-paste" title="Save preset as" data-i18n="[title]Save preset as"></i>
|
||||
<i data-newbie-hidden data-preset-manager-import="kobold" class="menu_button fa-solid fa-file-import" title="Import preset" data-i18n="[title]Import preset"></i>
|
||||
<i data-newbie-hidden data-preset-manager-export="kobold" class="menu_button fa-solid fa-file-export" title="Export preset" data-i18n="[title]Export preset"></i>
|
||||
<i data-newbie-hidden data-preset-manager-restore="kobold" class="menu_button fa-solid fa-recycle" title="Restore current preset" data-i18n="[title]Restore current preset"></i>
|
||||
<i data-newbie-hidden data-preset-manager-delete="kobold" class="menu_button fa-solid fa-trash-can" title="Delete the preset" data-i18n="[title]Delete the preset"></i>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="novel_api-presets">
|
||||
<h4 class="margin0">
|
||||
<span data-i18n="novelaipreserts">NovelAI Presets</span>
|
||||
@ -154,48 +157,56 @@
|
||||
<span class="fa-solid fa-circle-question note-link-span"></span>
|
||||
</a>
|
||||
</h4>
|
||||
<div class="flex-container">
|
||||
<select id="settings_preset_novel" class="flex1 text_pole" data-preset-manager-for="novel">
|
||||
<div class="flex-container flexNoGap">
|
||||
<select id="settings_preset_novel" class="flex1 flexBasis100p text_pole" data-preset-manager-for="novel">
|
||||
<option value="gui" data-i18n="default">Default</option>
|
||||
</select>
|
||||
<div class="flex-container flexBasis100p justifyCenter">
|
||||
<input type="file" hidden data-preset-manager-file="novel" accept=".json, .settings">
|
||||
<i data-newbie-hidden data-preset-manager-update="novel" class="menu_button fa-solid fa-save" title="Update current preset" data-i18n="[title]Update current preset"></i>
|
||||
<i data-newbie-hidden data-preset-manager-new="novel" class="menu_button fa-solid fa-plus" title="Create new preset" data-i18n="[title]Create new preset"></i>
|
||||
<i data-newbie-hidden data-preset-manager-new="novel" class="menu_button fa-solid fa-paste" title="Save preset as" data-i18n="[title]Save preset as"></i>
|
||||
<i data-newbie-hidden data-preset-manager-import="novel" class="menu_button fa-solid fa-file-import" title="Import preset" data-i18n="[title]Import preset"></i>
|
||||
<i data-newbie-hidden data-preset-manager-export="novel" class="menu_button fa-solid fa-file-export" title="Export preset" data-i18n="[title]Export preset"></i>
|
||||
<i data-newbie-hidden data-preset-manager-restore="novel" class="menu_button fa-solid fa-recycle" title="Restore current preset" data-i18n="[title]Restore current preset"></i>
|
||||
<i data-newbie-hidden data-preset-manager-delete="novel" class="menu_button fa-solid fa-trash-can" title="Delete the preset" data-i18n="[title]Delete the preset"></i>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="openai_api-presets">
|
||||
<div>
|
||||
<h4 class="margin0"><span data-i18n="openaipresets">Chat Completion Presets</span></h4>
|
||||
<div class="flex-container">
|
||||
<div class="flex-container flexNoGap">
|
||||
<select id="settings_preset_openai" class="flex1 text_pole">
|
||||
<option value="gui" data-i18n="default">Default</option>
|
||||
</select>
|
||||
<div class="flex-container flexBasis100p justifyCenter">
|
||||
<input id="openai_preset_import_file" type="file" accept=".json,.settings" hidden />
|
||||
<i id="update_oai_preset" class="menu_button fa-solid fa-save" title="Update current preset" data-i18n="[title]Update current preset"></i>
|
||||
<i id="new_oai_preset" class="menu_button fa-solid fa-plus" title="Create new preset" data-i18n="[title]Create new preset"></i>
|
||||
<i id="new_oai_preset" class="menu_button fa-solid fa-paste" title="Save preset as" data-i18n="[title]Save preset as"></i>
|
||||
<i data-newbie-hidden title="Import preset" id="import_oai_preset" class="menu_button fa-solid fa-file-import" data-i18n="[title]Import preset"></i>
|
||||
<i data-newbie-hidden title="Export preset" id="export_oai_preset" class="menu_button fa-solid fa-file-export" data-i18n="[title]Export preset"></i>
|
||||
<i data-newbie-hidden id="delete_oai_preset" class="menu_button fa-solid fa-trash-can" title="Delete the preset" data-i18n="[title]Delete the preset"></i>
|
||||
<input id="openai_preset_import_file" type="file" accept=".json,.settings" hidden />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="textgenerationwebui_api-presets">
|
||||
<h4 class="margin0"><span data-i18n="Text Completion presets">Text Completion presets</span></h4>
|
||||
<div class="flex-container">
|
||||
<div class="flex-container flexNoGap">
|
||||
<select id="settings_preset_textgenerationwebui" class="flex1 text_pole" data-preset-manager-for="textgenerationwebui">
|
||||
</select>
|
||||
<div class="flex-container flexBasis100p justifyCenter">
|
||||
<input type="file" hidden data-preset-manager-file="textgenerationwebui" accept=".json, .settings">
|
||||
<i data-newbie-hidden data-preset-manager-update="textgenerationwebui" class="menu_button fa-solid fa-save" title="Update current preset" data-i18n="[title]Update current preset"></i>
|
||||
<i data-newbie-hidden data-preset-manager-new="textgenerationwebui" class="menu_button fa-solid fa-plus" title="Create new preset" data-i18n="[title]Create new preset"></i>
|
||||
<i data-newbie-hidden data-preset-manager-new="textgenerationwebui" class="menu_button fa-solid fa-paste" title="Save preset as" data-i18n="[title]Save preset as"></i>
|
||||
<i data-newbie-hidden data-preset-manager-import="textgenerationwebui" class="menu_button fa-solid fa-file-import" title="Import preset" data-i18n="[title]Import preset"></i>
|
||||
<i data-newbie-hidden data-preset-manager-export="textgenerationwebui" class="menu_button fa-solid fa-file-export" title="Export preset" data-i18n="[title]Export preset"></i>
|
||||
<i data-newbie-hidden data-preset-manager-restore="textgenerationwebui" class="menu_button fa-solid fa-recycle" title="Restore current preset" data-i18n="[title]Restore current preset"></i>
|
||||
<i data-newbie-hidden data-preset-manager-delete="textgenerationwebui" class="menu_button fa-solid fa-trash-can" title="Delete the preset" data-i18n="[title]Delete the preset"></i>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div data-newbie-hidden id="ai_module_block_novel" class="width100p">
|
||||
<div class="range-block">
|
||||
<div class="range-block-title justifyLeft" data-i18n="AI Module">
|
||||
@ -2150,7 +2161,7 @@
|
||||
<input type="file" hidden data-preset-manager-file="context" accept=".json, .settings">
|
||||
<i id="context_set_default" class="menu_button fa-solid fa-heart" title="Auto-select this preset for Instruct Mode."></i>
|
||||
<i data-newbie-hidden data-preset-manager-update="context" class="menu_button fa-solid fa-save" title="Update current preset" data-i18n="[title]Update current preset"></i>
|
||||
<i data-newbie-hidden data-preset-manager-new="context" class="menu_button fa-solid fa-plus" title="Create new preset" data-i18n="[title]Create new preset"></i>
|
||||
<i data-newbie-hidden data-preset-manager-new="context" class="menu_button fa-solid fa-paste" title="Save preset as" data-i18n="[title]Save preset as"></i>
|
||||
<i data-newbie-hidden data-preset-manager-import="context" class="menu_button fa-solid fa-file-import" title="Import preset" data-i18n="[title]Import preset"></i>
|
||||
<i data-newbie-hidden data-preset-manager-export="context" class="menu_button fa-solid fa-file-export" title="Export preset" data-i18n="[title]Export preset"></i>
|
||||
<i data-newbie-hidden id="context_delete_preset" data-preset-manager-delete="context" class="menu_button fa-solid fa-trash-can" title="Delete the preset" data-i18n="[title]Delete the preset"></i>
|
||||
@ -2241,7 +2252,7 @@
|
||||
<input type="file" hidden data-preset-manager-file="instruct" accept=".json, .settings">
|
||||
<i id="instruct_set_default" class="menu_button fa-solid fa-heart" title="Auto-select this preset on API connection."></i>
|
||||
<i data-newbie-hidden data-preset-manager-update="instruct" class="menu_button fa-solid fa-save" title="Update current preset" data-i18n="[title]Update current preset"></i>
|
||||
<i data-newbie-hidden data-preset-manager-new="instruct" class="menu_button fa-solid fa-plus" title="Create new preset" data-i18n="[title]Create new preset"></i>
|
||||
<i data-newbie-hidden data-preset-manager-new="instruct" class="menu_button fa-solid fa-paste" title="Save preset as" data-i18n="[title]Save preset as"></i>
|
||||
<i data-newbie-hidden data-preset-manager-import="instruct" class="menu_button fa-solid fa-file-import" title="Import preset" data-i18n="[title]Import preset"></i>
|
||||
<i data-newbie-hidden data-preset-manager-export="instruct" class="menu_button fa-solid fa-file-export" title="Export preset" data-i18n="[title]Export preset"></i>
|
||||
<i data-newbie-hidden data-preset-manager-delete="instruct" class="menu_button fa-solid fa-trash-can" title="Delete the preset" data-i18n="[title]Delete the preset"></i>
|
||||
|
@ -117,10 +117,11 @@ class PresetManager {
|
||||
}
|
||||
|
||||
async savePresetAs() {
|
||||
const inputValue = this.getSelectedPresetName();
|
||||
const popupText = `
|
||||
<h3>Preset name:</h3>
|
||||
${!this.isNonGenericApi() ? '<h4>Hint: Use a character/group name to bind preset to a specific chat.</h4>' : ''}`;
|
||||
const name = await callPopup(popupText, 'input');
|
||||
const name = await callPopup(popupText, 'input', inputValue);
|
||||
|
||||
if (!name) {
|
||||
console.log('Preset name not provided');
|
||||
@ -314,11 +315,22 @@ class PresetManager {
|
||||
body: JSON.stringify({ name: nameToDelete, apiId: this.apiId }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
toastr.warning('Preset was not deleted from server');
|
||||
} else {
|
||||
toastr.success('Preset deleted');
|
||||
return response.ok;
|
||||
}
|
||||
|
||||
async getDefaultPreset(name) {
|
||||
const response = await fetch('/api/presets/restore', {
|
||||
method: 'POST',
|
||||
headers: getRequestHeaders(),
|
||||
body: JSON.stringify({ name, apiId: this.apiId }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
toastr.error('Failed to restore default preset');
|
||||
return;
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
}
|
||||
}
|
||||
|
||||
@ -417,7 +429,65 @@ jQuery(async () => {
|
||||
return;
|
||||
}
|
||||
|
||||
await presetManager.deleteCurrentPreset();
|
||||
const result = await presetManager.deleteCurrentPreset();
|
||||
|
||||
if (result) {
|
||||
toastr.success('Preset deleted');
|
||||
} else {
|
||||
toastr.warning('Preset was not deleted from server');
|
||||
}
|
||||
|
||||
saveSettingsDebounced();
|
||||
});
|
||||
|
||||
$(document).on('click', '[data-preset-manager-restore]', async function() {
|
||||
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) {
|
||||
toastr.error('Default preset cannot be restored');
|
||||
return;
|
||||
}
|
||||
|
||||
const confirm = await callPopup('<h3>Are you sure?</h3>Resetting a <b>default preset</b> will restore the default settings.', 'confirm');
|
||||
|
||||
if (!confirm) {
|
||||
return;
|
||||
}
|
||||
|
||||
await presetManager.deleteCurrentPreset();
|
||||
await presetManager.savePreset(name, data.preset);
|
||||
const option = presetManager.findPreset(name);
|
||||
presetManager.selectPreset(option);
|
||||
toastr.success('Default preset restored');
|
||||
} else {
|
||||
const confirm = await callPopup('<h3>Are you sure?</h3>Resetting a <b>custom preset</b> will restore to the last saved state.', 'confirm');
|
||||
|
||||
if (!confirm) {
|
||||
return;
|
||||
}
|
||||
|
||||
const option = presetManager.findPreset(name);
|
||||
presetManager.selectPreset(option);
|
||||
toastr.success('Preset restored');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
@ -7,11 +7,56 @@ const contentDirectory = path.join(process.cwd(), 'default/content');
|
||||
const contentLogPath = path.join(contentDirectory, 'content.log');
|
||||
const contentIndexPath = path.join(contentDirectory, 'index.json');
|
||||
const { DIRECTORIES } = require('./constants');
|
||||
const presetFolders = [DIRECTORIES.koboldAI_Settings, DIRECTORIES.openAI_Settings, DIRECTORIES.novelAI_Settings, DIRECTORIES.textGen_Settings];
|
||||
|
||||
/**
|
||||
* Gets the default presets from the content directory.
|
||||
* @returns {object[]} Array of default presets
|
||||
*/
|
||||
function getDefaultPresets() {
|
||||
try {
|
||||
const contentIndexText = fs.readFileSync(contentIndexPath, 'utf8');
|
||||
const contentIndex = JSON.parse(contentIndexText);
|
||||
|
||||
const presets = [];
|
||||
|
||||
for (const contentItem of contentIndex) {
|
||||
if (contentItem.type.endsWith('_preset')) {
|
||||
contentItem.name = path.parse(contentItem.filename).name;
|
||||
contentItem.folder = getTargetByType(contentItem.type);
|
||||
presets.push(contentItem);
|
||||
}
|
||||
}
|
||||
|
||||
return presets;
|
||||
} catch (err) {
|
||||
console.log('Failed to get default presets', err);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a default JSON file from the content directory.
|
||||
* @param {string} filename Name of the file to get
|
||||
* @returns {object | null} JSON object or null if the file doesn't exist
|
||||
*/
|
||||
function getDefaultPresetFile(filename) {
|
||||
try {
|
||||
const contentPath = path.join(contentDirectory, filename);
|
||||
|
||||
if (!fs.existsSync(contentPath)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const fileContent = fs.readFileSync(contentPath, 'utf8');
|
||||
return JSON.parse(fileContent);
|
||||
} catch (err) {
|
||||
console.log(`Failed to get default file ${filename}`, err);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function migratePresets() {
|
||||
const presetFolders = [DIRECTORIES.koboldAI_Settings, DIRECTORIES.openAI_Settings, DIRECTORIES.novelAI_Settings, DIRECTORIES.textGen_Settings];
|
||||
|
||||
for (const presetFolder of presetFolders) {
|
||||
const presetPath = path.join(process.cwd(), presetFolder);
|
||||
const presetFiles = fs.readdirSync(presetPath);
|
||||
@ -20,9 +65,12 @@ function migratePresets() {
|
||||
const presetFilePath = path.join(presetPath, presetFile);
|
||||
const newFileName = presetFile.replace('.settings', '.json');
|
||||
const newFilePath = path.join(presetPath, newFileName);
|
||||
const backupFileName = presetFolder.replace('/', '_') + '_' + presetFile;
|
||||
const backupFilePath = path.join(DIRECTORIES.backups, backupFileName);
|
||||
|
||||
if (presetFilePath.endsWith('.settings')) {
|
||||
if (!fs.existsSync(newFilePath)) {
|
||||
fs.cpSync(presetFilePath, backupFilePath);
|
||||
fs.cpSync(presetFilePath, newFilePath);
|
||||
console.log(`Migrated ${presetFilePath} to ${newFilePath}`);
|
||||
}
|
||||
@ -310,4 +358,6 @@ function registerEndpoints(app, jsonParser) {
|
||||
module.exports = {
|
||||
checkForNewContent,
|
||||
registerEndpoints,
|
||||
getDefaultPresets,
|
||||
getDefaultPresetFile,
|
||||
};
|
||||
|
@ -3,6 +3,7 @@ const path = require('path');
|
||||
const sanitize = require('sanitize-filename');
|
||||
const writeFileAtomicSync = require('write-file-atomic').sync;
|
||||
const { DIRECTORIES } = require('./constants');
|
||||
const { getDefaultPresetFile, getDefaultPresets } = require('./content-manager');
|
||||
|
||||
/**
|
||||
* Gets the folder and extension for the preset settings based on the API source ID.
|
||||
@ -76,6 +77,28 @@ function registerEndpoints(app, jsonParser) {
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/presets/restore', jsonParser, function (request, response) {
|
||||
try {
|
||||
const settings = getPresetSettingsByAPI(request.body.apiId);
|
||||
const name = sanitize(request.body.name);
|
||||
const defaultPresets = getDefaultPresets();
|
||||
|
||||
const defaultPreset = defaultPresets.find(p => p.name === name && p.folder === settings.folder);
|
||||
|
||||
const result = { isDefault: false, preset: {} };
|
||||
|
||||
if (defaultPreset) {
|
||||
result.isDefault = true;
|
||||
result.preset = getDefaultPresetFile(defaultPreset.filename) || {};
|
||||
}
|
||||
|
||||
return response.send(result);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return response.sendStatus(500);
|
||||
}
|
||||
});
|
||||
|
||||
// TODO: Merge with /api/presets/save
|
||||
app.post('/api/presets/save-openai', jsonParser, function (request, response) {
|
||||
if (!request.body || typeof request.query.name !== 'string') return response.sendStatus(400);
|
||||
|
Reference in New Issue
Block a user