Remove duplicated code

e2c083ba31d0c9e3ab1e168995f03c0e9d37daac

Joe <joenunezb@gmail.com>

1 files changed, +76 -169Ignore whitespace
public/scripts/power-user.js+76 -169
@@ -328,6 +328,22 @@ const contextControls = [
328328let browser_has_focus = true;
329329const debug_functions = [];
330330
331+const fuzzySearchCaches = {
332+ characters: { keyHash: null, resultMap: new Map() },
333+ worldInfo: { keyHash: null, resultMap: new Map() },
334+ personas: { keyHash: null, resultMap: new Map() },
335+ tags: { keyHash: null, resultMap: new Map() },
336+ groups: { keyHash: null, resultMap: new Map() },
337+};
338+
339+const fuzzySearchCategories = {
340+ characters: 'characters',
341+ worldInfo: 'worldInfo',
342+ personas: 'personas',
343+ tags: 'tags',
344+ groups: 'groups',
345+};
346+
331347const setHotswapsDebounced = debounce(favsToHotswap);
332348
333349function playMessageSound() {
@@ -1824,15 +1840,6 @@ async function loadContextSettings() {
18241840 });
18251841}
18261842
1827-// Create separate caches for each type of fuzzy search
1828-const fuzzySearchCaches = {
1829- characters: { keyHash: null, resultMap: new Map() },
1830- worldInfo: { keyHash: null, resultMap: new Map() },
1831- personas: { keyHash: null, resultMap: new Map() },
1832- tags: { keyHash: null, resultMap: new Map() },
1833- groups: { keyHash: null, resultMap: new Map() },
1834-};
1835-
18361843/**
18371844 * Generates a hash of the Fuse configuration keys
18381845 * @param {Array<{name: string, weight: number, getFn?: Function}>} keys
@@ -1849,45 +1856,32 @@ function hashFuseKeys(keys) {
18491856}
18501857
18511858/**
18521859 * FuzzyCommon searchfunction charactersto byperform afuzzy search term with caching
1860+ * @param {string} type - Type of search from fuzzySearchCategories
1861+ * @param {any[]} data - Data array to search in
1862+ * @param {Array<{name: string, weight: number, getFn?: Function}>} keys - Fuse.js keys configuration
18531863 * @param {string} searchValue - The search term
18541864 * @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score
18551865 */
18561866export function fuzzySearchCharactersperformFuzzySearch(type, data, keys, searchValue) {
18571867 const fuseKeyscurrentKeyHash = [hashFuseKeys(keys);
1858- { name: 'data.name', weight: 20 },
1868+ const cache = fuzzySearchCaches[type];
1859- { name: '#tags', weight: 10, getFn: (character) => getTagsList(character.avatar).map(x => x.name).join('||') },
1860- { name: 'data.description', weight: 3 },
1861- { name: 'data.mes_example', weight: 3 },
1862- { name: 'data.scenario', weight: 2 },
1863- { name: 'data.personality', weight: 2 },
1864- { name: 'data.first_mes', weight: 2 },
1865- { name: 'data.creator_notes', weight: 2 },
1866- { name: 'data.creator', weight: 1 },
1867- { name: 'data.tags', weight: 1 },
1868- { name: 'data.alternate_greetings', weight: 1 },
1869- ];
1870-
1871- // Generate hash of current keys configuration
1872- const currentKeyHash = hashFuseKeys(fuseKeys);
1873- const cache = fuzzySearchCaches.characters;
18741869
18751870 // Check if we have a cached resultcache for this searchexisting valueresults
18761871 if (cache.keyHash === currentKeyHash && cache.resultMap.has(searchValue)) {
18771872 console.debuglog('`Using cached ${type} fuzzy search results for ' + ${searchValue}`);
18781873 return cache.resultMap.get(searchValue);
18791874 }
18801875
18811876 // If key configurationClear changed,cache clearif thekeys cachechanged
18821877 if (cache.keyHash !== currentKeyHash) {
18831878 console.debuglog('`${type} Fuse keys changed, clearing cache'`);
18841879 cache.keyHash = currentKeyHash;
18851880 cache.resultMap.clear();
18861881 }
18871882
1888- // Create new Fuse instance and perform search
1883+ const fuse = new Fuse(data, {
1889- const fuse = new Fuse(characters, {
1884+ keys: keys,
1890- keys: fuseKeys,
18911885 includeScore: true,
18921886 ignoreLocation: true,
18931887 useExtendedSearch: true,
@@ -1895,22 +1889,53 @@ export function fuzzySearchCharacters(searchValue) {
18951889 });
18961890
18971891 const results = fuse.search(searchValue);
1898-
1899- // Cache the results
19001892 cache.resultMap.set(searchValue, results);
19011893
19021894 console.debuglog('Characters`${type} fuzzy search results for ' + ${searchValue}`, results);
19031895 return results;
19041896}
19051897
19061898/**
1899+ * Clears all fuzzy search caches
1900+ */
1901+export function clearFuzzySearchCaches() {
1902+ for (const cache of Object.values(fuzzySearchCaches)) {
1903+ cache.keyHash = null;
1904+ cache.resultMap.clear();
1905+ }
1906+}
1907+
1908+/**
1909+ * Fuzzy search characters by a search term
1910+ * @param {string} searchValue - The search term
1911+ * @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score
1912+ */
1913+export function fuzzySearchCharacters(searchValue) {
1914+ const keys = [
1915+ { name: 'data.name', weight: 20 },
1916+ { name: '#tags', weight: 10, getFn: (character) => getTagsList(character.avatar).map(x => x.name).join('||') },
1917+ { name: 'data.description', weight: 3 },
1918+ { name: 'data.mes_example', weight: 3 },
1919+ { name: 'data.scenario', weight: 2 },
1920+ { name: 'data.personality', weight: 2 },
1921+ { name: 'data.first_mes', weight: 2 },
1922+ { name: 'data.creator_notes', weight: 2 },
1923+ { name: 'data.creator', weight: 1 },
1924+ { name: 'data.tags', weight: 1 },
1925+ { name: 'data.alternate_greetings', weight: 1 },
1926+ ];
1927+
1928+ return performFuzzySearch(fuzzySearchCategories.characters, characters, keys, searchValue);
1929+}
1930+
1931+/**
19071932 * Fuzzy search world info entries by a search term
19081933 * @param {*[]} data - WI items data array
19091934 * @param {string} searchValue - The search term
19101935 * @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score
19111936 */
19121937export function fuzzySearchWorldInfo(data, searchValue) {
19131938 const fuseKeyskeys = [
19141939 { name: 'key', weight: 20 },
19151940 { name: 'group', weight: 15 },
19161941 { name: 'comment', weight: 10 },
@@ -1920,35 +1945,7 @@ export function fuzzySearchWorldInfo(data, searchValue) {
19201945 { name: 'automationId', weight: 1 },
19211946 ];
19221947
1923- const currentKeyHash = hashFuseKeys(fuseKeys);
1948+ return performFuzzySearch(fuzzySearchCategories.worldInfo, data, keys, searchValue);
1924- const cache = fuzzySearchCaches.worldInfo;
1925-
1926- // Check cache for existing results
1927- if (cache.keyHash === currentKeyHash && cache.resultMap.has(searchValue)) {
1928- console.debug('Using cached WI fuzzy search results for ' + searchValue);
1929- return cache.resultMap.get(searchValue);
1930- }
1931-
1932- // Clear cache if keys changed
1933- if (cache.keyHash !== currentKeyHash) {
1934- console.debug('WI Fuse keys changed, clearing cache');
1935- cache.keyHash = currentKeyHash;
1936- cache.resultMap.clear();
1937- }
1938-
1939- const fuse = new Fuse(data, {
1940- keys: fuseKeys,
1941- includeScore: true,
1942- ignoreLocation: true,
1943- useExtendedSearch: true,
1944- threshold: 0.2,
1945- });
1946-
1947- const results = fuse.search(searchValue);
1948- cache.resultMap.set(searchValue, results);
1949-
1950- console.debug('World Info fuzzy search results for ' + searchValue, results);
1951- return results;
19521949}
19531950
19541951/**
@@ -1958,42 +1955,18 @@ export function fuzzySearchWorldInfo(data, searchValue) {
19581955 * @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score
19591956 */
19601957export function fuzzySearchPersonas(data, searchValue) {
1961- data = data.map(x => ({ key: x, name: power_user.personas[x] ?? '', description: power_user.persona_descriptions[x]?.description ?? '' }));
1958+ const mappedData = data.map(x => ({
1959+ key: x,
1960+ name: power_user.personas[x] ?? '',
1961+ description: power_user.persona_descriptions[x]?.description ?? ''
1962+ }));
19621963
19631964 const fuseKeyskeys = [
19641965 { name: 'name', weight: 20 },
19651966 { name: 'description', weight: 3 },
19661967 ];
19671968
1968- const currentKeyHash = hashFuseKeys(fuseKeys);
1969+ return performFuzzySearch(fuzzySearchCategories.personas, mappedData, keys, searchValue);
1969- const cache = fuzzySearchCaches.personas;
1970-
1971- // Check cache for existing results
1972- if (cache.keyHash === currentKeyHash && cache.resultMap.has(searchValue)) {
1973- console.debug('Using cached personas fuzzy search results for ' + searchValue);
1974- return cache.resultMap.get(searchValue);
1975- }
1976-
1977- // Clear cache if keys changed
1978- if (cache.keyHash !== currentKeyHash) {
1979- console.debug('Personas Fuse keys changed, clearing cache');
1980- cache.keyHash = currentKeyHash;
1981- cache.resultMap.clear();
1982- }
1983-
1984- const fuse = new Fuse(data, {
1985- keys: fuseKeys,
1986- includeScore: true,
1987- ignoreLocation: true,
1988- useExtendedSearch: true,
1989- threshold: 0.2,
1990- });
1991-
1992- const results = fuse.search(searchValue);
1993- cache.resultMap.set(searchValue, results);
1994-
1995- console.debug('Personas fuzzy search results for ' + searchValue, results);
1996- return results;
19971970}
19981971
19991972/**
@@ -2002,39 +1975,11 @@ export function fuzzySearchPersonas(data, searchValue) {
20021975 * @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score
20031976 */
20041977export function fuzzySearchTags(searchValue) {
20051978 const fuseKeyskeys = [
20061979 { name: 'name', weight: 1 },
20071980 ];
20081981
2009- const currentKeyHash = hashFuseKeys(fuseKeys);
1982+ return performFuzzySearch(fuzzySearchCategories.tags, tags, keys, searchValue);
2010- const cache = fuzzySearchCaches.tags;
2011-
2012- // Check cache for existing results
2013- if (cache.keyHash === currentKeyHash && cache.resultMap.has(searchValue)) {
2014- console.debug('Using cached tags fuzzy search results for ' + searchValue);
2015- return cache.resultMap.get(searchValue);
2016- }
2017-
2018- // Clear cache if keys changed
2019- if (cache.keyHash !== currentKeyHash) {
2020- console.debug('Tags Fuse keys changed, clearing cache');
2021- cache.keyHash = currentKeyHash;
2022- cache.resultMap.clear();
2023- }
2024-
2025- const fuse = new Fuse(tags, {
2026- keys: fuseKeys,
2027- includeScore: true,
2028- ignoreLocation: true,
2029- useExtendedSearch: true,
2030- threshold: 0.2,
2031- });
2032-
2033- const results = fuse.search(searchValue);
2034- cache.resultMap.set(searchValue, results);
2035-
2036- console.debug('Tags fuzzy search results for ' + searchValue, results);
2037- return results;
20381983}
20391984
20401985/**
@@ -2043,52 +1988,14 @@ export function fuzzySearchTags(searchValue) {
20431988 * @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score
20441989 */
20451990export function fuzzySearchGroups(searchValue) {
20461991 const fuseKeyskeys = [
20471992 { name: 'name', weight: 20 },
20481993 { name: 'members', weight: 15 },
20491994 { name: '#tags', weight: 10, getFn: (group) => getTagsList(group.id).map(x => x.name).join('||') },
20501995 { name: 'id', weight: 1 },
20511996 ];
20521997
2053- const currentKeyHash = hashFuseKeys(fuseKeys);
1998+ return performFuzzySearch(fuzzySearchCategories.groups, groups, keys, searchValue);
2054- const cache = fuzzySearchCaches.groups;
2055-
2056- // Check cache for existing results
2057- if (cache.keyHash === currentKeyHash && cache.resultMap.has(searchValue)) {
2058- console.debug('Using cached groups fuzzy search results for ' + searchValue);
2059- return cache.resultMap.get(searchValue);
2060- }
2061-
2062- // Clear cache if keys changed
2063- if (cache.keyHash !== currentKeyHash) {
2064- console.debug('Groups Fuse keys changed, clearing cache');
2065- cache.keyHash = currentKeyHash;
2066- cache.resultMap.clear();
2067- }
2068-
2069- const fuse = new Fuse(groups, {
2070- keys: fuseKeys,
2071- includeScore: true,
2072- ignoreLocation: true,
2073- useExtendedSearch: true,
2074- threshold: 0.2,
2075- });
2076-
2077- const results = fuse.search(searchValue);
2078- cache.resultMap.set(searchValue, results);
2079-
2080- console.debug('Groups fuzzy search results for ' + searchValue, results);
2081- return results;
2082-}
2083-
2084-/**
2085- * Clears all fuzzy search caches
2086- */
2087-export function clearFuzzySearchCaches() {
2088- for (const cache of Object.values(fuzzySearchCaches)) {
2089- cache.keyHash = null;
2090- cache.resultMap.clear();
2091- }
20921999}
20932000
20942001/**