Spaces:
Running
Running
axeronis commited on
Commit ·
e6d0a09
1
Parent(s): d9f3520
feat: Smart escalation system with 4 new triggers, pulsing indicator, and attention badge
Browse files
backend/app/services/escalation_service.py
CHANGED
|
@@ -29,6 +29,10 @@ class EscalationReason(str, Enum):
|
|
| 29 |
HIGH_VALUE_DEAL = "high_value_deal"
|
| 30 |
RESPONSE_TIMEOUT = "response_timeout"
|
| 31 |
AI_REQUESTED = "ai_requested" # AI added [NEED_HUMAN] tag
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
class EscalationUrgency(str, Enum):
|
|
@@ -78,6 +82,34 @@ REPETITION_PHRASES = [
|
|
| 78 |
"I already asked", "I repeat", "again", "how many times"
|
| 79 |
]
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
class EscalationService:
|
| 83 |
"""
|
|
@@ -111,22 +143,32 @@ class EscalationService:
|
|
| 111 |
EscalationResult with escalation decision and details
|
| 112 |
"""
|
| 113 |
|
| 114 |
-
# 1. Check
|
| 115 |
-
result = self.
|
| 116 |
if result.should_escalate:
|
| 117 |
return result
|
| 118 |
|
| 119 |
-
# 2. Check negative sentiment (high priority)
|
| 120 |
result = self._check_negative_sentiment(user_message)
|
| 121 |
if result.should_escalate:
|
| 122 |
return result
|
| 123 |
|
| 124 |
-
# 3. Check for
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
result = self._check_repeated_questions(user_message, conversation_history)
|
| 126 |
if result.should_escalate:
|
| 127 |
return result
|
| 128 |
|
| 129 |
-
#
|
| 130 |
result = self._check_high_value_deal(heat_score, user_message, current_stage)
|
| 131 |
if result.should_escalate or result.notify_only:
|
| 132 |
return result
|
|
@@ -134,21 +176,83 @@ class EscalationService:
|
|
| 134 |
# No escalation needed
|
| 135 |
return EscalationResult(should_escalate=False)
|
| 136 |
|
| 137 |
-
def
|
| 138 |
-
"""Check if
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
|
|
|
|
|
|
| 141 |
for phrase in LOW_CONFIDENCE_PHRASES:
|
| 142 |
-
if phrase.lower() in ai_lower:
|
| 143 |
return EscalationResult(
|
| 144 |
should_escalate=True,
|
| 145 |
-
reason=EscalationReason.
|
| 146 |
-
urgency=EscalationUrgency.
|
| 147 |
-
details=f"AI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
)
|
| 149 |
|
| 150 |
return EscalationResult(should_escalate=False)
|
| 151 |
|
|
|
|
| 152 |
def _check_negative_sentiment(self, message: str) -> EscalationResult:
|
| 153 |
"""Check for negative sentiment in customer message."""
|
| 154 |
message_lower = message.lower()
|
|
@@ -294,12 +398,16 @@ class EscalationService:
|
|
| 294 |
}
|
| 295 |
|
| 296 |
reason_text = {
|
| 297 |
-
EscalationReason.NEGATIVE_SENTIMENT: "Негативный тон клиента",
|
| 298 |
-
EscalationReason.LOW_CONFIDENCE: "AI не уверен в ответе",
|
| 299 |
-
EscalationReason.REPEATED_QUESTIONS: "Клиент повторяет вопросы",
|
| 300 |
-
EscalationReason.HIGH_VALUE_DEAL: "Горячая сделка",
|
| 301 |
-
EscalationReason.RESPONSE_TIMEOUT: "Долгое ожидание ответа",
|
| 302 |
-
EscalationReason.AI_REQUESTED: "AI запросил помощь человека"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
}
|
| 304 |
|
| 305 |
# 1. WebSocket notification (in-app)
|
|
|
|
| 29 |
HIGH_VALUE_DEAL = "high_value_deal"
|
| 30 |
RESPONSE_TIMEOUT = "response_timeout"
|
| 31 |
AI_REQUESTED = "ai_requested" # AI added [NEED_HUMAN] tag
|
| 32 |
+
REQUEST_HUMAN = "request_human" # Client explicitly asks for human
|
| 33 |
+
NO_KNOWLEDGE = "no_knowledge" # AI has no info in knowledge base
|
| 34 |
+
COMPLEX_QUESTION = "complex_question" # Question too complex for AI
|
| 35 |
+
AI_PROMISED_NOTIFY = "ai_promised_notify" # AI promised to notify manager
|
| 36 |
|
| 37 |
|
| 38 |
class EscalationUrgency(str, Enum):
|
|
|
|
| 82 |
"I already asked", "I repeat", "again", "how many times"
|
| 83 |
]
|
| 84 |
|
| 85 |
+
# NEW: Phrases indicating client wants a human
|
| 86 |
+
REQUEST_HUMAN_PHRASES = [
|
| 87 |
+
"позовите человека", "свяжите с менеджером", "хочу с человеком",
|
| 88 |
+
"talk to human", "speak to manager", "real person", "живой человек",
|
| 89 |
+
"оператор", "настоящий человек", "не бот", "живого человека",
|
| 90 |
+
"human operator", "connect me to", "transfer me", "переключите на",
|
| 91 |
+
"хочу поговорить с", "соедините с", "перезвоните мне"
|
| 92 |
+
]
|
| 93 |
+
|
| 94 |
+
# NEW: AI tags indicating no knowledge or complex question
|
| 95 |
+
AI_NO_KNOWLEDGE_TAGS = [
|
| 96 |
+
"[NO_INFO]", "[НЕТ_ИНФО]", "[UNKNOWN]"
|
| 97 |
+
]
|
| 98 |
+
|
| 99 |
+
AI_COMPLEX_TAGS = [
|
| 100 |
+
"[COMPLEX]", "[СЛОЖНЫЙ]", "[EXPERT_NEEDED]"
|
| 101 |
+
]
|
| 102 |
+
|
| 103 |
+
# NEW: AI phrases indicating it will notify manager
|
| 104 |
+
AI_PROMISE_PHRASES = [
|
| 105 |
+
"[WILL_NOTIFY]", "[УВЕДОМЛЮ]",
|
| 106 |
+
"сообщу менеджеру", "сообщу руководству", "передам коллеге",
|
| 107 |
+
"сообщу специалисту", "проинформирую", "свяжусь с экспертом",
|
| 108 |
+
"уведомлю", "передам вашу просьбу", "сообщу мастеру",
|
| 109 |
+
"let my manager know", "inform my supervisor", "notify my colleague",
|
| 110 |
+
"I'll let", "I will inform", "I'll pass this on"
|
| 111 |
+
]
|
| 112 |
+
|
| 113 |
|
| 114 |
class EscalationService:
|
| 115 |
"""
|
|
|
|
| 143 |
EscalationResult with escalation decision and details
|
| 144 |
"""
|
| 145 |
|
| 146 |
+
# 1. Check if client explicitly requests human (highest priority)
|
| 147 |
+
result = self._check_request_human(user_message)
|
| 148 |
if result.should_escalate:
|
| 149 |
return result
|
| 150 |
|
| 151 |
+
# 2. Check for negative sentiment (high priority)
|
| 152 |
result = self._check_negative_sentiment(user_message)
|
| 153 |
if result.should_escalate:
|
| 154 |
return result
|
| 155 |
|
| 156 |
+
# 3. Check for AI-requested escalation tags
|
| 157 |
+
result = self._check_ai_tags(ai_response)
|
| 158 |
+
if result.should_escalate or result.notify_only:
|
| 159 |
+
return result
|
| 160 |
+
|
| 161 |
+
# 4. Check if AI promised to notify manager
|
| 162 |
+
result = self._check_ai_promised_notify(ai_response)
|
| 163 |
+
if result.notify_only:
|
| 164 |
+
return result
|
| 165 |
+
|
| 166 |
+
# 5. Check for repeated questions
|
| 167 |
result = self._check_repeated_questions(user_message, conversation_history)
|
| 168 |
if result.should_escalate:
|
| 169 |
return result
|
| 170 |
|
| 171 |
+
# 6. Check for high-value deal (notify only)
|
| 172 |
result = self._check_high_value_deal(heat_score, user_message, current_stage)
|
| 173 |
if result.should_escalate or result.notify_only:
|
| 174 |
return result
|
|
|
|
| 176 |
# No escalation needed
|
| 177 |
return EscalationResult(should_escalate=False)
|
| 178 |
|
| 179 |
+
def _check_request_human(self, message: str) -> EscalationResult:
|
| 180 |
+
"""Check if client explicitly requests to speak with a human."""
|
| 181 |
+
message_lower = message.lower()
|
| 182 |
+
|
| 183 |
+
for phrase in REQUEST_HUMAN_PHRASES:
|
| 184 |
+
if phrase.lower() in message_lower:
|
| 185 |
+
return EscalationResult(
|
| 186 |
+
should_escalate=True,
|
| 187 |
+
reason=EscalationReason.REQUEST_HUMAN,
|
| 188 |
+
urgency=EscalationUrgency.HIGH,
|
| 189 |
+
details=f"Клиент просит связать с человеком: '{phrase}'"
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
return EscalationResult(should_escalate=False)
|
| 193 |
+
|
| 194 |
+
def _check_ai_tags(self, ai_response: str) -> EscalationResult:
|
| 195 |
+
"""Check for AI escalation tags in response."""
|
| 196 |
+
|
| 197 |
+
# Check for [NO_INFO] tag - AI doesn't have information
|
| 198 |
+
for tag in AI_NO_KNOWLEDGE_TAGS:
|
| 199 |
+
if tag in ai_response:
|
| 200 |
+
return EscalationResult(
|
| 201 |
+
should_escalate=True,
|
| 202 |
+
reason=EscalationReason.NO_KNOWLEDGE,
|
| 203 |
+
urgency=EscalationUrgency.MEDIUM,
|
| 204 |
+
details="AI не нашёл информацию в базе знаний"
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
# Check for [COMPLEX] tag - question too complex
|
| 208 |
+
for tag in AI_COMPLEX_TAGS:
|
| 209 |
+
if tag in ai_response:
|
| 210 |
+
return EscalationResult(
|
| 211 |
+
should_escalate=True,
|
| 212 |
+
reason=EscalationReason.COMPLEX_QUESTION,
|
| 213 |
+
urgency=EscalationUrgency.MEDIUM,
|
| 214 |
+
details="Вопрос слишком сложен для AI"
|
| 215 |
+
)
|
| 216 |
+
|
| 217 |
+
# Check for [NEED_HUMAN] tag
|
| 218 |
+
if "[NEED_HUMAN]" in ai_response or "[НУЖЕН_ЧЕЛОВЕК]" in ai_response:
|
| 219 |
+
return EscalationResult(
|
| 220 |
+
should_escalate=True,
|
| 221 |
+
reason=EscalationReason.AI_REQUESTED,
|
| 222 |
+
urgency=EscalationUrgency.HIGH,
|
| 223 |
+
details="AI явно запросил помощь человека"
|
| 224 |
+
)
|
| 225 |
|
| 226 |
+
# Check for low confidence phrases
|
| 227 |
+
ai_lower = ai_response.lower()
|
| 228 |
for phrase in LOW_CONFIDENCE_PHRASES:
|
| 229 |
+
if phrase.lower() in ai_lower and "[" not in phrase:
|
| 230 |
return EscalationResult(
|
| 231 |
should_escalate=True,
|
| 232 |
+
reason=EscalationReason.LOW_CONFIDENCE,
|
| 233 |
+
urgency=EscalationUrgency.MEDIUM,
|
| 234 |
+
details=f"AI не уверен в ответе: '{phrase}'"
|
| 235 |
+
)
|
| 236 |
+
|
| 237 |
+
return EscalationResult(should_escalate=False)
|
| 238 |
+
|
| 239 |
+
def _check_ai_promised_notify(self, ai_response: str) -> EscalationResult:
|
| 240 |
+
"""Check if AI promised to notify manager/specialist."""
|
| 241 |
+
ai_lower = ai_response.lower()
|
| 242 |
+
|
| 243 |
+
for phrase in AI_PROMISE_PHRASES:
|
| 244 |
+
if phrase.lower() in ai_lower:
|
| 245 |
+
return EscalationResult(
|
| 246 |
+
should_escalate=False,
|
| 247 |
+
notify_only=True,
|
| 248 |
+
reason=EscalationReason.AI_PROMISED_NOTIFY,
|
| 249 |
+
urgency=EscalationUrgency.LOW,
|
| 250 |
+
details=f"AI обещал сообщить: '{phrase}'"
|
| 251 |
)
|
| 252 |
|
| 253 |
return EscalationResult(should_escalate=False)
|
| 254 |
|
| 255 |
+
|
| 256 |
def _check_negative_sentiment(self, message: str) -> EscalationResult:
|
| 257 |
"""Check for negative sentiment in customer message."""
|
| 258 |
message_lower = message.lower()
|
|
|
|
| 398 |
}
|
| 399 |
|
| 400 |
reason_text = {
|
| 401 |
+
EscalationReason.NEGATIVE_SENTIMENT: "😠 Негативный тон клиента",
|
| 402 |
+
EscalationReason.LOW_CONFIDENCE: "🤔 AI не уверен в ответе",
|
| 403 |
+
EscalationReason.REPEATED_QUESTIONS: "🔁 Клиент повторяет вопросы",
|
| 404 |
+
EscalationReason.HIGH_VALUE_DEAL: "🔥 Горячая сделка",
|
| 405 |
+
EscalationReason.RESPONSE_TIMEOUT: "⏱️ Долгое ожидание ответа",
|
| 406 |
+
EscalationReason.AI_REQUESTED: "🆘 AI запросил помощь человека",
|
| 407 |
+
EscalationReason.REQUEST_HUMAN: "🧑💼 Клиент просит связать с человеком",
|
| 408 |
+
EscalationReason.NO_KNOWLEDGE: "❓ AI не нашёл информацию в базе",
|
| 409 |
+
EscalationReason.COMPLEX_QUESTION: "🧠 Слишком сложный вопрос для AI",
|
| 410 |
+
EscalationReason.AI_PROMISED_NOTIFY: "📢 AI обещал сообщить менеджеру"
|
| 411 |
}
|
| 412 |
|
| 413 |
# 1. WebSocket notification (in-app)
|
frontend/components/ChatWindow/ChatHeader.tsx
CHANGED
|
@@ -62,6 +62,11 @@ export function ChatHeader({
|
|
| 62 |
<h2 className="font-bold text-white text-sm">{lead?.name || 'Лид'}</h2>
|
| 63 |
<div className="flex items-center gap-2">
|
| 64 |
<span className="text-xs text-slate-400 font-medium">{(lead as any)?.channel_source || 'Неизвестно'}</span>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
{isAnalyzing && (
|
| 66 |
<span className="flex items-center gap-1 text-[10px] text-indigo-600 bg-indigo-50 px-2 py-0.5 rounded-full animate-pulse border border-indigo-100 font-semibold">
|
| 67 |
<Sparkles className="w-3 h-3" />
|
|
|
|
| 62 |
<h2 className="font-bold text-white text-sm">{lead?.name || 'Лид'}</h2>
|
| 63 |
<div className="flex items-center gap-2">
|
| 64 |
<span className="text-xs text-slate-400 font-medium">{(lead as any)?.channel_source || 'Неизвестно'}</span>
|
| 65 |
+
{lead?.needs_attention && (
|
| 66 |
+
<span className="flex items-center gap-1 text-[10px] text-red-400 bg-red-500/20 px-2 py-0.5 rounded-full animate-pulse border border-red-500/30 font-semibold">
|
| 67 |
+
🚨 Внимание
|
| 68 |
+
</span>
|
| 69 |
+
)}
|
| 70 |
{isAnalyzing && (
|
| 71 |
<span className="flex items-center gap-1 text-[10px] text-indigo-600 bg-indigo-50 px-2 py-0.5 rounded-full animate-pulse border border-indigo-100 font-semibold">
|
| 72 |
<Sparkles className="w-3 h-3" />
|
frontend/components/LeadsList.tsx
CHANGED
|
@@ -272,19 +272,15 @@ export function LeadsList({
|
|
| 272 |
<div className="w-10 h-10 rounded-full bg-gradient-to-br from-indigo-500 to-purple-600 flex items-center justify-center text-white font-bold text-sm shadow-inner group-hover/avatar:scale-105 transition-transform">
|
| 273 |
{lead.name?.charAt(0) || '?'}
|
| 274 |
</div>
|
| 275 |
-
{/* Online/Status Indicator */}
|
| 276 |
-
<div className={cn(
|
| 277 |
-
"absolute bottom-0 right-0 w-3 h-3 rounded-full border-2 border-[#0D0E18]",
|
| 278 |
-
lead.heat_level === 'HOT' ? "bg-red-500" :
|
| 279 |
-
lead.heat_level === 'WARM' ? "bg-orange-500" : "bg-green-500"
|
| 280 |
-
)} />
|
| 281 |
|
| 282 |
-
{/* Heat Level Indicator
|
| 283 |
<div className={cn(
|
| 284 |
-
"absolute -top-0.5 -right-0.5 w-3 h-3 rounded-full border-2 border-[#0D0E18]",
|
| 285 |
(!lead.heat_level || lead.heat_level === 'COLD') ? "bg-blue-500" :
|
| 286 |
lead.heat_level === 'WARM' ? "bg-orange-500" :
|
| 287 |
-
"bg-red-500" // HOT
|
|
|
|
|
|
|
| 288 |
)} />
|
| 289 |
</div>
|
| 290 |
|
|
|
|
| 272 |
<div className="w-10 h-10 rounded-full bg-gradient-to-br from-indigo-500 to-purple-600 flex items-center justify-center text-white font-bold text-sm shadow-inner group-hover/avatar:scale-105 transition-transform">
|
| 273 |
{lead.name?.charAt(0) || '?'}
|
| 274 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
|
| 276 |
+
{/* Heat Level Indicator with Escalation Pulse */}
|
| 277 |
<div className={cn(
|
| 278 |
+
"absolute -top-0.5 -right-0.5 w-3.5 h-3.5 rounded-full border-2 border-[#0D0E18] transition-all",
|
| 279 |
(!lead.heat_level || lead.heat_level === 'COLD') ? "bg-blue-500" :
|
| 280 |
lead.heat_level === 'WARM' ? "bg-orange-500" :
|
| 281 |
+
"bg-red-500", // HOT
|
| 282 |
+
// Pulsing ring when needs attention
|
| 283 |
+
lead.needs_attention && "animate-pulse ring-2 ring-red-500/60 ring-offset-1 ring-offset-[#0D0E18]"
|
| 284 |
)} />
|
| 285 |
</div>
|
| 286 |
|
frontend/types/index.ts
CHANGED
|
@@ -72,6 +72,10 @@ export interface Lead {
|
|
| 72 |
created_at: string
|
| 73 |
current_stage_id?: string | null
|
| 74 |
last_message?: string | null // Last message preview for chat list
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
}
|
| 76 |
|
| 77 |
export interface Message {
|
|
|
|
| 72 |
created_at: string
|
| 73 |
current_stage_id?: string | null
|
| 74 |
last_message?: string | null // Last message preview for chat list
|
| 75 |
+
// Escalation
|
| 76 |
+
needs_attention?: boolean
|
| 77 |
+
escalation_reason?: string | null
|
| 78 |
+
escalation_at?: string | null
|
| 79 |
}
|
| 80 |
|
| 81 |
export interface Message {
|