Penta hace 3 semanas
padre
commit
104efaf229
Se han modificado 1 ficheros con 12 adiciones y 14 borrados
  1. 12 14
      chatbot.py

+ 12 - 14
chatbot.py

@@ -96,6 +96,7 @@ class STTSink(Sink):
         self.user_ws = {}
         self.buffers = {}
         self.last_voice = {}
+        self.flush_tasks = {}   # 🔥 protection concurrence
 
     async def _get_ws(self, user_id):
         if user_id not in self.user_ws:
@@ -112,7 +113,7 @@ class STTSink(Sink):
                     text = data["text"].strip()
 
                     if self._ignore_text(text):
-                        return
+                        continue   # ✅ PAS return
 
                     if reply_mode == ReplyMode.TEXT and reply_text_channel:
                         await reply_text_channel.send(f"🗣️ {text}")
@@ -138,12 +139,10 @@ class STTSink(Sink):
 
         self.buffers[user_id].extend(audio)
 
-        # Durée buffer actuelle
         buffer_sec = len(self.buffers[user_id]) / (16000 * 2)
 
-        # Silence détecté → on flush
-        if buffer_sec >= 2.0:
-            asyncio.run_coroutine_threadsafe(
+        if buffer_sec >= 2.0 and user_id not in self.flush_tasks:
+            self.flush_tasks[user_id] = asyncio.run_coroutine_threadsafe(
                 self._flush_if_silence(user_id),
                 MAIN_LOOP
             )
@@ -151,15 +150,16 @@ class STTSink(Sink):
     async def _flush_if_silence(self, user_id):
         await asyncio.sleep(1.2)
 
-        last = self.last_voice.get(user_id, 0)
-        if time.time() - last < 1.2:
-            return  # toujours en train de parler
+        if time.time() - self.last_voice.get(user_id, 0) < 1.2:
+            self.flush_tasks.pop(user_id, None)
+            return
 
-        chunk = bytes(self.buffers[user_id])
+        chunk = bytes(self.buffers.get(user_id, b""))
         self.buffers[user_id].clear()
+        self.flush_tasks.pop(user_id, None)
 
         if len(chunk) < 16000 * 2 * 2:
-            return  # trop court
+            return
 
         ws = await self._get_ws(user_id)
         await ws.send(chunk)
@@ -176,10 +176,8 @@ class STTSink(Sink):
         ]
 
         t = text.lower()
-        return (
-            len(t) < 3
-            or any(b in t for b in BAD)
-        )
+        return len(t) < 3 or any(b in t for b in BAD)
+        
 
 # Liste pour stocker l'historique des conversations
 conversation_history = []