Spaces:
Build error
Build error
Update backend_app/flows.py
Browse files- backend_app/flows.py +230 -222
backend_app/flows.py
CHANGED
|
@@ -1,222 +1,230 @@
|
|
| 1 |
-
from __future__ import annotations
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
from .
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
"
|
| 33 |
-
"
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
if any(k in t for k in ["
|
| 45 |
-
intents.append("
|
| 46 |
-
if any(k in t for k in ["
|
| 47 |
-
intents.append("
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
"
|
| 59 |
-
"
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
"
|
| 87 |
-
"
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
"
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
"
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
"
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
"
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
"
|
| 161 |
-
"
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
"""
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from typing import Dict, List
|
| 4 |
+
import re
|
| 5 |
+
|
| 6 |
+
from .email_service import send_contact_email
|
| 7 |
+
from .suggestions import (
|
| 8 |
+
default_suggestions,
|
| 9 |
+
suggestions_for_intent,
|
| 10 |
+
suggestions_from_text,
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class FlowManager:
|
| 15 |
+
"""
|
| 16 |
+
Manages lightweight session state for:
|
| 17 |
+
- Contact flow (collect message + email)
|
| 18 |
+
- Language flow (ask region -> ask language)
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
def __init__(self):
|
| 22 |
+
self.sessions: Dict[str, Dict] = {}
|
| 23 |
+
|
| 24 |
+
# ---------- Suggestions ----------
|
| 25 |
+
def default_suggestions(self) -> List[str]:
|
| 26 |
+
return default_suggestions()
|
| 27 |
+
|
| 28 |
+
# ---------- Session helpers ----------
|
| 29 |
+
def _get(self, session_id: str) -> Dict:
|
| 30 |
+
if session_id not in self.sessions:
|
| 31 |
+
self.sessions[session_id] = {
|
| 32 |
+
"mode": "normal", # normal | contact_wait_msg | contact_wait_email | lang_wait_region | lang_wait_language
|
| 33 |
+
"contact_msg": None,
|
| 34 |
+
"lang": None, # preferred language (string), e.g. "Sinhala"
|
| 35 |
+
"region": None, # store region (string) if provided
|
| 36 |
+
}
|
| 37 |
+
return self.sessions[session_id]
|
| 38 |
+
|
| 39 |
+
# ---------- Intents ----------
|
| 40 |
+
def _detect_intents(self, text: str) -> List[str]:
|
| 41 |
+
t = (text or "").lower()
|
| 42 |
+
|
| 43 |
+
intents: List[str] = []
|
| 44 |
+
if any(k in t for k in ["contact", "support", "help desk", "reach", "email us", "contact us"]):
|
| 45 |
+
intents.append("contact")
|
| 46 |
+
if any(k in t for k in ["change response language", "change language", "language", "switch language", "translate"]):
|
| 47 |
+
intents.append("language")
|
| 48 |
+
if any(k in t for k in ["service", "services", "what do you do", "features", "what is syslink", "about"]):
|
| 49 |
+
intents.append("services")
|
| 50 |
+
|
| 51 |
+
return intents or ["rag"]
|
| 52 |
+
|
| 53 |
+
# ---------- Main entry ----------
|
| 54 |
+
def handle_message(self, session_id: str, user_message: str) -> Dict:
|
| 55 |
+
"""
|
| 56 |
+
Returns dict:
|
| 57 |
+
{
|
| 58 |
+
"action": "flow" | "rag",
|
| 59 |
+
"answer": "...",
|
| 60 |
+
"suggestions": [...],
|
| 61 |
+
"lang": optional preferred language for RAG
|
| 62 |
+
}
|
| 63 |
+
"""
|
| 64 |
+
state = self._get(session_id)
|
| 65 |
+
msg = (user_message or "").strip()
|
| 66 |
+
|
| 67 |
+
# 1) In-flow handling first
|
| 68 |
+
if state["mode"].startswith("contact_"):
|
| 69 |
+
return self._handle_contact_flow(state, msg)
|
| 70 |
+
|
| 71 |
+
if state["mode"].startswith("lang_"):
|
| 72 |
+
return self._handle_language_flow(state, msg)
|
| 73 |
+
|
| 74 |
+
# 2) Detect intent(s)
|
| 75 |
+
intents = self._detect_intents(msg)
|
| 76 |
+
|
| 77 |
+
# If user typed a custom prompt, provide new related suggestions
|
| 78 |
+
dynamic_suggestions = suggestions_from_text(msg)
|
| 79 |
+
|
| 80 |
+
# 3) Multi-intent handling
|
| 81 |
+
# If user asks "contact + language" together, do language first, then contact
|
| 82 |
+
if "contact" in intents and "language" in intents:
|
| 83 |
+
state["mode"] = "lang_wait_region"
|
| 84 |
+
state["next_after_lang"] = "contact"
|
| 85 |
+
return {
|
| 86 |
+
"action": "flow",
|
| 87 |
+
"answer": "Sure — first, tell me your region/country.",
|
| 88 |
+
"suggestions": [],
|
| 89 |
+
"lang": state.get("lang"),
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
# 4) Single intent handling
|
| 93 |
+
if "language" in intents:
|
| 94 |
+
state["mode"] = "lang_wait_region"
|
| 95 |
+
state["next_after_lang"] = None
|
| 96 |
+
return {
|
| 97 |
+
"action": "flow",
|
| 98 |
+
"answer": "Sure — tell me your region/country.",
|
| 99 |
+
"suggestions": [],
|
| 100 |
+
"lang": state.get("lang"),
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
if "contact" in intents:
|
| 104 |
+
state["mode"] = "contact_wait_msg"
|
| 105 |
+
return {
|
| 106 |
+
"action": "flow",
|
| 107 |
+
"answer": "Sure — please type your message for our team.",
|
| 108 |
+
"suggestions": suggestions_for_intent("contact"),
|
| 109 |
+
"lang": state.get("lang"),
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
if "services" in intents:
|
| 113 |
+
return {
|
| 114 |
+
"action": "rag",
|
| 115 |
+
"answer": "",
|
| 116 |
+
"suggestions": suggestions_for_intent("services"),
|
| 117 |
+
"lang": state.get("lang"),
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
# 5) Default: RAG
|
| 121 |
+
return {
|
| 122 |
+
"action": "rag",
|
| 123 |
+
"answer": "",
|
| 124 |
+
"suggestions": dynamic_suggestions,
|
| 125 |
+
"lang": state.get("lang"),
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
# ---------- Contact flow ----------
|
| 129 |
+
def _handle_contact_flow(self, state: Dict, msg: str) -> Dict:
|
| 130 |
+
if state["mode"] == "contact_wait_msg":
|
| 131 |
+
state["contact_msg"] = msg
|
| 132 |
+
state["mode"] = "contact_wait_email"
|
| 133 |
+
return {
|
| 134 |
+
"action": "flow",
|
| 135 |
+
"answer": "Thanks. Now please enter your email address.",
|
| 136 |
+
"suggestions": [],
|
| 137 |
+
"lang": state.get("lang"),
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
if state["mode"] == "contact_wait_email":
|
| 141 |
+
if not self._is_valid_email(msg):
|
| 142 |
+
return {
|
| 143 |
+
"action": "flow",
|
| 144 |
+
"answer": "That email doesn’t look valid. Please type a valid email (example: name@gmail.com).",
|
| 145 |
+
"suggestions": [],
|
| 146 |
+
"lang": state.get("lang"),
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
email = msg
|
| 150 |
+
message = state.get("contact_msg") or ""
|
| 151 |
+
|
| 152 |
+
result = send_contact_email(user_email=email, user_message=message)
|
| 153 |
+
|
| 154 |
+
# Reset flow state
|
| 155 |
+
state["mode"] = "normal"
|
| 156 |
+
state["contact_msg"] = None
|
| 157 |
+
|
| 158 |
+
if result.get("ok"):
|
| 159 |
+
return {
|
| 160 |
+
"action": "flow",
|
| 161 |
+
"answer": "✅ Sent! Thanks — our team will contact you soon.",
|
| 162 |
+
"suggestions": default_suggestions(),
|
| 163 |
+
"lang": state.get("lang"),
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
return {
|
| 167 |
+
"action": "flow",
|
| 168 |
+
"answer": (
|
| 169 |
+
"✅ I saved your message, but email sending isn’t configured yet on the server.\n"
|
| 170 |
+
"Our team can still contact you using the details you provided."
|
| 171 |
+
),
|
| 172 |
+
"suggestions": default_suggestions(),
|
| 173 |
+
"lang": state.get("lang"),
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
# fallback
|
| 177 |
+
state["mode"] = "normal"
|
| 178 |
+
return {"action": "rag", "answer": "", "suggestions": default_suggestions(), "lang": state.get("lang")}
|
| 179 |
+
|
| 180 |
+
def _is_valid_email(self, s: str) -> bool:
|
| 181 |
+
return bool(re.match(r"^[^@\s]+@[^@\s]+\.[^@\s]+$", (s or "").strip()))
|
| 182 |
+
|
| 183 |
+
# ---------- Language flow (NO mapping) ----------
|
| 184 |
+
def _handle_language_flow(self, state: Dict, msg: str) -> Dict:
|
| 185 |
+
# Step 1: ask region
|
| 186 |
+
if state["mode"] == "lang_wait_region":
|
| 187 |
+
state["region"] = msg
|
| 188 |
+
state["mode"] = "lang_wait_language"
|
| 189 |
+
return {
|
| 190 |
+
"action": "flow",
|
| 191 |
+
"answer": "Thanks. What language would you like me to respond in? (Type it, e.g., Sinhala / Tamil / English)",
|
| 192 |
+
"suggestions": ["Sinhala", "Tamil", "English"],
|
| 193 |
+
"lang": state.get("lang"),
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
# Step 2: set language
|
| 197 |
+
if state["mode"] == "lang_wait_language":
|
| 198 |
+
chosen = (msg or "").strip()
|
| 199 |
+
if not chosen:
|
| 200 |
+
return {
|
| 201 |
+
"action": "flow",
|
| 202 |
+
"answer": "Please type the language you want (example: Sinhala / Tamil / English).",
|
| 203 |
+
"suggestions": ["Sinhala", "Tamil", "English"],
|
| 204 |
+
"lang": state.get("lang"),
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
state["lang"] = chosen
|
| 208 |
+
state["mode"] = "normal"
|
| 209 |
+
|
| 210 |
+
# If user wanted contact after language, jump into contact flow
|
| 211 |
+
next_after = state.pop("next_after_lang", None)
|
| 212 |
+
if next_after == "contact":
|
| 213 |
+
state["mode"] = "contact_wait_msg"
|
| 214 |
+
return {
|
| 215 |
+
"action": "flow",
|
| 216 |
+
"answer": f"✅ Done. I’ll reply in {state['lang']} from now on.\nNow, please type your message for our team.",
|
| 217 |
+
"suggestions": [],
|
| 218 |
+
"lang": state.get("lang"),
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
return {
|
| 222 |
+
"action": "flow",
|
| 223 |
+
"answer": f"✅ Done. I’ll reply in {state['lang']} from now on.",
|
| 224 |
+
"suggestions": default_suggestions(),
|
| 225 |
+
"lang": state.get("lang"),
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
# fallback
|
| 229 |
+
state["mode"] = "normal"
|
| 230 |
+
return {"action": "rag", "answer": "", "suggestions": default_suggestions(), "lang": state.get("lang")}
|