优化长对话与单对话的LED显示,修复对话异常问题,增加录音开始提示音
authorTimekeeperPL <1624952103@qq.com>
Thu, 21 May 2026 05:57:22 +0000 (13:57 +0800)
committerTimekeeperPL <1624952103@qq.com>
Thu, 21 May 2026 05:57:22 +0000 (13:57 +0800)
2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_buttons.py
2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_fsm.py
2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_led.py
2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_session.py
2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_session_io.py
2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_session_media.py

index bf7ac80..e99ba05 100644 (file)
@@ -25,7 +25,7 @@ try:
 except Exception:\r
     vc_config = None\r
 from vc_common import dev_type\r
-from vc_hw import set_ipv4_datacall\r
+from vc_hw import set_ipv4_datacall, _BatteryCfg\r
 from vc_uart import AI_UART\r
 \r
 audio_buf = ['chaobingfatis', 'chenggong', 'didianguanji', 'IMEIhao', 'jianquancuowu', 'key_prompt', 'liuliangtishi', 'Low_power_warning', 'R_CHARGE_FULL', 'R_CHARGE_IN', 'R_CHARGE_OUT', 'R_READ_TIME_0', 'R_READ_TIME_1', 'R_READ_TIME_2', 'R_READ_TIME_3', 'R_READ_TIME_4', 'R_READ_TIME_5', 'R_READ_TIME_6', 'R_READ_TIME_7', 'R_READ_TIME_8', 'R_READ_TIME_9', 'R_READ_TIME_A', 'R_READ_TIME_B', 'R_READ_TIME_C', 'R_READ_TIME_D', 'R_READ_TIME_E', 'R_READ_TIME_F', 'R_READ_TIME_G', 'R_READ_TIME_H', 'R_READ_TIME_I', 'R_READ_TIME_J', 'R_READ_TIME_K', 'R_READ_TIME_L', 'R_READ_TIME_M', 'R_READ_TIME_N', 'R_READ_TIME_O', 'R_READ_TIME_P', 'R_READ_TIME_Q', 'R_READ_TIME_R', 'R_READ_TIME_S', 'R_READ_TIME_T', 'R_READ_TIME_U', 'R_READ_TIME_V', 'R_READ_TIME_W', 'R_READ_TIME_X', 'R_READ_TIME_Y', 'R_READ_TIME_Z', 'send_tone', 'shouhou', 'xinhaoruo1', 'zhengzaiguanji']\r
@@ -652,6 +652,7 @@ class AI_dev(Abstract):
                 self.KeyAI_long_press = 0\r
                 self.KeyAI_timer.start(100, 1, self.KeyAI_timer_cb)\r
                 self.KeyAI_timer_count = 0\r
+                EventMesh.publish("vc_long_dialogue_key_down")\r
                 EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_key_press})\r
         except Exception as e:\r
             usys.print_exception(e)\r
@@ -702,6 +703,7 @@ class AI_dev(Abstract):
                     # if dev_type == "7PIN":\r
                         # self.aud.aud_tone_play(10,100)#播放按键音\r
                         \r
+                    EventMesh.publish("vc_long_dialogue_key_down")\r
                     EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_key_press})  \r
         except Exception as e: \r
             usys.print_exception(e)\r
index 4ac2468..61d9937 100644 (file)
@@ -138,11 +138,10 @@ class VCStateMachine:
     def on_server_started(self):
         self.reset_rx_phases()
         self.session = VCSessionState.Started
-        # 新会话 started:确保进入 Listening,且清理上轮播放门控
+        # 新会话 started:一律回到 Listening(待机再按键时 turn 可能仍停在 Thinking/Playing)
         self.await_played = False
         self.last_tts_completed_ms = 0
-        if self.turn in (VCTurnState.Idle, VCTurnState.AwaitPlayedAck):
-            self.turn = VCTurnState.Listening
+        self.turn = VCTurnState.Listening
         self.log("server.started")
 
     def on_server_vad_silence(self):
index bea629c..f204dd3 100644 (file)
@@ -50,6 +50,7 @@ class AI_led(Abstract):
         self._period_ms = 1000\r
         self._gb_on_ms = 1000\r
         self._gb_off_ms = 1000\r
+        self._prefer_off = 0\r
 \r
         EventMesh.subscribe("set_led_state", self.set_led_state)\r
         EventMesh.subscribe("get_led_debug_state", self.get_led_debug_state)\r
@@ -139,9 +140,29 @@ class AI_led(Abstract):
         self._set_rgb(0, 1, 0)\r
         self._timer.start(self._gb_on_ms, 0, self._timer_cb)\r
 \r
+    def _listening_blink_period_ms(self):\r
+        """单对话 PTT 250ms 快闪;长对话 1000ms 慢闪。优先运行时 VC_mode。"""\r
+        try:\r
+            if EventMesh.publish("get_vc_mode") == 1:\r
+                return 250\r
+        except Exception:\r
+            pass\r
+        try:\r
+            if EventMesh.publish("get_storage_config", "VC_mode") == 1:\r
+                return 250\r
+        except Exception:\r
+            pass\r
+        return 1000\r
+\r
     def _set_blue_blink(self, period_ms=1000):\r
+        period_ms = int(period_ms or 1000)\r
+        try:\r
+            if self._mode == "blue_blink" and int(self._period_ms) == period_ms:\r
+                return\r
+        except Exception:\r
+            pass\r
         self._mode = "blue_blink"\r
-        self._period_ms = period_ms or 1000\r
+        self._period_ms = period_ms\r
         self._blink_on = 1\r
         self._timer_stop()\r
         self._set_rgb(0, 0, 1)\r
@@ -224,6 +245,10 @@ class AI_led(Abstract):
         if self._mode == "blue_blink":\r
             self._blink_on = 0 if self._blink_on else 1\r
             self._set_rgb(0, 0, 1 if self._blink_on else 0)\r
+            try:\r
+                self._timer.start(self._period_ms, 1, self._timer_cb)\r
+            except Exception:\r
+                pass\r
             return\r
         if self._mode == "red_blink":\r
             self._blink_on = 0 if self._blink_on else 1\r
@@ -293,13 +318,29 @@ class AI_led(Abstract):
                     if aval in (None, "", "none", "clear"):\r
                         self._st_alert = None\r
                 if "ai_state" in msg:\r
-                    self._st_ai = msg.get("ai_state") or "idle"\r
+                    new_ai = msg.get("ai_state") or "idle"\r
+                    try:\r
+                        if EventMesh.publish("get_ptt_should_force_idle_led") == 1:\r
+                            if new_ai != "idle":\r
+                                if self._led_state_debug_enabled():\r
+                                    print("[LED] coerce ai={} -> idle (ptt_stop_latch)".format(new_ai))\r
+                                new_ai = "idle"\r
+                        elif EventMesh.publish("get_vc_hold_listening_led") == 1:\r
+                            if new_ai != "listening":\r
+                                if self._led_state_debug_enabled():\r
+                                    print("[LED] coerce ai={} -> listening (listen_hold)".format(new_ai))\r
+                                new_ai = "listening"\r
+                    except Exception:\r
+                        pass\r
+                    self._st_ai = new_ai\r
                 if "charging" in msg:\r
                     self._st_charging = msg.get("charging") or "none"\r
                 if "battery" in msg:\r
                     self._st_battery = msg.get("battery") or "normal"\r
                 if "signal" in msg:\r
                     self._st_signal = msg.get("signal") or "normal"\r
+                if "prefer_off" in msg:\r
+                    self._prefer_off = 1 if msg.get("prefer_off") else 0\r
         except Exception:\r
             pass\r
         if self._mode in ("alert_rb", "red_flash_n"):\r
@@ -361,13 +402,7 @@ class AI_led(Abstract):
                 return\r
             if self._st_ai == "listening":\r
                 # 长对话蓝慢闪(≈1s);单对话 PTT 蓝快闪(≈2次/s,REQ-AI-07)\r
-                period_ms = 1000\r
-                try:\r
-                    if EventMesh.publish("get_storage_config", "VC_mode") == 1:\r
-                        period_ms = 250\r
-                except Exception:\r
-                    pass\r
-                self._set_blue_blink(period_ms)\r
+                self._set_blue_blink(self._listening_blink_period_ms())\r
                 return\r
         except Exception:\r
             pass\r
@@ -384,6 +419,14 @@ class AI_led(Abstract):
                     _skip_charge_led = 1\r
             except Exception:\r
                 pass\r
+        # 模式切换/提示音待机:ai=idle 时优先灭灯,勿被 charging=full 显示成「绿灯常亮」\r
+        try:\r
+            if self._st_ai == "idle" and getattr(self, "_prefer_off", 0):\r
+                self._prefer_off = 0\r
+                self._set_off()\r
+                return\r
+        except Exception:\r
+            pass\r
         # 基础态:充电/电量/信号\r
         try:\r
             if not _skip_charge_led:\r
index c8cf361..6f640c1 100644 (file)
@@ -18,16 +18,241 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
             )
         except Exception:
             return "fsm=?"
-    def _vc_capture_allowed(self, reason=""):
-        """采集/上行开麦前:下行播放或本地短音未结束则禁止。"""
+    # -------------------------------------------------------------------------
+    # 对话模式分界(VC_mode:0=长对话连续/VAD,1=单对话 PTT)
+    # 约定:带 _ptt_ 前缀的状态/逻辑仅单对话;带 _long_dialogue_ 仅长对话;公共路径须先判 VC_mode。
+    # -------------------------------------------------------------------------
+    def _vc_is_ptt_mode(self):
         try:
-            if self._vc_tts_pipeline_busy():
+            return int(getattr(self, "VC_mode", -1)) == 1
+        except Exception:
+            return False
+
+    def _vc_is_long_dialogue_mode(self):
+        try:
+            return int(getattr(self, "VC_mode", -1)) == 0
+        except Exception:
+            return False
+
+    def _vc_reset_dialogue_mode_state(self):
+        """Close/模式切换时清零双模式按键与灯锁,避免残留互相影响。"""
+        try:
+            self._ptt_idle_latch = 0
+            self._ptt_short_press_stop = 0
+            self._ptt_interrupt_active = 0
+            self._ptt_led_lock_listening = 0
+            self._vc_play_end_suppress_idle_led = 0
+            self._ptt_pending_arm = 0
+            self._vc_serial_canceling = 0
+            self._vc_cancel_deadline_ms = 0
+            self._ptt_force_record_on_started = 0
+            self._ptt_await_server = 0
+            self._ptt_audio_created = 0
+        except Exception:
+            pass
+        try:
+            self._long_dialogue_key_arm = 0
+            self._long_dialogue_interrupt_settled_ms = 0
+            self._long_dialogue_end_interrupt()
+        except Exception:
+            pass
+        try:
+            self._drop_tts_audio = 0
+            self.is_ruppt_tts = 0
+            self._vc_last_interrupt_uplink_ms = 0
+        except Exception:
+            pass
+
+    def _vc_should_hold_listening_led(self):
+        """当前模式是否应强制保持 listening 灯(不混用另一模式的锁标志)。"""
+        if self._vc_is_ptt_mode():
+            return self._ptt_should_hold_listening_led()
+        if self._vc_is_long_dialogue_mode():
+            return self._long_dialogue_should_hold_listening_led()
+        return False
+
+    def _vc_publish_listening_for_mode(self, reason=""):
+        """按 VC_mode 发 listening(vc_led:长对话 1000ms / 单对话 250ms)。"""
+        try:
+            if self._vc_is_ptt_mode() or self._vc_is_long_dialogue_mode():
+                self._vc_publish_led("listening", reason)
+        except Exception:
+            pass
+
+    def _ptt_capture_allowed(self, reason=""):
+        if not self._vc_is_ptt_mode():
+            return True
+        try:
+            if reason in ("ptt_begin", "ptt_interrupt", "canceled", "ptt_short_press"):
+                if self._ptt_is_active_tts_playback():
+                    return False
+            elif self._vc_tts_pipeline_busy():
                 return False
             if self._vc_local_play_busy():
                 return False
         except Exception:
             return False
         return True
+
+    def _long_dialogue_capture_allowed(self, reason=""):
+        if not self._vc_is_long_dialogue_mode():
+            return True
+        try:
+            if getattr(self, "_vc_pending_interrupt_uplink", 0) == 1:
+                return True
+            if self._vc_tts_pipeline_busy() or self._vc_local_play_busy():
+                return False
+        except Exception:
+            return False
+        return True
+
+    def _long_dialogue_media_busy(self):
+        """长对话:TTS/思考/播报中(按键须走打断,勿误走 continue)。"""
+        try:
+            if self._vc_tts_playback_active() or self._vc_tts_pipeline_busy():
+                return True
+            t = getattr(self.fsm, "turn", VCTurnState.Idle)
+            return t in (
+                VCTurnState.Thinking, VCTurnState.TtsReceiving,
+                VCTurnState.Playing, VCTurnState.AwaitPlayedAck,
+            )
+        except Exception:
+            return False
+
+    def _long_dialogue_end_interrupt(self):
+        try:
+            self._vc_pending_interrupt_uplink = 0
+            self._vc_cancel_deadline_ms = 0
+            self.is_ruppt_tts = 0
+            self._long_dialogue_interrupt_settled_ms = utime.ticks_ms()
+            # 勿清 _drop_tts_audio:开麦后迟到 TTS 仍须丢弃,避免 VC_Play_Start 抢停录
+        except Exception:
+            pass
+
+    def _long_dialogue_drop_tts_window_ms(self):
+        try:
+            if "vc_config" in globals() and vc_config:
+                return int(vc_config.get_int("LONG_DIALOGUE_DROP_TTS_MS", 8000))
+        except Exception:
+            pass
+        return 8000
+
+    def _long_dialogue_should_drop_inbound_tts(self):
+        """长对话打断窗口:丢弃迟到 TTS 帧 / 禁止起播。"""
+        if not self._vc_is_long_dialogue_mode():
+            return False
+        try:
+            if getattr(self, "_drop_tts_audio", 0) == 1:
+                return True
+            if getattr(self, "_vc_pending_interrupt_uplink", 0) == 1:
+                return True
+            if getattr(self, "is_ruppt_tts", 0) == 1:
+                return True
+            settled = getattr(self, "_long_dialogue_interrupt_settled_ms", 0)
+            if settled and utime.ticks_diff(utime.ticks_ms(), settled) < self._long_dialogue_drop_tts_window_ms():
+                return True
+        except Exception:
+            pass
+        return False
+
+    def _long_dialogue_on_server_tts_started(self):
+        """新一轮服务端 TTS(非打断尾包):允许下行播放。"""
+        if not self._vc_is_long_dialogue_mode():
+            return
+        try:
+            if getattr(self, "_vc_pending_interrupt_uplink", 0) == 1:
+                return
+            if getattr(self, "is_ruppt_tts", 0) == 1:
+                return
+            self._drop_tts_audio = 0
+            self._long_dialogue_interrupt_settled_ms = 0
+        except Exception:
+            pass
+
+    def _long_dialogue_is_stale_server_cancel(self):
+        """打断已开麦后迟到的 canceled:勿停录/勿灭灯(多次短按常见)。"""
+        if not self._vc_is_long_dialogue_mode():
+            return False
+        try:
+            if getattr(self, "_vc_pending_interrupt_uplink", 0) == 1:
+                return False
+            if getattr(self, "is_ruppt_tts", 0) == 1:
+                return False
+            if getattr(self, "VC_record_state_flag", 0) == 0:
+                return False
+            settled = getattr(self, "_long_dialogue_interrupt_settled_ms", 0)
+            if not settled:
+                return False
+            return utime.ticks_diff(utime.ticks_ms(), settled) < 5000
+        except Exception:
+            return False
+
+    def _long_dialogue_handle_stale_cancel(self):
+        """仅清 TTS/播放下行,保持录音与蓝慢闪。"""
+        try:
+            self._drop_tts_audio = 1
+            if getattr(self, "AI_audio_queue", None) is not None:
+                if self.AI_audio_queue.empty() != True:
+                    self.AI_audio_queue.clear()
+            self._ptt_hard_mute_local_playback()
+            if getattr(self, "VC_Play_flag", 0) == 1 or getattr(self, "play_pcm", None) is not None:
+                self.VC_Play_End()
+            self.fsm.await_played = False
+            self._need_audio_played_ack = 0
+            self.fsm.on_server_canceled()
+            self._long_dialogue_publish_listening("canceled/stale")
+            print("[KEY] long_dialogue stale canceled -> keep recording")
+        except Exception:
+            pass
+
+    def _vc_long_dialogue_key_arm_session(self):
+        """长对话按键开录意图:等 server.started 后再滴/开麦。"""
+        try:
+            self._long_dialogue_key_arm = 1
+            self._vc_arm_record_start_tone()
+        except Exception:
+            pass
+
+    def _vc_ptt_mark_record_after_started(self):
+        """PTT 长按:等 started / asr.started 后再滴。"""
+        try:
+            self._ptt_force_record_on_started = 1
+            self._vc_arm_record_start_tone()
+        except Exception:
+            pass
+
+    def _long_dialogue_key_down_arm(self, event=None, msg=None):
+        """EXTI 线程同步:先于 btn 队列置打断标志,避免 WS canceled 抢跑。"""
+        if not self._vc_is_long_dialogue_mode():
+            return
+        try:
+            if getattr(self, "AI_client_VC_isconnect", 0) != 1 or getattr(self, "_vc_session_started", 0) != 1:
+                return
+        except Exception:
+            return
+        if not self._long_dialogue_media_busy():
+            return
+        try:
+            self._vc_pending_interrupt_uplink = 1
+            self.is_ruppt_tts = 1
+        except Exception:
+            pass
+
+    def _vc_capture_allowed(self, reason=""):
+        """采集/上行开麦前:按模式分别门控,互不共用打断标志。"""
+        try:
+            if self._vc_is_ptt_mode():
+                return self._ptt_capture_allowed(reason)
+            if self._vc_is_long_dialogue_mode():
+                return self._long_dialogue_capture_allowed(reason)
+        except Exception:
+            pass
+        try:
+            if self._vc_tts_pipeline_busy() or self._vc_local_play_busy():
+                return False
+        except Exception:
+            pass
+        return True
     def _vc_stop_downlink_for_interrupt(self, reason="interrupt"):
         """详设 DES-SCN-003:打断/取消时先本地停播清队列,再允许开麦。"""
         try:
@@ -63,9 +288,25 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
         except Exception:
             pass
         try:
-            pending_intr = getattr(self, "_vc_pending_interrupt_uplink", 0) == 1
-            if (
+            # _vc_pending_interrupt_uplink 仅长对话;单对话用 _ptt_interrupt_active,勿混用
+            pending_intr = (
+                self._vc_is_long_dialogue_mode()
+                and getattr(self, "_vc_pending_interrupt_uplink", 0) == 1
+            )
+            ptt_intr = (
+                self._vc_is_ptt_mode()
+                and getattr(self, "_ptt_interrupt_active", 0) == 1
+            )
+            stale_cancel = (
+                self._vc_is_long_dialogue_mode()
+                and str(reason).find("canceled") >= 0
+                and self._long_dialogue_is_stale_server_cancel()
+            )
+            if stale_cancel:
+                pass
+            elif (
                 not pending_intr
+                and not ptt_intr
                 and getattr(self, "VC_record_state_flag", 0) != 0
                 and getattr(self, "record_data_save_flag", 0) == 0
             ):
@@ -184,76 +425,224 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
         else:
             EventMesh.publish("publish_AI_btnInfo", {"func": self.Close})
         EventMesh.publish("stop_sos_call")
-    def _ai_key_press_ptt_mode(self):
-        """单对话 PTT:短按仅停 TTS(不换 sid);非播放态短按吞掉;长按由 AI_key_long_press→ptt_begin。"""
-        if self.VC_mode != 1:
-            return False
+    def _ptt_is_active_tts_playback(self):
+        """下行 TTS 真在播(PCM/VC_Play);不含仅 Thinking 等服务器。"""
         try:
-            playing = False
-            try:
-                t = getattr(self.fsm, "turn", VCTurnState.Idle)
-                playing = t in (VCTurnState.TtsReceiving, VCTurnState.Playing)
-            except Exception:
-                playing = False
-            if playing or self._vc_tts_pipeline_busy():
-                self._ptt_short_press_stop = 1
-                try:
-                    self._ptt_hard_mute_local_playback()
-                except Exception:
-                    pass
-                try:
-                    self.VC_Play_End()
-                except Exception:
-                    pass
-                try:
-                    self.fsm.await_played = False
-                except Exception:
-                    pass
-                try:
-                    self._ptt_led_lock_listening = 0
-                    self._ptt_interrupt_active = 0
-                    self.is_ruppt_tts = 0
-                except Exception:
-                    pass
-                try:
-                    self._vc_publish_led("idle", "ptt_short_press/stop_tts")
-                except Exception:
-                    pass
+            if self._vc_tts_playback_active():
                 return True
+            t = getattr(self.fsm, "turn", VCTurnState.Idle)
+            return t in (VCTurnState.TtsReceiving, VCTurnState.Playing, VCTurnState.AwaitPlayedAck)
         except Exception:
             pass
-        return True
-    def _ai_key_press_long_dialogue_interrupt(self):
-        """长对话:TTS/播放忙或 turn==Thinking 时短按 → RequestToSpeak(先 cancel,canceled 后再开麦)。"""
-        if self.VC_mode != 0:
-            return False
+        return False
+
+    def _ptt_should_force_idle_led(self):
+        """单对话短按停 TTS 后保持灭灯,勿被 VC_Play_End/fsm_sync 拉回 playing/thinking。"""
         try:
-            if getattr(self, "AI_client_VC_isconnect", 0) != 1 or getattr(self, "_vc_session_started", 0) != 1:
+            if getattr(self, "VC_mode", -1) != 1:
+                return False
+            if getattr(self, "_ptt_idle_latch", 0) == 1:
+                return True
+            if getattr(self, "_ptt_short_press_stop", 0) == 1:
+                return True
+        except Exception:
+            pass
+        return False
+
+    def get_ptt_should_force_idle_led(self, event=None, msg=None):
+        try:
+            return 1 if self._ptt_should_force_idle_led() else 0
+        except Exception:
+            return 0
+
+    def get_vc_mode(self, event=None, msg=None):
+        try:
+            return int(getattr(self, "VC_mode", 0))
+        except Exception:
+            return 0
+
+    def _ptt_should_hold_listening_led(self):
+        """单对话打断/cancel 等待/录音:保持聆听蓝快闪,勿经 fsm 回到 playing/thinking。"""
+        try:
+            if getattr(self, "VC_mode", -1) != 1:
                 return False
+            if getattr(self, "_ptt_led_lock_listening", 0) == 1:
+                return True
+            if getattr(self, "_ptt_interrupt_active", 0) == 1:
+                return True
+            if getattr(self, "_vc_serial_canceling", 0) == 1:
+                return True
+            if getattr(self, "_ptt_pending_arm", 0) == 1:
+                return True
+            if getattr(self, "VC_record_state_flag", 0) != 0:
+                return True
         except Exception:
+            pass
+        return False
+
+    def get_vc_hold_listening_led(self, event=None, msg=None):
+        try:
+            return 1 if self._vc_should_hold_listening_led() else 0
+        except Exception:
+            return 0
+
+    def get_ptt_led_lock_listening(self, event=None, msg=None):
+        """兼容旧订阅名;语义为「当前模式是否锁定 listening 灯」。"""
+        return self.get_vc_hold_listening_led(event, msg)
+
+    def _ptt_publish_listening_fast(self, reason=""):
+        """单对话:蓝灯快闪(250ms)。"""
+        if not self._vc_is_ptt_mode():
+            return
+        self._vc_publish_listening_for_mode(reason)
+
+    def _long_dialogue_publish_listening(self, reason=""):
+        """长对话:蓝灯慢闪(1000ms)。"""
+        if not self._vc_is_long_dialogue_mode():
+            return
+        self._vc_publish_listening_for_mode(reason)
+
+    def _long_dialogue_should_hold_listening_led(self):
+        """长对话打断等待/录音:保持蓝慢闪(含 cancel→开麦 窗口)。"""
+        try:
+            if getattr(self, "VC_mode", -1) != 0:
+                return False
+            if getattr(self, "_vc_pending_interrupt_uplink", 0) == 1:
+                return True
+            if getattr(self, "is_ruppt_tts", 0) == 1 and getattr(self, "_vc_session_started", 0) == 1:
+                return True
+            if getattr(self, "VC_record_state_flag", 0) != 0:
+                return True
+        except Exception:
+            pass
+        return False
+
+    def _long_dialogue_interrupt_on_short_press(self):
+        """长对话 TTS/思考中短按:停播 + cancel;canceled(或 cancel 超时)后开麦。"""
+        if not self._vc_is_long_dialogue_mode():
+            return
+        try:
+            self.Close_flag = 0
+            self.is_ruppt_tts = 1
+            self._drop_tts_audio = 1
+            self._vc_pending_interrupt_uplink = 1
+            self._vc_arm_record_start_tone()
+            self.fsm.await_played = False
+            wait_ms = 800
+            if "vc_config" in globals() and vc_config:
+                wait_ms = int(vc_config.get_int("LONG_DIALOGUE_CANCEL_WAIT_MS", wait_ms))
+            self._vc_cancel_deadline_ms = utime.ticks_add(utime.ticks_ms(), wait_ms)
+        except Exception:
+            pass
+        try:
+            self._vc_stop_downlink_for_interrupt("long_press/interrupt")
+        except Exception:
+            pass
+        self._long_dialogue_publish_listening("long_press/interrupt")
+        try:
+            if getattr(self, "AI_client_VC_isconnect", 0) == 1:
+                self.AI_client_VC_send(ujson.dumps({"event": "cancel", "payload": {}}))
+                print("[KEY] long_dialogue interrupt -> cancel")
+        except Exception:
+            pass
+        try:
+            self.fsm.reset_rx_phases()
+            if getattr(self.fsm, "session", None) == VCSessionState.Started:
+                self.fsm.turn = VCTurnState.Listening
+        except Exception:
+            pass
+
+    def _ptt_stop_tts_on_short_press(self):
+        """单对话短按:停本地+下行 TTS、灭灯;发 cancel,不换 sid、不开录。"""
+        if not self._vc_is_ptt_mode():
+            return
+        self._ptt_short_press_stop = 1
+        self._ptt_idle_latch = 1
+        try:
+            self.is_ruppt_tts = 1
+            self._drop_tts_audio = 1
+            self._ptt_interrupt_active = 0
+            self._ptt_led_lock_listening = 0
+            self.fsm.await_played = False
+        except Exception:
+            pass
+        try:
+            self._vc_stop_downlink_for_interrupt("ptt_short_press")
+        except Exception:
+            pass
+        try:
+            if getattr(self, "AI_client_VC_isconnect", 0) == 1 and getattr(self, "_vc_session_started", 0) == 1:
+                json_data = {"event": "cancel", "payload": {}}
+                self.AI_client_VC_send(ujson.dumps(json_data))
+                print("[PTT] short_press stop_tts -> cancel")
+        except Exception:
+            pass
+        try:
+            self._vc_publish_led("idle", "ptt_short_press/stop_tts")
+        except Exception:
+            pass
+
+    def _ai_key_press_ptt_mode(self):
+        """单对话 PTT:按下仅吞掉;短按停播在松手且未长按时;长按→ptt_begin。"""
+        if not self._vc_is_ptt_mode():
             return False
-        need_cancel = False
-        if self._vc_tts_pipeline_busy():
-            need_cancel = True
-        else:
-            try:
-                if getattr(self.fsm, "turn", VCTurnState.Idle) == VCTurnState.Thinking:
-                    need_cancel = True
-            except Exception:
-                pass
-        if not need_cancel:
+        return True
+
+    def _ai_key_press_long_dialogue_dispatch(self):
+        """长对话 AI 键按下分发(与 PTT 分支互斥)。"""
+        if not self._vc_is_long_dialogue_mode():
             return False
-        print("[KEY] long interrupt -> RequestToSpeak (cancel then uplink)")
-        self.is_ruppt_tts = 1
+        if self._ai_key_press_long_dialogue_session():
+            return True
+        if self._ai_key_press_long_dialogue_wake():
+            EventMesh.publish(constants.EVENT.SYSTEM_UP)
+            return True
+        print("[KEY] AI_key_press fallback -> AI_VC_start ws={} started={}".format(
+            getattr(self, "AI_client_VC_isconnect", -1),
+            getattr(self, "_vc_session_started", -1)))
         try:
-            self._vc_pending_interrupt_uplink = 1
+            self.Close_flag = 0
+            self._skip_started_uplink_once = 0
+            self._vc_long_dialogue_key_arm_session()
         except Exception:
             pass
-        EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_client_VC_RequestToSpeak})
+        self._long_dialogue_publish_listening("key.press/fallback")
+        self.get_config_timer_count = 0
+        self.key_state = 1
+        self.AI_TTS_str = ""
+        self.AI_ASR_str = ""
+        self.AI_ASR_str_tmp = ""
+        EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_VC_start})
+        self.AI_key_long_press_flag = 0
         return True
+
+    def _ai_key_press_long_dialogue_session(self):
+        """长对话已连接且 started:忙则打断,否则续聊开麦。"""
+        if not self._vc_is_long_dialogue_mode():
+            return False
+        try:
+            if getattr(self, "AI_client_VC_isconnect", 0) != 1 or getattr(self, "_vc_session_started", 0) != 1:
+                return False
+        except Exception:
+            return False
+        try:
+            if self._long_dialogue_media_busy():
+                self._long_dialogue_interrupt_on_short_press()
+                return True
+            self.Close_flag = 0
+            self._skip_started_uplink_once = 0
+            self._vc_long_dialogue_key_arm_session()
+            self._long_dialogue_publish_listening("key.press/continue")
+            self._vc_clear_stale_record_state("key.press/continue")
+            self.request_uplink_start("key.press/continue")
+            return True
+        except Exception as e:
+            usys.print_exception(e)
+        return False
+
     def _ai_key_press_long_dialogue_wake(self):
         """长对话未连接或未 started:直接 AI_VC_start,勿 Close+sleep。"""
-        if self.VC_mode != 0:
+        if not self._vc_is_long_dialogue_mode():
             return False
         if getattr(self, "voice_flag", 0) == 1 or getattr(self, "meet_flag", 0) == 1 or getattr(self, "trans_flag", 0) == 1:
             return False
@@ -265,8 +654,11 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
         print("[KEY] long wake AI_VC_start (no Close)")
         try:
             self.Close_flag = 0
+            self._skip_started_uplink_once = 0
+            self._vc_long_dialogue_key_arm_session()
         except Exception:
             pass
+        self._long_dialogue_publish_listening("key.press/wake")
         self.get_config_timer_count = 0
         self.key_state = 1
         self.AI_TTS_str = ""
@@ -280,25 +672,17 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
             EventMesh.publish("set_led_state", {"alert": "rb_flash_2"})
         except Exception:
             pass
-    def _ai_key_press_long_dialogue_when_session_ready(self):
-        """长对话已连接且 session 已 started:非播放态直接开麦续聊(勿 idle 时发 cancel)。"""
-        if self.VC_mode != 0:
-            return False
-        try:
-            if getattr(self, "AI_client_VC_isconnect", 0) == 1 and getattr(self, "_vc_session_started", 0) == 1:
-                # 模式切换提示音会置 Close_flag=1;用户再短按须恢复可开麦
-                try:
-                    self.Close_flag = 0
-                except Exception:
-                    pass
-                self._vc_clear_stale_record_state("key.press/continue")
-                self.request_uplink_start("key.press/continue")
-                return True
-        except Exception:
-            pass
-        return False
     def _ai_key_release_handle_ptt(self):
         """单对话 PTT 松手:仅在上行录音中时 end_speech;禁止误触发 AI_VC_start(否则新 sid / cancel 竞态)。"""
+        if not self._vc_is_ptt_mode():
+            return
+        if self.AI_key_long_press_flag == 0:
+            try:
+                if self._ptt_is_active_tts_playback():
+                    self._ptt_stop_tts_on_short_press()
+                    return
+            except Exception:
+                pass
         if self.AI_key_long_press_flag == 1:
             if getattr(self, "VC_record_state_flag", 0) != 0:
                 try:
@@ -328,23 +712,28 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
             pass
         print("[PTT] release_short_no_long")
     def _ai_key_long_press_route(self, event=None, msg=None):
-        """长按:翻译先 Close 再开翻译录音;单对话→ptt_begin;长对话→与短按一致走 AI_key_press 链路。"""
+        """长按:翻译先 Close 再开翻译录音;单对话→ptt_begin;长对话→走长对话短按分发。"""
         if self.trans_flag == 1:
             self.Close()
             EventMesh.publish("start_voice_record", 4)
         self.AI_key_long_press_flag = 1
-        if self.VC_mode == 1:
+        if self._vc_is_ptt_mode():
+            try:
+                self._ptt_idle_latch = 0
+                self._ptt_short_press_stop = 0
+            except Exception:
+                pass
             EventMesh.publish("publish_AI_btnInfo", {"func": self.ptt_begin})
-        else:
-            EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_key_press})
+        elif self._vc_is_long_dialogue_mode():
+            EventMesh.publish("publish_AI_btnInfo", {"func": self._ai_key_press_long_dialogue_dispatch})
     def _ptt_end_led_suppress(self):
         # 保持 _vc_play_end_suppress_idle_led / _ptt_led_lock_listening 直至真开录(VC_Speech_Start)
         pass
     def _ptt_should_drop_inbound_tts(self):
         """单对话打断窗口:丢弃迟到 TTS 帧 / 禁止起播。"""
+        if not self._vc_is_ptt_mode():
+            return False
         try:
-            if getattr(self, "VC_mode", -1) != 1:
-                return False
             if getattr(self, "_drop_tts_audio", 0) == 1:
                 return True
             if getattr(self, "is_ruppt_tts", 0) == 1:
@@ -361,6 +750,14 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
     def _ptt_should_skip_remote_playback(self):
         return self._ptt_should_drop_inbound_tts()
     def _ptt_enter_interrupt_arm(self):
+        if not self._vc_is_ptt_mode():
+            return
+        try:
+            self._ptt_idle_latch = 0
+            self._ptt_short_press_stop = 0
+            self._vc_pending_interrupt_uplink = 0
+        except Exception:
+            pass
         try:
             self._ptt_arm_epoch = int(getattr(self, "_ptt_arm_epoch", 0)) + 1
             self._ptt_interrupt_active = 1
@@ -371,6 +768,8 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
         except Exception:
             pass
     def _ptt_abort_interrupt_arm(self):
+        if not self._vc_is_ptt_mode():
+            return
         try:
             self._ptt_interrupt_active = 0
             self._ptt_led_lock_listening = 0
@@ -380,10 +779,9 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
         except Exception:
             pass
     def _ptt_on_record_hardware_started(self):
-        """真开录后:允许新 TTS、放开灯锁;旧 TTS 已在打断期丢弃。"""
+        """真开录后:允许新 TTS;聆听灯锁保持至松手 VC_Speech_End。"""
         try:
             self._ptt_interrupt_active = 0
-            self._ptt_led_lock_listening = 0
             self._vc_play_end_suppress_idle_led = 0
             self._drop_tts_audio = 0
             self.is_ruppt_tts = 0
@@ -482,6 +880,7 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
             self._drop_tts_audio = 1
             print("[VC][serial] cancel_wait reason={} turn={}".format(
                 reason, getattr(getattr(self, "fsm", None), "turn", "?")))
+            self._ptt_publish_listening_fast("ptt_cancel_wait")
         except Exception:
             pass
     def _vc_on_ptt_cancel_settled(self, via="canceled"):
@@ -497,7 +896,26 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
                 self._ptt_arm_uplink()
         except Exception:
             pass
-    def _vc_tick_serial_cancel_timeout(self):
+    def _vc_tick_cancel_deadline(self):
+        """长对话打断等 canceled / PTT cancel 等待:共用 _vc_cancel_deadline_ms 超时兜底。"""
+        try:
+            if (
+                self._vc_is_long_dialogue_mode()
+                and getattr(self, "_vc_pending_interrupt_uplink", 0) == 1
+            ):
+                dl = getattr(self, "_vc_cancel_deadline_ms", 0)
+                if dl and utime.ticks_diff(utime.ticks_ms(), dl) >= 0:
+                    self._vc_cancel_deadline_ms = 0
+                    if getattr(self, "VC_record_state_flag", 0) == 0:
+                        print("[KEY] long_dialogue cancel_wait timeout -> uplink")
+                        try:
+                            self._vc_clear_stale_record_state("canceled/timeout")
+                        except Exception:
+                            pass
+                        self.request_uplink_start("canceled")
+                    return
+        except Exception:
+            pass
         if not getattr(self, "_vc_serial_canceling", 0):
             return
         try:
@@ -526,13 +944,17 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
         except Exception:
             pass
         try:
-            self.VC_Speech_Start_now()
-        except Exception:
-            pass
-        try:
-            self._vc_publish_led("listening", "ptt_begin/recording")
+            self._vc_arm_record_start_tone()
+            if (
+                getattr(self, "_vc_session_started", 0) == 1
+                and getattr(self, "_vc_pending_record_start_tone", 0) == 1
+            ):
+                self._ptt_defer_speech_until_asr = 1
+            else:
+                self.VC_Speech_Start_now()
         except Exception:
             pass
+        self._ptt_publish_listening_fast("ptt_begin/recording")
         self._ptt_end_led_suppress()
         try:
             if t0:
@@ -540,10 +962,7 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
         except Exception:
             pass
     def ptt_begin(self, reason="key.long_press"):
-        try:
-            if self.VC_mode != 1:
-                return
-        except Exception:
+        if not self._vc_is_ptt_mode():
             return
 
         # PTT 新一轮开始:清理模式切换/提示音退出标志
@@ -560,10 +979,7 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
             t0 = 0
 
         self._ptt_enter_interrupt_arm()
-        try:
-            self._vc_publish_led("listening", "ptt_begin/pre")
-        except Exception:
-            pass
+        self._ptt_publish_listening_fast("ptt_begin/pre")
 
         # 本地先硬静音再 VC_init,避免 DMA/队列尾包漏播
         try:
@@ -604,11 +1020,11 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
         # 会话未连接:先建链并发 Start,等待 started 后再自动开麦
         try:
             if getattr(self, "AI_client_VC_isconnect", 0) != 1:
-                self._ptt_force_record_on_started = 1
+                self._vc_ptt_mark_record_after_started()
                 self.AI_VC_start()
                 return
         except Exception:
-            self._ptt_force_record_on_started = 1
+            self._vc_ptt_mark_record_after_started()
             try:
                 self.AI_VC_start()
             except Exception:
@@ -617,7 +1033,7 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
 
         # 会话还没 initialized:先发 Start,请求服务端进入 started
         if getattr(self, "_vc_session_started", 0) != 1:
-            self._ptt_force_record_on_started = 1
+            self._vc_ptt_mark_record_after_started()
             try:
                 try:
                     if t0:
@@ -645,6 +1061,14 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
     def _vc_led_should_standby_idle(self):
         """未录音且非 TTS 播中:待机/提示音/模式切换/单对话轮间应灭灯(DES-L3 idle),勿被 FSM.turn=Listening 误导。"""
         try:
+            if (
+                self._vc_is_long_dialogue_mode()
+                and (
+                    getattr(self, "_vc_pending_interrupt_uplink", 0) == 1
+                    or getattr(self, "is_ruppt_tts", 0) == 1
+                )
+            ):
+                return False
             if getattr(self, "VC_record_state_flag", 0) != 0:
                 return False
             if self._vc_tts_playback_active():
@@ -669,14 +1093,23 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
         except Exception:
             pass
         return False
-    def _vc_publish_led(self, ai_state, reason=""):
+    def _vc_publish_led(self, ai_state, reason="", prefer_off=0):
         try:
-            if getattr(self, "_ptt_led_lock_listening", 0) == 1:
+            if self._ptt_should_force_idle_led():
+                if ai_state != "idle":
+                    if self._vc_led_state_debug_enabled():
+                        print("[LED] publish coerce ai={} -> idle reason={} (ptt_stop_latch)".format(
+                            ai_state, reason))
+                    ai_state = "idle"
+        except Exception:
+            pass
+        try:
+            if self._vc_should_hold_listening_led():
                 if ai_state != "listening":
                     if self._vc_led_state_debug_enabled():
-                        print("[LED] publish blocked ai={} reason={} (ptt_arm_lock)".format(
+                        print("[LED] publish coerce ai={} -> listening reason={} (listen_hold)".format(
                             ai_state, reason))
-                    return
+                    ai_state = "listening"
         except Exception:
             pass
         try:
@@ -688,25 +1121,49 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
         except Exception:
             pass
         try:
-            EventMesh.publish("set_led_state", {"ai_state": ai_state})
+            led_msg = {"ai_state": ai_state}
+            if prefer_off:
+                led_msg["prefer_off"] = 1
+            EventMesh.publish("set_led_state", led_msg)
         except Exception:
             pass
+    def _vc_led_standby_prefer_off(self):
+        """模式切换/提示音播完待机:优先灭灯,勿被 charging=full 拉成绿灯常亮。"""
+        try:
+            if getattr(self, "Close_flag", 0) == 1:
+                return True
+            k = getattr(self, "_last_tts_kind", "") or ""
+            if k in ("prompt", "boot_prompt", "mode_switch_long", "mode_switch_ptt"):
+                return True
+        except Exception:
+            pass
+        return False
     def _vc_led_sync_from_fsm(self):
         """按 FSM/真实 TTS 播放恢复 AI 灯;本地 aud.play 短音不参与。"""
         try:
-            if getattr(self, "_ptt_led_lock_listening", 0) == 1:
-                if getattr(self, "VC_record_state_flag", 0) == 0:
-                    return
+            if self._ptt_should_force_idle_led():
+                self._vc_publish_led("idle", "fsm_sync/ptt_stop_latch")
+                return
+        except Exception:
+            pass
+        try:
+            if self._vc_should_hold_listening_led():
+                self._vc_publish_listening_for_mode("fsm_sync/listen_hold")
+                return
         except Exception:
             pass
         try:
             if self._vc_led_should_standby_idle():
-                self._vc_publish_led("idle", "fsm_sync/standby_idle")
+                self._vc_publish_led(
+                    "idle",
+                    "fsm_sync/standby_idle",
+                    prefer_off=1 if self._vc_led_standby_prefer_off() else 0,
+                )
                 return
         except Exception:
             pass
         try:
-            if getattr(self, "VC_mode", -1) == 1 and getattr(self, "_ptt_await_server", 0) == 1:
+            if self._vc_is_ptt_mode() and getattr(self, "_ptt_await_server", 0) == 1:
                 self._vc_publish_led("thinking", "fsm_sync/ptt_await")
                 return
         except Exception:
@@ -866,6 +1323,13 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
         self._ptt_arm_epoch = 0
         self._ptt_interrupt_active = 0
         self._ptt_led_lock_listening = 0
+        self._ptt_idle_latch = 0
+        self._ptt_short_press_stop = 0
+        self._long_dialogue_key_arm = 0
+        # AI 键短/长按开录:置位后等 server.started / asr.started(或会话已 started)再播 start.mp3
+        self._vc_pending_record_start_tone = 0
+        # PTT:已 started 时等 asr.started 滴后再 VC_Speech_Start
+        self._ptt_defer_speech_until_asr = 0
         # 端侧主动发 synthesis 后到收到 tts.started 之间的窗口标志,用于阻止误开下一轮录音上行
         self._tts_pending = 0
         self._tts_pending_ms = 0
@@ -1153,10 +1617,12 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
         self.AI_key_long_press_flag = 0
         self._asr_completed_tone_done = 0  # asr.completed 本地 think 提示每轮仅一次
         self._last_asr_result_text = ""  # 保留最近 asr.result,防 VC_Speech_End 早于 asr.completed 清空
-        self._vc_last_auto_uplink_ms = 0  # 长对话自动开麦防抖
+        self._vc_last_auto_uplink_ms = 0  # 长对话自动开麦防抖(play_end/completed 等)
+        self._vc_last_interrupt_uplink_ms = 0  # 长对话用户打断开麦防抖(与自动轨独立)
         self._vc_local_play_active = 0  # aud.play 本地短音占用(与采集互斥)
         self._vc_local_play_until_ms = 0
         self._vc_pending_interrupt_uplink = 0  # 长对话短按打断:等 canceled 再开麦
+        self._long_dialogue_interrupt_settled_ms = 0  # 打断开麦完成时刻(过滤迟到 canceled)
         self.websocket_error_count = 0
         self.AI_new_voice_url = "" #未读语音地址/音乐播放地址/闹钟播放标志
         self.music_play_flag = 0 #音乐播放标志
@@ -1178,6 +1644,7 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
         if self.is_noread_voice == None:
             self.is_noread_voice = 0#是否还有未读留言(开机判断)
         self.is_ruppt_tts = 0#是否已经打断tts播放
+        EventMesh.subscribe("vc_long_dialogue_key_down", self._long_dialogue_key_down_arm)
         EventMesh.subscribe("AI_key_press", self.AI_key_press)
         EventMesh.subscribe("BT_AI_key_press", self.BT_AI_key_press)
         EventMesh.subscribe("stop_AI_record", self.stop_AI_record)
@@ -1189,6 +1656,10 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
         EventMesh.subscribe("get_VC_satrt_flag", self.get_VC_satrt_flag)
         EventMesh.subscribe("get_vc_stream_active", self.get_vc_stream_active)
         EventMesh.subscribe("get_vc_tts_playback_active", self.get_vc_tts_playback_active)
+        EventMesh.subscribe("get_ptt_should_force_idle_led", self.get_ptt_should_force_idle_led)
+        EventMesh.subscribe("get_vc_mode", self.get_vc_mode)
+        EventMesh.subscribe("get_ptt_led_lock_listening", self.get_ptt_led_lock_listening)
+        EventMesh.subscribe("get_vc_hold_listening_led", self.get_vc_hold_listening_led)
         EventMesh.subscribe("battery_priority_mute_vc", self.battery_priority_mute_vc)
         EventMesh.subscribe("VC_error", self.VC_error)
         EventMesh.subscribe("audio_test", self.audio_test)
@@ -1508,25 +1979,11 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
                     getattr(self, "VC_mode", -1), self._vc_fsm_log_snip(), t, led))
         except Exception:
             pass
-        # å\8d\95对è¯\9d PTT / é\95¿å¯¹è¯\9då\88\86æ\94¯ï¼\88æ\8c\89é\94®ç\9f­æ\8c\89æ\8f\90示é\9f³å·²å\85³ï¼\8cé\81¿å\85\8dä¸\8eå¼\80å½\95ç­\89æ\8f\90示é\9f³å\8f å\9c¨ä¸\80èµ·ï¼\89
+        # å\8f\8c模å¼\8fäº\92æ\96¥ï¼\9aå\85\88 PTT(VC_mode==1)ï¼\8cå\86\8dé\95¿å¯¹è¯\9d(VC_mode==0)
         try:
             if self._ai_key_press_ptt_mode():
                 return
-        except Exception as e:
-            usys.print_exception(e)
-        try:
-            if self._ai_key_press_long_dialogue_interrupt():
-                return
-        except Exception as e:
-            usys.print_exception(e)
-        try:
-            if self._ai_key_press_long_dialogue_when_session_ready():
-                return
-        except Exception as e:
-            usys.print_exception(e)
-        try:
-            if self._ai_key_press_long_dialogue_wake():
-                EventMesh.publish(constants.EVENT.SYSTEM_UP)
+            if self._ai_key_press_long_dialogue_dispatch():
                 return
         except Exception as e:
             usys.print_exception(e)
@@ -1550,22 +2007,6 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
             return
         
         EventMesh.publish("stop_voice_record") #关闭语音录音
-        if self.VC_mode == 0:
-            print("[KEY] AI_key_press fallback -> AI_VC_start ws={} started={}".format(
-                getattr(self, "AI_client_VC_isconnect", -1),
-                getattr(self, "_vc_session_started", -1)))
-            try:
-                self.Close_flag = 0
-            except Exception:
-                pass
-            self.get_config_timer_count = 0
-            self.key_state = 1
-            self.AI_TTS_str = ""
-            self.AI_ASR_str = ""
-            self.AI_ASR_str_tmp = ""
-            EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_VC_start})
-            self.AI_key_long_press_flag = 0
-            return
         print("AI_key_press: unhandled mode branch VC_mode={}".format(
             getattr(self, "VC_mode", -1)))
         self.AI_key_long_press_flag = 0
@@ -1588,10 +2029,12 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
         # - 对话进行中:打断/续聊(复用 dialog_id)
         # - 已停止/未 started:走新会话流程
         try:
-            if self.VC_mode == 0:
+            if self._vc_is_long_dialogue_mode():
                 if getattr(self, "AI_client_VC_isconnect", 0) == 1 and getattr(self, "_vc_session_started", 0) == 1:
-                    EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_client_VC_RequestToSpeak})
+                    EventMesh.publish("publish_AI_btnInfo", {"func": self._ai_key_press_long_dialogue_dispatch})
                     return
+                EventMesh.publish("publish_AI_btnInfo", {"func": self._ai_key_press_long_dialogue_dispatch})
+                return
         except Exception:
             pass
 
@@ -1612,6 +2055,7 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
         #     EventMesh.publish("publish_AI_btnInfo",{"func":self.Close}) 
         #     EventMesh.publish("publish_AI_btnInfo",{"func":utime.sleep_ms,'data':(10)})
         self._uplink_request_stop("BT_AI_key_press/compat2")
+        self._vc_arm_record_start_tone()
         self._uplink_request_start("BT_AI_key_press/compat2")
         
         self.AI_VC_start() 
@@ -1819,7 +2263,7 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
             pass
     def _1S_timer_cb_(self):
         try:
-            self._vc_tick_serial_cancel_timeout()
+            self._vc_tick_cancel_deadline()
         except Exception:
             pass
 
@@ -1950,7 +2394,12 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
         self.VC_satrt_kws_flag = 1
         if self.VC_Thinking_timerout < 100:
             self.VC_Thinking_timerout = 0
-        # 不在这里提前切灯态:播放/聆听灯只在“播放/录音真正开始/停止”处更新
+        # 长对话:按键唤醒建链时先蓝慢闪聆听;单对话仍只在真开录时切灯
+        try:
+            if getattr(self, "VC_mode", -1) == 0:
+                self._long_dialogue_publish_listening("AI_VC_start")
+        except Exception:
+            pass
         EventMesh.publish("wakeup_BT")
         #self.VC_Play_End()#关闭音频播放
         print("AI_VC_start2")
@@ -2130,12 +2579,17 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
         self._vc_session_started = 0
         self._ptt_audio_created = 0
         self._vc_pending_interrupt_uplink = 0
+        try:
+            self._vc_pending_record_start_tone = 0
+            self._ptt_defer_speech_until_asr = 0
+        except Exception:
+            pass
         try:
             self._skip_started_uplink_once = 0
         except Exception:
             pass
         try:
-            self._ptt_abort_interrupt_arm()
+            self._vc_reset_dialogue_mode_state()
         except Exception:
             pass
         # Power 短按停止对话后,再次按 AI 需要新建 dialog_id:清理本轮 dialog/session 标识
@@ -2154,7 +2608,7 @@ class AI_VC(VCSessionIoMixin, VCSessionMediaMixin, Abstract):
                 print("[VC][long_idle] step=set_led_state_idle (first)")
         except Exception:
             pass
-        EventMesh.publish("set_led_state", {"ai_state": "idle"})
+        EventMesh.publish("set_led_state", {"ai_state": "idle", "prefer_off": 1})
         self.tts_in_data_len = 0
         self.tts_out_data_len = 0
         #必须先销毁线程,不然线程会容易报错
index 185997d..675d200 100644 (file)
@@ -18,7 +18,51 @@ class VCSessionIoMixin:
             usys.print_exception(e)
         if ev == "canceled":
             self._vc_on_ptt_cancel_settled(via="canceled")
+
+    def _vc_fsm_dispatch_cancel_cb(self, data):
+        """btn 线程:长对话 canceled(与按键同队列,避免 pending 竞态)。"""
+        try:
+            ev, jsload = data
+        except Exception:
+            return
+        try:
+            if hasattr(self, "_long_dialogue_is_stale_server_cancel") and self._long_dialogue_is_stale_server_cancel():
+                self._long_dialogue_handle_stale_cancel()
+                return
+        except Exception:
+            pass
+        try:
+            self._vc_stop_downlink_for_interrupt("canceled")
+        except Exception:
+            pass
+        try:
+            actions = self.fsm.on_server_event(ev, jsload)
+            if hasattr(self, "_long_dialogue_is_stale_server_cancel"):
+                try:
+                    if self._long_dialogue_is_stale_server_cancel():
+                        actions = [a for a in (actions or []) if a and a[0] != "uplink_start"]
+                except Exception:
+                    pass
+            self._fsm_apply_actions(actions)
+        except Exception:
+            pass
+
     def _vc_dispatch_server_event(self, ev, jsload_data=None):
+        if ev == "tts.started":
+            try:
+                self._vc_tts_rx_counters_reset()
+            except Exception:
+                pass
+        if getattr(self, "VC_mode", -1) == 0:
+            if ev == "tts.started":
+                try:
+                    if hasattr(self, "_long_dialogue_on_server_tts_started"):
+                        self._long_dialogue_on_server_tts_started()
+                except Exception:
+                    pass
+            if ev == "canceled":
+                self._vc_enqueue_btn(self._vc_fsm_dispatch_cancel_cb, (ev, jsload_data))
+                return
         try:
             if ev == "canceled":
                 self._vc_stop_downlink_for_interrupt("canceled")
@@ -549,6 +593,15 @@ class VCSessionIoMixin:
                                 if not _skip_uplink:
                                     # 会话已 started:若此时上行未启动,则在这里补一次开麦,避免“Start 前开麦被 Close 停掉”后
                                     # 出现首轮无音频上行导致“第一次对话没有”的情况。
+                                    try:
+                                        if getattr(self, "_long_dialogue_key_arm", 0) == 1:
+                                            self._long_dialogue_publish_listening("server.started")
+                                    except Exception:
+                                        pass
+                                    try:
+                                        self._vc_on_server_record_uplink_ready("server.started")
+                                    except Exception:
+                                        pass
                                     if self.record_data_save_flag == 0:
                                         if self.VC_record_state_flag == 0:
                                             self.request_uplink_start("server.started")
@@ -617,17 +670,27 @@ class VCSessionIoMixin:
                                 if self.VC_mode == 0:
                                     self.request_uplink_start("server.started/idle")
                                 else:
-                                    # Push-To-Talk:必须先同步发出 audio.create,再开麦。
-                                    # request_send_audio_create() 走 EventMesh 异步队列,会导致录音线程先于
-                                    # audio.create 上行,深度睡眠唤醒后首轮 ASR 空结果、第二轮才正常。
+                                    # Push-To-Talk:started 后先滴(可上传)→ audio.create → 再开麦
+                                    _ptt_rec = False
+                                    try:
+                                        _ptt_rec = getattr(self, "_ptt_force_record_on_started", 0) == 1
+                                        if _ptt_rec:
+                                            self._ptt_force_record_on_started = 0
+                                    except Exception:
+                                        pass
+                                    try:
+                                        if _ptt_rec:
+                                            self._vc_on_server_record_uplink_ready(
+                                                "ptt_begin/started"
+                                            )
+                                    except Exception:
+                                        pass
                                     try:
                                         self.AI_client_VC_send_speech()
                                     except Exception:
                                         self.request_send_audio_create()
-                                    # 若长按发生在“未 started”窗口,收到 started 后立即开麦进入新一轮 PTT
                                     try:
-                                        if getattr(self, "_ptt_force_record_on_started", 0) == 1:
-                                            self._ptt_force_record_on_started = 0
+                                        if _ptt_rec:
                                             self.VC_Speech_Start_now()
                                     except Exception:
                                         pass
@@ -648,6 +711,10 @@ class VCSessionIoMixin:
                                 self.fsm.on_server_asr_started()
                             except Exception:
                                 pass
+                            try:
+                                self._vc_on_server_record_uplink_ready("asr.started")
+                            except Exception:
+                                pass
                             self._server_vad_silence = 0
                             self._turn_has_asr_completed = 0
                             self._turn_has_vad_silence = 0
@@ -698,6 +765,16 @@ class VCSessionIoMixin:
                                         print("[VC][ASR] completed empty, skip think tone")
                                     except Exception:
                                         pass
+                                    try:
+                                        if (
+                                            getattr(self, "VC_mode", 0) == 0
+                                            and getattr(self, "Close_flag", 0) == 0
+                                            and getattr(self, "_vc_session_started", 0) == 1
+                                        ):
+                                            self._server_vad_silence = 0
+                                            self.request_uplink_start("asr.empty/reopen")
+                                    except Exception:
+                                        pass
                                 else:
                                     stream_on = False
                                     try:
@@ -767,7 +844,10 @@ class VCSessionIoMixin:
                             except Exception:
                                 pass
                             try:
-                                EventMesh.publish("set_led_state", {"ai_state": "thinking"})
+                                if not self._vc_tts_playback_active() and not getattr(
+                                    self.fsm, "await_played", False
+                                ):
+                                    EventMesh.publish("set_led_state", {"ai_state": "thinking"})
                             except Exception:
                                 pass
                             self._llm_completed_ms = 0
@@ -868,15 +948,32 @@ class VCSessionIoMixin:
                                 self._tts_completed_wall_ms = utime.ticks_ms()
                             except Exception:
                                 self._tts_completed_wall_ms = 0
+                            try:
+                                if getattr(self, "VC_mode", 0) == 0:
+                                    EventMesh.publish(
+                                        "publish_AI_btnInfo",
+                                        {"func": self._vc_try_play_end_tick},
+                                    )
+                            except Exception:
+                                pass
                             # 播完上报完全由 FSM await_played + play_end 驱动,这里不再使用 LocalRespondingEnded_timerout/_need_audio_played_ack
                             pass
 
                         elif jsload_data["event"] == "canceled" :#打断
-                            # 停播/清队列已在 _vc_dispatch_server_event(canceled) 完成;此处仅收尾标志
-                            self.is_ruppt_tts = 0#设备上发中断tts,收到服务器返回的cancel后置为0
+                            # 停播/开麦在 btn 队列 _vc_fsm_dispatch_cancel_cb;is_ruppt 在长对话打断收尾时清
+                            if getattr(self, "VC_mode", 0) != 0:
+                                self.is_ruppt_tts = 0
                             self._need_audio_played_ack = 0
                             self._tts_pending = 0
                             self._tts_pending_ms = 0
+                            try:
+                                if (
+                                    getattr(self, "VC_mode", 0) == 0
+                                    and getattr(self, "_vc_pending_interrupt_uplink", 0) == 1
+                                ):
+                                    self._long_dialogue_publish_listening("server.canceled/interrupt")
+                            except Exception:
+                                pass
                             # 开麦由 FSM action list 统一触发(uplink_start:canceled),避免重复调用
                             if self.AI_audio_queue_thread_id ==0:
                                 _thread.stack_size(24*1024)
@@ -889,8 +986,23 @@ class VCSessionIoMixin:
                             # 重要:服务端常在 tts.completed 后紧跟 completed。
                             # 若本轮存在 TTS/待发 audio.played,则不能在此清零 LocalRespondingEnded_timerout/_need_audio_played_ack,
                             # 否则会掐断「播完→audio.played→下一轮录音」链路,表现为最多 1~2 轮就停。
-                            if self._need_audio_played_ack == 1 or self._vc_tts_pipeline_busy() or self.VC_Play_flag == 1:
-                                pass
+                            if (
+                                self._need_audio_played_ack == 1
+                                or self._vc_tts_pipeline_busy()
+                                or self.VC_Play_flag == 1
+                            ):
+                                try:
+                                    if (
+                                        getattr(self, "VC_mode", 0) == 0
+                                        and getattr(self.fsm, "await_played", False)
+                                        and getattr(self, "VC_record_state_flag", 0) == 0
+                                    ):
+                                        EventMesh.publish(
+                                            "publish_AI_btnInfo",
+                                            {"func": self._vc_try_play_end_tick},
+                                        )
+                                except Exception:
+                                    pass
                             else:
                                 # 无 TTS 的 completed:清状态并按需进入下一轮(见下方条件)
                                 self.LocalRespondingEnded_timerout = 0
@@ -1696,27 +1808,17 @@ class VCSessionIoMixin:
         except Exception:
             pass
     def AI_client_VC_RequestToSpeak(self):
-        json_data = \
-        {
-            "event": "cancel",
-            "payload": {}
-        }
+        """兼容旧入口:长对话短按打断(勿 VC_init,避免灯态/fsm 进 thinking)。"""
         print("AI_client_VC_RequestToSpeak!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
-        # 模式切换等场景 play_tts(..., is_close_VC=1) 会置 Close_flag=1;用户再按 AI 键走 cancel→canceled→
-        # uplink_start 时,FSM 与 _uplink_request_start 均会因 Close_flag 拒绝开麦,表现为“切回长对话后按键无上行”。
         try:
-            if self.VC_mode == 0:
-                self.Close_flag = 0
-                self._vc_pending_interrupt_uplink = 1
-                self._vc_clear_stale_record_state("RequestToSpeak")
-        except Exception:
-            pass
-        # 只做“打断+清播放队列”,不要在这里关音频队列线程(会导致后续 TTS/提示音不稳定,且触发竞态)
-        EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_init})
-        EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_VC_send,"data":ujson.dumps(json_data)})
-        utime.sleep_ms(1)
+            if getattr(self, "VC_mode", -1) == 0:
+                self._long_dialogue_interrupt_on_short_press()
+            return
+        except Exception as e:
+            usys.print_exception(e)
+        json_data = {"event": "cancel", "payload": {}}
         try:
-            self.fsm.reset_rx_phases()
+            self.AI_client_VC_send(ujson.dumps(json_data))
         except Exception:
             pass
     def AI_client_new_voice(self): #上传未读的语音地址/包括音频地址也可以
index b5d2038..440f3f3 100644 (file)
@@ -68,6 +68,146 @@ class VCSessionMediaMixin:
         except Exception:
             pass
         return False
+    def _vc_arm_record_start_tone(self):
+        """AI 键开录意图:待服务端允许上行后再播 start.mp3。"""
+        try:
+            self._vc_pending_record_start_tone = 1
+        except Exception:
+            pass
+
+    def _vc_record_start_tone_debug(self):
+        try:
+            if "vc_config" in globals() and vc_config and (
+                vc_config.get_bool("KEY_LED_DEBUG", False)
+                or vc_config.get_bool("LED_STATE_DEBUG", False)
+            ):
+                return True
+        except Exception:
+            pass
+        return False
+
+    def _vc_try_play_record_start_tone_when_ready(self, via=""):
+        """收到 server.started / asr.started(PTT)或会话已 started 的按键续聊后再滴。"""
+        try:
+            v = str(via or "")
+            if not getattr(self, "_vc_pending_record_start_tone", 0):
+                if self._vc_record_start_tone_debug():
+                    print("[VC] record_start_tone skip pending=0 via={}".format(v))
+                return False
+            if getattr(self, "Close_flag", 0) == 1:
+                if self._vc_record_start_tone_debug():
+                    print("[VC] record_start_tone skip Close_flag via={}".format(v))
+                return False
+            if getattr(self, "_skip_started_uplink_once", 0) == 1:
+                if self._vc_record_start_tone_debug():
+                    print("[VC] record_start_tone skip boot_prompt via={}".format(v))
+                return False
+            if v == "asr.started":
+                if getattr(self, "VC_mode", -1) != 1:
+                    return False
+            elif v == "uplink_ready":
+                if getattr(self, "_vc_session_started", 0) != 1:
+                    return False
+            elif v not in (
+                "server.started",
+                "server.started/idle",
+                "server.started/retry",
+                "ptt_begin/started",
+            ):
+                if self._vc_record_start_tone_debug():
+                    print("[VC] record_start_tone skip bad_via={}".format(v))
+                return False
+            if not self._vc_play_record_start_tone(via=v):
+                return False
+            self._vc_pending_record_start_tone = 0
+            return True
+        except Exception:
+            return False
+
+    def _vc_play_record_start_tone(self, via=""):
+        """开录提示音 start.mp3;配置为 None 时回退 U:/static/start.mp3。"""
+        try:
+            path = "U:/static/start.mp3"
+            hold_ms = 180
+            if "vc_config" in globals() and vc_config:
+                cfg = vc_config.get_attr("RECORD_START_TONE_FILE", None)
+                if cfg is not None and str(cfg).strip():
+                    path = str(cfg).strip()
+                hold_ms = int(vc_config.get_int("RECORD_START_TONE_GAP_MS", hold_ms))
+            print("[VC] record_start_tone ready via={} path={}".format(via or "-", path))
+            self._vc_local_play(path, hold_ms=hold_ms)
+            return True
+        except Exception as e:
+            usys.print_exception(e)
+            return False
+
+    _vc_play_ai_key_record_start_tone = _vc_play_record_start_tone
+
+    def _vc_uplink_reason_is_user_key(self, reason):
+        r = str(reason or "")
+        if r.startswith("key.press") or r.startswith("BT_AI_key_press"):
+            return True
+        if r == "canceled" and (
+            getattr(self, "_vc_pending_interrupt_uplink", 0) == 1
+            or getattr(self, "is_ruppt_tts", 0) == 1
+        ):
+            return True
+        return False
+
+    def _vc_on_server_record_uplink_ready(self, via):
+        """WS started / asr.started:服务端允许上行后再滴;PTT 可延后开麦。"""
+        try:
+            self._vc_try_play_record_start_tone_when_ready(via)
+        except Exception:
+            pass
+        if str(via or "") != "asr.started":
+            return
+        try:
+            if getattr(self, "_ptt_defer_speech_until_asr", 0) == 1:
+                self._ptt_defer_speech_until_asr = 0
+                self.VC_Speech_Start_now()
+                print("[PTT] defer_speech -> VC_Speech_Start (asr.started)")
+        except Exception:
+            pass
+
+    def _vc_uplink_start_recording(self, reason=""):
+        """开麦统一出口(按键/打断续聊):会话已 started 时先滴再 VC_Speech_Start。"""
+        try:
+            if (
+                getattr(self, "_vc_session_started", 0) == 1
+                and self._vc_uplink_reason_is_user_key(reason)
+            ):
+                self._vc_try_play_record_start_tone_when_ready("uplink_ready")
+        except Exception:
+            pass
+        try:
+            if getattr(self, "VC_mode", 0) == 0:
+                self._long_dialogue_key_arm = 0
+        except Exception:
+            pass
+        self.VC_Speech_Start_now()
+        try:
+            if getattr(self, "VC_mode", 0) == 0 and hasattr(self, "_long_dialogue_end_interrupt"):
+                self._long_dialogue_end_interrupt()
+        except Exception:
+            pass
+
+    def _vc_fire_local_play_end(self, tag=""):
+        """TTS 本地播完:VC_Play_End + FSM play_end + 模式切换 Close。"""
+        try:
+            if tag:
+                print("[TTS] play_end_{}".format(tag))
+        except Exception:
+            pass
+        EventMesh.publish("publish_AI_btnInfo", {"func": self.VC_Play_End})
+        self._fsm_apply_actions(self.fsm.on_local_play_end())
+        try:
+            self.fsm.reset_rx_phases()
+        except Exception:
+            pass
+        self._tts_first_write_ms = 0
+        self._vc_finish_play_end_close_if_needed()
+
     def _vc_local_play(self, path, play_mode=1, loop=1, hold_ms=650):
         """本地 MP3 短音:播前停录/停流式 TTS,避免与麦克风采集叠用。"""
         try:
@@ -143,65 +283,96 @@ class VCSessionMediaMixin:
         except Exception:
             return ""
     def _vc_long_dialogue_auto_uplink_ok(self, reason):
-        """长对话 canceled/completed 自动开麦:须有有效 ASR,且防抖/勿在 LLM 无 TTS 后抢开。"""
+        """长对话自动/打断开麦:ASR/LLM 门控 + 双轨防抖(自动 1200ms / 用户打断 450ms)。"""
         try:
             if reason not in ("canceled", "completed", "local.play_end", "audio.played"):
                 return True
             if getattr(self, "Close_flag", 0) == 1:
                 return False
-            if reason == "canceled" and getattr(self, "_vc_pending_interrupt_uplink", 0) == 1:
+            user_intr = reason == "canceled" and (
+                getattr(self, "_vc_pending_interrupt_uplink", 0) == 1
+                or getattr(self, "is_ruppt_tts", 0) == 1
+            )
+            if (
+                reason == "canceled"
+                and getattr(self, "VC_record_state_flag", 0) != 0
+                and hasattr(self, "_long_dialogue_is_stale_server_cancel")
+                and self._long_dialogue_is_stale_server_cancel()
+            ):
                 pass
-            elif not self._vc_effective_asr_text():
+            elif not user_intr and not self._vc_effective_asr_text():
                 k = getattr(self, "_last_tts_kind", "") or ""
                 if reason == "local.play_end" and k == "boot_prompt":
                     pass
+                elif (
+                    self._vc_is_long_dialogue_mode()
+                    and reason in ("completed", "canceled")
+                    and (
+                        getattr(self, "_turn_has_vad_silence", 0) == 1
+                        or getattr(self, "_turn_has_asr_completed", 0) == 1
+                    )
+                ):
+                    # 长对话:vad 已结束一轮但 ASR 空,仍回到蓝慢闪聆听(勿停住)
+                    pass
                 else:
-                    try:
-                        print("[VC][uplink_ctrl] Deny START by {} cause=empty_asr".format(reason))
-                    except Exception:
-                        pass
+                    print("[VC][uplink_ctrl] Deny START by {} cause=empty_asr".format(reason))
                     return False
-            if reason == "completed":
-                if getattr(self, "_turn_has_tts_started", 0) == 0:
-                    try:
-                        if getattr(self, "_llm_completed_ms", 0) or getattr(self.fsm, "turn", None) == VCTurnState.Thinking:
-                            print("[VC][uplink_ctrl] Deny START by completed cause=llm_without_tts")
-                            return False
-                    except Exception:
-                        pass
-            try:
-                now = utime.ticks_ms()
-                last = getattr(self, "_vc_last_auto_uplink_ms", 0)
-                if last and utime.ticks_diff(now, last) < 1200:
-                    print("[VC][uplink_ctrl] Deny START by {} cause=auto_uplink_debounce".format(reason))
+            if reason == "completed" and getattr(self, "_turn_has_tts_started", 0) == 0:
+                if getattr(self, "_llm_completed_ms", 0) or getattr(
+                    self.fsm, "turn", None
+                ) == VCTurnState.Thinking:
+                    print("[VC][uplink_ctrl] Deny START by completed cause=llm_without_tts")
                     return False
-                self._vc_last_auto_uplink_ms = now
-            except Exception:
-                pass
+            debounce_ms = 450 if user_intr else 1200
+            last_attr = "_vc_last_interrupt_uplink_ms" if user_intr else "_vc_last_auto_uplink_ms"
+            if "vc_config" in globals() and vc_config:
+                key = "VC_INTERRUPT_UPLINK_DEBOUNCE_MS" if user_intr else "VC_AUTO_UPLINK_DEBOUNCE_MS"
+                debounce_ms = int(vc_config.get_int(key, debounce_ms))
+            now = utime.ticks_ms()
+            last = getattr(self, last_attr, 0)
+            if last and utime.ticks_diff(now, last) < debounce_ms:
+                tag = "interrupt_debounce" if user_intr else "auto_debounce"
+                print("[VC][uplink_ctrl] Deny START by {} cause={} ({}ms)".format(
+                    reason, tag, debounce_ms))
+                return False
+            setattr(self, last_attr, now)
         except Exception:
             pass
         return True
     def _uplink_request_start(self, reason="", *args, **kwargs):
         # 统一入口:避免多处同时 start 造成冲突(尤其是 TTS/播放阶段误开麦导致回声触发与状态紊乱)
+        _key_reasons = (
+            "key.press/continue", "key.press/wake", "key.press/fallback",
+            "AI_VC_start", "server.started", "server.started/idle", "server.started/retry",
+        )
         try:
             if self.Close_flag == 1:
-                try:
-                    if reason in ("canceled", "local.play_end", "audio.played", "key.press/continue"):
-                        print("[VC][uplink_ctrl] Deny START by {} cause=Close_flag".format(reason))
-                except Exception:
-                    pass
-                return
+                if reason in _key_reasons or getattr(self, "_long_dialogue_key_arm", 0) == 1:
+                    try:
+                        self.Close_flag = 0
+                    except Exception:
+                        pass
+                else:
+                    try:
+                        if reason in ("canceled", "local.play_end", "audio.played", "key.press/continue"):
+                            print("[VC][uplink_ctrl] Deny START by {} cause=Close_flag".format(reason))
+                    except Exception:
+                        pass
+                    return
+        except Exception:
+            return
+
+        # 单对话开录走 ptt_begin→VC_Speech_Start_now,不经本处长对话 auto-uplink 门控
+        try:
+            if self._vc_is_ptt_mode():
+                if reason not in ("canceled",):
+                    return
         except Exception:
             return
 
         # 连续对话才允许“自动开新一轮上行”
         try:
-            if self.VC_mode != 0:
-                try:
-                    if reason in ("canceled", "local.play_end", "audio.played"):
-                        print("[VC][uplink_ctrl] Deny START by {} cause=VC_mode({})".format(reason, self.VC_mode))
-                except Exception:
-                    pass
+            if not self._vc_is_long_dialogue_mode():
                 return
         except Exception:
             return
@@ -226,7 +397,9 @@ class VCSessionMediaMixin:
         try:
             if getattr(self.fsm, "session", VCSessionState.Disconnected) != VCSessionState.Started:
                 try:
-                    if reason in ("canceled", "local.play_end", "audio.played"):
+                    if reason in _key_reasons:
+                        print("[VC][uplink_ctrl] Defer START by {} (wait server.started)".format(reason))
+                    elif reason in ("canceled", "local.play_end", "audio.played"):
                         print("[VC][uplink_ctrl] Deny START by {} cause=fsm.session".format(reason))
                 except Exception:
                     pass
@@ -235,11 +408,20 @@ class VCSessionMediaMixin:
             # 仅 Listening/SpeechEnded 允许开麦进入下一轮;其它阶段一律拒绝(避免抢跑打乱链路)
             if turn not in (VCTurnState.Listening, VCTurnState.SpeechEnded):
                 try:
-                    if reason in ("canceled", "local.play_end", "audio.played"):
-                        print("[VC][uplink_ctrl] Deny START by {} cause=fsm.turn({})".format(reason, turn))
+                    coerce = reason in _key_reasons or (
+                        reason == "canceled"
+                        and getattr(self, "_vc_pending_interrupt_uplink", 0) == 1
+                    )
+                    if getattr(self, "VC_mode", 0) == 0 and coerce:
+                        self.fsm.turn = VCTurnState.Listening
+                        turn = VCTurnState.Listening
+                        print("[VC][uplink_ctrl] coerce turn->Listening for {}".format(reason))
+                    else:
+                        if reason in _key_reasons or reason in ("canceled", "local.play_end", "audio.played"):
+                            print("[VC][uplink_ctrl] Deny START by {} cause=fsm.turn({})".format(reason, turn))
+                        return
                 except Exception:
-                    pass
-                return
+                    return
         except Exception:
             return
 
@@ -265,13 +447,34 @@ class VCSessionMediaMixin:
             self._server_vad_silence = 0
 
         print("[VC][uplink_ctrl] Request START by {}".format(reason))
+        try:
+            if (
+                reason == "canceled"
+                and getattr(self, "VC_mode", 0) == 0
+                and (
+                    getattr(self, "_vc_pending_interrupt_uplink", 0) == 1
+                    or getattr(self, "is_ruppt_tts", 0) == 1
+                )
+                and getattr(self, "VC_record_state_flag", 0) != 0
+            ):
+                print("[VC][uplink_ctrl] interrupt -> already recording, skip restart")
+                try:
+                    self._long_dialogue_key_arm = 0
+                    if hasattr(self, "_long_dialogue_end_interrupt"):
+                        self._long_dialogue_end_interrupt()
+                    elif hasattr(self, "_long_dialogue_publish_listening"):
+                        self._long_dialogue_publish_listening("canceled/already_rec")
+                except Exception:
+                    pass
+                return
+        except Exception:
+            pass
         try:
             if reason == "canceled" and getattr(self, "_vc_pending_interrupt_uplink", 0) == 1:
-                self._vc_pending_interrupt_uplink = 0
-                print("[VC][uplink_ctrl] interrupt_uplink armed -> start (canceled)")
+                print("[VC][uplink_ctrl] interrupt -> start (canceled)")
         except Exception:
             pass
-        self.VC_Speech_Start_now()
+        self._vc_uplink_start_recording(reason)
     def _uplink_request_stop(self, reason="", *args, **kwargs):
         # 统一入口:避免多处同时 stop 造成冲突
         if getattr(self, "record_data_save_flag", 0) == 0:
@@ -376,7 +579,7 @@ class VCSessionMediaMixin:
                 pass
             try:
                 if getattr(self, "VC_record_state_flag", 0) != 0:
-                    self._vc_publish_led("listening", "audio_cb/finish/recording")
+                    self._ptt_publish_listening_fast("audio_cb/finish/recording")
                     return
             except Exception:
                 pass
@@ -388,7 +591,8 @@ class VCSessionMediaMixin:
             except Exception:
                 pass
             try:
-                if getattr(self, "_ptt_led_lock_listening", 0) == 1:
+                if self._vc_should_hold_listening_led():
+                    self._vc_publish_listening_for_mode("audio_cb/finish/listen_hold")
                     return
             except Exception:
                 pass
@@ -449,7 +653,27 @@ class VCSessionMediaMixin:
                                 usys.print_exception(e)
                     elif self.VC_record_TYPE == VC_record_TYPE_PCM:
                         print("record_pcm.stream_read_start()!!!!!!!!!!!!")
-                        self.record.stream_start(audio.Record.WAV, self.VC_record_samplerate, 0)
+                        _rec_started = 0
+                        for _ri in range(2):
+                            try:
+                                self.record.stream_start(audio.Record.WAV, self.VC_record_samplerate, 0)
+                                _rec_started = 1
+                                break
+                            except Exception as e:
+                                _msg = str(e)
+                                if "busy" in _msg and _ri == 0:
+                                    try:
+                                        self.record.stream_stop()
+                                    except Exception:
+                                        pass
+                                    try:
+                                        utime.sleep_ms(40)
+                                    except Exception:
+                                        pass
+                                    continue
+                                raise e
+                        if not _rec_started:
+                            raise Exception("record stream_start failed")
                         # if self.record_pcm != None:
                         #     try:
                         #         self.record_pcm.stream_stop()
@@ -478,7 +702,7 @@ class VCSessionMediaMixin:
                     pass
                 # 录音开始:聆听灯(PTT 蓝快闪 / 长对话蓝慢闪)
                 try:
-                    self._vc_publish_led("listening", "VC_Speech_Start")
+                    self._vc_publish_listening_for_mode("VC_Speech_Start")
                 except Exception:
                     pass
                 try:
@@ -543,14 +767,23 @@ class VCSessionMediaMixin:
                 self.AI_key_long_press_flag = 0
         except Exception:
             pass
-        # å½\95é\9f³å\81\9cæ­¢ï¼\9aå\8d\95对è¯\9dè¿\9bå\85¥æ\80\9dè\80\83æ\80\81ï¼\88ç­\89æ\9c\8då\8a¡å\99¨ï¼\89ï¼\8cå\8b¿ç»§ç»­è\93\9dé\97ªè\81\86å\90¬
+        # å\81\9cå½\95å\90\8eç\81¯æ\80\81ï¼\9aPTT æ\9d¾æ\89\8bâ\86\92æ\80\9dè\80\83(è\93\9d绿交æ\9b¿)ï¼\9bé\95¿å¯¹è¯\9d vad å\81\9c麦â\86\92ä»\8dè\93\9dæ\85¢é\97ªï¼\8cç­\89 llm.created å\86\8dæ\80\9dè\80\83
         try:
             if getattr(self, "VC_mode", -1) == 1:
+                self._ptt_led_lock_listening = 0
+                self._ptt_interrupt_active = 0
                 self._ptt_await_server = 1
                 self._vc_publish_led("thinking", "VC_Speech_End/ptt")
             else:
                 self._ptt_await_server = 0
-                self._vc_publish_led("idle", "VC_Speech_End")
+                if (
+                    getattr(self, "_vc_session_started", 0) == 1
+                    and getattr(self, "Close_flag", 0) == 0
+                    and getattr(self, "AI_client_VC_isconnect", 0) == 1
+                ):
+                    self._vc_publish_listening_for_mode("VC_Speech_End/long_wait")
+                else:
+                    self._vc_publish_led("idle", "VC_Speech_End")
         except Exception:
             pass
         print("[VC] Speech_End record_state={}".format(self.VC_record_state_flag))
@@ -918,13 +1151,10 @@ class VCSessionMediaMixin:
                 if self._vc_led_state_debug_enabled():
                     print("[VC] VC_Play_Start skip (ptt_interrupt)")
                 return
-        except Exception:
-            pass
-        self.tts_in_data_len = 0
-        self.tts_out_data_len = 0
-        self._tts_first_write_ms = 0
-        try:
-            self._tts_completed_wall_ms = 0
+            if hasattr(self, "_long_dialogue_should_drop_inbound_tts") and self._long_dialogue_should_drop_inbound_tts():
+                if self._vc_led_state_debug_enabled():
+                    print("[VC] VC_Play_Start skip (long_interrupt)")
+                return
         except Exception:
             pass
         if self.VC_Play_flag == 0:
@@ -943,10 +1173,30 @@ class VCSessionMediaMixin:
                 except Exception:
                     pass
                 # 资源互斥:播放前确保录音上行已停止(无 AEC 场景)
+                # 长对话打断后已开麦:迟到 TTS 不得抢停录
                 try:
                     if self.record_data_save_flag == 0 and self.VC_record_state_flag != 0:
-                        self._uplink_request_stop("VC_Play_Start")
-                        utime.sleep_ms(30)
+                        _skip_stop = False
+                        if getattr(self, "VC_mode", 0) == 0 and hasattr(
+                            self, "_long_dialogue_should_drop_inbound_tts"
+                        ):
+                            if self._long_dialogue_should_drop_inbound_tts():
+                                if self._vc_led_state_debug_enabled():
+                                    print("[VC] VC_Play_Start skip stop_rec (long_drop)")
+                                return
+                            settled = getattr(self, "_long_dialogue_interrupt_settled_ms", 0)
+                            win_ms = 8000
+                            try:
+                                win_ms = self._long_dialogue_drop_tts_window_ms()
+                            except Exception:
+                                pass
+                            if settled and utime.ticks_diff(utime.ticks_ms(), settled) < win_ms:
+                                _skip_stop = True
+                                if self._vc_led_state_debug_enabled():
+                                    print("[VC] VC_Play_Start skip stop_rec (long_after_intr)")
+                        if not _skip_stop:
+                            self._uplink_request_stop("VC_Play_Start")
+                            utime.sleep_ms(30)
                 except Exception:
                     pass
                 if self.VC_play_TYPE == VC_play_TYPE_PCM:
@@ -1018,11 +1268,18 @@ class VCSessionMediaMixin:
 
         # TTS 播完:按真实 FSM/录音态恢复灯;本地短音不走此路径
         try:
-            if getattr(self, "_vc_play_end_suppress_idle_led", 0):
-                if self._vc_led_state_debug_enabled():
+            if self._ptt_should_force_idle_led():
+                self._vc_publish_led("idle", "VC_Play_End/ptt_stop")
+            elif getattr(self, "_vc_play_end_suppress_idle_led", 0) or self._vc_should_hold_listening_led():
+                if self._vc_should_hold_listening_led():
+                    self._vc_publish_listening_for_mode("VC_Play_End/listen_hold")
+                elif self._vc_led_state_debug_enabled():
                     print("[LED] VC_Play_End skip led (suppress)")
             elif getattr(self, "VC_record_state_flag", 0) != 0:
-                self._vc_publish_led("listening", "VC_Play_End/recording")
+                if getattr(self, "VC_mode", 0) == 0:
+                    self._vc_publish_listening_for_mode("VC_Play_End/recording")
+                else:
+                    self._ptt_publish_listening_fast("VC_Play_End/recording")
             else:
                 self._vc_led_sync_from_fsm()
         except Exception:
@@ -1055,6 +1312,27 @@ class VCSessionMediaMixin:
             return 0
         return self._PCM_STREAM_RING_BYTES - pcm_free
 
+    def _vc_finish_play_end_close_if_needed(self):
+        """旧版 LocalRespondingEnded:Close_flag==1 时提示音/模式切换播完应 Close 会话。"""
+        try:
+            if getattr(self, "Close_flag", 0) != 1:
+                return
+            self.Close_flag = 0
+            print("[VC] play_end -> Close (Close_flag)")
+            EventMesh.publish("publish_AI_btnInfo", {"func": self.Close})
+        except Exception as e:
+            usys.print_exception(e)
+
+    def _vc_tts_rx_counters_reset(self):
+        """新一轮 tts.started:重置下行计数;勿在 VC_Play_Start 清零(会丢掉先入队包的字节统计)。"""
+        self.tts_in_data_len = 0
+        self.tts_out_data_len = 0
+        self._tts_first_write_ms = 0
+        try:
+            self._tts_completed_wall_ms = 0
+        except Exception:
+            pass
+
     def _vc_try_play_end_tick(self):
         """tts.completed 后判定本地 PCM 是否真正播完,避免过早 VC_Play_End 截断 TTS。"""
         try:
@@ -1068,7 +1346,70 @@ class VCSessionMediaMixin:
                 q_empty = True
             tts_done_ms = getattr(self, "_tts_completed_wall_ms", 0) or getattr(
                 self.fsm, "last_tts_completed_ms", 0)
-            if not (in_len > 0 and out_len >= in_len and q_empty and tts_done_ms):
+            if not (q_empty and tts_done_ms):
+                return False
+            now = utime.ticks_ms()
+            try:
+                since_done = utime.ticks_diff(now, tts_done_ms)
+            except Exception:
+                since_done = 0
+            # 零包 completed:仅当从未收到下行音频包且队列已空(避免截断迟到的 TTS)
+            if in_len == 0 and out_len == 0:
+                if not q_empty:
+                    return False
+                if getattr(self, "_tts_first_write_ms", 0):
+                    return False
+                if since_done < 600:
+                    return False
+                is_pcm_empty_z = (self.play_pcm is None) or (
+                    self.play_pcm is not None and self.play_pcm.stream_is_empty() == 1
+                )
+                pcm_pending_z = 0
+                if self.play_pcm is not None:
+                    try:
+                        pcm_pending_z = self._vc_pcm_stream_pending_bytes(
+                            self.play_pcm.stream_get_buf_len())
+                    except Exception:
+                        pcm_pending_z = 0
+                if (not is_pcm_empty_z) or pcm_pending_z > 0:
+                    if since_done < 5000:
+                        return False
+                self._vc_fire_local_play_end(
+                    "zero_bytes since_done={}ms".format(since_done))
+                return True
+            if in_len <= 0:
+                return False
+            # 允许少量未写入(常见 1~3%);环形缓冲待播计入已播进度
+            slack = 8192
+            try:
+                if in_len > 4096:
+                    slack = max(slack, in_len // 25)
+            except Exception:
+                pass
+            try:
+                if self.play_pcm is not None:
+                    pcm_free_pre = self.play_pcm.stream_get_buf_len()
+                else:
+                    pcm_free_pre = 0
+            except Exception:
+                pcm_free_pre = 0
+            pcm_pending_pre = self._vc_pcm_stream_pending_bytes(pcm_free_pre)
+            if pcm_pending_pre < 0:
+                pcm_pending_pre = max(0, in_len - out_len)
+            played_ok = (out_len >= in_len) or (
+                (in_len - out_len) <= slack
+            ) or (out_len + pcm_pending_pre >= in_len - slack)
+            if not played_ok:
+                try:
+                    if not getattr(self, "_tts_play_check_log_ms", 0) or utime.ticks_diff(
+                        utime.ticks_ms(), self._tts_play_check_log_ms
+                    ) >= 800:
+                        self._tts_play_check_log_ms = utime.ticks_ms()
+                        print(
+                            "[TTS] play_end_wait bytes in={} out={} slack={} pending={}".format(
+                                in_len, out_len, slack, pcm_pending_pre))
+                except Exception:
+                    pass
                 return False
 
             is_pcm_empty = (self.play_pcm is None) or (
@@ -1084,7 +1425,6 @@ class VCSessionMediaMixin:
             if pcm_pending < 0:
                 pcm_pending = max(0, in_len - out_len)
 
-            now = utime.ticks_ms()
             bps = self._vc_tts_bytes_per_sec()
             fw = getattr(self, "_tts_first_write_ms", 0)
             # 丢弃明显陈旧的 completed 时间(上轮残留或 last_tts_completed_ms==1 等)
@@ -1109,6 +1449,16 @@ class VCSessionMediaMixin:
             # 与旧 ai_VC LocalRespondingEnded 一致:stream 空且环形缓冲无待播数据
             pcm_drained = is_pcm_empty and pcm_pending == 0
             ready = pcm_drained and tail_ok
+            # 服务端 completed 后长时间仍不满足:队列已空则强制收尾,避免绿灯常亮不开麦
+            if not ready and q_empty and tts_done_ms:
+                try:
+                    if since_done >= 15000 and pcm_pending <= slack:
+                        print(
+                            "[TTS] play_end_force since_done={}ms pcm_pending={}".format(
+                                since_done, pcm_pending))
+                        ready = True
+                except Exception:
+                    pass
 
             if not ready:
                 try:
@@ -1128,23 +1478,9 @@ class VCSessionMediaMixin:
                     pass
                 return False
 
-            try:
-                print(
-                    "[TTS] play_end_fire since_done={}ms tail~{}ms "
-                    "in={} out={} pcm_free={} pcm_pending={} pcm_empty={}".format(
-                        since_done, tail_ms, in_len, out_len, pcm_free, pcm_pending,
-                        1 if is_pcm_empty else 0,
-                    )
-                )
-            except Exception:
-                pass
-            EventMesh.publish("publish_AI_btnInfo", {"func": self.VC_Play_End})
-            self._fsm_apply_actions(self.fsm.on_local_play_end())
-            try:
-                self.fsm.reset_rx_phases()
-            except Exception:
-                pass
-            self._tts_first_write_ms = 0
+            self._vc_fire_local_play_end(
+                "fire since_done={}ms tail~{}ms in={} out={}".format(
+                    since_done, tail_ms, in_len, out_len))
             return True
         except Exception as e:
             usys.print_exception(e)
@@ -1174,16 +1510,6 @@ class VCSessionMediaMixin:
                           "(等对端 started 后再开麦)".format(self._vc_session_started, VC_PROTO_GUARDS))
                 except Exception:
                     pass
-        
-
-            
-            
-        
-        
-        try:
-            self.fsm.on_server_asr_started()
-        except Exception:
-            pass
     def AI_client_VC_end_speech(self):
         json_data = \
         {
@@ -1608,6 +1934,11 @@ class VCSessionMediaMixin:
                         try:
                             if self._ptt_should_drop_inbound_tts():
                                 continue
+                            if (
+                                hasattr(self, "_long_dialogue_should_drop_inbound_tts")
+                                and self._long_dialogue_should_drop_inbound_tts()
+                            ):
+                                continue
                         except Exception:
                             pass
                         for i in range(5):