Spaces:
Running

axeronis commited on
Commit
f673563
·
1 Parent(s): cf62886

fix: reduce voice frequency - only critical moments, max 1 per 5 messages

Browse files
Files changed (1) hide show
  1. backend/app/api/webhook.py +27 -36
backend/app/api/webhook.py CHANGED
@@ -1286,60 +1286,51 @@ async def process_ai_response_background(
1286
  user_text_lower = user_text.lower() if user_text else ""
1287
  ai_text_lower = ai_response["text"].lower()
1288
 
1289
- if not ai_wants_voice and tenant.voice_enabled and not user_requested_no_voice:
1290
- # 1. FIRST MESSAGE - greeting
 
 
 
 
 
 
 
 
 
 
 
1291
  if is_first_message:
1292
  voice_trigger_reason = "FIRST_MESSAGE"
1293
  ai_wants_voice = True
1294
 
1295
- # 2. CLIENT DOUBTS/OBJECTIONS
1296
  elif any(word in user_text_lower for word in [
1297
- "дорого", "дороговато", "подумаю", "не уверен", "не знаю",
1298
- "сомневаюсь", "боюсь", "а вдруг", "не получится", "страшно",
1299
- "надо подумать", "посмотрим", "может быть", "наверное нет"
1300
- ]):
1301
- voice_trigger_reason = "CLIENT_DOUBTS"
1302
- ai_wants_voice = True
1303
-
1304
- # 3. COMPLEX EXPLANATION (AI response > 300 chars or 3+ sentences)
1305
- elif len(ai_response["text"]) > 300 or ai_response["text"].count('.') >= 3:
1306
- voice_trigger_reason = "COMPLEX_EXPLANATION"
1307
- ai_wants_voice = True
1308
-
1309
- # 4. APOLOGY in AI response
1310
- elif any(word in ai_text_lower for word in [
1311
- "простите", "извините", "прошу прощения", "сожалею",
1312
- "приношу извинения", "моя ошибка"
1313
- ]):
1314
- voice_trigger_reason = "APOLOGY"
1315
- ai_wants_voice = True
1316
-
1317
- # 5. IMPORTANT PROMISE in AI response
1318
- elif any(word in ai_text_lower for word in [
1319
- "обещаю", "гарантирую", "лично прослежу", "обязательно",
1320
- "100%", "точно сделаю", "даю слово"
1321
  ]):
1322
- voice_trigger_reason = "PROMISE"
1323
  ai_wants_voice = True
1324
 
1325
- # 6. CLIENT NEGATIVE EMOTIONS
1326
  elif any(word in user_text_lower for word in [
1327
- "плохо", "ужасно", "устал", "надоело", "разочарован",
1328
- "расстроен", "зол", "бесит", "не работает", "сломалось"
1329
  ]):
1330
  voice_trigger_reason = "CLIENT_UPSET"
1331
  ai_wants_voice = True
1332
 
1333
- # 7. DEAL CLOSING signals
1334
  elif any(word in user_text_lower for word in [
1335
- "беру", "оформляю", "покупаю", "давайте", "согласен",
1336
- "оплачу", "записывайте", "бронирую", "заказываю"
1337
- ]) or any(word in ai_text_lower for word in [
1338
- "оформляем", "отлично! записываю", "принято", "заказ создан"
1339
  ]):
1340
  voice_trigger_reason = "DEAL_CLOSING"
1341
  ai_wants_voice = True
1342
 
 
 
 
 
1343
  if voice_trigger_reason:
1344
  logger.info(f"🎙️ Smart voice trigger: {voice_trigger_reason}")
1345
 
 
1286
  user_text_lower = user_text.lower() if user_text else ""
1287
  ai_text_lower = ai_response["text"].lower()
1288
 
1289
+ # Count recent voice messages to limit frequency
1290
+ recent_ai_voice_count = 0
1291
+ for msg in conversation_history[-10:]: # Last 10 messages
1292
+ if getattr(msg, 'sender_type', None) == 'ai':
1293
+ # Check if this AI message was a voice (has media)
1294
+ if getattr(msg, 'media_type', None) == 'voice' or getattr(msg, 'media_url', None):
1295
+ recent_ai_voice_count += 1
1296
+
1297
+ # FREQUENCY LIMIT: Only 1 voice per 5+ messages
1298
+ voice_allowed_by_frequency = recent_ai_voice_count < 1 or ai_message_count % 5 == 0
1299
+
1300
+ if not ai_wants_voice and tenant.voice_enabled and not user_requested_no_voice and voice_allowed_by_frequency:
1301
+ # 1. FIRST MESSAGE - greeting (critical for trust)
1302
  if is_first_message:
1303
  voice_trigger_reason = "FIRST_MESSAGE"
1304
  ai_wants_voice = True
1305
 
1306
+ # 2. CLIENT SERIOUS OBJECTIONS (need to persuade)
1307
  elif any(word in user_text_lower for word in [
1308
+ "дорого", "дороговато", "сомневаюсь", "боюсь",
1309
+ "не получится", "не уверен", "трудно сказать"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1310
  ]):
1311
+ voice_trigger_reason = "CLIENT_OBJECTION"
1312
  ai_wants_voice = True
1313
 
1314
+ # 3. CLIENT UPSET (need empathy)
1315
  elif any(word in user_text_lower for word in [
1316
+ "плохо", "ужасно", "разочарован", "расстроен",
1317
+ "бесит", "не работает", "сломалось"
1318
  ]):
1319
  voice_trigger_reason = "CLIENT_UPSET"
1320
  ai_wants_voice = True
1321
 
1322
+ # 4. DEAL CLOSING (celebration moment)
1323
  elif any(word in user_text_lower for word in [
1324
+ "беру", "оформляю", "покупаю", "согласен",
1325
+ "оплачу", "записывайте", "бронирую"
 
 
1326
  ]):
1327
  voice_trigger_reason = "DEAL_CLOSING"
1328
  ai_wants_voice = True
1329
 
1330
+ # Log if voice was blocked by frequency
1331
+ if not voice_allowed_by_frequency and not user_requested_no_voice:
1332
+ logger.info(f"🔇 Voice blocked by frequency limit (recent voice count: {recent_ai_voice_count})")
1333
+
1334
  if voice_trigger_reason:
1335
  logger.info(f"🎙️ Smart voice trigger: {voice_trigger_reason}")
1336