Migrate sysprompts from instruct

2f7d694f5429fd95e342bd0825e96eee1cc66170

Cohee <18619528+Cohee1207@users.noreply.github.com>

3 files changed, +49 -0Showing whitespace changes
public/scripts/sysprompt.js+16 -0
@@ -9,6 +9,21 @@ const $select = $('#sysprompt_select');
9const $content = $('#sysprompt_content');9const $content = $('#sysprompt_content');
10const $contentBlock = $('#SystemPromptBlock');10const $contentBlock = $('#SystemPromptBlock');
1111
12function migrateSystemPromptFromInstructMode() {
13 if ('system_prompt' in power_user.instruct) {
14 power_user.sysprompt.enabled = power_user.instruct.enabled;
15 power_user.sysprompt.content = String(power_user.instruct.system_prompt);
16 delete power_user.instruct.system_prompt;
17
18 if (system_prompts.some(x => x.name === power_user.instruct.preset)) {
19 power_user.sysprompt.name = power_user.instruct.preset;
20 }
21
22 saveSettingsDebounced();
23 toastr.info('System prompt settings have been moved from the Instruct Mode.', 'Migration notice', { timeOut: 5000 });
24 }
25}
26
12/**27/**
13 * Loads sysprompt settings from the given data object.28 * Loads sysprompt settings from the given data object.
14 * @param {object} data Settings data object.29 * @param {object} data Settings data object.
@@ -18,6 +33,7 @@ export async function loadSystemPrompts(data) {
18 system_prompts = data.sysprompt;33 system_prompts = data.sysprompt;
19 }34 }
2035
36 migrateSystemPromptFromInstructMode();
21 toggleSyspromptDisabledControls();37 toggleSyspromptDisabledControls();
2238
23 for (const prompt of system_prompts) {39 for (const prompt of system_prompts) {
server.js+1 -0
@@ -932,6 +932,7 @@ async function verifySecuritySettings() {
932userModule.initUserStorage(dataRoot)932userModule.initUserStorage(dataRoot)
933 .then(userModule.ensurePublicDirectoriesExist)933 .then(userModule.ensurePublicDirectoriesExist)
934 .then(userModule.migrateUserData)934 .then(userModule.migrateUserData)
935 .then(userModule.migrateSystemPrompts)
935 .then(verifySecuritySettings)936 .then(verifySecuritySettings)
936 .then(preSetupTasks)937 .then(preSetupTasks)
937 .finally(startServer);938 .finally(startServer);
src/users.js+32 -0
@@ -9,6 +9,7 @@ const storage = require('node-persist');
9const express = require('express');9const express = require('express');
10const mime = require('mime-types');10const mime = require('mime-types');
11const archiver = require('archiver');11const archiver = require('archiver');
12const writeFileAtomicSync = require('write-file-atomic').sync;
1213
13const { USER_DIRECTORY_TEMPLATE, DEFAULT_USER, PUBLIC_DIRECTORIES, DEFAULT_AVATAR, SETTINGS_FILE } = require('./constants');14const { USER_DIRECTORY_TEMPLATE, DEFAULT_USER, PUBLIC_DIRECTORIES, DEFAULT_AVATAR, SETTINGS_FILE } = require('./constants');
14const { getConfigValue, color, delay, setConfigValue, generateTimestamp } = require('./util');15const { getConfigValue, color, delay, setConfigValue, generateTimestamp } = require('./util');
@@ -323,6 +324,36 @@ async function migrateUserData() {
323 console.log(color.green('Migration completed!'));324 console.log(color.green('Migration completed!'));
324}325}
325326
327async function migrateSystemPrompts() {
328 const directories = await getUserDirectoriesList();
329 for (const directory of directories) {
330 try {
331 const migrateMarker = path.join(directory.sysprompt, '.migrated');
332 if (fs.existsSync(migrateMarker)) {
333 continue;
334 }
335 const instucts = fs.readdirSync(directory.instruct);
336 for (const instruct of instucts) {
337 const instructPath = path.join(directory.instruct, instruct);
338 const syspromptPath = path.join(directory.sysprompt, instruct);
339 if (path.extname(instruct) === '.json' && !fs.existsSync(syspromptPath)) {
340 const instructData = JSON.parse(fs.readFileSync(instructPath, 'utf8'));
341 if ('system_prompt' in instructData && 'name' in instructData) {
342 const syspromptData = { name: instructData.name, content: instructData.system_prompt };
343 writeFileAtomicSync(syspromptPath, JSON.stringify(syspromptData, null, 4));
344 delete instructData.system_prompt;
345 writeFileAtomicSync(instructPath, JSON.stringify(instructData, null, 4));
346 console.log(`Migrated system prompt ${instruct} for ${directory.root.split(path.sep).pop()}`);
347 }
348 }
349 }
350 writeFileAtomicSync(migrateMarker, '');
351 } catch {
352 // Ignore errors
353 }
354 }
355}
356
326/**357/**
327 * Converts a user handle to a storage key.358 * Converts a user handle to a storage key.
328 * @param {string} handle User handle359 * @param {string} handle User handle
@@ -724,6 +755,7 @@ module.exports = {
724 requireLoginMiddleware,755 requireLoginMiddleware,
725 requireAdminMiddleware,756 requireAdminMiddleware,
726 migrateUserData,757 migrateUserData,
758 migrateSystemPrompts,
727 getPasswordSalt,759 getPasswordSalt,
728 getPasswordHash,760 getPasswordHash,
729 getCsrfSecret,761 getCsrfSecret,