Skip to content

Send a phone call from AI agent, in an API call. Or, directly call the bot from the configured phone number!

Python

fix: Better detect action and styles in sentences

Clémence Lesnéa244f97d7c35
diff --git a/models/message.py b/models/message.py
index db1e158..5fb1795 100644
--- a/models/message.py
+++ b/models/message.py
@@ -16,8 +16,8 @@ import json
_FUNC_NAME_SANITIZER_R = r"[^a-zA-Z0-9_-]"
-_MESSAGE_ACTION_R = r"action(?:=)?([a-z_]*)( .*)?"
-_MESSAGE_STYLE_R = r"style(?:=)?([a-z_]*)( .*)?"
+_MESSAGE_ACTION_R = r"(?:action=*([a-z_]*))? *(.*)"
+_MESSAGE_STYLE_R = r"(?:style=*([a-z_]*))? *(.*)"
class StyleEnum(str, Enum):
@@ -212,22 +212,28 @@ def remove_message_action(text: str) -> str:
"""
Remove action from content. AI often adds it by mistake event if explicitly asked not to.
"""
+ # TODO: Use JSON as LLM response instead of using a regex to parse the text
res = re.match(_MESSAGE_ACTION_R, text)
if not res:
- return text.strip()
- content = res.group(2)
- return content.strip() if content else ""
+ return text
+ try:
+ return res.group(2) or ""
+ except ValueError: # Regex failed, return original text
+ return text
def extract_message_style(text: str) -> tuple[Optional[StyleEnum], str]:
"""
Detect the style of a message.
"""
+ # TODO: Use JSON as LLM response instead of using a regex to parse the text
res = re.match(_MESSAGE_STYLE_R, text)
if not res:
return None, text
try:
- content = res.group(2)
- return StyleEnum(res.group(1)), (content.strip() if content else "")
- except ValueError:
+ return (
+ StyleEnum(res.group(1)), # style
+ (res.group(2) or ""), # content
+ )
+ except ValueError: # Regex failed, return original text
return None, text