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 | 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, isTrueBoolean } 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'; |
| @@ -678,7 +678,7 @@ async function setSpriteFolderCommand(_, folder) { | ||
| 678 | 678 | return ''; |
| 679 | 679 | } |
| 680 | 680 | |
| 681 | 681 | async function classifyCallback(/** @type {{api: string?, filter: string?, prompt: string?}} */ { api = null, filter = null, prompt = null }, text) { |
| 682 | 682 | if (!text) { |
| 683 | 683 | toastr.error('No text provided'); |
| 684 | 684 | return ''; |
| @@ -689,13 +689,14 @@ async function classifyCallback(/** @type {{api: string?, prompt: string?}} */ { | ||
| 689 | 689 | } |
| 690 | 690 | |
| 691 | 691 | const expressionApi = EXPRESSION_API[api] || extension_settings.expressions.api; |
| 692 | + const filterAvailable = isTrueBoolean(filter); | |
| 692 | 693 | |
| 693 | 694 | if (!modules.includes('classify') && expressionApi == EXPRESSION_API.extras) { |
| 694 | 695 | toastr.warning('Text classification is disabled or not available'); |
| 695 | 696 | return ''; |
| 696 | 697 | } |
| 697 | 698 | |
| 698 | 699 | const label = await getExpressionLabel(text, expressionApi, { filterAvailable: filterAvailable, customPrompt: prompt }); |
| 699 | 700 | console.debug(`Classification result for "${text}": ${label}`); |
| 700 | 701 | return label; |
| 701 | 702 | } |
| @@ -988,10 +989,11 @@ function onTextGenSettingsReady(args) { | ||
| 988 | 989 | * @param {string} text - The text to classify and retrieve the expression label for. |
| 989 | 990 | * @param {EXPRESSION_API} [expressionsApi=extension_settings.expressions.api] - The expressions API to use for classification. |
| 990 | 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 | 993 | * @param {string?} [options.customPrompt=null] - The custom prompt to use for classification. |
| 992 | 994 | * @returns {Promise<string?>} - The label of the expression. |
| 993 | 995 | */ |
| 994 | 996 | export async function getExpressionLabel(text, expressionsApi = extension_settings.expressions.api, { filterAvailable = null, customPrompt = null } = {}) { |
| 995 | 997 | // Return if text is undefined, saving a costly fetch request |
| 996 | 998 | if ((!modules.includes('classify') && expressionsApi == EXPRESSION_API.extras) || !text) { |
| 997 | 999 | return extension_settings.expressions.fallback_expression; |
| @@ -1003,6 +1005,11 @@ export async function getExpressionLabel(text, expressionsApi = extension_settin | ||
| 1003 | 1005 | |
| 1004 | 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 | 1013 | try { |
| 1007 | 1014 | switch (expressionsApi) { |
| 1008 | 1015 | // Local BERT pipeline |
| @@ -1027,7 +1034,7 @@ export async function getExpressionLabel(text, expressionsApi = extension_settin | ||
| 1027 | 1034 | return extension_settings.expressions.fallback_expression; |
| 1028 | 1035 | } |
| 1029 | 1036 | |
| 1030 | 1037 | const expressionsList = await getExpressionsList({ filterAvailable: filterAvailable }); |
| 1031 | 1038 | const prompt = substituteParamsExtended(customPrompt, { labels: expressionsList }) || await getLlmPrompt(expressionsList); |
| 1032 | 1039 | eventSource.once(event_types.TEXT_COMPLETION_SETTINGS_READY, onTextGenSettingsReady); |
| 1033 | 1040 | const emotionResponse = await generateRaw(text, main_api, false, false, prompt); |
| @@ -1040,7 +1047,7 @@ export async function getExpressionLabel(text, expressionsApi = extension_settin | ||
| 1040 | 1047 | return extension_settings.expressions.fallback_expression; |
| 1041 | 1048 | } |
| 1042 | 1049 | |
| 1043 | 1050 | const expressionsList = await getExpressionsList({ filterAvailable: filterAvailable }); |
| 1044 | 1051 | const prompt = substituteParamsExtended(customPrompt, { labels: expressionsList }) || await getLlmPrompt(expressionsList); |
| 1045 | 1052 | const messages = [ |
| 1046 | 1053 | { role: 'user', content: text + '\n\n' + prompt }, |
| @@ -1320,12 +1327,28 @@ function getCachedExpressions() { | ||
| 1320 | 1327 | return [...expressionsList, ...extension_settings.expressions.custom].filter(onlyUnique); |
| 1321 | 1328 | } |
| 1322 | 1329 | |
| 1323 | 1330 | export async function getExpressionsList({ filterAvailable = false } = {}) { |
| 1324 | 1331 | // ReturnIf there is no cached list, ifload availableand cache it |
| 1325 | 1332 | if (!Array.isArray(expressionsList)) { |
| 1326 | 1333 | returnexpressionsList getCachedExpressions= 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 | 1353 | * Returns the list of expressions from the API or fallback in offline mode. |
| 1331 | 1354 | * @returns {Promise<string[]>} |
| @@ -1372,9 +1395,6 @@ export async function getExpressionsList() { | ||
| 1372 | 1395 | expressionsList = DEFAULT_EXPRESSIONS.slice(); |
| 1373 | 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 | 2122 | extension_settings.expressions.rerollIfSame = !!$(this).prop('checked'); |
| 2103 | 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 | 2129 | $('#expression_override_cleanup_button').on('click', onClickExpressionOverrideRemoveAllButton); |
| 2106 | 2130 | $(document).on('dragstart', '.expression', (e) => { |
| 2107 | 2131 | e.preventDefault(); |
| @@ -2279,13 +2303,13 @@ function migrateSettings() { | ||
| 2279 | 2303 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ |
| 2280 | 2304 | name: 'expression-list', |
| 2281 | 2305 | aliases: ['expressions'], |
| 2282 | 2306 | /** @type {(args: {return: string, filterAvailable: string}) => Promise<string>} */ |
| 2283 | 2307 | callback: async (args) => { |
| 2284 | 2308 | let returnType = |
| 2285 | 2309 | /** @type {import('../../slash-commands/SlashCommandReturnHelper.js').SlashCommandReturnType} */ |
| 2286 | 2310 | (args.return); |
| 2287 | 2311 | |
| 2288 | 2312 | const list = await getExpressionsList({ filterAvailable: isTrueBoolean(args.filterAvailable) }); |
| 2289 | 2313 | |
| 2290 | 2314 | return await slashCommandReturnHelper.doReturn(returnType ?? 'pipe', list, { objectToStringFunc: list => list.join(', ') }); |
| 2291 | 2315 | }, |
| @@ -2298,6 +2322,13 @@ function migrateSettings() { | ||
| 2298 | 2322 | enumList: slashCommandReturnHelper.enumList({ allowObject: true }), |
| 2299 | 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 | 2333 | returns: 'The comma-separated list of available expressions, including custom expressions.', |
| 2303 | 2334 | helpString: 'Returns a list of available expressions, including custom expressions.', |
| @@ -2314,6 +2345,13 @@ function migrateSettings() { | ||
| 2314 | 2345 | enumList: Object.keys(EXPRESSION_API).map(api => new SlashCommandEnumValue(api, null, enumTypes.enum)), |
| 2315 | 2346 | }), |
| 2316 | 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 | 2355 | name: 'prompt', |
| 2318 | 2356 | description: 'Custom prompt for classification. Only relevant if Classifier API is set to LLM.', |
| 2319 | 2357 | 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> |