Merge branch 'staging' into request-proxy

This commit is contained in:
Cohee
2024-09-12 20:17:22 +03:00

View File

@@ -428,6 +428,7 @@ export function initDefaultSlashCommands() {
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'ask', name: 'ask',
callback: askCharacter, callback: askCharacter,
returns: 'the generated text',
namedArgumentList: [ namedArgumentList: [
SlashCommandNamedArgument.fromProps({ SlashCommandNamedArgument.fromProps({
name: 'name', name: 'name',
@@ -439,7 +440,7 @@ export function initDefaultSlashCommands() {
], ],
unnamedArgumentList: [ unnamedArgumentList: [
new SlashCommandArgument( new SlashCommandArgument(
'prompt', [ARGUMENT_TYPE.STRING], true, false, 'prompt', [ARGUMENT_TYPE.STRING], false, false,
), ),
], ],
helpString: 'Asks a specified character card a prompt. Character name must be provided in a named argument.', helpString: 'Asks a specified character card a prompt. Character name must be provided in a named argument.',
@@ -2468,44 +2469,40 @@ async function askCharacter(args, text) {
// Not supported in group chats // Not supported in group chats
// TODO: Maybe support group chats? // TODO: Maybe support group chats?
if (selected_group) { if (selected_group) {
toastr.error('Cannot run this command in a group chat!'); toastr.error('Cannot run /ask command in a group chat!');
return '';
}
if (!text) {
console.warn('WARN: No text provided for /ask command');
toastr.warning('No text provided for /ask command');
return ''; return '';
} }
let name = ''; let name = '';
let mesText = '';
if (args?.name) { if (args?.name) {
name = args.name.trim(); name = args.name.trim();
mesText = text.trim();
if (!name && !mesText) { if (!name) {
toastr.warning('You must specify a name and text to ask.'); toastr.warning('You must specify a name of the character to ask.');
return ''; return '';
} }
} }
mesText = getRegexedString(mesText, regex_placement.SLASH_COMMAND);
const prevChId = this_chid; const prevChId = this_chid;
// Find the character // Find the character
const chId = characters.findIndex((e) => e.name === name); const chId = characters.findIndex((e) => e.name === name || e.avatar === name);
if (!characters[chId] || chId === -1) { if (!characters[chId] || chId === -1) {
toastr.error('Character not found.'); toastr.error('Character not found.');
return ''; return '';
} }
if (text) {
const mesText = getRegexedString(text.trim(), regex_placement.SLASH_COMMAND);
// Sending a message implicitly saves the chat, so this needs to be done before changing the character
// Otherwise, a corruption will occur
await sendMessageAsUser(mesText, '');
}
// Override character and send a user message // Override character and send a user message
setCharacterId(String(chId)); setCharacterId(String(chId));
// TODO: Maybe look up by filename instead of name
const character = characters[chId]; const character = characters[chId];
let force_avatar, original_avatar; let force_avatar, original_avatar;
@@ -2520,9 +2517,11 @@ async function askCharacter(args, text) {
setCharacterName(character.name); setCharacterName(character.name);
await sendMessageAsUser(mesText, '');
const restoreCharacter = () => { const restoreCharacter = () => {
if (String(this_chid) !== String(chId)) {
return;
}
setCharacterId(prevChId); setCharacterId(prevChId);
setCharacterName(characters[prevChId].name); setCharacterName(characters[prevChId].name);
@@ -2535,22 +2534,25 @@ async function askCharacter(args, text) {
} }
}; };
let askResult = '';
// Run generate and restore previous character // Run generate and restore previous character
try { try {
eventSource.once(event_types.MESSAGE_RECEIVED, restoreCharacter);
toastr.info(`Asking ${character.name} something...`); toastr.info(`Asking ${character.name} something...`);
await Generate('ask_command'); askResult = await Generate('ask_command');
await saveChatConditional();
} catch (error) { } catch (error) {
restoreCharacter();
console.error('Error running /ask command', error); console.error('Error running /ask command', error);
} finally { } finally {
restoreCharacter(); if (String(this_chid) === String(prevChId)) {
await saveChatConditional();
if (this_chid !== prevChId) { } else {
toastr.error('It is strongly recommended to reload the page.', 'Something went wrong'); toastr.error('It is strongly recommended to reload the page.', 'Something went wrong');
} }
} }
return ''; return askResult;
} }
async function hideMessageCallback(_, arg) { async function hideMessageCallback(_, arg) {