diff --git a/public/scripts/world-info.js b/public/scripts/world-info.js index 4f9bd733f..83d4a1727 100644 --- a/public/scripts/world-info.js +++ b/public/scripts/world-info.js @@ -95,6 +95,11 @@ class WorldInfoBuffer { */ #skew = 0; + /** + * @type {number} The starting depth of the global scan depth. Incremented by "min activations" feature to not repeat scans. When > 0 it means a complete scan was done up to #startDepth already, and `advanceScanPosition` was called. + */ + #startDepth = 0; + /** * Initialize the buffer with the given messages. * @param {string[]} messages Array of messages to add to the buffer @@ -137,7 +142,10 @@ class WorldInfoBuffer { * @returns {string} A slice of buffer until the given depth (inclusive) */ get(entry) { - let depth = entry.scanDepth ?? (world_info_depth + this.#skew); + let depth = entry.scanDepth ?? this.getDepth(); + if (depth <= this.#startDepth) { + return ''; + } if (depth < 0) { console.error(`Invalid WI scan depth ${depth}. Must be >= 0`); @@ -149,7 +157,7 @@ class WorldInfoBuffer { depth = MAX_SCAN_DEPTH; } - let result = this.#depthBuffer.slice(0, depth).join('\n'); + let result = this.#depthBuffer.slice(this.#startDepth, depth).join('\n'); if (this.#recurseBuffer.length > 0) { result += '\n' + this.#recurseBuffer.join('\n'); @@ -197,11 +205,19 @@ class WorldInfoBuffer { } /** - * Adds an increment to depth skew. + * Increments skew and sets startDepth to previous depth. */ - addSkew() { + advanceScanPosition() { + this.#startDepth = this.getDepth(); this.#skew++; } + + /** + * @returns {number} Settings' depth + current skew. + */ + getDepth() { + return world_info_depth + this.#skew; + } } export function getWorldInfoSettings() { @@ -2009,7 +2025,6 @@ async function checkWorldInfo(chat, maxContext) { const buffer = new WorldInfoBuffer(chat); // Combine the chat - let minActivationMsgIndex = world_info_depth; // tracks chat index to satisfy `world_info_min_activations` // Add the depth or AN if enabled // Put this code here since otherwise, the chat reference is modified @@ -2223,15 +2238,14 @@ async function checkWorldInfo(chat, maxContext) { // world_info_min_activations if (!needsToScan && !token_budget_overflowed) { if (world_info_min_activations > 0 && (allActivatedEntries.size < world_info_min_activations)) { - let over_max = false; - over_max = ( + let over_max = ( world_info_min_activations_depth_max > 0 && - minActivationMsgIndex > world_info_min_activations_depth_max - ) || (minActivationMsgIndex >= chat.length); + buffer.getDepth() > world_info_min_activations_depth_max + ) || (buffer.getDepth() > chat.length); + if (!over_max) { - needsToScan = true; - minActivationMsgIndex += 1; - buffer.addSkew(); + needsToScan = true; // loop + buffer.advanceScanPosition(); } } }