mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-01-07 07:06:05 +01:00
Add confirmation step and allow character prompt order import
This commit is contained in:
parent
bfb4e5026d
commit
6ec249bb30
@ -1,5 +1,5 @@
|
|||||||
import {DraggablePromptListModule as DraggableList} from "./DraggableList.js";
|
import {DraggablePromptListModule as DraggableList} from "./DraggableList.js";
|
||||||
import {event_types, eventSource, substituteParams} from "../script.js";
|
import {callPopup, event_types, eventSource, substituteParams} from "../script.js";
|
||||||
import {TokenHandler} from "./openai.js";
|
import {TokenHandler} from "./openai.js";
|
||||||
import {power_user} from "./power-user.js";
|
import {power_user} from "./power-user.js";
|
||||||
|
|
||||||
@ -316,32 +316,38 @@ PromptManagerModule.prototype.init = function (moduleConfiguration, serviceSetti
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.handleImport = () => {
|
this.handleImport = () => {
|
||||||
const fileOpener = document.createElement('input');
|
|
||||||
fileOpener.type = 'file';
|
|
||||||
fileOpener.accept = '.json';
|
|
||||||
|
|
||||||
fileOpener.addEventListener('change', (event) => {
|
callPopup('Your prompts will be merged with the import. Imported prompts with the same ID will override existing prompts.', 'confirm',)
|
||||||
const file = event.target.files[0];
|
.then(userChoice => {
|
||||||
if (!file) {
|
if (false === userChoice) return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const reader = new FileReader();
|
const fileOpener = document.createElement('input');
|
||||||
|
fileOpener.type = 'file';
|
||||||
|
fileOpener.accept = '.json';
|
||||||
|
|
||||||
reader.onload = (event) => {
|
fileOpener.addEventListener('change', (event) => {
|
||||||
const fileContent = event.target.result;
|
const file = event.target.files[0];
|
||||||
try {
|
if (!file) return;
|
||||||
const data = JSON.parse(fileContent);
|
|
||||||
this.import(data);
|
|
||||||
} catch (err) {
|
|
||||||
console.error('An error occurred while parsing the file content: ', err);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
reader.readAsText(file);
|
const reader = new FileReader();
|
||||||
|
|
||||||
|
reader.onload = (event) => {
|
||||||
|
const fileContent = event.target.result;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const data = JSON.parse(fileContent);
|
||||||
|
this.import(data);
|
||||||
|
} catch (err) {
|
||||||
|
this.log('An error occurred while importing prompts');
|
||||||
|
this.log(err.toString());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
reader.readAsText(file);
|
||||||
|
});
|
||||||
|
|
||||||
|
fileOpener.click();
|
||||||
});
|
});
|
||||||
|
|
||||||
fileOpener.click();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Re-render when the character changes.
|
// Re-render when the character changes.
|
||||||
@ -1138,9 +1144,8 @@ PromptManagerModule.prototype.export = function (data, type, name = 'export') {
|
|||||||
};
|
};
|
||||||
|
|
||||||
PromptManagerModule.prototype.import = function (importData) {
|
PromptManagerModule.prototype.import = function (importData) {
|
||||||
console.log(importData)
|
|
||||||
const mergeKeepNewer = (prompts, newPrompts) => {
|
const mergeKeepNewer = (prompts, newPrompts) => {
|
||||||
let merged = [prompts, newPrompts];
|
let merged = [...prompts, ...newPrompts];
|
||||||
|
|
||||||
let map = new Map();
|
let map = new Map();
|
||||||
for (let obj of merged) {
|
for (let obj of merged) {
|
||||||
@ -1153,12 +1158,14 @@ PromptManagerModule.prototype.import = function (importData) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const prompts = mergeKeepNewer(this.serviceSettings.prompts, importData.data.prompts);
|
const prompts = mergeKeepNewer(this.serviceSettings.prompts, importData.data.prompts);
|
||||||
console.log(prompts)
|
|
||||||
this.setPrompts(prompts);
|
this.setPrompts(prompts);
|
||||||
|
this.log('Prompt import succeeded');
|
||||||
|
|
||||||
if ('character' === importData.type) {
|
if ('character' === importData.type) {
|
||||||
const promptList = this.getPromptListForCharacter(this.activeCharacter);
|
const promptList = this.getPromptListForCharacter(this.activeCharacter);
|
||||||
promptList.list = importData.data.promptList;
|
Object.assign(promptList, importData.data.promptList);
|
||||||
|
this.log(`Prompt order import for character ${this.activeCharacter.name} completed`);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.saveServiceSettings().then(() => this.render());
|
this.saveServiceSettings().then(() => this.render());
|
||||||
|
Loading…
Reference in New Issue
Block a user