@@ -5512,6 +5512,20 @@ Current version indicated by LITEVER below.
55125512 })
55135513 }
55145514
5515+ function saveKLiteSaveToIndexDB(filename, data)
5516+ {
5517+ let charName = filename.replaceAll(/[^\w()_\-'",!\[\].]/g, " ").replaceAll(/\s+/g, " ").trim();
5518+ let dataToSave = {
5519+ name: charName,
5520+ data: data
5521+ }
5522+ indexeddb_save(`character_${charName}`, JSON.stringify(dataToSave)).then(() => {
5523+ allCharacterNames.push({name: charName, type: "Save"})
5524+ updateCharacterListFromAllDe();
5525+ })
5526+ }
5527+
5528+
55155529 function getTypeFromAllCharacterData(data)
55165530 {
55175531 return !!data?.type ? data.type : "Character"
@@ -38150,6 +38164,45 @@ flowchart TD\n${treeToViewOutput.outputText.trim()}`
3815038164 updateCharacterListFromAll()
3815138165 }).button("Close", () => popupUtils.reset()).show();
3815238166 }
38167+ else if (type === "Save")
38168+ {
38169+ let contents = document.createElement("span");
38170+ try
38171+ {
38172+ let data = await getCharacterData(name), {AI_portrait} = data;
38173+ let {memory, prompt, tempmemory, worldinfo} = (data)?.data;
38174+ contents = createDetailsContent(name);
38175+ if (!!AI_portrait)
38176+ {
38177+ let imageContainer = document.createElement("span"), imageElem = document.createElement("img");
38178+ imageElem.src = AI_portrait;
38179+ imageElem.style = "height: 30%; width: 30%; border-radius: 10px;"
38180+ imageContainer.style = "width: 100%; display: flex; justify-content: space-around; padding: 10px;";
38181+ imageContainer.appendChild(imageElem);
38182+ contents.appendChild(imageContainer);
38183+ }
38184+ createSection(contents, "Memory", memory);
38185+ createSection(contents, "Temporary memory", tempmemory);
38186+ createSection(contents, "First message", prompt);
38187+ createSection(contents, "World info", worldinfo?.map(entry => {
38188+ return wiEntryToString(entry);
38189+ }));
38190+ }
38191+ catch (e)
38192+ {
38193+ console.error(e)
38194+ }
38195+
38196+ popupUtils.reset().title("Save Options").content(contents).button("Back", showCharacterList).button("Load save", async () => {
38197+ popupUtils.reset()
38198+ let charData = await getCharacterData(name)
38199+ kai_json_load(charData.data, false);
38200+ }).button("Delete save", async () => {
38201+ popupUtils.reset()
38202+ allCharacterNames = allCharacterNames.filter(c => c.name !== name)
38203+ updateCharacterListFromAll()
38204+ }).button("Close", () => popupUtils.reset()).show();
38205+ }
3815338206 else
3815438207 {
3815538208 popupUtils.reset()
@@ -38162,12 +38215,12 @@ flowchart TD\n${treeToViewOutput.outputText.trim()}`
3816238215 containers.forEach(container => popupUtils.content(container))
3816338216 popupUtils
3816438217 .button("New character", () => { try { showCharacterCreator(); } catch(e){ console.error(e); } })
38165- .button("Upload characters, lorebooks and world info", () => {
38218+ .button("Upload characters, saves, lorebooks and world info", () => {
3816638219 popupUtils.reset()
3816738220 promptUserForLocalFile(async (result) => {
3816838221 let { file, fileName, ext, content, plaintext, dataArr } = result;
3816938222 waitingToast.show()
38170- waitingToast.setText(`Loading character ${fileName}`)
38223+ waitingToast.setText(`Loading data ${fileName}`)
3817138224 if (ext === ".png")
3817238225 {
3817338226 let arr = new Uint8Array(dataArr)
@@ -38180,45 +38233,52 @@ flowchart TD\n${treeToViewOutput.outputText.trim()}`
3818038233 }
3818138234 else
3818238235 {
38183- let wiToAdd = JSON.parse(plaintext);
38184- let has_tav_wi_check = has_tavern_wi_check(wiToAdd), wiName = fileName;
38185- if(has_tav_wi_check)
38186- {
38187- if (wiToAdd?.name !== undefined && wiToAdd.name.trim().length > 0)
38188- {
38189- wiName = wiToAdd.name
38190- }
38191- wiToAdd = load_tavern_wi(wiToAdd);
38192- if(wiToAdd && wiToAdd.length > 0)
38193- {
38194- wiToAdd.forEach(wi => wi.wigroup = fileName.replace("'", ""))
38195- }
38236+ let data = JSON.parse(plaintext);
38237+ if (is_kai_json(data) && !data?.scenarioVersion) {
38238+ // Handle as a regular KLite save
38239+ saveKLiteSaveToIndexDB(fileName, data)
3819638240 }
3819738241 else
3819838242 {
38199- try
38243+ let wiToAdd = data, has_tav_wi_check = has_tavern_wi_check(wiToAdd), wiName = fileName;
38244+ if(has_tav_wi_check)
3820038245 {
38201- let hasNoGeneralWI = wiToAdd.length === 0 || wiToAdd.find(wi => wi?.wigroup === undefined || wi.wigroup.trim() === null || wi.wigroup === "" || wi.wigroup === "General") === undefined;
38202- if (hasNoGeneralWI)
38246+ if (wiToAdd?.name !== undefined && wiToAdd.name.trim().length > 0)
3820338247 {
38204- let wiAllHaveSameGroup = wiToAdd.find((e, p, a) => a.find(c => c?.wigroup !== e.wigroup)) === undefined
38205- if (wiAllHaveSameGroup)
38206- {
38207- wiName = wiToAdd[0].wigroup
38208- }
38248+ wiName = wiToAdd.name
38249+ }
38250+ wiToAdd = load_tavern_wi(wiToAdd);
38251+ if(wiToAdd && wiToAdd.length > 0)
38252+ {
38253+ wiToAdd.forEach(wi => wi.wigroup = fileName.replace("'", ""))
3820938254 }
3821038255 }
38211- catch (e)
38256+ else
3821238257 {
38213- console.error(e)
38258+ try
38259+ {
38260+ let hasNoGeneralWI = wiToAdd.length === 0 || wiToAdd.find(wi => wi?.wigroup === undefined || wi.wigroup.trim() === null || wi.wigroup === "" || wi.wigroup === "General") === undefined;
38261+ if (hasNoGeneralWI)
38262+ {
38263+ let wiAllHaveSameGroup = wiToAdd.find((e, p, a) => a.find(c => c?.wigroup !== e.wigroup)) === undefined
38264+ if (wiAllHaveSameGroup)
38265+ {
38266+ wiName = wiToAdd[0].wigroup
38267+ }
38268+ }
38269+ }
38270+ catch (e)
38271+ {
38272+ console.error(e)
38273+ }
3821438274 }
38215- }
38216- if (Array.isArray(wiToAdd))
38217- {
38218- wiToAdd = wiToAdd.filter(wi => wi?.key !== undefined)
38219- if (wiToAdd.length > 0)
38275+ if (Array.isArray(wiToAdd))
3822038276 {
38221- saveLorebookToIndexDB(wiName, wiToAdd, JSON.parse(plaintext))
38277+ wiToAdd = wiToAdd.filter(wi => wi?.key !== undefined)
38278+ if (wiToAdd.length > 0)
38279+ {
38280+ saveLorebookToIndexDB(wiName, wiToAdd, JSON.parse(plaintext))
38281+ }
3822238282 }
3822338283 }
3822438284 }
0 commit comments