diff --git a/public/scripts/extensions.js b/public/scripts/extensions.js index 6d546dbe2..4101f5c02 100644 --- a/public/scripts/extensions.js +++ b/public/scripts/extensions.js @@ -62,6 +62,7 @@ const extension_settings = { tts: {}, sd: { prompts: {}, + character_prompts: {}, }, chromadb: {}, translate: {}, diff --git a/public/scripts/extensions/stable-diffusion/index.js b/public/scripts/extensions/stable-diffusion/index.js index 60add3181..238f6f793 100644 --- a/public/scripts/extensions/stable-diffusion/index.js +++ b/public/scripts/extensions/stable-diffusion/index.js @@ -9,10 +9,12 @@ import { event_types, eventSource, appendImageToMessage, - generateQuietPrompt + generateQuietPrompt, + this_chid, } from "../../../script.js"; import { getApiUrl, getContext, extension_settings, doExtrasFetch, modules } from "../../extensions.js"; -import { stringFormat, initScrollHeight, resetScrollHeight, timestampToMoment } from "../../utils.js"; +import { selected_group } from "../../group-chats.js"; +import { stringFormat, initScrollHeight, resetScrollHeight, timestampToMoment, getCharaFilename } from "../../utils.js"; export { MODULE_NAME }; // Wraps a string into monospace font-face span @@ -157,6 +159,10 @@ async function loadSettings() { extension_settings.sd.prompts = promptTemplates; } + if (extension_settings.sd.character_prompts === undefined) { + extension_settings.sd.character_prompts = {}; + } + $('#sd_scale').val(extension_settings.sd.scale).trigger('input'); $('#sd_steps').val(extension_settings.sd.steps).trigger('input'); $('#sd_prompt_prefix').val(extension_settings.sd.prompt_prefix).trigger('input'); @@ -183,7 +189,7 @@ function addPromptTemplates() { .text(modeLabels[name]) .attr('for', `sd_prompt_${name}`); const textarea = $('') - .addClass('textarea_compact') + .addClass('textarea_compact text_pole') .attr('id', `sd_prompt_${name}`) .attr('rows', 6) .val(prompt).on('input', () => { @@ -221,6 +227,52 @@ async function refinePrompt(prompt) { return prompt; } +function onChatChanged() { + if (this_chid === undefined || selected_group) { + $('#sd_character_prompt_block').hide(); + return; + } + + $('#sd_character_prompt_block').show(); + const key = getCharaFilename(this_chid); + $('#sd_character_prompt').val(key ? (extension_settings.sd.character_prompts[key] || '') : ''); +} + +function onCharacterPromptInput() { + const key = getCharaFilename(this_chid); + extension_settings.sd.character_prompts[key] = $('#sd_character_prompt').val(); + saveSettingsDebounced(); +} + +function getCharacterPrefix() { + if (selected_group) { + return ''; + } + + const key = getCharaFilename(this_chid); + + if (key) { + return extension_settings.sd.character_prompts[key] || ''; + } + + return ''; +} + +function combinePrefixes(str1, str2) { + if (!str2) { + return str1; + } + + // Remove leading/trailing white spaces and commas from the strings + str1 = str1.trim().replace(/^,|,$/g, ''); + str2 = str2.trim().replace(/^,|,$/g, ''); + + // Combine the strings with a comma between them + var result = `${str1}, ${str2},`; + + return result; +} + function onRefineModeInput() { extension_settings.sd.refine_mode = !!$('#sd_refine_mode').prop('checked'); saveSettingsDebounced(); @@ -573,7 +625,7 @@ async function generateExtrasImage(prompt, callback) { scale: extension_settings.sd.scale, width: extension_settings.sd.width, height: extension_settings.sd.height, - prompt_prefix: extension_settings.sd.prompt_prefix, + prompt_prefix: combinePrefixes(extension_settings.sd.prompt_prefix, getCharacterPrefix()), negative_prompt: extension_settings.sd.negative_prompt, restore_faces: !!extension_settings.sd.restore_faces, enable_hr: !!extension_settings.sd.enable_hr, @@ -601,7 +653,7 @@ async function generateHordeImage(prompt, callback) { scale: extension_settings.sd.scale, width: extension_settings.sd.width, height: extension_settings.sd.height, - prompt_prefix: extension_settings.sd.prompt_prefix, + prompt_prefix: combinePrefixes(extension_settings.sd.prompt_prefix, getCharacterPrefix()), negative_prompt: extension_settings.sd.negative_prompt, model: extension_settings.sd.model, nsfw: extension_settings.sd.horde_nsfw, @@ -814,60 +866,66 @@ jQuery(async () => {