feat: 扩展后端WebSocket处理

- 优化api/routers/websocket.py

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
iammm0
2026-02-10 17:09:48 +08:00
parent 0b7bd37d5d
commit be3532d4b1

View File

@@ -30,6 +30,7 @@ class MessageType(str, Enum):
CONNECT = "connect"
AUDIO_CHUNK = "audio_chunk"
AUDIO_MESSAGE = "audio_message" # 完整音频消息(类似微信语音)
TRANSCRIBE_ONLY = "transcribe_only" # 仅转写,不落库、不触发 Agent用于「转文字」发送
TEXT = "text" # 文本消息
TRANSCRIPT = "transcript" # 语音转文字结果
AGENT_RESPONSE = "agent_response"
@@ -280,6 +281,33 @@ async def websocket_endpoint(
"timestamp": datetime.now(timezone.utc).isoformat()
})
elif msg_type == MessageType.TRANSCRIBE_ONLY:
# 仅转写:不落库、不触发 Agent用于客户端「转文字」后发文本
data = message.get("data", {})
audio_base64 = data.get("audio_base64", "")
if not audio_base64:
await manager.send_message(conversation_id, {
"type": MessageType.ERROR,
"data": {"message": "缺少 audio_base64"},
"timestamp": datetime.now(timezone.utc).isoformat()
})
continue
try:
transcript_text = await asr_service.transcribe(audio_base64)
await manager.send_message(conversation_id, {
"type": MessageType.TRANSCRIPT,
"conversation_id": conversation_id,
"data": {"text": transcript_text or ""},
"timestamp": datetime.now(timezone.utc).isoformat()
})
except Exception as e:
logger.error(f"仅转写失败: {e}", exc_info=True)
await manager.send_message(conversation_id, {
"type": MessageType.ERROR,
"data": {"message": f"转写失败: {str(e)}"},
"timestamp": datetime.now(timezone.utc).isoformat()
})
elif msg_type == MessageType.END_CONVERSATION:
# 结束对话
conversation.status = "ended"