Move preset management into a separate file

This commit is contained in:
Cohee 2023-09-16 17:36:54 +03:00
parent 38b63b07f5
commit 61995bb33f
4 changed files with 101 additions and 74 deletions

View File

@ -2153,7 +2153,7 @@ async function saveOpenAIPreset(name, settings, triggerUi = true) {
use_alt_scale: settings.use_alt_scale,
};
const savePresetSettings = await fetch(`/savepreset_openai?name=${name}`, {
const savePresetSettings = await fetch(`/api/presets/save-openai?name=${name}`, {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify(presetBody),
@ -2313,7 +2313,7 @@ async function onPresetImportFileChange(e) {
}
}
const savePresetSettings = await fetch(`/savepreset_openai?name=${name}`, {
const savePresetSettings = await fetch(`/api/presets/save-openai?name=${name}`, {
method: 'POST',
headers: getRequestHeaders(),
body: importedFile,

View File

@ -133,7 +133,7 @@ class PresetManager {
async savePreset(name, settings) {
const preset = settings ?? this.getPresetSettings(name);
const res = await fetch(`/save_preset`, {
const res = await fetch(`/api/presets/save`, {
method: "POST",
headers: getRequestHeaders(),
body: JSON.stringify({ preset, name, apiId: this.apiId })
@ -303,7 +303,7 @@ class PresetManager {
$(this.select).trigger('change');
}
const response = await fetch('/delete_preset', {
const response = await fetch('/api/presets/delete', {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify({ name: nameToDelete, apiId: this.apiId }),

View File

@ -3449,76 +3449,6 @@ app.post("/tokenize_ai21", jsonParser, async function (request, response_tokeniz
}
});
app.post("/save_preset", jsonParser, function (request, response) {
const name = sanitize(request.body.name);
if (!request.body.preset || !name) {
return response.sendStatus(400);
}
const settings = getPresetSettingsByAPI(request.body.apiId);
const filename = name + settings.extension;
if (!settings.folder) {
return response.sendStatus(400);
}
const fullpath = path.join(settings.folder, filename);
writeFileAtomicSync(fullpath, JSON.stringify(request.body.preset, null, 4), 'utf-8');
return response.send({ name });
});
app.post("/delete_preset", jsonParser, function (request, response) {
const name = sanitize(request.body.name);
if (!name) {
return response.sendStatus(400);
}
const settings = getPresetSettingsByAPI(request.body.apiId);
const filename = name + settings.extension;
if (!settings.folder) {
return response.sendStatus(400);
}
const fullpath = path.join(settings.folder, filename);
if (fs.existsSync(fullpath)) {
fs.unlinkSync(fullpath);
return response.sendStatus(200);
} else {
return response.sendStatus(404);
}
});
app.post("/savepreset_openai", jsonParser, function (request, response) {
if (!request.body || typeof request.query.name !== 'string') return response.sendStatus(400);
const name = sanitize(request.query.name);
if (!name) return response.sendStatus(400);
const filename = `${name}.settings`;
const fullpath = path.join(DIRECTORIES.openAI_Settings, filename);
writeFileAtomicSync(fullpath, JSON.stringify(request.body, null, 4), 'utf-8');
return response.send({ name });
});
function getPresetSettingsByAPI(apiId) {
switch (apiId) {
case 'kobold':
case 'koboldhorde':
return { folder: DIRECTORIES.koboldAI_Settings, extension: '.settings' };
case 'novel':
return { folder: DIRECTORIES.novelAI_Settings, extension: '.settings' };
case 'textgenerationwebui':
return { folder: DIRECTORIES.textGen_Settings, extension: '.settings' };
case 'instruct':
return { folder: DIRECTORIES.instruct, extension: '.json' };
case 'context':
return { folder: DIRECTORIES.context, extension: '.json' };
default:
return { folder: null, extension: null };
}
}
function createSentencepieceEncodingHandler(getTokenizerFn) {
return async function (request, response) {
try {
@ -3663,6 +3593,9 @@ async function postAsync(url, args) { return fetchJSON(url, { method: 'POST', ti
// ** END **
// Preset management
require('./src/presets').registerEndpoints(app, jsonParser);
// Secrets managemenet
require('./src/secrets').registerEndpoints(app, jsonParser);

94
src/presets.js Normal file
View File

@ -0,0 +1,94 @@
const fs = require('fs');
const path = require('path');
const sanitize = require('sanitize-filename');
const writeFileAtomicSync = require('write-file-atomic').sync;
const { DIRECTORIES } = require('./constants');
/**
* Gets the folder and extension for the preset settings based on the API source ID.
* @param {string} apiId API source ID
* @returns {object} Object containing the folder and extension for the preset settings
*/
function getPresetSettingsByAPI(apiId) {
switch (apiId) {
case 'kobold':
case 'koboldhorde':
return { folder: DIRECTORIES.koboldAI_Settings, extension: '.settings' };
case 'novel':
return { folder: DIRECTORIES.novelAI_Settings, extension: '.settings' };
case 'textgenerationwebui':
return { folder: DIRECTORIES.textGen_Settings, extension: '.settings' };
case 'openai':
return { folder: DIRECTORIES.openAI_Settings, extension: '.settings' };
case 'instruct':
return { folder: DIRECTORIES.instruct, extension: '.json' };
case 'context':
return { folder: DIRECTORIES.context, extension: '.json' };
default:
return { folder: null, extension: null };
}
}
/**
* Registers the preset management endpoints.
* @param {import('express').Express} app Express app
* @param {any} jsonParser JSON parser middleware
*/
function registerEndpoints(app, jsonParser) {
app.post("/api/presets/save", jsonParser, function (request, response) {
const name = sanitize(request.body.name);
if (!request.body.preset || !name) {
return response.sendStatus(400);
}
const settings = getPresetSettingsByAPI(request.body.apiId);
const filename = name + settings.extension;
if (!settings.folder) {
return response.sendStatus(400);
}
const fullpath = path.join(settings.folder, filename);
writeFileAtomicSync(fullpath, JSON.stringify(request.body.preset, null, 4), 'utf-8');
return response.send({ name });
});
app.post("/api/presets/delete", jsonParser, function (request, response) {
const name = sanitize(request.body.name);
if (!name) {
return response.sendStatus(400);
}
const settings = getPresetSettingsByAPI(request.body.apiId);
const filename = name + settings.extension;
if (!settings.folder) {
return response.sendStatus(400);
}
const fullpath = path.join(settings.folder, filename);
if (fs.existsSync(fullpath)) {
fs.unlinkSync(fullpath);
return response.sendStatus(200);
} else {
return response.sendStatus(404);
}
});
// 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);
const name = sanitize(request.query.name);
if (!name) return response.sendStatus(400);
const filename = `${name}.settings`;
const fullpath = path.join(DIRECTORIES.openAI_Settings, filename);
writeFileAtomicSync(fullpath, JSON.stringify(request.body, null, 4), 'utf-8');
return response.send({ name });
});
}
module.exports = {
registerEndpoints,
}