Add commands to list, create, update profiles

065daa7599589d627a6827d14a78f6842149a124

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

1 files changed, +56 -3Showing whitespace changes
public/scripts/extensions/connection-manager/index.js+56 -3
@@ -3,7 +3,7 @@ import { extension_settings, renderExtensionTemplateAsync } from '../../extensio
33import { callGenericPopup, Popup, POPUP_TYPE } from '../../popup.js';
44import { executeSlashCommandsWithOptions } from '../../slash-commands.js';
55import { SlashCommand } from '../../slash-commands/SlashCommand.js';
66import { ARGUMENT_TYPE, SlashCommandArgument } from '../../slash-commands/SlashCommandArgument.js';
77import { enumIcons } from '../../slash-commands/SlashCommandCommonEnumsProvider.js';
88import { enumTypes, SlashCommandEnumValue } from '../../slash-commands/SlashCommandEnumValue.js';
99import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js';
@@ -109,9 +109,10 @@ async function readProfileFromCommands(mode, profile, cleanUp = false) {
109109
110110/**
111111 * Creates a new connection profile.
112+ * @param {string} [forceName] Name of the connection profile
112113 * @returns {Promise<ConnectionProfile>} Created connection profile
113114 */
114115async function createConnectionProfile(forceName = null) {
115116 const mode = main_api === 'openai' ? 'cc' : 'tc';
116117 const id = uuidv4();
117118 const profile = {
@@ -125,7 +126,7 @@ async function createConnectionProfile() {
125126 const template = await renderExtensionTemplateAsync(MODULE_NAME, 'profile', { profile: profileForDisplay });
126127 const checkName = (n) => extension_settings.connectionManager.profiles.some(p => p.name === n);
127128 const suggestedName = makeUnique(collapseSpaces(`${profile.api ?? ''} ${profile.model ?? ''} - ${profile.preset ?? ''}`), checkName, 1);
128129 const name = forceName ?? await callGenericPopup(template, POPUP_TYPE.INPUT, suggestedName, { rows: 2 });
129130
130131 if (!name) {
131132 return null;
@@ -349,6 +350,7 @@ async function renderDetailsContent(details, detailsContent) {
349350 SlashCommandParser.addCommandObject(SlashCommand.fromProps({
350351 name: 'profile',
351352 helpString: 'Switch to a connection profile or return the name of the current profile in no argument is provided. Use <code>&lt;None&gt;</code> to switch to no profile.',
353+ returns: 'name of the profile',
352354 unnamedArgumentList: [
353355 SlashCommandArgument.fromProps({
354356 description: 'Name of the connection profile',
@@ -395,4 +397,55 @@ async function renderDetailsContent(details, detailsContent) {
395397 return bestMatch.item.name;
396398 },
397399 }));
400+
401+ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
402+ name: 'profile-list',
403+ helpString: 'List all connection profile names.',
404+ returns: 'list of profile names',
405+ callback: () => JSON.stringify(extension_settings.connectionManager.profiles.map(p => p.name)),
406+ }));
407+
408+ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
409+ name: 'profile-create',
410+ returns: 'name of the new profile',
411+ helpString: 'Create a new connection profile using the current settings.',
412+ unnamedArgumentList: [
413+ SlashCommandArgument.fromProps({
414+ description: 'name of the new connection profile',
415+ isRequired: true,
416+ typeList: [ARGUMENT_TYPE.STRING],
417+ }),
418+ ],
419+ callback: async (_args, name) => {
420+ if (!name || typeof name !== 'string') {
421+ toastr.warning('Please provide a name for the new connection profile.');
422+ return '';
423+ }
424+ const profile = await createConnectionProfile(name);
425+ if (!profile) {
426+ return '';
427+ }
428+ extension_settings.connectionManager.profiles.push(profile);
429+ extension_settings.connectionManager.selectedProfile = profile.id;
430+ saveSettingsDebounced();
431+ renderConnectionProfiles(profiles);
432+ await renderDetailsContent(details, detailsContent);
433+ return profile.name;
434+ },
435+ }));
436+
437+ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
438+ name: 'profile-update',
439+ helpString: 'Update the selected connection profile.',
440+ callback: async () => {
441+ const selectedProfile = extension_settings.connectionManager.selectedProfile;
442+ const profile = extension_settings.connectionManager.profiles.find(p => p.id === selectedProfile);
443+ if (!profile) {
444+ return '';
445+ }
446+ await updateConnectionProfile(profile);
447+ await renderDetailsContent(details, detailsContent);
448+ saveSettingsDebounced();
449+ },
450+ }));
398451})();