mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2024-12-11 17:07:07 +01:00
Refactor /listvar, deprecate its "format" arg
- Update /listvar - Fix toasts not doing correct HMTL here
This commit is contained in:
parent
62fd450c59
commit
697b3b2034
@ -1524,21 +1524,21 @@ export function initDefaultSlashCommands() {
|
|||||||
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
|
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
|
||||||
name: 'listinjects',
|
name: 'listinjects',
|
||||||
callback: listInjectsCallback,
|
callback: listInjectsCallback,
|
||||||
helpString: 'Lists all script injections for the current chat. Displays injects in a popup by default. Use the <code>format</code> argument to change the output format.',
|
helpString: 'Lists all script injections for the current chat. Displays injects in a popup by default. Use the <code>return</code> argument to change the return type.',
|
||||||
returns: 'Optionalls the JSON object of script injections',
|
returns: 'Optionalls the JSON object of script injections',
|
||||||
namedArgumentList: [
|
namedArgumentList: [
|
||||||
SlashCommandNamedArgument.fromProps({
|
SlashCommandNamedArgument.fromProps({
|
||||||
name: 'return',
|
name: 'return',
|
||||||
description: 'The way how you want the return value to be provided',
|
description: 'The way how you want the return value to be provided',
|
||||||
typeList: [ARGUMENT_TYPE.STRING],
|
typeList: [ARGUMENT_TYPE.STRING],
|
||||||
defaultValue: 'object',
|
defaultValue: 'popup-html',
|
||||||
enumList: slashCommandReturnHelper.enumList({ allowPipe: false, allowObject: true, allowChat: true, allowPopup: true, allowTextVersion: false }),
|
enumList: slashCommandReturnHelper.enumList({ allowPipe: false, allowObject: true, allowChat: true, allowPopup: true, allowTextVersion: false }),
|
||||||
forceEnum: true,
|
forceEnum: true,
|
||||||
}),
|
}),
|
||||||
// TODO remove some day
|
// TODO remove some day
|
||||||
SlashCommandNamedArgument.fromProps({
|
SlashCommandNamedArgument.fromProps({
|
||||||
name: 'format',
|
name: 'format',
|
||||||
description: 'DEPRECATED - use "return" instead - output format',
|
description: '!!! DEPRECATED - use "return" instead !!! output format)',
|
||||||
typeList: [ARGUMENT_TYPE.STRING],
|
typeList: [ARGUMENT_TYPE.STRING],
|
||||||
isRequired: true,
|
isRequired: true,
|
||||||
forceEnum: true,
|
forceEnum: true,
|
||||||
@ -1832,7 +1832,6 @@ async function listInjectsCallback(args) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Now the actual new return type handling
|
// Now the actual new return type handling
|
||||||
|
|
||||||
const buildTextValue = (injects) => {
|
const buildTextValue = (injects) => {
|
||||||
const injectsStr = Object.entries(injects)
|
const injectsStr = Object.entries(injects)
|
||||||
.map(([id, inject]) => {
|
.map(([id, inject]) => {
|
||||||
@ -1844,7 +1843,7 @@ async function listInjectsCallback(args) {
|
|||||||
return `### Script injections:\n${injectsStr}`;
|
return `### Script injections:\n${injectsStr}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
return await slashCommandReturnHelper.doReturn(returnType ?? 'object', chat_metadata.script_injects, { objectToStringFunc: buildTextValue });
|
return await slashCommandReturnHelper.doReturn(returnType ?? 'popup-html', chat_metadata.script_injects, { objectToStringFunc: buildTextValue });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -58,7 +58,7 @@ export const slashCommandReturnHelper = {
|
|||||||
|
|
||||||
if (type.startsWith('popup')) await callGenericPopup(shouldHtml ? makeHtml(stringValue) : escapeHtml(stringValue), POPUP_TYPE.TEXT);
|
if (type.startsWith('popup')) await callGenericPopup(shouldHtml ? makeHtml(stringValue) : escapeHtml(stringValue), POPUP_TYPE.TEXT);
|
||||||
if (type.startsWith('chat')) sendSystemMessage(system_message_types.GENERIC, shouldHtml ? makeHtml(stringValue) : escapeHtml(stringValue));
|
if (type.startsWith('chat')) sendSystemMessage(system_message_types.GENERIC, shouldHtml ? makeHtml(stringValue) : escapeHtml(stringValue));
|
||||||
if (type.startsWith('toast')) toastr.info(stringValue, null, { escapeHtml: !shouldHtml }); // Toastr handles HTML conversion internally already
|
if (type.startsWith('toast')) toastr.info(shouldHtml ? makeHtml(stringValue) : escapeHtml(stringValue), null, { escapeHtml: !shouldHtml });
|
||||||
|
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ import { SlashCommandClosureResult } from './slash-commands/SlashCommandClosureR
|
|||||||
import { commonEnumProviders, enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js';
|
import { commonEnumProviders, enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js';
|
||||||
import { SlashCommandEnumValue, enumTypes } from './slash-commands/SlashCommandEnumValue.js';
|
import { SlashCommandEnumValue, enumTypes } from './slash-commands/SlashCommandEnumValue.js';
|
||||||
import { PARSER_FLAG, SlashCommandParser } from './slash-commands/SlashCommandParser.js';
|
import { PARSER_FLAG, SlashCommandParser } from './slash-commands/SlashCommandParser.js';
|
||||||
|
import { slashCommandReturnHelper } from './slash-commands/SlashCommandReturnHelper.js';
|
||||||
import { SlashCommandScope } from './slash-commands/SlashCommandScope.js';
|
import { SlashCommandScope } from './slash-commands/SlashCommandScope.js';
|
||||||
import { isFalseBoolean, convertValueType, isTrueBoolean } from './utils.js';
|
import { isFalseBoolean, convertValueType, isTrueBoolean } from './utils.js';
|
||||||
|
|
||||||
@ -305,7 +306,31 @@ export function replaceVariableMacros(input) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function listVariablesCallback(args) {
|
async function listVariablesCallback(args) {
|
||||||
const type = String(args?.format || '').toLowerCase().trim() || 'popup';
|
/** @type {import('./slash-commands/SlashCommandReturnHelper.js').SlashCommandReturnType} */
|
||||||
|
let returnType = args.return;
|
||||||
|
|
||||||
|
// Old legacy return type handling
|
||||||
|
if (args.format) {
|
||||||
|
toastr.warning(`Legacy argument 'format' with value '${args.format}' is deprecated. Please use 'return' instead. Routing to the correct return type...`, 'Deprecation warning');
|
||||||
|
const type = String(args?.format).toLowerCase().trim();
|
||||||
|
if (!chat_metadata.script_injects || !Object.keys(chat_metadata.script_injects).length) {
|
||||||
|
type !== 'none' && toastr.info('No script injections for the current chat');
|
||||||
|
}
|
||||||
|
switch (type) {
|
||||||
|
case 'none':
|
||||||
|
returnType = 'none';
|
||||||
|
break;
|
||||||
|
case 'chat':
|
||||||
|
returnType = 'chat-html';
|
||||||
|
break;
|
||||||
|
case 'popup':
|
||||||
|
default:
|
||||||
|
returnType = 'popup-html';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now the actual new return type handling
|
||||||
const scope = String(args?.scope || '').toLowerCase().trim() || 'all';
|
const scope = String(args?.scope || '').toLowerCase().trim() || 'all';
|
||||||
if (!chat_metadata.variables) {
|
if (!chat_metadata.variables) {
|
||||||
chat_metadata.variables = {};
|
chat_metadata.variables = {};
|
||||||
@ -317,35 +342,24 @@ async function listVariablesCallback(args) {
|
|||||||
const localVariables = includeLocalVariables ? Object.entries(chat_metadata.variables).map(([name, value]) => `${name}: ${value}`) : [];
|
const localVariables = includeLocalVariables ? Object.entries(chat_metadata.variables).map(([name, value]) => `${name}: ${value}`) : [];
|
||||||
const globalVariables = includeGlobalVariables ? Object.entries(extension_settings.variables.global).map(([name, value]) => `${name}: ${value}`) : [];
|
const globalVariables = includeGlobalVariables ? Object.entries(extension_settings.variables.global).map(([name, value]) => `${name}: ${value}`) : [];
|
||||||
|
|
||||||
|
const buildTextValue = (_) => {
|
||||||
|
const localVariablesString = localVariables.length > 0 ? localVariables.join('\n\n') : 'No local variables';
|
||||||
|
const globalVariablesString = globalVariables.length > 0 ? globalVariables.join('\n\n') : 'No global variables';
|
||||||
|
const chatName = getCurrentChatId();
|
||||||
|
|
||||||
|
const message = [
|
||||||
|
includeLocalVariables ? `### Local variables (${chatName}):\n${localVariablesString}` : '',
|
||||||
|
includeGlobalVariables ? `### Global variables:\n${globalVariablesString}` : '',
|
||||||
|
].filter(x => x).join('\n\n');
|
||||||
|
return message;
|
||||||
|
};
|
||||||
|
|
||||||
const jsonVariables = [
|
const jsonVariables = [
|
||||||
...Object.entries(chat_metadata.variables).map(x => ({ key: x[0], value: x[1], scope: 'local' })),
|
...Object.entries(chat_metadata.variables).map(x => ({ key: x[0], value: x[1], scope: 'local' })),
|
||||||
...Object.entries(extension_settings.variables.global).map(x => ({ key: x[0], value: x[1], scope: 'global' })),
|
...Object.entries(extension_settings.variables.global).map(x => ({ key: x[0], value: x[1], scope: 'global' })),
|
||||||
];
|
];
|
||||||
|
|
||||||
const localVariablesString = localVariables.length > 0 ? localVariables.join('\n\n') : 'No local variables';
|
return await slashCommandReturnHelper.doReturn(returnType ?? 'popup-html', jsonVariables, { objectToStringFunc: buildTextValue });
|
||||||
const globalVariablesString = globalVariables.length > 0 ? globalVariables.join('\n\n') : 'No global variables';
|
|
||||||
const chatName = getCurrentChatId();
|
|
||||||
|
|
||||||
const converter = new showdown.Converter();
|
|
||||||
const message = [
|
|
||||||
includeLocalVariables ? `### Local variables (${chatName}):\n${localVariablesString}` : '',
|
|
||||||
includeGlobalVariables ? `### Global variables:\n${globalVariablesString}` : '',
|
|
||||||
].filter(x => x).join('\n\n');
|
|
||||||
const htmlMessage = DOMPurify.sanitize(converter.makeHtml(message));
|
|
||||||
|
|
||||||
switch (type) {
|
|
||||||
case 'none':
|
|
||||||
break;
|
|
||||||
case 'chat':
|
|
||||||
sendSystemMessage(system_message_types.GENERIC, htmlMessage);
|
|
||||||
break;
|
|
||||||
case 'popup':
|
|
||||||
default:
|
|
||||||
await callGenericPopup(htmlMessage, POPUP_TYPE.TEXT);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return JSON.stringify(jsonVariables);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -910,7 +924,7 @@ export function registerVariableCommands() {
|
|||||||
name: 'listvar',
|
name: 'listvar',
|
||||||
callback: listVariablesCallback,
|
callback: listVariablesCallback,
|
||||||
aliases: ['listchatvar'],
|
aliases: ['listchatvar'],
|
||||||
helpString: 'List registered chat variables. Displays variables in a popup by default. Use the <code>format</code> argument to change the output format.',
|
helpString: 'List registered chat variables. Displays variables in a popup by default. Use the <code>return</code> argument to change the return type.',
|
||||||
returns: 'JSON list of local variables',
|
returns: 'JSON list of local variables',
|
||||||
namedArgumentList: [
|
namedArgumentList: [
|
||||||
SlashCommandNamedArgument.fromProps({
|
SlashCommandNamedArgument.fromProps({
|
||||||
@ -926,9 +940,18 @@ export function registerVariableCommands() {
|
|||||||
new SlashCommandEnumValue('global', 'Global variables', enumTypes.enum, enumIcons.globalVariable),
|
new SlashCommandEnumValue('global', 'Global variables', enumTypes.enum, enumIcons.globalVariable),
|
||||||
],
|
],
|
||||||
}),
|
}),
|
||||||
|
SlashCommandNamedArgument.fromProps({
|
||||||
|
name: 'return',
|
||||||
|
description: 'The way how you want the return value to be provided',
|
||||||
|
typeList: [ARGUMENT_TYPE.STRING],
|
||||||
|
defaultValue: 'popup-html',
|
||||||
|
enumList: slashCommandReturnHelper.enumList({ allowPipe: false, allowObject: true, allowChat: true, allowPopup: true, allowTextVersion: false }),
|
||||||
|
forceEnum: true,
|
||||||
|
}),
|
||||||
|
// TODO remove some day
|
||||||
SlashCommandNamedArgument.fromProps({
|
SlashCommandNamedArgument.fromProps({
|
||||||
name: 'format',
|
name: 'format',
|
||||||
description: 'output format',
|
description: '!!! DEPRECATED - use "return" instead !!! output format)',
|
||||||
typeList: [ARGUMENT_TYPE.STRING],
|
typeList: [ARGUMENT_TYPE.STRING],
|
||||||
isRequired: true,
|
isRequired: true,
|
||||||
forceEnum: true,
|
forceEnum: true,
|
||||||
|
Loading…
Reference in New Issue
Block a user