Revert "refactor: regex caching" (#824)
# PR Checklist - [ ] Have you checked if it works normally in all models? *Ignore this if it doesn't use models.* - [ ] Have you checked if it works normally in all web, local, and node hosted versions? If it doesn't, have you blocked it in those versions? - [ ] Have you added type definitions? # Description
This commit is contained in:
@@ -68,29 +68,26 @@ export async function importRegex(o?:customscript[]):Promise<customscript[]>{
|
|||||||
let bestMatchCache = new Map<string, string>()
|
let bestMatchCache = new Map<string, string>()
|
||||||
let processScriptCache = new Map<string, string>()
|
let processScriptCache = new Map<string, string>()
|
||||||
|
|
||||||
function generateScriptCacheKey(scripts: customscript[], data: string, mode: ScriptMode, chatID = -1, cbsConditions: CbsConditions = {}) {
|
function cacheScript(scripts:customscript[], data:string, result:string, mode:ScriptMode){
|
||||||
let hash = data + '|||' + mode + '|||';
|
let hash = data + '|||' + mode + '|||'
|
||||||
for (const script of scripts) {
|
for(const script of scripts){
|
||||||
if(script.type !== mode){
|
hash += `${script.in}|||${script.out}|||${script.flag}|||${script.ableFlag}|||${script.type}`
|
||||||
continue
|
|
||||||
}
|
|
||||||
hash += `${script.flag?.includes('<cbs>') ?
|
|
||||||
risuChatParser(script.in, { chatID: chatID, cbsConditions }) :
|
|
||||||
script.in}|||${risuChatParser(script.out, { chatID: chatID, cbsConditions})}|||${script.flag ?? ''}|||${script.ableFlag ? 1 : 0}`;
|
|
||||||
}
|
}
|
||||||
return hash;
|
|
||||||
}
|
|
||||||
|
|
||||||
function cacheScript(hash:string, result:string){
|
|
||||||
processScriptCache.set(hash, result)
|
processScriptCache.set(hash, result)
|
||||||
|
|
||||||
if(processScriptCache.size > 1000){
|
if(processScriptCache.size > 500){
|
||||||
processScriptCache.delete(processScriptCache.keys().next().value)
|
processScriptCache.delete(processScriptCache.keys().next().value)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getScriptCache(hash:string){
|
function getScriptCache(scripts:customscript[], data:string, mode:ScriptMode){
|
||||||
|
let hash = data + '|||' + mode + '|||'
|
||||||
|
for(const script of scripts){
|
||||||
|
hash += `${script.in}|||${script.out}|||${script.flag}|||${script.ableFlag}|||${script.type}`
|
||||||
|
}
|
||||||
|
|
||||||
return processScriptCache.get(hash)
|
return processScriptCache.get(hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,7 +98,12 @@ export function resetScriptCache(){
|
|||||||
export async function processScriptFull(char:character|groupChat|simpleCharacterArgument, data:string, mode:ScriptMode, chatID = -1, cbsConditions:CbsConditions = {}){
|
export async function processScriptFull(char:character|groupChat|simpleCharacterArgument, data:string, mode:ScriptMode, chatID = -1, cbsConditions:CbsConditions = {}){
|
||||||
let db = getDatabase()
|
let db = getDatabase()
|
||||||
const originalData = data
|
const originalData = data
|
||||||
|
const cached = getScriptCache((db.presetRegex ?? []).concat(char.customscript), originalData, mode)
|
||||||
|
if(cached){
|
||||||
|
return {data: cached, emoChanged: false}
|
||||||
|
}
|
||||||
let emoChanged = false
|
let emoChanged = false
|
||||||
|
const scripts = (db.presetRegex ?? []).concat(char.customscript).concat(getModuleRegexScripts())
|
||||||
data = await runLuaEditTrigger(char, mode, data)
|
data = await runLuaEditTrigger(char, mode, data)
|
||||||
|
|
||||||
if(mode === 'editdisplay'){
|
if(mode === 'editdisplay'){
|
||||||
@@ -115,7 +117,7 @@ export async function processScriptFull(char:character|groupChat|simpleCharacter
|
|||||||
displayData: data
|
displayData: data
|
||||||
})
|
})
|
||||||
|
|
||||||
data = d?.displayData ?? data
|
data = d.displayData ?? data
|
||||||
console.log('Trigger time', performance.now() - perf)
|
console.log('Trigger time', performance.now() - perf)
|
||||||
}
|
}
|
||||||
catch(e){
|
catch(e){
|
||||||
@@ -132,26 +134,14 @@ export async function processScriptFull(char:character|groupChat|simpleCharacter
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
data = risuChatParser(data, { chatID: chatID, cbsConditions })
|
|
||||||
const scripts = (db.presetRegex ?? []).concat(char.customscript).concat(getModuleRegexScripts())
|
|
||||||
const hash = generateScriptCacheKey(scripts, data, mode, chatID, cbsConditions)
|
|
||||||
const cached = getScriptCache(hash)
|
|
||||||
if(cached){
|
|
||||||
return {data: cached, emoChanged: false}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(scripts.length === 0){
|
if(scripts.length === 0){
|
||||||
cacheScript(hash, data)
|
cacheScript(scripts, originalData, data, mode)
|
||||||
return {data, emoChanged}
|
return {data, emoChanged}
|
||||||
}
|
}
|
||||||
function executeScript(pscript:pScript){
|
function executeScript(pscript:pScript){
|
||||||
const script = pscript.script
|
const script = pscript.script
|
||||||
|
|
||||||
if(script.in === ''){
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if(script.type === mode){
|
if(script.type === mode){
|
||||||
|
|
||||||
let outScript2 = script.out.replaceAll("$n", "\n")
|
let outScript2 = script.out.replaceAll("$n", "\n")
|
||||||
@@ -347,7 +337,6 @@ export async function processScriptFull(char:character|groupChat|simpleCharacter
|
|||||||
|
|
||||||
if(db.dynamicAssets && (char.type === 'simple' || char.type === 'character') && char.additionalAssets && char.additionalAssets.length > 0){
|
if(db.dynamicAssets && (char.type === 'simple' || char.type === 'character') && char.additionalAssets && char.additionalAssets.length > 0){
|
||||||
if(!db.dynamicAssetsEditDisplay && mode === 'editdisplay'){
|
if(!db.dynamicAssetsEditDisplay && mode === 'editdisplay'){
|
||||||
cacheScript(hash, data)
|
|
||||||
return {data, emoChanged}
|
return {data, emoChanged}
|
||||||
}
|
}
|
||||||
const assetNames = char.additionalAssets.map((v) => v[0])
|
const assetNames = char.additionalAssets.map((v) => v[0])
|
||||||
@@ -383,7 +372,7 @@ export async function processScriptFull(char:character|groupChat|simpleCharacter
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheScript(hash, data)
|
cacheScript(scripts, originalData, data, mode)
|
||||||
|
|
||||||
return {data, emoChanged}
|
return {data, emoChanged}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user