mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Remove double-loop nesting of WI key processing
This commit is contained in:
@@ -3693,76 +3693,85 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If selectiveLogic isn't found, assume it's AND, only do this once per entry
|
|
||||||
const selectiveLogic = entry.selectiveLogic ?? 0;
|
|
||||||
|
|
||||||
// Cache the text to scan before the loop, it won't change its content
|
// Cache the text to scan before the loop, it won't change its content
|
||||||
const textToScan = buffer.get(entry, scanState);
|
const textToScan = buffer.get(entry, scanState);
|
||||||
|
|
||||||
primary: for (let key of entry.key) {
|
// PRIMARY KEYWORDS
|
||||||
|
let primaryKeyMatch = entry.key.find(key => {
|
||||||
const substituted = substituteParams(key);
|
const substituted = substituteParams(key);
|
||||||
|
return substituted && buffer.matchKeys(textToScan, substituted.trim(), entry);
|
||||||
|
});
|
||||||
|
|
||||||
if (substituted && buffer.matchKeys(textToScan, substituted.trim(), entry)) {
|
if (!primaryKeyMatch) {
|
||||||
log('has match on primary keyword', substituted);
|
// Don't write logs for simple no-matches
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
//selective logic begins
|
const hasSecondaryKeywords = (
|
||||||
if (
|
|
||||||
entry.selective && //all entries are selective now
|
entry.selective && //all entries are selective now
|
||||||
Array.isArray(entry.keysecondary) && //always true
|
Array.isArray(entry.keysecondary) && //always true
|
||||||
entry.keysecondary.length //ignore empties
|
entry.keysecondary.length //ignore empties
|
||||||
) {
|
);
|
||||||
log('has secondary keywords. Checking logic', Object.entries(world_info_logic).find(x => x[1] === entry.selectiveLogic));
|
|
||||||
|
if (!hasSecondaryKeywords) {
|
||||||
|
// Handle cases where secondary is empty
|
||||||
|
log('activated by primary key match', primaryKeyMatch);
|
||||||
|
activatedNow.add(entry);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// SECONDARY KEYWORDS
|
||||||
|
const selectiveLogic = entry.selectiveLogic ?? 0; // If selectiveLogic isn't found, assume it's AND, only do this once per entry
|
||||||
|
log('Entry with primary key match', primaryKeyMatch, 'has secondary keywords. Checking with logic logic', Object.entries(world_info_logic).find(x => x[1] === entry.selectiveLogic));
|
||||||
|
|
||||||
|
/** @type {() => boolean} */
|
||||||
|
function matchSecondaryKeys() {
|
||||||
let hasAnyMatch = false;
|
let hasAnyMatch = false;
|
||||||
let hasAllMatch = true;
|
let hasAllMatch = true;
|
||||||
secondary: for (let keysecondary of entry.keysecondary) {
|
for (let keysecondary of entry.keysecondary) {
|
||||||
const secondarySubstituted = substituteParams(keysecondary);
|
const secondarySubstituted = substituteParams(keysecondary);
|
||||||
const hasSecondaryMatch = secondarySubstituted && buffer.matchKeys(textToScan, secondarySubstituted.trim(), entry);
|
const hasSecondaryMatch = secondarySubstituted && buffer.matchKeys(textToScan, secondarySubstituted.trim(), entry);
|
||||||
|
|
||||||
if (hasSecondaryMatch) {
|
if (hasSecondaryMatch) hasAnyMatch = true;
|
||||||
hasAnyMatch = true;
|
if (!hasSecondaryMatch) hasAllMatch = false;
|
||||||
}
|
|
||||||
|
|
||||||
if (!hasSecondaryMatch) {
|
|
||||||
hasAllMatch = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Simplified AND ANY / NOT ALL if statement. (Proper fix for PR#1356 by Bronya)
|
// Simplified AND ANY / NOT ALL if statement. (Proper fix for PR#1356 by Bronya)
|
||||||
// If AND ANY logic and the main checks pass OR if NOT ALL logic and the main checks do not pass
|
// If AND ANY logic and the main checks pass OR if NOT ALL logic and the main checks do not pass
|
||||||
if ((selectiveLogic === world_info_logic.AND_ANY && hasSecondaryMatch) || (selectiveLogic === world_info_logic.NOT_ALL && !hasSecondaryMatch)) {
|
if (selectiveLogic === world_info_logic.AND_ANY && hasSecondaryMatch) {
|
||||||
if (selectiveLogic === world_info_logic.AND_ANY) {
|
|
||||||
log('activated. (AND ANY) Found match secondary keyword', secondarySubstituted);
|
log('activated. (AND ANY) Found match secondary keyword', secondarySubstituted);
|
||||||
} else {
|
return true;
|
||||||
log('activated. (NOT ALL) Found not matching secondary keyword', secondarySubstituted);
|
|
||||||
}
|
}
|
||||||
activatedNow.add(entry);
|
if (selectiveLogic === world_info_logic.NOT_ALL && !hasSecondaryMatch) {
|
||||||
break secondary;
|
log('activated. (NOT ALL) Found not matching secondary keyword', secondarySubstituted);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle NOT ANY logic
|
// Handle NOT ANY logic
|
||||||
if (selectiveLogic === world_info_logic.NOT_ANY && !hasAnyMatch) {
|
if (selectiveLogic === world_info_logic.NOT_ANY && !hasAnyMatch) {
|
||||||
log('activated. (NOT ANY) No secondary keywords found', entry.keysecondary);
|
log('activated. (NOT ANY) No secondary keywords found', entry.keysecondary);
|
||||||
activatedNow.add(entry);
|
return true;
|
||||||
break primary;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle AND ALL logic
|
// Handle AND ALL logic
|
||||||
if (selectiveLogic === world_info_logic.AND_ALL && hasAllMatch) {
|
if (selectiveLogic === world_info_logic.AND_ALL && hasAllMatch) {
|
||||||
log('activated. (AND ALL) All secondary keywords found', entry.keysecondary);
|
log('activated. (AND ALL) All secondary keywords found', entry.keysecondary);
|
||||||
activatedNow.add(entry);
|
return true;
|
||||||
break primary;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const matched = matchSecondaryKeys();
|
||||||
|
if (!matched) {
|
||||||
log('skipped. Secondary keywords not satisfied', entry.keysecondary);
|
log('skipped. Secondary keywords not satisfied', entry.keysecondary);
|
||||||
break primary;
|
continue;
|
||||||
} else {
|
}
|
||||||
// Handle cases where secondary is empty
|
|
||||||
log('activated by primary keyword', substituted);
|
// Success logging was already done inside the function, so just add the entry
|
||||||
activatedNow.add(entry);
|
activatedNow.add(entry);
|
||||||
break primary;
|
continue;
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.debug(`[WI] Search done. Found ${activatedNow.size} possible entries.`);
|
console.debug(`[WI] Search done. Found ${activatedNow.size} possible entries.`);
|
||||||
|
Reference in New Issue
Block a user