From 171a571a0d1908b839ccd03e674a9494d1e5ca5e Mon Sep 17 00:00:00 2001 From: Vito Sansevero Date: Fri, 3 Apr 2026 07:12:35 -0700 Subject: [PATCH] feat: inject LoRA trigger words into prompts at encoding time (#52) When enabled, scans for tags in prompt text and appends trigger words from LoraManager metadata. Behind config toggle. --- prompt_manager.py | 3 +++ prompt_manager_base.py | 39 +++++++++++++++++++++++++++++++++++++++ prompt_manager_text.py | 3 +++ 3 files changed, 45 insertions(+) diff --git a/prompt_manager.py b/prompt_manager.py index e8d904a..a1eceff 100644 --- a/prompt_manager.py +++ b/prompt_manager.py @@ -148,6 +148,9 @@ class PromptManager(PromptManagerBase, ComfyNodeABC): parts.append(append_text.strip()) final_text = " ".join(parts) + # Inject LoRA trigger words if integration is enabled + final_text = self._inject_lora_trigger_words(final_text) + # Use the combined text for encoding encoding_text = final_text diff --git a/prompt_manager_base.py b/prompt_manager_base.py index 9a393fc..43a6326 100644 --- a/prompt_manager_base.py +++ b/prompt_manager_base.py @@ -102,6 +102,45 @@ class PromptManagerBase: self.logger.error(f"Error saving prompt to database: {e}") return None + def _inject_lora_trigger_words(self, text: str) -> str: + """Append LoRA trigger words if integration is enabled. + + Returns the text unchanged if the integration is disabled or + LoraManager is not installed. + """ + try: + from .py.config import IntegrationConfig + except ImportError: + try: + from py.config import IntegrationConfig + except ImportError: + return text + + if ( + not IntegrationConfig.LORA_MANAGER_ENABLED + or not IntegrationConfig.LORA_TRIGGER_WORDS_ENABLED + ): + return text + + try: + from .py.lora_utils import get_trigger_cache, inject_trigger_words + except ImportError: + try: + from py.lora_utils import get_trigger_cache, inject_trigger_words + except ImportError: + return text + + cache = get_trigger_cache() + + # Lazy-load cache on first use + if not cache.is_loaded and IntegrationConfig.LORA_MANAGER_PATH: + cache.load(IntegrationConfig.LORA_MANAGER_PATH) + + modified, injected = inject_trigger_words(text, cache) + if injected: + self.logger.info(f"Injected trigger words: {', '.join(injected)}") + return modified + def _generate_hash(self, text: str) -> str: """Generate SHA256 hash for the prompt text. diff --git a/prompt_manager_text.py b/prompt_manager_text.py index e10d146..6e0736c 100644 --- a/prompt_manager_text.py +++ b/prompt_manager_text.py @@ -142,6 +142,9 @@ class PromptManagerText(PromptManagerBase, ComfyNodeABC): parts.append(append_text.strip()) final_text = " ".join(parts) + # Inject LoRA trigger words if integration is enabled + final_text = self._inject_lora_trigger_words(final_text) + # For database storage, save the original main text with metadata about prepend/append storage_text = text