/stop slash command to stop generation
| @@ -520,6 +520,7 @@ const chatElement = $('#chat'); | |||
| 520 | let dialogueResolve = null; | 520 | let dialogueResolve = null; |
| 521 | let dialogueCloseStop = false; | 521 | let dialogueCloseStop = false; |
| 522 | export let chat_metadata = {}; | 522 | export let chat_metadata = {}; |
| 523 | /** @type {StreamingProcessor} */ | ||
| 523 | export let streamingProcessor = null; | 524 | export let streamingProcessor = null; |
| 524 | let crop_data = undefined; | 525 | let crop_data = undefined; |
| 525 | let is_delete_mode = false; | 526 | let is_delete_mode = false; |
| @@ -837,6 +838,7 @@ export let main_api;// = "kobold"; | |||
| 837 | //novel settings | 838 | //novel settings |
| 838 | export let novelai_settings; | 839 | export let novelai_settings; |
| 839 | export let novelai_setting_names; | 840 | export let novelai_setting_names; |
| 841 | /** @type {AbortController} */ | ||
| 840 | let abortController; | 842 | let abortController; |
| 841 | 843 | ||
| 842 | //css | 844 | //css |
| @@ -4381,6 +4383,25 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro | |||
| 4381 | } | 4383 | } |
| 4382 | 4384 | ||
| 4383 | /** | 4385 | /** |
| 4386 | * Stops the generation and any streaming if it is currently running. | ||
| 4387 | */ | ||
| 4388 | export function stopGeneration() { | ||
| 4389 | let stopped = false; | ||
| 4390 | if (streamingProcessor) { | ||
| 4391 | streamingProcessor.onStopStreaming(); | ||
| 4392 | streamingProcessor = null; | ||
| 4393 | stopped = true; | ||
| 4394 | } | ||
| 4395 | if (abortController) { | ||
| 4396 | abortController.abort('Clicked stop button'); | ||
| 4397 | hideStopButton(); | ||
| 4398 | stopped = true; | ||
| 4399 | } | ||
| 4400 | eventSource.emit(event_types.GENERATION_STOPPED); | ||
| 4401 | return stopped; | ||
| 4402 | } | ||
| 4403 | |||
| 4404 | /** | ||
| 4384 | * Injects extension prompts into chat messages. | 4405 | * Injects extension prompts into chat messages. |
| 4385 | * @param {object[]} messages Array of chat messages | 4406 | * @param {object[]} messages Array of chat messages |
| 4386 | * @param {boolean} isContinue Whether the generation is a continuation. If true, the extension prompts of depth 0 are injected at position 1. | 4407 | * @param {boolean} isContinue Whether the generation is a continuation. If true, the extension prompts of depth 0 are injected at position 1. |
| @@ -10342,15 +10363,7 @@ jQuery(async function () { | |||
| 10342 | }); | 10363 | }); |
| 10343 | 10364 | ||
| 10344 | $(document).on('click', '.mes_stop', function () { | 10365 | $(document).on('click', '.mes_stop', function () { |
| 10345 | if (streamingProcessor) { | 10366 | stopGeneration(); |
| 10346 | streamingProcessor.onStopStreaming(); | ||
| 10347 | streamingProcessor = null; | ||
| 10348 | } | ||
| 10349 | if (abortController) { | ||
| 10350 | abortController.abort('Clicked stop button'); | ||
| 10351 | hideStopButton(); | ||
| 10352 | } | ||
| 10353 | eventSource.emit(event_types.GENERATION_STOPPED); | ||
| 10354 | }); | 10367 | }); |
| 10355 | 10368 | ||
| 10356 | $(document).on('click', '#form_sheld .stscript_continue', function () { | 10369 | $(document).on('click', '#form_sheld .stscript_continue', function () { |
| @@ -10839,3 +10852,4 @@ jQuery(async function () { | |||
| 10839 | 10852 | ||
| 10840 | initCustomSelectedSamplers(); | 10853 | initCustomSelectedSamplers(); |
| 10841 | }); | 10854 | }); |
| 10855 | |||
| @@ -33,6 +33,7 @@ import { | |||
| 33 | setCharacterName, | 33 | setCharacterName, |
| 34 | setExtensionPrompt, | 34 | setExtensionPrompt, |
| 35 | setUserName, | 35 | setUserName, |
| 36 | stopGeneration, | ||
| 36 | substituteParams, | 37 | substituteParams, |
| 37 | system_avatar, | 38 | system_avatar, |
| 38 | system_message_types, | 39 | system_message_types, |
| @@ -899,6 +900,24 @@ export function initDefaultSlashCommands() { | |||
| 899 | helpString: 'Adds a swipe to the last chat message.', | 900 | helpString: 'Adds a swipe to the last chat message.', |
| 900 | })); | 901 | })); |
| 901 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ | 902 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ |
| 903 | name: 'stop', | ||
| 904 | callback: () => { | ||
| 905 | const stopped = stopGeneration(); | ||
| 906 | return String(stopped); | ||
| 907 | }, | ||
| 908 | returns: 'true/false, whether the generation was running and got stopped', | ||
| 909 | helpString: ` | ||
| 910 | <div> | ||
| 911 | Stops the generation and any streaming if it is currently running. | ||
| 912 | </div> | ||
| 913 | <div> | ||
| 914 | Note: This command cannot be executed from the chat input, as sending any message or script from there is blocked during generation. | ||
| 915 | But it can be executed via automations or QR scripts/buttons. | ||
| 916 | </div> | ||
| 917 | `, | ||
| 918 | aliases: ['generate-stop'], | ||
| 919 | })); | ||
| 920 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ | ||
| 902 | name: 'abort', | 921 | name: 'abort', |
| 903 | callback: abortCallback, | 922 | callback: abortCallback, |
| 904 | namedArgumentList: [ | 923 | namedArgumentList: [ |