Merge pull request #3725 from SillyTavern/feat/expressions-filter-available Adds filtering to expressions to ignore labels that do not have sprites available
Signed| @@ -4,7 +4,7 @@ import { characters, eventSource, event_types, generateRaw, getRequestHeaders, m | ||
| 4 | 4 | import { dragElement, isMobile } from '../../RossAscends-mods.js'; |
| 5 | 5 | import { getContext, getApiUrl, modules, extension_settings, ModuleWorkerWrapper, doExtrasFetch, renderExtensionTemplateAsync } from '../../extensions.js'; |
| 6 | 6 | import { loadMovingUIState, performFuzzySearch, power_user } from '../../power-user.js'; |
| 7 | 7 | import { onlyUnique, debounce, getCharaFilename, trimToEndSentence, trimToStartSentence, waitUntilCondition, findChar, isFalseBoolean } from '../../utils.js'; |
| 8 | 8 | import { hideMutedSprites, selected_group } from '../../group-chats.js'; |
| 9 | 9 | import { isJsonSchemaSupported } from '../../textgen-settings.js'; |
| 10 | 10 | import { debounce_timeout } from '../../constants.js'; |
| @@ -679,7 +679,7 @@ async function setSpriteFolderCommand(_, folder) { | ||
| 679 | 679 | return ''; |
| 680 | 680 | } |
| 681 | 681 | |
| 682 | 682 | async function classifyCallback(/** @type {{api: string?, filter: string?, prompt: string?}} */ { api = null, filter = null, prompt = null }, text) { |
| 683 | 683 | if (!text) { |
| 684 | 684 | toastr.error('No text provided'); |
| 685 | 685 | return ''; |
| @@ -690,13 +690,14 @@ async function classifyCallback(/** @type {{api: string?, prompt: string?}} */ { | ||
| 690 | 690 | } |
| 691 | 691 | |
| 692 | 692 | const expressionApi = EXPRESSION_API[api] || extension_settings.expressions.api; |
| 693 | + const filterAvailable = !isFalseBoolean(filter); | |
| 693 | 694 | |
| 694 | 695 | if (!modules.includes('classify') && expressionApi == EXPRESSION_API.extras) { |
| 695 | 696 | toastr.warning('Text classification is disabled or not available'); |
| 696 | 697 | return ''; |
| 697 | 698 | } |
| 698 | 699 | |
| 699 | 700 | const label = await getExpressionLabel(text, expressionApi, { filterAvailable: filterAvailable, customPrompt: prompt }); |
| 700 | 701 | console.debug(`Classification result for "${text}": ${label}`); |
| 701 | 702 | return label; |
| 702 | 703 | } |
| @@ -992,10 +993,11 @@ function onTextGenSettingsReady(args) { | ||
| 992 | 993 | * @param {string} text - The text to classify and retrieve the expression label for. |
| 993 | 994 | * @param {EXPRESSION_API} [expressionsApi=extension_settings.expressions.api] - The expressions API to use for classification. |
| 994 | 995 | * @param {object} [options={}] - Optional arguments. |
| 996 | + * @param {boolean?} [options.filterAvailable=null] - Whether to filter available expressions. If not specified, uses the extension setting. | |
| 995 | 997 | * @param {string?} [options.customPrompt=null] - The custom prompt to use for classification. |
| 996 | 998 | * @returns {Promise<string?>} - The label of the expression. |
| 997 | 999 | */ |
| 998 | 1000 | export async function getExpressionLabel(text, expressionsApi = extension_settings.expressions.api, { filterAvailable = null, customPrompt = null } = {}) { |
| 999 | 1001 | // Return if text is undefined, saving a costly fetch request |
| 1000 | 1002 | if ((!modules.includes('classify') && expressionsApi == EXPRESSION_API.extras) || !text) { |
| 1001 | 1003 | return extension_settings.expressions.fallback_expression; |
| @@ -1007,6 +1009,11 @@ export async function getExpressionLabel(text, expressionsApi = extension_settin | ||
| 1007 | 1009 | |
| 1008 | 1010 | text = sampleClassifyText(text); |
| 1009 | 1011 | |
| 1012 | + filterAvailable ??= extension_settings.expressions.filterAvailable; | |
| 1013 | + if (filterAvailable && ![EXPRESSION_API.llm, EXPRESSION_API.webllm].includes(expressionsApi)) { | |
| 1014 | + console.debug('Filter available is only supported for LLM and WebLLM expressions'); | |
| 1015 | + } | |
| 1016 | + | |
| 1010 | 1017 | try { |
| 1011 | 1018 | switch (expressionsApi) { |
| 1012 | 1019 | // Local BERT pipeline |
| @@ -1031,7 +1038,7 @@ export async function getExpressionLabel(text, expressionsApi = extension_settin | ||
| 1031 | 1038 | return extension_settings.expressions.fallback_expression; |
| 1032 | 1039 | } |
| 1033 | 1040 | |
| 1034 | 1041 | const expressionsList = await getExpressionsList({ filterAvailable: filterAvailable }); |
| 1035 | 1042 | const prompt = substituteParamsExtended(customPrompt, { labels: expressionsList }) || await getLlmPrompt(expressionsList); |
| 1036 | 1043 | eventSource.once(event_types.TEXT_COMPLETION_SETTINGS_READY, onTextGenSettingsReady); |
| 1037 | 1044 | const emotionResponse = await generateRaw(text, main_api, false, false, prompt); |
| @@ -1044,7 +1051,7 @@ export async function getExpressionLabel(text, expressionsApi = extension_settin | ||
| 1044 | 1051 | return extension_settings.expressions.fallback_expression; |
| 1045 | 1052 | } |
| 1046 | 1053 | |
| 1047 | 1054 | const expressionsList = await getExpressionsList({ filterAvailable: filterAvailable }); |
| 1048 | 1055 | const prompt = substituteParamsExtended(customPrompt, { labels: expressionsList }) || await getLlmPrompt(expressionsList); |
| 1049 | 1056 | const messages = [ |
| 1050 | 1057 | { role: 'user', content: text + '\n\n' + prompt }, |
| @@ -1324,12 +1331,28 @@ function getCachedExpressions() { | ||
| 1324 | 1331 | return [...expressionsList, ...extension_settings.expressions.custom].filter(onlyUnique); |
| 1325 | 1332 | } |
| 1326 | 1333 | |
| 1327 | 1334 | export async function getExpressionsList({ filterAvailable = false } = {}) { |
| 1328 | 1335 | // ReturnIf there is no cached list, ifload availableand cache it |
| 1329 | 1336 | if (!Array.isArray(expressionsList)) { |
| 1330 | 1337 | returnexpressionsList getCachedExpressions= await resolveExpressionsList(); |
| 1338 | + } | |
| 1339 | + | |
| 1340 | + const expressions = getCachedExpressions(); | |
| 1341 | + | |
| 1342 | + // Filtering is only available for llm and webllm APIs | |
| 1343 | + if (!filterAvailable || ![EXPRESSION_API.llm, EXPRESSION_API.webllm].includes(extension_settings.expressions.api)) { | |
| 1344 | + return expressions; | |
| 1331 | 1345 | } |
| 1332 | 1346 | |
| 1347 | + // Get expressions with available sprites | |
| 1348 | + const currentLastMessage = selected_group ? getLastCharacterMessage() : null; | |
| 1349 | + const spriteFolderName = getSpriteFolderName(currentLastMessage, currentLastMessage?.name); | |
| 1350 | + | |
| 1351 | + return expressions.filter(label => { | |
| 1352 | + const expression = spriteCache[spriteFolderName]?.find(x => x.label === label); | |
| 1353 | + return (expression?.files.length ?? 0) > 0; | |
| 1354 | + }); | |
| 1355 | + | |
| 1333 | 1356 | /** |
| 1334 | 1357 | * Returns the list of expressions from the API or fallback in offline mode. |
| 1335 | 1358 | * @returns {Promise<string[]>} |
| @@ -1376,9 +1399,6 @@ export async function getExpressionsList() { | ||
| 1376 | 1399 | expressionsList = DEFAULT_EXPRESSIONS.slice(); |
| 1377 | 1400 | return expressionsList; |
| 1378 | 1401 | } |
| 1379 | - | |
| 1380 | - const result = await resolveExpressionsList(); | |
| 1381 | - return [...result, ...extension_settings.expressions.custom].filter(onlyUnique); | |
| 1382 | 1402 | } |
| 1383 | 1403 | |
| 1384 | 1404 | /** |
| @@ -2106,6 +2126,10 @@ function migrateSettings() { | ||
| 2106 | 2126 | extension_settings.expressions.rerollIfSame = !!$(this).prop('checked'); |
| 2107 | 2127 | saveSettingsDebounced(); |
| 2108 | 2128 | }); |
| 2129 | + $('#expressions_filter_available').prop('checked', extension_settings.expressions.filterAvailable).on('input', function () { | |
| 2130 | + extension_settings.expressions.filterAvailable = !!$(this).prop('checked'); | |
| 2131 | + saveSettingsDebounced(); | |
| 2132 | + }); | |
| 2109 | 2133 | $('#expression_override_cleanup_button').on('click', onClickExpressionOverrideRemoveAllButton); |
| 2110 | 2134 | $(document).on('dragstart', '.expression', (e) => { |
| 2111 | 2135 | e.preventDefault(); |
| @@ -2283,13 +2307,13 @@ function migrateSettings() { | ||
| 2283 | 2307 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ |
| 2284 | 2308 | name: 'expression-list', |
| 2285 | 2309 | aliases: ['expressions'], |
| 2286 | 2310 | /** @type {(args: {return: string, filter: string}) => Promise<string>} */ |
| 2287 | 2311 | callback: async (args) => { |
| 2288 | 2312 | let returnType = |
| 2289 | 2313 | /** @type {import('../../slash-commands/SlashCommandReturnHelper.js').SlashCommandReturnType} */ |
| 2290 | 2314 | (args.return); |
| 2291 | 2315 | |
| 2292 | 2316 | const list = await getExpressionsList({ filterAvailable: !isFalseBoolean(args.filter) }); |
| 2293 | 2317 | |
| 2294 | 2318 | return await slashCommandReturnHelper.doReturn(returnType ?? 'pipe', list, { objectToStringFunc: list => list.join(', ') }); |
| 2295 | 2319 | }, |
| @@ -2302,6 +2326,13 @@ function migrateSettings() { | ||
| 2302 | 2326 | enumList: slashCommandReturnHelper.enumList({ allowObject: true }), |
| 2303 | 2327 | forceEnum: true, |
| 2304 | 2328 | }), |
| 2329 | + SlashCommandNamedArgument.fromProps({ | |
| 2330 | + name: 'filter', | |
| 2331 | + description: 'Filter the list to only include expressions that have available sprites for the current character.', | |
| 2332 | + typeList: [ARGUMENT_TYPE.BOOLEAN], | |
| 2333 | + enumList: commonEnumProviders.boolean('trueFalse')(), | |
| 2334 | + defaultValue: 'true', | |
| 2335 | + }), | |
| 2305 | 2336 | ], |
| 2306 | 2337 | returns: 'The comma-separated list of available expressions, including custom expressions.', |
| 2307 | 2338 | helpString: 'Returns a list of available expressions, including custom expressions.', |
| @@ -2318,6 +2349,13 @@ function migrateSettings() { | ||
| 2318 | 2349 | enumList: Object.keys(EXPRESSION_API).map(api => new SlashCommandEnumValue(api, null, enumTypes.enum)), |
| 2319 | 2350 | }), |
| 2320 | 2351 | SlashCommandNamedArgument.fromProps({ |
| 2352 | + name: 'filter', | |
| 2353 | + description: 'Filter the list to only include expressions that have available sprites for the current character.', | |
| 2354 | + typeList: [ARGUMENT_TYPE.BOOLEAN], | |
| 2355 | + enumList: commonEnumProviders.boolean('trueFalse')(), | |
| 2356 | + defaultValue: 'true', | |
| 2357 | + }), | |
| 2358 | + SlashCommandNamedArgument.fromProps({ | |
| 2321 | 2359 | name: 'prompt', |
| 2322 | 2360 | description: 'Custom prompt for classification. Only relevant if Classifier API is set to LLM.', |
| 2323 | 2361 | typeList: [ARGUMENT_TYPE.STRING], |
| @@ -29,7 +29,11 @@ | ||
| 29 | 29 | </select> |
| 30 | 30 | </div> |
| 31 | 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 | 37 | <span data-i18n="LLM Prompt">LLM Prompt</span> |
| 34 | 38 | <div id="expression_llm_prompt_restore" title="Restore default value" class="right_menu_button"> |
| 35 | 39 | <i class="fa-solid fa-clock-rotate-left fa-sm"></i> |