Migrate sysprompts from instruct

This commit is contained in:
Cohee
2024-09-17 11:33:24 +00:00
parent 73ee869749
commit 2f7d694f54
3 changed files with 49 additions and 0 deletions

View File

@@ -9,6 +9,7 @@ const storage = require('node-persist');
const express = require('express');
const mime = require('mime-types');
const archiver = require('archiver');
const writeFileAtomicSync = require('write-file-atomic').sync;
const { USER_DIRECTORY_TEMPLATE, DEFAULT_USER, PUBLIC_DIRECTORIES, DEFAULT_AVATAR, SETTINGS_FILE } = require('./constants');
const { getConfigValue, color, delay, setConfigValue, generateTimestamp } = require('./util');
@@ -323,6 +324,36 @@ async function migrateUserData() {
console.log(color.green('Migration completed!'));
}
async function migrateSystemPrompts() {
const directories = await getUserDirectoriesList();
for (const directory of directories) {
try {
const migrateMarker = path.join(directory.sysprompt, '.migrated');
if (fs.existsSync(migrateMarker)) {
continue;
}
const instucts = fs.readdirSync(directory.instruct);
for (const instruct of instucts) {
const instructPath = path.join(directory.instruct, instruct);
const syspromptPath = path.join(directory.sysprompt, instruct);
if (path.extname(instruct) === '.json' && !fs.existsSync(syspromptPath)) {
const instructData = JSON.parse(fs.readFileSync(instructPath, 'utf8'));
if ('system_prompt' in instructData && 'name' in instructData) {
const syspromptData = { name: instructData.name, content: instructData.system_prompt };
writeFileAtomicSync(syspromptPath, JSON.stringify(syspromptData, null, 4));
delete instructData.system_prompt;
writeFileAtomicSync(instructPath, JSON.stringify(instructData, null, 4));
console.log(`Migrated system prompt ${instruct} for ${directory.root.split(path.sep).pop()}`);
}
}
}
writeFileAtomicSync(migrateMarker, '');
} catch {
// Ignore errors
}
}
}
/**
* Converts a user handle to a storage key.
* @param {string} handle User handle
@@ -724,6 +755,7 @@ module.exports = {
requireLoginMiddleware,
requireAdminMiddleware,
migrateUserData,
migrateSystemPrompts,
getPasswordSalt,
getPasswordHash,
getCsrfSecret,