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

This commit is contained in:
Cohee
2025-03-19 19:32:55 +02:00
6 changed files with 97 additions and 34 deletions

View File

@@ -13,9 +13,11 @@ permissions:
jobs: jobs:
label-by-size: label-by-size:
name: 🏷️ Label PR by Size name: 🏷️ Label PR by Size
# This job should run after all others, to prevent possible concurrency issues
needs: [label-by-branches, label-by-files, remove-stale-label, check-merge-blocking-labels, write-auto-comments]
runs-on: ubuntu-latest runs-on: ubuntu-latest
# Only needs to run when code is changed # Only needs to run when code is changed
if: github.event.action == 'opened' || github.event.action == 'synchronize' if: always() && (github.event.action == 'opened' || github.event.action == 'synchronize')
# Override permissions, the labeler needs issues write access # Override permissions, the labeler needs issues write access
permissions: permissions:
@@ -159,7 +161,7 @@ jobs:
write-auto-comments: write-auto-comments:
name: 💬 Post PR Comments Based on Labels name: 💬 Post PR Comments Based on Labels
needs: [label-by-size, label-by-branches, label-by-files] needs: [label-by-branches, label-by-files]
runs-on: ubuntu-latest runs-on: ubuntu-latest
# Run, even if the previous jobs were skipped/failed # Run, even if the previous jobs were skipped/failed
if: always() if: always()
@@ -184,6 +186,12 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
if: github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'staging' if: github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'staging'
# Override permissions, We need to be able to write to issues
permissions:
contents: read
issues: write
pull-requests: write
steps: steps:
- name: Extract Linked Issues From PR Description - name: Extract Linked Issues From PR Description
id: extract_issues id: extract_issues

View File

@@ -495,6 +495,8 @@ export const event_types = {
GENERATE_AFTER_COMBINE_PROMPTS: 'generate_after_combine_prompts', GENERATE_AFTER_COMBINE_PROMPTS: 'generate_after_combine_prompts',
GENERATE_AFTER_DATA: 'generate_after_data', GENERATE_AFTER_DATA: 'generate_after_data',
GROUP_MEMBER_DRAFTED: 'group_member_drafted', GROUP_MEMBER_DRAFTED: 'group_member_drafted',
GROUP_WRAPPER_STARTED: 'group_wrapper_started',
GROUP_WRAPPER_FINISHED: 'group_wrapper_finished',
WORLD_INFO_ACTIVATED: 'world_info_activated', WORLD_INFO_ACTIVATED: 'world_info_activated',
TEXT_COMPLETION_SETTINGS_READY: 'text_completion_settings_ready', TEXT_COMPLETION_SETTINGS_READY: 'text_completion_settings_ready',
CHAT_COMPLETION_SETTINGS_READY: 'chat_completion_settings_ready', CHAT_COMPLETION_SETTINGS_READY: 'chat_completion_settings_ready',
@@ -3098,6 +3100,9 @@ export function baseChatReplace(value, name1, name2) {
/** /**
* Returns the character card fields for the current character. * Returns the character card fields for the current character.
* @param {object} [options]
* @param {number} [options.chid] Optional character index
*
* @typedef {object} CharacterCardFields * @typedef {object} CharacterCardFields
* @property {string} system System prompt * @property {string} system System prompt
* @property {string} mesExamples Message examples * @property {string} mesExamples Message examples
@@ -3110,7 +3115,9 @@ export function baseChatReplace(value, name1, name2) {
* @property {string} charDepthPrompt Character depth note * @property {string} charDepthPrompt Character depth note
* @returns {CharacterCardFields} Character card fields * @returns {CharacterCardFields} Character card fields
*/ */
export function getCharacterCardFields() { export function getCharacterCardFields({ chid = null } = {}) {
const currentChid = chid ?? this_chid;
const result = { const result = {
system: '', system: '',
mesExamples: '', mesExamples: '',
@@ -3124,7 +3131,7 @@ export function getCharacterCardFields() {
}; };
result.persona = baseChatReplace(power_user.persona_description?.trim(), name1, name2); result.persona = baseChatReplace(power_user.persona_description?.trim(), name1, name2);
const character = characters[this_chid]; const character = characters[currentChid];
if (!character) { if (!character) {
return result; return result;
@@ -3141,7 +3148,7 @@ export function getCharacterCardFields() {
result.charDepthPrompt = baseChatReplace(character.data?.extensions?.depth_prompt?.prompt?.trim(), name1, name2); result.charDepthPrompt = baseChatReplace(character.data?.extensions?.depth_prompt?.prompt?.trim(), name1, name2);
if (selected_group) { if (selected_group) {
const groupCards = getGroupCharacterCards(selected_group, Number(this_chid)); const groupCards = getGroupCharacterCards(selected_group, Number(currentChid));
if (groupCards) { if (groupCards) {
result.description = groupCards.description; result.description = groupCards.description;

View File

@@ -4,7 +4,7 @@ import { characters, eventSource, event_types, generateRaw, getRequestHeaders, m
import { dragElement, isMobile } from '../../RossAscends-mods.js'; import { dragElement, isMobile } from '../../RossAscends-mods.js';
import { getContext, getApiUrl, modules, extension_settings, ModuleWorkerWrapper, doExtrasFetch, renderExtensionTemplateAsync } from '../../extensions.js'; import { getContext, getApiUrl, modules, extension_settings, ModuleWorkerWrapper, doExtrasFetch, renderExtensionTemplateAsync } from '../../extensions.js';
import { loadMovingUIState, performFuzzySearch, power_user } from '../../power-user.js'; import { loadMovingUIState, performFuzzySearch, power_user } from '../../power-user.js';
import { onlyUnique, debounce, getCharaFilename, trimToEndSentence, trimToStartSentence, waitUntilCondition, findChar } from '../../utils.js'; import { onlyUnique, debounce, getCharaFilename, trimToEndSentence, trimToStartSentence, waitUntilCondition, findChar, isFalseBoolean } from '../../utils.js';
import { hideMutedSprites, selected_group } from '../../group-chats.js'; import { hideMutedSprites, selected_group } from '../../group-chats.js';
import { isJsonSchemaSupported } from '../../textgen-settings.js'; import { isJsonSchemaSupported } from '../../textgen-settings.js';
import { debounce_timeout } from '../../constants.js'; import { debounce_timeout } from '../../constants.js';
@@ -17,6 +17,7 @@ import { slashCommandReturnHelper } from '../../slash-commands/SlashCommandRetur
import { generateWebLlmChatPrompt, isWebLlmSupported } from '../shared.js'; import { generateWebLlmChatPrompt, isWebLlmSupported } from '../shared.js';
import { Popup, POPUP_RESULT } from '../../popup.js'; import { Popup, POPUP_RESULT } from '../../popup.js';
import { t } from '../../i18n.js'; import { t } from '../../i18n.js';
import { removeReasoningFromString } from '../../reasoning.js';
export { MODULE_NAME }; export { MODULE_NAME };
/** /**
@@ -678,7 +679,7 @@ async function setSpriteFolderCommand(_, folder) {
return ''; return '';
} }
async function classifyCallback(/** @type {{api: string?, prompt: string?}} */ { api = null, prompt = null }, text) { async function classifyCallback(/** @type {{api: string?, filter: string?, prompt: string?}} */ { api = null, filter = null, prompt = null }, text) {
if (!text) { if (!text) {
toastr.error('No text provided'); toastr.error('No text provided');
return ''; return '';
@@ -689,13 +690,14 @@ async function classifyCallback(/** @type {{api: string?, prompt: string?}} */ {
} }
const expressionApi = EXPRESSION_API[api] || extension_settings.expressions.api; const expressionApi = EXPRESSION_API[api] || extension_settings.expressions.api;
const filterAvailable = !isFalseBoolean(filter);
if (!modules.includes('classify') && expressionApi == EXPRESSION_API.extras) { if (!modules.includes('classify') && expressionApi == EXPRESSION_API.extras) {
toastr.warning('Text classification is disabled or not available'); toastr.warning('Text classification is disabled or not available');
return ''; return '';
} }
const label = await getExpressionLabel(text, expressionApi, { customPrompt: prompt }); const label = await getExpressionLabel(text, expressionApi, { filterAvailable: filterAvailable, customPrompt: prompt });
console.debug(`Classification result for "${text}": ${label}`); console.debug(`Classification result for "${text}": ${label}`);
return label; return label;
} }
@@ -928,6 +930,9 @@ function parseLlmResponse(emotionResponse, labels) {
return response; return response;
} catch { } catch {
// Clean possible reasoning from response
emotionResponse = removeReasoningFromString(emotionResponse);
const fuse = new Fuse(labels, { includeScore: true }); const fuse = new Fuse(labels, { includeScore: true });
console.debug('Using fuzzy search in labels:', labels); console.debug('Using fuzzy search in labels:', labels);
const result = fuse.search(emotionResponse); const result = fuse.search(emotionResponse);
@@ -988,10 +993,11 @@ function onTextGenSettingsReady(args) {
* @param {string} text - The text to classify and retrieve the expression label for. * @param {string} text - The text to classify and retrieve the expression label for.
* @param {EXPRESSION_API} [expressionsApi=extension_settings.expressions.api] - The expressions API to use for classification. * @param {EXPRESSION_API} [expressionsApi=extension_settings.expressions.api] - The expressions API to use for classification.
* @param {object} [options={}] - Optional arguments. * @param {object} [options={}] - Optional arguments.
* @param {boolean?} [options.filterAvailable=null] - Whether to filter available expressions. If not specified, uses the extension setting.
* @param {string?} [options.customPrompt=null] - The custom prompt to use for classification. * @param {string?} [options.customPrompt=null] - The custom prompt to use for classification.
* @returns {Promise<string?>} - The label of the expression. * @returns {Promise<string?>} - The label of the expression.
*/ */
export async function getExpressionLabel(text, expressionsApi = extension_settings.expressions.api, { customPrompt = null } = {}) { export async function getExpressionLabel(text, expressionsApi = extension_settings.expressions.api, { filterAvailable = null, customPrompt = null } = {}) {
// Return if text is undefined, saving a costly fetch request // Return if text is undefined, saving a costly fetch request
if ((!modules.includes('classify') && expressionsApi == EXPRESSION_API.extras) || !text) { if ((!modules.includes('classify') && expressionsApi == EXPRESSION_API.extras) || !text) {
return extension_settings.expressions.fallback_expression; return extension_settings.expressions.fallback_expression;
@@ -1003,6 +1009,11 @@ export async function getExpressionLabel(text, expressionsApi = extension_settin
text = sampleClassifyText(text); text = sampleClassifyText(text);
filterAvailable ??= extension_settings.expressions.filterAvailable;
if (filterAvailable && ![EXPRESSION_API.llm, EXPRESSION_API.webllm].includes(expressionsApi)) {
console.debug('Filter available is only supported for LLM and WebLLM expressions');
}
try { try {
switch (expressionsApi) { switch (expressionsApi) {
// Local BERT pipeline // Local BERT pipeline
@@ -1027,7 +1038,7 @@ export async function getExpressionLabel(text, expressionsApi = extension_settin
return extension_settings.expressions.fallback_expression; return extension_settings.expressions.fallback_expression;
} }
const expressionsList = await getExpressionsList(); const expressionsList = await getExpressionsList({ filterAvailable: filterAvailable });
const prompt = substituteParamsExtended(customPrompt, { labels: expressionsList }) || await getLlmPrompt(expressionsList); const prompt = substituteParamsExtended(customPrompt, { labels: expressionsList }) || await getLlmPrompt(expressionsList);
eventSource.once(event_types.TEXT_COMPLETION_SETTINGS_READY, onTextGenSettingsReady); eventSource.once(event_types.TEXT_COMPLETION_SETTINGS_READY, onTextGenSettingsReady);
const emotionResponse = await generateRaw(text, main_api, false, false, prompt); const emotionResponse = await generateRaw(text, main_api, false, false, prompt);
@@ -1040,7 +1051,7 @@ export async function getExpressionLabel(text, expressionsApi = extension_settin
return extension_settings.expressions.fallback_expression; return extension_settings.expressions.fallback_expression;
} }
const expressionsList = await getExpressionsList(); const expressionsList = await getExpressionsList({ filterAvailable: filterAvailable });
const prompt = substituteParamsExtended(customPrompt, { labels: expressionsList }) || await getLlmPrompt(expressionsList); const prompt = substituteParamsExtended(customPrompt, { labels: expressionsList }) || await getLlmPrompt(expressionsList);
const messages = [ const messages = [
{ role: 'user', content: text + '\n\n' + prompt }, { role: 'user', content: text + '\n\n' + prompt },
@@ -1320,12 +1331,28 @@ function getCachedExpressions() {
return [...expressionsList, ...extension_settings.expressions.custom].filter(onlyUnique); return [...expressionsList, ...extension_settings.expressions.custom].filter(onlyUnique);
} }
export async function getExpressionsList() { export async function getExpressionsList({ filterAvailable = false } = {}) {
// Return cached list if available // If there is no cached list, load and cache it
if (Array.isArray(expressionsList)) { if (!Array.isArray(expressionsList)) {
return getCachedExpressions(); expressionsList = await resolveExpressionsList();
} }
const expressions = getCachedExpressions();
// Filtering is only available for llm and webllm APIs
if (!filterAvailable || ![EXPRESSION_API.llm, EXPRESSION_API.webllm].includes(extension_settings.expressions.api)) {
return expressions;
}
// Get expressions with available sprites
const currentLastMessage = selected_group ? getLastCharacterMessage() : null;
const spriteFolderName = getSpriteFolderName(currentLastMessage, currentLastMessage?.name);
return expressions.filter(label => {
const expression = spriteCache[spriteFolderName]?.find(x => x.label === label);
return (expression?.files.length ?? 0) > 0;
});
/** /**
* Returns the list of expressions from the API or fallback in offline mode. * Returns the list of expressions from the API or fallback in offline mode.
* @returns {Promise<string[]>} * @returns {Promise<string[]>}
@@ -1372,9 +1399,6 @@ export async function getExpressionsList() {
expressionsList = DEFAULT_EXPRESSIONS.slice(); expressionsList = DEFAULT_EXPRESSIONS.slice();
return expressionsList; return expressionsList;
} }
const result = await resolveExpressionsList();
return [...result, ...extension_settings.expressions.custom].filter(onlyUnique);
} }
/** /**
@@ -1810,7 +1834,7 @@ async function onClickExpressionUpload(event) {
} }
} }
} else { } else {
spriteName = withoutExtension(clickedFileName); spriteName = withoutExtension(expression);
} }
if (!spriteName) { if (!spriteName) {
@@ -2102,6 +2126,10 @@ function migrateSettings() {
extension_settings.expressions.rerollIfSame = !!$(this).prop('checked'); extension_settings.expressions.rerollIfSame = !!$(this).prop('checked');
saveSettingsDebounced(); saveSettingsDebounced();
}); });
$('#expressions_filter_available').prop('checked', extension_settings.expressions.filterAvailable).on('input', function () {
extension_settings.expressions.filterAvailable = !!$(this).prop('checked');
saveSettingsDebounced();
});
$('#expression_override_cleanup_button').on('click', onClickExpressionOverrideRemoveAllButton); $('#expression_override_cleanup_button').on('click', onClickExpressionOverrideRemoveAllButton);
$(document).on('dragstart', '.expression', (e) => { $(document).on('dragstart', '.expression', (e) => {
e.preventDefault(); e.preventDefault();
@@ -2279,13 +2307,13 @@ function migrateSettings() {
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'expression-list', name: 'expression-list',
aliases: ['expressions'], aliases: ['expressions'],
/** @type {(args: {return: string}) => Promise<string>} */ /** @type {(args: {return: string, filter: string}) => Promise<string>} */
callback: async (args) => { callback: async (args) => {
let returnType = let returnType =
/** @type {import('../../slash-commands/SlashCommandReturnHelper.js').SlashCommandReturnType} */ /** @type {import('../../slash-commands/SlashCommandReturnHelper.js').SlashCommandReturnType} */
(args.return); (args.return);
const list = await getExpressionsList(); const list = await getExpressionsList({ filterAvailable: !isFalseBoolean(args.filter) });
return await slashCommandReturnHelper.doReturn(returnType ?? 'pipe', list, { objectToStringFunc: list => list.join(', ') }); return await slashCommandReturnHelper.doReturn(returnType ?? 'pipe', list, { objectToStringFunc: list => list.join(', ') });
}, },
@@ -2298,6 +2326,13 @@ function migrateSettings() {
enumList: slashCommandReturnHelper.enumList({ allowObject: true }), enumList: slashCommandReturnHelper.enumList({ allowObject: true }),
forceEnum: true, forceEnum: true,
}), }),
SlashCommandNamedArgument.fromProps({
name: 'filter',
description: 'Filter the list to only include expressions that have available sprites for the current character.',
typeList: [ARGUMENT_TYPE.BOOLEAN],
enumList: commonEnumProviders.boolean('trueFalse')(),
defaultValue: 'true',
}),
], ],
returns: 'The comma-separated list of available expressions, including custom expressions.', returns: 'The comma-separated list of available expressions, including custom expressions.',
helpString: 'Returns a list of available expressions, including custom expressions.', helpString: 'Returns a list of available expressions, including custom expressions.',
@@ -2313,6 +2348,13 @@ function migrateSettings() {
typeList: [ARGUMENT_TYPE.STRING], typeList: [ARGUMENT_TYPE.STRING],
enumList: Object.keys(EXPRESSION_API).map(api => new SlashCommandEnumValue(api, null, enumTypes.enum)), enumList: Object.keys(EXPRESSION_API).map(api => new SlashCommandEnumValue(api, null, enumTypes.enum)),
}), }),
SlashCommandNamedArgument.fromProps({
name: 'filter',
description: 'Filter the list to only include expressions that have available sprites for the current character.',
typeList: [ARGUMENT_TYPE.BOOLEAN],
enumList: commonEnumProviders.boolean('trueFalse')(),
defaultValue: 'true',
}),
SlashCommandNamedArgument.fromProps({ SlashCommandNamedArgument.fromProps({
name: 'prompt', name: 'prompt',
description: 'Custom prompt for classification. Only relevant if Classifier API is set to LLM.', description: 'Custom prompt for classification. Only relevant if Classifier API is set to LLM.',

View File

@@ -29,7 +29,11 @@
</select> </select>
</div> </div>
<div class="expression_llm_prompt_block m-b-1 m-t-1"> <div class="expression_llm_prompt_block m-b-1 m-t-1">
<label for="expression_llm_prompt" class="title_restorable"> <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.">
<input id="expressions_filter_available" type="checkbox">
<span data-i18n="Filter expressions for available sprites">Filter expressions for available sprites</span>
</label>
<label for="expression_llm_prompt" class="title_restorable m-t-1">
<span data-i18n="LLM Prompt">LLM Prompt</span> <span data-i18n="LLM Prompt">LLM Prompt</span>
<div id="expression_llm_prompt_restore" title="Restore default value" class="right_menu_button"> <div id="expression_llm_prompt_restore" title="Restore default value" class="right_menu_button">
<i class="fa-solid fa-clock-rotate-left fa-sm"></i> <i class="fa-solid fa-clock-rotate-left fa-sm"></i>

View File

@@ -435,16 +435,18 @@ export function getGroupCharacterCards(groupId, characterId) {
* @param {string} value Value to replace * @param {string} value Value to replace
* @param {string} fieldName Name of the field * @param {string} fieldName Name of the field
* @param {string} characterName Name of the character * @param {string} characterName Name of the character
* @param {boolean} trim Whether to trim the value
* @returns {string} Replaced text * @returns {string} Replaced text
* */ * */
function customBaseChatReplace(value, fieldName, characterName) { function customBaseChatReplace(value, fieldName, characterName, trim) {
if (!value) { if (!value) {
return ''; return '';
} }
// We should do the custom field name replacement first, and then run it through the normal macro engine with provided names // We should do the custom field name replacement first, and then run it through the normal macro engine with provided names
value = value.replace(/<FIELDNAME>/gi, fieldName); value = value.replace(/<FIELDNAME>/gi, fieldName);
return baseChatReplace(value.trim(), name1, characterName); value = trim ? value.trim() : value;
return baseChatReplace(value, name1, characterName);
} }
/** /**
@@ -467,13 +469,12 @@ export function getGroupCharacterCards(groupId, characterId) {
} }
// Prepare and replace prefixes // Prepare and replace prefixes
const prefix = customBaseChatReplace(group.generation_mode_join_prefix, fieldName, characterName); const prefix = customBaseChatReplace(group.generation_mode_join_prefix, fieldName, characterName, false);
const suffix = customBaseChatReplace(group.generation_mode_join_suffix, fieldName, characterName); const suffix = customBaseChatReplace(group.generation_mode_join_suffix, fieldName, characterName, false);
const separator = power_user.instruct.wrap ? '\n' : '';
// Also run the macro replacement on the actual content // Also run the macro replacement on the actual content
value = customBaseChatReplace(value, fieldName, characterName); value = customBaseChatReplace(value, fieldName, characterName, true);
return `${prefix ? prefix + separator : ''}${value}${suffix ? separator + suffix : ''}`; return `${prefix}${value}${suffix}`;
} }
const scenarioOverride = chat_metadata['scenario']; const scenarioOverride = chat_metadata['scenario'];
@@ -904,6 +905,7 @@ async function generateGroupWrapper(by_auto_mode, type = null, params = {}) {
groupChatQueueOrder.set(characters[activatedMembers[i]].avatar, i + 1); groupChatQueueOrder.set(characters[activatedMembers[i]].avatar, i + 1);
} }
} }
await eventSource.emit(event_types.GROUP_WRAPPER_STARTED, { selected_group, type });
// now the real generation begins: cycle through every activated character // now the real generation begins: cycle through every activated character
for (const chId of activatedMembers) { for (const chId of activatedMembers) {
throwIfAborted(); throwIfAborted();
@@ -942,6 +944,7 @@ async function generateGroupWrapper(by_auto_mode, type = null, params = {}) {
setCharacterName(''); setCharacterName('');
activateSendButtons(); activateSendButtons();
showSwipeButtons(); showSwipeButtons();
await eventSource.emit(event_types.GROUP_WRAPPER_FINISHED, { selected_group, type });
} }
return Promise.resolve(textResult); return Promise.resolve(textResult);

View File

@@ -783,12 +783,11 @@ router.post('/search', validateAvatarUrlMiddleware, function (request, response)
continue; continue;
} }
// Search through messages // Search through title and messages of the chat
const fragments = query.trim().toLowerCase().split(/\s+/).filter(x => x); const fragments = query.trim().toLowerCase().split(/\s+/).filter(x => x);
const hasMatch = messages.some(message => { const text = [chatFile.path.split(/[\\/]/).pop().replace(/.jsonl$/, ''),
const text = message?.mes?.toLowerCase(); ...messages.map(message => message?.mes)].join('\n').toLowerCase();
return text && fragments.every(fragment => text.includes(fragment)); const hasMatch = fragments.every(fragment => text.includes(fragment));
});
if (hasMatch) { if (hasMatch) {
results.push({ results.push({