Adds sprite-based filtering for expressions - Functionality only available for LLM/webLLM - New toggle to filter expressions on availalbe sprites - `getExpressionsList` filters cached expressions when checked (using sprite folder name/override) - `/expression-list` slash command has "filter" arg to filter list - `/expression-classify` slash command has "filter" arg now, to use filtered list for classification - `getExpressionLabel` uses filtered expressions when LLM/webLLM
| @@ -4,7 +4,7 @@ import { characters, eventSource, event_types, generateRaw, getRequestHeaders, m | |||
| 4 | import { dragElement, isMobile } from '../../RossAscends-mods.js'; | 4 | import { dragElement, isMobile } from '../../RossAscends-mods.js'; |
| 5 | import { getContext, getApiUrl, modules, extension_settings, ModuleWorkerWrapper, doExtrasFetch, renderExtensionTemplateAsync } from '../../extensions.js'; | 5 | import { getContext, getApiUrl, modules, extension_settings, ModuleWorkerWrapper, doExtrasFetch, renderExtensionTemplateAsync } from '../../extensions.js'; |
| 6 | import { loadMovingUIState, performFuzzySearch, power_user } from '../../power-user.js'; | 6 | import { loadMovingUIState, performFuzzySearch, power_user } from '../../power-user.js'; |
| 7 | import { onlyUnique, debounce, getCharaFilename, trimToEndSentence, trimToStartSentence, waitUntilCondition, findChar } from '../../utils.js'; | 7 | import { onlyUnique, debounce, getCharaFilename, trimToEndSentence, trimToStartSentence, waitUntilCondition, findChar, isTrueBoolean } from '../../utils.js'; |
| 8 | import { hideMutedSprites, selected_group } from '../../group-chats.js'; | 8 | import { hideMutedSprites, selected_group } from '../../group-chats.js'; |
| 9 | import { isJsonSchemaSupported } from '../../textgen-settings.js'; | 9 | import { isJsonSchemaSupported } from '../../textgen-settings.js'; |
| 10 | import { debounce_timeout } from '../../constants.js'; | 10 | import { debounce_timeout } from '../../constants.js'; |
| @@ -678,7 +678,7 @@ async function setSpriteFolderCommand(_, folder) { | |||
| 678 | return ''; | 678 | return ''; |
| 679 | } | 679 | } |
| 680 | 680 | ||
| 681 | async function classifyCallback(/** @type {{api: string?, prompt: string?}} */ { api = null, prompt = null }, text) { | 681 | async function classifyCallback(/** @type {{api: string?, filter: string?, prompt: string?}} */ { api = null, filter = null, prompt = null }, text) { |
| 682 | if (!text) { | 682 | if (!text) { |
| 683 | toastr.error('No text provided'); | 683 | toastr.error('No text provided'); |
| 684 | return ''; | 684 | return ''; |
| @@ -689,13 +689,14 @@ async function classifyCallback(/** @type {{api: string?, prompt: string?}} */ { | |||
| 689 | } | 689 | } |
| 690 | 690 | ||
| 691 | const expressionApi = EXPRESSION_API[api] || extension_settings.expressions.api; | 691 | const expressionApi = EXPRESSION_API[api] || extension_settings.expressions.api; |
| 692 | const filterAvailable = isTrueBoolean(filter); | ||
| 692 | 693 | ||
| 693 | if (!modules.includes('classify') && expressionApi == EXPRESSION_API.extras) { | 694 | if (!modules.includes('classify') && expressionApi == EXPRESSION_API.extras) { |
| 694 | toastr.warning('Text classification is disabled or not available'); | 695 | toastr.warning('Text classification is disabled or not available'); |
| 695 | return ''; | 696 | return ''; |
| 696 | } | 697 | } |
| 697 | 698 | ||
| 698 | const label = await getExpressionLabel(text, expressionApi, { customPrompt: prompt }); | 699 | const label = await getExpressionLabel(text, expressionApi, { filterAvailable: filterAvailable, customPrompt: prompt }); |
| 699 | console.debug(`Classification result for "${text}": ${label}`); | 700 | console.debug(`Classification result for "${text}": ${label}`); |
| 700 | return label; | 701 | return label; |
| 701 | } | 702 | } |
| @@ -988,10 +989,11 @@ function onTextGenSettingsReady(args) { | |||
| 988 | * @param {string} text - The text to classify and retrieve the expression label for. | 989 | * @param {string} text - The text to classify and retrieve the expression label for. |
| 989 | * @param {EXPRESSION_API} [expressionsApi=extension_settings.expressions.api] - The expressions API to use for classification. | 990 | * @param {EXPRESSION_API} [expressionsApi=extension_settings.expressions.api] - The expressions API to use for classification. |
| 990 | * @param {object} [options={}] - Optional arguments. | 991 | * @param {object} [options={}] - Optional arguments. |
| 992 | * @param {boolean?} [options.filterAvailable=null] - Whether to filter available expressions. If not specified, uses the extension setting. | ||
| 991 | * @param {string?} [options.customPrompt=null] - The custom prompt to use for classification. | 993 | * @param {string?} [options.customPrompt=null] - The custom prompt to use for classification. |
| 992 | * @returns {Promise<string?>} - The label of the expression. | 994 | * @returns {Promise<string?>} - The label of the expression. |
| 993 | */ | 995 | */ |
| 994 | export async function getExpressionLabel(text, expressionsApi = extension_settings.expressions.api, { customPrompt = null } = {}) { | 996 | export async function getExpressionLabel(text, expressionsApi = extension_settings.expressions.api, { filterAvailable = null, customPrompt = null } = {}) { |
| 995 | // Return if text is undefined, saving a costly fetch request | 997 | // Return if text is undefined, saving a costly fetch request |
| 996 | if ((!modules.includes('classify') && expressionsApi == EXPRESSION_API.extras) || !text) { | 998 | if ((!modules.includes('classify') && expressionsApi == EXPRESSION_API.extras) || !text) { |
| 997 | return extension_settings.expressions.fallback_expression; | 999 | return extension_settings.expressions.fallback_expression; |
| @@ -1003,6 +1005,11 @@ export async function getExpressionLabel(text, expressionsApi = extension_settin | |||
| 1003 | 1005 | ||
| 1004 | text = sampleClassifyText(text); | 1006 | text = sampleClassifyText(text); |
| 1005 | 1007 | ||
| 1008 | filterAvailable ??= extension_settings.expressions.filterAvailable; | ||
| 1009 | if (filterAvailable && ![EXPRESSION_API.llm, EXPRESSION_API.webllm].includes(expressionsApi)) { | ||
| 1010 | console.warn('Filter available is only supported for LLM and WebLLM expressions'); | ||
| 1011 | } | ||
| 1012 | |||
| 1006 | try { | 1013 | try { |
| 1007 | switch (expressionsApi) { | 1014 | switch (expressionsApi) { |
| 1008 | // Local BERT pipeline | 1015 | // Local BERT pipeline |
| @@ -1027,7 +1034,7 @@ export async function getExpressionLabel(text, expressionsApi = extension_settin | |||
| 1027 | return extension_settings.expressions.fallback_expression; | 1034 | return extension_settings.expressions.fallback_expression; |
| 1028 | } | 1035 | } |
| 1029 | 1036 | ||
| 1030 | const expressionsList = await getExpressionsList(); | 1037 | const expressionsList = await getExpressionsList({ filterAvailable: filterAvailable }); |
| 1031 | const prompt = substituteParamsExtended(customPrompt, { labels: expressionsList }) || await getLlmPrompt(expressionsList); | 1038 | const prompt = substituteParamsExtended(customPrompt, { labels: expressionsList }) || await getLlmPrompt(expressionsList); |
| 1032 | eventSource.once(event_types.TEXT_COMPLETION_SETTINGS_READY, onTextGenSettingsReady); | 1039 | eventSource.once(event_types.TEXT_COMPLETION_SETTINGS_READY, onTextGenSettingsReady); |
| 1033 | const emotionResponse = await generateRaw(text, main_api, false, false, prompt); | 1040 | const emotionResponse = await generateRaw(text, main_api, false, false, prompt); |
| @@ -1040,7 +1047,7 @@ export async function getExpressionLabel(text, expressionsApi = extension_settin | |||
| 1040 | return extension_settings.expressions.fallback_expression; | 1047 | return extension_settings.expressions.fallback_expression; |
| 1041 | } | 1048 | } |
| 1042 | 1049 | ||
| 1043 | const expressionsList = await getExpressionsList(); | 1050 | const expressionsList = await getExpressionsList({ filterAvailable: filterAvailable }); |
| 1044 | const prompt = substituteParamsExtended(customPrompt, { labels: expressionsList }) || await getLlmPrompt(expressionsList); | 1051 | const prompt = substituteParamsExtended(customPrompt, { labels: expressionsList }) || await getLlmPrompt(expressionsList); |
| 1045 | const messages = [ | 1052 | const messages = [ |
| 1046 | { role: 'user', content: text + '\n\n' + prompt }, | 1053 | { role: 'user', content: text + '\n\n' + prompt }, |
| @@ -1320,12 +1327,28 @@ function getCachedExpressions() { | |||
| 1320 | return [...expressionsList, ...extension_settings.expressions.custom].filter(onlyUnique); | 1327 | return [...expressionsList, ...extension_settings.expressions.custom].filter(onlyUnique); |
| 1321 | } | 1328 | } |
| 1322 | 1329 | ||
| 1323 | export async function getExpressionsList() { | 1330 | export async function getExpressionsList({ filterAvailable = false } = {}) { |
| 1324 | // Return cached list if available | 1331 | // If there is no cached list, load and cache it |
| 1325 | if (Array.isArray(expressionsList)) { | 1332 | if (!Array.isArray(expressionsList)) { |
| 1326 | return getCachedExpressions(); | 1333 | expressionsList = await resolveExpressionsList(); |
| 1327 | } | 1334 | } |
| 1328 | 1335 | ||
| 1336 | const expressions = getCachedExpressions(); | ||
| 1337 | |||
| 1338 | // Filtering is only available for llm and webllm APIs | ||
| 1339 | if (!filterAvailable || ![EXPRESSION_API.llm, EXPRESSION_API.webllm].includes(extension_settings.expressions.api)) { | ||
| 1340 | return expressions; | ||
| 1341 | } | ||
| 1342 | |||
| 1343 | // Get expressions with available sprites | ||
| 1344 | const currentLastMessage = selected_group ? getLastCharacterMessage() : null; | ||
| 1345 | const spriteFolderName = getSpriteFolderName(currentLastMessage, currentLastMessage?.name); | ||
| 1346 | |||
| 1347 | return expressions.filter(label => { | ||
| 1348 | const expression = spriteCache[spriteFolderName]?.find(x => x.label === label); | ||
| 1349 | return (expression?.files.length ?? 0) > 0; | ||
| 1350 | }); | ||
| 1351 | |||
| 1329 | /** | 1352 | /** |
| 1330 | * Returns the list of expressions from the API or fallback in offline mode. | 1353 | * Returns the list of expressions from the API or fallback in offline mode. |
| 1331 | * @returns {Promise<string[]>} | 1354 | * @returns {Promise<string[]>} |
| @@ -1372,9 +1395,6 @@ export async function getExpressionsList() { | |||
| 1372 | expressionsList = DEFAULT_EXPRESSIONS.slice(); | 1395 | expressionsList = DEFAULT_EXPRESSIONS.slice(); |
| 1373 | return expressionsList; | 1396 | return expressionsList; |
| 1374 | } | 1397 | } |
| 1375 | |||
| 1376 | const result = await resolveExpressionsList(); | ||
| 1377 | return [...result, ...extension_settings.expressions.custom].filter(onlyUnique); | ||
| 1378 | } | 1398 | } |
| 1379 | 1399 | ||
| 1380 | /** | 1400 | /** |
| @@ -2102,6 +2122,10 @@ function migrateSettings() { | |||
| 2102 | extension_settings.expressions.rerollIfSame = !!$(this).prop('checked'); | 2122 | extension_settings.expressions.rerollIfSame = !!$(this).prop('checked'); |
| 2103 | saveSettingsDebounced(); | 2123 | saveSettingsDebounced(); |
| 2104 | }); | 2124 | }); |
| 2125 | $('#expressions_filter_available').prop('checked', extension_settings.expressions.filterAvailable).on('input', function () { | ||
| 2126 | extension_settings.expressions.filterAvailable = !!$(this).prop('checked'); | ||
| 2127 | saveSettingsDebounced(); | ||
| 2128 | }); | ||
| 2105 | $('#expression_override_cleanup_button').on('click', onClickExpressionOverrideRemoveAllButton); | 2129 | $('#expression_override_cleanup_button').on('click', onClickExpressionOverrideRemoveAllButton); |
| 2106 | $(document).on('dragstart', '.expression', (e) => { | 2130 | $(document).on('dragstart', '.expression', (e) => { |
| 2107 | e.preventDefault(); | 2131 | e.preventDefault(); |
| @@ -2279,13 +2303,13 @@ function migrateSettings() { | |||
| 2279 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ | 2303 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ |
| 2280 | name: 'expression-list', | 2304 | name: 'expression-list', |
| 2281 | aliases: ['expressions'], | 2305 | aliases: ['expressions'], |
| 2282 | /** @type {(args: {return: string}) => Promise<string>} */ | 2306 | /** @type {(args: {return: string, filterAvailable: string}) => Promise<string>} */ |
| 2283 | callback: async (args) => { | 2307 | callback: async (args) => { |
| 2284 | let returnType = | 2308 | let returnType = |
| 2285 | /** @type {import('../../slash-commands/SlashCommandReturnHelper.js').SlashCommandReturnType} */ | 2309 | /** @type {import('../../slash-commands/SlashCommandReturnHelper.js').SlashCommandReturnType} */ |
| 2286 | (args.return); | 2310 | (args.return); |
| 2287 | 2311 | ||
| 2288 | const list = await getExpressionsList(); | 2312 | const list = await getExpressionsList({ filterAvailable: isTrueBoolean(args.filterAvailable) }); |
| 2289 | 2313 | ||
| 2290 | return await slashCommandReturnHelper.doReturn(returnType ?? 'pipe', list, { objectToStringFunc: list => list.join(', ') }); | 2314 | return await slashCommandReturnHelper.doReturn(returnType ?? 'pipe', list, { objectToStringFunc: list => list.join(', ') }); |
| 2291 | }, | 2315 | }, |
| @@ -2298,6 +2322,13 @@ function migrateSettings() { | |||
| 2298 | enumList: slashCommandReturnHelper.enumList({ allowObject: true }), | 2322 | enumList: slashCommandReturnHelper.enumList({ allowObject: true }), |
| 2299 | forceEnum: true, | 2323 | forceEnum: true, |
| 2300 | }), | 2324 | }), |
| 2325 | SlashCommandNamedArgument.fromProps({ | ||
| 2326 | name: 'filter', | ||
| 2327 | description: 'Filter the list to only include expressions that have available sprites for the current character.', | ||
| 2328 | typeList: [ARGUMENT_TYPE.BOOLEAN], | ||
| 2329 | enumList: commonEnumProviders.boolean('trueFalse')(), | ||
| 2330 | defaultValue: 'true', | ||
| 2331 | }), | ||
| 2301 | ], | 2332 | ], |
| 2302 | returns: 'The comma-separated list of available expressions, including custom expressions.', | 2333 | returns: 'The comma-separated list of available expressions, including custom expressions.', |
| 2303 | helpString: 'Returns a list of available expressions, including custom expressions.', | 2334 | helpString: 'Returns a list of available expressions, including custom expressions.', |
| @@ -2314,6 +2345,13 @@ function migrateSettings() { | |||
| 2314 | enumList: Object.keys(EXPRESSION_API).map(api => new SlashCommandEnumValue(api, null, enumTypes.enum)), | 2345 | enumList: Object.keys(EXPRESSION_API).map(api => new SlashCommandEnumValue(api, null, enumTypes.enum)), |
| 2315 | }), | 2346 | }), |
| 2316 | SlashCommandNamedArgument.fromProps({ | 2347 | SlashCommandNamedArgument.fromProps({ |
| 2348 | name: 'filter', | ||
| 2349 | description: 'Filter the list to only include expressions that have available sprites for the current character.', | ||
| 2350 | typeList: [ARGUMENT_TYPE.BOOLEAN], | ||
| 2351 | enumList: commonEnumProviders.boolean('trueFalse')(), | ||
| 2352 | defaultValue: 'true', | ||
| 2353 | }), | ||
| 2354 | SlashCommandNamedArgument.fromProps({ | ||
| 2317 | name: 'prompt', | 2355 | name: 'prompt', |
| 2318 | description: 'Custom prompt for classification. Only relevant if Classifier API is set to LLM.', | 2356 | description: 'Custom prompt for classification. Only relevant if Classifier API is set to LLM.', |
| 2319 | typeList: [ARGUMENT_TYPE.STRING], | 2357 | typeList: [ARGUMENT_TYPE.STRING], |
| @@ -29,7 +29,11 @@ | |||
| 29 | </select> | 29 | </select> |
| 30 | </div> | 30 | </div> |
| 31 | <div class="expression_llm_prompt_block m-b-1 m-t-1"> | 31 | <div class="expression_llm_prompt_block m-b-1 m-t-1"> |
| 32 | <label for="expression_llm_prompt" class="title_restorable"> | 32 | <label class="checkbox_label" for="expressions_filter_available" title="When using LLM or WebLLM classifier, only show and use expressions that have sprites assigned to them." data-i18n="[title]When using LLM or WebLLM classifier, only show and use expressions that have sprites assigned to them."> |
| 33 | <input id="expressions_filter_available" type="checkbox"> | ||
| 34 | <span data-i18n="Filter expressions for available sprites">Filter expressions for available sprites</span> | ||
| 35 | </label> | ||
| 36 | <label for="expression_llm_prompt" class="title_restorable m-t-1"> | ||
| 33 | <span data-i18n="LLM Prompt">LLM Prompt</span> | 37 | <span data-i18n="LLM Prompt">LLM Prompt</span> |
| 34 | <div id="expression_llm_prompt_restore" title="Restore default value" class="right_menu_button"> | 38 | <div id="expression_llm_prompt_restore" title="Restore default value" class="right_menu_button"> |
| 35 | <i class="fa-solid fa-clock-rotate-left fa-sm"></i> | 39 | <i class="fa-solid fa-clock-rotate-left fa-sm"></i> |