Fix multigen with group chats

This commit is contained in:
SillyLossy
2023-05-14 16:36:07 +03:00
parent 07e7028269
commit bd6255c758
2 changed files with 67 additions and 36 deletions

View File

@ -1579,6 +1579,9 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
message_already_generated = `${magName}: `;
}
// To trim after multigen ended
const magFirst = message_already_generated;
const interruptedByCommand = processCommands($("#send_textarea").val(), type);
if (interruptedByCommand) {
@ -1605,6 +1608,14 @@ 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) {
generateGroupWrapper(false, type, { resolve, reject, quiet_prompt, force_chid });
return;
@ -2279,23 +2290,6 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
hideSwipeButtons();
let getMessage = await streamingProcessor.generate();
// Cohee: Basically a dead-end code... (disabled by isStreamingEnabled)
// I wasn't able to get multigen working with real streaming
// consistently without screwing the interim prompting
if (isMultigenEnabled()) {
tokens_already_generated += this_amount_gen;
message_already_generated += getMessage;
promptBias = '';
if (!streamingProcessor.isStopped && shouldContinueMultigen(getMessage, isImpersonate)) {
streamingProcessor.isFinished = false;
runGenerate(getMessage);
console.log('returning to make generate again');
return;
}
getMessage = message_already_generated;
}
if (streamingProcessor && !streamingProcessor.isStopped && streamingProcessor.isFinished) {
streamingProcessor.onFinishStreaming(streamingProcessor.messageId, getMessage);
streamingProcessor = null;
@ -2343,7 +2337,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
return;
}
getMessage = message_already_generated;
getMessage = message_already_generated.substring(magFirst.length);
}
//Formating
@ -2402,13 +2396,14 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
showSwipeButtons();
setGenerationProgress(0);
$('.mes_buttons:last').show();
if (type !== 'quiet') {
resolve();
}
};
function onError(jqXHR, exception) {
if (type == 'quiet') {
reject(exception);
}
$("#send_textarea").removeAttr('disabled');
is_send_press = false;
activateSendButtons();
@ -2740,6 +2735,10 @@ function saveReply(type, getMessage, this_mes_is_name, title) {
const item = chat[chat.length - 1];
if (item['swipe_id'] !== undefined) {
item['swipes'][item['swipes'].length - 1] = item['mes'];
} else {
item['swipe_id'] = 0;
item['swipes'] = [];
item['swipes'][0] = chat[chat.length - 1]['mes'];
}
return { type, getMessage };

View File

@ -46,6 +46,7 @@ import {
menu_type,
select_selected_character,
cancelTtsPlay,
isMultigenEnabled,
} from "../script.js";
import { appendTagToList, createTagMapFromList, getTagsList, applyTagsOnCharacterSelect } from './tags.js';
@ -426,7 +427,7 @@ async function generateGroupWrapper(by_auto_mode, type = null, params = {}) {
let lastMessageText = lastMessage.mes;
let activationText = "";
let isUserInput = false;
let isQuietGenDone = false;
let isGenerationDone = false;
if (userInput && userInput.length && !by_auto_mode) {
isUserInput = true;
@ -438,6 +439,23 @@ async function generateGroupWrapper(by_auto_mode, type = null, params = {}) {
}
}
const resolveOriginal = params.resolve;
const rejectOriginal = params.reject;
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);
let activatedMembers = [];
@ -450,16 +468,6 @@ async function generateGroupWrapper(by_auto_mode, type = null, params = {}) {
activatedMembers = activateListOrder(group.members.slice(0, 1));
}
const resolveOriginal = params.resolve;
const rejectOriginal = params.reject;
params.resolve = function () {
isQuietGenDone = true;
resolveOriginal.apply(this, arguments);
};
params.reject = function () {
isQuietGenDone = true;
rejectOriginal.apply(this, arguments);
}
}
else if (type === "swipe") {
activatedMembers = activateSwipe(group.members);
@ -482,13 +490,14 @@ async function generateGroupWrapper(by_auto_mode, type = null, params = {}) {
// now the real generation begins: cycle through every character
for (const chId of activatedMembers) {
isGenerationDone = false;
const generateType = type == "swipe" || type == "impersonate" || type == "quiet" ? type : "group_chat";
setCharacterId(chId);
setCharacterName(characters[chId].name)
await Generate(generateType, { automatic_trigger: by_auto_mode, ...(params || {}) });
if (type !== "swipe" && type !== "impersonate") {
if (type !== "swipe" && type !== "impersonate" && !isMultigenEnabled()) {
// update indicator and scroll down
typingIndicator
.find(".typing_indicator_name")
@ -499,9 +508,10 @@ async function generateGroupWrapper(by_auto_mode, type = null, params = {}) {
});
}
// TODO: This is awful. Refactor this
while (true) {
// if not swipe - check if message generated already
if (type !== "swipe" && chat.length == messagesBefore) {
if (type !== "swipe" && !isMultigenEnabled() && chat.length == messagesBefore) {
await delay(100);
}
// if swipe - see if message changed
@ -514,6 +524,13 @@ async function generateGroupWrapper(by_auto_mode, type = null, params = {}) {
break;
}
}
else if (isMultigenEnabled()) {
if (isGenerationDone) {
break;
} else {
await delay(100);
}
}
else {
if (lastMessageText === chat[chat.length - 1].mes) {
await delay(100);
@ -532,6 +549,13 @@ async function generateGroupWrapper(by_auto_mode, type = null, params = {}) {
break;
}
}
else if (isMultigenEnabled()) {
if (isGenerationDone) {
break;
} else {
await delay(100);
}
}
else {
if (!$("#send_textarea").val() || $("#send_textarea").val() == userInput) {
await delay(100);
@ -542,7 +566,15 @@ async function generateGroupWrapper(by_auto_mode, type = null, params = {}) {
}
}
else if (type === 'quiet') {
if (isQuietGenDone) {
if (isGenerationDone) {
break;
} else {
await delay(100);
}
}
else if (isMultigenEnabled()) {
if (isGenerationDone) {
messagesBefore++;
break;
} else {
await delay(100);