Files
banban/talkingq-url/handlers/response_processor.py
2026-03-24 15:04:36 +08:00

65 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import asyncio
from typing import List, Dict
from services.audio_session import AudioSession
from handlers.prompt_sound_handler import handle_prompt_sound_request
from handlers.response_coordinator import process_response
from interfaces.llm import LLM
from interfaces.tts import TTS
from utils.logger import session_logger
from services.tts_error_manager import tts_error_manager
from implementations.minimax_tts import MiniMaxTTS
async def generate_and_process_response(
transcript: str,
history: List[Dict[str, str]],
selected_role: dict,
device_id: str,
session_id: str,
device_history,
session: AudioSession,
language: str = None, # 默认为None
):
llm_service: LLM = session.llm_service
if hasattr(llm_service, "device_id"):
llm_service.device_id = device_id
if hasattr(llm_service, "session_id"):
llm_service.session_id = session_id
tts_service: TTS = session.tts_service
if not isinstance(tts_service, MiniMaxTTS):
session_logger.warning(
device_id, session_id, "检测到TTS服务不是MiniMaxTTS正在切换到MiniMaxTTS"
)
tts_service = MiniMaxTTS(selected_role=selected_role)
session.tts_service = tts_service
try:
if isinstance(language, (int, float)) or (isinstance(language, str) and not language.isalpha()):
session_logger.warning(
device_id, session_id, f"检测到无效的语言代码: {language},将使用默认语言"
)
language = "zh" # 使用默认语言
await process_response(
transcript,
history,
selected_role,
device_id,
session_id,
device_history,
session,
language, # 传递合法化后的语言参数
)
except Exception as e:
session_logger.error(
device_id, session_id, f"生成或处理回复时出错: {e}", exc_info=True
)
if session.websocket and session.websocket.client_state.name == "CONNECTED":
try:
await session.websocket.send_text("TTS_ERROR")
except Exception as ws_error:
session_logger.error(device_id, session_id, f"发送TTS错误通知失败: {ws_error}")
await handle_prompt_sound_request(device_id, "tts_error")
finally:
await tts_error_manager.end_tts_session(device_id, session_id)