Remove lorebook.svelte.ts legacy lorebook handling

This commit is contained in:
kwaroran
2024-10-30 12:46:21 +09:00
parent a1632daca0
commit de7a6ca224

View File

@@ -39,171 +39,6 @@ export function addLorebook(type:number) {
}
}
interface formatedLore{
keys:string[]|'always'|{type:'regex',regex:string},
secondKey:string[]|{type:'regex',regex:string}
content: string
order: number
activatied: boolean
}
const rmRegex = / |\n/g
export async function loadLoreBookPrompt(){
const selectedID = get(selectedCharID)
const char = DBState.db.characters[selectedID]
const page = char.chatPage
const characterLore = char.globalLore ?? []
const chatLore = char.chats[page].localLore ?? []
const moduleLorebook = getModuleLorebooks()
const fullLore = characterLore.concat(chatLore).concat(moduleLorebook)
const currentChat = char.chats[page].message
const loreDepth = char.loreSettings?.scanDepth ?? DBState.db.loreBookDepth
const loreToken = char.loreSettings?.tokenBudget ?? DBState.db.loreBookToken
const fullWordMatching = char.loreSettings?.fullWordMatching ?? false
let activatiedPrompt: string[] = []
let formatedLore:formatedLore[] = []
for (const lore of fullLore){
if(lore){
if(lore.key.length > 1 || lore.alwaysActive){
if(!checkNullish(lore.activationPercent)){
let activationPercent = lore.activationPercent
if(isNaN(activationPercent) || !activationPercent || activationPercent < 0){
activationPercent = 0
}
if(activationPercent < (Math.random() * 100)){
continue
}
}
if(lore.key?.startsWith('@@@')){
lore.key = lore.key.replace('@@@','@@')
}
formatedLore.push({
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
})
}
}
}
formatedLore.sort((a, b) => {
return b.order - a.order
})
const formatedChatMain = currentChat.slice(currentChat.length - loreDepth,currentChat.length).map((msg) => {
return msg.data
}).join('||').replace(rmRegex,'').toLocaleLowerCase()
let loreListUpdated = true
while(loreListUpdated){
loreListUpdated = false
const formatedChat = formatedChatMain + activatiedPrompt.join('').replace(rmRegex,'').toLocaleLowerCase()
const formatedChatList = fullWordMatching ? formatedChat.split(' ') : formatedChat
for(let i=0;i<formatedLore.length;i++){
const lore = formatedLore[i]
if(lore.activatied){
continue
}
const totalTokens = await tokenize(activatiedPrompt.concat([lore.content]).join('\n\n'))
if(totalTokens > loreToken){
break
}
if(lore.keys === 'always'){
activatiedPrompt.push(lore.content)
lore.activatied = true
loreListUpdated = true
continue
}
let firstKeyActivation = false
if(Array.isArray(lore.keys)){
for(const key of lore.keys){
if(key){
if(formatedChatList.includes(key)){
firstKeyActivation = true
break
}
}
}
}
else{
if(formatedChat.match(new RegExp(lore.keys.regex,'g'))){
firstKeyActivation = true
}
}
if(firstKeyActivation){
if(Array.isArray(lore.secondKey)){
if(lore.secondKey.length === 0){
activatiedPrompt.push(lore.content)
lore.activatied = true
loreListUpdated = true
continue
}
for(const key of lore.secondKey){
if(formatedChatList.includes(key)){
activatiedPrompt.push(lore.content)
lore.activatied = true
loreListUpdated = true
break
}
}
}
else{
if(formatedChat.match(new RegExp(lore.secondKey.regex,'g'))){
firstKeyActivation = true
}
}
}
}
if(!(char.loreSettings?.recursiveScanning)){
break
}
}
let sactivated:string[] = []
let decoratedArray:{
depth:number,
pos:string,
prompt:string
}[] = []
activatiedPrompt = activatiedPrompt.filter((v) => {
if(v.startsWith("@@@end")){
sactivated.push(v.replace('@@@end','').trim())
return false
}
if(v.startsWith('@@end')){
sactivated.push(v.replace('@@end','').trim())
return false
}
return true
})
return {
act: activatiedPrompt.reverse().join('\n\n'),
special_act: sactivated.reverse().join('\n\n'),
decorated: decoratedArray
}
}
export async function loadLoreBookV3Prompt(){
const selectedID = get(selectedCharID)
const char = DBState.db.characters[selectedID]