Migrate sysprompts from instruct
| @@ -9,6 +9,21 @@ const $select = $('#sysprompt_select'); | ||
| 9 | 9 | const $content = $('#sysprompt_content'); |
| 10 | 10 | const $contentBlock = $('#SystemPromptBlock'); |
| 11 | 11 | |
| 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 | + | |
| 12 | 27 | /** |
| 13 | 28 | * Loads sysprompt settings from the given data object. |
| 14 | 29 | * @param {object} data Settings data object. |
| @@ -18,6 +33,7 @@ export async function loadSystemPrompts(data) { | ||
| 18 | 33 | system_prompts = data.sysprompt; |
| 19 | 34 | } |
| 20 | 35 | |
| 36 | + migrateSystemPromptFromInstructMode(); | |
| 21 | 37 | toggleSyspromptDisabledControls(); |
| 22 | 38 | |
| 23 | 39 | for (const prompt of system_prompts) { |
| @@ -932,6 +932,7 @@ async function verifySecuritySettings() { | ||
| 932 | 932 | userModule.initUserStorage(dataRoot) |
| 933 | 933 | .then(userModule.ensurePublicDirectoriesExist) |
| 934 | 934 | .then(userModule.migrateUserData) |
| 935 | + .then(userModule.migrateSystemPrompts) | |
| 935 | 936 | .then(verifySecuritySettings) |
| 936 | 937 | .then(preSetupTasks) |
| 937 | 938 | .finally(startServer); |
| @@ -9,6 +9,7 @@ const storage = require('node-persist'); | ||
| 9 | 9 | const express = require('express'); |
| 10 | 10 | const mime = require('mime-types'); |
| 11 | 11 | const archiver = require('archiver'); |
| 12 | +const writeFileAtomicSync = require('write-file-atomic').sync; | |
| 12 | 13 | |
| 13 | 14 | const { USER_DIRECTORY_TEMPLATE, DEFAULT_USER, PUBLIC_DIRECTORIES, DEFAULT_AVATAR, SETTINGS_FILE } = require('./constants'); |
| 14 | 15 | const { getConfigValue, color, delay, setConfigValue, generateTimestamp } = require('./util'); |
| @@ -323,6 +324,36 @@ async function migrateUserData() { | ||
| 323 | 324 | console.log(color.green('Migration completed!')); |
| 324 | 325 | } |
| 325 | 326 | |
| 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 | + | |
| 326 | 357 | /** |
| 327 | 358 | * Converts a user handle to a storage key. |
| 328 | 359 | * @param {string} handle User handle |
| @@ -724,6 +755,7 @@ module.exports = { | ||
| 724 | 755 | requireLoginMiddleware, |
| 725 | 756 | requireAdminMiddleware, |
| 726 | 757 | migrateUserData, |
| 758 | + migrateSystemPrompts, | |
| 727 | 759 | getPasswordSalt, |
| 728 | 760 | getPasswordHash, |
| 729 | 761 | getCsrfSecret, |