fix(practice): 获取报告时自动生成不存在的报告
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -896,7 +896,53 @@ async def get_practice_report(
|
|||||||
report = result.scalar_one_or_none()
|
report = result.scalar_one_or_none()
|
||||||
|
|
||||||
if not report:
|
if not report:
|
||||||
raise HTTPException(status_code=404, detail="分析报告不存在,请先生成报告")
|
# 报告不存在,自动生成
|
||||||
|
logger.info(f"报告不存在,自动生成: session_id={session_id}")
|
||||||
|
|
||||||
|
# 查询对话历史
|
||||||
|
result = await db.execute(
|
||||||
|
select(PracticeDialogue).where(
|
||||||
|
PracticeDialogue.session_id == session_id
|
||||||
|
).order_by(PracticeDialogue.sequence)
|
||||||
|
)
|
||||||
|
dialogue_list = result.scalars().all()
|
||||||
|
|
||||||
|
if not dialogue_list:
|
||||||
|
raise HTTPException(status_code=404, detail="没有对话记录,无法生成报告")
|
||||||
|
|
||||||
|
# 构建对话历史
|
||||||
|
dialogue_history = [
|
||||||
|
{"role": "user" if d.speaker == "user" else "assistant", "content": d.content}
|
||||||
|
for d in dialogue_list
|
||||||
|
]
|
||||||
|
|
||||||
|
# 调用分析服务
|
||||||
|
from app.services.practice_analysis_service import PracticeAnalysisService
|
||||||
|
import json
|
||||||
|
|
||||||
|
practice_analysis_service = PracticeAnalysisService()
|
||||||
|
analysis_result = await practice_analysis_service.analyze(dialogue_history, db=db)
|
||||||
|
|
||||||
|
if not analysis_result.success:
|
||||||
|
raise HTTPException(status_code=500, detail=f"分析失败: {analysis_result.error}")
|
||||||
|
|
||||||
|
analysis_data = analysis_result.to_dict()
|
||||||
|
|
||||||
|
# 保存报告
|
||||||
|
report = PracticeReport(
|
||||||
|
session_id=session_id,
|
||||||
|
total_score=analysis_data.get("overall_score", 0),
|
||||||
|
score_breakdown=analysis_data.get("score_breakdown", []),
|
||||||
|
ability_dimensions=analysis_data.get("ability_dimensions", []),
|
||||||
|
dialogue_review=analysis_data.get("dialogue_review", []),
|
||||||
|
suggestions=analysis_data.get("suggestions", []),
|
||||||
|
summary=analysis_data.get("summary", ""),
|
||||||
|
raw_response=json.dumps(analysis_data, ensure_ascii=False)
|
||||||
|
)
|
||||||
|
db.add(report)
|
||||||
|
await db.commit()
|
||||||
|
await db.refresh(report)
|
||||||
|
logger.info(f"报告自动生成成功: session_id={session_id}, 总分={report.total_score}")
|
||||||
|
|
||||||
# 3. 查询完整对话记录(从数据库)
|
# 3. 查询完整对话记录(从数据库)
|
||||||
result = await db.execute(
|
result = await db.execute(
|
||||||
|
|||||||
Reference in New Issue
Block a user