From b02f2491668d938c4a97770d6d6136c00def8021 Mon Sep 17 00:00:00 2001 From: yuliang_guo Date: Wed, 4 Feb 2026 14:56:36 +0800 Subject: [PATCH] =?UTF-8?q?fix(practice):=20=E4=BF=AE=E5=A4=8D=E7=BB=93?= =?UTF-8?q?=E6=9D=9F=E4=BC=9A=E8=AF=9D=E6=8E=A5=E5=8F=A3=20DetachedInstanc?= =?UTF-8?q?eError?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 ORM 对象转换为 PracticeSessionResponse 后再返回 - 添加 COZE_WORKSPACE_ID 配置到 .env.ex Co-authored-by: Cursor --- backend/.env.ex | 1 + backend/app/api/v1/practice.py | 23 ++++++++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/backend/.env.ex b/backend/.env.ex index a27766d..e4d85e4 100644 --- a/backend/.env.ex +++ b/backend/.env.ex @@ -41,6 +41,7 @@ UPLOAD_DIR=uploads COZE_OAUTH_CLIENT_ID=1114009328887 COZE_OAUTH_PUBLIC_KEY_ID=GGs9pw0BDHx2k9vGGehUyRgKV-PyUWLBncDs-YNNN_I COZE_OAUTH_PRIVATE_KEY_PATH=/app/secrets/coze_private_key.pem +COZE_WORKSPACE_ID=7461992708538974244 COZE_PRACTICE_BOT_ID=7602204855037591602 # Dify 工作流 API Key 配置 diff --git a/backend/app/api/v1/practice.py b/backend/app/api/v1/practice.py index aa1e3a9..cb60496 100644 --- a/backend/app/api/v1/practice.py +++ b/backend/app/api/v1/practice.py @@ -734,14 +734,27 @@ async def end_practice_session( except Exception: pass + # 将 ORM 对象转换为响应格式,避免 DetachedInstanceError + session_data = PracticeSessionResponse( + id=session.id, + session_id=session.session_id, + user_id=session.user_id, + scene_id=session.scene_id, + scene_name=session.scene_name or "", + scene_type=session.scene_type, + conversation_id=session.conversation_id, + start_time=session.start_time, + end_time=session.end_time, + duration_seconds=session.duration_seconds or 0, + turns=session.turns or 0, + status=session.status, + created_at=session.created_at + ) + return ResponseModel( code=200, message="会话已结束", - data={ - "session": session, - "exp_result": exp_result, - "new_badges": new_badges - } + data=session_data ) except HTTPException: