Refactor /classify-expressions, deprecating... - Update /classify-expressions, deprecating the old "format" - Fix some oversights

7a1b43eb8943b48747bd3efa139e81186346f964

Wolfsblvt <wolfsblvt@gmail.com>

4 files changed, +34 -8Showing whitespace changes
public/scripts/extensions/expressions/index.js+31 -5
@@ -12,6 +12,8 @@ import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from '
1212import { isFunctionCallingSupported } from '../../openai.js';
1313import { SlashCommandEnumValue, enumTypes } from '../../slash-commands/SlashCommandEnumValue.js';
1414import { commonEnumProviders } from '../../slash-commands/SlashCommandCommonEnumsProvider.js';
15+import { slashCommandReturnHelper } from '../../slash-commands/SlashCommandReturnHelper.js';
16+import { SlashCommandClosure } from '../../slash-commands/SlashCommandClosure.js';
1517export { MODULE_NAME };
1618
1719const MODULE_NAME = 'expressions';
@@ -2128,18 +2130,42 @@ function migrateSettings() {
21282130 name: 'classify-expressions',
21292131 aliases: ['expressions'],
21302132 callback: async (args) => {
2131- const list = await getExpressionsList();
2133+ /** @type {import('../../slash-commands/SlashCommandReturnHelper.js').SlashCommandReturnType} */
2132- switch (String(args.format).toLowerCase()) {
2134+ // @ts-ignore
2135+ let returnType = args.return;
2136+
2137+ // Old legacy return type handling
2138+ if (args.format) {
2139+ toastr.warning(`Legacy argument 'format' with value '${args.format}' is deprecated. Please use 'return' instead. Routing to the correct return type...`, 'Deprecation warning');
2140+ const type = String(args?.format).toLowerCase().trim();
2141+ switch (type) {
21332142 case 'json':
2134- return JSON.stringify(list);
2143+ returnType = 'object';
2144+ break;
21352145 default:
2136- return list.join(', ');
2146+ returnType = 'pipe';
2147+ break;
2148+ }
21372149 }
2150+
2151+ // Now the actual new return type handling
2152+ const list = await getExpressionsList();
2153+
2154+ return await slashCommandReturnHelper.doReturn(returnType ?? 'pipe', list, { objectToStringFunc: list => list.join(', ') });
21382155 },
21392156 namedArgumentList: [
21402157 SlashCommandNamedArgument.fromProps({
2158+ name: 'return',
2159+ description: 'The way how you want the return value to be provided',
2160+ typeList: [ARGUMENT_TYPE.STRING],
2161+ defaultValue: 'pipe',
2162+ enumList: slashCommandReturnHelper.enumList({ allowObject: true }),
2163+ forceEnum: true,
2164+ }),
2165+ // TODO remove some day
2166+ SlashCommandNamedArgument.fromProps({
21412167 name: 'format',
21422168 description: '!!! DEPRECATED - use "return" instead !!! The format to return the list in: comma-separated plain text or JSON array. Default is plain text.',
21432169 typeList: [ARGUMENT_TYPE.STRING],
21442170 enumList: [
21452171 new SlashCommandEnumValue('plain', null, enumTypes.enum, ', '),
public/scripts/slash-commands.js+1 -1
@@ -1538,7 +1538,7 @@ export function initDefaultSlashCommands() {
15381538 // TODO remove some day
15391539 SlashCommandNamedArgument.fromProps({
15401540 name: 'format',
15411541 description: '!!! DEPRECATED - use "return" instead !!! output format)',
15421542 typeList: [ARGUMENT_TYPE.STRING],
15431543 isRequired: true,
15441544 forceEnum: true,
public/scripts/slash-commands/SlashCommandReturnHelper.js+1 -1
@@ -23,7 +23,7 @@ export const slashCommandReturnHelper = {
2323 */
2424 enumList: ({ allowPipe = true, allowObject = false, allowChat = false, allowPopup = false, allowTextVersion = true } = {}) => [
2525 allowPipe && new SlashCommandEnumValue('pipe', 'Return to the pipe for the next command', enumTypes.name, '|'),
2626 allowObject && new SlashCommandEnumValue('object', 'Return as an object (or array) to the pipe for the next command', enumTypes.variable, enumIcons.dictionary),
2727 allowChat && new SlashCommandEnumValue('chat-html', 'Sending a chat message with the return value - Can display HTML', enumTypes.command, enumIcons.message),
2828 allowChat && allowTextVersion && new SlashCommandEnumValue('chat-text', 'Sending a chat message with the return value - Will only display as text', enumTypes.qr, enumIcons.message),
2929 allowPopup && new SlashCommandEnumValue('popup-html', 'Showing as a popup with the return value - Can display HTML', enumTypes.command, enumIcons.popup),
public/scripts/variables.js+1 -1
@@ -951,7 +951,7 @@ export function registerVariableCommands() {
951951 // TODO remove some day
952952 SlashCommandNamedArgument.fromProps({
953953 name: 'format',
954954 description: '!!! DEPRECATED - use "return" instead !!! output format)',
955955 typeList: [ARGUMENT_TYPE.STRING],
956956 isRequired: true,
957957 forceEnum: true,