Merge branch 'staging' into request-proxy

051cdf554889269cb5f6a40b38b70dc28dd46560

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

1 files changed, +26 -24Showing whitespace changes
public/scripts/slash-commands.js+26 -24
@@ -428,6 +428,7 @@ export function initDefaultSlashCommands() {
428428 SlashCommandParser.addCommandObject(SlashCommand.fromProps({
429429 name: 'ask',
430430 callback: askCharacter,
431+ returns: 'the generated text',
431432 namedArgumentList: [
432433 SlashCommandNamedArgument.fromProps({
433434 name: 'name',
@@ -439,7 +440,7 @@ export function initDefaultSlashCommands() {
439440 ],
440441 unnamedArgumentList: [
441442 new SlashCommandArgument(
442443 'prompt', [ARGUMENT_TYPE.STRING], truefalse, false,
443444 ),
444445 ],
445446 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) {
24682469 // Not supported in group chats
24692470 // TODO: Maybe support group chats?
24702471 if (selected_group) {
24712472 toastr.error('Cannot run this/ask command in a group chat!');
2472- return '';
2473- }
2474-
2475- if (!text) {
2476- console.warn('WARN: No text provided for /ask command');
2477- toastr.warning('No text provided for /ask command');
24782473 return '';
24792474 }
24802475
24812476 let name = '';
2482- let mesText = '';
24832477
24842478 if (args?.name) {
24852479 name = args.name.trim();
2486- mesText = text.trim();
24872480
24882481 if (!name && !mesText) {
24892482 toastr.warning('You must specify a name andof textthe character to ask.');
24902483 return '';
24912484 }
24922485 }
24932486
2494- mesText = getRegexedString(mesText, regex_placement.SLASH_COMMAND);
2495-
24962487 const prevChId = this_chid;
24972488
24982489 // Find the character
24992490 const chId = characters.findIndex((e) => e.name === name || e.avatar === name);
25002491 if (!characters[chId] || chId === -1) {
25012492 toastr.error('Character not found.');
25022493 return '';
25032494 }
25042495
2496+ if (text) {
2497+ const mesText = getRegexedString(text.trim(), regex_placement.SLASH_COMMAND);
2498+ // Sending a message implicitly saves the chat, so this needs to be done before changing the character
2499+ // Otherwise, a corruption will occur
2500+ await sendMessageAsUser(mesText, '');
2501+ }
2502+
25052503 // Override character and send a user message
25062504 setCharacterId(String(chId));
25072505
2508- // TODO: Maybe look up by filename instead of name
25092506 const character = characters[chId];
25102507 let force_avatar, original_avatar;
25112508
@@ -2520,9 +2517,11 @@ async function askCharacter(args, text) {
25202517
25212518 setCharacterName(character.name);
25222519
2523- await sendMessageAsUser(mesText, '');
2524-
25252520 const restoreCharacter = () => {
2521+ if (String(this_chid) !== String(chId)) {
2522+ return;
2523+ }
2524+
25262525 setCharacterId(prevChId);
25272526 setCharacterName(characters[prevChId].name);
25282527
@@ -2535,22 +2534,25 @@ async function askCharacter(args, text) {
25352534 }
25362535 };
25372536
2537+ let askResult = '';
2538+
25382539 // Run generate and restore previous character
25392540 try {
2541+ eventSource.once(event_types.MESSAGE_RECEIVED, restoreCharacter);
25402542 toastr.info(`Asking ${character.name} something...`);
25412543 askResult = await Generate('ask_command');
2542- await saveChatConditional();
25432544 } catch (error) {
2545+ restoreCharacter();
25442546 console.error('Error running /ask command', error);
25452547 } finally {
2546- restoreCharacter();
2548+ if (String(this_chid) === String(prevChId)) {
2547-
2549+ await saveChatConditional();
2548- if (this_chid !== prevChId) {
2550+ } else {
25492551 toastr.error('It is strongly recommended to reload the page.', 'Something went wrong');
25502552 }
25512553 }
25522554
25532555 return ''askResult;
25542556}
25552557
25562558async function hideMessageCallback(_, arg) {