Migrate all DataBase to DBState
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import { get, writable } from "svelte/store"
|
||||
import { DataBase } from "./database.svelte"
|
||||
import { writable } from "svelte/store"
|
||||
import { getDatabase } from "./database.svelte"
|
||||
import { hubURL } from "../characterCards"
|
||||
import localforage from "localforage"
|
||||
import { alertError, alertLogin, alertStore, alertWait } from "../alert"
|
||||
import { forageStorage, getUnpargeables, replaceDbResources } from "./globalApi"
|
||||
import { alertLogin, alertStore, alertWait } from "../alert"
|
||||
import { forageStorage, getUnpargeables } from "./globalApi"
|
||||
import { encodeRisuSave } from "./risuSave"
|
||||
import { v4 } from "uuid"
|
||||
import { language } from "src/lang"
|
||||
@@ -99,7 +99,7 @@ export class AccountStorage{
|
||||
return Buffer.from(ab)
|
||||
}
|
||||
async keys():Promise<string[]>{
|
||||
let db = get(DataBase)
|
||||
let db = getDatabase()
|
||||
return getUnpargeables(db, 'pure')
|
||||
}
|
||||
async removeItem(key:string){
|
||||
@@ -107,7 +107,7 @@ export class AccountStorage{
|
||||
}
|
||||
|
||||
private checkAuth(){
|
||||
const db = get(DataBase)
|
||||
const db = getDatabase()
|
||||
this.auth = db?.account?.token
|
||||
if(!this.auth){
|
||||
db.account = JSON.parse(localStorage.getItem("fallbackRisuToken"))
|
||||
@@ -122,7 +122,7 @@ export class AccountStorage{
|
||||
|
||||
export async function unMigrationAccount() {
|
||||
const keys = await forageStorage.keys()
|
||||
let db = get(DataBase)
|
||||
let db = getDatabase()
|
||||
let i = 0;
|
||||
const MigrationStorage = localforage.createInstance({name: "risuai"})
|
||||
|
||||
|
||||
@@ -3,8 +3,7 @@ import { isNodeServer, replaceDbResources } from "./globalApi"
|
||||
import { NodeStorage } from "./nodeStorage"
|
||||
import { OpfsStorage } from "./opfsStorage"
|
||||
import { alertInput, alertSelect, alertStore } from "../alert"
|
||||
import { get } from "svelte/store"
|
||||
import { DataBase, type Database } from "./database.svelte"
|
||||
import { getDatabase, type Database } from "./database.svelte"
|
||||
import { AccountStorage } from "./accountStorage"
|
||||
import { decodeRisuSave, encodeRisuSave } from "./risuSave";
|
||||
import { language } from "src/lang"
|
||||
@@ -40,7 +39,7 @@ export class AutoStorage{
|
||||
}
|
||||
|
||||
async checkAccountSync(){
|
||||
let db = get(DataBase)
|
||||
let db = getDatabase()
|
||||
if(this.isAccount){
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -12,28 +12,13 @@ import { defaultColorScheme, type ColorScheme } from '../gui/colorscheme';
|
||||
import type { PromptItem, PromptSettings } from '../process/prompt';
|
||||
import type { OobaChatCompletionRequestParams } from '../model/ooba';
|
||||
|
||||
export const DataBase = writable({} as any as Database)
|
||||
export const DBState = $state({
|
||||
db: get(DataBase)
|
||||
db: {} as any as Database
|
||||
})
|
||||
export const loadedStore = writable(false)
|
||||
export let appVer = "137.1.0"
|
||||
export let webAppSubVer = '-svelte5-exp'
|
||||
|
||||
DataBase.subscribe(data => {
|
||||
//check it is pointing to the right object
|
||||
if(DBState.db !== data){
|
||||
//if not, update it
|
||||
DBState.db = data
|
||||
}
|
||||
})
|
||||
|
||||
$effect(() => {
|
||||
//same as above, but for the other way around
|
||||
if(DBState.db !== get(DataBase)){
|
||||
DataBase.set(DBState.db)
|
||||
}
|
||||
})
|
||||
|
||||
export function setDatabase(data:Database){
|
||||
if(checkNullish(data.characters)){
|
||||
@@ -464,21 +449,36 @@ export function setDatabase(data:Database){
|
||||
data.groupOtherBotRole ??= 'user'
|
||||
data.customGUI ??= ''
|
||||
changeLanguage(data.language)
|
||||
DataBase.set(data)
|
||||
setDatabaseLite(data)
|
||||
}
|
||||
|
||||
export function getCurrentCharacter(){
|
||||
const db = get(DataBase)
|
||||
export function setDatabaseLite(data:Database){
|
||||
DBState.db = data
|
||||
}
|
||||
|
||||
interface getDatabaseOptions{
|
||||
snapshot?:boolean
|
||||
}
|
||||
|
||||
export function getDatabase(options:getDatabaseOptions = {}):Database{
|
||||
if(options.snapshot){
|
||||
return $state.snapshot(DBState.db) as Database
|
||||
}
|
||||
return DBState.db as Database
|
||||
}
|
||||
|
||||
export function getCurrentCharacter(options:getDatabaseOptions = {}):character|groupChat{
|
||||
const db = getDatabase(options)
|
||||
db.characters ??= []
|
||||
const char = db.characters?.[get(selectedCharID)]
|
||||
return char
|
||||
}
|
||||
|
||||
export function setCurrentCharacter(char:character|groupChat){
|
||||
const db = get(DataBase)
|
||||
const db = getDatabase()
|
||||
db.characters ??= []
|
||||
db.characters[get(selectedCharID)] = char
|
||||
DataBase.set(db)
|
||||
setDatabaseLite(db)
|
||||
}
|
||||
|
||||
export function getCurrentChat(){
|
||||
@@ -1328,7 +1328,7 @@ export const defaultSdDataFunc = () =>{
|
||||
}
|
||||
|
||||
export function saveCurrentPreset(){
|
||||
let db = get(DataBase)
|
||||
let db = getDatabase()
|
||||
let pres = db.botPresets
|
||||
pres[db.botPresetsId] = {
|
||||
name: pres[db.botPresetsId].name,
|
||||
@@ -1392,7 +1392,7 @@ export function saveCurrentPreset(){
|
||||
|
||||
export function copyPreset(id:number){
|
||||
saveCurrentPreset()
|
||||
let db = get(DataBase)
|
||||
let db = getDatabase()
|
||||
let pres = db.botPresets
|
||||
const newPres = structuredClone(pres[id])
|
||||
newPres.name += " Copy"
|
||||
@@ -1404,7 +1404,7 @@ export function changeToPreset(id =0, savecurrent = true){
|
||||
if(savecurrent){
|
||||
saveCurrentPreset()
|
||||
}
|
||||
let db = get(DataBase)
|
||||
let db = getDatabase()
|
||||
let pres = db.botPresets
|
||||
const newPres = pres[id]
|
||||
db.botPresetsId = id
|
||||
@@ -1492,7 +1492,7 @@ import { selectedCharID } from '../stores';
|
||||
|
||||
export async function downloadPreset(id:number, type:'json'|'risupreset'|'return' = 'json'){
|
||||
saveCurrentPreset()
|
||||
let db = get(DataBase)
|
||||
let db = getDatabase()
|
||||
let pres = structuredClone(db.botPresets[id])
|
||||
console.log(pres)
|
||||
pres.openAIKey = ''
|
||||
@@ -1563,7 +1563,7 @@ export async function importPreset(f:{
|
||||
pre = {...presetTemplate,...(JSON.parse(Buffer.from(f.data).toString('utf-8')))}
|
||||
console.log(pre)
|
||||
}
|
||||
let db = get(DataBase)
|
||||
let db = getDatabase()
|
||||
if(pre.presetVersion && pre.presetVersion >= 3){
|
||||
//NAI preset
|
||||
const pr = structuredClone(prebuiltPresets.NAI2)
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { get } from "svelte/store";
|
||||
import { DataBase } from "./database.svelte";
|
||||
import { getDatabase } from "./database.svelte";
|
||||
import { downloadFile } from "./globalApi";
|
||||
import { alertNormal } from "../alert";
|
||||
import { language } from "src/lang";
|
||||
|
||||
export async function exportAsDataset(){
|
||||
const db = get(DataBase)
|
||||
const db = getDatabase()
|
||||
|
||||
let dataset = []
|
||||
for(const char of db.characters){
|
||||
|
||||
@@ -13,7 +13,7 @@ import { v4 as uuidv4, v4 } from 'uuid';
|
||||
import { appDataDir, join } from "@tauri-apps/api/path";
|
||||
import { get } from "svelte/store";
|
||||
import {open} from '@tauri-apps/plugin-shell'
|
||||
import { DataBase, loadedStore, setDatabase, type Database, defaultSdDataFunc } from "./database.svelte";
|
||||
import { loadedStore, setDatabase, type Database, defaultSdDataFunc, getDatabase } from "./database.svelte";
|
||||
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow";
|
||||
import { checkRisuUpdate } from "../update";
|
||||
import { MobileGUI, botMakerMode, selectedCharID } from "../stores";
|
||||
@@ -310,12 +310,9 @@ let lastSave = ''
|
||||
* @returns {Promise<void>} - A promise that resolves when the database has been saved.
|
||||
*/
|
||||
export async function saveDb(){
|
||||
lastSave = JSON.stringify(get(DataBase))
|
||||
let changed = false
|
||||
lastSave = JSON.stringify(getDatabase())
|
||||
let changed = true
|
||||
syncDrive()
|
||||
DataBase.subscribe(() => {
|
||||
changed = true
|
||||
})
|
||||
let gotChannel = false
|
||||
const sessionID = v4()
|
||||
let channel:BroadcastChannel
|
||||
@@ -346,8 +343,11 @@ export async function saveDb(){
|
||||
if(channel){
|
||||
channel.postMessage(sessionID)
|
||||
}
|
||||
changed = false
|
||||
let db = get(DataBase)
|
||||
let db = getDatabase()
|
||||
if(!db.characters){
|
||||
await sleep(1000)
|
||||
continue
|
||||
}
|
||||
db.saveTime = Math.floor(Date.now() / 1000)
|
||||
if(isTauri){
|
||||
const dbData = encodeRisuSave(db)
|
||||
@@ -393,7 +393,7 @@ export async function saveDb(){
|
||||
* @returns {Promise<number[]>} - A promise that resolves to an array of backup timestamps.
|
||||
*/
|
||||
async function getDbBackups() {
|
||||
let db = get(DataBase)
|
||||
let db = getDatabase()
|
||||
if(db?.account?.useSync){
|
||||
return []
|
||||
}
|
||||
@@ -547,7 +547,7 @@ export async function loadData() {
|
||||
else{
|
||||
usingSw = false
|
||||
}
|
||||
if(get(DataBase).didFirstSetup){
|
||||
if(getDatabase().didFirstSetup){
|
||||
characterURLImport()
|
||||
}
|
||||
}
|
||||
@@ -557,7 +557,7 @@ export async function loadData() {
|
||||
try {
|
||||
await loadPlugins()
|
||||
} catch (error) {}
|
||||
if(get(DataBase).account){
|
||||
if(getDatabase().account){
|
||||
try {
|
||||
await loadRisuAccountData()
|
||||
} catch (error) {}
|
||||
@@ -572,7 +572,7 @@ export async function loadData() {
|
||||
|
||||
}
|
||||
await checkNewFormat()
|
||||
const db = get(DataBase);
|
||||
const db = getDatabase();
|
||||
updateColorScheme()
|
||||
updateTextThemeAndCSS()
|
||||
updateAnimationSpeed()
|
||||
@@ -720,7 +720,7 @@ export function addFetchLog(arg: {
|
||||
*/
|
||||
export async function globalFetch(url: string, arg: GlobalFetchArgs = {}): Promise<GlobalFetchResult> {
|
||||
try {
|
||||
const db = get(DataBase);
|
||||
const db = getDatabase();
|
||||
const method = arg.method ?? "POST";
|
||||
db.requestmet = "normal";
|
||||
|
||||
@@ -1056,7 +1056,7 @@ export function replaceDbResources(db: Database, replacer: { [key: string]: stri
|
||||
* @returns {Promise<void>} - A promise that resolves when the database format check and update is complete.
|
||||
*/
|
||||
async function checkNewFormat(): Promise<void> {
|
||||
let db = get(DataBase);
|
||||
let db = getDatabase();
|
||||
|
||||
// Check data integrity
|
||||
db.characters = db.characters.map((v) => {
|
||||
@@ -1147,40 +1147,7 @@ async function checkNewFormat(): Promise<void> {
|
||||
db.formatversion = 3;
|
||||
}
|
||||
if (db.formatversion < 4) {
|
||||
db.modules ??= [];
|
||||
db.enabledModules ??= [];
|
||||
// Convert global lore and global regex to modules
|
||||
if (db.globalscript && db.globalscript.length > 0) {
|
||||
const id = v4();
|
||||
let regexModule: RisuModule = {
|
||||
name: "Global Regex",
|
||||
description: "Converted from legacy global regex",
|
||||
id: id,
|
||||
regex: structuredClone(db.globalscript)
|
||||
};
|
||||
db.modules.push(regexModule);
|
||||
db.enabledModules.push(id);
|
||||
db.globalscript = [];
|
||||
}
|
||||
if (db.loreBook && db.loreBook.length > 0) {
|
||||
const selIndex = db.loreBookPage;
|
||||
for (let i = 0; i < db.loreBook.length; i++) {
|
||||
const id = v4();
|
||||
let lbModule: RisuModule = {
|
||||
name: db.loreBook[i].name || "Unnamed Global Lorebook",
|
||||
description: "Converted from legacy global lorebook",
|
||||
id: id,
|
||||
lorebook: structuredClone(db.loreBook[i].data)
|
||||
};
|
||||
db.modules.push(lbModule);
|
||||
if (i === selIndex) {
|
||||
db.enabledModules.push(id);
|
||||
}
|
||||
db.globalscript = [];
|
||||
}
|
||||
db.loreBook = [];
|
||||
}
|
||||
|
||||
//migration removed due to issues
|
||||
db.formatversion = 4;
|
||||
}
|
||||
if (!db.characterOrder) {
|
||||
@@ -1209,9 +1176,9 @@ async function checkNewFormat(): Promise<void> {
|
||||
* Ensures that all characters are properly ordered and removes any invalid entries.
|
||||
*/
|
||||
export function checkCharOrder() {
|
||||
let db = get(DataBase)
|
||||
let db = getDatabase()
|
||||
db.characterOrder = db.characterOrder ?? []
|
||||
let ordered = structuredClone(db.characterOrder ?? [])
|
||||
let ordered = []
|
||||
for(let i=0;i<db.characterOrder.length;i++){
|
||||
const folder =db.characterOrder[i]
|
||||
if(typeof(folder) !== 'string' && folder){
|
||||
@@ -1219,6 +1186,9 @@ export function checkCharOrder() {
|
||||
ordered.push(f)
|
||||
}
|
||||
}
|
||||
if(typeof(folder) === 'string'){
|
||||
ordered.push(folder)
|
||||
}
|
||||
}
|
||||
|
||||
let charIdList:string[] = []
|
||||
@@ -1276,7 +1246,7 @@ export function checkCharOrder() {
|
||||
* Removes files from the assets directory that are not in the list of unpargeable items.
|
||||
*/
|
||||
async function pargeChunks(){
|
||||
const db = get(DataBase)
|
||||
const db = getDatabase()
|
||||
if(db.account?.useSync){
|
||||
return
|
||||
}
|
||||
@@ -1797,7 +1767,7 @@ export async function fetchNative(url:string, arg:{
|
||||
chatId?:string
|
||||
}):Promise<{ body: ReadableStream<Uint8Array>; headers: Headers; status: number }> {
|
||||
let headers = arg.headers ?? {}
|
||||
const db = get(DataBase)
|
||||
const db = getDatabase()
|
||||
let throughProxy = (!isTauri) && (!isNodeServer) && (!db.usePlainFetch)
|
||||
let fetchLogIndex = addFetchLog({
|
||||
body: arg.body,
|
||||
@@ -1977,7 +1947,7 @@ export function trimNonLatin(data:string){
|
||||
* The corresponding CSS variable '--risu-height-size' is set accordingly.
|
||||
*/
|
||||
export function updateHeightMode(){
|
||||
const db = get(DataBase)
|
||||
const db = getDatabase()
|
||||
const root = document.querySelector(':root') as HTMLElement;
|
||||
switch(db.heightMode){
|
||||
case 'auto':
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { get } from "svelte/store";
|
||||
import { DataBase } from "./database.svelte";
|
||||
import { getDatabase } from "./database.svelte";
|
||||
import { alertNormal } from "../alert";
|
||||
import { language } from "src/lang";
|
||||
import { isNodeServer, isTauri } from "./globalApi";
|
||||
@@ -38,7 +38,7 @@ async function requestPersistantStorageMain() {
|
||||
}
|
||||
|
||||
export async function persistantStorageRecommended() {
|
||||
const db = get(DataBase)
|
||||
const db = getDatabase()
|
||||
if(navigator.storage && navigator.storage.persist && (!isTauri) && (!isNodeServer)) {
|
||||
if(await navigator.storage.persisted()) {
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user