mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-02-18 13:10:40 +01:00
Compare commits
2 Commits
d1cac298c6
...
e56faaaed5
Author | SHA1 | Date | |
---|---|---|---|
|
e56faaaed5 | ||
|
e1d6a47048 |
@ -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 */
|
||||
|
@ -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');
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user