Await for profiles loading before continuing

This commit is contained in:
Cohee
2024-09-07 21:33:31 +03:00
parent 739d0c95c3
commit 10ddf77948
2 changed files with 33 additions and 7 deletions

View File

@@ -462,6 +462,7 @@ export const event_types = {
LLM_FUNCTION_TOOL_CALL: 'llm_function_tool_call', LLM_FUNCTION_TOOL_CALL: 'llm_function_tool_call',
ONLINE_STATUS_CHANGED: 'online_status_changed', ONLINE_STATUS_CHANGED: 'online_status_changed',
IMAGE_SWIPED: 'image_swiped', IMAGE_SWIPED: 'image_swiped',
CONNECTION_PROFILE_LOADED: 'connection_profile_loaded',
}; };
export const eventSource = new EventEmitter(); export const eventSource = new EventEmitter();

View File

@@ -1,13 +1,13 @@
import { main_api, saveSettingsDebounced } from '../../../script.js'; import { event_types, eventSource, main_api, saveSettingsDebounced } from '../../../script.js';
import { extension_settings, renderExtensionTemplateAsync } from '../../extensions.js'; import { extension_settings, renderExtensionTemplateAsync } from '../../extensions.js';
import { callGenericPopup, Popup, POPUP_TYPE } from '../../popup.js'; import { callGenericPopup, Popup, POPUP_TYPE } from '../../popup.js';
import { executeSlashCommandsWithOptions } from '../../slash-commands.js'; import { executeSlashCommandsWithOptions } from '../../slash-commands.js';
import { SlashCommand } from '../../slash-commands/SlashCommand.js'; import { SlashCommand } from '../../slash-commands/SlashCommand.js';
import { ARGUMENT_TYPE, SlashCommandArgument } from '../../slash-commands/SlashCommandArgument.js'; import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from '../../slash-commands/SlashCommandArgument.js';
import { enumIcons } from '../../slash-commands/SlashCommandCommonEnumsProvider.js'; import { commonEnumProviders, enumIcons } from '../../slash-commands/SlashCommandCommonEnumsProvider.js';
import { enumTypes, SlashCommandEnumValue } from '../../slash-commands/SlashCommandEnumValue.js'; import { enumTypes, SlashCommandEnumValue } from '../../slash-commands/SlashCommandEnumValue.js';
import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js'; import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js';
import { collapseSpaces, getUniqueName, uuidv4 } from '../../utils.js'; import { collapseSpaces, getUniqueName, isFalseBoolean, uuidv4 } from '../../utils.js';
const MODULE_NAME = 'connection-manager'; const MODULE_NAME = 'connection-manager';
const NONE = '<None>'; const NONE = '<None>';
@@ -51,7 +51,7 @@ const FANCY_NAMES = {
/** @type {() => SlashCommandEnumValue[]} */ /** @type {() => SlashCommandEnumValue[]} */
const profilesProvider = () => [ const profilesProvider = () => [
new SlashCommandEnumValue(NONE, NONE), new SlashCommandEnumValue(NONE),
...extension_settings.connectionManager.profiles.map(p => new SlashCommandEnumValue(p.name, null, enumTypes.name, enumIcons.server)), ...extension_settings.connectionManager.profiles.map(p => new SlashCommandEnumValue(p.name, null, enumTypes.name, enumIcons.server)),
]; ];
@@ -155,7 +155,7 @@ async function createConnectionProfile(forceName = null) {
return null; return null;
} }
if (isNameTaken(name)) { if (isNameTaken(name) || name === NONE) {
toastr.error('A profile with the same name already exists.'); toastr.error('A profile with the same name already exists.');
return null; return null;
} }
@@ -304,6 +304,8 @@ async function renderDetailsContent(details, detailsContent) {
profiles.addEventListener('change', async function () { profiles.addEventListener('change', async function () {
const selectedProfile = profiles.selectedOptions[0]; const selectedProfile = profiles.selectedOptions[0];
if (!selectedProfile) { if (!selectedProfile) {
// Safety net for preventing the command getting stuck
await eventSource.emit(event_types.CONNECTION_PROFILE_LOADED, NONE);
return; return;
} }
@@ -314,6 +316,7 @@ async function renderDetailsContent(details, detailsContent) {
// None option selected // None option selected
if (!profileId) { if (!profileId) {
await eventSource.emit(event_types.CONNECTION_PROFILE_LOADED, NONE);
return; return;
} }
@@ -325,6 +328,7 @@ async function renderDetailsContent(details, detailsContent) {
} }
await applyConnectionProfile(profile); await applyConnectionProfile(profile);
await eventSource.emit(event_types.CONNECTION_PROFILE_LOADED, profile.name);
}); });
const reloadButton = document.getElementById('reload_connection_profile'); const reloadButton = document.getElementById('reload_connection_profile');
@@ -337,6 +341,7 @@ async function renderDetailsContent(details, detailsContent) {
} }
await applyConnectionProfile(profile); await applyConnectionProfile(profile);
await renderDetailsContent(details, detailsContent); await renderDetailsContent(details, detailsContent);
await eventSource.emit(event_types.CONNECTION_PROFILE_LOADED, profile.name);
toastr.success('Connection profile reloaded', '', { timeOut: 1500 }); toastr.success('Connection profile reloaded', '', { timeOut: 1500 });
}); });
@@ -351,6 +356,7 @@ async function renderDetailsContent(details, detailsContent) {
saveSettingsDebounced(); saveSettingsDebounced();
renderConnectionProfiles(profiles); renderConnectionProfiles(profiles);
await renderDetailsContent(details, detailsContent); await renderDetailsContent(details, detailsContent);
await eventSource.emit(event_types.CONNECTION_PROFILE_LOADED, profile.name);
}); });
const updateButton = document.getElementById('update_connection_profile'); const updateButton = document.getElementById('update_connection_profile');
@@ -364,6 +370,7 @@ async function renderDetailsContent(details, detailsContent) {
await updateConnectionProfile(profile); await updateConnectionProfile(profile);
await renderDetailsContent(details, detailsContent); await renderDetailsContent(details, detailsContent);
saveSettingsDebounced(); saveSettingsDebounced();
await eventSource.emit(event_types.CONNECTION_PROFILE_LOADED, profile.name);
toastr.success('Connection profile updated', '', { timeOut: 1500 }); toastr.success('Connection profile updated', '', { timeOut: 1500 });
}); });
@@ -372,6 +379,7 @@ async function renderDetailsContent(details, detailsContent) {
await deleteConnectionProfile(); await deleteConnectionProfile();
renderConnectionProfiles(profiles); renderConnectionProfiles(profiles);
await renderDetailsContent(details, detailsContent); await renderDetailsContent(details, detailsContent);
await eventSource.emit(event_types.CONNECTION_PROFILE_LOADED, NONE);
}); });
/** @type {HTMLDetailsElement} */ /** @type {HTMLDetailsElement} */
@@ -391,7 +399,17 @@ async function renderDetailsContent(details, detailsContent) {
isRequired: false, isRequired: false,
}), }),
], ],
callback: async (_args, value) => { namedArgumentList: [
SlashCommandNamedArgument.fromProps({
name: 'await',
description: 'Wait for the connection profile to be applied before returning.',
isRequired: false,
typeList: [ARGUMENT_TYPE.BOOLEAN],
defaultValue: 'true',
enumList: commonEnumProviders.boolean('trueFalse')(),
}),
],
callback: async (args, value) => {
if (!value || typeof value !== 'string') { if (!value || typeof value !== 'string') {
const selectedProfile = extension_settings.connectionManager.selectedProfile; const selectedProfile = extension_settings.connectionManager.selectedProfile;
const profile = extension_settings.connectionManager.profiles.find(p => p.id === selectedProfile); const profile = extension_settings.connectionManager.profiles.find(p => p.id === selectedProfile);
@@ -413,9 +431,16 @@ async function renderDetailsContent(details, detailsContent) {
return ''; return '';
} }
const shouldAwait = !isFalseBoolean(String(args?.await));
const awaitPromise = new Promise((resolve) => eventSource.once(event_types.CONNECTION_PROFILE_LOADED, resolve));
profiles.selectedIndex = Array.from(profiles.options).findIndex(o => o.value === profile.id); profiles.selectedIndex = Array.from(profiles.options).findIndex(o => o.value === profile.id);
profiles.dispatchEvent(new Event('change')); profiles.dispatchEvent(new Event('change'));
if (shouldAwait) {
await awaitPromise;
}
return profile.name; return profile.name;
}, },
})); }));