mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Merge pull request #3664 from SillyTavern/hide-name
Add "name" argument to /hide and /unhide
This commit is contained in:
@@ -130,9 +130,10 @@ function getConverter(type) {
|
||||
* @param {number} start Starting message ID
|
||||
* @param {number} end Ending message ID (inclusive)
|
||||
* @param {boolean} unhide If true, unhide the messages instead.
|
||||
* @param {string} nameFitler Optional name filter
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export async function hideChatMessageRange(start, end, unhide) {
|
||||
export async function hideChatMessageRange(start, end, unhide, nameFitler = null) {
|
||||
if (isNaN(start)) return;
|
||||
if (!end) end = start;
|
||||
const hide = !unhide;
|
||||
@@ -140,6 +141,7 @@ export async function hideChatMessageRange(start, end, unhide) {
|
||||
for (let messageId = start; messageId <= end; messageId++) {
|
||||
const message = chat[messageId];
|
||||
if (!message) continue;
|
||||
if (nameFitler && message.name !== nameFitler) continue;
|
||||
|
||||
message.is_system = hide;
|
||||
|
||||
|
@@ -667,11 +667,21 @@ export function initDefaultSlashCommands() {
|
||||
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
|
||||
name: 'hide',
|
||||
callback: hideMessageCallback,
|
||||
namedArgumentList: [
|
||||
SlashCommandNamedArgument.fromProps({
|
||||
name: 'name',
|
||||
description: 'only hide messages from a certain character or persona',
|
||||
typeList: [ARGUMENT_TYPE.STRING],
|
||||
enumProvider: commonEnumProviders.messageNames,
|
||||
isRequired: false,
|
||||
acceptsMultiple: false,
|
||||
}),
|
||||
],
|
||||
unnamedArgumentList: [
|
||||
SlashCommandArgument.fromProps({
|
||||
description: 'message index (starts with 0) or range',
|
||||
description: 'message index (starts with 0) or range, defaults to the last message index if not provided',
|
||||
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.RANGE],
|
||||
isRequired: true,
|
||||
isRequired: false,
|
||||
enumProvider: commonEnumProviders.messages(),
|
||||
}),
|
||||
],
|
||||
@@ -680,11 +690,21 @@ export function initDefaultSlashCommands() {
|
||||
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
|
||||
name: 'unhide',
|
||||
callback: unhideMessageCallback,
|
||||
namedArgumentList: [
|
||||
SlashCommandNamedArgument.fromProps({
|
||||
name: 'name',
|
||||
description: 'only unhide messages from a certain character or persona',
|
||||
typeList: [ARGUMENT_TYPE.STRING],
|
||||
enumProvider: commonEnumProviders.messageNames,
|
||||
isRequired: false,
|
||||
acceptsMultiple: false,
|
||||
}),
|
||||
],
|
||||
unnamedArgumentList: [
|
||||
SlashCommandArgument.fromProps({
|
||||
description: 'message index (starts with 0) or range',
|
||||
description: 'message index (starts with 0) or range, defaults to the last message index if not provided',
|
||||
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.RANGE],
|
||||
isRequired: true,
|
||||
isRequired: false,
|
||||
enumProvider: commonEnumProviders.messages(),
|
||||
}),
|
||||
],
|
||||
@@ -3034,37 +3054,29 @@ async function askCharacter(args, text) {
|
||||
return await slashCommandReturnHelper.doReturn(args.return ?? 'pipe', message, { objectToStringFunc: x => x.mes });
|
||||
}
|
||||
|
||||
async function hideMessageCallback(_, arg) {
|
||||
if (!arg) {
|
||||
console.warn('WARN: No argument provided for /hide command');
|
||||
return '';
|
||||
}
|
||||
|
||||
const range = stringToRange(arg, 0, chat.length - 1);
|
||||
async function hideMessageCallback(args, value) {
|
||||
const range = value ? stringToRange(value, 0, chat.length - 1) : { start: chat.length - 1, end: chat.length - 1 };
|
||||
|
||||
if (!range) {
|
||||
console.warn(`WARN: Invalid range provided for /hide command: ${arg}`);
|
||||
console.warn(`WARN: Invalid range provided for /hide command: ${value}`);
|
||||
return '';
|
||||
}
|
||||
|
||||
await hideChatMessageRange(range.start, range.end, false);
|
||||
const nameFilter = String(args.name ?? '').trim();
|
||||
await hideChatMessageRange(range.start, range.end, false, nameFilter);
|
||||
return '';
|
||||
}
|
||||
|
||||
async function unhideMessageCallback(_, arg) {
|
||||
if (!arg) {
|
||||
console.warn('WARN: No argument provided for /unhide command');
|
||||
return '';
|
||||
}
|
||||
|
||||
const range = stringToRange(arg, 0, chat.length - 1);
|
||||
async function unhideMessageCallback(args, value) {
|
||||
const range = value ? stringToRange(value, 0, chat.length - 1) : { start: chat.length - 1, end: chat.length - 1 };
|
||||
|
||||
if (!range) {
|
||||
console.warn(`WARN: Invalid range provided for /unhide command: ${arg}`);
|
||||
console.warn(`WARN: Invalid range provided for /unhide command: ${value}`);
|
||||
return '';
|
||||
}
|
||||
|
||||
await hideChatMessageRange(range.start, range.end, true);
|
||||
const nameFilter = String(args.name ?? '').trim();
|
||||
await hideChatMessageRange(range.start, range.end, true, nameFilter);
|
||||
return '';
|
||||
}
|
||||
|
||||
|
@@ -3,6 +3,7 @@ import { extension_settings } from '../extensions.js';
|
||||
import { getGroupMembers, groups } from '../group-chats.js';
|
||||
import { power_user } from '../power-user.js';
|
||||
import { searchCharByName, getTagsList, tags, tag_map } from '../tags.js';
|
||||
import { onlyUniqueJson, sortIgnoreCaseAndAccents } from '../utils.js';
|
||||
import { world_names } from '../world-info.js';
|
||||
import { SlashCommandClosure } from './SlashCommandClosure.js';
|
||||
import { SlashCommandEnumValue, enumTypes } from './SlashCommandEnumValue.js';
|
||||
@@ -251,14 +252,29 @@ export const commonEnumProviders = {
|
||||
* @param {boolean} [options.allowVars=false] - Whether to add enum option for variable names
|
||||
* @returns {(executor:SlashCommandExecutor, scope:SlashCommandScope) => SlashCommandEnumValue[]}
|
||||
*/
|
||||
messages: ({ allowIdAfter = false, allowVars = false } = {}) => (_, scope) => {
|
||||
messages: ({ allowIdAfter = false, allowVars = false } = {}) => (executor, scope) => {
|
||||
const nameFilter = executor.namedArgumentList.find(it => it.name == 'name')?.value || '';
|
||||
return [
|
||||
...chat.map((message, index) => new SlashCommandEnumValue(String(index), `${message.name}: ${message.mes}`, enumTypes.number, message.is_user ? enumIcons.user : message.is_system ? enumIcons.system : enumIcons.assistant)),
|
||||
...chat.map((message, index) => new SlashCommandEnumValue(String(index), `${message.name}: ${message.mes}`, enumTypes.number, message.is_user ? enumIcons.user : message.is_system ? enumIcons.system : enumIcons.assistant)).filter(value => !nameFilter || value.description.startsWith(`${nameFilter}:`)),
|
||||
...allowIdAfter ? [new SlashCommandEnumValue(String(chat.length), '>> After Last Message >>', enumTypes.enum, '➕')] : [],
|
||||
...allowVars ? commonEnumProviders.variables('all')(_, scope) : [],
|
||||
...allowVars ? commonEnumProviders.variables('all')(executor, scope) : [],
|
||||
];
|
||||
},
|
||||
|
||||
/**
|
||||
* All names used in the current chat.
|
||||
*
|
||||
* @returns {SlashCommandEnumValue[]}
|
||||
*/
|
||||
messageNames: () => chat
|
||||
.map(message => ({
|
||||
name: message.name,
|
||||
icon: message.is_user ? enumIcons.user : enumIcons.assistant,
|
||||
}))
|
||||
.filter(onlyUniqueJson)
|
||||
.sort((a, b) => sortIgnoreCaseAndAccents(a.name, b.name))
|
||||
.map(name => new SlashCommandEnumValue(name.name, null, null, name.icon)),
|
||||
|
||||
/**
|
||||
* All existing worlds / lorebooks
|
||||
*
|
||||
|
@@ -6,7 +6,7 @@ import {
|
||||
} from '../lib.js';
|
||||
|
||||
import { getContext } from './extensions.js';
|
||||
import { characters, getRequestHeaders, this_chid } from '../script.js';
|
||||
import { characters, getRequestHeaders, this_chid, user_avatar } from '../script.js';
|
||||
import { isMobile } from './RossAscends-mods.js';
|
||||
import { collapseNewlines, power_user } from './power-user.js';
|
||||
import { debounce_timeout } from './constants.js';
|
||||
@@ -14,7 +14,7 @@ import { Popup, POPUP_RESULT, POPUP_TYPE } from './popup.js';
|
||||
import { SlashCommandClosure } from './slash-commands/SlashCommandClosure.js';
|
||||
import { getTagsList } from './tags.js';
|
||||
import { groups, selected_group } from './group-chats.js';
|
||||
import { getCurrentLocale } from './i18n.js';
|
||||
import { getCurrentLocale, t } from './i18n.js';
|
||||
|
||||
/**
|
||||
* Pagination status string template.
|
||||
@@ -196,6 +196,17 @@ export function onlyUnique(value, index, array) {
|
||||
return array.indexOf(value) === index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if a value is unique in an array of objects.
|
||||
* @param {any} value Current value.
|
||||
* @param {number} index Current index.
|
||||
* @param {any[]} array The array being processed.
|
||||
* @returns {boolean} True if the value is unique, false otherwise.
|
||||
*/
|
||||
export function onlyUniqueJson(value, index, array) {
|
||||
return array.map(v => JSON.stringify(v)).indexOf(JSON.stringify(value)) === index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the first occurrence of a specified item from an array
|
||||
*
|
||||
@@ -1799,8 +1810,9 @@ export function runAfterAnimation(control, callback, timeout = 500) {
|
||||
*
|
||||
* @param {string} a - The first string to compare.
|
||||
* @param {string} b - The second string to compare.
|
||||
* @param {(a:string,b:string)=>boolean} comparisonFunction - The function to use for the comparison.
|
||||
* @returns {*} - The result of the comparison.
|
||||
* @param {(a:string,b:string)=>T} comparisonFunction - The function to use for the comparison.
|
||||
* @returns {T} - The result of the comparison.
|
||||
* @template T
|
||||
*/
|
||||
export function compareIgnoreCaseAndAccents(a, b, comparisonFunction) {
|
||||
if (!a || !b) return comparisonFunction(a, b); // Return the comparison result if either string is empty
|
||||
@@ -1837,6 +1849,16 @@ export function equalsIgnoreCaseAndAccents(a, b) {
|
||||
return compareIgnoreCaseAndAccents(a, b, (a, b) => a === b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a case-insensitive and accent-insensitive sort.
|
||||
* @param {string} a - The first string to compare
|
||||
* @param {string} b - The second string to compare
|
||||
* @returns {number} -1 if a < b, 1 if a > b, 0 if a === b
|
||||
*/
|
||||
export function sortIgnoreCaseAndAccents(a, b) {
|
||||
return compareIgnoreCaseAndAccents(a, b, (a, b) => a?.localeCompare(b));
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef {object} Select2Option The option object for select2 controls
|
||||
* @property {string} id - The unique ID inside this select
|
||||
@@ -2196,6 +2218,48 @@ export async function showFontAwesomePicker(customList = null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a persona by name, with optional filtering and precedence for avatars
|
||||
* @param {object} [options={}] - The options for the search
|
||||
* @param {string?} [options.name=null] - The name to search for
|
||||
* @param {boolean} [options.allowAvatar=true] - Whether to allow searching by avatar
|
||||
* @param {boolean} [options.insensitive=true] - Whether the search should be case insensitive
|
||||
* @param {boolean} [options.preferCurrentPersona=true] - Whether to prefer the current persona(s)
|
||||
* @param {boolean} [options.quiet=false] - Whether to suppress warnings
|
||||
* @returns {PersonaViewModel} The persona object
|
||||
* @typedef {object} PersonaViewModel
|
||||
* @property {string} avatar - The avatar of the persona
|
||||
* @property {string} name - The name of the persona
|
||||
*/
|
||||
export function findPersona({ name = null, allowAvatar = true, insensitive = true, preferCurrentPersona = true, quiet = false } = {}) {
|
||||
/** @type {PersonaViewModel[]} */
|
||||
const personas = Object.entries(power_user.personas).map(([avatar, name]) => ({ avatar, name }));
|
||||
const matches = (/** @type {PersonaViewModel} */ persona) => !name || (allowAvatar && persona.avatar === name) || (insensitive ? equalsIgnoreCaseAndAccents(persona.name, name) : persona.name === name);
|
||||
|
||||
// If we have a current persona and prefer it, return that if it matches
|
||||
const currentPersona = personas.find(a => a.avatar === user_avatar);
|
||||
if (preferCurrentPersona && currentPersona && matches(currentPersona)) {
|
||||
return currentPersona;
|
||||
}
|
||||
|
||||
// If allowAvatar is true, search by avatar first
|
||||
if (allowAvatar && name) {
|
||||
const personaByAvatar = personas.find(a => a.avatar === name);
|
||||
if (personaByAvatar && matches(personaByAvatar)) {
|
||||
return personaByAvatar;
|
||||
}
|
||||
}
|
||||
|
||||
// Search for matching personas by name
|
||||
const matchingPersonas = personas.filter(a => matches(a));
|
||||
if (matchingPersonas.length > 1) {
|
||||
if (!quiet) toastr.warning(t`Multiple personas found for given conditions.`);
|
||||
else console.warn(t`Multiple personas found for given conditions. Returning the first match.`);
|
||||
}
|
||||
|
||||
return matchingPersonas[0] || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a character by name, with optional filtering and precedence for avatars
|
||||
* @param {object} [options={}] - The options for the search
|
||||
@@ -2228,8 +2292,8 @@ export function findChar({ name = null, allowAvatar = true, insensitive = true,
|
||||
if (preferCurrentChar) {
|
||||
const preferredCharSearch = currentChars.filter(matches);
|
||||
if (preferredCharSearch.length > 1) {
|
||||
if (!quiet) toastr.warning('Multiple characters found for given conditions.');
|
||||
else console.warn('Multiple characters found for given conditions. Returning the first match.');
|
||||
if (!quiet) toastr.warning(t`Multiple characters found for given conditions.`);
|
||||
else console.warn(t`Multiple characters found for given conditions. Returning the first match.`);
|
||||
}
|
||||
if (preferredCharSearch.length) {
|
||||
return preferredCharSearch[0];
|
||||
|
Reference in New Issue
Block a user