SillyTavern/src/users.js
2024-04-07 03:01:55 +03:00

353 lines
13 KiB
JavaScript

const path = require('path');
const fs = require('fs');
const { USER_DIRECTORY_TEMPLATE, DEFAULT_USER, PUBLIC_DIRECTORIES } = require('./constants');
const { getConfigValue, color, delay } = require('./util');
const express = require('express');
const DATA_ROOT = getConfigValue('dataRoot', './data');
/**
* @typedef {Object} User
* @property {string} uuid - The user's id
* @property {string} handle - The user's short handle. Used for directories and other references
* @property {string} name - The user's name. Displayed in the UI
* @property {number} created - The timestamp when the user was created
* @property {string} password - SHA256 hash of the user's password
*/
/**
* @typedef {Object} UserDirectoryList
* @property {string} root - The root directory for the user
* @property {string} thumbnails - The directory where the thumbnails are stored
* @property {string} thumbnailsBg - The directory where the background thumbnails are stored
* @property {string} thumbnailsAvatar - The directory where the avatar thumbnails are stored
* @property {string} worlds - The directory where the WI are stored
* @property {string} user - The directory where the user's public data is stored
* @property {string} avatars - The directory where the avatars are stored
* @property {string} userImages - The directory where the images are stored
* @property {string} groups - The directory where the groups are stored
* @property {string} groupChats - The directory where the group chats are stored
* @property {string} chats - The directory where the chats are stored
* @property {string} characters - The directory where the characters are stored
* @property {string} backgrounds - The directory where the backgrounds are stored
* @property {string} novelAI_Settings - The directory where the NovelAI settings are stored
* @property {string} koboldAI_Settings - The directory where the KoboldAI settings are stored
* @property {string} openAI_Settings - The directory where the OpenAI settings are stored
* @property {string} textGen_Settings - The directory where the TextGen settings are stored
* @property {string} themes - The directory where the themes are stored
* @property {string} movingUI - The directory where the moving UI data is stored
* @property {string} extensions - The directory where the extensions are stored
* @property {string} instruct - The directory where the instruct templates is stored
* @property {string} context - The directory where the context templates is stored
* @property {string} quickreplies - The directory where the quick replies are stored
* @property {string} assets - The directory where the assets are stored
* @property {string} comfyWorkflows - The directory where the ComfyUI workflows are stored
* @property {string} files - The directory where the uploaded files are stored
* @property {string} vectors - The directory where the vectors are stored
*/
/**
* Perform migration from the old user data format to the new one.
*/
async function migrateUserData() {
const publicDirectory = path.join(process.cwd(), 'public');
// No need to migrate if the characters directory doesn't exists
if (!fs.existsSync(path.join(publicDirectory, 'characters'))) {
return;
}
const TIMEOUT = 10;
console.log();
console.log(color.magenta('Preparing to migrate user data...'));
console.log(`All public data will be moved to the ${DATA_ROOT} directory.`);
console.log('This process may take a while depending on the amount of data to move.');
console.log(`Backups will be placed in the ${PUBLIC_DIRECTORIES.backups} directory.`);
console.log(`The process will start in ${TIMEOUT} seconds. Press Ctrl+C to cancel.`);
for (let i = TIMEOUT; i > 0; i--) {
console.log(`${i}...`);
await delay(1000);
}
console.log(color.magenta('Starting migration... Do not interrupt the process!'));
const userDirectories = getUserDirectories(DEFAULT_USER.handle);
const dataMigrationMap = [
{
old: path.join(publicDirectory, 'assets'),
new: userDirectories.assets,
file: false,
},
{
old: path.join(publicDirectory, 'backgrounds'),
new: userDirectories.backgrounds,
file: false,
},
{
old: path.join(publicDirectory, 'characters'),
new: userDirectories.characters,
file: false,
},
{
old: path.join(publicDirectory, 'chats'),
new: userDirectories.chats,
file: false,
},
{
old: path.join(publicDirectory, 'context'),
new: userDirectories.context,
file: false,
},
{
old: path.join(publicDirectory, 'group chats'),
new: userDirectories.groupChats,
file: false,
},
{
old: path.join(publicDirectory, 'groups'),
new: userDirectories.groups,
file: false,
},
{
old: path.join(publicDirectory, 'instruct'),
new: userDirectories.instruct,
file: false,
},
{
old: path.join(publicDirectory, 'KoboldAI Settings'),
new: userDirectories.koboldAI_Settings,
file: false,
},
{
old: path.join(publicDirectory, 'movingUI'),
new: userDirectories.movingUI,
file: false,
},
{
old: path.join(publicDirectory, 'NovelAI Settings'),
new: userDirectories.novelAI_Settings,
file: false,
},
{
old: path.join(publicDirectory, 'OpenAI Settings'),
new: userDirectories.openAI_Settings,
file: false,
},
{
old: path.join(publicDirectory, 'QuickReplies'),
new: userDirectories.quickreplies,
file: false,
},
{
old: path.join(publicDirectory, 'TextGen Settings'),
new: userDirectories.textGen_Settings,
file: false,
},
{
old: path.join(publicDirectory, 'themes'),
new: userDirectories.themes,
file: false,
},
{
old: path.join(publicDirectory, 'user'),
new: userDirectories.user,
file: false,
},
{
old: path.join(publicDirectory, 'User Avatars'),
new: userDirectories.avatars,
file: false,
},
{
old: path.join(publicDirectory, 'worlds'),
new: userDirectories.worlds,
file: false,
},
{
old: path.join(publicDirectory, 'scripts/extensions/third-party'),
new: userDirectories.extensions,
file: false,
},
{
old: path.join(process.cwd(), 'thumbnails'),
new: userDirectories.thumbnails,
file: false,
},
{
old: path.join(process.cwd(), 'vectors'),
new: userDirectories.vectors,
file: false,
},
{
old: path.join(process.cwd(), 'secrets.json'),
new: path.join(userDirectories.root, 'secrets.json'),
file: true,
},
{
old: path.join(publicDirectory, 'settings.json'),
new: path.join(userDirectories.root, 'settings.json'),
file: true,
},
{
old: path.join(publicDirectory, 'stats.json'),
new: path.join(userDirectories.root, 'stats.json'),
file: true,
},
];
const currentDate = new Date().toISOString().split('T')[0];
const backupDirectory = path.join(process.cwd(), PUBLIC_DIRECTORIES.backups, '_migration', currentDate);
if (!fs.existsSync(backupDirectory)) {
fs.mkdirSync(backupDirectory, { recursive: true });
}
const errors = [];
for (const migration of dataMigrationMap) {
console.log(`Migrating ${migration.old} to ${migration.new}...`);
try {
if (!fs.existsSync(migration.old)) {
console.log(color.yellow(`Skipping migration of ${migration.old} as it does not exist.`));
continue;
}
if (migration.file) {
// Copy the file to the new location
fs.cpSync(migration.old, migration.new, { force: true });
// Move the file to the backup location
fs.renameSync(migration.old, path.join(backupDirectory, path.basename(migration.old)));
} else {
// Copy the directory to the new location
fs.cpSync(migration.old, migration.new, { recursive: true, force: true });
// Move the directory to the backup location
fs.renameSync(migration.old, path.join(backupDirectory, path.basename(migration.old)));
}
} catch (error) {
console.error(color.red(`Error migrating ${migration.old} to ${migration.new}:`), error.message);
errors.push(migration.old);
}
}
if (errors.length > 0) {
console.log(color.red('Migration completed with errors. Move the following files manually:'));
errors.forEach(error => console.error(error));
}
console.log(color.green('Migration completed!'));
}
/**
* Initializes the user storage. Currently a no-op.
* @returns {Promise<void>}
*/
async function initUserStorage() {
return Promise.resolve();
}
/**
* Gets a user for the current request. Hard coded to return the default user.
* @param {import('express').Request} _req - The request object. Currently unused.
* @returns {Promise<string>} - The user's handle
*/
async function getCurrentUserHandle(_req) {
return DEFAULT_USER.handle;
}
/**
* Gets a list of all user handles. Currently hard coded to return the default user's handle.
* @returns {Promise<string[]>} - The list of user handles
*/
async function getAllUserHandles() {
return [DEFAULT_USER.handle];
}
/**
* Gets the directories listing for the provided user.
* @param {import('express').Request} req - The request object
* @returns {Promise<UserDirectoryList>} - The user's directories like {worlds: 'data/user0/worlds/', ...
*/
async function getCurrentUserDirectories(req) {
const handle = await getCurrentUserHandle(req);
return getUserDirectories(handle);
}
/**
* Gets the directories listing for the provided user.
* @param {string} handle User handle
* @returns {UserDirectoryList} User directories
*/
function getUserDirectories(handle) {
const directories = structuredClone(USER_DIRECTORY_TEMPLATE);
for (const key in directories) {
directories[key] = path.join(DATA_ROOT, handle, USER_DIRECTORY_TEMPLATE[key]);
}
return directories;
}
/**
* Middleware to add user data to the request object.
* @returns {import('express').RequestHandler}
*/
function userDataMiddleware() {
/**
* Middleware to add user data to the request object.
* @param {import('express').Request} req Request object
* @param {import('express').Response} res Response object
* @param {import('express').NextFunction} next Next function
*/
return async (req, res, next) => {
const directories = await getCurrentUserDirectories(req);
req.user = {
profile: DEFAULT_USER,
directories: directories,
};
next();
};
}
/**
* Creates a route handler for serving files from a specific directory.
* @param {(req: import('express').Request) => string} directoryFn A function that returns the directory path to serve files from
* @returns {import('express').RequestHandler}
*/
function createRouteHandler(directoryFn) {
return async (req, res) => {
try {
const directory = directoryFn(req);
const filePath = decodeURIComponent(req.params[0]);
return res.sendFile(filePath, { root: directory });
} catch (error) {
console.error(error);
return res.sendStatus(404);
}
};
}
/**
* Express router for serving files from the user's directories.
*/
const router = express.Router();
router.use('/backgrounds/*', createRouteHandler(req => req.user.directories.backgrounds));
router.use('/characters/*', createRouteHandler(req => req.user.directories.characters));
router.use('/User%20Avatars/*', createRouteHandler(req => req.user.directories.avatars));
router.use('/assets/*', createRouteHandler(req => req.user.directories.assets));
router.use('/user/images/*', createRouteHandler(req => req.user.directories.userImages));
router.use('/user/files/*', createRouteHandler(req => req.user.directories.files));
router.use('/scripts/extensions/third-party/*', createRouteHandler(req => req.user.directories.extensions));
module.exports = {
initUserStorage,
getCurrentUserDirectories,
getCurrentUserHandle,
getAllUserHandles,
getUserDirectories,
userDataMiddleware,
migrateUserData,
router,
};