Have Generate() return a promise

Generate(), being async, now returns a promise-within-a-promise.
If called with `let p = await Generate(...)`, it'll wait for generation
to *start*. If you then `await p`, you'll wait for generation to
*finish*. This makes it much easier to tell exactly when generation's
done. generateGroupWrapper has been similarly modified.
This commit is contained in:
valadaptive 2023-12-07 17:46:15 -05:00
parent 03884b29ad
commit 33f969f097
2 changed files with 256 additions and 300 deletions

View File

@ -2307,26 +2307,8 @@ function getStoppingStrings(isImpersonate, isContinue) {
*/
export async function generateQuietPrompt(quiet_prompt, quietToLoud, skipWIAN, quietImage = null) {
console.log('got into genQuietPrompt');
return await new Promise(
async function promptPromise(resolve, reject) {
if (quietToLoud === true) {
try {
await Generate('quiet', { resolve, reject, quiet_prompt, quietToLoud: true, skipWIAN: skipWIAN, force_name2: true, quietImage: quietImage });
}
catch {
reject();
}
}
else {
try {
console.log('going to generate non-QuietToLoud');
await Generate('quiet', { resolve, reject, quiet_prompt, quietToLoud: false, skipWIAN: skipWIAN, force_name2: true, quietImage: quietImage });
}
catch {
reject();
}
}
});
const generateFinished = await Generate('quiet', { quiet_prompt, quietToLoud, skipWIAN: skipWIAN, force_name2: true, quietImage: quietImage });
await generateFinished;
}
async function processCommands(message, type, dryRun) {
@ -2906,7 +2888,8 @@ export async function generateRaw(prompt, api, instructOverride) {
return message;
}
async function Generate(type, { automatic_trigger, force_name2, resolve, reject, quiet_prompt, quietToLoud, skipWIAN, force_chid, signal, quietImage } = {}, dryRun = false) {
// Returns a promise that resolves when the text is done generating.
async function Generate(type, { automatic_trigger, force_name2, quiet_prompt, quietToLoud, skipWIAN, force_chid, signal, quietImage } = {}, dryRun = false) {
console.log('Generate entered');
setGenerationProgress(0);
generation_started = new Date();
@ -2916,14 +2899,6 @@ 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';
@ -2935,15 +2910,13 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
if (interruptedByCommand) {
//$("#send_textarea").val('').trigger('input');
unblockGeneration();
resolve();
return;
return Promise.resolve();
}
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;
return Promise.resolve();
}
if (main_api === 'textgenerationwebui' &&
@ -2952,14 +2925,12 @@ 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;
return Promise.resolve();
}
if (isHordeGenerationNotAllowed()) {
unblockGeneration();
resolve();
return;
return Promise.resolve();
}
// Hide swipes if not in a dry run.
@ -2968,8 +2939,8 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
}
if (selected_group && !is_group_generating && !dryRun) {
generateGroupWrapper(false, type, { resolve, reject, quiet_prompt, force_chid, signal: abortController.signal, quietImage });
return;
// TODO: await here!
return generateGroupWrapper(false, type, { quiet_prompt, force_chid, signal: abortController.signal, quietImage });
} else if (selected_group && !is_group_generating && dryRun) {
const characterIndexMap = new Map(characters.map((char, index) => [char.avatar, index]));
const group = groups.find((x) => x.id === selected_group);
@ -2991,8 +2962,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
} else {
console.log('No enabled members found');
unblockGeneration();
resolve();
return;
return Promise.resolve();
}
}
@ -3156,8 +3126,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
if (aborted) {
console.debug('Generation aborted by extension interceptors');
unblockGeneration();
resolve();
return;
return Promise.resolve();
}
} else {
console.debug('Skipping extension interceptors for dry run');
@ -3212,8 +3181,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
}
catch {
unblockGeneration();
resolve();
return;
return Promise.resolve();
}
if (horde_settings.auto_adjust_context_length) {
this_max_context = (adjustedParams.maxContextLength - adjustedParams.maxLength);
@ -3373,7 +3341,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
}
const originalType = type;
runGenerate(cyclePrompt);
return runGenerate(cyclePrompt);
async function runGenerate(cycleGenerationPrompt = '') {
if (!dryRun) {
@ -3721,6 +3689,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
}
}
return new Promise(async (resolve, reject) => {
if (true === dryRun) return onSuccess({ error: 'dryRun' });
if (power_user.console_log_prompts) {
@ -3973,6 +3942,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
console.log(exception);
streamingProcessor = null;
}
});
} //rungenerate ends
} else { //generate's primary loop ends, after this is error handling for no-connection or safety-id

View File

@ -612,11 +612,11 @@ async function generateGroupWrapper(by_auto_mode, type = null, params = {}) {
if (online_status === 'no_connection') {
is_group_generating = false;
setSendButtonState(false);
return;
return Promise.resolve();
}
if (is_group_generating) {
return false;
return Promise.resolve();
}
// Auto-navigate back to group menu
@ -630,7 +630,7 @@ async function generateGroupWrapper(by_auto_mode, type = null, params = {}) {
if (!group || !Array.isArray(group.members) || !group.members.length) {
sendSystemMessage(system_message_types.EMPTY, '', { isSmallSys: true });
return;
return Promise.resolve();
}
try {
@ -717,23 +717,8 @@ async function generateGroupWrapper(by_auto_mode, type = null, params = {}) {
setCharacterName(characters[chId].name);
// 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();
},
});
const generateFinished = await Generate(generateType, { automatic_trigger: by_auto_mode, ...(params || {}) });
await generateFinished;
if (type !== 'swipe' && type !== 'impersonate' && !isStreamingEnabled()) {
// update indicator and scroll down
@ -742,7 +727,6 @@ async function generateGroupWrapper(by_auto_mode, type = null, params = {}) {
.text(characters[chId].name);
typingIndicator.show();
}
});
}
} finally {
typingIndicator.hide();
@ -755,6 +739,8 @@ async function generateGroupWrapper(by_auto_mode, type = null, params = {}) {
activateSendButtons();
showSwipeButtons();
}
return Promise.resolve();
}
function getLastMessageGenerationId() {