Migrate sysprompts from instruct

2f7d694f5429fd95e342bd0825e96eee1cc66170

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

3 files changed, +49 -0Ignore whitespace
public/scripts/sysprompt.js+16 -0
@@ -9,6 +9,21 @@ const $select = $('#sysprompt_select');
99const $content = $('#sysprompt_content');
1010const $contentBlock = $('#SystemPromptBlock');
1111
12+function 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+
1227/**
1328 * Loads sysprompt settings from the given data object.
1429 * @param {object} data Settings data object.
@@ -18,6 +33,7 @@ export async function loadSystemPrompts(data) {
1833 system_prompts = data.sysprompt;
1934 }
2035
36+ migrateSystemPromptFromInstructMode();
2137 toggleSyspromptDisabledControls();
2238
2339 for (const prompt of system_prompts) {
server.js+1 -0
@@ -932,6 +932,7 @@ async function verifySecuritySettings() {
932932userModule.initUserStorage(dataRoot)
933933 .then(userModule.ensurePublicDirectoriesExist)
934934 .then(userModule.migrateUserData)
935+ .then(userModule.migrateSystemPrompts)
935936 .then(verifySecuritySettings)
936937 .then(preSetupTasks)
937938 .finally(startServer);
src/users.js+32 -0
@@ -9,6 +9,7 @@ const storage = require('node-persist');
99const express = require('express');
1010const mime = require('mime-types');
1111const archiver = require('archiver');
12+const writeFileAtomicSync = require('write-file-atomic').sync;
1213
1314const { USER_DIRECTORY_TEMPLATE, DEFAULT_USER, PUBLIC_DIRECTORIES, DEFAULT_AVATAR, SETTINGS_FILE } = require('./constants');
1415const { getConfigValue, color, delay, setConfigValue, generateTimestamp } = require('./util');
@@ -323,6 +324,36 @@ async function migrateUserData() {
323324 console.log(color.green('Migration completed!'));
324325}
325326
327+async 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+
326357/**
327358 * Converts a user handle to a storage key.
328359 * @param {string} handle User handle
@@ -724,6 +755,7 @@ module.exports = {
724755 requireLoginMiddleware,
725756 requireAdminMiddleware,
726757 migrateUserData,
758+ migrateSystemPrompts,
727759 getPasswordSalt,
728760 getPasswordHash,
729761 getCsrfSecret,