Migrate to svelte 5
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
import { get, writable, type Writable } from "svelte/store"
|
||||
import { alertCardExport, alertConfirm, alertError, alertInput, alertMd, alertNormal, alertSelect, alertStore, alertTOS, alertWait } from "./alert"
|
||||
import { DataBase, defaultSdDataFunc, type character, setDatabase, type customscript, type loreSettings, type loreBook, type triggerscript, importPreset, type groupChat } from "./storage/database"
|
||||
import { DataBase, defaultSdDataFunc, type character, setDatabase, type customscript, type loreSettings, type loreBook, type triggerscript, importPreset, type groupChat, setCurrentCharacter, getCurrentCharacter } from "./storage/database"
|
||||
import { checkNullish, decryptBuffer, encryptBuffer, isKnownUri, selectFileByDom, selectMultipleFile, sleep } from "./util"
|
||||
import { language } from "src/lang"
|
||||
import { v4 as uuidv4, v4 } from 'uuid';
|
||||
import { characterFormatUpdate } from "./characters"
|
||||
import { AppendableBuffer, BlankWriter, checkCharOrder, downloadFile, isNodeServer, isTauri, loadAsset, LocalWriter, openURL, readImage, saveAsset, VirtualWriter } from "./storage/globalApi"
|
||||
import { CurrentCharacter, SettingsMenuIndex, ShowRealmFrameStore, selectedCharID, settingsOpen } from "./stores"
|
||||
import { SettingsMenuIndex, ShowRealmFrameStore, selectedCharID, settingsOpen } from "./stores"
|
||||
import { convertImage, hasher } from "./parser"
|
||||
import { CCardLib, type CharacterCardV3, type LorebookEntry } from '@risuai/ccardlib'
|
||||
import { reencodeImage } from "./process/files/image"
|
||||
@@ -1450,12 +1450,12 @@ export async function shareRisuHub2(char:character, arg:{
|
||||
else{
|
||||
const resJSON = await res.json()
|
||||
alertMd(resJSON.message)
|
||||
const currentChar = get(CurrentCharacter)
|
||||
const currentChar = getCurrentCharacter()
|
||||
if(currentChar.type === 'group'){
|
||||
return
|
||||
}
|
||||
currentChar.realmId = resJSON.id
|
||||
CurrentCharacter.set(currentChar)
|
||||
setCurrentCharacter(currentChar)
|
||||
}
|
||||
} catch (error) {
|
||||
alertError(`${error}`)
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import { get } from "svelte/store";
|
||||
import { runTrigger } from "./process/triggers";
|
||||
import { CurrentCharacter, CurrentChat } from "./stores";
|
||||
import { runCharacterJS } from "./plugins/embedscript";
|
||||
import { sleep } from "./util";
|
||||
import { getCurrentCharacter, getCurrentChat, setCurrentChat } from "./storage/database";
|
||||
|
||||
|
||||
function nodeObserve(node:HTMLElement){
|
||||
@@ -15,17 +14,17 @@ function nodeObserve(node:HTMLElement){
|
||||
}
|
||||
if(triggerName){
|
||||
node.addEventListener('click', async () => {
|
||||
const currentChar = get(CurrentCharacter)
|
||||
const currentChar = getCurrentCharacter()
|
||||
if(currentChar.type === 'group'){
|
||||
return;
|
||||
}
|
||||
const triggerResult = await runTrigger(currentChar, 'manual', {
|
||||
chat: get(CurrentChat),
|
||||
chat: getCurrentChat(),
|
||||
manualName: triggerName,
|
||||
});
|
||||
|
||||
if(triggerResult){
|
||||
CurrentChat.set(triggerResult.chat);
|
||||
setCurrentChat(triggerResult.chat);
|
||||
}
|
||||
|
||||
}, {
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import DOMPurify from 'isomorphic-dompurify';
|
||||
import markdownit from 'markdown-it'
|
||||
import { DataBase, setDatabase, type Database, type Message, type character, type customscript, type groupChat, type triggerscript } from './storage/database';
|
||||
import { DataBase, getCurrentCharacter, setDatabase, type Database, type Message, type character, type customscript, type groupChat, type triggerscript } from './storage/database';
|
||||
import { getFileSrc } from './storage/globalApi';
|
||||
import { processScriptFull } from './process/scripts';
|
||||
import { get } from 'svelte/store';
|
||||
import css, { type CssAtRuleAST } from '@adobe/css-tools'
|
||||
import { CurrentCharacter, SizeStore, selectedCharID } from './stores';
|
||||
import { SizeStore, selectedCharID } from './stores';
|
||||
import { calcString } from './process/infunctions';
|
||||
import { findCharacterbyId, getPersonaPrompt, getUserIcon, getUserName, parseKeyValue, sfc32, sleep, uuidtoNumber } from './util';
|
||||
import { getInlayImage } from './process/files/image';
|
||||
@@ -348,7 +348,7 @@ async function parseAdditionalAssets(data:string, char:simpleCharacterArgument|c
|
||||
})
|
||||
|
||||
if(needsSourceAccess){
|
||||
const chara = get(CurrentCharacter)
|
||||
const chara = getCurrentCharacter()
|
||||
if(chara.image){}
|
||||
data = data.replace(/\uE9b4CHAR\uE9b4/g,
|
||||
chara.image ? (await getFileSrc(chara.image)) : ''
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { get } from "svelte/store";
|
||||
import { DataBase, setDatabase } from "../storage/database";
|
||||
import { CurrentCharacter, CurrentChat, selectedCharID } from "../stores";
|
||||
import { DataBase, getCurrentCharacter, getCurrentChat, setCurrentChat, setDatabase } from "../storage/database";
|
||||
import { selectedCharID } from "../stores";
|
||||
import { alertInput, alertMd, alertNormal, alertSelect, alertToast } from "../alert";
|
||||
import { sayTTS } from "./tts";
|
||||
import { risuChatParser } from "../parser";
|
||||
@@ -222,17 +222,17 @@ async function processCommand(command:string, pipe:string):Promise<false | strin
|
||||
return JSON.stringify(p)
|
||||
}
|
||||
case 'trigger':{
|
||||
const currentChar = get(CurrentCharacter)
|
||||
const currentChar = getCurrentCharacter()
|
||||
if(currentChar.type === 'group'){
|
||||
return;
|
||||
}
|
||||
const triggerResult = await runTrigger(currentChar, 'manual', {
|
||||
chat: get(CurrentChat),
|
||||
chat: getCurrentChat(),
|
||||
manualName: arg
|
||||
});
|
||||
|
||||
if(triggerResult){
|
||||
CurrentChat.set(triggerResult.chat);
|
||||
setCurrentChat(triggerResult.chat);
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { getChatVar, hasher, risuChatParser, setChatVar, type simpleCharacterArgument } from "../parser";
|
||||
import { LuaEngine, LuaFactory } from "wasmoon";
|
||||
import { DataBase, setDatabase, type Chat, type character, type groupChat } from "../storage/database";
|
||||
import { DataBase, getCurrentCharacter, getCurrentChat, setCurrentChat, setDatabase, type Chat, type character, type groupChat } from "../storage/database";
|
||||
import { get } from "svelte/store";
|
||||
import { CurrentCharacter, CurrentChat, ReloadGUIPointer, selectedCharID } from "../stores";
|
||||
import { ReloadGUIPointer, selectedCharID } from "../stores";
|
||||
import { alertError, alertInput, alertNormal } from "../alert";
|
||||
import { HypaProcesser } from "./memory/hypamemory";
|
||||
import { generateAIImage } from "./stableDiff";
|
||||
@@ -35,12 +35,12 @@ export async function runLua(code:string, arg:{
|
||||
mode?: string,
|
||||
data?: any
|
||||
}){
|
||||
const char = arg.char ?? get(CurrentCharacter)
|
||||
const char = arg.char ?? getCurrentCharacter()
|
||||
const setVar = arg.setVar ?? setChatVar
|
||||
const getVar = arg.getVar ?? getChatVar
|
||||
const mode = arg.mode ?? 'manual'
|
||||
const data = arg.data ?? {}
|
||||
let chat = arg.chat ?? get(CurrentChat)
|
||||
let chat = arg.chat ?? getCurrentChat()
|
||||
let stopSending = false
|
||||
let lowLevelAccess = arg.lowLevelAccess ?? false
|
||||
|
||||
@@ -104,75 +104,75 @@ export async function runLua(code:string, arg:{
|
||||
if(!LuaSafeIds.has(id)){
|
||||
return
|
||||
}
|
||||
chat = get(CurrentChat)
|
||||
chat = getCurrentChat()
|
||||
const message = chat.message?.at(index)
|
||||
if(message){
|
||||
message.data = value
|
||||
}
|
||||
CurrentChat.set(chat)
|
||||
setCurrentChat(chat)
|
||||
})
|
||||
luaEngine.global.set('setChatRole', (id:string, index:number, value:string) => {
|
||||
if(!LuaSafeIds.has(id)){
|
||||
return
|
||||
}
|
||||
chat = get(CurrentChat)
|
||||
chat = getCurrentChat()
|
||||
const message = chat.message?.at(index)
|
||||
if(message){
|
||||
message.role = value === 'user' ? 'user' : 'char'
|
||||
}
|
||||
CurrentChat.set(chat)
|
||||
setCurrentChat(chat)
|
||||
})
|
||||
luaEngine.global.set('cutChat', (id:string, start:number, end:number) => {
|
||||
if(!LuaSafeIds.has(id)){
|
||||
return
|
||||
}
|
||||
chat = get(CurrentChat)
|
||||
chat = getCurrentChat()
|
||||
chat.message = chat.message.slice(start,end)
|
||||
CurrentChat.set(chat)
|
||||
setCurrentChat(chat)
|
||||
})
|
||||
luaEngine.global.set('removeChat', (id:string, index:number) => {
|
||||
if(!LuaSafeIds.has(id)){
|
||||
return
|
||||
}
|
||||
chat = get(CurrentChat)
|
||||
chat = getCurrentChat()
|
||||
chat.message.splice(index, 1)
|
||||
CurrentChat.set(chat)
|
||||
setCurrentChat(chat)
|
||||
})
|
||||
luaEngine.global.set('addChat', (id:string, role:string, value:string) => {
|
||||
if(!LuaSafeIds.has(id)){
|
||||
return
|
||||
}
|
||||
chat = get(CurrentChat)
|
||||
chat = getCurrentChat()
|
||||
let roleData:'user'|'char' = role === 'user' ? 'user' : 'char'
|
||||
chat.message.push({role: roleData, data: value})
|
||||
CurrentChat.set(chat)
|
||||
setCurrentChat(chat)
|
||||
})
|
||||
luaEngine.global.set('insertChat', (id:string, index:number, role:string, value:string) => {
|
||||
if(!LuaSafeIds.has(id)){
|
||||
return
|
||||
}
|
||||
chat = get(CurrentChat)
|
||||
chat = getCurrentChat()
|
||||
let roleData:'user'|'char' = role === 'user' ? 'user' : 'char'
|
||||
chat.message.splice(index, 0, {role: roleData, data: value})
|
||||
CurrentChat.set(chat)
|
||||
setCurrentChat(chat)
|
||||
})
|
||||
luaEngine.global.set('removeChat', (id:string, index:number) => {
|
||||
if(!LuaSafeIds.has(id)){
|
||||
return
|
||||
}
|
||||
chat = get(CurrentChat)
|
||||
chat = getCurrentChat()
|
||||
chat.message.splice(index, 1)
|
||||
CurrentChat.set(chat)
|
||||
setCurrentChat(chat)
|
||||
})
|
||||
luaEngine.global.set('getChatLength', (id:string) => {
|
||||
if(!LuaSafeIds.has(id)){
|
||||
return
|
||||
}
|
||||
chat = get(CurrentChat)
|
||||
chat = getCurrentChat()
|
||||
return chat.message.length
|
||||
})
|
||||
luaEngine.global.set('getFullChatMain', (id:string) => {
|
||||
chat = get(CurrentChat)
|
||||
chat = getCurrentChat()
|
||||
const data = JSON.stringify(chat.message.map((v) => {
|
||||
return {
|
||||
role: v.role,
|
||||
@@ -187,14 +187,14 @@ export async function runLua(code:string, arg:{
|
||||
if(!LuaSafeIds.has(id)){
|
||||
return
|
||||
}
|
||||
chat = get(CurrentChat)
|
||||
chat = getCurrentChat()
|
||||
chat.message = realValue.map((v) => {
|
||||
return {
|
||||
role: v.role,
|
||||
data: v.data
|
||||
}
|
||||
})
|
||||
CurrentChat.set(chat)
|
||||
setCurrentChat(chat)
|
||||
})
|
||||
|
||||
luaEngine.global.set('logMain', (value:string) => {
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
import { language } from "src/lang"
|
||||
import { alertConfirm, alertError, alertModuleSelect, alertNormal, alertStore } from "../alert"
|
||||
import { DataBase, setDatabase, type customscript, type loreBook, type triggerscript } from "../storage/database"
|
||||
import { DataBase, getCurrentCharacter, getCurrentChat, setCurrentCharacter, setDatabase, type customscript, type loreBook, type triggerscript } from "../storage/database"
|
||||
import { AppendableBuffer, downloadFile, isNodeServer, isTauri, readImage, saveAsset } from "../storage/globalApi"
|
||||
import { get } from "svelte/store"
|
||||
import { CurrentCharacter, CurrentChat } from "../stores"
|
||||
import { selectSingleFile, sleep } from "../util"
|
||||
import { v4 } from "uuid"
|
||||
import { convertExternalLorebook } from "./lorebook"
|
||||
import { encode } from "msgpackr"
|
||||
import { decodeRPack, encodeRPack } from "../rpack/rpack_bg"
|
||||
import { convertImage } from "../parser"
|
||||
import { Capacitor } from "@capacitor/core"
|
||||
@@ -274,7 +272,7 @@ function getModuleByIds(ids:string[]){
|
||||
let lastModules = ''
|
||||
let lastModuleData:RisuModule[] = []
|
||||
export function getModules(){
|
||||
const currentChat = get(CurrentChat)
|
||||
const currentChat = getCurrentChat()
|
||||
const db = get(DataBase)
|
||||
let ids = db.enabledModules ?? []
|
||||
if (currentChat){
|
||||
@@ -368,7 +366,7 @@ export async function applyModule() {
|
||||
return
|
||||
}
|
||||
|
||||
const currentChar = get(CurrentCharacter)
|
||||
const currentChar = getCurrentCharacter()
|
||||
if (!currentChar) {
|
||||
return
|
||||
}
|
||||
@@ -392,7 +390,7 @@ export async function applyModule() {
|
||||
}
|
||||
}
|
||||
|
||||
CurrentCharacter.set(currentChar)
|
||||
setCurrentCharacter(currentChar)
|
||||
|
||||
alertNormal(language.successApplyModule)
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
import { Template } from '@huggingface/jinja';
|
||||
import type { OpenAIChat } from '..';
|
||||
import { get } from 'svelte/store';
|
||||
import { DataBase } from 'src/ts/storage/database';
|
||||
import { CurrentCharacter } from 'src/ts/stores';
|
||||
import { DataBase, getCurrentCharacter } from 'src/ts/storage/database';
|
||||
import { getUserName } from 'src/ts/util';
|
||||
|
||||
export const chatTemplates = {
|
||||
@@ -31,7 +30,7 @@ export const applyChatTemplate = (messages:OpenAIChat[], arg:{
|
||||
custom?: string
|
||||
} = {}) => {
|
||||
const db = get(DataBase)
|
||||
const currentChar = get(CurrentCharacter)
|
||||
const currentChar = getCurrentCharacter()
|
||||
const type = arg.type ?? db.instructChatTemplate
|
||||
if(!type){
|
||||
throw new Error('Template type is not set')
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { parseChatML, risuChatParser } from "../parser";
|
||||
import { DataBase, type Chat, type character } from "../storage/database";
|
||||
import { DataBase, getCurrentCharacter, getCurrentChat, type Chat, type character } from "../storage/database";
|
||||
import { tokenize } from "../tokenizer";
|
||||
import { getModuleTriggers } from "./modules";
|
||||
import { get } from "svelte/store";
|
||||
import { CurrentCharacter, CurrentChat, ReloadGUIPointer, selectedCharID } from "../stores";
|
||||
import { ReloadGUIPointer, selectedCharID } from "../stores";
|
||||
import { processMultiCommand } from "./command";
|
||||
import { parseKeyValue } from "../util";
|
||||
import { alertError, alertInput, alertNormal, alertSelect } from "../alert";
|
||||
@@ -157,7 +157,7 @@ export async function runTrigger(char:character,mode:triggerMode, arg:{
|
||||
let stopSending = arg.stopSending ?? false
|
||||
const CharacterlowLevelAccess = char.lowLevelAccess ?? false
|
||||
let sendAIprompt = false
|
||||
const currentChat = get(CurrentChat)
|
||||
const currentChat = getCurrentChat()
|
||||
let additonalSysPrompt:additonalSysPrompt = arg.additonalSysPrompt ?? {
|
||||
start:'',
|
||||
historyend: '',
|
||||
@@ -190,7 +190,7 @@ export async function runTrigger(char:character,mode:triggerMode, arg:{
|
||||
|
||||
function setVar(key:string, value:string){
|
||||
const selectedCharId = get(selectedCharID)
|
||||
const currentCharacter = get(CurrentCharacter)
|
||||
const currentCharacter = getCurrentCharacter()
|
||||
const db = get(DataBase)
|
||||
varChanged = true
|
||||
chat.scriptstate ??= {}
|
||||
@@ -517,7 +517,7 @@ export async function runTrigger(char:character,mode:triggerMode, arg:{
|
||||
if(triggerCodeResult.stopSending){
|
||||
stopSending = true
|
||||
}
|
||||
chat = get(CurrentChat)
|
||||
chat = getCurrentChat()
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -535,7 +535,7 @@ export async function runTrigger(char:character,mode:triggerMode, arg:{
|
||||
caculatedTokens += await tokenize(additonalSysPrompt.promptend)
|
||||
}
|
||||
if(varChanged){
|
||||
const currentChat = get(CurrentChat)
|
||||
const currentChat = getCurrentChat()
|
||||
currentChat.scriptstate = chat.scriptstate
|
||||
ReloadGUIPointer.set(get(ReloadGUIPointer) + 1)
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { get } from "svelte/store";
|
||||
import { alertError } from "../alert";
|
||||
import { DataBase, type character } from "../storage/database";
|
||||
import { DataBase, getCurrentCharacter, type character } from "../storage/database";
|
||||
import { runTranslator, translateVox } from "../translator/translator";
|
||||
import { globalFetch, loadAsset } from "../storage/globalApi";
|
||||
import { language } from "src/lang";
|
||||
import { getCurrentCharacter, sleep } from "../util";
|
||||
import { sleep } from "../util";
|
||||
import { registerOnnxModel, runVITS } from "./transformers";
|
||||
|
||||
let sourceNode:AudioBufferSourceNode = null
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import { get } from "svelte/store";
|
||||
import { exportCharacterCard } from "./characterCards";
|
||||
import { VirtualWriter, isTauri, openURL } from "./storage/globalApi";
|
||||
import { sleep } from "./util";
|
||||
import { CurrentCharacter } from "./stores";
|
||||
import { DataBase, type character } from "./storage/database";
|
||||
import { DataBase, getCurrentCharacter, type character } from "./storage/database";
|
||||
import { alertStore } from "./alert";
|
||||
|
||||
let pong = false;
|
||||
@@ -17,7 +15,7 @@ window.addEventListener("message", (event) => {
|
||||
});
|
||||
|
||||
export async function shareRealmCardData():Promise<{ name: ArrayBuffer; data: ArrayBuffer; }> {
|
||||
const char = structuredClone(get(CurrentCharacter)) as character
|
||||
const char = structuredClone(getCurrentCharacter()) as character
|
||||
const trimedName = char.name.replace(/[^a-zA-Z0-9]/g, '') || 'character';
|
||||
const writer = new VirtualWriter()
|
||||
const namebuf = new TextEncoder().encode(trimedName + '.png')
|
||||
|
||||
@@ -450,6 +450,30 @@ export function setDatabase(data:Database){
|
||||
DataBase.set(data)
|
||||
}
|
||||
|
||||
export function getCurrentCharacter(){
|
||||
const db = get(DataBase)
|
||||
db.characters ??= []
|
||||
const char = db.characters?.[get(selectedCharID)]
|
||||
return char
|
||||
}
|
||||
|
||||
export function setCurrentCharacter(char:character|groupChat){
|
||||
const db = get(DataBase)
|
||||
db.characters ??= []
|
||||
db.characters[get(selectedCharID)] = char
|
||||
DataBase.set(db)
|
||||
}
|
||||
|
||||
export function getCurrentChat(){
|
||||
const char = getCurrentCharacter()
|
||||
return char?.chats[char.chatPage]
|
||||
}
|
||||
|
||||
export function setCurrentChat(chat:Chat){
|
||||
const char = getCurrentCharacter()
|
||||
char.chats[char.chatPage] = chat
|
||||
setCurrentCharacter(char)
|
||||
}
|
||||
|
||||
export interface Database{
|
||||
characters: (character|groupChat)[],
|
||||
@@ -1423,6 +1447,7 @@ 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';
|
||||
import { selectedCharID } from '../stores';
|
||||
|
||||
export async function downloadPreset(id:number, type:'json'|'risupreset'|'return' = 'json'){
|
||||
saveCurrentPreset()
|
||||
|
||||
140
src/ts/stores.ts
140
src/ts/stores.ts
@@ -1,5 +1,5 @@
|
||||
import { get, writable, type Writable } from "svelte/store";
|
||||
import { DataBase, type Chat, type character, type groupChat } from "./storage/database";
|
||||
import { DataBase, getCurrentCharacter, type Chat, type character, type groupChat } from "./storage/database";
|
||||
import { isEqual } from "lodash";
|
||||
import type { simpleCharacterArgument } from "./parser";
|
||||
import { getUserIcon, getUserIconProtrait, getUserName, sleep } from "./util";
|
||||
@@ -32,12 +32,8 @@ export const MobileGUI = writable(false)
|
||||
export const MobileGUIStack = writable(0)
|
||||
export const MobileSideBar = writable(0)
|
||||
//optimization
|
||||
export const CurrentCharacter = writable(null) as Writable<character | groupChat>
|
||||
export const CurrentSimpleCharacter = writable(null) as Writable<simpleCharacterArgument>
|
||||
export const CurrentChat = writable(null) as Writable<Chat>
|
||||
export const CurrentUsername = writable('') as Writable<string>
|
||||
export const CurrentUserIcon = writable('') as Writable<string>
|
||||
export const CurrentShowMemoryLimit = writable(false) as Writable<boolean>
|
||||
|
||||
export const ShowVN = writable(false)
|
||||
export const SettingsMenuIndex = writable(-1)
|
||||
export const ReloadGUIPointer = writable(0)
|
||||
@@ -72,7 +68,7 @@ CustomCSSStore.subscribe((css) => {
|
||||
}
|
||||
})
|
||||
|
||||
function createSimpleCharacter(char:character|groupChat){
|
||||
export function createSimpleCharacter(char:character|groupChat){
|
||||
if((!char) || char.type === 'group'){
|
||||
return null
|
||||
}
|
||||
@@ -94,14 +90,6 @@ function createSimpleCharacter(char:character|groupChat){
|
||||
function trySync(){
|
||||
try {
|
||||
let db = get(DataBase)
|
||||
let currentChar = get(selectedCharID)
|
||||
let currentCharacter = db.characters ? (db.characters[currentChar]) : null
|
||||
let currentChat = currentCharacter ? (currentCharacter.chats[currentCharacter.chatPage]) : null
|
||||
CurrentCharacter.set(structuredClone(currentCharacter))
|
||||
CurrentSimpleCharacter.set(createSimpleCharacter(currentCharacter))
|
||||
CurrentChat.set(structuredClone(currentChat))
|
||||
CurrentUsername.set(getUserName())
|
||||
CurrentUserIcon.set(getUserIcon())
|
||||
CurrentShowMemoryLimit.set(db.showMemoryLimit)
|
||||
} catch (error) {}
|
||||
}
|
||||
@@ -110,128 +98,6 @@ trySync()
|
||||
|
||||
async function preInit(){
|
||||
await sleep(1)
|
||||
trySync()
|
||||
function updateCurrentCharacter(){
|
||||
|
||||
const db = get(DataBase)
|
||||
if(!db.characters){
|
||||
CurrentCharacter.set(null)
|
||||
updateCurrentChat()
|
||||
return
|
||||
}
|
||||
|
||||
const currentCharId = get(selectedCharID)
|
||||
const currentChar = db.characters[currentCharId]
|
||||
const gotCharacter = get(CurrentCharacter)
|
||||
if(isEqual(gotCharacter, currentChar)){
|
||||
return
|
||||
}
|
||||
if((currentChar?.viewScreen === 'vn') !== get(ShowVN)){
|
||||
ShowVN.set(currentChar?.viewScreen === 'vn')
|
||||
}
|
||||
|
||||
CurrentCharacter.set(structuredClone(currentChar))
|
||||
const simp = createSimpleCharacter(currentChar)
|
||||
|
||||
if(!isEqual(get(CurrentSimpleCharacter), simp)){
|
||||
CurrentSimpleCharacter.set(simp)
|
||||
}
|
||||
|
||||
updateCurrentChat()
|
||||
}
|
||||
|
||||
function updateCurrentChat(){
|
||||
const currentChar = get(CurrentCharacter)
|
||||
if(!currentChar){
|
||||
CurrentChat.set(null)
|
||||
return
|
||||
}
|
||||
const chat = (currentChar.chats[currentChar.chatPage])
|
||||
const gotChat = get(CurrentChat)
|
||||
if(isEqual(gotChat, chat)){
|
||||
return
|
||||
}
|
||||
CurrentChat.set(structuredClone(chat))
|
||||
}
|
||||
|
||||
DataBase.subscribe((data) => {
|
||||
updateCurrentCharacter()
|
||||
if(getUserName() !== get(CurrentUsername)){
|
||||
CurrentUsername.set(getUserName())
|
||||
}
|
||||
if(getUserIcon() !== get(CurrentUserIcon)){
|
||||
CurrentUserIcon.set(getUserIcon())
|
||||
}
|
||||
if(getUserIconProtrait() !== get(UserIconProtrait)){
|
||||
UserIconProtrait.set(getUserIconProtrait())
|
||||
}
|
||||
if(data.showMemoryLimit !== get(CurrentShowMemoryLimit)){
|
||||
CurrentShowMemoryLimit.set(data.showMemoryLimit)
|
||||
}
|
||||
if(!isEqual(data.enabledModules, lastGlobalEnabledModules)){
|
||||
lastGlobalEnabledModules = data.enabledModules || []
|
||||
onModuleUpdate()
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
selectedCharID.subscribe((id) => {
|
||||
|
||||
updateCurrentCharacter()
|
||||
})
|
||||
|
||||
CurrentCharacter.subscribe((char) => {
|
||||
updateCurrentChat()
|
||||
let db = get(DataBase)
|
||||
let charId = get(selectedCharID)
|
||||
if(char?.hideChatIcon !== characterHideIcon){
|
||||
characterHideIcon = char?.hideChatIcon
|
||||
HideIconStore.set(characterHideIcon || moduleHideIcon)
|
||||
}
|
||||
if(getUserName() !== get(CurrentUsername)){
|
||||
CurrentUsername.set(getUserName())
|
||||
}
|
||||
if(getUserIcon() !== get(CurrentUserIcon)){
|
||||
CurrentUserIcon.set(getUserIcon())
|
||||
}
|
||||
if(getUserIconProtrait() !== get(UserIconProtrait)){
|
||||
UserIconProtrait.set(getUserIconProtrait())
|
||||
}
|
||||
if(charId === -1 || charId > db.characters.length){
|
||||
return
|
||||
}
|
||||
let cha = db.characters[charId]
|
||||
if(isEqual(cha, char)){
|
||||
return
|
||||
}
|
||||
db.characters[charId] = structuredClone(char)
|
||||
DataBase.set(db)
|
||||
})
|
||||
|
||||
CurrentChat.subscribe((chat) => {
|
||||
let currentChar = get(CurrentCharacter)
|
||||
|
||||
if(currentChar){
|
||||
if(!isEqual(currentChar.chats[currentChar.chatPage], chat)){
|
||||
currentChar.chats[currentChar.chatPage] = structuredClone(chat)
|
||||
CurrentCharacter.set(currentChar)
|
||||
}
|
||||
}
|
||||
|
||||
if(!isEqual(lastChatEnabledModules, chat?.modules)){
|
||||
lastChatEnabledModules = chat?.modules || []
|
||||
onModuleUpdate()
|
||||
return
|
||||
}
|
||||
|
||||
if(getUserName() !== get(CurrentUsername)){
|
||||
CurrentUsername.set(getUserName())
|
||||
}
|
||||
|
||||
if(getUserIcon() !== get(CurrentUserIcon)){
|
||||
CurrentUserIcon.set(getUserIcon())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function onModuleUpdate(){
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { v4 } from 'uuid';
|
||||
import { alertError, alertInput, alertNormal, alertStore, alertWait } from '../alert';
|
||||
import { get, writable } from 'svelte/store';
|
||||
import { DataBase, setDatabase, type character, saveImage, type Chat } from '../storage/database';
|
||||
import { CurrentChat, selectedCharID } from '../stores';
|
||||
import { DataBase, setDatabase, type character, saveImage, type Chat, getCurrentChat, setCurrentChat } from '../storage/database';
|
||||
import { selectedCharID } from '../stores';
|
||||
import { findCharacterIndexbyId, sleep } from '../util';
|
||||
import type { DataConnection, Peer } from 'peerjs';
|
||||
import { readImage } from '../storage/globalApi';
|
||||
@@ -365,7 +365,7 @@ export async function peerSync(){
|
||||
return
|
||||
}
|
||||
await sleep(1)
|
||||
const chat = get(CurrentChat)
|
||||
const chat = getCurrentChat()
|
||||
latestSyncChat = chat
|
||||
if(!conn){
|
||||
// host user
|
||||
@@ -426,5 +426,5 @@ export function peerRevertChat() {
|
||||
if(!connectionOpen || !latestSyncChat){
|
||||
return
|
||||
}
|
||||
CurrentChat.set(latestSyncChat)
|
||||
setCurrentChat(latestSyncChat)
|
||||
}
|
||||
@@ -1,13 +1,12 @@
|
||||
import type { Tiktoken } from "@dqbd/tiktoken";
|
||||
import type { Tokenizer } from "@mlc-ai/web-tokenizers";
|
||||
import { DataBase, type groupChat, type character, type Chat } from "./storage/database";
|
||||
import { DataBase, type groupChat, type character, type Chat, getCurrentCharacter } from "./storage/database";
|
||||
import { get } from "svelte/store";
|
||||
import type { MultiModal, OpenAIChat } from "./process";
|
||||
import { supportsInlayImage } from "./process/files/image";
|
||||
import { risuChatParser } from "./parser";
|
||||
import { tokenizeGGUFModel } from "./process/models/local";
|
||||
import { globalFetch } from "./storage/globalApi";
|
||||
import { CurrentCharacter } from "./stores";
|
||||
|
||||
|
||||
export const tokenizerList = [
|
||||
@@ -353,7 +352,7 @@ export async function getCharToken(char?:character|groupChat|null){
|
||||
let dynamic = 0
|
||||
|
||||
if(!char){
|
||||
const c = get(CurrentCharacter)
|
||||
const c = getCurrentCharacter()
|
||||
char = c
|
||||
}
|
||||
if(char.type === 'group'){
|
||||
|
||||
@@ -429,12 +429,6 @@ export async function decryptBuffer(data:Uint8Array, keys:string){
|
||||
return result
|
||||
}
|
||||
|
||||
export function getCurrentCharacter(){
|
||||
const db = get(DataBase)
|
||||
const selectedChar = get(selectedCharID)
|
||||
return db.characters[selectedChar]
|
||||
}
|
||||
|
||||
export function toState<T>(t:T):Writable<T>{
|
||||
return writable(t)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user