Execute profile commands callbacks directly

f926d9abd70d9fe1c106c8c934a3814fc44a1001

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +29 -11Showing whitespace changes
public/scripts/extensions/connection-manager/index.js+29 -11
@@ -1,12 +1,14 @@
11import { event_types, eventSource, main_api, saveSettingsDebounced } from '../../../script.js';
22import { extension_settings, renderExtensionTemplateAsync } from '../../extensions.js';
33import { callGenericPopup, Popup, POPUP_TYPE } from '../../popup.js';
4-import { executeSlashCommandsWithOptions } from '../../slash-commands.js';
54import { SlashCommand } from '../../slash-commands/SlashCommand.js';
5+import { SlashCommandAbortController } from '../../slash-commands/SlashCommandAbortController.js';
66import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from '../../slash-commands/SlashCommandArgument.js';
77import { commonEnumProviders, enumIcons } from '../../slash-commands/SlashCommandCommonEnumsProvider.js';
8+import { SlashCommandDebugController } from '../../slash-commands/SlashCommandDebugController.js';
89import { enumTypes, SlashCommandEnumValue } from '../../slash-commands/SlashCommandEnumValue.js';
910import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js';
11+import { SlashCommandScope } from '../../slash-commands/SlashCommandScope.js';
1012import { collapseSpaces, getUniqueName, isFalseBoolean, uuidv4 } from '../../utils.js';
1113
1214const MODULE_NAME = 'connection-manager';
@@ -91,6 +93,24 @@ class ConnectionManagerSpinner {
9193 }
9294}
9395
96+/**
97+ * Get named arguments for the command callback.
98+ * @param {object} [args] Additional named arguments
99+ * @returns {object} Named arguments
100+ */
101+function getNamedArguments(args = {}) {
102+ // None of the commands here use underscored args, but better safe than sorry
103+ return {
104+ _scope: new SlashCommandScope(),
105+ _abortController: new SlashCommandAbortController(),
106+ _debugController: new SlashCommandDebugController(),
107+ _parserFlags: {},
108+ _hasUnnamedArgument: false,
109+ quiet: 'true',
110+ ...args,
111+ };
112+}
113+
94114/** @type {() => SlashCommandEnumValue[]} */
95115const profilesProvider = () => [
96116 new SlashCommandEnumValue(NONE),
@@ -112,8 +132,6 @@ const profilesProvider = () => [
112132 * @property {string} [tokenizer] Tokenizer
113133 */
114134
115-const escapeArgument = (a) => a.replace(/"/g, '\\"').replace(/\|/g, '\\|');
116-
117135/**
118136 * Finds the best match for the search value.
119137 * @param {string} value Search value
@@ -149,15 +167,15 @@ async function readProfileFromCommands(mode, profile, cleanUp = false) {
149167 const commands = mode === 'cc' ? CC_COMMANDS : TC_COMMANDS;
150168 const opposingCommands = mode === 'cc' ? TC_COMMANDS : CC_COMMANDS;
151169 for (const command of commands) {
152- const commandText = `/${command} quiet=true`;
153170 try {
154- const result = await executeSlashCommandsWithOptions(commandText, { handleParserErrors: false, handleExecutionErrors: false });
171+ const args = getNamedArguments();
155- if (result.pipe) {
172+ const result = await SlashCommandParser.commands[command].callback(args, '');
156- profile[command] = result.pipe;
173+ if (result) {
174+ profile[command] = result;
157175 continue;
158176 }
159177 } catch (error) {
160178 console.warn(`Failed to execute command: ${commandTextcommand}`, error);
161179 }
162180 }
163181
@@ -273,11 +291,11 @@ async function applyConnectionProfile(profile) {
273291 if (!argument) {
274292 continue;
275293 }
276- const commandText = `/${command} quiet=true ${escapeArgument(argument)}`;
277294 try {
278- await executeSlashCommandsWithOptions(commandText, { handleParserErrors: false, handleExecutionErrors: false });
295+ const args = getNamedArguments();
296+ await SlashCommandParser.commands[command].callback(args, argument);
279297 } catch (error) {
280298 console.error(`Failed to execute command: ${commandTextcommand} ${argument}`, error);
281299 }
282300 }
283301