Ignore recurse buffer for min activation steps
| @@ -158,6 +158,11 @@ class WorldInfoBuffer { | ||
| 158 | 158 | #recurseBuffer = []; |
| 159 | 159 | |
| 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 | 166 | * @type {number} The skew of the global scan depth. Used in "min activations" |
| 162 | 167 | */ |
| 163 | 168 | #skew = 0; |
| @@ -206,9 +211,10 @@ class WorldInfoBuffer { | ||
| 206 | 211 | /** |
| 207 | 212 | * Gets all messages up to the given depth + recursion buffer. |
| 208 | 213 | * @param {WIScanEntry} entry The entry that triggered the scan |
| 214 | + * @param {number} scanState The state of the scan | |
| 209 | 215 | * @returns {string} A slice of buffer until the given depth (inclusive) |
| 210 | 216 | */ |
| 211 | 217 | get(entry, scanState) { |
| 212 | 218 | let depth = entry.scanDepth ?? this.getDepth(); |
| 213 | 219 | if (depth <= this.#startDepth) { |
| 214 | 220 | return ''; |
| @@ -226,7 +232,12 @@ class WorldInfoBuffer { | ||
| 226 | 232 | |
| 227 | 233 | let result = this.#depthBuffer.slice(this.#startDepth, depth).join('\n'); |
| 228 | 234 | |
| 229 | 235 | 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) { | |
| 230 | 241 | result += '\n' + this.#recurseBuffer.join('\n'); |
| 231 | 242 | } |
| 232 | 243 | |
| @@ -281,6 +292,14 @@ class WorldInfoBuffer { | ||
| 281 | 292 | } |
| 282 | 293 | |
| 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 | 303 | * Increments skew and sets startDepth to previous depth. |
| 285 | 304 | */ |
| 286 | 305 | advanceScanPosition() { |
| @@ -315,10 +334,11 @@ class WorldInfoBuffer { | ||
| 315 | 334 | /** |
| 316 | 335 | * Gets the match score for the given entry. |
| 317 | 336 | * @param {WIScanEntry} entry Entry to check |
| 337 | + * @param {number} scanState The state of the scan | |
| 318 | 338 | * @returns {number} The number of key activations for the given entry |
| 319 | 339 | */ |
| 320 | 340 | getScore(entry, scanState) { |
| 321 | 341 | const bufferState = this.get(entry, scanState); |
| 322 | 342 | let numberOfPrimaryKeys = 0; |
| 323 | 343 | let numberOfSecondaryKeys = 0; |
| 324 | 344 | let primaryScore = 0; |
| @@ -3525,7 +3545,7 @@ async function checkWorldInfo(chat, maxContext, isDryRun) { | ||
| 3525 | 3545 | if (context.extensionPrompts[key]?.scan) { |
| 3526 | 3546 | const prompt = getExtensionPromptByName(key); |
| 3527 | 3547 | if (prompt) { |
| 3528 | 3548 | buffer.addRecurseaddInject(prompt); |
| 3529 | 3549 | } |
| 3530 | 3550 | } |
| 3531 | 3551 | } |
| @@ -3636,7 +3656,7 @@ async function checkWorldInfo(chat, maxContext, isDryRun) { | ||
| 3636 | 3656 | |
| 3637 | 3657 | primary: for (let key of entry.key) { |
| 3638 | 3658 | const substituted = substituteParams(key); |
| 3639 | 3659 | const textToScan = buffer.get(entry, scanState); |
| 3640 | 3660 | |
| 3641 | 3661 | if (substituted && buffer.matchKeys(textToScan, substituted.trim(), entry)) { |
| 3642 | 3662 | console.debug(`WI UID ${entry.uid} found by primary match: ${substituted}.`); |
| @@ -3706,7 +3726,7 @@ async function checkWorldInfo(chat, maxContext, isDryRun) { | ||
| 3706 | 3726 | const textToScanTokens = await getTokenCountAsync(allActivatedText); |
| 3707 | 3727 | const probabilityChecksBefore = failedProbabilityChecks.size; |
| 3708 | 3728 | |
| 3709 | 3729 | filterByInclusionGroups(newEntries, allActivatedEntries, buffer, scanState); |
| 3710 | 3730 | |
| 3711 | 3731 | console.debug('-- PROBABILITY CHECKS BEGIN --'); |
| 3712 | 3732 | for (const entry of newEntries) { |
| @@ -3858,8 +3878,9 @@ async function checkWorldInfo(chat, maxContext, isDryRun) { | ||
| 3858 | 3878 | * @param {Record<string, WIScanEntry[]>} groups The groups to filter |
| 3859 | 3879 | * @param {WorldInfoBuffer} buffer The buffer to use for scoring |
| 3860 | 3880 | * @param {(entry: WIScanEntry) => void} removeEntry The function to remove an entry |
| 3881 | + * @param {number} scanState The current scan state | |
| 3861 | 3882 | */ |
| 3862 | 3883 | function filterGroupsByScoring(groups, buffer, removeEntry, scanState) { |
| 3863 | 3884 | for (const [key, group] of Object.entries(groups)) { |
| 3864 | 3885 | // Group scoring is disabled both globally and for the group entries |
| 3865 | 3886 | if (!world_info_use_group_scoring && !group.some(x => x.useGroupScoring)) { |
| @@ -3867,7 +3888,7 @@ function filterGroupsByScoring(groups, buffer, removeEntry) { | ||
| 3867 | 3888 | continue; |
| 3868 | 3889 | } |
| 3869 | 3890 | |
| 3870 | 3891 | const scores = group.map(entry => buffer.getScore(entry, scanState)); |
| 3871 | 3892 | const maxScore = Math.max(...scores); |
| 3872 | 3893 | console.debug(`Group '${key}' max score: ${maxScore}`); |
| 3873 | 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 | 3916 | * @param {object[]} newEntries Entries activated on current recursion level |
| 3896 | 3917 | * @param {Set<object>} allActivatedEntries Set of all activated entries |
| 3897 | 3918 | * @param {WorldInfoBuffer} buffer The buffer to use for scanning |
| 3919 | + * @param {number} scanState The current scan state | |
| 3898 | 3920 | */ |
| 3899 | 3921 | function filterByInclusionGroups(newEntries, allActivatedEntries, buffer, scanState) { |
| 3900 | 3922 | console.debug('-- INCLUSION GROUP CHECKS BEGIN --'); |
| 3901 | 3923 | const grouped = newEntries.filter(x => x.group).reduce((acc, item) => { |
| 3902 | 3924 | item.group.split(/,\s*/).filter(x => x).forEach(group => { |
| @@ -3925,7 +3947,7 @@ function filterByInclusionGroups(newEntries, allActivatedEntries, buffer) { | ||
| 3925 | 3947 | } |
| 3926 | 3948 | } |
| 3927 | 3949 | |
| 3928 | 3950 | filterGroupsByScoring(grouped, buffer, removeEntry, scanState); |
| 3929 | 3951 | |
| 3930 | 3952 | for (const [key, group] of Object.entries(grouped)) { |
| 3931 | 3953 | console.debug(`Checking inclusion group '${key}' with ${group.length} entries`, group); |