Image Generation: Add swipes for generated images

Supersedes #2648
This commit is contained in:
Cohee
2024-08-13 23:21:00 +03:00
parent 78bee4631d
commit e1a29b36f5
4 changed files with 146 additions and 1 deletions

View File

@ -59,6 +59,7 @@ const initiators = {
action: 'action',
interactive: 'interactive',
wand: 'wand',
swipe: 'swipe',
};
const generationMode = {
@ -3429,6 +3430,7 @@ async function sendMessage(prompt, image, generationType, additionalNegativePref
generationType: generationType,
negative: additionalNegativePrefix,
inline_image: false,
image_swipes: [image],
},
};
context.chat.push(message);
@ -3657,6 +3659,99 @@ async function writePromptFields(characterId) {
await writeExtensionField(characterId, 'sd_character_prompt', promptObject);
}
/**
* Switches an image to the next or previous one in the swipe list.
* @param {object} args Event arguments
* @param {any} args.message Message object
* @param {JQuery<HTMLElement>} args.element Message element
* @param {string} args.direction Swipe direction
* @returns {Promise<void>}
*/
async function onImageSwiped({ message, element, direction }) {
const context = getContext();
const animationClass = 'fa-fade';
const messageImg = element.find('.mes_img');
// Current image is already animating
if (messageImg.hasClass(animationClass)) {
return;
}
const swipes = message?.extra?.image_swipes;
if (!Array.isArray(swipes)) {
console.warn('No image swipes found in the message');
return;
}
const currentIndex = swipes.indexOf(message.extra.image);
if (currentIndex === -1) {
console.warn('Current image not found in the swipes');
return;
}
// Switch to previous image or wrap around if at the beginning
if (direction === 'left') {
const newIndex = currentIndex === 0 ? swipes.length - 1 : currentIndex - 1;
message.extra.image = swipes[newIndex];
// Update the image in the message
appendMediaToMessage(message, element, false);
}
// Switch to next image or generate a new one if at the end
if (direction === 'right') {
const newIndex = currentIndex === swipes.length - 1 ? swipes.length : currentIndex + 1;
if (newIndex === swipes.length) {
const abortController = new AbortController();
const swipeControls = element.find('.mes_img_swipes');
const stopButton = document.getElementById('sd_stop_gen');
const stopListener = () => abortController.abort('Aborted by user');
const generationType = message?.extra?.generationType ?? generationMode.FREE;
const dimensions = setTypeSpecificDimensions(generationType);
const originalSeed = extension_settings.sd.seed;
extension_settings.sd.seed = Math.round(Math.random() * Number.MAX_SAFE_INTEGER);
let imagePath = '';
try {
$(stopButton).show();
eventSource.once(CUSTOM_STOP_EVENT, stopListener);
const callback = () => { };
const hasNegative = message.extra.negative;
const prompt = await refinePrompt(message.extra.title, false, false);
const negativePromptPrefix = hasNegative ? await refinePrompt(message.extra.negative, false, true) : '';
const characterName = context.characterId
? context.characters[context.characterId].name
: context.groups[Object.keys(context.groups).filter(x => context.groups[x].id === context.groupId)[0]]?.id?.toString();
messageImg.addClass(animationClass);
swipeControls.hide();
imagePath = await sendGenerationRequest(generationType, prompt, negativePromptPrefix, characterName, callback, initiators.swipe, abortController.signal);
} finally {
$(stopButton).hide();
messageImg.removeClass(animationClass);
swipeControls.show();
eventSource.removeListener(CUSTOM_STOP_EVENT, stopListener);
restoreOriginalDimensions(dimensions);
extension_settings.sd.seed = originalSeed;
}
if (!imagePath) {
return;
}
swipes.push(imagePath);
}
message.extra.image = swipes[newIndex];
appendMediaToMessage(message, element, false);
}
await context.saveChat();
}
jQuery(async () => {
await addSDGenButtons();
@ -3795,6 +3890,8 @@ jQuery(async () => {
}
});
eventSource.on(event_types.IMAGE_SWIPED, onImageSwiped);
eventSource.on(event_types.CHAT_CHANGED, onChatChanged);
await loadSettings();