mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-02-23 15:37:50 +01:00
Handle stale cache
This commit is contained in:
parent
eb29f03ab0
commit
2661881bc4
@ -330,12 +330,6 @@ const debug_functions = [];
|
|||||||
|
|
||||||
const setHotswapsDebounced = debounce(favsToHotswap);
|
const setHotswapsDebounced = debounce(favsToHotswap);
|
||||||
|
|
||||||
const fuzzySearchCharactersCache = new Map();
|
|
||||||
const fuzzySearchWorldInfoCache = new Map();
|
|
||||||
const fuzzySearchPersonasCache = new Map();
|
|
||||||
const fuzzySearchTagsCache = new Map();
|
|
||||||
const fuzzySearchGroupsCache = new Map();
|
|
||||||
|
|
||||||
function playMessageSound() {
|
function playMessageSound() {
|
||||||
if (!power_user.play_message_sound) {
|
if (!power_user.play_message_sound) {
|
||||||
return;
|
return;
|
||||||
@ -1830,20 +1824,37 @@ async function loadContextSettings() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create separate caches for each type of fuzzy search
|
||||||
|
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() },
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fuzzy search characters by a search term
|
* 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fuzzy search characters by a search term with caching
|
||||||
* @param {string} searchValue - The search term
|
* @param {string} searchValue - The search term
|
||||||
* @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score
|
* @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score
|
||||||
*/
|
*/
|
||||||
export function fuzzySearchCharacters(searchValue) {
|
export function fuzzySearchCharacters(searchValue) {
|
||||||
|
const fuseKeys = [
|
||||||
if (fuzzySearchCharactersCache.has(searchValue)) {
|
|
||||||
return fuzzySearchCharactersCache.get(searchValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
// @ts-ignore
|
|
||||||
const fuse = new Fuse(characters, {
|
|
||||||
keys: [
|
|
||||||
{ name: 'data.name', weight: 20 },
|
{ name: 'data.name', weight: 20 },
|
||||||
{ name: '#tags', weight: 10, getFn: (character) => getTagsList(character.avatar).map(x => x.name).join('||') },
|
{ name: '#tags', weight: 10, getFn: (character) => getTagsList(character.avatar).map(x => x.name).join('||') },
|
||||||
{ name: 'data.description', weight: 3 },
|
{ name: 'data.description', weight: 3 },
|
||||||
@ -1855,7 +1866,28 @@ export function fuzzySearchCharacters(searchValue) {
|
|||||||
{ name: 'data.creator', weight: 1 },
|
{ name: 'data.creator', weight: 1 },
|
||||||
{ name: 'data.tags', weight: 1 },
|
{ name: 'data.tags', weight: 1 },
|
||||||
{ name: 'data.alternate_greetings', weight: 1 },
|
{ name: 'data.alternate_greetings', weight: 1 },
|
||||||
],
|
];
|
||||||
|
|
||||||
|
// Generate hash of current keys configuration
|
||||||
|
const currentKeyHash = hashFuseKeys(fuseKeys);
|
||||||
|
const cache = fuzzySearchCaches.characters;
|
||||||
|
|
||||||
|
// Check if we have a cached result for this search value
|
||||||
|
if (cache.keyHash === currentKeyHash && cache.resultMap.has(searchValue)) {
|
||||||
|
console.debug('Using cached fuzzy search results for ' + searchValue);
|
||||||
|
return cache.resultMap.get(searchValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If key configuration changed, clear the cache
|
||||||
|
if (cache.keyHash !== currentKeyHash) {
|
||||||
|
console.debug('Fuse keys changed, clearing cache');
|
||||||
|
cache.keyHash = currentKeyHash;
|
||||||
|
cache.resultMap.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create new Fuse instance and perform search
|
||||||
|
const fuse = new Fuse(characters, {
|
||||||
|
keys: fuseKeys,
|
||||||
includeScore: true,
|
includeScore: true,
|
||||||
ignoreLocation: true,
|
ignoreLocation: true,
|
||||||
useExtendedSearch: true,
|
useExtendedSearch: true,
|
||||||
@ -1863,8 +1895,11 @@ export function fuzzySearchCharacters(searchValue) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const results = fuse.search(searchValue);
|
const results = fuse.search(searchValue);
|
||||||
|
|
||||||
|
// Cache the results
|
||||||
|
cache.resultMap.set(searchValue, results);
|
||||||
|
|
||||||
console.debug('Characters fuzzy search results for ' + searchValue, results);
|
console.debug('Characters fuzzy search results for ' + searchValue, results);
|
||||||
fuzzySearchCharactersCache.set(searchValue, results);
|
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1875,13 +1910,7 @@ export function fuzzySearchCharacters(searchValue) {
|
|||||||
* @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score
|
* @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score
|
||||||
*/
|
*/
|
||||||
export function fuzzySearchWorldInfo(data, searchValue) {
|
export function fuzzySearchWorldInfo(data, searchValue) {
|
||||||
if (fuzzySearchWorldInfoCache.has(searchValue)) {
|
const fuseKeys = [
|
||||||
return fuzzySearchWorldInfoCache.get(searchValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
// @ts-ignore
|
|
||||||
const fuse = new Fuse(data, {
|
|
||||||
keys: [
|
|
||||||
{ name: 'key', weight: 20 },
|
{ name: 'key', weight: 20 },
|
||||||
{ name: 'group', weight: 15 },
|
{ name: 'group', weight: 15 },
|
||||||
{ name: 'comment', weight: 10 },
|
{ name: 'comment', weight: 10 },
|
||||||
@ -1889,7 +1918,26 @@ export function fuzzySearchWorldInfo(data, searchValue) {
|
|||||||
{ name: 'content', weight: 3 },
|
{ name: 'content', weight: 3 },
|
||||||
{ name: 'uid', weight: 1 },
|
{ name: 'uid', weight: 1 },
|
||||||
{ name: 'automationId', weight: 1 },
|
{ name: 'automationId', weight: 1 },
|
||||||
],
|
];
|
||||||
|
|
||||||
|
const currentKeyHash = hashFuseKeys(fuseKeys);
|
||||||
|
const cache = fuzzySearchCaches.worldInfo;
|
||||||
|
|
||||||
|
// Check cache for existing results
|
||||||
|
if (cache.keyHash === currentKeyHash && cache.resultMap.has(searchValue)) {
|
||||||
|
console.debug('Using cached WI fuzzy search results for ' + searchValue);
|
||||||
|
return cache.resultMap.get(searchValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear cache if keys changed
|
||||||
|
if (cache.keyHash !== currentKeyHash) {
|
||||||
|
console.debug('WI Fuse keys changed, clearing cache');
|
||||||
|
cache.keyHash = currentKeyHash;
|
||||||
|
cache.resultMap.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
const fuse = new Fuse(data, {
|
||||||
|
keys: fuseKeys,
|
||||||
includeScore: true,
|
includeScore: true,
|
||||||
ignoreLocation: true,
|
ignoreLocation: true,
|
||||||
useExtendedSearch: true,
|
useExtendedSearch: true,
|
||||||
@ -1897,8 +1945,9 @@ export function fuzzySearchWorldInfo(data, searchValue) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const results = fuse.search(searchValue);
|
const results = fuse.search(searchValue);
|
||||||
|
cache.resultMap.set(searchValue, results);
|
||||||
|
|
||||||
console.debug('World Info fuzzy search results for ' + searchValue, results);
|
console.debug('World Info fuzzy search results for ' + searchValue, results);
|
||||||
fuzzySearchWorldInfoCache.set(searchValue, results);
|
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1909,17 +1958,31 @@ export function fuzzySearchWorldInfo(data, searchValue) {
|
|||||||
* @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score
|
* @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score
|
||||||
*/
|
*/
|
||||||
export function fuzzySearchPersonas(data, searchValue) {
|
export function fuzzySearchPersonas(data, searchValue) {
|
||||||
if (fuzzySearchPersonasCache.has(searchValue)) {
|
|
||||||
return fuzzySearchPersonasCache.get(searchValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
data = data.map(x => ({ key: x, name: power_user.personas[x] ?? '', description: power_user.persona_descriptions[x]?.description ?? '' }));
|
data = data.map(x => ({ key: x, name: power_user.personas[x] ?? '', description: power_user.persona_descriptions[x]?.description ?? '' }));
|
||||||
// @ts-ignore
|
|
||||||
const fuse = new Fuse(data, {
|
const fuseKeys = [
|
||||||
keys: [
|
|
||||||
{ name: 'name', weight: 20 },
|
{ name: 'name', weight: 20 },
|
||||||
{ name: 'description', weight: 3 },
|
{ name: 'description', weight: 3 },
|
||||||
],
|
];
|
||||||
|
|
||||||
|
const currentKeyHash = hashFuseKeys(fuseKeys);
|
||||||
|
const cache = fuzzySearchCaches.personas;
|
||||||
|
|
||||||
|
// Check cache for existing results
|
||||||
|
if (cache.keyHash === currentKeyHash && cache.resultMap.has(searchValue)) {
|
||||||
|
console.debug('Using cached personas fuzzy search results for ' + searchValue);
|
||||||
|
return cache.resultMap.get(searchValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear cache if keys changed
|
||||||
|
if (cache.keyHash !== currentKeyHash) {
|
||||||
|
console.debug('Personas Fuse keys changed, clearing cache');
|
||||||
|
cache.keyHash = currentKeyHash;
|
||||||
|
cache.resultMap.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
const fuse = new Fuse(data, {
|
||||||
|
keys: fuseKeys,
|
||||||
includeScore: true,
|
includeScore: true,
|
||||||
ignoreLocation: true,
|
ignoreLocation: true,
|
||||||
useExtendedSearch: true,
|
useExtendedSearch: true,
|
||||||
@ -1927,8 +1990,9 @@ export function fuzzySearchPersonas(data, searchValue) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const results = fuse.search(searchValue);
|
const results = fuse.search(searchValue);
|
||||||
|
cache.resultMap.set(searchValue, results);
|
||||||
|
|
||||||
console.debug('Personas fuzzy search results for ' + searchValue, results);
|
console.debug('Personas fuzzy search results for ' + searchValue, results);
|
||||||
fuzzySearchPersonasCache.set(searchValue, results);
|
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1938,15 +2002,28 @@ export function fuzzySearchPersonas(data, searchValue) {
|
|||||||
* @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score
|
* @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score
|
||||||
*/
|
*/
|
||||||
export function fuzzySearchTags(searchValue) {
|
export function fuzzySearchTags(searchValue) {
|
||||||
if (fuzzySearchTagsCache.has(searchValue)) {
|
const fuseKeys = [
|
||||||
return fuzzySearchTagsCache.get(searchValue);
|
{ name: 'name', weight: 1 },
|
||||||
|
];
|
||||||
|
|
||||||
|
const currentKeyHash = hashFuseKeys(fuseKeys);
|
||||||
|
const cache = fuzzySearchCaches.tags;
|
||||||
|
|
||||||
|
// Check cache for existing results
|
||||||
|
if (cache.keyHash === currentKeyHash && cache.resultMap.has(searchValue)) {
|
||||||
|
console.debug('Using cached tags fuzzy search results for ' + searchValue);
|
||||||
|
return cache.resultMap.get(searchValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear cache if keys changed
|
||||||
|
if (cache.keyHash !== currentKeyHash) {
|
||||||
|
console.debug('Tags Fuse keys changed, clearing cache');
|
||||||
|
cache.keyHash = currentKeyHash;
|
||||||
|
cache.resultMap.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
// @ts-ignore
|
|
||||||
const fuse = new Fuse(tags, {
|
const fuse = new Fuse(tags, {
|
||||||
keys: [
|
keys: fuseKeys,
|
||||||
{ name: 'name', weight: 1 },
|
|
||||||
],
|
|
||||||
includeScore: true,
|
includeScore: true,
|
||||||
ignoreLocation: true,
|
ignoreLocation: true,
|
||||||
useExtendedSearch: true,
|
useExtendedSearch: true,
|
||||||
@ -1954,8 +2031,9 @@ export function fuzzySearchTags(searchValue) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const results = fuse.search(searchValue);
|
const results = fuse.search(searchValue);
|
||||||
|
cache.resultMap.set(searchValue, results);
|
||||||
|
|
||||||
console.debug('Tags fuzzy search results for ' + searchValue, results);
|
console.debug('Tags fuzzy search results for ' + searchValue, results);
|
||||||
fuzzySearchTagsCache.set(searchValue, results);
|
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1965,18 +2043,31 @@ export function fuzzySearchTags(searchValue) {
|
|||||||
* @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score
|
* @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score
|
||||||
*/
|
*/
|
||||||
export function fuzzySearchGroups(searchValue) {
|
export function fuzzySearchGroups(searchValue) {
|
||||||
if (fuzzySearchGroupsCache.has(searchValue)) {
|
const fuseKeys = [
|
||||||
return fuzzySearchGroupsCache.get(searchValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
// @ts-ignore
|
|
||||||
const fuse = new Fuse(groups, {
|
|
||||||
keys: [
|
|
||||||
{ name: 'name', weight: 20 },
|
{ name: 'name', weight: 20 },
|
||||||
{ name: 'members', weight: 15 },
|
{ name: 'members', weight: 15 },
|
||||||
{ name: '#tags', weight: 10, getFn: (group) => getTagsList(group.id).map(x => x.name).join('||') },
|
{ name: '#tags', weight: 10, getFn: (group) => getTagsList(group.id).map(x => x.name).join('||') },
|
||||||
{ name: 'id', weight: 1 },
|
{ name: 'id', weight: 1 },
|
||||||
],
|
];
|
||||||
|
|
||||||
|
const currentKeyHash = hashFuseKeys(fuseKeys);
|
||||||
|
const cache = fuzzySearchCaches.groups;
|
||||||
|
|
||||||
|
// Check cache for existing results
|
||||||
|
if (cache.keyHash === currentKeyHash && cache.resultMap.has(searchValue)) {
|
||||||
|
console.debug('Using cached groups fuzzy search results for ' + searchValue);
|
||||||
|
return cache.resultMap.get(searchValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear cache if keys changed
|
||||||
|
if (cache.keyHash !== currentKeyHash) {
|
||||||
|
console.debug('Groups Fuse keys changed, clearing cache');
|
||||||
|
cache.keyHash = currentKeyHash;
|
||||||
|
cache.resultMap.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
const fuse = new Fuse(groups, {
|
||||||
|
keys: fuseKeys,
|
||||||
includeScore: true,
|
includeScore: true,
|
||||||
ignoreLocation: true,
|
ignoreLocation: true,
|
||||||
useExtendedSearch: true,
|
useExtendedSearch: true,
|
||||||
@ -1984,11 +2075,22 @@ export function fuzzySearchGroups(searchValue) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const results = fuse.search(searchValue);
|
const results = fuse.search(searchValue);
|
||||||
|
cache.resultMap.set(searchValue, results);
|
||||||
|
|
||||||
console.debug('Groups fuzzy search results for ' + searchValue, results);
|
console.debug('Groups fuzzy search results for ' + searchValue, results);
|
||||||
fuzzySearchGroupsCache.set(searchValue, results);
|
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clears all fuzzy search caches
|
||||||
|
*/
|
||||||
|
export function clearFuzzySearchCaches() {
|
||||||
|
for (const cache of Object.values(fuzzySearchCaches)) {
|
||||||
|
cache.keyHash = null;
|
||||||
|
cache.resultMap.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Renders a story string template with the given parameters.
|
* Renders a story string template with the given parameters.
|
||||||
* @param {object} params Template parameters.
|
* @param {object} params Template parameters.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user