mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-25 08:33:53 +08:00
Merge pull request #3196 from xinnan-tech/py-device-calling
Py device calling
This commit is contained in:
@@ -190,6 +190,9 @@ class ConnectionHandler:
|
||||
# 初始化提示词管理器
|
||||
self.prompt_manager = PromptManager(self.config, self.logger)
|
||||
|
||||
# 初始化通话状态
|
||||
self.calling = False
|
||||
|
||||
async def handle_connection(self, ws: websockets.ServerConnection):
|
||||
try:
|
||||
# 获取运行中的事件循环(必须在异步上下文中)
|
||||
|
||||
@@ -47,7 +47,8 @@ async def sendAudioMessage(conn: "ConnectionHandler", sentenceType, audios, text
|
||||
conn.logger.bind(tag=TAG).info(f"发送音频消息: {sentenceType}, {text}")
|
||||
|
||||
# 发送结束消息(如果是最后一个文本)
|
||||
if sentenceType == SentenceType.LAST:
|
||||
# 通话需要维持speaking状态
|
||||
if not conn.calling and sentenceType == SentenceType.LAST:
|
||||
await send_tts_message(conn, "stop", None)
|
||||
if conn.close_after_chat:
|
||||
await conn.close()
|
||||
|
||||
@@ -1,17 +1,21 @@
|
||||
import time
|
||||
import uuid
|
||||
import asyncio
|
||||
from typing import Dict, Any, TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from core.connection import ConnectionHandler
|
||||
|
||||
from core.utils.dialogue import Message
|
||||
from core.providers.asr.dto.dto import InterfaceType
|
||||
from core.handle.receiveAudioHandle import startToChat
|
||||
from core.handle.reportHandle import enqueue_asr_report
|
||||
from core.handle.sendAudioHandle import send_stt_message, send_tts_message
|
||||
from core.handle.textMessageHandler import TextMessageHandler
|
||||
from core.handle.textMessageType import TextMessageType
|
||||
from core.utils.util import remove_punctuation_and_length
|
||||
from core.providers.asr.dto.dto import InterfaceType
|
||||
from core.providers.tts.dto.dto import ContentType, TTSMessageDTO, SentenceType
|
||||
|
||||
|
||||
TAG = __name__
|
||||
|
||||
@@ -54,6 +58,25 @@ class ListenTextMessageHandler(TextMessageHandler):
|
||||
original_text
|
||||
)
|
||||
|
||||
# 检查是否是设备呼叫指令 [device_call]
|
||||
if original_text.startswith("[device_call]"):
|
||||
# 提取 tag 后的文本
|
||||
call_text = original_text[len("[device_call]"):].strip()
|
||||
conn.logger.bind(tag=TAG).info(f"收到设备呼叫指令: {call_text}")
|
||||
|
||||
# 准备开始新会话
|
||||
conn.sentence_id = uuid.uuid4().hex
|
||||
|
||||
await send_stt_message(conn, call_text)
|
||||
conn.tts.store_tts_text(conn.sentence_id, call_text)
|
||||
conn.tts.tts_text_queue.put(TTSMessageDTO(sentence_id=conn.sentence_id, sentence_type=SentenceType.FIRST, content_type=ContentType.ACTION))
|
||||
conn.tts.tts_one_sentence(conn, ContentType.TEXT, content_detail=call_text)
|
||||
conn.tts.tts_text_queue.put(TTSMessageDTO(sentence_id=conn.sentence_id, sentence_type=SentenceType.LAST, content_type=ContentType.ACTION))
|
||||
|
||||
# 添加到对话历史,让模型理解上下文
|
||||
conn.dialogue.put(Message(role="assistant", content=call_text))
|
||||
return
|
||||
|
||||
# 识别是否是唤醒词
|
||||
is_wakeup_words = filtered_text in conn.config.get("wakeup_words")
|
||||
# 是否开启唤醒词回复
|
||||
|
||||
Reference in New Issue
Block a user