feat(trigger-word-toggle): add strength value support for trigger words

- Extract and preserve strength values from trigger words in format "(word:strength)"
- Maintain strength formatting when filtering active trigger words in both group and individual modes
- Update active state tracking to handle strength-modified words correctly
- Ensure backward compatibility with existing trigger word formats
This commit is contained in:
Will Miao
2025-11-07 16:38:04 +08:00
parent 388ff7f5b4
commit dd27411ebf
3 changed files with 265 additions and 65 deletions

View File

@@ -63,7 +63,22 @@ class TriggerWordToggle:
trigger_data = json.loads(trigger_data)
# Create dictionaries to track active state of words or groups
active_state = {item['text']: item.get('active', False) for item in trigger_data}
# Also track strength values for each trigger word
active_state = {}
strength_map = {}
for item in trigger_data:
text = item['text']
active = item.get('active', False)
# Extract strength if it's in the format "(word:strength)"
strength_match = re.match(r'\((.+):([\d.]+)\)', text)
if strength_match:
original_word = strength_match.group(1)
strength = float(strength_match.group(2))
active_state[original_word] = active
strength_map[original_word] = strength
else:
active_state[text] = active
if group_mode:
# Split by two or more consecutive commas to get groups
@@ -71,19 +86,60 @@ class TriggerWordToggle:
# Remove leading/trailing whitespace from each group
groups = [group.strip() for group in groups]
# Filter groups: keep those not in toggle_trigger_words or those that are active
filtered_groups = [group for group in groups if group not in active_state or active_state[group]]
# Process groups: keep those not in toggle_trigger_words or those that are active
filtered_groups = []
for group in groups:
# Check if this group contains any words that are in the active_state
group_words = [word.strip() for word in group.split(',')]
active_group_words = []
for word in group_words:
# Remove any existing strength formatting for comparison
word_comparison = re.sub(r'\((.+):([\d.]+)\)', r'\1', word).strip()
if word_comparison not in active_state or active_state[word_comparison]:
# If this word has a strength value, use that instead of the original
if word_comparison in strength_map:
active_group_words.append(f"({word_comparison}:{strength_map[word_comparison]:.2f})")
else:
# Preserve existing strength formatting if the word was previously modified
# Check if the original word had strength formatting
strength_match = re.match(r'\((.+):([\d.]+)\)', word)
if strength_match:
active_group_words.append(word)
else:
active_group_words.append(word)
if active_group_words:
filtered_groups.append(', '.join(active_group_words))
if filtered_groups:
filtered_triggers = ', '.join(filtered_groups)
else:
filtered_triggers = ""
else:
# Original behavior for individual words mode
# Normal mode: split by commas and treat each word as a separate tag
original_words = [word.strip() for word in trigger_words.split(',')]
# Filter out empty strings
original_words = [word for word in original_words if word]
filtered_words = [word for word in original_words if word not in active_state or active_state[word]]
filtered_words = []
for word in original_words:
# Remove any existing strength formatting for comparison
word_comparison = re.sub(r'\((.+):([\d.]+)\)', r'\1', word).strip()
if word_comparison not in active_state or active_state[word_comparison]:
# If this word has a strength value, use that instead of the original
if word_comparison in strength_map:
filtered_words.append(f"({word_comparison}:{strength_map[word_comparison]:.2f})")
else:
# Preserve existing strength formatting if the word was previously modified
# Check if the original word had strength formatting
strength_match = re.match(r'\((.+):([\d.]+)\)', word)
if strength_match:
filtered_words.append(word)
else:
filtered_words.append(word)
if filtered_words:
filtered_triggers = ', '.join(filtered_words)