Merge pull request #2465 from SillyTavern/stop-gen-slash-command

/stop slash command to stop generation
This commit is contained in:
Cohee 2024-07-03 20:37:25 +03:00 committed by GitHub
commit 7820ec66b9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 48 additions and 11 deletions

View File

@ -227,7 +227,7 @@ import { appendFileContent, hasPendingFileAttachment, populateFileAttachment, de
import { initPresetManager } from './scripts/preset-manager.js'; import { initPresetManager } from './scripts/preset-manager.js';
import { MacrosParser, evaluateMacros } from './scripts/macros.js'; import { MacrosParser, evaluateMacros } from './scripts/macros.js';
import { currentUser, setUserControls } from './scripts/user.js'; import { currentUser, setUserControls } from './scripts/user.js';
import { POPUP_TYPE, Popup, callGenericPopup, fixToastrForDialogs } from './scripts/popup.js'; import { POPUP_RESULT, POPUP_TYPE, Popup, callGenericPopup, fixToastrForDialogs } from './scripts/popup.js';
import { renderTemplate, renderTemplateAsync } from './scripts/templates.js'; import { renderTemplate, renderTemplateAsync } from './scripts/templates.js';
import { ScraperManager } from './scripts/scrapers.js'; import { ScraperManager } from './scripts/scrapers.js';
import { SlashCommandParser } from './scripts/slash-commands/SlashCommandParser.js'; import { SlashCommandParser } from './scripts/slash-commands/SlashCommandParser.js';
@ -520,6 +520,7 @@ const chatElement = $('#chat');
let dialogueResolve = null; let dialogueResolve = null;
let dialogueCloseStop = false; let dialogueCloseStop = false;
export let chat_metadata = {}; export let chat_metadata = {};
/** @type {StreamingProcessor} */
export let streamingProcessor = null; export let streamingProcessor = null;
let crop_data = undefined; let crop_data = undefined;
let is_delete_mode = false; let is_delete_mode = false;
@ -837,6 +838,7 @@ export let main_api;// = "kobold";
//novel settings //novel settings
export let novelai_settings; export let novelai_settings;
export let novelai_setting_names; export let novelai_setting_names;
/** @type {AbortController} */
let abortController; let abortController;
//css //css
@ -4380,6 +4382,25 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
} }
} }
/**
* Stops the generation and any streaming if it is currently running.
*/
export function stopGeneration() {
let stopped = false;
if (streamingProcessor) {
streamingProcessor.onStopStreaming();
streamingProcessor = null;
stopped = true;
}
if (abortController) {
abortController.abort('Clicked stop button');
hideStopButton();
stopped = true;
}
eventSource.emit(event_types.GENERATION_STOPPED);
return stopped;
}
/** /**
* Injects extension prompts into chat messages. * Injects extension prompts into chat messages.
* @param {object[]} messages Array of chat messages * @param {object[]} messages Array of chat messages
@ -7163,7 +7184,8 @@ function onScenarioOverrideRemoveClick() {
* @param {string} inputValue - Value to set the input to. * @param {string} inputValue - Value to set the input to.
* @param {PopupOptions} options - Options for the popup. * @param {PopupOptions} options - Options for the popup.
* @typedef {{okButton?: string, rows?: number, wide?: boolean, wider?: boolean, large?: boolean, allowHorizontalScrolling?: boolean, allowVerticalScrolling?: boolean, cropAspect?: number }} PopupOptions - Options for the popup. * @typedef {{okButton?: string, rows?: number, wide?: boolean, wider?: boolean, large?: boolean, allowHorizontalScrolling?: boolean, allowVerticalScrolling?: boolean, cropAspect?: number }} PopupOptions - Options for the popup.
* @returns * @returns {Promise<any>} A promise that resolves when the popup is closed.
* @deprecated Use `callGenericPopup` instead.
*/ */
export function callPopup(text, type, inputValue = '', { okButton, rows, wide, wider, large, allowHorizontalScrolling, allowVerticalScrolling, cropAspect } = {}) { export function callPopup(text, type, inputValue = '', { okButton, rows, wide, wider, large, allowHorizontalScrolling, allowVerticalScrolling, cropAspect } = {}) {
function getOkButtonText() { function getOkButtonText() {
@ -7794,6 +7816,7 @@ window['SillyTavern'].getContext = function () {
eventTypes: event_types, eventTypes: event_types,
addOneMessage: addOneMessage, addOneMessage: addOneMessage,
generate: Generate, generate: Generate,
stopGeneration: stopGeneration,
getTokenCount: getTokenCount, getTokenCount: getTokenCount,
extensionPrompts: extension_prompts, extensionPrompts: extension_prompts,
setExtensionPrompt: setExtensionPrompt, setExtensionPrompt: setExtensionPrompt,
@ -7849,6 +7872,8 @@ window['SillyTavern'].getContext = function () {
* @deprecated Legacy snake-case naming, compatibility with old extensions * @deprecated Legacy snake-case naming, compatibility with old extensions
*/ */
event_types: event_types, event_types: event_types,
POPUP_TYPE: POPUP_TYPE,
POPUP_RESULT: POPUP_RESULT,
}; };
}; };
@ -10342,15 +10367,7 @@ jQuery(async function () {
}); });
$(document).on('click', '.mes_stop', function () { $(document).on('click', '.mes_stop', function () {
if (streamingProcessor) { stopGeneration();
streamingProcessor.onStopStreaming();
streamingProcessor = null;
}
if (abortController) {
abortController.abort('Clicked stop button');
hideStopButton();
}
eventSource.emit(event_types.GENERATION_STOPPED);
}); });
$(document).on('click', '#form_sheld .stscript_continue', function () { $(document).on('click', '#form_sheld .stscript_continue', function () {
@ -10839,3 +10856,4 @@ jQuery(async function () {
initCustomSelectedSamplers(); initCustomSelectedSamplers();
}); });

View File

@ -33,6 +33,7 @@ import {
setCharacterName, setCharacterName,
setExtensionPrompt, setExtensionPrompt,
setUserName, setUserName,
stopGeneration,
substituteParams, substituteParams,
system_avatar, system_avatar,
system_message_types, system_message_types,
@ -898,6 +899,24 @@ export function initDefaultSlashCommands() {
], ],
helpString: 'Adds a swipe to the last chat message.', helpString: 'Adds a swipe to the last chat message.',
})); }));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'stop',
callback: () => {
const stopped = stopGeneration();
return String(stopped);
},
returns: 'true/false, whether the generation was running and got stopped',
helpString: `
<div>
Stops the generation and any streaming if it is currently running.
</div>
<div>
Note: This command cannot be executed from the chat input, as sending any message or script from there is blocked during generation.
But it can be executed via automations or QR scripts/buttons.
</div>
`,
aliases: ['generate-stop'],
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'abort', name: 'abort',
callback: abortCallback, callback: abortCallback,