From be3532d4b147e4068c4e6dd68ea92be9d86946a2 Mon Sep 17 00:00:00 2001 From: iammm0 Date: Tue, 10 Feb 2026 17:09:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=89=A9=E5=B1=95=E5=90=8E=E7=AB=AFWeb?= =?UTF-8?q?Socket=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 优化api/routers/websocket.py Co-authored-by: Cursor --- api/routers/websocket.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/api/routers/websocket.py b/api/routers/websocket.py index 796abb7..3e124fb 100644 --- a/api/routers/websocket.py +++ b/api/routers/websocket.py @@ -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"