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 {
158158 #recurseBuffer = [];
159159
160160 /**
161+ * @type {string[]} Array of strings added by prompt injections that are valid for the current scan
162+ */
163+ #injectBuffer = [];
164+
165+ /**
161166 * @type {number} The skew of the global scan depth. Used in "min activations"
162167 */
163168 #skew = 0;
@@ -206,9 +211,10 @@ class WorldInfoBuffer {
206211 /**
207212 * Gets all messages up to the given depth + recursion buffer.
208213 * @param {WIScanEntry} entry The entry that triggered the scan
214+ * @param {number} scanState The state of the scan
209215 * @returns {string} A slice of buffer until the given depth (inclusive)
210216 */
211217 get(entry, scanState) {
212218 let depth = entry.scanDepth ?? this.getDepth();
213219 if (depth <= this.#startDepth) {
214220 return '';
@@ -226,7 +232,12 @@ class WorldInfoBuffer {
226232
227233 let result = this.#depthBuffer.slice(this.#startDepth, depth).join('\n');
228234
229235 if (this.#recurseBufferinjectBuffer.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) {
230241 result += '\n' + this.#recurseBuffer.join('\n');
231242 }
232243
@@ -281,6 +292,14 @@ class WorldInfoBuffer {
281292 }
282293
283294 /**
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+ /**
284303 * Increments skew and sets startDepth to previous depth.
285304 */
286305 advanceScanPosition() {
@@ -315,10 +334,11 @@ class WorldInfoBuffer {
315334 /**
316335 * Gets the match score for the given entry.
317336 * @param {WIScanEntry} entry Entry to check
337+ * @param {number} scanState The state of the scan
318338 * @returns {number} The number of key activations for the given entry
319339 */
320340 getScore(entry, scanState) {
321341 const bufferState = this.get(entry, scanState);
322342 let numberOfPrimaryKeys = 0;
323343 let numberOfSecondaryKeys = 0;
324344 let primaryScore = 0;
@@ -3525,7 +3545,7 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
35253545 if (context.extensionPrompts[key]?.scan) {
35263546 const prompt = getExtensionPromptByName(key);
35273547 if (prompt) {
35283548 buffer.addRecurseaddInject(prompt);
35293549 }
35303550 }
35313551 }
@@ -3636,7 +3656,7 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
36363656
36373657 primary: for (let key of entry.key) {
36383658 const substituted = substituteParams(key);
36393659 const textToScan = buffer.get(entry, scanState);
36403660
36413661 if (substituted && buffer.matchKeys(textToScan, substituted.trim(), entry)) {
36423662 console.debug(`WI UID ${entry.uid} found by primary match: ${substituted}.`);
@@ -3706,7 +3726,7 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
37063726 const textToScanTokens = await getTokenCountAsync(allActivatedText);
37073727 const probabilityChecksBefore = failedProbabilityChecks.size;
37083728
37093729 filterByInclusionGroups(newEntries, allActivatedEntries, buffer, scanState);
37103730
37113731 console.debug('-- PROBABILITY CHECKS BEGIN --');
37123732 for (const entry of newEntries) {
@@ -3858,8 +3878,9 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
38583878 * @param {Record<string, WIScanEntry[]>} groups The groups to filter
38593879 * @param {WorldInfoBuffer} buffer The buffer to use for scoring
38603880 * @param {(entry: WIScanEntry) => void} removeEntry The function to remove an entry
3881+ * @param {number} scanState The current scan state
38613882 */
38623883function filterGroupsByScoring(groups, buffer, removeEntry, scanState) {
38633884 for (const [key, group] of Object.entries(groups)) {
38643885 // Group scoring is disabled both globally and for the group entries
38653886 if (!world_info_use_group_scoring && !group.some(x => x.useGroupScoring)) {
@@ -3867,7 +3888,7 @@ function filterGroupsByScoring(groups, buffer, removeEntry) {
38673888 continue;
38683889 }
38693890
38703891 const scores = group.map(entry => buffer.getScore(entry, scanState));
38713892 const maxScore = Math.max(...scores);
38723893 console.debug(`Group '${key}' max score: ${maxScore}`);
38733894 //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) {
38953916 * @param {object[]} newEntries Entries activated on current recursion level
38963917 * @param {Set<object>} allActivatedEntries Set of all activated entries
38973918 * @param {WorldInfoBuffer} buffer The buffer to use for scanning
3919+ * @param {number} scanState The current scan state
38983920 */
38993921function filterByInclusionGroups(newEntries, allActivatedEntries, buffer, scanState) {
39003922 console.debug('-- INCLUSION GROUP CHECKS BEGIN --');
39013923 const grouped = newEntries.filter(x => x.group).reduce((acc, item) => {
39023924 item.group.split(/,\s*/).filter(x => x).forEach(group => {
@@ -3925,7 +3947,7 @@ function filterByInclusionGroups(newEntries, allActivatedEntries, buffer) {
39253947 }
39263948 }
39273949
39283950 filterGroupsByScoring(grouped, buffer, removeEntry, scanState);
39293951
39303952 for (const [key, group] of Object.entries(grouped)) {
39313953 console.debug(`Checking inclusion group '${key}' with ${group.length} entries`, group);