/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');
520520let dialogueResolve = null;
521521let dialogueCloseStop = false;
522522export let chat_metadata = {};
523+/** @type {StreamingProcessor} */
523524export let streamingProcessor = null;
524525let crop_data = undefined;
525526let is_delete_mode = false;
@@ -837,6 +838,7 @@ export let main_api;// = "kobold";
837838//novel settings
838839export let novelai_settings;
839840export let novelai_setting_names;
841+/** @type {AbortController} */
840842let abortController;
841843
842844//css
@@ -4381,6 +4383,25 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
43814383}
43824384
43834385/**
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+/**
43844405 * Injects extension prompts into chat messages.
43854406 * @param {object[]} messages Array of chat messages
43864407 * @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 () {
1034210363 });
1034310364
1034410365 $(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);
1035410367 });
1035510368
1035610369 $(document).on('click', '#form_sheld .stscript_continue', function () {
@@ -10839,3 +10852,4 @@ jQuery(async function () {
1083910852
1084010853 initCustomSelectedSamplers();
1084110854});
10855+
public/scripts/slash-commands.js+19 -0
@@ -33,6 +33,7 @@ import {
3333 setCharacterName,
3434 setExtensionPrompt,
3535 setUserName,
36+ stopGeneration,
3637 substituteParams,
3738 system_avatar,
3839 system_message_types,
@@ -899,6 +900,24 @@ export function initDefaultSlashCommands() {
899900 helpString: 'Adds a swipe to the last chat message.',
900901 }));
901902 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({
902921 name: 'abort',
903922 callback: abortCallback,
904923 namedArgumentList: [