Improve performance

This commit is contained in:
kwaroran
2024-11-03 23:16:51 +09:00
parent b51bbad838
commit c22f6b789a
5 changed files with 71 additions and 35 deletions

View File

@@ -6,7 +6,7 @@
import { language } from "../../lang";
import { type MessageGenerationInfo } from "../../ts/storage/database.svelte";
import { DBState } from 'src/ts/stores.svelte';
import { HideIconStore, ReloadGUIPointer, selectedCharID } from "../../ts/stores.svelte";
import { HideIconStore, ReloadGUIPointer, selIdState } from "../../ts/stores.svelte";
import { translateHTML } from "../../ts/translator/translator";
import { risuChatParser } from "src/ts/process/scripts";
import { get, type Unsubscriber } from "svelte/store";
@@ -18,6 +18,7 @@
import { ColorSchemeTypeStore } from "src/ts/gui/colorscheme";
import { ConnectionOpenStore } from "src/ts/sync/multiuser";
import { onDestroy, onMount } from "svelte";
import { PerformanceDebugger } from "src/ts/globalApi.svelte";
let translating = $state(false)
let editMode = $state(false)
let statusMessage:string = $state('')
@@ -55,12 +56,12 @@
let msgDisplay = $state('')
let translated = $state(DBState.db.autoTranslate)
let role = $derived(DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage].message[idx]?.role)
let role = $derived(DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage].message[idx]?.role)
async function rm(e:MouseEvent, rec?:boolean){
if(e.shiftKey){
let msg = DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage].message
let msg = DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage].message
msg = msg.slice(0, idx)
DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage].message = msg
DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage].message = msg
return
}
@@ -68,32 +69,32 @@
if(rm){
if(DBState.db.instantRemove || rec){
const r = await alertConfirm(language.instantRemoveConfirm)
let msg = DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage].message
let msg = DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage].message
if(!r){
msg = msg.slice(0, idx)
}
else{
msg.splice(idx, 1)
}
DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage].message = msg
DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage].message = msg
}
else{
let msg = DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage].message
let msg = DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage].message
msg.splice(idx, 1)
DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage].message = msg
DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage].message = msg
}
}
}
async function edit(){
DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage].message[idx].data = message
DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage].message[idx].data = message
}
function getCbsCondition(){
try{
const cbsConditions:CbsConditions = {
firstmsg: firstMessage ?? false,
chatRole: DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage]?.message?.[idx]?.role ?? null,
chatRole: DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage]?.message?.[idx]?.role ?? null,
}
return cbsConditions
}
@@ -106,6 +107,7 @@
}
function displaya(message:string){
const perf = performance.now()
msgDisplay = risuChatParser(message, {chara: name, chatID: idx, rmVar: true, visualize: true, cbsConditions: getCbsCondition()})
}
@@ -135,7 +137,6 @@
lastChatId = chatID
translateText = false
try {
console.log('Checking autoTranslate')
if(DBState.db.autoTranslate){
translateText = true
}
@@ -147,7 +148,6 @@
console.error(error)
}
}
console.log(`Translattext: ${translateText}, autoTranslate: ${DBState.db.autoTranslate}`)
if(translateText){
if(!DBState.db.legacyTranslation){
const marked = await ParseMarkdown(data, charArg, 'pretranslate', chatID, getCbsCondition())
@@ -284,7 +284,7 @@
</button>
{/if}
{#if idx > -1}
{#if DBState.db.characters[$selectedCharID].type !== 'group' && DBState.db.characters[$selectedCharID].ttsMode !== 'none' && (DBState.db.characters[$selectedCharID].ttsMode)}
{#if DBState.db.characters[selIdState.selId].type !== 'group' && DBState.db.characters[selIdState.selId].ttsMode !== 'none' && (DBState.db.characters[selIdState.selId].ttsMode)}
<button class="ml-2 hover:text-blue-500 transition-colors" onclick={()=>{
return sayTTS(null, message)
}}>
@@ -335,7 +335,7 @@
{#snippet icon(options:{rounded?:boolean,styleFix?:string} = {})}
{#if !blankMessage && !$HideIconStore}
{#if DBState.db.characters[$selectedCharID]?.chaId === "§playground"}
{#if DBState.db.characters[selIdState.selId]?.chaId === "§playground"}
<div class="shadow-lg border-textcolor2 border flex justify-center items-center text-textcolor2" style={options?.styleFix ?? `height:${DBState.db.iconsize * 3.5 / 100}rem;width:${DBState.db.iconsize * 3.5 / 100}rem;min-width:${DBState.db.iconsize * 3.5 / 100}rem`}
class:rounded-md={options?.rounded} class:rounded-full={options?.rounded}>
{#if name === 'assistant'}
@@ -516,7 +516,7 @@
class:rounded-tr-none={role === 'user'}
>
<p class="text-gray-800">{@render textBox()}</p>
{#if DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage].message[idx]?.time}
{#if DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage].message[idx]?.time}
<span class="text-xs text-textcolor2 mt-1 block">
{new Intl.DateTimeFormat(undefined, {
hour: '2-digit',
@@ -525,7 +525,7 @@
month: '2-digit',
day: '2-digit',
hour12: false
}).format(DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage].message[idx].time)}
}).format(DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage].message[idx].time)}
</span>
{/if}
</div>
@@ -563,12 +563,12 @@
{@render icon({rounded: DBState.db.roundIcons})}
<span class="flex flex-col ml-4 w-full max-w-full min-w-0 text-black">
<div class="flexium items-center chat-width">
{#if DBState.db.characters[$selectedCharID]?.chaId === "§playground" && !blankMessage}
{#if DBState.db.characters[selIdState.selId]?.chaId === "§playground" && !blankMessage}
<span class="chat-width text-xl border-darkborderc flex items-center">
<span>{name === 'assistant' ? 'Assistant' : 'User'}</span>
<button class="ml-2 text-textcolor2 hover:text-textcolor" onclick={() => {
DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage].message[idx].role = DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage].message[idx].role === 'char' ? 'user' : 'char'
DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage] = DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage]
DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage].message[idx].role = DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage].message[idx].role === 'char' ? 'user' : 'char'
DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage] = DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage]
}}><ArrowLeftRightIcon size="18" /></button>
</span>
{:else if !blankMessage && !$HideIconStore}

View File

@@ -245,12 +245,9 @@ export async function runCharacterJS(arg:{
mode: ScriptMode|'onButtonClick'|'modifyRequestChat'
data: any
}):Promise<any>{
const perf = performance.now()
try {
if(arg.code === null){
const db = getDatabase()
const selectedChar = get(selectedCharID)
arg.code = db.characters[selectedChar].virtualscript
}
arg.code = arg.code ?? ''
const codes = {
"editinput": 'editInput',
"editoutput": 'editOutput',
@@ -320,5 +317,8 @@ export async function runCharacterJS(arg:{
}
return arg.data
}
finally{
console.log('runCharacterJS',performance.now() - perf)
}
}

View File

@@ -8,7 +8,8 @@ import { convertExternalLorebook } from "./lorebook.svelte"
import { decodeRPack, encodeRPack } from "../rpack/rpack_bg"
import { convertImage } from "../parser.svelte"
import { Capacitor } from "@capacitor/core"
import { DBState, HideIconStore, moduleBackgroundEmbedding } from "../stores.svelte"
import { HideIconStore, moduleBackgroundEmbedding, ReloadGUIPointer } from "../stores.svelte"
import {get} from "svelte/store"
export interface RisuModule{
name: string
@@ -395,19 +396,19 @@ export async function applyModule() {
alertNormal(language.successApplyModule)
}
let lastGlobalEnabledModules: string[] = []
let lastChatEnabledModules: string[] = []
let lastModuleIds:string = ''
export function moduleUpdate(){
if(!Array.isArray(lastGlobalEnabledModules)){
lastGlobalEnabledModules = []
}
if(!Array.isArray(lastChatEnabledModules)){
lastChatEnabledModules = []
}
const m = getModules()
const ids = m.map((m) => m.id).join('-')
if(lastModuleIds !== ids){
ReloadGUIPointer.set(get(ReloadGUIPointer) + 1)
lastModuleIds = ids
}
let moduleHideIcon = false
let backgroundEmbedding = ''
m.forEach((module) => {

View File

@@ -65,9 +65,38 @@ export async function importRegex(o?:customscript[]):Promise<customscript[]>{
}
let bestMatchCache = new Map<string, string>()
let processScriptCache = new Map<string, string>()
function cacheScript(scripts:customscript[], data:string, result:string){
let hash = data + '|||'
for(const script of scripts){
hash += `${script.in}|||${script.out}|||${script.flag}|||${script.ableFlag}|||${script.type}`
}
processScriptCache.set(hash, result)
}
function getScriptCache(scripts:customscript[], data:string){
let hash = data + '|||'
for(const script of scripts){
hash += `${script.in}|||${script.out}|||${script.flag}|||${script.ableFlag}|||${script.type}`
}
return processScriptCache.get(hash)
}
export function resetScriptCache(){
processScriptCache = new Map()
}
export async function processScriptFull(char:character|groupChat|simpleCharacterArgument, data:string, mode:ScriptMode, chatID = -1, cbsConditions:CbsConditions = {}){
let db = getDatabase()
const originalData = data
const cached = getScriptCache((db.globalscript ?? []).concat(char.customscript), originalData)
if(cached){
return {data: cached, emoChanged: false}
}
let emoChanged = false
const scripts = (db.globalscript ?? []).concat(char.customscript).concat(getModuleRegexScripts())
data = await runCharacterJS({
@@ -77,6 +106,7 @@ export async function processScriptFull(char:character|groupChat|simpleCharacter
})
data = await runLuaEditTrigger(char, mode, data)
if(scripts.length === 0){
cacheScript(scripts, originalData, data)
return {data, emoChanged}
}
function executeScript(pscript:pScript){
@@ -311,6 +341,8 @@ export async function processScriptFull(char:character|groupChat|simpleCharacter
}
}
cacheScript(scripts, originalData, data)
return {data, emoChanged}
}

View File

@@ -3,6 +3,7 @@ import type { character, Database, groupChat } from "./storage/database.svelte";
import type { simpleCharacterArgument } from "./parser.svelte";
import type { alertData } from "./alert";
import { getModules, moduleUpdate } from "./process/modules";
import { resetScriptCache } from "./process/scripts";
function updateSize(){
SizeStore.set({
@@ -95,6 +96,10 @@ export const DBState = $state({
export const disableHighlight = writable(true)
ReloadGUIPointer.subscribe(() => {
resetScriptCache()
})
$effect.root(() => {
selectedCharID.subscribe((v) => {
selIdState.selId = v
@@ -106,8 +111,6 @@ $effect.root(() => {
DBState?.db?.characters?.[selIdState.selId]?.chats?.[DBState?.db?.characters?.[selIdState.selId]?.chatPage]?.modules?.length
DBState?.db?.characters?.[selIdState.selId]?.hideChatIcon
DBState?.db?.moduleIntergration
ReloadGUIPointer.set(get(ReloadGUIPointer) + 1)
moduleUpdate()
})
})