diff --git a/main/xiaozhi-server/core/connection.py b/main/xiaozhi-server/core/connection.py index f14f8ec62..0ccc99e7b 100644 --- a/main/xiaozhi-server/core/connection.py +++ b/main/xiaozhi-server/core/connection.py @@ -1122,13 +1122,19 @@ class ConnectionHandler: # 仅在该说话人首次出现时把身份注入 system,之后靠对话历史首轮保留, # 避免每轮在 system 重复出现名字诱导模型反复称呼 speaker_for_system = None - # 展示名优先取本轮 speaker 快照:manual 模式下两轮 chat 线程可能并发, - # conn.current_speaker 已被下一轮覆盖,连接态仅在无快照路径作兜底 + # 展示名只取本轮数据(声纹快照 → query JSON),不读并发下会被覆盖的连接态 cs = "" if speaker is not None: cs = (speaker.name or "").strip() - if not cs: - cs = (self.current_speaker or "").strip() + if not cs and depth == 0 and query: + q = query.strip() + if q.startswith("{") and q.endswith("}"): + try: + parsed = json.loads(q) + if isinstance(parsed, dict): + cs = (parsed.get("speaker") or "").strip() + except (json.JSONDecodeError, TypeError, ValueError): + pass if cs and cs != UNKNOWN_SPEAKER_NAME and cs not in self.system_introduced_speakers: self.system_introduced_speakers.add(cs) speaker_for_system = cs