فهرست منبع

[3.0.0] Sink adjustments

Penta 3 هفته پیش
والد
کامیت
fa1f36c4d9
1فایلهای تغییر یافته به همراه13 افزوده شده و 8 حذف شده
  1. 13 8
      chatbot.py

+ 13 - 8
chatbot.py

@@ -96,7 +96,7 @@ class STTSink(Sink):
         self.user_ws = {}
         self.buffers = {}
         self.last_voice = {}
-        self.flush_tasks = {}   # 🔥 protection concurrence
+        self.flush_tasks = {}
 
     async def _get_ws(self, user_id):
         if user_id not in self.user_ws:
@@ -141,7 +141,7 @@ class STTSink(Sink):
 
         buffer_sec = len(self.buffers[user_id]) / (16000 * 2)
 
-        if buffer_sec >= 2.0 and user_id not in self.flush_tasks:
+        if buffer_sec >= 1.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
@@ -150,21 +150,26 @@ class STTSink(Sink):
     async def _flush_if_silence(self, user_id):
         await asyncio.sleep(1.2)
 
-        if time.time() - self.last_voice.get(user_id, 0) < 1.2:
+        if time.time() - self.last_voice.get(user_id, 0) < 0.6:
             self.flush_tasks.pop(user_id, None)
             return
 
         chunk = bytes(self.buffers.get(user_id, b""))
-        self.buffers[user_id].clear()
+
+        self.buffers[user_id] = bytearray()
         self.flush_tasks.pop(user_id, None)
 
         if len(chunk) < 16000 * 2 * 2:
+            self.buffers[user_id].extend(chunk)
             return
 
-        ws = await self._get_ws(user_id)
-        await ws.send(chunk)
-
-        logger.debug(f"[STT] chunk envoyé user={user_id} bytes={len(chunk)}")
+        try:
+            ws = await self._get_ws(user_id)
+            await ws.send(chunk)
+            logger.debug(f"[STT] chunk envoyé user={user_id} bytes={len(chunk)}")
+        except Exception as e:
+            logger.warning(f"[STT] envoi échoué user={user_id}: {e}")
+            self.user_ws.pop(user_id, None)
 
     def _ignore_text(self, text: str) -> bool:
         BAD = [