Spaces:
Running

axeronis commited on
Commit
6df5db6
·
1 Parent(s): 81b3ca5

feat: track LATEST voice preference - allow re-enabling voice after disabling

Browse files
backend/app/api/webhook.py CHANGED
@@ -1267,26 +1267,13 @@ async def process_ai_response_background(
1267
  # AI can add [SEND_VOICE] tag when it detects persuasion/trust moment
1268
  ai_wants_voice = "[SEND_VOICE]" in ai_response["text"]
1269
 
1270
- # === CHECK IF USER REQUESTED NO VOICE ===
1271
- # First use smart LLM-based detection from response_generator (if available)
 
1272
  user_requested_no_voice = ai_response.get("user_requested_no_voice", False)
1273
 
1274
- # Fallback: keyword check if LLM check didn't run
1275
- if not user_requested_no_voice:
1276
- no_voice_keywords = [
1277
- 'не присылай', 'не отправляй', 'не шли', 'не надо голос', 'не голос',
1278
- 'без голосов', 'только текст', 'пиши текст', 'текстом', 'зачем голос',
1279
- 'no voice', 'text only', 'хватит голос', 'голосовые не нужны'
1280
- ]
1281
- for msg in conversation_history:
1282
- if getattr(msg, 'sender_type', None) == 'user':
1283
- msg_text = getattr(msg, 'content', '') or ''
1284
- if any(kw in msg_text.lower() for kw in no_voice_keywords):
1285
- user_requested_no_voice = True
1286
- logger.info(f"🔇 USER REQUESTED NO VOICE (keyword fallback): {msg_text[:50]}")
1287
- break
1288
- else:
1289
- logger.info("🔇 USER REQUESTED NO VOICE (smart LLM detection)")
1290
 
1291
  # If user requested no voice, disable all voice triggers
1292
  if user_requested_no_voice:
 
1267
  # AI can add [SEND_VOICE] tag when it detects persuasion/trust moment
1268
  ai_wants_voice = "[SEND_VOICE]" in ai_response["text"]
1269
 
1270
+ # === CHECK USER'S LATEST VOICE PREFERENCE ===
1271
+ # Use smart detection from response_generator which tracks LATEST preference
1272
+ # (handles both "no voice" and "yes voice" requests, using the most recent one)
1273
  user_requested_no_voice = ai_response.get("user_requested_no_voice", False)
1274
 
1275
+ if user_requested_no_voice:
1276
+ logger.info("🔇 USER REQUESTED NO VOICE (latest preference from response_generator)")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1277
 
1278
  # If user requested no voice, disable all voice triggers
1279
  if user_requested_no_voice:
backend/app/services/ai_core/response_generator.py CHANGED
@@ -90,49 +90,49 @@ async def generate_sales_response_impl(
90
 
91
  ai_message_count = len([m for m in conversation_history if get_sender_type(m) == 'ai']) if conversation_history else 0
92
 
93
- # DETECT IF USER REQUESTED NO VOICE MESSAGES
94
- # Using DUAL approach: fast keyword check + smart LLM check
95
  user_requested_no_voice = False
96
- user_messages = [get_content(m) for m in (conversation_history or []) if get_sender_type(m) == 'user']
97
 
98
  # DEBUG: Log what we're checking
99
- logger.info(f"🔍 VOICE CHECK: {len(user_messages)} user messages in history")
100
- for i, msg in enumerate(user_messages[-5:]): # Log last 5
101
- logger.info(f" [{i}] {msg[:80]}...")
102
 
103
- # 1. FAST CHECK: Common phrases (instant, no API call)
104
  no_voice_keywords = [
105
  'не присылай', 'не отправляй', 'не шли', 'не надо голос', 'не голос',
106
  'без голосов', 'только текст', 'пиши текст', 'текстом', 'зачем голос',
107
  'no voice', 'text only', 'stop voice', 'хватит голос', 'голосовые не нужны'
108
  ]
109
- for msg in user_messages:
 
 
 
 
 
 
 
 
 
110
  msg_lower = msg.lower()
 
111
  if any(kw in msg_lower for kw in no_voice_keywords):
112
- user_requested_no_voice = True
113
- logger.info(f"🔇 FAST CHECK: User requested NO VOICE: {msg[:50]}")
114
- break
 
 
 
 
 
 
 
115
 
116
- # 2. SMART CHECK: Use LLM only if fast check didn't trigger (saves API calls)
117
- if not user_requested_no_voice and user_messages:
118
- user_context = "\n".join([f"- {msg}" for msg in user_messages[-10:]])
119
- try:
120
- voice_check = await ai_core.llm.generate_response(
121
- prompt=f"""Did the user express ANY preference against voice/audio messages in these messages?
122
-
123
- User messages:
124
- {user_context}
125
-
126
- Answer ONLY: YES or NO""",
127
- system_instruction="Output only YES or NO. YES if user dislikes voice messages.",
128
- context=None,
129
- chat_history=[]
130
- )
131
- if "YES" in voice_check["text"].upper():
132
- user_requested_no_voice = True
133
- logger.info(f"🔇 LLM CHECK: User prefers NO VOICE")
134
- except Exception as e:
135
- logger.warning(f"LLM voice check failed: {e}")
136
 
137
  # 4. DYNAMIC CONTEXT (Things that change per request)
138
  dynamic_context = f"""
 
90
 
91
  ai_message_count = len([m for m in conversation_history if get_sender_type(m) == 'ai']) if conversation_history else 0
92
 
93
+ # DETECT USER'S LATEST VOICE PREFERENCE
94
+ # Check for both "no voice" and "yes voice" requests, use the LATEST one
95
  user_requested_no_voice = False
96
+ user_messages_with_idx = [(i, get_content(m)) for i, m in enumerate(conversation_history or []) if get_sender_type(m) == 'user']
97
 
98
  # DEBUG: Log what we're checking
99
+ logger.info(f"🔍 VOICE CHECK: {len(user_messages_with_idx)} user messages in history")
100
+ for _, msg in user_messages_with_idx[-5:]: # Log last 5
101
+ logger.info(f" {msg[:80]}...")
102
 
103
+ # Keywords for preferences
104
  no_voice_keywords = [
105
  'не присылай', 'не отправляй', 'не шли', 'не надо голос', 'не голос',
106
  'без голосов', 'только текст', 'пиши текст', 'текстом', 'зачем голос',
107
  'no voice', 'text only', 'stop voice', 'хватит голос', 'голосовые не нужны'
108
  ]
109
+ yes_voice_keywords = [
110
+ 'отправь голос', 'давай голос', 'голосом', 'голосовыми', 'хочу голос',
111
+ 'send voice', 'use voice', 'говори', 'скажи голосом'
112
+ ]
113
+
114
+ # Find the LATEST preference (scan from newest to oldest)
115
+ last_voice_preference = None # None = no preference, 'no' = no voice, 'yes' = want voice
116
+ last_pref_idx = -1
117
+
118
+ for idx, msg in user_messages_with_idx:
119
  msg_lower = msg.lower()
120
+ # Check for no-voice
121
  if any(kw in msg_lower for kw in no_voice_keywords):
122
+ if idx > last_pref_idx:
123
+ last_voice_preference = 'no'
124
+ last_pref_idx = idx
125
+ logger.info(f"🔇 Found NO VOICE preference at idx {idx}: {msg[:50]}")
126
+ # Check for yes-voice
127
+ if any(kw in msg_lower for kw in yes_voice_keywords):
128
+ if idx > last_pref_idx:
129
+ last_voice_preference = 'yes'
130
+ last_pref_idx = idx
131
+ logger.info(f"🔊 Found YES VOICE preference at idx {idx}: {msg[:50]}")
132
 
133
+ user_requested_no_voice = (last_voice_preference == 'no')
134
+ if last_voice_preference:
135
+ logger.info(f"📢 LATEST voice preference: {last_voice_preference.upper()}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
 
137
  # 4. DYNAMIC CONTEXT (Things that change per request)
138
  dynamic_context = f"""