Merge branch 'staging' into request-proxy
| @@ -428,6 +428,7 @@ export function initDefaultSlashCommands() { | ||
| 428 | 428 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ |
| 429 | 429 | name: 'ask', |
| 430 | 430 | callback: askCharacter, |
| 431 | + returns: 'the generated text', | |
| 431 | 432 | namedArgumentList: [ |
| 432 | 433 | SlashCommandNamedArgument.fromProps({ |
| 433 | 434 | name: 'name', |
| @@ -439,7 +440,7 @@ export function initDefaultSlashCommands() { | ||
| 439 | 440 | ], |
| 440 | 441 | unnamedArgumentList: [ |
| 441 | 442 | new SlashCommandArgument( |
| 442 | 443 | 'prompt', [ARGUMENT_TYPE.STRING], truefalse, false, |
| 443 | 444 | ), |
| 444 | 445 | ], |
| 445 | 446 | 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) { | ||
| 2468 | 2469 | // Not supported in group chats |
| 2469 | 2470 | // TODO: Maybe support group chats? |
| 2470 | 2471 | if (selected_group) { |
| 2471 | 2472 | 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'); | |
| 2478 | 2473 | return ''; |
| 2479 | 2474 | } |
| 2480 | 2475 | |
| 2481 | 2476 | let name = ''; |
| 2482 | - let mesText = ''; | |
| 2483 | 2477 | |
| 2484 | 2478 | if (args?.name) { |
| 2485 | 2479 | name = args.name.trim(); |
| 2486 | - mesText = text.trim(); | |
| 2487 | 2480 | |
| 2488 | 2481 | if (!name && !mesText) { |
| 2489 | 2482 | toastr.warning('You must specify a name andof textthe character to ask.'); |
| 2490 | 2483 | return ''; |
| 2491 | 2484 | } |
| 2492 | 2485 | } |
| 2493 | 2486 | |
| 2494 | - mesText = getRegexedString(mesText, regex_placement.SLASH_COMMAND); | |
| 2495 | - | |
| 2496 | 2487 | const prevChId = this_chid; |
| 2497 | 2488 | |
| 2498 | 2489 | // Find the character |
| 2499 | 2490 | const chId = characters.findIndex((e) => e.name === name || e.avatar === name); |
| 2500 | 2491 | if (!characters[chId] || chId === -1) { |
| 2501 | 2492 | toastr.error('Character not found.'); |
| 2502 | 2493 | return ''; |
| 2503 | 2494 | } |
| 2504 | 2495 | |
| 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 | + | |
| 2505 | 2503 | // Override character and send a user message |
| 2506 | 2504 | setCharacterId(String(chId)); |
| 2507 | 2505 | |
| 2508 | - // TODO: Maybe look up by filename instead of name | |
| 2509 | 2506 | const character = characters[chId]; |
| 2510 | 2507 | let force_avatar, original_avatar; |
| 2511 | 2508 | |
| @@ -2520,9 +2517,11 @@ async function askCharacter(args, text) { | ||
| 2520 | 2517 | |
| 2521 | 2518 | setCharacterName(character.name); |
| 2522 | 2519 | |
| 2523 | - await sendMessageAsUser(mesText, ''); | |
| 2524 | - | |
| 2525 | 2520 | const restoreCharacter = () => { |
| 2521 | + if (String(this_chid) !== String(chId)) { | |
| 2522 | + return; | |
| 2523 | + } | |
| 2524 | + | |
| 2526 | 2525 | setCharacterId(prevChId); |
| 2527 | 2526 | setCharacterName(characters[prevChId].name); |
| 2528 | 2527 | |
| @@ -2535,22 +2534,25 @@ async function askCharacter(args, text) { | ||
| 2535 | 2534 | } |
| 2536 | 2535 | }; |
| 2537 | 2536 | |
| 2537 | + let askResult = ''; | |
| 2538 | + | |
| 2538 | 2539 | // Run generate and restore previous character |
| 2539 | 2540 | try { |
| 2541 | + eventSource.once(event_types.MESSAGE_RECEIVED, restoreCharacter); | |
| 2540 | 2542 | toastr.info(`Asking ${character.name} something...`); |
| 2541 | 2543 | askResult = await Generate('ask_command'); |
| 2542 | - await saveChatConditional(); | |
| 2543 | 2544 | } catch (error) { |
| 2545 | + restoreCharacter(); | |
| 2544 | 2546 | console.error('Error running /ask command', error); |
| 2545 | 2547 | } finally { |
| 2546 | - restoreCharacter(); | |
| 2548 | + if (String(this_chid) === String(prevChId)) { | |
| 2547 | - | |
| 2549 | + await saveChatConditional(); | |
| 2548 | - if (this_chid !== prevChId) { | |
| 2550 | + } else { | |
| 2549 | 2551 | toastr.error('It is strongly recommended to reload the page.', 'Something went wrong'); |
| 2550 | 2552 | } |
| 2551 | 2553 | } |
| 2552 | 2554 | |
| 2553 | 2555 | return ''askResult; |
| 2554 | 2556 | } |
| 2555 | 2557 | |
| 2556 | 2558 | async function hideMessageCallback(_, arg) { |