From 0646a47b810fa595ff7fc928f1b755fec487e42c Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Wed, 25 Sep 2024 14:23:43 +0000 Subject: [PATCH] Add ability to omit settings from Connection Profiles --- .../extensions/connection-manager/edit.html | 16 ++++ .../extensions/connection-manager/index.js | 80 +++++++++++++++++-- .../connection-manager/profile.html | 13 ++- .../connection-manager/settings.html | 2 +- 4 files changed, 99 insertions(+), 12 deletions(-) create mode 100644 public/scripts/extensions/connection-manager/edit.html diff --git a/public/scripts/extensions/connection-manager/edit.html b/public/scripts/extensions/connection-manager/edit.html new file mode 100644 index 000000000..9676e218e --- /dev/null +++ b/public/scripts/extensions/connection-manager/edit.html @@ -0,0 +1,16 @@ +
+

Included settings:

+
+ {{#each settings}} + + {{/each}} +
+

Profile name:

+
diff --git a/public/scripts/extensions/connection-manager/index.js b/public/scripts/extensions/connection-manager/index.js index e00d0660d..cb26c0ce9 100644 --- a/public/scripts/extensions/connection-manager/index.js +++ b/public/scripts/extensions/connection-manager/index.js @@ -135,6 +135,7 @@ const profilesProvider = () => [ * @property {string} [context] Context Template * @property {string} [instruct-state] Instruct Mode * @property {string} [tokenizer] Tokenizer + * @property {string[]} [exclude] Commands to exclude */ /** @@ -171,8 +172,13 @@ function findProfileByName(value) { async function readProfileFromCommands(mode, profile, cleanUp = false) { const commands = mode === 'cc' ? CC_COMMANDS : TC_COMMANDS; const opposingCommands = mode === 'cc' ? TC_COMMANDS : CC_COMMANDS; + const excludeList = Array.isArray(profile.exclude) ? profile.exclude : []; for (const command of commands) { try { + if (excludeList.includes(command)) { + continue; + } + const args = getNamedArguments(); const result = await SlashCommandParser.commands[command].callback(args, ''); if (result) { @@ -208,15 +214,36 @@ async function readProfileFromCommands(mode, profile, cleanUp = false) { async function createConnectionProfile(forceName = null) { const mode = main_api === 'openai' ? 'cc' : 'tc'; const id = uuidv4(); + /** @type {ConnectionProfile} */ const profile = { id, mode, + exclude: [], }; await readProfileFromCommands(mode, profile); const profileForDisplay = makeFancyProfile(profile); - const template = await renderExtensionTemplateAsync(MODULE_NAME, 'profile', { profile: profileForDisplay }); + const template = $(await renderExtensionTemplateAsync(MODULE_NAME, 'profile', { profile: profileForDisplay })); + template.find('input[name="exclude"]').on('input', function () { + const fancyName = String($(this).val()); + const keyName = Object.entries(FANCY_NAMES).find(x => x[1] === fancyName)?.[0]; + if (!keyName) { + console.warn('Key not found for fancy name:', fancyName); + return; + } + + if (!Array.isArray(profile.exclude)) { + profile.exclude = []; + } + + const excludeState = !$(this).prop('checked'); + if (excludeState) { + profile.exclude.push(keyName); + } else { + profile.exclude.splice(profile.exclude.indexOf(keyName), 1); + } + }); const isNameTaken = (n) => extension_settings.connectionManager.profiles.some(p => p.name === n); const suggestedName = getUniqueName(collapseSpaces(`${profile.api ?? ''} ${profile.model ?? ''} - ${profile.preset ?? ''}`), isNameTaken); const name = forceName ?? await callGenericPopup(template, POPUP_TYPE.INPUT, suggestedName, { rows: 2 }); @@ -230,7 +257,13 @@ async function createConnectionProfile(forceName = null) { return null; } - profile.name = name; + if (Array.isArray(profile.exclude)) { + for (const command of profile.exclude) { + delete profile[command]; + } + } + + profile.name = String(name); return profile; } @@ -357,6 +390,9 @@ async function renderDetailsContent(detailsContent) { const profile = extension_settings.connectionManager.profiles.find(p => p.id === selectedProfile); if (profile) { const profileForDisplay = makeFancyProfile(profile); + if (Array.isArray(profile.exclude) && profile.exclude.length > 0) { + profileForDisplay['Omitted Settings'] = profile.exclude.map(e => FANCY_NAMES[e]).join(', '); + } const template = await renderExtensionTemplateAsync(MODULE_NAME, 'view', { profile: profileForDisplay }); detailsContent.innerHTML = template; } else { @@ -472,8 +508,8 @@ async function renderDetailsContent(detailsContent) { await eventSource.emit(event_types.CONNECTION_PROFILE_LOADED, NONE); }); - const renameButton = document.getElementById('rename_connection_profile'); - renameButton.addEventListener('click', async () => { + const editButton = document.getElementById('edit_connection_profile'); + editButton.addEventListener('click', async () => { const selectedProfile = extension_settings.connectionManager.selectedProfile; const profile = extension_settings.connectionManager.profiles.find(p => p.id === selectedProfile); if (!profile) { @@ -481,20 +517,48 @@ async function renderDetailsContent(detailsContent) { return; } - const newName = await Popup.show.input('Enter a new name', null, profile.name, { rows: 2 }); + if (!Array.isArray(profile.exclude)) { + profile.exclude = []; + } + + const commands = profile.mode === 'cc' ? CC_COMMANDS : TC_COMMANDS; + const settings = commands.reduce((acc, command) => { + const fancyName = FANCY_NAMES[command]; + acc[fancyName] = !profile.exclude.includes(command); + return acc; + }, {}); + const template = $(await renderExtensionTemplateAsync(MODULE_NAME, 'edit', { name: profile.name, settings })); + const newName = await callGenericPopup(template, POPUP_TYPE.INPUT, profile.name); + if (!newName) { return; } - if (extension_settings.connectionManager.profiles.some(p => p.name === newName)) { + if (profile.name !== newName && extension_settings.connectionManager.profiles.some(p => p.name === newName)) { toastr.error('A profile with the same name already exists.'); return; } - profile.name = newName; + const newExcludeList = template.find('input[name="exclude"]:not(:checked)').map(function () { + return Object.entries(FANCY_NAMES).find(x => x[1] === String($(this).val()))?.[0]; + }).get(); + + if (newExcludeList.length !== profile.exclude.length || !newExcludeList.every(e => profile.exclude.includes(e))) { + profile.exclude = newExcludeList; + for (const command of newExcludeList) { + delete profile[command]; + } + toastr.info('Press "Update" to record them into the profile.', 'Included settings list updated'); + } + + if (profile.name !== newName) { + toastr.success('Connection profile renamed.'); + profile.name = String(newName); + } + saveSettingsDebounced(); renderConnectionProfiles(profiles); - toastr.success('Connection profile renamed', '', { timeOut: 1500 }); + await renderDetailsContent(detailsContent); }); /** @type {HTMLElement} */ diff --git a/public/scripts/extensions/connection-manager/profile.html b/public/scripts/extensions/connection-manager/profile.html index d8be91561..bc2f9d352 100644 --- a/public/scripts/extensions/connection-manager/profile.html +++ b/public/scripts/extensions/connection-manager/profile.html @@ -2,11 +2,18 @@

Creating a Connection Profile

- + + + Hint: + Click on the setting name to omit it from the profile. +

Enter a name:

diff --git a/public/scripts/extensions/connection-manager/settings.html b/public/scripts/extensions/connection-manager/settings.html index 03c13a4b5..e94a16bd5 100644 --- a/public/scripts/extensions/connection-manager/settings.html +++ b/public/scripts/extensions/connection-manager/settings.html @@ -13,7 +13,7 @@ - +