rtc-voice-chat/backend/routes/v1/scenes.py
2026-04-02 20:15:15 +08:00

100 lines
3.3 KiB
Python
Raw Permalink 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.

"""
POST /getScenes — 场景列表
"""
from fastapi import APIRouter, Request
from fastapi.responses import JSONResponse
from security.internal_auth import verify_internal_request
from services.scene_service import Scenes, prepare_scene_runtime
from services.session_store import save_session
router = APIRouter(tags=["场景"])
@router.post(
"/getScenes",
summary="获取场景列表",
description=(
"返回所有已配置场景的信息及对应的 RTC 房间参数AppId / RoomId / Token 等)。\n\n"
"调用后会将本次生成的 `RoomId / UserId / TaskId` 写入 Session"
"供后续 `/proxy?Action=StartVoiceChat` 自动取用,无需客户端传递。\n\n"
"**鉴权**:需附加内部服务签名 Header由 java-mock 自动添加)。"
),
responses={
401: {"description": "内部签名校验失败"},
},
)
async def get_scenes(request: Request):
if not verify_internal_request(request.headers):
return JSONResponse({"code": 401, "message": "鉴权失败"}, status_code=401)
try:
scenes_list = []
for scene_name, data in Scenes.items():
scene_config, rtc_config, voice_chat = prepare_scene_runtime(
scene_name, data
)
scene_config["id"] = scene_name
scene_config["botName"] = voice_chat.get("AgentConfig", {}).get("UserId")
scene_config["isInterruptMode"] = (
voice_chat.get("Config", {}).get("InterruptMode") == 0
)
scene_config["isVision"] = (
voice_chat.get("Config", {})
.get("LLMConfig", {})
.get("VisionConfig", {})
.get("Enable")
)
scene_config["isScreenMode"] = (
voice_chat.get("Config", {})
.get("LLMConfig", {})
.get("VisionConfig", {})
.get("SnapshotConfig", {})
.get("StreamType")
== 1
)
scene_config["isAvatarScene"] = (
voice_chat.get("Config", {}).get("AvatarConfig", {}).get("Enabled")
)
scene_config["avatarBgUrl"] = (
voice_chat.get("Config", {})
.get("AvatarConfig", {})
.get("BackgroundUrl")
)
rtc_out = {k: v for k, v in rtc_config.items() if k != "AppKey"}
rtc_out["TaskId"] = voice_chat.get("TaskId", "")
# 存储本次生成的房间信息,供后续 StartVoiceChat 使用
save_session(request, scene_name, {
"RoomId": rtc_config.get("RoomId", ""),
"UserId": rtc_config.get("UserId", ""),
"TaskId": voice_chat.get("TaskId", ""),
})
scenes_list.append(
{
"scene": scene_config,
"rtc": rtc_out,
}
)
return JSONResponse(
{
"ResponseMetadata": {"Action": "getScenes"},
"Result": {"scenes": scenes_list},
}
)
except ValueError as e:
return JSONResponse(
{
"ResponseMetadata": {
"Action": "getScenes",
"Error": {"Code": -1, "Message": str(e)},
}
}
)