mirror of
https://github.com/open-webui/open-webui.git
synced 2026-05-04 19:29:27 -05:00
refac
This commit is contained in:
@@ -1,16 +1,16 @@
|
||||
import re
|
||||
|
||||
|
||||
def extract_mentions(message: str, triggerChar: str = "@"):
|
||||
def extract_mentions(message: str, triggerChar: str = '@'):
|
||||
# Escape triggerChar in case it's a regex special character
|
||||
triggerChar = re.escape(triggerChar)
|
||||
pattern = rf"<{triggerChar}([A-Z]):([^|>]+)"
|
||||
pattern = rf'<{triggerChar}([A-Z]):([^|>]+)'
|
||||
|
||||
matches = re.findall(pattern, message)
|
||||
return [{"id_type": id_type, "id": id_value} for id_type, id_value in matches]
|
||||
return [{'id_type': id_type, 'id': id_value} for id_type, id_value in matches]
|
||||
|
||||
|
||||
def replace_mentions(message: str, triggerChar: str = "@", use_label: bool = True):
|
||||
def replace_mentions(message: str, triggerChar: str = '@', use_label: bool = True):
|
||||
"""
|
||||
Replace mentions in the message with either their label (after the pipe `|`)
|
||||
or their id if no label exists.
|
||||
@@ -27,5 +27,5 @@ def replace_mentions(message: str, triggerChar: str = "@", use_label: bool = Tru
|
||||
return label if use_label and label else id_value
|
||||
|
||||
# Regex captures: idType, id, optional label
|
||||
pattern = rf"<{triggerChar}([A-Z]):([^|>]+)(?:\|([^>]+))?>"
|
||||
pattern = rf'<{triggerChar}([A-Z]):([^|>]+)(?:\|([^>]+))?>'
|
||||
return re.sub(pattern, replacer, message)
|
||||
|
||||
Reference in New Issue
Block a user