feat: add lorebook regex matching

This commit is contained in:
kwaroran
2024-06-03 17:28:00 +09:00
parent a39fbee4ff
commit 9583142ee7

View File

@@ -236,21 +236,22 @@ export async function loadLoreBookV3Prompt(){
return msg.data return msg.data
}).join('||') }).join('||')
if(arg.regex){ if(arg.regex){
const regexString = arg.keys[0] for(const regexString of arg.keys){
if(!regexString.startsWith('/')){ if(!regexString.startsWith('/')){
return false
}
const regexFlag = regexString.split('/').pop()
if(regexFlag){
arg.keys[0] = regexString.replace('/'+regexFlag,'')
try {
const regex = new RegExp(arg.keys[0],regexFlag)
return regex.test(mText)
} catch (error) {
return false return false
} }
const regexFlag = regexString.split('/').pop()
if(regexFlag){
arg.keys[0] = regexString.replace('/'+regexFlag,'')
try {
const regex = new RegExp(arg.keys[0],regexFlag)
return regex.test(mText)
} catch (error) {
return false
}
}
return false
} }
return false
} }
mText = mText.toLocaleLowerCase() mText = mText.toLocaleLowerCase()
@@ -433,18 +434,6 @@ export async function loadLoreBookV3Prompt(){
if(!activated || forceState !== 'none' || fullLore[i].alwaysActive){ if(!activated || forceState !== 'none' || fullLore[i].alwaysActive){
//if the lore is not activated or force activated, skip the search //if the lore is not activated or force activated, skip the search
} }
else if(fullLore[i].useRegex){
const match = searchMatch(currentChat, {
keys: [fullLore[i].key],
searchDepth: scanDepth,
regex: true,
fullWordMatching: fullWordMatching
})
if(!match){
activated = false
}
}
else{ else{
searchQueries.push({ searchQueries.push({
keys: fullLore[i].key.split(','), keys: fullLore[i].key.split(','),
@@ -455,7 +444,7 @@ export async function loadLoreBookV3Prompt(){
const result = searchMatch(currentChat, { const result = searchMatch(currentChat, {
keys: query.keys, keys: query.keys,
searchDepth: scanDepth, searchDepth: scanDepth,
regex: false, regex: fullLore[i].useRegex,
fullWordMatching: fullWordMatching fullWordMatching: fullWordMatching
}) })
if(query.negative){ if(query.negative){