Add commands to list, create, update profiles
| @@ -3,7 +3,7 @@ import { extension_settings, renderExtensionTemplateAsync } from '../../extensio | |||
| 3 | import { callGenericPopup, Popup, POPUP_TYPE } from '../../popup.js'; | 3 | import { callGenericPopup, Popup, POPUP_TYPE } from '../../popup.js'; |
| 4 | import { executeSlashCommandsWithOptions } from '../../slash-commands.js'; | 4 | import { executeSlashCommandsWithOptions } from '../../slash-commands.js'; |
| 5 | import { SlashCommand } from '../../slash-commands/SlashCommand.js'; | 5 | import { SlashCommand } from '../../slash-commands/SlashCommand.js'; |
| 6 | import { SlashCommandArgument } from '../../slash-commands/SlashCommandArgument.js'; | 6 | import { ARGUMENT_TYPE, SlashCommandArgument } from '../../slash-commands/SlashCommandArgument.js'; |
| 7 | import { enumIcons } from '../../slash-commands/SlashCommandCommonEnumsProvider.js'; | 7 | import { enumIcons } from '../../slash-commands/SlashCommandCommonEnumsProvider.js'; |
| 8 | import { enumTypes, SlashCommandEnumValue } from '../../slash-commands/SlashCommandEnumValue.js'; | 8 | import { enumTypes, SlashCommandEnumValue } from '../../slash-commands/SlashCommandEnumValue.js'; |
| 9 | import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js'; | 9 | import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js'; |
| @@ -109,9 +109,10 @@ async function readProfileFromCommands(mode, profile, cleanUp = false) { | |||
| 109 | 109 | ||
| 110 | /** | 110 | /** |
| 111 | * Creates a new connection profile. | 111 | * Creates a new connection profile. |
| 112 | * @param {string} [forceName] Name of the connection profile | ||
| 112 | * @returns {Promise<ConnectionProfile>} Created connection profile | 113 | * @returns {Promise<ConnectionProfile>} Created connection profile |
| 113 | */ | 114 | */ |
| 114 | async function createConnectionProfile() { | 115 | async function createConnectionProfile(forceName = null) { |
| 115 | const mode = main_api === 'openai' ? 'cc' : 'tc'; | 116 | const mode = main_api === 'openai' ? 'cc' : 'tc'; |
| 116 | const id = uuidv4(); | 117 | const id = uuidv4(); |
| 117 | const profile = { | 118 | const profile = { |
| @@ -125,7 +126,7 @@ async function createConnectionProfile() { | |||
| 125 | const template = await renderExtensionTemplateAsync(MODULE_NAME, 'profile', { profile: profileForDisplay }); | 126 | const template = await renderExtensionTemplateAsync(MODULE_NAME, 'profile', { profile: profileForDisplay }); |
| 126 | const checkName = (n) => extension_settings.connectionManager.profiles.some(p => p.name === n); | 127 | const checkName = (n) => extension_settings.connectionManager.profiles.some(p => p.name === n); |
| 127 | const suggestedName = makeUnique(collapseSpaces(`${profile.api ?? ''} ${profile.model ?? ''} - ${profile.preset ?? ''}`), checkName, 1); | 128 | const suggestedName = makeUnique(collapseSpaces(`${profile.api ?? ''} ${profile.model ?? ''} - ${profile.preset ?? ''}`), checkName, 1); |
| 128 | const name = await callGenericPopup(template, POPUP_TYPE.INPUT, suggestedName, { rows: 2 }); | 129 | const name = forceName ?? await callGenericPopup(template, POPUP_TYPE.INPUT, suggestedName, { rows: 2 }); |
| 129 | 130 | ||
| 130 | if (!name) { | 131 | if (!name) { |
| 131 | return null; | 132 | return null; |
| @@ -349,6 +350,7 @@ async function renderDetailsContent(details, detailsContent) { | |||
| 349 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ | 350 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ |
| 350 | name: 'profile', | 351 | name: 'profile', |
| 351 | helpString: 'Switch to a connection profile or return the name of the current profile in no argument is provided. Use <code><None></code> to switch to no profile.', | 352 | helpString: 'Switch to a connection profile or return the name of the current profile in no argument is provided. Use <code><None></code> to switch to no profile.', |
| 353 | returns: 'name of the profile', | ||
| 352 | unnamedArgumentList: [ | 354 | unnamedArgumentList: [ |
| 353 | SlashCommandArgument.fromProps({ | 355 | SlashCommandArgument.fromProps({ |
| 354 | description: 'Name of the connection profile', | 356 | description: 'Name of the connection profile', |
| @@ -395,4 +397,55 @@ async function renderDetailsContent(details, detailsContent) { | |||
| 395 | return bestMatch.item.name; | 397 | return bestMatch.item.name; |
| 396 | }, | 398 | }, |
| 397 | })); | 399 | })); |
| 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 | })); | ||
| 398 | })(); | 451 | })(); |