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:
valadaptive 2023-12-07 17:32:07 -05:00
parent f5d2e50f5e
commit 03884b29ad
2 changed files with 44 additions and 111 deletions

View File

@ -2916,6 +2916,14 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
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.
const isInstruct = power_user.instruct.enabled && main_api !== 'openai';
const isImpersonate = type == 'impersonate';
@ -2927,12 +2935,14 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
if (interruptedByCommand) {
//$("#send_textarea").val('').trigger('input');
unblockGeneration();
resolve();
return;
}
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 });
unblockGeneration();
resolve();
return;
}
@ -2942,11 +2952,13 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
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 });
unblockGeneration();
resolve();
return;
}
if (isHordeGenerationNotAllowed()) {
unblockGeneration();
resolve();
return;
}
@ -2955,14 +2967,6 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
hideSwipeButtons();
}
// Set empty promise resolution functions
if (typeof resolve !== 'function') {
resolve = () => { };
}
if (typeof reject !== 'function') {
reject = () => { };
}
if (selected_group && !is_group_generating && !dryRun) {
generateGroupWrapper(false, type, { resolve, reject, quiet_prompt, force_chid, signal: abortController.signal, quietImage });
return;
@ -2987,6 +2991,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
} else {
console.log('No enabled members found');
unblockGeneration();
resolve();
return;
}
}
@ -3151,6 +3156,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
if (aborted) {
console.debug('Generation aborted by extension interceptors');
unblockGeneration();
resolve();
return;
}
} else {
@ -3206,6 +3212,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
}
catch {
unblockGeneration();
resolve();
return;
}
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');
is_send_press = false;
swipe_right();
// TODO: do we want to resolve after an auto-swipe?
resolve();
return;
}
}
@ -3950,8 +3959,8 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
if (type !== 'quiet') {
triggerAutoContinue(messageChunk, isImpersonate);
resolve();
}
resolve();
}
function onError(exception) {

View File

@ -8,7 +8,6 @@ import {
extractAllWords,
saveBase64AsFile,
PAGINATION_TEMPLATE,
waitUntilCondition,
getBase64Async,
} from './utils.js';
import { RA_CountCharTokens, humanizedDateTime, dragElement, favsToHotswap, getMessageTimeStamp } from './RossAscends-mods.js';
@ -46,7 +45,6 @@ import {
updateChatMetadata,
isStreamingEnabled,
getThumbnailUrl,
streamingProcessor,
getRequestHeaders,
setMenuType,
menu_type,
@ -653,41 +651,20 @@ async function generateGroupWrapper(by_auto_mode, type = null, params = {}) {
// id of this specific batch for regeneration purposes
group_generation_id = Date.now();
const lastMessage = chat[chat.length - 1];
let messagesBefore = chat.length;
let lastMessageText = lastMessage?.mes || '';
let activationText = '';
let isUserInput = false;
let isGenerationDone = false;
if (userInput?.length && !by_auto_mode) {
isUserInput = true;
activationText = userInput;
messagesBefore++;
} else {
if (lastMessage && !lastMessage.is_system) {
activationText = lastMessage.mes;
}
}
const resolveOriginal = params.resolve;
const rejectOriginal = params.reject;
if (params.signal instanceof AbortSignal && params.signal.aborted) {
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);
};
throw new Error('Already aborted signal passed. Group generation stopped');
}
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
for (const chId of activatedMembers) {
deactivateSendButtons();
isGenerationDone = false;
const generateType = type == 'swipe' || type == 'impersonate' || type == 'quiet' || type == 'continue' ? type : 'group_chat';
setCharacterId(chId);
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()) {
// update indicator and scroll down
typingIndicator
.find('.typing_indicator_name')
.text(characters[chId].name);
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 (type !== 'swipe' && type !== 'impersonate' && !isStreamingEnabled()) {
// update indicator and scroll down
typingIndicator
.find('.typing_indicator_name')
.text(characters[chId].name);
typingIndicator.show();
}
// 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 {
typingIndicator.hide();