- 新增数据库迁移脚本 (practice_rooms, practice_room_messages) - 新增后端 API: 房间创建/加入/消息同步/报告生成 - 新增前端页面: 入口页/对练房间/报告页 - 新增 AI 双人评估服务和提示词
This commit is contained in:
323
backend/app/services/ai/duo_practice_analysis_service.py
Normal file
323
backend/app/services/ai/duo_practice_analysis_service.py
Normal file
@@ -0,0 +1,323 @@
|
||||
"""
|
||||
双人对练分析服务
|
||||
|
||||
功能:
|
||||
- 分析双人对练对话
|
||||
- 生成双方评估报告
|
||||
- 对话标注和建议
|
||||
"""
|
||||
import json
|
||||
import logging
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from app.services.ai.ai_service import AIService
|
||||
from app.services.ai.prompts.duo_practice_prompts import SYSTEM_PROMPT, USER_PROMPT
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass
|
||||
class UserEvaluation:
|
||||
"""用户评估结果"""
|
||||
user_name: str
|
||||
role_name: str
|
||||
total_score: int
|
||||
dimensions: Dict[str, Dict[str, Any]]
|
||||
highlights: List[str]
|
||||
improvements: List[Dict[str, str]]
|
||||
|
||||
|
||||
@dataclass
|
||||
class DuoPracticeAnalysisResult:
|
||||
"""双人对练分析结果"""
|
||||
# 整体评估
|
||||
interaction_quality: int = 0
|
||||
scene_restoration: int = 0
|
||||
overall_comment: str = ""
|
||||
|
||||
# 用户A评估
|
||||
user_a_evaluation: Optional[UserEvaluation] = None
|
||||
|
||||
# 用户B评估
|
||||
user_b_evaluation: Optional[UserEvaluation] = None
|
||||
|
||||
# 对话标注
|
||||
dialogue_annotations: List[Dict[str, Any]] = field(default_factory=list)
|
||||
|
||||
# AI 元数据
|
||||
raw_response: str = ""
|
||||
ai_provider: str = ""
|
||||
ai_model: str = ""
|
||||
ai_latency_ms: int = 0
|
||||
|
||||
|
||||
class DuoPracticeAnalysisService:
|
||||
"""
|
||||
双人对练分析服务
|
||||
|
||||
使用示例:
|
||||
```python
|
||||
service = DuoPracticeAnalysisService()
|
||||
result = await service.analyze(
|
||||
scene_name="销售场景",
|
||||
scene_background="客户咨询产品",
|
||||
role_a_name="销售顾问",
|
||||
role_b_name="顾客",
|
||||
user_a_name="张三",
|
||||
user_b_name="李四",
|
||||
dialogue_history=dialogue_list,
|
||||
duration_seconds=300,
|
||||
total_turns=20
|
||||
)
|
||||
```
|
||||
"""
|
||||
|
||||
MODULE_CODE = "duo_practice_analysis"
|
||||
|
||||
async def analyze(
|
||||
self,
|
||||
scene_name: str,
|
||||
scene_background: str,
|
||||
role_a_name: str,
|
||||
role_b_name: str,
|
||||
role_a_description: str,
|
||||
role_b_description: str,
|
||||
user_a_name: str,
|
||||
user_b_name: str,
|
||||
dialogue_history: List[Dict[str, Any]],
|
||||
duration_seconds: int,
|
||||
total_turns: int,
|
||||
db: Any = None
|
||||
) -> DuoPracticeAnalysisResult:
|
||||
"""
|
||||
分析双人对练
|
||||
|
||||
Args:
|
||||
scene_name: 场景名称
|
||||
scene_background: 场景背景
|
||||
role_a_name: 角色A名称
|
||||
role_b_name: 角色B名称
|
||||
role_a_description: 角色A描述
|
||||
role_b_description: 角色B描述
|
||||
user_a_name: 用户A名称
|
||||
user_b_name: 用户B名称
|
||||
dialogue_history: 对话历史列表
|
||||
duration_seconds: 对练时长(秒)
|
||||
total_turns: 总对话轮次
|
||||
db: 数据库会话
|
||||
|
||||
Returns:
|
||||
DuoPracticeAnalysisResult: 分析结果
|
||||
"""
|
||||
try:
|
||||
logger.info(f"开始双人对练分析: {scene_name}, 轮次={total_turns}")
|
||||
|
||||
# 格式化对话历史
|
||||
dialogue_text = self._format_dialogue_history(dialogue_history)
|
||||
|
||||
# 创建 AI 服务
|
||||
ai_service = AIService(module_code=self.MODULE_CODE, db_session=db)
|
||||
|
||||
# 构建用户提示词
|
||||
user_prompt = USER_PROMPT.format(
|
||||
scene_name=scene_name,
|
||||
scene_background=scene_background or "未设置",
|
||||
role_a_name=role_a_name,
|
||||
role_b_name=role_b_name,
|
||||
role_a_description=role_a_description or f"扮演{role_a_name}角色",
|
||||
role_b_description=role_b_description or f"扮演{role_b_name}角色",
|
||||
user_a_name=user_a_name,
|
||||
user_b_name=user_b_name,
|
||||
dialogue_history=dialogue_text,
|
||||
duration_seconds=duration_seconds,
|
||||
total_turns=total_turns
|
||||
)
|
||||
|
||||
# 调用 AI
|
||||
messages = [
|
||||
{"role": "system", "content": SYSTEM_PROMPT},
|
||||
{"role": "user", "content": user_prompt}
|
||||
]
|
||||
|
||||
ai_response = await ai_service.chat(
|
||||
messages=messages,
|
||||
model="gemini-3-flash-preview", # 使用快速模型
|
||||
temperature=0.3,
|
||||
prompt_name="duo_practice_analysis"
|
||||
)
|
||||
|
||||
logger.info(f"AI 分析完成: provider={ai_response.provider}, latency={ai_response.latency_ms}ms")
|
||||
|
||||
# 解析 AI 输出
|
||||
result = self._parse_analysis_result(
|
||||
ai_response.content,
|
||||
user_a_name=user_a_name,
|
||||
user_b_name=user_b_name,
|
||||
role_a_name=role_a_name,
|
||||
role_b_name=role_b_name
|
||||
)
|
||||
|
||||
# 填充 AI 元数据
|
||||
result.raw_response = ai_response.content
|
||||
result.ai_provider = ai_response.provider
|
||||
result.ai_model = ai_response.model
|
||||
result.ai_latency_ms = ai_response.latency_ms
|
||||
|
||||
return result
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"双人对练分析失败: {e}", exc_info=True)
|
||||
# 返回空结果
|
||||
return DuoPracticeAnalysisResult(
|
||||
overall_comment=f"分析失败: {str(e)}"
|
||||
)
|
||||
|
||||
def _format_dialogue_history(self, dialogues: List[Dict[str, Any]]) -> str:
|
||||
"""格式化对话历史"""
|
||||
lines = []
|
||||
for d in dialogues:
|
||||
speaker = d.get("role_name") or d.get("speaker", "未知")
|
||||
content = d.get("content", "")
|
||||
seq = d.get("sequence", 0)
|
||||
lines.append(f"[{seq}] {speaker}:{content}")
|
||||
return "\n".join(lines)
|
||||
|
||||
def _parse_analysis_result(
|
||||
self,
|
||||
ai_output: str,
|
||||
user_a_name: str,
|
||||
user_b_name: str,
|
||||
role_a_name: str,
|
||||
role_b_name: str
|
||||
) -> DuoPracticeAnalysisResult:
|
||||
"""解析 AI 输出"""
|
||||
result = DuoPracticeAnalysisResult()
|
||||
|
||||
try:
|
||||
# 尝试提取 JSON
|
||||
json_str = ai_output
|
||||
|
||||
# 如果输出包含 markdown 代码块,提取其中的 JSON
|
||||
if "```json" in ai_output:
|
||||
start = ai_output.find("```json") + 7
|
||||
end = ai_output.find("```", start)
|
||||
json_str = ai_output[start:end].strip()
|
||||
elif "```" in ai_output:
|
||||
start = ai_output.find("```") + 3
|
||||
end = ai_output.find("```", start)
|
||||
json_str = ai_output[start:end].strip()
|
||||
|
||||
data = json.loads(json_str)
|
||||
|
||||
# 解析整体评估
|
||||
overall = data.get("overall_evaluation", {})
|
||||
result.interaction_quality = overall.get("interaction_quality", 0)
|
||||
result.scene_restoration = overall.get("scene_restoration", 0)
|
||||
result.overall_comment = overall.get("overall_comment", "")
|
||||
|
||||
# 解析用户A评估
|
||||
user_a_data = data.get("user_a_evaluation", {})
|
||||
if user_a_data:
|
||||
result.user_a_evaluation = UserEvaluation(
|
||||
user_name=user_a_data.get("user_name", user_a_name),
|
||||
role_name=user_a_data.get("role_name", role_a_name),
|
||||
total_score=user_a_data.get("total_score", 0),
|
||||
dimensions=user_a_data.get("dimensions", {}),
|
||||
highlights=user_a_data.get("highlights", []),
|
||||
improvements=user_a_data.get("improvements", [])
|
||||
)
|
||||
|
||||
# 解析用户B评估
|
||||
user_b_data = data.get("user_b_evaluation", {})
|
||||
if user_b_data:
|
||||
result.user_b_evaluation = UserEvaluation(
|
||||
user_name=user_b_data.get("user_name", user_b_name),
|
||||
role_name=user_b_data.get("role_name", role_b_name),
|
||||
total_score=user_b_data.get("total_score", 0),
|
||||
dimensions=user_b_data.get("dimensions", {}),
|
||||
highlights=user_b_data.get("highlights", []),
|
||||
improvements=user_b_data.get("improvements", [])
|
||||
)
|
||||
|
||||
# 解析对话标注
|
||||
result.dialogue_annotations = data.get("dialogue_annotations", [])
|
||||
|
||||
except json.JSONDecodeError as e:
|
||||
logger.warning(f"JSON 解析失败: {e}")
|
||||
result.overall_comment = "AI 输出格式异常,请重试"
|
||||
except Exception as e:
|
||||
logger.error(f"解析分析结果失败: {e}")
|
||||
result.overall_comment = f"解析失败: {str(e)}"
|
||||
|
||||
return result
|
||||
|
||||
def result_to_dict(self, result: DuoPracticeAnalysisResult) -> Dict[str, Any]:
|
||||
"""将结果转换为字典(用于 API 响应)"""
|
||||
return {
|
||||
"overall_evaluation": {
|
||||
"interaction_quality": result.interaction_quality,
|
||||
"scene_restoration": result.scene_restoration,
|
||||
"overall_comment": result.overall_comment
|
||||
},
|
||||
"user_a_evaluation": {
|
||||
"user_name": result.user_a_evaluation.user_name,
|
||||
"role_name": result.user_a_evaluation.role_name,
|
||||
"total_score": result.user_a_evaluation.total_score,
|
||||
"dimensions": result.user_a_evaluation.dimensions,
|
||||
"highlights": result.user_a_evaluation.highlights,
|
||||
"improvements": result.user_a_evaluation.improvements
|
||||
} if result.user_a_evaluation else None,
|
||||
"user_b_evaluation": {
|
||||
"user_name": result.user_b_evaluation.user_name,
|
||||
"role_name": result.user_b_evaluation.role_name,
|
||||
"total_score": result.user_b_evaluation.total_score,
|
||||
"dimensions": result.user_b_evaluation.dimensions,
|
||||
"highlights": result.user_b_evaluation.highlights,
|
||||
"improvements": result.user_b_evaluation.improvements
|
||||
} if result.user_b_evaluation else None,
|
||||
"dialogue_annotations": result.dialogue_annotations,
|
||||
"ai_metadata": {
|
||||
"provider": result.ai_provider,
|
||||
"model": result.ai_model,
|
||||
"latency_ms": result.ai_latency_ms
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# ==================== 全局实例 ====================
|
||||
|
||||
duo_practice_analysis_service = DuoPracticeAnalysisService()
|
||||
|
||||
|
||||
# ==================== 便捷函数 ====================
|
||||
|
||||
async def analyze_duo_practice(
|
||||
scene_name: str,
|
||||
scene_background: str,
|
||||
role_a_name: str,
|
||||
role_b_name: str,
|
||||
role_a_description: str,
|
||||
role_b_description: str,
|
||||
user_a_name: str,
|
||||
user_b_name: str,
|
||||
dialogue_history: List[Dict[str, Any]],
|
||||
duration_seconds: int,
|
||||
total_turns: int,
|
||||
db: Any = None
|
||||
) -> DuoPracticeAnalysisResult:
|
||||
"""便捷函数:分析双人对练"""
|
||||
return await duo_practice_analysis_service.analyze(
|
||||
scene_name=scene_name,
|
||||
scene_background=scene_background,
|
||||
role_a_name=role_a_name,
|
||||
role_b_name=role_b_name,
|
||||
role_a_description=role_a_description,
|
||||
role_b_description=role_b_description,
|
||||
user_a_name=user_a_name,
|
||||
user_b_name=user_b_name,
|
||||
dialogue_history=dialogue_history,
|
||||
duration_seconds=duration_seconds,
|
||||
total_turns=total_turns,
|
||||
db=db
|
||||
)
|
||||
207
backend/app/services/ai/prompts/duo_practice_prompts.py
Normal file
207
backend/app/services/ai/prompts/duo_practice_prompts.py
Normal file
@@ -0,0 +1,207 @@
|
||||
"""
|
||||
双人对练评估提示词模板
|
||||
|
||||
功能:评估双人角色扮演对练的表现
|
||||
"""
|
||||
|
||||
# ==================== 元数据 ====================
|
||||
|
||||
PROMPT_META = {
|
||||
"name": "duo_practice_analysis",
|
||||
"display_name": "双人对练评估",
|
||||
"description": "评估双人角色扮演对练中双方的表现",
|
||||
"module": "kaopeilian",
|
||||
"variables": [
|
||||
"scene_name", "scene_background",
|
||||
"role_a_name", "role_b_name",
|
||||
"role_a_description", "role_b_description",
|
||||
"user_a_name", "user_b_name",
|
||||
"dialogue_history",
|
||||
"duration_seconds", "total_turns"
|
||||
],
|
||||
"version": "1.0.0",
|
||||
"author": "kaopeilian-team",
|
||||
}
|
||||
|
||||
|
||||
# ==================== 系统提示词 ====================
|
||||
|
||||
SYSTEM_PROMPT = """你是一位资深的销售培训专家和沟通教练,擅长评估角色扮演对练的表现。
|
||||
你需要观察双人对练的对话记录,分别对两位参与者的表现进行专业评估。
|
||||
|
||||
评估原则:
|
||||
1. 客观公正,基于对话内容给出评价
|
||||
2. 突出亮点,指出不足
|
||||
3. 给出具体、可操作的改进建议
|
||||
4. 考虑角色特点,评估角色代入度
|
||||
|
||||
输出格式要求:
|
||||
- 必须返回有效的 JSON 格式
|
||||
- 分数范围 0-100
|
||||
- 建议具体可行"""
|
||||
|
||||
|
||||
# ==================== 用户提示词模板 ====================
|
||||
|
||||
USER_PROMPT = """# 双人对练评估任务
|
||||
|
||||
## 场景信息
|
||||
- **场景名称**:{scene_name}
|
||||
- **场景背景**:{scene_background}
|
||||
|
||||
## 角色设置
|
||||
### {role_a_name}
|
||||
- **扮演者**:{user_a_name}
|
||||
- **角色描述**:{role_a_description}
|
||||
|
||||
### {role_b_name}
|
||||
- **扮演者**:{user_b_name}
|
||||
- **角色描述**:{role_b_description}
|
||||
|
||||
## 对练数据
|
||||
- **对练时长**:{duration_seconds} 秒
|
||||
- **总对话轮次**:{total_turns} 轮
|
||||
|
||||
## 对话记录
|
||||
{dialogue_history}
|
||||
|
||||
---
|
||||
|
||||
## 评估要求
|
||||
|
||||
请按以下 JSON 格式输出评估结果:
|
||||
|
||||
```json
|
||||
{{
|
||||
"overall_evaluation": {{
|
||||
"interaction_quality": 85,
|
||||
"scene_restoration": 80,
|
||||
"overall_comment": "整体评价..."
|
||||
}},
|
||||
"user_a_evaluation": {{
|
||||
"user_name": "{user_a_name}",
|
||||
"role_name": "{role_a_name}",
|
||||
"total_score": 85,
|
||||
"dimensions": {{
|
||||
"role_immersion": {{
|
||||
"score": 85,
|
||||
"comment": "角色代入度评价..."
|
||||
}},
|
||||
"communication": {{
|
||||
"score": 80,
|
||||
"comment": "沟通表达能力评价..."
|
||||
}},
|
||||
"professional_knowledge": {{
|
||||
"score": 75,
|
||||
"comment": "专业知识运用评价..."
|
||||
}},
|
||||
"response_quality": {{
|
||||
"score": 82,
|
||||
"comment": "回应质量评价..."
|
||||
}},
|
||||
"goal_achievement": {{
|
||||
"score": 78,
|
||||
"comment": "目标达成度评价..."
|
||||
}}
|
||||
}},
|
||||
"highlights": [
|
||||
"亮点1...",
|
||||
"亮点2..."
|
||||
],
|
||||
"improvements": [
|
||||
{{
|
||||
"issue": "问题描述",
|
||||
"suggestion": "改进建议",
|
||||
"example": "示例话术"
|
||||
}}
|
||||
]
|
||||
}},
|
||||
"user_b_evaluation": {{
|
||||
"user_name": "{user_b_name}",
|
||||
"role_name": "{role_b_name}",
|
||||
"total_score": 82,
|
||||
"dimensions": {{
|
||||
"role_immersion": {{
|
||||
"score": 80,
|
||||
"comment": "角色代入度评价..."
|
||||
}},
|
||||
"communication": {{
|
||||
"score": 85,
|
||||
"comment": "沟通表达能力评价..."
|
||||
}},
|
||||
"professional_knowledge": {{
|
||||
"score": 78,
|
||||
"comment": "专业知识运用评价..."
|
||||
}},
|
||||
"response_quality": {{
|
||||
"score": 80,
|
||||
"comment": "回应质量评价..."
|
||||
}},
|
||||
"goal_achievement": {{
|
||||
"score": 75,
|
||||
"comment": "目标达成度评价..."
|
||||
}}
|
||||
}},
|
||||
"highlights": [
|
||||
"亮点1...",
|
||||
"亮点2..."
|
||||
],
|
||||
"improvements": [
|
||||
{{
|
||||
"issue": "问题描述",
|
||||
"suggestion": "改进建议",
|
||||
"example": "示例话术"
|
||||
}}
|
||||
]
|
||||
}},
|
||||
"dialogue_annotations": [
|
||||
{{
|
||||
"sequence": 1,
|
||||
"speaker": "{role_a_name}",
|
||||
"tags": ["good_opening"],
|
||||
"comment": "开场白自然得体"
|
||||
}},
|
||||
{{
|
||||
"sequence": 3,
|
||||
"speaker": "{role_b_name}",
|
||||
"tags": ["needs_improvement"],
|
||||
"comment": "可以更主动表达需求"
|
||||
}}
|
||||
]
|
||||
}}
|
||||
```
|
||||
|
||||
请基于对话内容,给出客观、专业的评估。"""
|
||||
|
||||
|
||||
# ==================== 维度说明 ====================
|
||||
|
||||
DIMENSION_DESCRIPTIONS = {
|
||||
"role_immersion": "角色代入度:是否完全进入角色,语言风格、态度是否符合角色设定",
|
||||
"communication": "沟通表达:语言是否清晰、逻辑是否通顺、表达是否得体",
|
||||
"professional_knowledge": "专业知识:是否展现出角色应有的专业素养和知识储备",
|
||||
"response_quality": "回应质量:对对方发言的回应是否及时、恰当、有针对性",
|
||||
"goal_achievement": "目标达成:是否朝着对练目标推进,是否达成预期效果"
|
||||
}
|
||||
|
||||
|
||||
# ==================== 对话标签 ====================
|
||||
|
||||
DIALOGUE_TAGS = {
|
||||
# 正面标签
|
||||
"good_opening": "开场良好",
|
||||
"active_listening": "积极倾听",
|
||||
"empathy": "共情表达",
|
||||
"professional": "专业表现",
|
||||
"good_closing": "结束得体",
|
||||
"creative_response": "创意回应",
|
||||
"problem_solving": "问题解决",
|
||||
|
||||
# 需改进标签
|
||||
"needs_improvement": "需要改进",
|
||||
"off_topic": "偏离主题",
|
||||
"too_passive": "过于被动",
|
||||
"lack_detail": "缺乏细节",
|
||||
"missed_opportunity": "错失机会",
|
||||
"unclear_expression": "表达不清"
|
||||
}
|
||||
514
backend/app/services/practice_room_service.py
Normal file
514
backend/app/services/practice_room_service.py
Normal file
@@ -0,0 +1,514 @@
|
||||
"""
|
||||
双人对练房间服务
|
||||
|
||||
功能:
|
||||
- 房间创建、加入、退出
|
||||
- 房间状态管理
|
||||
- 消息广播
|
||||
- 对练结束处理
|
||||
"""
|
||||
import logging
|
||||
import random
|
||||
import string
|
||||
from datetime import datetime
|
||||
from typing import Optional, List, Dict, Any
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select, update, and_
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
from app.models.practice_room import PracticeRoom, PracticeRoomMessage
|
||||
from app.models.practice import PracticeDialogue, PracticeSession
|
||||
from app.models.user import User
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class PracticeRoomService:
|
||||
"""双人对练房间服务"""
|
||||
|
||||
# 房间状态常量
|
||||
STATUS_WAITING = "waiting" # 等待加入
|
||||
STATUS_READY = "ready" # 准备就绪
|
||||
STATUS_PRACTICING = "practicing" # 对练中
|
||||
STATUS_COMPLETED = "completed" # 已完成
|
||||
STATUS_CANCELED = "canceled" # 已取消
|
||||
|
||||
# 消息类型常量
|
||||
MSG_TYPE_CHAT = "chat" # 聊天消息
|
||||
MSG_TYPE_SYSTEM = "system" # 系统消息
|
||||
MSG_TYPE_JOIN = "join" # 加入消息
|
||||
MSG_TYPE_LEAVE = "leave" # 离开消息
|
||||
MSG_TYPE_START = "start" # 开始消息
|
||||
MSG_TYPE_END = "end" # 结束消息
|
||||
|
||||
def __init__(self, db: AsyncSession):
|
||||
self.db = db
|
||||
|
||||
# ==================== 房间管理 ====================
|
||||
|
||||
async def create_room(
|
||||
self,
|
||||
host_user_id: int,
|
||||
scene_id: Optional[int] = None,
|
||||
scene_name: Optional[str] = None,
|
||||
scene_type: Optional[str] = None,
|
||||
scene_background: Optional[str] = None,
|
||||
role_a_name: str = "销售顾问",
|
||||
role_b_name: str = "顾客",
|
||||
role_a_description: Optional[str] = None,
|
||||
role_b_description: Optional[str] = None,
|
||||
host_role: str = "A",
|
||||
room_name: Optional[str] = None
|
||||
) -> PracticeRoom:
|
||||
"""
|
||||
创建对练房间
|
||||
|
||||
Args:
|
||||
host_user_id: 房主用户ID
|
||||
scene_id: 场景ID(可选)
|
||||
scene_name: 场景名称
|
||||
scene_type: 场景类型
|
||||
scene_background: 场景背景
|
||||
role_a_name: 角色A名称
|
||||
role_b_name: 角色B名称
|
||||
role_a_description: 角色A描述
|
||||
role_b_description: 角色B描述
|
||||
host_role: 房主选择的角色(A或B)
|
||||
room_name: 房间名称
|
||||
|
||||
Returns:
|
||||
PracticeRoom: 创建的房间对象
|
||||
"""
|
||||
# 生成唯一的6位房间码
|
||||
room_code = await self._generate_unique_room_code()
|
||||
|
||||
# 创建房间
|
||||
room = PracticeRoom(
|
||||
room_code=room_code,
|
||||
room_name=room_name or f"{scene_name or '双人对练'}房间",
|
||||
scene_id=scene_id,
|
||||
scene_name=scene_name,
|
||||
scene_type=scene_type,
|
||||
scene_background=scene_background,
|
||||
role_a_name=role_a_name,
|
||||
role_b_name=role_b_name,
|
||||
role_a_description=role_a_description,
|
||||
role_b_description=role_b_description,
|
||||
host_user_id=host_user_id,
|
||||
host_role=host_role,
|
||||
status=self.STATUS_WAITING
|
||||
)
|
||||
|
||||
self.db.add(room)
|
||||
await self.db.commit()
|
||||
await self.db.refresh(room)
|
||||
|
||||
logger.info(f"创建房间成功: room_code={room_code}, host_user_id={host_user_id}")
|
||||
return room
|
||||
|
||||
async def join_room(
|
||||
self,
|
||||
room_code: str,
|
||||
user_id: int
|
||||
) -> PracticeRoom:
|
||||
"""
|
||||
加入房间
|
||||
|
||||
Args:
|
||||
room_code: 房间码
|
||||
user_id: 用户ID
|
||||
|
||||
Returns:
|
||||
PracticeRoom: 房间对象
|
||||
|
||||
Raises:
|
||||
ValueError: 房间不存在、已满或状态不允许加入
|
||||
"""
|
||||
# 查询房间
|
||||
room = await self.get_room_by_code(room_code)
|
||||
if not room:
|
||||
raise ValueError("房间不存在或已过期")
|
||||
|
||||
# 检查是否是房主(房主重新进入)
|
||||
if room.host_user_id == user_id:
|
||||
return room
|
||||
|
||||
# 检查房间状态
|
||||
if room.status not in [self.STATUS_WAITING, self.STATUS_READY]:
|
||||
raise ValueError("房间已开始对练或已结束,无法加入")
|
||||
|
||||
# 检查是否已满
|
||||
if room.guest_user_id and room.guest_user_id != user_id:
|
||||
raise ValueError("房间已满")
|
||||
|
||||
# 加入房间
|
||||
room.guest_user_id = user_id
|
||||
room.status = self.STATUS_READY
|
||||
|
||||
await self.db.commit()
|
||||
await self.db.refresh(room)
|
||||
|
||||
# 发送系统消息
|
||||
await self._add_system_message(room.id, f"用户已加入房间", self.MSG_TYPE_JOIN, user_id)
|
||||
|
||||
logger.info(f"用户加入房间: room_code={room_code}, user_id={user_id}")
|
||||
return room
|
||||
|
||||
async def leave_room(
|
||||
self,
|
||||
room_code: str,
|
||||
user_id: int
|
||||
) -> bool:
|
||||
"""
|
||||
离开房间
|
||||
|
||||
Args:
|
||||
room_code: 房间码
|
||||
user_id: 用户ID
|
||||
|
||||
Returns:
|
||||
bool: 是否成功离开
|
||||
"""
|
||||
room = await self.get_room_by_code(room_code)
|
||||
if not room:
|
||||
return False
|
||||
|
||||
# 如果是房主离开,取消房间
|
||||
if room.host_user_id == user_id:
|
||||
room.status = self.STATUS_CANCELED
|
||||
await self._add_system_message(room.id, "房主离开,房间已关闭", self.MSG_TYPE_LEAVE, user_id)
|
||||
# 如果是嘉宾离开
|
||||
elif room.guest_user_id == user_id:
|
||||
room.guest_user_id = None
|
||||
room.status = self.STATUS_WAITING
|
||||
await self._add_system_message(room.id, "对方已离开房间", self.MSG_TYPE_LEAVE, user_id)
|
||||
else:
|
||||
return False
|
||||
|
||||
await self.db.commit()
|
||||
logger.info(f"用户离开房间: room_code={room_code}, user_id={user_id}")
|
||||
return True
|
||||
|
||||
async def start_practice(
|
||||
self,
|
||||
room_code: str,
|
||||
user_id: int
|
||||
) -> PracticeRoom:
|
||||
"""
|
||||
开始对练(仅房主可操作)
|
||||
|
||||
Args:
|
||||
room_code: 房间码
|
||||
user_id: 用户ID(必须是房主)
|
||||
|
||||
Returns:
|
||||
PracticeRoom: 房间对象
|
||||
"""
|
||||
room = await self.get_room_by_code(room_code)
|
||||
if not room:
|
||||
raise ValueError("房间不存在")
|
||||
|
||||
if room.host_user_id != user_id:
|
||||
raise ValueError("只有房主可以开始对练")
|
||||
|
||||
if room.status != self.STATUS_READY:
|
||||
raise ValueError("房间未就绪,请等待对方加入")
|
||||
|
||||
room.status = self.STATUS_PRACTICING
|
||||
room.started_at = datetime.now()
|
||||
|
||||
await self.db.commit()
|
||||
await self.db.refresh(room)
|
||||
|
||||
# 发送开始消息
|
||||
await self._add_system_message(room.id, "对练开始!", self.MSG_TYPE_START)
|
||||
|
||||
logger.info(f"对练开始: room_code={room_code}")
|
||||
return room
|
||||
|
||||
async def end_practice(
|
||||
self,
|
||||
room_code: str,
|
||||
user_id: int
|
||||
) -> PracticeRoom:
|
||||
"""
|
||||
结束对练
|
||||
|
||||
Args:
|
||||
room_code: 房间码
|
||||
user_id: 用户ID
|
||||
|
||||
Returns:
|
||||
PracticeRoom: 房间对象
|
||||
"""
|
||||
room = await self.get_room_by_code(room_code)
|
||||
if not room:
|
||||
raise ValueError("房间不存在")
|
||||
|
||||
if room.status != self.STATUS_PRACTICING:
|
||||
raise ValueError("对练未在进行中")
|
||||
|
||||
# 计算时长
|
||||
if room.started_at:
|
||||
duration = (datetime.now() - room.started_at).total_seconds()
|
||||
room.duration_seconds = int(duration)
|
||||
|
||||
room.status = self.STATUS_COMPLETED
|
||||
room.ended_at = datetime.now()
|
||||
|
||||
await self.db.commit()
|
||||
await self.db.refresh(room)
|
||||
|
||||
# 发送结束消息
|
||||
await self._add_system_message(room.id, "对练结束!", self.MSG_TYPE_END)
|
||||
|
||||
logger.info(f"对练结束: room_code={room_code}, duration={room.duration_seconds}s")
|
||||
return room
|
||||
|
||||
# ==================== 消息管理 ====================
|
||||
|
||||
async def send_message(
|
||||
self,
|
||||
room_id: int,
|
||||
user_id: int,
|
||||
content: str,
|
||||
role_name: Optional[str] = None
|
||||
) -> PracticeRoomMessage:
|
||||
"""
|
||||
发送聊天消息
|
||||
|
||||
Args:
|
||||
room_id: 房间ID
|
||||
user_id: 发送者ID
|
||||
content: 消息内容
|
||||
role_name: 角色名称
|
||||
|
||||
Returns:
|
||||
PracticeRoomMessage: 消息对象
|
||||
"""
|
||||
# 获取当前消息序号
|
||||
sequence = await self._get_next_sequence(room_id)
|
||||
|
||||
message = PracticeRoomMessage(
|
||||
room_id=room_id,
|
||||
user_id=user_id,
|
||||
message_type=self.MSG_TYPE_CHAT,
|
||||
content=content,
|
||||
role_name=role_name,
|
||||
sequence=sequence
|
||||
)
|
||||
|
||||
self.db.add(message)
|
||||
|
||||
# 更新房间统计
|
||||
room = await self.get_room_by_id(room_id)
|
||||
if room:
|
||||
room.total_turns += 1
|
||||
user_role = room.get_user_role(user_id)
|
||||
if user_role == "A":
|
||||
room.role_a_turns += 1
|
||||
elif user_role == "B":
|
||||
room.role_b_turns += 1
|
||||
|
||||
await self.db.commit()
|
||||
await self.db.refresh(message)
|
||||
|
||||
return message
|
||||
|
||||
async def get_messages(
|
||||
self,
|
||||
room_id: int,
|
||||
since_sequence: int = 0,
|
||||
limit: int = 100
|
||||
) -> List[PracticeRoomMessage]:
|
||||
"""
|
||||
获取房间消息(用于SSE轮询)
|
||||
|
||||
Args:
|
||||
room_id: 房间ID
|
||||
since_sequence: 从该序号之后开始获取
|
||||
limit: 最大数量
|
||||
|
||||
Returns:
|
||||
List[PracticeRoomMessage]: 消息列表
|
||||
"""
|
||||
result = await self.db.execute(
|
||||
select(PracticeRoomMessage)
|
||||
.where(
|
||||
and_(
|
||||
PracticeRoomMessage.room_id == room_id,
|
||||
PracticeRoomMessage.sequence > since_sequence
|
||||
)
|
||||
)
|
||||
.order_by(PracticeRoomMessage.sequence)
|
||||
.limit(limit)
|
||||
)
|
||||
return list(result.scalars().all())
|
||||
|
||||
async def get_all_messages(self, room_id: int) -> List[PracticeRoomMessage]:
|
||||
"""
|
||||
获取房间所有消息
|
||||
|
||||
Args:
|
||||
room_id: 房间ID
|
||||
|
||||
Returns:
|
||||
List[PracticeRoomMessage]: 消息列表
|
||||
"""
|
||||
result = await self.db.execute(
|
||||
select(PracticeRoomMessage)
|
||||
.where(PracticeRoomMessage.room_id == room_id)
|
||||
.order_by(PracticeRoomMessage.sequence)
|
||||
)
|
||||
return list(result.scalars().all())
|
||||
|
||||
# ==================== 查询方法 ====================
|
||||
|
||||
async def get_room_by_code(self, room_code: str) -> Optional[PracticeRoom]:
|
||||
"""根据房间码获取房间"""
|
||||
result = await self.db.execute(
|
||||
select(PracticeRoom).where(
|
||||
and_(
|
||||
PracticeRoom.room_code == room_code,
|
||||
PracticeRoom.is_deleted == False
|
||||
)
|
||||
)
|
||||
)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
async def get_room_by_id(self, room_id: int) -> Optional[PracticeRoom]:
|
||||
"""根据ID获取房间"""
|
||||
result = await self.db.execute(
|
||||
select(PracticeRoom).where(
|
||||
and_(
|
||||
PracticeRoom.id == room_id,
|
||||
PracticeRoom.is_deleted == False
|
||||
)
|
||||
)
|
||||
)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
async def get_user_rooms(
|
||||
self,
|
||||
user_id: int,
|
||||
status: Optional[str] = None,
|
||||
limit: int = 20
|
||||
) -> List[PracticeRoom]:
|
||||
"""获取用户的房间列表"""
|
||||
query = select(PracticeRoom).where(
|
||||
and_(
|
||||
(PracticeRoom.host_user_id == user_id) | (PracticeRoom.guest_user_id == user_id),
|
||||
PracticeRoom.is_deleted == False
|
||||
)
|
||||
)
|
||||
|
||||
if status:
|
||||
query = query.where(PracticeRoom.status == status)
|
||||
|
||||
query = query.order_by(PracticeRoom.created_at.desc()).limit(limit)
|
||||
|
||||
result = await self.db.execute(query)
|
||||
return list(result.scalars().all())
|
||||
|
||||
async def get_room_with_users(self, room_code: str) -> Optional[Dict[str, Any]]:
|
||||
"""获取房间详情(包含用户信息)"""
|
||||
room = await self.get_room_by_code(room_code)
|
||||
if not room:
|
||||
return None
|
||||
|
||||
# 获取用户信息
|
||||
host_user = None
|
||||
guest_user = None
|
||||
|
||||
if room.host_user_id:
|
||||
result = await self.db.execute(
|
||||
select(User).where(User.id == room.host_user_id)
|
||||
)
|
||||
host_user = result.scalar_one_or_none()
|
||||
|
||||
if room.guest_user_id:
|
||||
result = await self.db.execute(
|
||||
select(User).where(User.id == room.guest_user_id)
|
||||
)
|
||||
guest_user = result.scalar_one_or_none()
|
||||
|
||||
return {
|
||||
"room": room,
|
||||
"host_user": host_user,
|
||||
"guest_user": guest_user,
|
||||
"host_role_name": room.get_role_name(room.host_role),
|
||||
"guest_role_name": room.get_role_name("B" if room.host_role == "A" else "A") if guest_user else None
|
||||
}
|
||||
|
||||
# ==================== 辅助方法 ====================
|
||||
|
||||
async def _generate_unique_room_code(self) -> str:
|
||||
"""生成唯一的6位房间码"""
|
||||
for _ in range(10): # 最多尝试10次
|
||||
code = ''.join(random.choices(string.ascii_uppercase + string.digits, k=6))
|
||||
# 排除容易混淆的字符
|
||||
code = code.replace('0', 'X').replace('O', 'Y').replace('I', 'Z').replace('1', 'W')
|
||||
|
||||
# 检查是否已存在
|
||||
existing = await self.get_room_by_code(code)
|
||||
if not existing:
|
||||
return code
|
||||
|
||||
raise ValueError("无法生成唯一房间码,请稍后重试")
|
||||
|
||||
async def _get_next_sequence(self, room_id: int) -> int:
|
||||
"""获取下一个消息序号"""
|
||||
result = await self.db.execute(
|
||||
select(PracticeRoomMessage.sequence)
|
||||
.where(PracticeRoomMessage.room_id == room_id)
|
||||
.order_by(PracticeRoomMessage.sequence.desc())
|
||||
.limit(1)
|
||||
)
|
||||
last_seq = result.scalar_one_or_none()
|
||||
return (last_seq or 0) + 1
|
||||
|
||||
async def _add_system_message(
|
||||
self,
|
||||
room_id: int,
|
||||
content: str,
|
||||
msg_type: str,
|
||||
user_id: Optional[int] = None
|
||||
) -> PracticeRoomMessage:
|
||||
"""添加系统消息"""
|
||||
sequence = await self._get_next_sequence(room_id)
|
||||
|
||||
message = PracticeRoomMessage(
|
||||
room_id=room_id,
|
||||
user_id=user_id,
|
||||
message_type=msg_type,
|
||||
content=content,
|
||||
sequence=sequence
|
||||
)
|
||||
|
||||
self.db.add(message)
|
||||
await self.db.commit()
|
||||
await self.db.refresh(message)
|
||||
|
||||
return message
|
||||
|
||||
|
||||
# ==================== 便捷函数 ====================
|
||||
|
||||
async def create_practice_room(
|
||||
db: AsyncSession,
|
||||
host_user_id: int,
|
||||
**kwargs
|
||||
) -> PracticeRoom:
|
||||
"""便捷函数:创建房间"""
|
||||
service = PracticeRoomService(db)
|
||||
return await service.create_room(host_user_id, **kwargs)
|
||||
|
||||
|
||||
async def join_practice_room(
|
||||
db: AsyncSession,
|
||||
room_code: str,
|
||||
user_id: int
|
||||
) -> PracticeRoom:
|
||||
"""便捷函数:加入房间"""
|
||||
service = PracticeRoomService(db)
|
||||
return await service.join_room(room_code, user_id)
|
||||
Reference in New Issue
Block a user