Merge branch 'staging' into or-prompt-post-processing

9b76b6dd3c6f07b367ee50fb6a543105a414e4c4

Cohee <18619528+Cohee1207@users.noreply.github.com>

6 files changed, +97 -34Showing whitespace changes
.github/workflows/pr-auto-manager.yml+10 -2
@@ -13,9 +13,11 @@ permissions:
1313jobs:
1414 label-by-size:
1515 name: 🏷️ Label PR by Size
16+ # This job should run after all others, to prevent possible concurrency issues
17+ needs: [label-by-branches, label-by-files, remove-stale-label, check-merge-blocking-labels, write-auto-comments]
1618 runs-on: ubuntu-latest
1719 # Only needs to run when code is changed
1820 if: always() && (github.event.action == 'opened' || github.event.action == 'synchronize')
1921
2022 # Override permissions, the labeler needs issues write access
2123 permissions:
@@ -159,7 +161,7 @@ jobs:
159161
160162 write-auto-comments:
161163 name: 💬 Post PR Comments Based on Labels
162164 needs: [label-by-size, label-by-branches, label-by-files]
163165 runs-on: ubuntu-latest
164166 # Run, even if the previous jobs were skipped/failed
165167 if: always()
@@ -184,6 +186,12 @@ jobs:
184186 runs-on: ubuntu-latest
185187 if: github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'staging'
186188
189+ # Override permissions, We need to be able to write to issues
190+ permissions:
191+ contents: read
192+ issues: write
193+ pull-requests: write
194+
187195 steps:
188196 - name: Extract Linked Issues From PR Description
189197 id: extract_issues
public/script.js+10 -3
@@ -495,6 +495,8 @@ export const event_types = {
495495 GENERATE_AFTER_COMBINE_PROMPTS: 'generate_after_combine_prompts',
496496 GENERATE_AFTER_DATA: 'generate_after_data',
497497 GROUP_MEMBER_DRAFTED: 'group_member_drafted',
498+ GROUP_WRAPPER_STARTED: 'group_wrapper_started',
499+ GROUP_WRAPPER_FINISHED: 'group_wrapper_finished',
498500 WORLD_INFO_ACTIVATED: 'world_info_activated',
499501 TEXT_COMPLETION_SETTINGS_READY: 'text_completion_settings_ready',
500502 CHAT_COMPLETION_SETTINGS_READY: 'chat_completion_settings_ready',
@@ -3098,6 +3100,9 @@ export function baseChatReplace(value, name1, name2) {
30983100
30993101/**
31003102 * Returns the character card fields for the current character.
3103+ * @param {object} [options]
3104+ * @param {number} [options.chid] Optional character index
3105+ *
31013106 * @typedef {object} CharacterCardFields
31023107 * @property {string} system System prompt
31033108 * @property {string} mesExamples Message examples
@@ -3110,7 +3115,9 @@ export function baseChatReplace(value, name1, name2) {
31103115 * @property {string} charDepthPrompt Character depth note
31113116 * @returns {CharacterCardFields} Character card fields
31123117 */
31133118export function getCharacterCardFields({ chid = null } = {}) {
3119+ const currentChid = chid ?? this_chid;
3120+
31143121 const result = {
31153122 system: '',
31163123 mesExamples: '',
@@ -3124,7 +3131,7 @@ export function getCharacterCardFields() {
31243131 };
31253132 result.persona = baseChatReplace(power_user.persona_description?.trim(), name1, name2);
31263133
31273134 const character = characters[this_chidcurrentChid];
31283135
31293136 if (!character) {
31303137 return result;
@@ -3141,7 +3148,7 @@ export function getCharacterCardFields() {
31413148 result.charDepthPrompt = baseChatReplace(character.data?.extensions?.depth_prompt?.prompt?.trim(), name1, name2);
31423149
31433150 if (selected_group) {
31443151 const groupCards = getGroupCharacterCards(selected_group, Number(this_chidcurrentChid));
31453152
31463153 if (groupCards) {
31473154 result.description = groupCards.description;
public/scripts/extensions/expressions/index.js+58 -16
@@ -4,7 +4,7 @@ import { characters, eventSource, event_types, generateRaw, getRequestHeaders, m
44import { dragElement, isMobile } from '../../RossAscends-mods.js';
55import { getContext, getApiUrl, modules, extension_settings, ModuleWorkerWrapper, doExtrasFetch, renderExtensionTemplateAsync } from '../../extensions.js';
66import { loadMovingUIState, performFuzzySearch, power_user } from '../../power-user.js';
77import { onlyUnique, debounce, getCharaFilename, trimToEndSentence, trimToStartSentence, waitUntilCondition, findChar, isFalseBoolean } from '../../utils.js';
88import { hideMutedSprites, selected_group } from '../../group-chats.js';
99import { isJsonSchemaSupported } from '../../textgen-settings.js';
1010import { debounce_timeout } from '../../constants.js';
@@ -17,6 +17,7 @@ import { slashCommandReturnHelper } from '../../slash-commands/SlashCommandRetur
1717import { generateWebLlmChatPrompt, isWebLlmSupported } from '../shared.js';
1818import { Popup, POPUP_RESULT } from '../../popup.js';
1919import { t } from '../../i18n.js';
20+import { removeReasoningFromString } from '../../reasoning.js';
2021export { MODULE_NAME };
2122
2223/**
@@ -678,7 +679,7 @@ async function setSpriteFolderCommand(_, folder) {
678679 return '';
679680}
680681
681682async function classifyCallback(/** @type {{api: string?, filter: string?, prompt: string?}} */ { api = null, filter = null, prompt = null }, text) {
682683 if (!text) {
683684 toastr.error('No text provided');
684685 return '';
@@ -689,13 +690,14 @@ async function classifyCallback(/** @type {{api: string?, prompt: string?}} */ {
689690 }
690691
691692 const expressionApi = EXPRESSION_API[api] || extension_settings.expressions.api;
693+ const filterAvailable = !isFalseBoolean(filter);
692694
693695 if (!modules.includes('classify') && expressionApi == EXPRESSION_API.extras) {
694696 toastr.warning('Text classification is disabled or not available');
695697 return '';
696698 }
697699
698700 const label = await getExpressionLabel(text, expressionApi, { filterAvailable: filterAvailable, customPrompt: prompt });
699701 console.debug(`Classification result for "${text}": ${label}`);
700702 return label;
701703}
@@ -928,6 +930,9 @@ function parseLlmResponse(emotionResponse, labels) {
928930
929931 return response;
930932 } catch {
933+ // Clean possible reasoning from response
934+ emotionResponse = removeReasoningFromString(emotionResponse);
935+
931936 const fuse = new Fuse(labels, { includeScore: true });
932937 console.debug('Using fuzzy search in labels:', labels);
933938 const result = fuse.search(emotionResponse);
@@ -988,10 +993,11 @@ function onTextGenSettingsReady(args) {
988993 * @param {string} text - The text to classify and retrieve the expression label for.
989994 * @param {EXPRESSION_API} [expressionsApi=extension_settings.expressions.api] - The expressions API to use for classification.
990995 * @param {object} [options={}] - Optional arguments.
996+ * @param {boolean?} [options.filterAvailable=null] - Whether to filter available expressions. If not specified, uses the extension setting.
991997 * @param {string?} [options.customPrompt=null] - The custom prompt to use for classification.
992998 * @returns {Promise<string?>} - The label of the expression.
993999 */
9941000export async function getExpressionLabel(text, expressionsApi = extension_settings.expressions.api, { filterAvailable = null, customPrompt = null } = {}) {
9951001 // Return if text is undefined, saving a costly fetch request
9961002 if ((!modules.includes('classify') && expressionsApi == EXPRESSION_API.extras) || !text) {
9971003 return extension_settings.expressions.fallback_expression;
@@ -1003,6 +1009,11 @@ export async function getExpressionLabel(text, expressionsApi = extension_settin
10031009
10041010 text = sampleClassifyText(text);
10051011
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+
10061017 try {
10071018 switch (expressionsApi) {
10081019 // Local BERT pipeline
@@ -1027,7 +1038,7 @@ export async function getExpressionLabel(text, expressionsApi = extension_settin
10271038 return extension_settings.expressions.fallback_expression;
10281039 }
10291040
10301041 const expressionsList = await getExpressionsList({ filterAvailable: filterAvailable });
10311042 const prompt = substituteParamsExtended(customPrompt, { labels: expressionsList }) || await getLlmPrompt(expressionsList);
10321043 eventSource.once(event_types.TEXT_COMPLETION_SETTINGS_READY, onTextGenSettingsReady);
10331044 const emotionResponse = await generateRaw(text, main_api, false, false, prompt);
@@ -1040,7 +1051,7 @@ export async function getExpressionLabel(text, expressionsApi = extension_settin
10401051 return extension_settings.expressions.fallback_expression;
10411052 }
10421053
10431054 const expressionsList = await getExpressionsList({ filterAvailable: filterAvailable });
10441055 const prompt = substituteParamsExtended(customPrompt, { labels: expressionsList }) || await getLlmPrompt(expressionsList);
10451056 const messages = [
10461057 { role: 'user', content: text + '\n\n' + prompt },
@@ -1320,12 +1331,28 @@ function getCachedExpressions() {
13201331 return [...expressionsList, ...extension_settings.expressions.custom].filter(onlyUnique);
13211332}
13221333
13231334export async function getExpressionsList({ filterAvailable = false } = {}) {
13241335 // ReturnIf there is no cached list, ifload availableand cache it
13251336 if (!Array.isArray(expressionsList)) {
13261337 returnexpressionsList getCachedExpressions= await resolveExpressionsList();
13271338 }
13281339
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;
1345+ }
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+
13291356 /**
13301357 * Returns the list of expressions from the API or fallback in offline mode.
13311358 * @returns {Promise<string[]>}
@@ -1372,9 +1399,6 @@ export async function getExpressionsList() {
13721399 expressionsList = DEFAULT_EXPRESSIONS.slice();
13731400 return expressionsList;
13741401 }
1375-
1376- const result = await resolveExpressionsList();
1377- return [...result, ...extension_settings.expressions.custom].filter(onlyUnique);
13781402}
13791403
13801404/**
@@ -1810,7 +1834,7 @@ async function onClickExpressionUpload(event) {
18101834 }
18111835 }
18121836 } else {
18131837 spriteName = withoutExtension(clickedFileNameexpression);
18141838 }
18151839
18161840 if (!spriteName) {
@@ -2102,6 +2126,10 @@ function migrateSettings() {
21022126 extension_settings.expressions.rerollIfSame = !!$(this).prop('checked');
21032127 saveSettingsDebounced();
21042128 });
2129+ $('#expressions_filter_available').prop('checked', extension_settings.expressions.filterAvailable).on('input', function () {
2130+ extension_settings.expressions.filterAvailable = !!$(this).prop('checked');
2131+ saveSettingsDebounced();
2132+ });
21052133 $('#expression_override_cleanup_button').on('click', onClickExpressionOverrideRemoveAllButton);
21062134 $(document).on('dragstart', '.expression', (e) => {
21072135 e.preventDefault();
@@ -2279,13 +2307,13 @@ function migrateSettings() {
22792307 SlashCommandParser.addCommandObject(SlashCommand.fromProps({
22802308 name: 'expression-list',
22812309 aliases: ['expressions'],
22822310 /** @type {(args: {return: string, filter: string}) => Promise<string>} */
22832311 callback: async (args) => {
22842312 let returnType =
22852313 /** @type {import('../../slash-commands/SlashCommandReturnHelper.js').SlashCommandReturnType} */
22862314 (args.return);
22872315
22882316 const list = await getExpressionsList({ filterAvailable: !isFalseBoolean(args.filter) });
22892317
22902318 return await slashCommandReturnHelper.doReturn(returnType ?? 'pipe', list, { objectToStringFunc: list => list.join(', ') });
22912319 },
@@ -2298,6 +2326,13 @@ function migrateSettings() {
22982326 enumList: slashCommandReturnHelper.enumList({ allowObject: true }),
22992327 forceEnum: true,
23002328 }),
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+ }),
23012336 ],
23022337 returns: 'The comma-separated list of available expressions, including custom expressions.',
23032338 helpString: 'Returns a list of available expressions, including custom expressions.',
@@ -2314,6 +2349,13 @@ function migrateSettings() {
23142349 enumList: Object.keys(EXPRESSION_API).map(api => new SlashCommandEnumValue(api, null, enumTypes.enum)),
23152350 }),
23162351 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({
23172359 name: 'prompt',
23182360 description: 'Custom prompt for classification. Only relevant if Classifier API is set to LLM.',
23192361 typeList: [ARGUMENT_TYPE.STRING],
public/scripts/extensions/expressions/settings.html+5 -1
@@ -29,7 +29,11 @@
2929 </select>
3030 </div>
3131 <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">
3337 <span data-i18n="LLM Prompt">LLM Prompt</span>
3438 <div id="expression_llm_prompt_restore" title="Restore default value" class="right_menu_button">
3539 <i class="fa-solid fa-clock-rotate-left fa-sm"></i>
public/scripts/group-chats.js+10 -7
@@ -435,16 +435,18 @@ export function getGroupCharacterCards(groupId, characterId) {
435435 * @param {string} value Value to replace
436436 * @param {string} fieldName Name of the field
437437 * @param {string} characterName Name of the character
438+ * @param {boolean} trim Whether to trim the value
438439 * @returns {string} Replaced text
439440 * */
440441 function customBaseChatReplace(value, fieldName, characterName, trim) {
441442 if (!value) {
442443 return '';
443444 }
444445
445446 // We should do the custom field name replacement first, and then run it through the normal macro engine with provided names
446447 value = value.replace(/<FIELDNAME>/gi, fieldName);
447448 returnvalue baseChatReplace(= trim ? value.trim(), name1,: characterName)value;
449+ return baseChatReplace(value, name1, characterName);
448450 }
449451
450452 /**
@@ -467,13 +469,12 @@ export function getGroupCharacterCards(groupId, characterId) {
467469 }
468470
469471 // Prepare and replace prefixes
470472 const prefix = customBaseChatReplace(group.generation_mode_join_prefix, fieldName, characterName, false);
471473 const suffix = customBaseChatReplace(group.generation_mode_join_suffix, fieldName, characterName, false);
472- const separator = power_user.instruct.wrap ? '\n' : '';
473474 // Also run the macro replacement on the actual content
474475 value = customBaseChatReplace(value, fieldName, characterName, true);
475476
476477 return `${prefix ? prefix + separator : ''}${value}${suffix ? separator + suffix : ''}`;
477478 }
478479
479480 const scenarioOverride = chat_metadata['scenario'];
@@ -904,6 +905,7 @@ async function generateGroupWrapper(by_auto_mode, type = null, params = {}) {
904905 groupChatQueueOrder.set(characters[activatedMembers[i]].avatar, i + 1);
905906 }
906907 }
908+ await eventSource.emit(event_types.GROUP_WRAPPER_STARTED, { selected_group, type });
907909 // now the real generation begins: cycle through every activated character
908910 for (const chId of activatedMembers) {
909911 throwIfAborted();
@@ -942,6 +944,7 @@ async function generateGroupWrapper(by_auto_mode, type = null, params = {}) {
942944 setCharacterName('');
943945 activateSendButtons();
944946 showSwipeButtons();
947+ await eventSource.emit(event_types.GROUP_WRAPPER_FINISHED, { selected_group, type });
945948 }
946949
947950 return Promise.resolve(textResult);
src/endpoints/chats.js+4 -5
@@ -783,12 +783,11 @@ router.post('/search', validateAvatarUrlMiddleware, function (request, response)
783783 continue;
784784 }
785785
786786 // Search through title and messages of the chat
787787 const fragments = query.trim().toLowerCase().split(/\s+/).filter(x => x);
788- const hasMatch = messages.some(message => {
788+ const text = [chatFile.path.split(/[\\/]/).pop().replace(/.jsonl$/, ''),
789789 const text...messages.map(message => message?.mes?)].join('\n').toLowerCase();
790790 return const texthasMatch &&= fragments.every(fragment => text.includes(fragment));
791- });
792791
793792 if (hasMatch) {
794793 results.push({