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