Add lorebook import for modules

This commit is contained in:
kwaroran
2024-02-07 06:20:20 +09:00
parent 7094673a6c
commit 0252961fbd
2 changed files with 80 additions and 46 deletions

View File

@@ -320,7 +320,22 @@ export async function importLoreBook(mode:'global'|'local'|'sglobal'){
} }
} }
else if(importedlore.entries){ else if(importedlore.entries){
const entries:{[key:string]:{ const entries:{[key:string]:CCLorebook} = importedlore.entries
lore.push(...convertExternalLorebook(entries))
}
if(mode === 'global'){
db.characters[selectedID].globalLore = lore
}
else{
db.characters[selectedID].chats[page].localLore = lore
}
setDatabase(db)
} catch (error) {
alertError(`${error}`)
}
}
interface CCLorebook{
key:string[] key:string[]
comment:string comment:string
content:string content:string
@@ -341,7 +356,10 @@ export async function importLoreBook(mode:'global'|'local'|'sglobal'){
prefix:string prefix:string
suffix:string suffix:string
} }
}} = importedlore.entries }
export function convertExternalLorebook(entries:{[key:string]:CCLorebook}){
let lore:loreBook[] = []
for(const key in entries){ for(const key in entries){
const currentLore = entries[key] const currentLore = entries[key]
lore.push({ lore.push({
@@ -357,17 +375,7 @@ export async function importLoreBook(mode:'global'|'local'|'sglobal'){
selective: currentLore.selective ?? false selective: currentLore.selective ?? false
}) })
} }
} return lore
if(mode === 'global'){
db.characters[selectedID].globalLore = lore
}
else{
db.characters[selectedID].chats[page].localLore = lore
}
setDatabase(db)
} catch (error) {
alertError(`${error}`)
}
} }
export async function exportLoreBook(mode:'global'|'local'|'sglobal'){ export async function exportLoreBook(mode:'global'|'local'|'sglobal'){

View File

@@ -6,6 +6,7 @@ import { get } from "svelte/store"
import { CurrentChat } from "../stores" import { CurrentChat } from "../stores"
import { selectSingleFile } from "../util" import { selectSingleFile } from "../util"
import { v4 } from "uuid" import { v4 } from "uuid"
import { convertExternalLorebook } from "./lorebook"
export interface RisuModule{ export interface RisuModule{
name: string name: string
@@ -26,25 +27,50 @@ export async function exportModule(module:RisuModule){
} }
export async function importModule(){ export async function importModule(){
const f = await selectSingleFile(['json']) const f = await selectSingleFile(['json', 'lorebook'])
if(!f){ if(!f){
return return
} }
const file = f.data const file = f.data
try {
const importedModule = JSON.parse(Buffer.from(file).toString())
if(importedModule.type === 'risuModule'){
const db = get(DataBase) const db = get(DataBase)
try {
const importData = JSON.parse(Buffer.from(file).toString())
if(importData.type === 'risuModule'){
if( if(
(!importedModule.name) (!importData.name)
|| (!importedModule.description) || (!importData.description)
|| (!importedModule.id) || (!importData.id)
){ ){
alertError(language.errors.noData) alertError(language.errors.noData)
} }
importedModule.id = v4() importData.id = v4()
db.modules.push(importedModule) db.modules.push(importData)
setDatabase(db) setDatabase(db)
return
}
if(importData.type === 'risu' && importData.data){
const lores:loreBook[] = importData.data
const importModule = {
name: importData.name,
description: importData.description,
lorebook: lores,
id: v4()
}
db.modules.push(importModule)
setDatabase(db)
return
}
if(importData.entries){
const lores:loreBook[] = convertExternalLorebook(importData.entries)
const importModule = {
name: importData.name,
description: importData.description,
lorebook: lores,
id: v4()
}
db.modules.push(importModule)
setDatabase(db)
return
} }
} catch (error) { } catch (error) {
alertNormal(language.errors.noData) alertNormal(language.errors.noData)