Compare commits

...

2 Commits

Author SHA1 Message Date
Joe
e56faaaed5 Remove hash function 2024-11-24 14:55:22 -08:00
Joe
e1d6a47048 Remove key hashing and explicitly clear the cache after printing characters 2024-11-24 14:54:12 -08:00
2 changed files with 11 additions and 33 deletions

View File

@ -268,6 +268,7 @@ import { initServerHistory } from './scripts/server-history.js';
import { initSettingsSearch } from './scripts/setting-search.js';
import { initBulkEdit } from './scripts/bulk-edit.js';
import { deriveTemplatesFromChatTemplate } from './scripts/chat-templates.js';
import { clearFuzzySearchCaches } from './scripts/power-user.js';
//exporting functions and vars for mods
export {
@ -1515,6 +1516,7 @@ export async function printCharacters(fullRefresh = false) {
});
favsToHotswap();
clearFuzzySearchCaches();
}
/** Checks the state of the current search, and adds/removes the search sorting option accordingly */

View File

@ -329,11 +329,11 @@ let browser_has_focus = true;
const debug_functions = [];
const fuzzySearchCaches = {
characters: { keyHash: null, resultMap: new Map() },
worldInfo: { keyHash: null, resultMap: new Map() },
personas: { keyHash: null, resultMap: new Map() },
tags: { keyHash: null, resultMap: new Map() },
groups: { keyHash: null, resultMap: new Map() },
characters: { resultMap: new Map() },
worldInfo: { resultMap: new Map() },
personas: { resultMap: new Map() },
tags: { resultMap: new Map() },
groups: { resultMap: new Map() },
};
const fuzzySearchCategories = {
@ -1840,21 +1840,6 @@ async function loadContextSettings() {
});
}
/**
* Generates a hash of the Fuse configuration keys
* @param {Array<{name: string, weight: number, getFn?: Function}>} keys
* @returns {number} Hash of the keys configuration
*/
function hashFuseKeys(keys) {
// Convert keys to a stable string representation
const keyString = keys.map(k => {
const getFnString = k.getFn ? k.getFn.toString() : '';
return `${k.name}:${k.weight}:${getFnString}`;
}).join('|');
return getStringHash(keyString);
}
/**
* Common function to perform fuzzy search with caching
* @param {string} type - Type of search from fuzzySearchCategories
@ -1864,22 +1849,14 @@ function hashFuseKeys(keys) {
* @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score
*/
function performFuzzySearch(type, data, keys, searchValue) {
const currentKeyHash = hashFuseKeys(keys);
const cache = fuzzySearchCaches[type];
// Check cache for existing results
if (cache.keyHash === currentKeyHash && cache.resultMap.has(searchValue)) {
// console.debug(`Using cached ${type} fuzzy search results for ${searchValue}`);
if (cache.resultMap.has(searchValue)) {
return cache.resultMap.get(searchValue);
}
// Clear cache if keys changed
if (cache.keyHash !== currentKeyHash) {
// console.debug(`${type} Fuse keys changed, clearing cache`);
cache.keyHash = currentKeyHash;
cache.resultMap.clear();
}
// @ts-ignore
const fuse = new Fuse(data, {
keys: keys,
includeScore: true,
@ -1890,19 +1867,18 @@ function performFuzzySearch(type, data, keys, searchValue) {
const results = fuse.search(searchValue);
cache.resultMap.set(searchValue, results);
// console.debug(`${type} fuzzy search results for ${searchValue}`, results);
return results;
}
/**
* Clears all fuzzy search caches
*/
export function clearFuzzySearchCaches() {
for (const cache of Object.values(fuzzySearchCaches)) {
cache.keyHash = null;
cache.resultMap.clear();
}
console.log('Fuzzy search caches cleared');
}
/**