Add Minimal Prompt Processing option (#5095) * Implement Minimal Prompt Processing * Fix and rename cmd argument. * Whitespace * Handle processing selection with a enum * Update processing argument description * Fix typo in minimal prompt processing label title * Add missing semi --------- Co-authored-by: Cohee <18619528+Cohee1207@users.noreply.github.com>

d6fafdd983c047ea304d63d518fcf416dc99d116

Kristy Aurelia <120577588+KrsityKu@users.noreply.github.com>

Signed
2 files changed, +51 -0Showing whitespace changes
public/scripts/extensions/stable-diffusion/index.js+47 -0
@@ -284,6 +284,7 @@ const defaultSettings = {
284 snap: false,284 snap: false,
285 free_extend: false,285 free_extend: false,
286 function_tool: false,286 function_tool: false,
287 minimal_prompt_processing: false,
287288
288 prompts: promptTemplates,289 prompts: promptTemplates,
289290
@@ -547,6 +548,7 @@ async function loadSettings() {
547 $('#sd_comfy_prompt').val(extension_settings.sd.comfy_prompt);548 $('#sd_comfy_prompt').val(extension_settings.sd.comfy_prompt);
548 $('#sd_comfy_runpod_url').val(extension_settings.sd.comfy_runpod_url);549 $('#sd_comfy_runpod_url').val(extension_settings.sd.comfy_runpod_url);
549 $('#sd_snap').prop('checked', extension_settings.sd.snap);550 $('#sd_snap').prop('checked', extension_settings.sd.snap);
551 $('#sd_minimal_prompt_processing').prop('checked', extension_settings.sd.minimal_prompt_processing);
550 $('#sd_clip_skip').val(extension_settings.sd.clip_skip);552 $('#sd_clip_skip').val(extension_settings.sd.clip_skip);
551 $('#sd_clip_skip_value').val(extension_settings.sd.clip_skip);553 $('#sd_clip_skip_value').val(extension_settings.sd.clip_skip);
552 $('#sd_seed').val(extension_settings.sd.seed);554 $('#sd_seed').val(extension_settings.sd.seed);
@@ -669,6 +671,11 @@ function onSnapInput() {
669 saveSettingsDebounced();671 saveSettingsDebounced();
670}672}
671673
674function onMinimalPromptProcessing() {
675 extension_settings.sd.minimal_prompt_processing = !!$(this).prop('checked');
676 saveSettingsDebounced();
677}
678
672function onStyleSelect() {679function onStyleSelect() {
673 const selectedStyle = String($('#sd_style').find(':selected').val());680 const selectedStyle = String($('#sd_style').find(':selected').val());
674 const styleObject = extension_settings.sd.styles.find(x => x.name === selectedStyle);681 const styleObject = extension_settings.sd.styles.find(x => x.name === selectedStyle);
@@ -2735,6 +2742,15 @@ function processReply(str) {
2735 return '';2742 return '';
2736 }2743 }
27372744
2745 if (extension_settings.sd.minimal_prompt_processing) {
2746 // Minimal prompt processing
2747 // JSON and similar should be preserved
2748 str = str.normalize('NFD');
2749 str = str.replace(/\s+/g, ' '); // Collapse multiple whitespaces into one
2750 str = str.trim();
2751 return str;
2752 }
2753
2738 str = str.replaceAll('"', '');2754 str = str.replaceAll('"', '');
2739 str = str.replaceAll('“', '');2755 str = str.replaceAll('“', '');
2740 str = str.replaceAll('\n', ', ');2756 str = str.replaceAll('\n', ', ');
@@ -5153,6 +5169,17 @@ function applyCommandArguments(args) {
5153 'denoise': 'denoising_strength',5169 'denoise': 'denoising_strength',
5154 '2ndpass': 'hr_second_pass_steps',5170 '2ndpass': 'hr_second_pass_steps',
5155 'faces': 'restore_faces',5171 'faces': 'restore_faces',
5172 'processing': 'minimal_prompt_processing',
5173 };
5174 const enumHandlers = {
5175 'processing': (value) => {
5176 if (/standard/gi.test(String(value))) {
5177 return false;
5178 }
5179 if (/minimal/gi.test(String(value))) {
5180 return true;
5181 }
5182 },
5156 };5183 };
51575184
5158 for (const [param, setting] of Object.entries(settingMap)) {5185 for (const [param, setting] of Object.entries(settingMap)) {
@@ -5161,6 +5188,14 @@ function applyCommandArguments(args) {
5161 }5188 }
5162 currentSettings[setting] = extension_settings.sd[setting];5189 currentSettings[setting] = extension_settings.sd[setting];
5163 const value = String(args[param]);5190 const value = String(args[param]);
5191 const enumHandler = enumHandlers[param];
5192 if (typeof enumHandler === 'function') {
5193 const enumValue = enumHandler(value);
5194 if (enumValue !== undefined) {
5195 overrideSettings[setting] = enumValue;
5196 }
5197 continue;
5198 }
5164 const type = typeof defaultSettings[setting];5199 const type = typeof defaultSettings[setting];
5165 switch (type) {5200 switch (type) {
5166 case 'boolean':5201 case 'boolean':
@@ -5283,6 +5318,17 @@ jQuery(async () => {
5283 acceptsMultiple: false,5318 acceptsMultiple: false,
5284 }),5319 }),
5285 SlashCommandNamedArgument.fromProps({5320 SlashCommandNamedArgument.fromProps({
5321 name: 'processing',
5322 description: 'level of response prompt processing returned by the LLM',
5323 typeList: [ARGUMENT_TYPE.STRING],
5324 enumList: [
5325 new SlashCommandEnumValue('standard', 'Standard prompt processing'),
5326 new SlashCommandEnumValue('minimal', 'Minimal prompt processing'),
5327 ],
5328 isRequired: false,
5329 acceptsMultiple: false,
5330 }),
5331 SlashCommandNamedArgument.fromProps({
5286 name: 'seed',5332 name: 'seed',
5287 description: 'random seed',5333 description: 'random seed',
5288 isRequired: false,5334 isRequired: false,
@@ -5565,6 +5611,7 @@ jQuery(async () => {
5565 $('#sd_openai_duration').on('input', onOpenAiDurationSelect);5611 $('#sd_openai_duration').on('input', onOpenAiDurationSelect);
5566 $('#sd_multimodal_captioning').on('input', onMultimodalCaptioningInput);5612 $('#sd_multimodal_captioning').on('input', onMultimodalCaptioningInput);
5567 $('#sd_snap').on('input', onSnapInput);5613 $('#sd_snap').on('input', onSnapInput);
5614 $('#sd_minimal_prompt_processing').on('input', onMinimalPromptProcessing);
5568 $('#sd_clip_skip').on('input', onClipSkipInput);5615 $('#sd_clip_skip').on('input', onClipSkipInput);
5569 $('#sd_seed').on('input', onSeedInput);5616 $('#sd_seed').on('input', onSeedInput);
5570 $('#sd_character_prompt_share').on('input', onCharacterPromptShareInput);5617 $('#sd_character_prompt_share').on('input', onCharacterPromptShareInput);
public/scripts/extensions/stable-diffusion/settings.html+4 -0
@@ -35,6 +35,10 @@
35 <input id="sd_snap" type="checkbox" />35 <input id="sd_snap" type="checkbox" />
36 <span data-i18n="sd_snap_txt">Snap auto-adjusted resolutions</span>36 <span data-i18n="sd_snap_txt">Snap auto-adjusted resolutions</span>
37 </label>37 </label>
38 <label for="sd_minimal_prompt_processing" class="checkbox_label" data-i18n="[title]sd_minimal_prompt_processing" title="Reduce post-processing on a prompt generated by the LLM to preserve JSON and other structured output.">
39 <input id="sd_minimal_prompt_processing" type="checkbox" />
40 <span data-i18n="sd_minimal_prompt_processing_txt">Minimal response prompt processing</span>
41 </label>
38 <label for="sd_source" data-i18n="Source">Source</label>42 <label for="sd_source" data-i18n="Source">Source</label>
39 <select id="sd_source">43 <select id="sd_source">
40 <option value="aimlapi">AI/ML API</option>44 <option value="aimlapi">AI/ML API</option>