From 9e3e5a327d88f40d5527375733a3d99e6a0f468c Mon Sep 17 00:00:00 2001 From: kwaroran Date: Tue, 25 Jul 2023 05:02:50 +0900 Subject: [PATCH] [feat] lorebook regex --- src/ts/process/lorebook.ts | 60 ++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/src/ts/process/lorebook.ts b/src/ts/process/lorebook.ts index 98747104..f15ecde5 100644 --- a/src/ts/process/lorebook.ts +++ b/src/ts/process/lorebook.ts @@ -51,8 +51,8 @@ export function addLorebook(type:number) { } interface formatedLore{ - keys:string[]|'always', - secondKey:string[] + keys:string[]|'always'|{type:'regex',regex:string}, + secondKey:string[]|{type:'regex',regex:string} content: string order: number activatied: boolean @@ -91,12 +91,14 @@ export async function loadLoreBookPrompt(){ } formatedLore.push({ - keys: lore.alwaysActive ? 'always' : (lore.key ?? '').replace(rmRegex, '').toLocaleLowerCase().split(',').filter((a) => { - return a.length > 1 - }), - secondKey: lore.selective ? (lore.secondkey ?? '').replace(rmRegex, '').toLocaleLowerCase().split(',').filter((a) => { - return a.length > 1 - }) : [], + keys: lore.alwaysActive ? 'always' : (lore.key?.startsWith("regex:")) ? ({type:'regex',regex:lore.key.replace('regex:','')}) : + (lore.key ?? '').replace(rmRegex, '').toLocaleLowerCase().split(',').filter((a) => { + return a.length > 1 + }), + secondKey: lore.selective ? ((lore.secondkey?.startsWith("regex:")) ? ({type:'regex',regex:lore.secondkey.replace('regex:','')}) : + (lore.secondkey ?? '').replace(rmRegex, '').toLocaleLowerCase().split(',').filter((a) => { + return a.length > 1 + })) : [], content: lore.content, order: lore.insertorder, activatied: false @@ -136,28 +138,42 @@ export async function loadLoreBookPrompt(){ } let firstKeyActivation = false - for(const key of lore.keys){ - if(key){ - if(formatedChat.includes(key)){ - firstKeyActivation = true - break + if(Array.isArray(lore.keys)){ + for(const key of lore.keys){ + if(key){ + if(formatedChat.includes(key)){ + firstKeyActivation = true + break + } } } } + else{ + if(formatedChat.match(new RegExp(lore.keys.regex,'g'))){ + firstKeyActivation = true + } + } if(firstKeyActivation){ - if(lore.secondKey.length === 0){ - activatiedPrompt.push(lore.content) - lore.activatied = true - loreListUpdated = true - continue - } - for(const key of lore.secondKey){ - if(formatedChat.includes(key)){ + if(Array.isArray(lore.secondKey)){ + if(lore.secondKey.length === 0){ activatiedPrompt.push(lore.content) lore.activatied = true loreListUpdated = true - break + continue + } + for(const key of lore.secondKey){ + if(formatedChat.includes(key)){ + activatiedPrompt.push(lore.content) + lore.activatied = true + loreListUpdated = true + break + } + } + } + else{ + if(formatedChat.match(new RegExp(lore.secondKey.regex,'g'))){ + firstKeyActivation = true } } }