mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Add format and scope arguments to /listvar
This commit is contained in:
@ -1,5 +1,6 @@
|
|||||||
import { chat_metadata, getCurrentChatId, saveSettingsDebounced, sendSystemMessage, system_message_types } from '../script.js';
|
import { chat_metadata, getCurrentChatId, saveSettingsDebounced, sendSystemMessage, system_message_types } from '../script.js';
|
||||||
import { extension_settings, saveMetadataDebounced } from './extensions.js';
|
import { extension_settings, saveMetadataDebounced } from './extensions.js';
|
||||||
|
import { callGenericPopup, POPUP_TYPE } from './popup.js';
|
||||||
import { executeSlashCommandsWithOptions } from './slash-commands.js';
|
import { executeSlashCommandsWithOptions } from './slash-commands.js';
|
||||||
import { SlashCommand } from './slash-commands/SlashCommand.js';
|
import { SlashCommand } from './slash-commands/SlashCommand.js';
|
||||||
import { SlashCommandAbortController } from './slash-commands/SlashCommandAbortController.js';
|
import { SlashCommandAbortController } from './slash-commands/SlashCommandAbortController.js';
|
||||||
@ -303,24 +304,48 @@ export function replaceVariableMacros(input) {
|
|||||||
return lines.join('\n');
|
return lines.join('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
function listVariablesCallback() {
|
async function listVariablesCallback(args) {
|
||||||
|
const type = String(args?.format || '').toLowerCase().trim() || 'popup';
|
||||||
|
const scope = String(args?.scope || '').toLowerCase().trim() || 'all';
|
||||||
if (!chat_metadata.variables) {
|
if (!chat_metadata.variables) {
|
||||||
chat_metadata.variables = {};
|
chat_metadata.variables = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
const localVariables = Object.entries(chat_metadata.variables).map(([name, value]) => `${name}: ${value}`);
|
const includeLocalVariables = scope === 'all' || scope === 'local';
|
||||||
const globalVariables = Object.entries(extension_settings.variables.global).map(([name, value]) => `${name}: ${value}`);
|
const includeGlobalVariables = scope === 'all' || scope === 'global';
|
||||||
|
|
||||||
|
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 jsonVariables = [
|
||||||
|
...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' })),
|
||||||
|
];
|
||||||
|
|
||||||
const localVariablesString = localVariables.length > 0 ? localVariables.join('\n\n') : 'No local variables';
|
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 globalVariablesString = globalVariables.length > 0 ? globalVariables.join('\n\n') : 'No global variables';
|
||||||
const chatName = getCurrentChatId();
|
const chatName = getCurrentChatId();
|
||||||
|
|
||||||
const converter = new showdown.Converter();
|
const converter = new showdown.Converter();
|
||||||
const message = `### Local variables (${chatName}):\n${localVariablesString}\n\n### Global variables:\n${globalVariablesString}`;
|
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));
|
const htmlMessage = DOMPurify.sanitize(converter.makeHtml(message));
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
case 'none':
|
||||||
|
break;
|
||||||
|
case 'chat':
|
||||||
sendSystemMessage(system_message_types.GENERIC, htmlMessage);
|
sendSystemMessage(system_message_types.GENERIC, htmlMessage);
|
||||||
return '';
|
break;
|
||||||
|
case 'popup':
|
||||||
|
default:
|
||||||
|
await callGenericPopup(htmlMessage, POPUP_TYPE.TEXT);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return JSON.stringify(jsonVariables);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -887,7 +912,35 @@ export function registerVariableCommands() {
|
|||||||
name: 'listvar',
|
name: 'listvar',
|
||||||
callback: listVariablesCallback,
|
callback: listVariablesCallback,
|
||||||
aliases: ['listchatvar'],
|
aliases: ['listchatvar'],
|
||||||
helpString: 'List registered chat variables.',
|
helpString: 'List registered chat variables. Displays variables in a popup by default. Use the <code>format</code> argument to change the output format.',
|
||||||
|
returns: 'JSON list of local variables',
|
||||||
|
namedArgumentList: [
|
||||||
|
SlashCommandNamedArgument.fromProps({
|
||||||
|
name: 'scope',
|
||||||
|
description: 'filter variables by scope',
|
||||||
|
typeList: [ARGUMENT_TYPE.STRING],
|
||||||
|
defaultValue: 'all',
|
||||||
|
isRequired: false,
|
||||||
|
forceEnum: true,
|
||||||
|
enumList: [
|
||||||
|
new SlashCommandEnumValue('all', 'All variables', enumTypes.enum, enumIcons.variable),
|
||||||
|
new SlashCommandEnumValue('local', 'Local variables', enumTypes.enum, enumIcons.localVariable),
|
||||||
|
new SlashCommandEnumValue('global', 'Global variables', enumTypes.enum, enumIcons.globalVariable),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
SlashCommandNamedArgument.fromProps({
|
||||||
|
name: 'format',
|
||||||
|
description: 'output format',
|
||||||
|
typeList: [ARGUMENT_TYPE.STRING],
|
||||||
|
isRequired: true,
|
||||||
|
forceEnum: true,
|
||||||
|
enumList: [
|
||||||
|
new SlashCommandEnumValue('popup', 'Show variables in a popup.', enumTypes.enum, enumIcons.default),
|
||||||
|
new SlashCommandEnumValue('chat', 'Post a system message to the chat.', enumTypes.enum, enumIcons.message),
|
||||||
|
new SlashCommandEnumValue('none', 'Just return the variables as a JSON list.', enumTypes.enum, enumIcons.array),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
],
|
||||||
}));
|
}));
|
||||||
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
|
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
|
||||||
name: 'setvar',
|
name: 'setvar',
|
||||||
|
Reference in New Issue
Block a user