From bcac8c065b044028937d0482c7255989d00b8b45 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Sat, 7 Sep 2024 13:58:46 +0300 Subject: [PATCH] Add command for switching profiles --- .../extensions/connection-manager/index.js | 64 ++++++++++++++++++- .../SlashCommandCommonEnumsProvider.js | 1 + 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/public/scripts/extensions/connection-manager/index.js b/public/scripts/extensions/connection-manager/index.js index 90cbe7039..d15d1299c 100644 --- a/public/scripts/extensions/connection-manager/index.js +++ b/public/scripts/extensions/connection-manager/index.js @@ -2,9 +2,15 @@ import { main_api, saveSettingsDebounced } from '../../../script.js'; import { extension_settings, renderExtensionTemplateAsync } from '../../extensions.js'; import { callGenericPopup, Popup, POPUP_TYPE } from '../../popup.js'; import { executeSlashCommandsWithOptions } from '../../slash-commands.js'; +import { SlashCommand } from '../../slash-commands/SlashCommand.js'; +import { SlashCommandArgument } from '../../slash-commands/SlashCommandArgument.js'; +import { enumIcons } from '../../slash-commands/SlashCommandCommonEnumsProvider.js'; +import { enumTypes, SlashCommandEnumValue } from '../../slash-commands/SlashCommandEnumValue.js'; +import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js'; import { uuidv4 } from '../../utils.js'; const MODULE_NAME = 'connection-manager'; +const NONE = ''; const DEFAULT_SETTINGS = { profiles: [], @@ -43,6 +49,12 @@ const FANCY_NAMES = { 'tokenizer': 'Tokenizer', }; +/** @type {() => SlashCommandEnumValue[]} */ +const profilesProvider = () => [ + new SlashCommandEnumValue(NONE, NONE), + ...extension_settings.connectionManager.profiles.map(p => new SlashCommandEnumValue(p.name, p.name, enumTypes.name, enumIcons.server)), +]; + /** * @typedef {Object} ConnectionProfile * @property {string} id Unique identifier @@ -209,7 +221,7 @@ function renderConnectionProfiles(profiles) { const noneOption = document.createElement('option'); noneOption.value = ''; - noneOption.textContent = ''; + noneOption.textContent = NONE; noneOption.selected = !extension_settings.connectionManager.selectedProfile; profiles.appendChild(noneOption); @@ -333,4 +345,54 @@ async function renderDetailsContent(details, detailsContent) { const details = document.getElementById('connection_profile_details'); const detailsContent = document.getElementById('connection_profile_details_content'); details.addEventListener('toggle', () => renderDetailsContent(details, detailsContent)); + + SlashCommandParser.addCommandObject(SlashCommand.fromProps({ + name: 'profile', + helpString: 'Switch to a connection profile or return the name of the current profile in no argument is provided. Use <None> to switch to no profile.', + unnamedArgumentList: [ + SlashCommandArgument.fromProps({ + description: 'Name of the connection profile', + enumProvider: profilesProvider, + isRequired: false, + }), + ], + callback: async (_args, value) => { + if (!value || typeof value !== 'string') { + const selectedProfile = extension_settings.connectionManager.selectedProfile; + const profile = extension_settings.connectionManager.profiles.find(p => p.id === selectedProfile); + if (!profile) { + return NONE; + } + return profile.name; + } + + if (value === NONE) { + profiles.selectedIndex = 0; + profiles.dispatchEvent(new Event('change')); + return NONE; + } + + // Try to find exact match + const profile = extension_settings.connectionManager.profiles.find(p => p.name === value); + + if (profile) { + profiles.selectedIndex = Array.from(profiles.options).findIndex(o => o.value === profile.id); + profiles.dispatchEvent(new Event('change')); + return profile.name; + } + + // Try to find fuzzy match + const fuse = new Fuse(extension_settings.connectionManager.profiles, { keys: ['name'] }); + const results = fuse.search(value); + + if (results.length === 0) { + return ''; + } + + const bestMatch = results[0]; + profiles.value = bestMatch.item.id; + profiles.dispatchEvent(new Event('change')); + return bestMatch.item.name; + }, + })); })(); diff --git a/public/scripts/slash-commands/SlashCommandCommonEnumsProvider.js b/public/scripts/slash-commands/SlashCommandCommonEnumsProvider.js index ad064e4e1..c6876482b 100644 --- a/public/scripts/slash-commands/SlashCommandCommonEnumsProvider.js +++ b/public/scripts/slash-commands/SlashCommandCommonEnumsProvider.js @@ -33,6 +33,7 @@ export const enumIcons = { file: '📄', message: '💬', voice: '🎤', + server: '🖥️', true: '✔️', false: '❌',