feat: inject LoRA trigger words into prompts at encoding time (#52)

When enabled, scans for <lora:NAME:WEIGHT> tags in prompt text and
appends trigger words from LoraManager metadata. Behind config toggle.
This commit is contained in:
Vito Sansevero
2026-04-03 07:28:24 -07:00
parent c0bcce5af1
commit 171a571a0d
3 changed files with 45 additions and 0 deletions
+3
View File
@@ -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
+39
View File
@@ -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.
+3
View File
@@ -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