/stop slash command to stop generation

7de43d3a9cdc2e27826577a2c373363c8fad8625

Wolfsblvt <wolfsblvt@gmail.com>

2 files changed, +42 -9Ignore whitespace
public/script.js+23 -9
@@ -520,6 +520,7 @@ const chatElement = $('#chat');
520let dialogueResolve = null;520let dialogueResolve = null;
521let dialogueCloseStop = false;521let dialogueCloseStop = false;
522export let chat_metadata = {};522export let chat_metadata = {};
523/** @type {StreamingProcessor} */
523export let streamingProcessor = null;524export let streamingProcessor = null;
524let crop_data = undefined;525let crop_data = undefined;
525let is_delete_mode = false;526let is_delete_mode = false;
@@ -837,6 +838,7 @@ export let main_api;// = "kobold";
837//novel settings838//novel settings
838export let novelai_settings;839export let novelai_settings;
839export let novelai_setting_names;840export let novelai_setting_names;
841/** @type {AbortController} */
840let abortController;842let abortController;
841843
842//css844//css
@@ -4381,6 +4383,25 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
4381}4383}
43824384
4383/**4385/**
4386 * Stops the generation and any streaming if it is currently running.
4387 */
4388export 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 messages4406 * @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 });
1034310364
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 });
1035510368
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 () {
1083910852
10840 initCustomSelectedSamplers();10853 initCustomSelectedSamplers();
10841});10854});
10855
public/scripts/slash-commands.js+19 -0
@@ -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: [