Merge branch 'main' of https://github.com/hashcoko/RisuAI
This commit is contained in:
@@ -2,12 +2,14 @@ import { get, writable } from "svelte/store"
|
||||
import { DataBase } from "./database"
|
||||
import { hubURL } from "../characterCards"
|
||||
import localforage from "localforage"
|
||||
import { alertLogin, alertStore } from "../alert"
|
||||
import { alertError, alertLogin, alertStore, alertWait } from "../alert"
|
||||
import { forageStorage, getUnpargeables, replaceDbResources } from "./globalApi"
|
||||
import { encodeRisuSave } from "./risuSave"
|
||||
import { v4 } from "uuid"
|
||||
import { language } from "src/lang"
|
||||
|
||||
export const AccountWarning = writable('')
|
||||
const risuSession = Date.now().toFixed(0)
|
||||
|
||||
let seenWarnings:string[] = []
|
||||
|
||||
@@ -26,7 +28,8 @@ export class AccountStorage{
|
||||
'content-type': 'application/json',
|
||||
'x-risu-key': key,
|
||||
'x-risu-auth': this.auth,
|
||||
'X-Format': 'nocheck'
|
||||
'X-Format': 'nocheck',
|
||||
'x-risu-session': risuSession
|
||||
}
|
||||
})
|
||||
if(da.headers.get('Content-Type') === 'application/json'){
|
||||
@@ -37,6 +40,11 @@ export class AccountStorage{
|
||||
AccountWarning.set(json.warning)
|
||||
}
|
||||
}
|
||||
if(json?.reloadSession){
|
||||
alertWait(language.reloadSession)
|
||||
location.reload()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if(da.status === 304){
|
||||
|
||||
@@ -2,7 +2,7 @@ import localforage from "localforage"
|
||||
import { isNodeServer, replaceDbResources } from "./globalApi"
|
||||
import { NodeStorage } from "./nodeStorage"
|
||||
import { OpfsStorage } from "./opfsStorage"
|
||||
import { alertSelect, alertStore } from "../alert"
|
||||
import { alertInput, alertSelect, alertStore } from "../alert"
|
||||
import { get } from "svelte/store"
|
||||
import { DataBase, type Database } from "./database"
|
||||
import { AccountStorage } from "./accountStorage"
|
||||
@@ -67,6 +67,13 @@ export class AutoStorage{
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
const confirm = await alertInput(`to overwrite your data, type "RISUAI"`)
|
||||
if(confirm !== "RISUAI"){
|
||||
localStorage.setItem('dosync', 'avoid')
|
||||
return false
|
||||
}
|
||||
|
||||
let replaced:{[key:string]:string} = {}
|
||||
|
||||
for(const key of keys){
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
export const DataBase = writable({} as any as Database)
|
||||
export const loadedStore = writable(false)
|
||||
export let appVer = "124.2.2"
|
||||
export let appVer = "136.0.1"
|
||||
export let webAppSubVer = ''
|
||||
|
||||
import { get, writable } from 'svelte/store';
|
||||
@@ -428,7 +428,21 @@ export function setDatabase(data:Database){
|
||||
negInputName: 'text',
|
||||
timeout: 30
|
||||
}
|
||||
|
||||
data.hideApiKey ??= true
|
||||
data.unformatQuotes ??= false
|
||||
data.ttsAutoSpeech ??= false
|
||||
data.translatorInputLanguage ??= 'auto'
|
||||
data.falModel ??= 'fal-ai/flux/dev'
|
||||
data.falLoraScale ??= 1
|
||||
data.customCSS ??= ''
|
||||
data.strictJsonSchema ??= true
|
||||
data.statics ??= {
|
||||
messages: 0,
|
||||
imports: 0
|
||||
}
|
||||
data.customQuotes ??= false
|
||||
data.customQuotesData ??= ['“','”','‘','’']
|
||||
data.groupOtherBotRole ??= 'user'
|
||||
changeLanguage(data.language)
|
||||
DataBase.set(data)
|
||||
}
|
||||
@@ -508,6 +522,7 @@ export interface Database{
|
||||
NAII2I:boolean
|
||||
NAIREF:boolean
|
||||
NAIImgConfig:NAIImgConfig
|
||||
ttsAutoSpeech?:boolean
|
||||
runpodKey:string
|
||||
promptPreprocess:boolean
|
||||
bias: [string, number][]
|
||||
@@ -612,6 +627,7 @@ export interface Database{
|
||||
emotionProcesser:'submodel'|'embedding',
|
||||
showMenuChatList?:boolean,
|
||||
translatorType:'google'|'deepl'|'none'|'llm'|'deeplX',
|
||||
translatorInputLanguage?:string
|
||||
NAIadventure?:boolean,
|
||||
NAIappendName?:boolean,
|
||||
deeplOptions:{
|
||||
@@ -710,6 +726,30 @@ export interface Database{
|
||||
comfyUiUrl: string
|
||||
useLegacyGUI: boolean
|
||||
claudeCachingExperimental: boolean
|
||||
hideApiKey: boolean
|
||||
unformatQuotes: boolean
|
||||
enableDevTools: boolean
|
||||
falToken: string
|
||||
falModel: string
|
||||
falLora: string
|
||||
falLoraName: string
|
||||
falLoraScale: number
|
||||
moduleIntergration: string
|
||||
customCSS: string
|
||||
betaMobileGUI:boolean
|
||||
jsonSchemaEnabled:boolean
|
||||
jsonSchema:string
|
||||
strictJsonSchema:boolean
|
||||
extractJson:string
|
||||
ai21Key:string
|
||||
statics: {
|
||||
messages: number
|
||||
imports: number
|
||||
}
|
||||
customQuotes:boolean
|
||||
customQuotesData?:[string, string, string, string]
|
||||
groupTemplate?:string
|
||||
groupOtherBotRole?:string
|
||||
}
|
||||
|
||||
export interface customscript{
|
||||
@@ -802,6 +842,27 @@ export interface character{
|
||||
voice?: string
|
||||
version?: string
|
||||
}
|
||||
gptSoVitsConfig?:{
|
||||
url?:string
|
||||
use_auto_path?:boolean
|
||||
ref_audio_path?:string
|
||||
use_long_audio?:boolean
|
||||
ref_audio_data?: {
|
||||
fileName:string
|
||||
assetId:string
|
||||
}
|
||||
volume?:number
|
||||
text_lang?: "auto" | "auto_yue" | "en" | "zh" | "ja" | "yue" | "ko" | "all_zh" | "all_ja" | "all_yue" | "all_ko"
|
||||
text?:string
|
||||
use_prompt?:boolean
|
||||
prompt?:string | null
|
||||
prompt_lang?: "auto" | "auto_yue" | "en" | "zh" | "ja" | "yue" | "ko" | "all_zh" | "all_ja" | "all_yue" | "all_ko"
|
||||
top_p?:number
|
||||
temperature?:number
|
||||
speed?:number
|
||||
top_k?:number
|
||||
text_split_method?: "cut0" | "cut1" | "cut2" | "cut3" | "cut4" | "cut5"
|
||||
}
|
||||
supaMemory?:boolean
|
||||
additionalAssets?:[string, string, string][]
|
||||
ttsReadOnlyQuoted?:boolean
|
||||
@@ -842,6 +903,8 @@ export interface character{
|
||||
defaultVariables?:string
|
||||
lowLevelAccess?:boolean
|
||||
hideChatIcon?:boolean
|
||||
lastInteraction?:number
|
||||
translatorNote?:string
|
||||
}
|
||||
|
||||
|
||||
@@ -890,6 +953,7 @@ export interface groupChat{
|
||||
defaultVariables?:string
|
||||
lowLevelAccess?:boolean
|
||||
hideChatIcon?:boolean
|
||||
lastInteraction?:number
|
||||
}
|
||||
|
||||
export interface botPreset{
|
||||
@@ -939,6 +1003,16 @@ export interface botPreset{
|
||||
useInstructPrompt?:boolean
|
||||
customPromptTemplateToggle?:string
|
||||
templateDefaultVariables?:string
|
||||
moduleIntergration?:string
|
||||
top_k?:number
|
||||
instructChatTemplate?:string
|
||||
JinjaTemplate?:string
|
||||
jsonSchemaEnabled?:boolean
|
||||
jsonSchema?:string
|
||||
strictJsonSchema?:boolean
|
||||
extractJson?:string
|
||||
groupTemplate?:string
|
||||
groupOtherBotRole?:string
|
||||
}
|
||||
|
||||
|
||||
@@ -1009,6 +1083,7 @@ export interface Chat{
|
||||
modules?:string[]
|
||||
id?:string
|
||||
bindedPersona?:string
|
||||
fmIndex?:number
|
||||
}
|
||||
|
||||
export interface Message{
|
||||
@@ -1018,6 +1093,8 @@ export interface Message{
|
||||
chatId?:string
|
||||
time?: number
|
||||
generationInfo?: MessageGenerationInfo
|
||||
name?:string
|
||||
otherUser?:boolean
|
||||
}
|
||||
|
||||
export interface MessageGenerationInfo{
|
||||
@@ -1040,7 +1117,7 @@ interface AINsettings{
|
||||
top_k:number
|
||||
}
|
||||
|
||||
interface OobaSettings{
|
||||
export interface OobaSettings{
|
||||
max_new_tokens: number,
|
||||
do_sample: boolean,
|
||||
temperature: number,
|
||||
@@ -1218,6 +1295,16 @@ export function saveCurrentPreset(){
|
||||
useInstructPrompt: db.useInstructPrompt,
|
||||
customPromptTemplateToggle: db.customPromptTemplateToggle ?? "",
|
||||
templateDefaultVariables: db.templateDefaultVariables ?? "",
|
||||
moduleIntergration: db.moduleIntergration ?? "",
|
||||
top_k: db.top_k,
|
||||
instructChatTemplate: db.instructChatTemplate,
|
||||
JinjaTemplate: db.JinjaTemplate ?? '',
|
||||
jsonSchemaEnabled:db.jsonSchemaEnabled??false,
|
||||
jsonSchema:db.jsonSchema ?? '',
|
||||
strictJsonSchema:db.strictJsonSchema ?? true,
|
||||
extractJson:db.extractJson ?? '',
|
||||
groupOtherBotRole: db.groupOtherBotRole ?? 'user',
|
||||
groupTemplate: db.groupTemplate ?? '',
|
||||
}
|
||||
db.botPresets = pres
|
||||
setDatabase(db)
|
||||
@@ -1302,6 +1389,16 @@ export function setPreset(db:Database, newPres: botPreset){
|
||||
db.useInstructPrompt = newPres.useInstructPrompt ?? false
|
||||
db.customPromptTemplateToggle = newPres.customPromptTemplateToggle ?? ''
|
||||
db.templateDefaultVariables = newPres.templateDefaultVariables ?? ''
|
||||
db.moduleIntergration = newPres.moduleIntergration ?? ''
|
||||
db.top_k = newPres.top_k ?? db.top_k
|
||||
db.instructChatTemplate = newPres.instructChatTemplate ?? db.instructChatTemplate
|
||||
db.JinjaTemplate = newPres.JinjaTemplate ?? db.JinjaTemplate
|
||||
db.jsonSchemaEnabled = newPres.jsonSchemaEnabled ?? false
|
||||
db.jsonSchema = newPres.jsonSchema ?? ''
|
||||
db.strictJsonSchema = newPres.strictJsonSchema ?? true
|
||||
db.extractJson = newPres.extractJson ?? ''
|
||||
db.groupOtherBotRole = newPres.groupOtherBotRole ?? 'user'
|
||||
db.groupTemplate = newPres.groupTemplate ?? ''
|
||||
return db
|
||||
}
|
||||
|
||||
@@ -1310,6 +1407,7 @@ import * as fflate from "fflate";
|
||||
import type { OnnxModelFiles } from '../process/transformers';
|
||||
import type { RisuModule } from '../process/modules';
|
||||
import type { HypaV2Data } from '../process/memory/hypav2';
|
||||
import { decodeRPack, encodeRPack } from '../rpack/rpack_bg';
|
||||
|
||||
export async function downloadPreset(id:number, type:'json'|'risupreset'|'return' = 'json'){
|
||||
saveCurrentPreset()
|
||||
@@ -1334,8 +1432,10 @@ export async function downloadPreset(id:number, type:'json'|'risupreset'|'return
|
||||
'risupreset'
|
||||
)
|
||||
}))
|
||||
|
||||
if(type === 'risupreset'){
|
||||
downloadFile(pres.name + "_preset.risupreset", buf)
|
||||
const buf2 = await encodeRPack(buf)
|
||||
downloadFile(pres.name + "_preset.risup", buf2)
|
||||
}
|
||||
else{
|
||||
return {
|
||||
@@ -1361,14 +1461,18 @@ export async function importPreset(f:{
|
||||
data:Uint8Array
|
||||
}|null = null){
|
||||
if(!f){
|
||||
f = await selectSingleFile(["json", "preset", "risupreset"])
|
||||
f = await selectSingleFile(["json", "preset", "risupreset", "risup"])
|
||||
}
|
||||
if(!f){
|
||||
return
|
||||
}
|
||||
let pre:any
|
||||
if(f.name.endsWith('.risupreset')){
|
||||
const decoded = await decodeMsgpack(fflate.decompressSync(f.data))
|
||||
if(f.name.endsWith('.risupreset') || f.name.endsWith('.risup')){
|
||||
let data = f.data
|
||||
if(f.name.endsWith('.risup')){
|
||||
data = await decodeRPack(data)
|
||||
}
|
||||
const decoded = await decodeMsgpack(fflate.decompressSync(data))
|
||||
console.log(decoded)
|
||||
if((decoded.presetVersion === 0 || decoded.presetVersion === 2) && decoded.type === 'preset'){
|
||||
pre = {...presetTemplate,...decodeMsgpack(Buffer.from(await decryptBuffer(decoded.preset ?? decoded.pres, 'risupreset')))}
|
||||
|
||||
@@ -1,18 +1,24 @@
|
||||
import { writeBinaryFile,BaseDirectory, readBinaryFile, exists, createDir, readDir, removeFile } from "@tauri-apps/api/fs"
|
||||
|
||||
import {
|
||||
writeFile,
|
||||
BaseDirectory,
|
||||
readFile,
|
||||
exists,
|
||||
mkdir,
|
||||
readDir,
|
||||
remove
|
||||
} from "@tauri-apps/plugin-fs"
|
||||
import { changeFullscreen, checkNullish, findCharacterbyId, sleep } from "../util"
|
||||
import { convertFileSrc, invoke } from "@tauri-apps/api/tauri"
|
||||
import { convertFileSrc, invoke } from "@tauri-apps/api/core"
|
||||
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/api/shell'
|
||||
import {open} from '@tauri-apps/plugin-shell'
|
||||
import { DataBase, loadedStore, setDatabase, type Database, defaultSdDataFunc } from "./database";
|
||||
import { appWindow } from "@tauri-apps/api/window";
|
||||
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow";
|
||||
import { checkRisuUpdate } from "../update";
|
||||
import { botMakerMode, selectedCharID } from "../stores";
|
||||
import { Body, ResponseType, fetch as TauriFetch } from "@tauri-apps/api/http";
|
||||
import { MobileGUI, botMakerMode, selectedCharID } from "../stores";
|
||||
import { loadPlugins } from "../plugins/plugins";
|
||||
import { alertConfirm, alertError, alertNormal, alertNormalWait, alertSelect } from "../alert";
|
||||
import { alertConfirm, alertError, alertNormal, alertNormalWait, alertSelect, alertTOS, alertWait } from "../alert";
|
||||
import { checkDriverInit, syncDrive } from "../drive/drive";
|
||||
import { hasher } from "../parser";
|
||||
import { characterURLImport, hubURL } from "../characterCards";
|
||||
@@ -21,11 +27,11 @@ import { loadRisuAccountData } from "../drive/accounter";
|
||||
import { decodeRisuSave, encodeRisuSave } from "./risuSave";
|
||||
import { AutoStorage } from "./autoStorage";
|
||||
import { updateAnimationSpeed } from "../gui/animation";
|
||||
import { updateColorScheme, updateTextTheme } from "../gui/colorscheme";
|
||||
import { updateColorScheme, updateTextThemeAndCSS } from "../gui/colorscheme";
|
||||
import { saveDbKei } from "../kei/backup";
|
||||
import { Capacitor, CapacitorHttp } from '@capacitor/core';
|
||||
import * as CapFS from '@capacitor/filesystem'
|
||||
import { save } from "@tauri-apps/api/dialog";
|
||||
import { save } from "@tauri-apps/plugin-dialog";
|
||||
import type { RisuModule } from "../process/modules";
|
||||
import { listen } from '@tauri-apps/api/event'
|
||||
import { registerPlugin } from '@capacitor/core';
|
||||
@@ -35,13 +41,18 @@ import { removeDefaultHandler } from "src/main";
|
||||
import { updateGuisize } from "../gui/guisize";
|
||||
import { encodeCapKeySafe } from "./mobileStorage";
|
||||
import { updateLorebooks } from "../characters";
|
||||
import { initMobileGesture } from "../hotkey";
|
||||
import { fetch as TauriHTTPFetch } from '@tauri-apps/plugin-http';
|
||||
|
||||
//@ts-ignore
|
||||
export const isTauri = !!window.__TAURI__
|
||||
export const isTauri = !!window.__TAURI_INTERNALS__
|
||||
//@ts-ignore
|
||||
export const isNodeServer = !!globalThis.__NODE__
|
||||
export const forageStorage = new AutoStorage()
|
||||
export const googleBuild = false
|
||||
export const isMobile = navigator.userAgent.match(/(iPad)|(iPhone)|(iPod)|(android)|(webOS)/i)
|
||||
|
||||
const appWindow = isTauri ? getCurrentWebviewWindow() : null
|
||||
|
||||
interface fetchLog{
|
||||
body:string
|
||||
@@ -56,26 +67,6 @@ interface fetchLog{
|
||||
|
||||
let fetchLog:fetchLog[] = []
|
||||
|
||||
async function writeBinaryFileFast(appPath: string, data: Uint8Array) {
|
||||
const secret = await invoke('get_http_secret') as string;
|
||||
const port = await invoke('get_http_port') as number;
|
||||
|
||||
const apiUrl = `http://127.0.0.1:${port}/?path=${encodeURIComponent(appPath)}`;
|
||||
|
||||
const response = await fetch(apiUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/octet-stream',
|
||||
'x-tauri-secret': secret
|
||||
},
|
||||
body: new Blob([data])
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
}
|
||||
|
||||
export async function downloadFile(name:string, dat:Uint8Array|ArrayBuffer|string) {
|
||||
if(typeof(dat) === 'string'){
|
||||
dat = Buffer.from(dat, 'utf-8')
|
||||
@@ -92,7 +83,7 @@ export async function downloadFile(name:string, dat:Uint8Array|ArrayBuffer|strin
|
||||
}
|
||||
|
||||
if(isTauri){
|
||||
await writeBinaryFile(name, data, {dir: BaseDirectory.Download})
|
||||
await writeFile(name, data, {baseDir: BaseDirectory.Download})
|
||||
}
|
||||
else{
|
||||
downloadURL(`data:png/image;base64,${Buffer.from(data).toString('base64')}`, name)
|
||||
@@ -183,7 +174,7 @@ export async function getFileSrc(loc:string) {
|
||||
return "/sw/img/" + encoded
|
||||
}
|
||||
else{
|
||||
const f:Uint8Array = await forageStorage.getItem(loc)
|
||||
const f:Uint8Array = await forageStorage.getItem(loc) as unknown as Uint8Array
|
||||
await fetch("/sw/register/" + encoded, {
|
||||
method: "POST",
|
||||
body: f
|
||||
@@ -212,7 +203,7 @@ export async function getFileSrc(loc:string) {
|
||||
ind = fileCache.origin.length
|
||||
fileCache.origin.push(loc)
|
||||
fileCache.res.push('loading')
|
||||
const f:Uint8Array = await forageStorage.getItem(loc)
|
||||
const f:Uint8Array = await forageStorage.getItem(loc) as unknown as Uint8Array
|
||||
fileCache.res[ind] = f
|
||||
return `data:image/png;base64,${Buffer.from(f).toString('base64')}`
|
||||
}
|
||||
@@ -247,12 +238,12 @@ export async function readImage(data:string) {
|
||||
if(appDataDirPath === ''){
|
||||
appDataDirPath = await appDataDir();
|
||||
}
|
||||
return await readBinaryFile(await join(appDataDirPath,data))
|
||||
return await readFile(await join(appDataDirPath,data))
|
||||
}
|
||||
return await readBinaryFile(data)
|
||||
return await readFile(data)
|
||||
}
|
||||
else{
|
||||
return (await forageStorage.getItem(data) as Uint8Array)
|
||||
return (await forageStorage.getItem(data) as unknown as Uint8Array)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -281,7 +272,9 @@ export async function saveAsset(data:Uint8Array, customId:string = '', fileName:
|
||||
fileExtension = fileName.split('.').pop()
|
||||
}
|
||||
if(isTauri){
|
||||
await writeBinaryFileFast(`assets/${id}.${fileExtension}`, data);
|
||||
await writeFile(`assets/${id}.${fileExtension}`, data, {
|
||||
baseDir: BaseDirectory.AppData
|
||||
});
|
||||
return `assets/${id}.${fileExtension}`
|
||||
}
|
||||
else{
|
||||
@@ -302,10 +295,10 @@ export async function saveAsset(data:Uint8Array, customId:string = '', fileName:
|
||||
*/
|
||||
export async function loadAsset(id:string){
|
||||
if(isTauri){
|
||||
return await readBinaryFile(id,{dir: BaseDirectory.AppData})
|
||||
return await readFile(id,{baseDir: BaseDirectory.AppData})
|
||||
}
|
||||
else{
|
||||
return await forageStorage.getItem(id) as Uint8Array
|
||||
return await forageStorage.getItem(id) as unknown as Uint8Array
|
||||
}
|
||||
}
|
||||
|
||||
@@ -336,8 +329,8 @@ export async function saveDb(){
|
||||
}
|
||||
if(!gotChannel){
|
||||
gotChannel = true
|
||||
await alertNormalWait(language.activeTabChange)
|
||||
gotChannel = false
|
||||
alertWait(language.activeTabChange)
|
||||
location.reload()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -358,8 +351,8 @@ export async function saveDb(){
|
||||
db.saveTime = Math.floor(Date.now() / 1000)
|
||||
if(isTauri){
|
||||
const dbData = encodeRisuSave(db)
|
||||
await writeBinaryFileFast('database/database.bin', dbData);
|
||||
await writeBinaryFileFast(`database/dbbackup-${(Date.now()/100).toFixed()}.bin`, dbData);
|
||||
await writeFile('database/database.bin', dbData, {baseDir: BaseDirectory.AppData});
|
||||
await writeFile(`database/dbbackup-${(Date.now()/100).toFixed()}.bin`, dbData, {baseDir: BaseDirectory.AppData});
|
||||
}
|
||||
else{
|
||||
if(!forageStorage.isAccount){
|
||||
@@ -405,7 +398,7 @@ async function getDbBackups() {
|
||||
return []
|
||||
}
|
||||
if(isTauri){
|
||||
const keys = await readDir('database', {dir: BaseDirectory.AppData})
|
||||
const keys = await readDir('database', {baseDir: BaseDirectory.AppData})
|
||||
let backups:number[] = []
|
||||
for(const key of keys){
|
||||
if(key.name.startsWith("dbbackup-")){
|
||||
@@ -417,7 +410,7 @@ async function getDbBackups() {
|
||||
backups.sort((a, b) => b - a)
|
||||
while(backups.length > 20){
|
||||
const last = backups.pop()
|
||||
await removeFile(`database/dbbackup-${last}.bin`,{dir: BaseDirectory.AppData})
|
||||
await remove(`database/dbbackup-${last}.bin`,{baseDir: BaseDirectory.AppData})
|
||||
}
|
||||
return backups
|
||||
}
|
||||
@@ -452,27 +445,27 @@ export async function loadData() {
|
||||
try {
|
||||
if(isTauri){
|
||||
appWindow.maximize()
|
||||
if(!await exists('', {dir: BaseDirectory.AppData})){
|
||||
await createDir('', {dir: BaseDirectory.AppData})
|
||||
if(!await exists('', {baseDir: BaseDirectory.AppData})){
|
||||
await mkdir('', {baseDir: BaseDirectory.AppData})
|
||||
}
|
||||
if(!await exists('database', {dir: BaseDirectory.AppData})){
|
||||
await createDir('database', {dir: BaseDirectory.AppData})
|
||||
if(!await exists('database', {baseDir: BaseDirectory.AppData})){
|
||||
await mkdir('database', {baseDir: BaseDirectory.AppData})
|
||||
}
|
||||
if(!await exists('assets', {dir: BaseDirectory.AppData})){
|
||||
await createDir('assets', {dir: BaseDirectory.AppData})
|
||||
if(!await exists('assets', {baseDir: BaseDirectory.AppData})){
|
||||
await mkdir('assets', {baseDir: BaseDirectory.AppData})
|
||||
}
|
||||
if(!await exists('database/database.bin', {dir: BaseDirectory.AppData})){
|
||||
await writeBinaryFileFast('database/database.bin', encodeRisuSave({}));
|
||||
if(!await exists('database/database.bin', {baseDir: BaseDirectory.AppData})){
|
||||
await writeFile('database/database.bin', encodeRisuSave({}), {baseDir: BaseDirectory.AppData});
|
||||
}
|
||||
try {
|
||||
const decoded = decodeRisuSave(await readBinaryFile('database/database.bin',{dir: BaseDirectory.AppData}))
|
||||
const decoded = decodeRisuSave(await readFile('database/database.bin',{baseDir: BaseDirectory.AppData}))
|
||||
setDatabase(decoded)
|
||||
} catch (error) {
|
||||
const backups = await getDbBackups()
|
||||
let backupLoaded = false
|
||||
for(const backup of backups){
|
||||
try {
|
||||
const backupData = await readBinaryFile(`database/dbbackup-${backup}.bin`,{dir: BaseDirectory.AppData})
|
||||
const backupData = await readFile(`database/dbbackup-${backup}.bin`,{baseDir: BaseDirectory.AppData})
|
||||
setDatabase(
|
||||
decodeRisuSave(backupData)
|
||||
)
|
||||
@@ -490,7 +483,7 @@ export async function loadData() {
|
||||
|
||||
}
|
||||
else{
|
||||
let gotStorage:Uint8Array = await forageStorage.getItem('database/database.bin')
|
||||
let gotStorage:Uint8Array = await forageStorage.getItem('database/database.bin') as unknown as Uint8Array
|
||||
if(checkNullish(gotStorage)){
|
||||
gotStorage = encodeRisuSave({})
|
||||
await forageStorage.setItem('database/database.bin', gotStorage)
|
||||
@@ -505,7 +498,7 @@ export async function loadData() {
|
||||
let backupLoaded = false
|
||||
for(const backup of backups){
|
||||
try {
|
||||
const backupData:Uint8Array = await forageStorage.getItem(`database/dbbackup-${backup}.bin`)
|
||||
const backupData:Uint8Array = await forageStorage.getItem(`database/dbbackup-${backup}.bin`) as unknown as Uint8Array
|
||||
setDatabase(
|
||||
decodeRisuSave(backupData)
|
||||
)
|
||||
@@ -517,7 +510,7 @@ export async function loadData() {
|
||||
}
|
||||
}
|
||||
if(await forageStorage.checkAccountSync()){
|
||||
let gotStorage:Uint8Array = await forageStorage.getItem('database/database.bin')
|
||||
let gotStorage:Uint8Array = await forageStorage.getItem('database/database.bin') as unknown as Uint8Array
|
||||
if(checkNullish(gotStorage)){
|
||||
gotStorage = encodeRisuSave({})
|
||||
await forageStorage.setItem('database/database.bin', gotStorage)
|
||||
@@ -531,7 +524,7 @@ export async function loadData() {
|
||||
let backupLoaded = false
|
||||
for(const backup of backups){
|
||||
try {
|
||||
const backupData:Uint8Array = await forageStorage.getItem(`database/dbbackup-${backup}.bin`)
|
||||
const backupData:Uint8Array = await forageStorage.getItem(`database/dbbackup-${backup}.bin`) as unknown as Uint8Array
|
||||
setDatabase(
|
||||
decodeRisuSave(backupData)
|
||||
)
|
||||
@@ -568,11 +561,20 @@ export async function loadData() {
|
||||
try {
|
||||
await loadRisuAccountData()
|
||||
} catch (error) {}
|
||||
}
|
||||
try {
|
||||
//@ts-ignore
|
||||
const isInStandaloneMode = (window.matchMedia('(display-mode: standalone)').matches) || (window.navigator.standalone) || document.referrer.includes('android-app://');
|
||||
if(isInStandaloneMode){
|
||||
await navigator.storage.persist()
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
}
|
||||
await checkNewFormat()
|
||||
const db = get(DataBase);
|
||||
updateColorScheme()
|
||||
updateTextTheme()
|
||||
updateTextThemeAndCSS()
|
||||
updateAnimationSpeed()
|
||||
updateHeightMode()
|
||||
updateErrorHandling()
|
||||
@@ -580,10 +582,21 @@ export async function loadData() {
|
||||
if(db.botSettingAtStart){
|
||||
botMakerMode.set(true)
|
||||
}
|
||||
if((db.betaMobileGUI && window.innerWidth <= 800) || import.meta.env.VITE_RISU_LITE === 'TRUE'){
|
||||
initMobileGesture()
|
||||
MobileGUI.set(true)
|
||||
}
|
||||
loadedStore.set(true)
|
||||
selectedCharID.set(-1)
|
||||
startObserveDom()
|
||||
saveDb()
|
||||
saveDb()
|
||||
if(import.meta.env.VITE_RISU_TOS === 'TRUE'){
|
||||
alertTOS().then((a) => {
|
||||
if(a === false){
|
||||
location.reload()
|
||||
}
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
alertError(`${error}`)
|
||||
}
|
||||
@@ -639,6 +652,7 @@ const knownHostes = ["localhost", "127.0.0.1", "0.0.0.0"];
|
||||
*/
|
||||
interface GlobalFetchArgs {
|
||||
plainFetchForce?: boolean;
|
||||
plainFetchDeforce?: boolean;
|
||||
body?: any;
|
||||
headers?: { [key: string]: string };
|
||||
rawResponse?: boolean;
|
||||
@@ -712,8 +726,8 @@ export async function globalFetch(url: string, arg: GlobalFetchArgs = {}): Promi
|
||||
|
||||
if (arg.abortSignal?.aborted) { return { ok: false, data: 'aborted', headers: {} }; }
|
||||
|
||||
const urlHost = new URL(url).hostname;
|
||||
const forcePlainFetch = (knownHostes.includes(urlHost) && !isTauri) || db.usePlainFetch || arg.plainFetchForce;
|
||||
const urlHost = new URL(url).hostname
|
||||
const forcePlainFetch = ((knownHostes.includes(urlHost) && !isTauri) || db.usePlainFetch || arg.plainFetchForce) && !arg.plainFetchDeforce
|
||||
|
||||
if (knownHostes.includes(urlHost) && !isTauri && !isNodeServer) {
|
||||
return { ok: false, headers: {}, data: 'You are trying local request on web version. This is not allowed due to browser security policy. Use the desktop version instead, or use a tunneling service like ngrok and set the CORS to allow all.' };
|
||||
@@ -802,41 +816,19 @@ async function fetchWithPlainFetch(url: string, arg: GlobalFetchArgs): Promise<G
|
||||
* @returns {Promise<GlobalFetchResult>} - The result of the fetch request.
|
||||
*/
|
||||
async function fetchWithTauri(url: string, arg: GlobalFetchArgs): Promise<GlobalFetchResult> {
|
||||
const body = !arg.body ? null : arg.body instanceof URLSearchParams ? Body.text(arg.body.toString()) : Body.json(arg.body);
|
||||
const headers = arg.headers ?? {};
|
||||
const fetchPromise = TauriFetch(url, {
|
||||
body,
|
||||
method: arg.method ?? 'POST',
|
||||
headers,
|
||||
timeout: { secs: get(DataBase).timeOut, nanos: 0 },
|
||||
responseType: arg.rawResponse ? ResponseType.Binary : ResponseType.JSON,
|
||||
});
|
||||
|
||||
let abortFn = () => {};
|
||||
const abortPromise = new Promise<"aborted">((res, rej) => {
|
||||
abortFn = () => res("aborted");
|
||||
arg.abortSignal?.addEventListener('abort', abortFn);
|
||||
});
|
||||
|
||||
const result = await Promise.any([fetchPromise, abortPromise]);
|
||||
arg.abortSignal?.removeEventListener('abort', abortFn);
|
||||
|
||||
if (result === 'aborted') {
|
||||
return { ok: false, data: 'aborted', headers: {} };
|
||||
}
|
||||
|
||||
const data = arg.rawResponse ? new Uint8Array(result.data as number[]) : result.data;
|
||||
addFetchLogInGlobalFetch(data, result.ok, url, arg);
|
||||
return { ok: result.ok, data, headers: result.headers };
|
||||
try {
|
||||
const headers = { 'Content-Type': 'application/json', ...arg.headers };
|
||||
const response = await TauriHTTPFetch(new URL(url), { body: JSON.stringify(arg.body), headers, method: arg.method ?? "POST", signal: arg.abortSignal });
|
||||
const data = arg.rawResponse ? new Uint8Array(await response.arrayBuffer()) : await response.json();
|
||||
const ok = response.status >= 200 && response.status < 300;
|
||||
addFetchLogInGlobalFetch(data, ok, url, arg);
|
||||
return { ok, data, headers: Object.fromEntries(response.headers) };
|
||||
} catch (error) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a fetch request using Capacitor.
|
||||
*
|
||||
* @param {string} url - The URL to fetch.
|
||||
* @param {GlobalFetchArgs} arg - The arguments for the fetch request.
|
||||
* @returns {Promise<GlobalFetchResult>} - The result of the fetch request.
|
||||
*/
|
||||
// Decoupled globalFetch built-in function
|
||||
async function fetchWithCapacitor(url: string, arg: GlobalFetchArgs): Promise<GlobalFetchResult> {
|
||||
const { body, headers = {}, rawResponse } = arg;
|
||||
headers["Content-Type"] = body instanceof URLSearchParams ? "application/x-www-form-urlencoded" : "application/json";
|
||||
@@ -992,7 +984,18 @@ export function getUnpargeables(db: Database, uptype: 'basename' | 'pure' = 'bas
|
||||
}
|
||||
}
|
||||
|
||||
if (db.personas) {
|
||||
if(db.modules){
|
||||
for(const module of db.modules){
|
||||
const assets = module.assets
|
||||
if(assets){
|
||||
for(const asset of assets){
|
||||
addUnparge(asset[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(db.personas){
|
||||
db.personas.map((v) => {
|
||||
addUnparge(v.icon);
|
||||
});
|
||||
@@ -1280,13 +1283,13 @@ async function pargeChunks(){
|
||||
|
||||
const unpargeable = getUnpargeables(db)
|
||||
if(isTauri){
|
||||
const assets = await readDir('assets', {dir: BaseDirectory.AppData})
|
||||
const assets = await readDir('assets', {baseDir: BaseDirectory.AppData})
|
||||
for(const asset of assets){
|
||||
const n = getBasename(asset.name)
|
||||
if(unpargeable.includes(n)){
|
||||
}
|
||||
else{
|
||||
await removeFile(asset.path)
|
||||
await remove(asset.name, {baseDir: BaseDirectory.AppData})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1401,7 +1404,7 @@ export class TauriWriter{
|
||||
* @param {Uint8Array} data - The data to write.
|
||||
*/
|
||||
async write(data:Uint8Array) {
|
||||
await writeBinaryFile(this.path, data, {
|
||||
await writeFile(this.path, data, {
|
||||
append: !this.firstWrite
|
||||
})
|
||||
this.firstWrite = false
|
||||
@@ -1864,7 +1867,7 @@ export async function fetchNative(url:string, arg:{
|
||||
const data = nativeFetchData[fetchId].shift()
|
||||
if(data.type === 'chunk'){
|
||||
const chunk = Buffer.from(data.body, 'base64')
|
||||
controller.enqueue(chunk)
|
||||
controller.enqueue(chunk as unknown as Uint8Array)
|
||||
}
|
||||
if(data.type === 'headers'){
|
||||
resHeaders = data.body
|
||||
@@ -2040,7 +2043,7 @@ export class BlankWriter{
|
||||
|
||||
export async function loadInternalBackup(){
|
||||
|
||||
const keys = isTauri ? (await readDir('database', {dir: BaseDirectory.AppData})).map((v) => {
|
||||
const keys = isTauri ? (await readDir('database', {baseDir: BaseDirectory.AppData})).map((v) => {
|
||||
return v.name
|
||||
}) : (await forageStorage.keys())
|
||||
let internalBackups:string[] = []
|
||||
@@ -2068,11 +2071,11 @@ export async function loadInternalBackup(){
|
||||
const selectedBackup = internalBackups[alertResult]
|
||||
|
||||
const data = isTauri ? (
|
||||
await readBinaryFile('database/' + selectedBackup, {dir: BaseDirectory.AppData})
|
||||
await readFile('database/' + selectedBackup, {baseDir: BaseDirectory.AppData})
|
||||
) : (await forageStorage.getItem(selectedBackup))
|
||||
|
||||
setDatabase(
|
||||
decodeRisuSave(data)
|
||||
decodeRisuSave(Buffer.from(data) as unknown as Uint8Array)
|
||||
)
|
||||
|
||||
await alertNormal('Loaded backup')
|
||||
|
||||
Reference in New Issue
Block a user