mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Always call resolve in Generate()
This lets us get rid of the janky hack in group-chats to tell when a message is done generating.
This commit is contained in:
@@ -2916,6 +2916,14 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
|
|||||||
abortController = new AbortController();
|
abortController = new AbortController();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set empty promise resolution functions
|
||||||
|
if (typeof resolve !== 'function') {
|
||||||
|
resolve = () => { };
|
||||||
|
}
|
||||||
|
if (typeof reject !== 'function') {
|
||||||
|
reject = () => { };
|
||||||
|
}
|
||||||
|
|
||||||
// OpenAI doesn't need instruct mode. Use OAI main prompt instead.
|
// OpenAI doesn't need instruct mode. Use OAI main prompt instead.
|
||||||
const isInstruct = power_user.instruct.enabled && main_api !== 'openai';
|
const isInstruct = power_user.instruct.enabled && main_api !== 'openai';
|
||||||
const isImpersonate = type == 'impersonate';
|
const isImpersonate = type == 'impersonate';
|
||||||
@@ -2927,12 +2935,14 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
|
|||||||
if (interruptedByCommand) {
|
if (interruptedByCommand) {
|
||||||
//$("#send_textarea").val('').trigger('input');
|
//$("#send_textarea").val('').trigger('input');
|
||||||
unblockGeneration();
|
unblockGeneration();
|
||||||
|
resolve();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (main_api == 'kobold' && kai_settings.streaming_kobold && !kai_flags.can_use_streaming) {
|
if (main_api == 'kobold' && kai_settings.streaming_kobold && !kai_flags.can_use_streaming) {
|
||||||
toastr.error('Streaming is enabled, but the version of Kobold used does not support token streaming.', undefined, { timeOut: 10000, preventDuplicates: true });
|
toastr.error('Streaming is enabled, but the version of Kobold used does not support token streaming.', undefined, { timeOut: 10000, preventDuplicates: true });
|
||||||
unblockGeneration();
|
unblockGeneration();
|
||||||
|
resolve();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2942,11 +2952,13 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
|
|||||||
textgen_settings.type !== MANCER) {
|
textgen_settings.type !== MANCER) {
|
||||||
toastr.error('Streaming is not supported for the Legacy API. Update Ooba and use --extensions openai to enable streaming.', undefined, { timeOut: 10000, preventDuplicates: true });
|
toastr.error('Streaming is not supported for the Legacy API. Update Ooba and use --extensions openai to enable streaming.', undefined, { timeOut: 10000, preventDuplicates: true });
|
||||||
unblockGeneration();
|
unblockGeneration();
|
||||||
|
resolve();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isHordeGenerationNotAllowed()) {
|
if (isHordeGenerationNotAllowed()) {
|
||||||
unblockGeneration();
|
unblockGeneration();
|
||||||
|
resolve();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2955,14 +2967,6 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
|
|||||||
hideSwipeButtons();
|
hideSwipeButtons();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set empty promise resolution functions
|
|
||||||
if (typeof resolve !== 'function') {
|
|
||||||
resolve = () => { };
|
|
||||||
}
|
|
||||||
if (typeof reject !== 'function') {
|
|
||||||
reject = () => { };
|
|
||||||
}
|
|
||||||
|
|
||||||
if (selected_group && !is_group_generating && !dryRun) {
|
if (selected_group && !is_group_generating && !dryRun) {
|
||||||
generateGroupWrapper(false, type, { resolve, reject, quiet_prompt, force_chid, signal: abortController.signal, quietImage });
|
generateGroupWrapper(false, type, { resolve, reject, quiet_prompt, force_chid, signal: abortController.signal, quietImage });
|
||||||
return;
|
return;
|
||||||
@@ -2987,6 +2991,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
|
|||||||
} else {
|
} else {
|
||||||
console.log('No enabled members found');
|
console.log('No enabled members found');
|
||||||
unblockGeneration();
|
unblockGeneration();
|
||||||
|
resolve();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3151,6 +3156,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
|
|||||||
if (aborted) {
|
if (aborted) {
|
||||||
console.debug('Generation aborted by extension interceptors');
|
console.debug('Generation aborted by extension interceptors');
|
||||||
unblockGeneration();
|
unblockGeneration();
|
||||||
|
resolve();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -3206,6 +3212,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
|
|||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
unblockGeneration();
|
unblockGeneration();
|
||||||
|
resolve();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (horde_settings.auto_adjust_context_length) {
|
if (horde_settings.auto_adjust_context_length) {
|
||||||
@@ -3925,6 +3932,8 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
|
|||||||
console.debug('swiping right automatically');
|
console.debug('swiping right automatically');
|
||||||
is_send_press = false;
|
is_send_press = false;
|
||||||
swipe_right();
|
swipe_right();
|
||||||
|
// TODO: do we want to resolve after an auto-swipe?
|
||||||
|
resolve();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3950,8 +3959,8 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
|
|||||||
|
|
||||||
if (type !== 'quiet') {
|
if (type !== 'quiet') {
|
||||||
triggerAutoContinue(messageChunk, isImpersonate);
|
triggerAutoContinue(messageChunk, isImpersonate);
|
||||||
resolve();
|
|
||||||
}
|
}
|
||||||
|
resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
function onError(exception) {
|
function onError(exception) {
|
||||||
|
@@ -8,7 +8,6 @@ import {
|
|||||||
extractAllWords,
|
extractAllWords,
|
||||||
saveBase64AsFile,
|
saveBase64AsFile,
|
||||||
PAGINATION_TEMPLATE,
|
PAGINATION_TEMPLATE,
|
||||||
waitUntilCondition,
|
|
||||||
getBase64Async,
|
getBase64Async,
|
||||||
} from './utils.js';
|
} from './utils.js';
|
||||||
import { RA_CountCharTokens, humanizedDateTime, dragElement, favsToHotswap, getMessageTimeStamp } from './RossAscends-mods.js';
|
import { RA_CountCharTokens, humanizedDateTime, dragElement, favsToHotswap, getMessageTimeStamp } from './RossAscends-mods.js';
|
||||||
@@ -46,7 +45,6 @@ import {
|
|||||||
updateChatMetadata,
|
updateChatMetadata,
|
||||||
isStreamingEnabled,
|
isStreamingEnabled,
|
||||||
getThumbnailUrl,
|
getThumbnailUrl,
|
||||||
streamingProcessor,
|
|
||||||
getRequestHeaders,
|
getRequestHeaders,
|
||||||
setMenuType,
|
setMenuType,
|
||||||
menu_type,
|
menu_type,
|
||||||
@@ -653,41 +651,20 @@ async function generateGroupWrapper(by_auto_mode, type = null, params = {}) {
|
|||||||
// id of this specific batch for regeneration purposes
|
// id of this specific batch for regeneration purposes
|
||||||
group_generation_id = Date.now();
|
group_generation_id = Date.now();
|
||||||
const lastMessage = chat[chat.length - 1];
|
const lastMessage = chat[chat.length - 1];
|
||||||
let messagesBefore = chat.length;
|
|
||||||
let lastMessageText = lastMessage?.mes || '';
|
|
||||||
let activationText = '';
|
let activationText = '';
|
||||||
let isUserInput = false;
|
let isUserInput = false;
|
||||||
let isGenerationDone = false;
|
|
||||||
|
|
||||||
if (userInput?.length && !by_auto_mode) {
|
if (userInput?.length && !by_auto_mode) {
|
||||||
isUserInput = true;
|
isUserInput = true;
|
||||||
activationText = userInput;
|
activationText = userInput;
|
||||||
messagesBefore++;
|
|
||||||
} else {
|
} else {
|
||||||
if (lastMessage && !lastMessage.is_system) {
|
if (lastMessage && !lastMessage.is_system) {
|
||||||
activationText = lastMessage.mes;
|
activationText = lastMessage.mes;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const resolveOriginal = params.resolve;
|
|
||||||
const rejectOriginal = params.reject;
|
|
||||||
|
|
||||||
if (params.signal instanceof AbortSignal && params.signal.aborted) {
|
if (params.signal instanceof AbortSignal && params.signal.aborted) {
|
||||||
throw new Error('Already aborted signal passed. Group generation stopped');
|
throw new Error('Already aborted signal passed. Group generation stopped');
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof params.resolve === 'function') {
|
|
||||||
params.resolve = function () {
|
|
||||||
isGenerationDone = true;
|
|
||||||
resolveOriginal.apply(this, arguments);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof params.reject === 'function') {
|
|
||||||
params.reject = function () {
|
|
||||||
isGenerationDone = true;
|
|
||||||
rejectOriginal.apply(this, arguments);
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const activationStrategy = Number(group.activation_strategy ?? group_activation_strategy.NATURAL);
|
const activationStrategy = Number(group.activation_strategy ?? group_activation_strategy.NATURAL);
|
||||||
@@ -735,90 +712,37 @@ async function generateGroupWrapper(by_auto_mode, type = null, params = {}) {
|
|||||||
// now the real generation begins: cycle through every activated character
|
// now the real generation begins: cycle through every activated character
|
||||||
for (const chId of activatedMembers) {
|
for (const chId of activatedMembers) {
|
||||||
deactivateSendButtons();
|
deactivateSendButtons();
|
||||||
isGenerationDone = false;
|
|
||||||
const generateType = type == 'swipe' || type == 'impersonate' || type == 'quiet' || type == 'continue' ? type : 'group_chat';
|
const generateType = type == 'swipe' || type == 'impersonate' || type == 'quiet' || type == 'continue' ? type : 'group_chat';
|
||||||
setCharacterId(chId);
|
setCharacterId(chId);
|
||||||
setCharacterName(characters[chId].name);
|
setCharacterName(characters[chId].name);
|
||||||
|
|
||||||
await Generate(generateType, { automatic_trigger: by_auto_mode, ...(params || {}) });
|
// Wait for generation to finish
|
||||||
|
await new Promise(async (resolve, reject) => {
|
||||||
|
await Generate(generateType, {
|
||||||
|
automatic_trigger: by_auto_mode,
|
||||||
|
...(params || {}),
|
||||||
|
resolve: function(...args) {
|
||||||
|
if (typeof params.resolve === 'function') {
|
||||||
|
params.resolve(...args);
|
||||||
|
}
|
||||||
|
resolve();
|
||||||
|
},
|
||||||
|
reject: function(...args) {
|
||||||
|
if (typeof params.reject === 'function') {
|
||||||
|
params.reject(...args);
|
||||||
|
}
|
||||||
|
reject();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
if (type !== 'swipe' && type !== 'impersonate' && !isStreamingEnabled()) {
|
if (type !== 'swipe' && type !== 'impersonate' && !isStreamingEnabled()) {
|
||||||
// update indicator and scroll down
|
// update indicator and scroll down
|
||||||
typingIndicator
|
typingIndicator
|
||||||
.find('.typing_indicator_name')
|
.find('.typing_indicator_name')
|
||||||
.text(characters[chId].name);
|
.text(characters[chId].name);
|
||||||
typingIndicator.show();
|
typingIndicator.show();
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: This is awful. Refactor this
|
|
||||||
while (true) {
|
|
||||||
deactivateSendButtons();
|
|
||||||
if (params.signal instanceof AbortSignal && params.signal.aborted) {
|
|
||||||
throw new Error('Group generation aborted');
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
// if not swipe - check if message generated already
|
|
||||||
if (generateType === 'group_chat' && chat.length == messagesBefore) {
|
|
||||||
await delay(100);
|
|
||||||
}
|
|
||||||
// if swipe - see if message changed
|
|
||||||
else if (type === 'swipe') {
|
|
||||||
if (isStreamingEnabled()) {
|
|
||||||
if (streamingProcessor && !streamingProcessor.isFinished) {
|
|
||||||
await delay(100);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (lastMessageText === chat[chat.length - 1].mes) {
|
|
||||||
await delay(100);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (type === 'impersonate') {
|
|
||||||
if (isStreamingEnabled()) {
|
|
||||||
if (streamingProcessor && !streamingProcessor.isFinished) {
|
|
||||||
await delay(100);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (!$('#send_textarea').val() || $('#send_textarea').val() == userInput) {
|
|
||||||
await delay(100);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (type === 'quiet') {
|
|
||||||
if (isGenerationDone) {
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
await delay(100);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (isStreamingEnabled()) {
|
|
||||||
if (streamingProcessor && !streamingProcessor.isFinished) {
|
|
||||||
await delay(100);
|
|
||||||
} else {
|
|
||||||
await waitUntilCondition(() => streamingProcessor == null, 1000, 10);
|
|
||||||
messagesBefore++;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
messagesBefore++;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
typingIndicator.hide();
|
typingIndicator.hide();
|
||||||
|
Reference in New Issue
Block a user