From 6142b3dc0cebf5c9756ec716b371bfa11ffa2e9b Mon Sep 17 00:00:00 2001 From: Will Miao Date: Sun, 25 Jan 2026 12:53:41 +0800 Subject: [PATCH] feat: consolidate ComfyUI settings and add custom words autocomplete toggle Create unified settings.js extension to centralize all Lora Manager ComfyUI settings registration, eliminating code duplication across multiple files. Add new setting "Enable Custom Words Autocomplete in Prompt Nodes" (enabled by default) to control custom words autocomplete in prompt node text widgets. When disabled, only 'emb:' prefix triggers embeddings autocomplete. Changes: - Create web/comfyui/settings.js with all three settings: * Trigger Word Wheel Sensitivity (existing) * Auto path correction (existing) * Enable Custom Words Autocomplete in Prompt Nodes (new) - Refactor autocomplete.js to respect the new setting - Update trigger_word_toggle.js to import from settings.js - Update usage_stats.js to import from settings.js --- web/comfyui/autocomplete.js | 11 ++- web/comfyui/settings.js | 141 +++++++++++++++++++++++++++++ web/comfyui/trigger_word_toggle.js | 48 +--------- web/comfyui/usage_stats.js | 40 +------- 4 files changed, 151 insertions(+), 89 deletions(-) create mode 100644 web/comfyui/settings.js diff --git a/web/comfyui/autocomplete.js b/web/comfyui/autocomplete.js index eb343b4a..9805836f 100644 --- a/web/comfyui/autocomplete.js +++ b/web/comfyui/autocomplete.js @@ -1,6 +1,7 @@ import { api } from "../../scripts/api.js"; import { app } from "../../scripts/app.js"; import { TextAreaCaretHelper } from "./textarea_caret_helper.js"; +import { getPromptCustomWordsAutocompletePreference } from "./settings.js"; function parseUsageTipNumber(value) { if (typeof value === 'number' && Number.isFinite(value)) { @@ -420,15 +421,19 @@ class AutoComplete { if (this.modelType === 'prompt') { const match = rawSearchTerm.match(/^emb:(.*)$/i); if (match) { - // User typed "emb:" prefix - search embeddings + // User typed "emb:" prefix - always allow embeddings search endpoint = '/lm/embeddings/relative-paths'; searchTerm = (match[1] || '').trim(); this.searchType = 'embeddings'; - } else { - // No prefix - search custom words + } else if (getPromptCustomWordsAutocompletePreference()) { + // Setting enabled - allow custom words search endpoint = '/lm/custom-words/search'; searchTerm = rawSearchTerm; this.searchType = 'custom_words'; + } else { + // Setting disabled - no autocomplete for non-emb: terms + this.hide(); + return; } } diff --git a/web/comfyui/settings.js b/web/comfyui/settings.js new file mode 100644 index 00000000..3917f8af --- /dev/null +++ b/web/comfyui/settings.js @@ -0,0 +1,141 @@ +import { app } from "../../scripts/app.js"; + +// ============================================================================ +// Setting IDs and Defaults +// ============================================================================ + +const TRIGGER_WORD_WHEEL_SENSITIVITY_ID = "loramanager.trigger_word_wheel_sensitivity"; +const TRIGGER_WORD_WHEEL_SENSITIVITY_DEFAULT = 0.02; + +const AUTO_PATH_CORRECTION_SETTING_ID = "loramanager.auto_path_correction"; +const AUTO_PATH_CORRECTION_DEFAULT = true; + +const PROMPT_CUSTOM_WORDS_AUTOCOMPLETE_SETTING_ID = "loramanager.prompt_custom_words_autocomplete"; +const PROMPT_CUSTOM_WORDS_AUTOCOMPLETE_DEFAULT = true; + +// ============================================================================ +// Helper Functions +// ============================================================================ + +const getWheelSensitivity = (() => { + let settingsUnavailableLogged = false; + + return () => { + const settingManager = app?.extensionManager?.setting; + if (!settingManager || typeof settingManager.get !== "function") { + if (!settingsUnavailableLogged) { + console.warn("LoRA Manager: settings API unavailable, using default wheel sensitivity."); + settingsUnavailableLogged = true; + } + return TRIGGER_WORD_WHEEL_SENSITIVITY_DEFAULT; + } + + try { + const value = settingManager.get(TRIGGER_WORD_WHEEL_SENSITIVITY_ID); + return value ?? TRIGGER_WORD_WHEEL_SENSITIVITY_DEFAULT; + } catch (error) { + if (!settingsUnavailableLogged) { + console.warn("LoRA Manager: unable to read wheel sensitivity setting, using default.", error); + settingsUnavailableLogged = true; + } + return TRIGGER_WORD_WHEEL_SENSITIVITY_DEFAULT; + } + }; +})(); + +const getAutoPathCorrectionPreference = (() => { + let settingsUnavailableLogged = false; + + return () => { + const settingManager = app?.extensionManager?.setting; + if (!settingManager || typeof settingManager.get !== "function") { + if (!settingsUnavailableLogged) { + console.warn("LoRA Manager: settings API unavailable, defaulting auto path correction to enabled."); + settingsUnavailableLogged = true; + } + return AUTO_PATH_CORRECTION_DEFAULT; + } + + try { + const value = settingManager.get(AUTO_PATH_CORRECTION_SETTING_ID); + return value ?? AUTO_PATH_CORRECTION_DEFAULT; + } catch (error) { + if (!settingsUnavailableLogged) { + console.warn("LoRA Manager: unable to read auto path correction setting, defaulting to enabled.", error); + settingsUnavailableLogged = true; + } + return AUTO_PATH_CORRECTION_DEFAULT; + } + }; +})(); + +const getPromptCustomWordsAutocompletePreference = (() => { + let settingsUnavailableLogged = false; + + return () => { + const settingManager = app?.extensionManager?.setting; + if (!settingManager || typeof settingManager.get !== "function") { + if (!settingsUnavailableLogged) { + console.warn("LoRA Manager: settings API unavailable, using default custom words autocomplete setting."); + settingsUnavailableLogged = true; + } + return PROMPT_CUSTOM_WORDS_AUTOCOMPLETE_DEFAULT; + } + + try { + const value = settingManager.get(PROMPT_CUSTOM_WORDS_AUTOCOMPLETE_SETTING_ID); + return value ?? PROMPT_CUSTOM_WORDS_AUTOCOMPLETE_DEFAULT; + } catch (error) { + if (!settingsUnavailableLogged) { + console.warn("LoRA Manager: unable to read custom words autocomplete setting, using default.", error); + settingsUnavailableLogged = true; + } + return PROMPT_CUSTOM_WORDS_AUTOCOMPLETE_DEFAULT; + } + }; +})(); + +// ============================================================================ +// Register Extension with All Settings +// ============================================================================ + +app.registerExtension({ + name: "LoraManager.Settings", + settings: [ + { + id: TRIGGER_WORD_WHEEL_SENSITIVITY_ID, + name: "Trigger Word Wheel Sensitivity", + type: "slider", + attrs: { + min: 0.01, + max: 0.1, + step: 0.01, + }, + defaultValue: TRIGGER_WORD_WHEEL_SENSITIVITY_DEFAULT, + tooltip: "Mouse wheel sensitivity for adjusting trigger word strength (default: 0.02)", + category: ["LoRA Manager", "Trigger Word Toggle", "Wheel Sensitivity"], + }, + { + id: AUTO_PATH_CORRECTION_SETTING_ID, + name: "Auto path correction", + type: "boolean", + defaultValue: AUTO_PATH_CORRECTION_DEFAULT, + tooltip: "Automatically update model paths to their current file locations.", + category: ["LoRA Manager", "Automation", "Auto path correction"], + }, + { + id: PROMPT_CUSTOM_WORDS_AUTOCOMPLETE_SETTING_ID, + name: "Enable Custom Words Autocomplete in Prompt Nodes", + type: "boolean", + defaultValue: PROMPT_CUSTOM_WORDS_AUTOCOMPLETE_DEFAULT, + tooltip: "When enabled, prompt nodes will autocomplete custom words. When disabled, only 'emb:' prefix will trigger embeddings autocomplete.", + category: ["LoRA Manager", "Autocomplete", "Prompt"], + }, + ], +}); + +// ============================================================================ +// Exports +// ============================================================================ + +export { getWheelSensitivity, getAutoPathCorrectionPreference, getPromptCustomWordsAutocompletePreference }; diff --git a/web/comfyui/trigger_word_toggle.js b/web/comfyui/trigger_word_toggle.js index 7ff85f78..40c90231 100644 --- a/web/comfyui/trigger_word_toggle.js +++ b/web/comfyui/trigger_word_toggle.js @@ -2,58 +2,12 @@ import { app } from "../../scripts/app.js"; import { api } from "../../scripts/api.js"; import { CONVERTED_TYPE, getNodeFromGraph } from "./utils.js"; import { addTagsWidget } from "./tags_widget.js"; - -// Setting ID for wheel sensitivity -const TRIGGER_WORD_WHEEL_SENSITIVITY_ID = "loramanager.trigger_word_wheel_sensitivity"; -const TRIGGER_WORD_WHEEL_SENSITIVITY_DEFAULT = 0.02; - -// Get the wheel sensitivity setting value -const getWheelSensitivity = (() => { - let settingsUnavailableLogged = false; - - return () => { - const settingManager = app?.extensionManager?.setting; - if (!settingManager || typeof settingManager.get !== "function") { - if (!settingsUnavailableLogged) { - console.warn("LoRA Manager: settings API unavailable, using default wheel sensitivity."); - settingsUnavailableLogged = true; - } - return TRIGGER_WORD_WHEEL_SENSITIVITY_DEFAULT; - } - - try { - const value = settingManager.get(TRIGGER_WORD_WHEEL_SENSITIVITY_ID); - return value ?? TRIGGER_WORD_WHEEL_SENSITIVITY_DEFAULT; - } catch (error) { - if (!settingsUnavailableLogged) { - console.warn("LoRA Manager: unable to read wheel sensitivity setting, using default.", error); - settingsUnavailableLogged = true; - } - return TRIGGER_WORD_WHEEL_SENSITIVITY_DEFAULT; - } - }; -})(); +import { getWheelSensitivity } from "./settings.js"; // TriggerWordToggle extension for ComfyUI app.registerExtension({ name: "LoraManager.TriggerWordToggle", - settings: [ - { - id: TRIGGER_WORD_WHEEL_SENSITIVITY_ID, - name: "Trigger Word Wheel Sensitivity", - type: "slider", - attrs: { - min: 0.01, - max: 0.1, - step: 0.01, - }, - defaultValue: TRIGGER_WORD_WHEEL_SENSITIVITY_DEFAULT, - tooltip: "Mouse wheel sensitivity for adjusting trigger word strength (default: 0.02)", - category: ["LoRA Manager", "Trigger Word Toggle", "Wheel Sensitivity"], - }, - ], - setup() { // Add message handler to listen for messages from Python api.addEventListener("trigger_word_update", (event) => { diff --git a/web/comfyui/usage_stats.js b/web/comfyui/usage_stats.js index 4a6ace12..e4b8f28c 100644 --- a/web/comfyui/usage_stats.js +++ b/web/comfyui/usage_stats.js @@ -2,6 +2,7 @@ import { app } from "../../scripts/app.js"; import { api } from "../../scripts/api.js"; import { showToast } from "./utils.js"; +import { getAutoPathCorrectionPreference } from "./settings.js"; // Define target nodes and their widget configurations const PATH_CORRECTION_TARGETS = [ @@ -17,48 +18,9 @@ const PATH_CORRECTION_TARGETS = [ { comfyClass: "easy loraStack", widgetNamePattern: "lora_\\d+_name", modelType: "loras" } ]; -const AUTO_PATH_CORRECTION_SETTING_ID = "loramanager.auto_path_correction"; -const AUTO_PATH_CORRECTION_DEFAULT = true; - -const getAutoPathCorrectionPreference = (() => { - let settingsUnavailableLogged = false; - - return () => { - const settingManager = app?.extensionManager?.setting; - if (!settingManager || typeof settingManager.get !== "function") { - if (!settingsUnavailableLogged) { - console.warn("LoRA Manager: settings API unavailable, defaulting auto path correction to enabled."); - settingsUnavailableLogged = true; - } - return AUTO_PATH_CORRECTION_DEFAULT; - } - - try { - const value = settingManager.get(AUTO_PATH_CORRECTION_SETTING_ID); - return value ?? AUTO_PATH_CORRECTION_DEFAULT; - } catch (error) { - if (!settingsUnavailableLogged) { - console.warn("LoRA Manager: unable to read auto path correction setting, defaulting to enabled.", error); - settingsUnavailableLogged = true; - } - return AUTO_PATH_CORRECTION_DEFAULT; - } - }; -})(); - // Register the extension app.registerExtension({ name: "LoraManager.UsageStats", - settings: [ - { - id: AUTO_PATH_CORRECTION_SETTING_ID, - name: "Auto path correction", - type: "boolean", - defaultValue: AUTO_PATH_CORRECTION_DEFAULT, - tooltip: "Automatically update model paths to their current file locations.", - category: ["LoRA Manager", "Automation", "Auto path correction"], - }, - ], setup() { // Listen for successful executions