Skip to content

Commit 9a047a5

Browse files
author
prima
committed
feat: Addition of scenarios / legacy server saves as read only section in manager. Fixed minimum width issue for details pop up.
1 parent 7995fd3 commit 9a047a5

File tree

2 files changed

+177
-64
lines changed

2 files changed

+177
-64
lines changed

embd_res/js/characterManager.js

Lines changed: 174 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,163 @@ let migrateOldData = async () => {
224224
}
225225
}
226226

227+
let createSection = (containerElem, section, text) => {
228+
if (!!text && text.length > 0) {
229+
let sectionContainer = document.createElement("span");
230+
sectionContainer.style = "width: 100%; display: flex; padding: 10px;";
231+
let sectionTitle = document.createElement("span"), sectionElem;
232+
sectionTitle.innerText = section;
233+
sectionTitle.style = "padding-right: 5px; font-weight: bold;"
234+
sectionContainer.appendChild(sectionTitle);
235+
if (Array.isArray(text)) {
236+
sectionElem = document.createElement("ul");
237+
text.forEach(listContent => {
238+
let listElem = document.createElement("li");
239+
let summaryOfKey = listContent.length > maxLengthForSection ? `${listContent.substr(0, halfMaxLengthForSection).trim()}...${listContent.substr(-halfMaxLengthForSection).trim()}` : listContent.trim();
240+
listElem.innerText = summaryOfKey
241+
if (listContent.length !== summaryOfKey.length) {
242+
let isShown = false
243+
listElem.onclick = () => {
244+
isShown = !isShown
245+
listElem.innerText = isShown ? listContent : summaryOfKey
246+
}
247+
listElem.title = "Click to show / hide full content"
248+
}
249+
sectionElem.appendChild(listElem)
250+
})
251+
}
252+
else {
253+
sectionElem = document.createElement("span");
254+
let summaryOfKey = text.length > maxLengthForSection ? `${text.substr(0, halfMaxLengthForSection).trim()}...${text.substr(-halfMaxLengthForSection).trim()}` : text.trim();
255+
sectionElem.innerText = summaryOfKey
256+
if (text.length !== summaryOfKey.length) {
257+
let isShown = false
258+
sectionElem.onclick = () => {
259+
isShown = !isShown
260+
sectionElem.innerText = isShown ? text : summaryOfKey
261+
}
262+
sectionElem.title = "Click to show / hide full content"
263+
}
264+
}
265+
sectionContainer.appendChild(sectionElem);
266+
containerElem.appendChild(sectionContainer);
267+
}
268+
}
269+
270+
let createDetailsContent = (name) => {
271+
let contents = document.createElement("div");
272+
contents.style.color = "white";
273+
contents.style.padding = "5px";
274+
let titleElem = document.createElement("span");
275+
titleElem.classList.add("popuptitletext");
276+
titleElem.innerText = name;
277+
titleElem.style.borderBottom = "solid";
278+
contents.appendChild(titleElem);
279+
return contents;
280+
}
281+
282+
let getScenariosAndLegacyServerSaves = async () => {
283+
preview_temp_scenario = () => {
284+
285+
popupUtils.reset()
286+
try {
287+
let { memory, prompt, tempmemory, worldinfo, chatname, chatopponent, AI_portrait } = temp_scenario;
288+
contents = createDetailsContent(temp_scenario.title);
289+
let image = AI_portrait || temp_scenario?.image
290+
if (!!image) {
291+
let imageContainer = document.createElement("span"), imageElem = document.createElement("img");
292+
imageElem.src = image;
293+
imageElem.style = "height: 30%; width: 30%; border-radius: 10px;"
294+
imageContainer.style = "width: 100%; display: flex; justify-content: space-around; padding: 10px;";
295+
imageContainer.appendChild(imageElem);
296+
contents.appendChild(imageContainer);
297+
}
298+
if (!!chatname) {
299+
createSection(contents, "User", chatname);
300+
}
301+
if (!!chatopponent) {
302+
createSection(contents, "Characters", chatopponent.split("||$||"));
303+
}
304+
createSection(contents, "Characters", memory);
305+
createSection(contents, "Memory", memory);
306+
createSection(contents, "Temporary memory", tempmemory);
307+
createSection(contents, "First message", prompt);
308+
createSection(contents, "World info", worldinfo?.map(entry => {
309+
return wiEntryToString(entry);
310+
}));
311+
312+
popupUtils.reset().title("Scenario Options").content(contents).css("min-height", "50%").css("min-width", "50%").button("Back", showCharacterList).button("Load scenario", async () => {
313+
popupUtils.reset()
314+
confirm_scenario_verify(() => {
315+
hide_popups();
316+
complete_load_scenario();
317+
temp_scenario = null;
318+
})
319+
}).show()
320+
}
321+
catch (e) {
322+
handleError(e)
323+
}
324+
}
325+
326+
if (is_using_kcpp_with_server_saving())
327+
{
328+
// Clean up scenario DB and the scenario dropdown options
329+
scenario_db = scenario_db.filter(scenario => !(scenario?.serverSave === true))
330+
Array.from(scenarioDropdown.children).filter(elem => !/^\d+$/.test(elem.value)).forEach(elem => elem.remove())
331+
serverSideTypes = []
332+
333+
await new Promise((resolve) => promptForAdminPassword(resolve));
334+
335+
let saves = await getServerSaves()
336+
for (save in saves) {
337+
if (!!saves[save].name && saves[save]?.typeName !== "Manager") {
338+
let name = saves[save].name, isPublic = saves[save].isPublic, isEncrypted = saves[save].isEncrypted
339+
let displayText = `${name} ${!!isPublic ? "(Public)" : "(Private)"} ${!!isEncrypted ? "🔒" : ""}`
340+
let typeName = saves[save].typeName
341+
let scenario = {
342+
title: displayText,
343+
desc: displayText,
344+
serverSave: true,
345+
serverSaveData: saves[save],
346+
serverSaveTypeName: typeName
347+
}
348+
349+
if (!!saves[save]?.previewContent) {
350+
scenario.image = saves[save]?.previewContent,
351+
scenario.image_aspect = 1
352+
}
353+
scenario_db.push(scenario)
354+
}
355+
}
356+
}
357+
358+
let scenarios = [];
359+
for (let i = 0; i < scenario_sources.length; ++i) {
360+
scenarios.push({
361+
name: scenario_sources[i].name,
362+
handler: () => {
363+
popupUtils.reset()
364+
import_scenario(scenario_sources[i])
365+
}
366+
})
367+
}
368+
369+
for (let i = 0; i < scenario_db.length; ++i) {
370+
let curr = scenario_db[i];
371+
scenarios.push({
372+
name: curr.title,
373+
handler: async () => {
374+
temp_scenario = curr
375+
preview_temp_scenario()
376+
},
377+
thumbnail: curr?.image
378+
})
379+
}
380+
381+
return scenarios
382+
}
383+
227384
let maxLengthForSection = 500, halfMaxLengthForSection = Math.floor(maxLengthForSection / 2);
228385
let showCharacterList = async () => {
229386
let containers = []
@@ -333,6 +490,7 @@ let showCharacterList = async () => {
333490
}
334491
}
335492

493+
let scenarios = await getScenariosAndLegacyServerSaves()
336494

337495
let dragIcon = createIcon("Click or drag characters, saves, lorebooks, world info or PDFs here to add")
338496
dragIcon.classList.add("searchExclude")
@@ -437,59 +595,6 @@ let showCharacterList = async () => {
437595
getContainerForType("Data").appendChild(charIcon);
438596
}
439597
else {
440-
let createDetailsContent = (name) => {
441-
let contents = document.createElement("div");
442-
contents.style.color = "white";
443-
contents.style.padding = "5px";
444-
let titleElem = document.createElement("span");
445-
titleElem.classList.add("popuptitletext");
446-
titleElem.innerText = name;
447-
titleElem.style.borderBottom = "solid";
448-
contents.appendChild(titleElem);
449-
return contents;
450-
}
451-
let createSection = (containerElem, section, text) => {
452-
if (!!text && text.length > 0) {
453-
let sectionContainer = document.createElement("span");
454-
sectionContainer.style = "width: 100%; display: flex; padding: 10px;";
455-
let sectionTitle = document.createElement("span"), sectionElem;
456-
sectionTitle.innerText = section;
457-
sectionTitle.style = "padding-right: 5px; font-weight: bold;"
458-
sectionContainer.appendChild(sectionTitle);
459-
if (Array.isArray(text)) {
460-
sectionElem = document.createElement("ul");
461-
text.forEach(listContent => {
462-
let listElem = document.createElement("li");
463-
let summaryOfKey = listContent.length > maxLengthForSection ? `${listContent.substr(0, halfMaxLengthForSection).trim()}...${listContent.substr(-halfMaxLengthForSection).trim()}` : listContent.trim();
464-
listElem.innerText = summaryOfKey
465-
if (listContent.length !== summaryOfKey.length) {
466-
let isShown = false
467-
listElem.onclick = () => {
468-
isShown = !isShown
469-
listElem.innerText = isShown ? listContent : summaryOfKey
470-
}
471-
listElem.title = "Click to show / hide full content"
472-
}
473-
sectionElem.appendChild(listElem)
474-
})
475-
}
476-
else {
477-
sectionElem = document.createElement("span");
478-
let summaryOfKey = text.length > maxLengthForSection ? `${text.substr(0, halfMaxLengthForSection).trim()}...${text.substr(-halfMaxLengthForSection).trim()}` : text.trim();
479-
sectionElem.innerText = summaryOfKey
480-
if (text.length !== summaryOfKey.length) {
481-
let isShown = false
482-
sectionElem.onclick = () => {
483-
isShown = !isShown
484-
sectionElem.innerText = isShown ? text : summaryOfKey
485-
}
486-
sectionElem.title = "Click to show / hide full content"
487-
}
488-
}
489-
sectionContainer.appendChild(sectionElem);
490-
containerElem.appendChild(sectionContainer);
491-
}
492-
}
493598
let lorebookEntryToString = (entry) => {
494599
return `Primary: ${[...entry?.keys].join(", ")}\nSecondary: ${[...entry?.secondary_keys].join(",")}`;
495600
}
@@ -537,7 +642,7 @@ let showCharacterList = async () => {
537642
console.error(e)
538643
}
539644

540-
popupUtils.reset().title("Character Options").content(contents).button("Back", showCharacterList).button("Load character", async () => {
645+
popupUtils.reset().title("Character Options").content(contents).css("min-height", "50%").css("min-width", "50%").button("Back", showCharacterList).button("Load character", async () => {
541646
popupUtils.reset()
542647
let charData = await getCharacterData(name)
543648
load_tavern_obj(charData.data);
@@ -577,7 +682,7 @@ let showCharacterList = async () => {
577682
console.error(e)
578683
}
579684

580-
popupUtils.reset().title("World Info Options").content(contents).button("Back", showCharacterList).button("Add to WI", async () => {
685+
popupUtils.reset().title("World Info Options").content(contents).css("min-height", "50%").css("min-width", "50%").button("Back", showCharacterList).button("Add to WI", async () => {
581686
popupUtils.reset()
582687
let charData = await getCharacterData(name)
583688
let wiToAdd = charData.data;
@@ -646,7 +751,7 @@ let showCharacterList = async () => {
646751
console.error(e)
647752
}
648753

649-
popupUtils.reset().title("Save Options").content(contents).button("Back", showCharacterList).button("Load save", async () => {
754+
popupUtils.reset().title("Save Options").content(contents).css("min-height", "50%").css("min-width", "50%").button("Back", showCharacterList).button("Load save", async () => {
650755
popupUtils.reset()
651756
let charData = await getCharacterData(name)
652757
kai_json_load(charData.data, false);
@@ -689,7 +794,7 @@ let showCharacterList = async () => {
689794
let charData = await getCharacterData(name), { extractedText } = charData;
690795
createSection(contents, "Has text been extracted?", !!extractedText ? "True" : "False");
691796

692-
popupUtils.reset().title("Document Options").content(contents).button("Back", showCharacterList).button("Add to TextDB", async () => {
797+
popupUtils.reset().title("Document Options").content(contents).css("min-height", "50%").css("min-width", "50%").button("Back", showCharacterList).button("Add to TextDB", async () => {
693798
popupUtils.reset()
694799
waitingToast.setText(`Extracting text to add to TextDB`)
695800
waitingToast.show()
@@ -746,6 +851,18 @@ let showCharacterList = async () => {
746851
getContainerForType(type).appendChild(charIcon)
747852
}
748853
}
854+
855+
// Add icons for scenarios and legacy server data
856+
for (scenario of scenarios) {
857+
let image = undefined
858+
if (scenario?.thumbnail !== undefined) {
859+
image = `url('${scenario.thumbnail}')`
860+
}
861+
let icon = createIcon(scenario.name, image)
862+
icon.addEventListener("click", scenario.handler)
863+
getContainerForType("Scenarios").appendChild(icon);
864+
}
865+
749866
popupUtils.reset().title(`Data List (${allCharacterNames.length})`).css("height", "80%").css("width", "80%").setMobileMenu(true)
750867
containers.forEach(container => popupUtils.content(container))
751868

@@ -798,10 +915,6 @@ let showCharacterList = async () => {
798915
popupUtils.buttonGroup("Esobold Server")
799916
.button("Overwrite", () => putAllCharacterManagerData())
800917
.button("Load", () => loadAllCharacterManagerData())
801-
.button("Legacy", () => {
802-
popupUtils.reset()
803-
showServerSavesPopup()
804-
})
805918
}
806919
popupUtils.resetButtonGroup().button("Close", () => popupUtils.reset()).show();
807920
}

embd_res/klite.embd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10016,7 +10016,7 @@ Current version indicated by LITEVER below.
1001610016
}
1001710017
}
1001810018
}
10019-
function confirm_scenario_verify()
10019+
function confirm_scenario_verify(confirmCallback = confirm_scenario)
1002010020
{
1002110021
if(temp_scenario.show_warning==true && localsettings.passed_ai_warning==false && passed_ai_warning_local==false)
1002210022
{
@@ -10031,7 +10031,7 @@ Current version indicated by LITEVER below.
1003110031
let userinput = getInputBoxValue().toLowerCase().trim();
1003210032
if(userinput.includes("understand"))
1003310033
{
10034-
confirm_scenario();
10034+
confirmCallback();
1003510035
localsettings.passed_ai_warning = true; //remember flag for session
1003610036
passed_ai_warning_local = true;
1003710037
}
@@ -10042,7 +10042,7 @@ Current version indicated by LITEVER below.
1004210042
localsettings.passed_ai_warning = true; //remember flag for session
1004310043
passed_ai_warning_local = true;
1004410044
}
10045-
confirm_scenario();
10045+
confirmCallback();
1004610046
}
1004710047
}
1004810048
function confirm_scenario()

0 commit comments

Comments
 (0)