Use deepMerge to combine payloads

This commit is contained in:
Cohee 2024-10-17 22:45:33 +03:00
parent 301a31b8b2
commit a3693ccf6d
3 changed files with 63 additions and 26 deletions

View File

@ -18,7 +18,7 @@ import {
} from '../../../script.js'; } from '../../../script.js';
import { getApiUrl, getContext, extension_settings, doExtrasFetch, modules, renderExtensionTemplateAsync, writeExtensionField } from '../../extensions.js'; import { getApiUrl, getContext, extension_settings, doExtrasFetch, modules, renderExtensionTemplateAsync, writeExtensionField } from '../../extensions.js';
import { selected_group } from '../../group-chats.js'; import { selected_group } from '../../group-chats.js';
import { stringFormat, initScrollHeight, resetScrollHeight, getCharaFilename, saveBase64AsFile, getBase64Async, delay, isTrueBoolean, debounce, isFalseBoolean } from '../../utils.js'; import { stringFormat, initScrollHeight, resetScrollHeight, getCharaFilename, saveBase64AsFile, getBase64Async, delay, isTrueBoolean, debounce, isFalseBoolean, deepMerge } from '../../utils.js';
import { getMessageTimeStamp, humanizedDateTime } from '../../RossAscends-mods.js'; import { getMessageTimeStamp, humanizedDateTime } from '../../RossAscends-mods.js';
import { SECRET_KEYS, secret_state, writeSecret } from '../../secrets.js'; import { SECRET_KEYS, secret_state, writeSecret } from '../../secrets.js';
import { getNovelUnlimitedImageGeneration, getNovelAnlas, loadNovelSubscriptionData } from '../../nai-settings.js'; import { getNovelUnlimitedImageGeneration, getNovelAnlas, loadNovelSubscriptionData } from '../../nai-settings.js';
@ -219,7 +219,7 @@ const defaultSettings = {
// Automatic1111/Horde exclusives // Automatic1111/Horde exclusives
restore_faces: false, restore_faces: false,
enable_hr: false, enable_hr: false,
aiFaceDetailer: false, adetailer_face: false,
// Horde settings // Horde settings
horde: false, horde: false,
@ -436,7 +436,7 @@ async function loadSettings() {
$('#sd_horde_sanitize').prop('checked', extension_settings.sd.horde_sanitize); $('#sd_horde_sanitize').prop('checked', extension_settings.sd.horde_sanitize);
$('#sd_restore_faces').prop('checked', extension_settings.sd.restore_faces); $('#sd_restore_faces').prop('checked', extension_settings.sd.restore_faces);
$('#sd_enable_hr').prop('checked', extension_settings.sd.enable_hr); $('#sd_enable_hr').prop('checked', extension_settings.sd.enable_hr);
$('#sd_aiFaceDetailer').prop('checked', extension_settings.sd.aiFaceDetailer); $('#sd_adetailer_face').prop('checked', extension_settings.sd.adetailer_face);
$('#sd_refine_mode').prop('checked', extension_settings.sd.refine_mode); $('#sd_refine_mode').prop('checked', extension_settings.sd.refine_mode);
$('#sd_multimodal_captioning').prop('checked', extension_settings.sd.multimodal_captioning); $('#sd_multimodal_captioning').prop('checked', extension_settings.sd.multimodal_captioning);
$('#sd_auto_url').val(extension_settings.sd.auto_url); $('#sd_auto_url').val(extension_settings.sd.auto_url);
@ -855,8 +855,8 @@ function onSamplerChange() {
saveSettingsDebounced(); saveSettingsDebounced();
} }
function onAiFaceDetailerChange() { function onADetailerFaceChange() {
extension_settings.sd.aiFaceDetailer = $('#sd_aiFaceDetailer').prop('checked'); extension_settings.sd.adetailer_face = !!$('#sd_adetailer_face').prop('checked');
saveSettingsDebounced(); saveSettingsDebounced();
} }
@ -2955,20 +2955,22 @@ async function generateAutoImage(prompt, negativePrompt, signal) {
do_not_save_grid: false, do_not_save_grid: false,
do_not_save_samples: false, do_not_save_samples: false,
}; };
// Conditionally add the ADetailer if aiFaceDetailer is enabled
console.log('extension_settings.sd.aiFaceDetailer:', extension_settings.sd.aiFaceDetailer); // Conditionally add the ADetailer if adetailer_face is enabled
if (extension_settings.sd.aiFaceDetailer) { if (extension_settings.sd.adetailer_face) {
payload.alwayson_scripts = { payload = deepMerge(payload, {
ADetailer: { alwayson_scripts: {
args: [ ADetailer: {
true, // ad_enable args: [
true, // skip_img2img true, // ad_enable
{ true, // skip_img2img
"ad_model": "face_yolov8n.pt" {
} 'ad_model': 'face_yolov8n.pt',
] },
} ],
}; },
},
});
} }
// Make the fetch call with the payload // Make the fetch call with the payload
@ -2978,8 +2980,6 @@ async function generateAutoImage(prompt, negativePrompt, signal) {
signal: signal, signal: signal,
body: JSON.stringify(payload), body: JSON.stringify(payload),
}); });
if (result.ok) { if (result.ok) {
const data = await result.json(); const data = await result.json();
@ -4210,7 +4210,7 @@ jQuery(async () => {
$('#sd_horde_sanitize').on('input', onHordeSanitizeInput); $('#sd_horde_sanitize').on('input', onHordeSanitizeInput);
$('#sd_restore_faces').on('input', onRestoreFacesInput); $('#sd_restore_faces').on('input', onRestoreFacesInput);
$('#sd_enable_hr').on('input', onHighResFixInput); $('#sd_enable_hr').on('input', onHighResFixInput);
$('#sd_aiFaceDetailer').on('change', onAiFaceDetailerChange); $('#sd_adetailer_face').on('change', onADetailerFaceChange);
$('#sd_refine_mode').on('input', onRefineModeInput); $('#sd_refine_mode').on('input', onRefineModeInput);
$('#sd_character_prompt').on('input', onCharacterPromptInput); $('#sd_character_prompt').on('input', onCharacterPromptInput);
$('#sd_character_negative_prompt').on('input', onCharacterNegativePromptInput); $('#sd_character_negative_prompt').on('input', onCharacterNegativePromptInput);

View File

@ -354,11 +354,16 @@
<small data-i18n="Karras">Karras</small> <small data-i18n="Karras">Karras</small>
<i class="fa-solid fa-info-circle fa-sm opacity50p" data-i18n="[title]Not all samplers supported." title="Not all samplers supported."></i> <i class="fa-solid fa-info-circle fa-sm opacity50p" data-i18n="[title]Not all samplers supported." title="Not all samplers supported."></i>
</label> </label>
<label for="sd_aiFaceDetailer" class="checkbox_label" data-i18n="[title]sd_aiFaceDetailer" title="Use AI Face Detailer in image generation."> </div>
<input id="sd_aiFaceDetailer" type="checkbox" />
<small data-i18n="sd_aiFaceDetailer">Use AI Face Detailer</small>
</label>
<div class="flex-container marginTopBot5" data-sd-source="auto">
<label for="sd_adetailer_face" class="flex1 checkbox_label" data-i18n="[title]sd_adetailer_face" title="Use ADetailer with face model during the generation. The ADetailer extension must be installed on the backend.">
<input id="sd_adetailer_face" type="checkbox" />
<small data-i18n="Use ADetailer (Face)">Use ADetailer (Face)</small>
</label>
<div class="flex1">
<!-- I will be useful later! -->
</div>
</div> </div>
<div class="flex-container marginTopBot5" data-sd-source="novel"> <div class="flex-container marginTopBot5" data-sd-source="novel">

View File

@ -23,6 +23,38 @@ export const navigation_option = {
previous: -1000, previous: -1000,
}; };
/**
* Determines if a value is an object.
* @param {any} item The item to check.
* @returns {boolean} True if the item is an object, false otherwise.
*/
function isObject(item) {
return (item && typeof item === 'object' && !Array.isArray(item));
}
/**
* Merges properties of two objects. If the property is an object, it will be merged recursively.
* @param {object} target The target object
* @param {object} source The source object
* @returns {object} Merged object
*/
export function deepMerge(target, source) {
let output = Object.assign({}, target);
if (isObject(target) && isObject(source)) {
Object.keys(source).forEach(key => {
if (isObject(source[key])) {
if (!(key in target))
Object.assign(output, { [key]: source[key] });
else
output[key] = deepMerge(target[key], source[key]);
} else {
Object.assign(output, { [key]: source[key] });
}
});
}
return output;
}
export function escapeHtml(str) { export function escapeHtml(str) {
return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;'); return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
} }