Ignore recurse buffer for min activation steps

35b7fc3186417dc4b76b67faa51e9446863e9453

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +33 -11Ignore whitespace
public/scripts/world-info.js+33 -11
@@ -158,6 +158,11 @@ class WorldInfoBuffer {
158 #recurseBuffer = [];158 #recurseBuffer = [];
159159
160 /**160 /**
161 * @type {string[]} Array of strings added by prompt injections that are valid for the current scan
162 */
163 #injectBuffer = [];
164
165 /**
161 * @type {number} The skew of the global scan depth. Used in "min activations"166 * @type {number} The skew of the global scan depth. Used in "min activations"
162 */167 */
163 #skew = 0;168 #skew = 0;
@@ -206,9 +211,10 @@ class WorldInfoBuffer {
206 /**211 /**
207 * Gets all messages up to the given depth + recursion buffer.212 * Gets all messages up to the given depth + recursion buffer.
208 * @param {WIScanEntry} entry The entry that triggered the scan213 * @param {WIScanEntry} entry The entry that triggered the scan
214 * @param {number} scanState The state of the scan
209 * @returns {string} A slice of buffer until the given depth (inclusive)215 * @returns {string} A slice of buffer until the given depth (inclusive)
210 */216 */
211 get(entry) {217 get(entry, scanState) {
212 let depth = entry.scanDepth ?? this.getDepth();218 let depth = entry.scanDepth ?? this.getDepth();
213 if (depth <= this.#startDepth) {219 if (depth <= this.#startDepth) {
214 return '';220 return '';
@@ -226,7 +232,12 @@ class WorldInfoBuffer {
226232
227 let result = this.#depthBuffer.slice(this.#startDepth, depth).join('\n');233 let result = this.#depthBuffer.slice(this.#startDepth, depth).join('\n');
228234
229 if (this.#recurseBuffer.length > 0) {235 if (this.#injectBuffer.length > 0) {
236 result += '\n' + this.#injectBuffer.join('\n');
237 }
238
239 // Min activations should not include the recursion buffer
240 if (this.#recurseBuffer.length > 0 && scanState !== scan_state.MIN_ACTIVATIONS) {
230 result += '\n' + this.#recurseBuffer.join('\n');241 result += '\n' + this.#recurseBuffer.join('\n');
231 }242 }
232243
@@ -281,6 +292,14 @@ class WorldInfoBuffer {
281 }292 }
282293
283 /**294 /**
295 * Adds an injection to the buffer.
296 * @param {string} message The injection to add
297 */
298 addInject(message) {
299 this.#injectBuffer.push(message);
300 }
301
302 /**
284 * Increments skew and sets startDepth to previous depth.303 * Increments skew and sets startDepth to previous depth.
285 */304 */
286 advanceScanPosition() {305 advanceScanPosition() {
@@ -315,10 +334,11 @@ class WorldInfoBuffer {
315 /**334 /**
316 * Gets the match score for the given entry.335 * Gets the match score for the given entry.
317 * @param {WIScanEntry} entry Entry to check336 * @param {WIScanEntry} entry Entry to check
337 * @param {number} scanState The state of the scan
318 * @returns {number} The number of key activations for the given entry338 * @returns {number} The number of key activations for the given entry
319 */339 */
320 getScore(entry) {340 getScore(entry, scanState) {
321 const bufferState = this.get(entry);341 const bufferState = this.get(entry, scanState);
322 let numberOfPrimaryKeys = 0;342 let numberOfPrimaryKeys = 0;
323 let numberOfSecondaryKeys = 0;343 let numberOfSecondaryKeys = 0;
324 let primaryScore = 0;344 let primaryScore = 0;
@@ -3525,7 +3545,7 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
3525 if (context.extensionPrompts[key]?.scan) {3545 if (context.extensionPrompts[key]?.scan) {
3526 const prompt = getExtensionPromptByName(key);3546 const prompt = getExtensionPromptByName(key);
3527 if (prompt) {3547 if (prompt) {
3528 buffer.addRecurse(prompt);3548 buffer.addInject(prompt);
3529 }3549 }
3530 }3550 }
3531 }3551 }
@@ -3636,7 +3656,7 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
36363656
3637 primary: for (let key of entry.key) {3657 primary: for (let key of entry.key) {
3638 const substituted = substituteParams(key);3658 const substituted = substituteParams(key);
3639 const textToScan = buffer.get(entry);3659 const textToScan = buffer.get(entry, scanState);
36403660
3641 if (substituted && buffer.matchKeys(textToScan, substituted.trim(), entry)) {3661 if (substituted && buffer.matchKeys(textToScan, substituted.trim(), entry)) {
3642 console.debug(`WI UID ${entry.uid} found by primary match: ${substituted}.`);3662 console.debug(`WI UID ${entry.uid} found by primary match: ${substituted}.`);
@@ -3706,7 +3726,7 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
3706 const textToScanTokens = await getTokenCountAsync(allActivatedText);3726 const textToScanTokens = await getTokenCountAsync(allActivatedText);
3707 const probabilityChecksBefore = failedProbabilityChecks.size;3727 const probabilityChecksBefore = failedProbabilityChecks.size;
37083728
3709 filterByInclusionGroups(newEntries, allActivatedEntries, buffer);3729 filterByInclusionGroups(newEntries, allActivatedEntries, buffer, scanState);
37103730
3711 console.debug('-- PROBABILITY CHECKS BEGIN --');3731 console.debug('-- PROBABILITY CHECKS BEGIN --');
3712 for (const entry of newEntries) {3732 for (const entry of newEntries) {
@@ -3858,8 +3878,9 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
3858 * @param {Record<string, WIScanEntry[]>} groups The groups to filter3878 * @param {Record<string, WIScanEntry[]>} groups The groups to filter
3859 * @param {WorldInfoBuffer} buffer The buffer to use for scoring3879 * @param {WorldInfoBuffer} buffer The buffer to use for scoring
3860 * @param {(entry: WIScanEntry) => void} removeEntry The function to remove an entry3880 * @param {(entry: WIScanEntry) => void} removeEntry The function to remove an entry
3881 * @param {number} scanState The current scan state
3861 */3882 */
3862function filterGroupsByScoring(groups, buffer, removeEntry) {3883function filterGroupsByScoring(groups, buffer, removeEntry, scanState) {
3863 for (const [key, group] of Object.entries(groups)) {3884 for (const [key, group] of Object.entries(groups)) {
3864 // Group scoring is disabled both globally and for the group entries3885 // Group scoring is disabled both globally and for the group entries
3865 if (!world_info_use_group_scoring && !group.some(x => x.useGroupScoring)) {3886 if (!world_info_use_group_scoring && !group.some(x => x.useGroupScoring)) {
@@ -3867,7 +3888,7 @@ function filterGroupsByScoring(groups, buffer, removeEntry) {
3867 continue;3888 continue;
3868 }3889 }
38693890
3870 const scores = group.map(entry => buffer.getScore(entry));3891 const scores = group.map(entry => buffer.getScore(entry, scanState));
3871 const maxScore = Math.max(...scores);3892 const maxScore = Math.max(...scores);
3872 console.debug(`Group '${key}' max score: ${maxScore}`);3893 console.debug(`Group '${key}' max score: ${maxScore}`);
3873 //console.table(group.map((entry, i) => ({ uid: entry.uid, key: JSON.stringify(entry.key), score: scores[i] })));3894 //console.table(group.map((entry, i) => ({ uid: entry.uid, key: JSON.stringify(entry.key), score: scores[i] })));
@@ -3895,8 +3916,9 @@ function filterGroupsByScoring(groups, buffer, removeEntry) {
3895 * @param {object[]} newEntries Entries activated on current recursion level3916 * @param {object[]} newEntries Entries activated on current recursion level
3896 * @param {Set<object>} allActivatedEntries Set of all activated entries3917 * @param {Set<object>} allActivatedEntries Set of all activated entries
3897 * @param {WorldInfoBuffer} buffer The buffer to use for scanning3918 * @param {WorldInfoBuffer} buffer The buffer to use for scanning
3919 * @param {number} scanState The current scan state
3898 */3920 */
3899function filterByInclusionGroups(newEntries, allActivatedEntries, buffer) {3921function filterByInclusionGroups(newEntries, allActivatedEntries, buffer, scanState) {
3900 console.debug('-- INCLUSION GROUP CHECKS BEGIN --');3922 console.debug('-- INCLUSION GROUP CHECKS BEGIN --');
3901 const grouped = newEntries.filter(x => x.group).reduce((acc, item) => {3923 const grouped = newEntries.filter(x => x.group).reduce((acc, item) => {
3902 item.group.split(/,\s*/).filter(x => x).forEach(group => {3924 item.group.split(/,\s*/).filter(x => x).forEach(group => {
@@ -3925,7 +3947,7 @@ function filterByInclusionGroups(newEntries, allActivatedEntries, buffer) {
3925 }3947 }
3926 }3948 }
39273949
3928 filterGroupsByScoring(grouped, buffer, removeEntry);3950 filterGroupsByScoring(grouped, buffer, removeEntry, scanState);
39293951
3930 for (const [key, group] of Object.entries(grouped)) {3952 for (const [key, group] of Object.entries(grouped)) {
3931 console.debug(`Checking inclusion group '${key}' with ${group.length} entries`, group);3953 console.debug(`Checking inclusion group '${key}' with ${group.length} entries`, group);