From b0fa43f652ea21baa4a24c28028a593695358faf Mon Sep 17 00:00:00 2001 From: Drobysh Date: Tue, 22 Sep 2026 12:57:40 +0000 Subject: [PATCH] =?UTF-8?q?fix(teacher):=20lesson=5Fid=20=D0=B8=D0=B7=20le?= =?UTF-8?q?arning=5Flessons,=20=D0=B1=D0=B5=D0=B7=D0=BE=D0=BF=D0=B0=D1=81?= =?UTF-8?q?=D0=BD=D1=8B=D0=B9=20content=20AI=20(22.09.2026)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routers/teacher.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/routers/teacher.py b/routers/teacher.py index 3510f63..1f629f9 100644 --- a/routers/teacher.py +++ b/routers/teacher.py @@ -240,7 +240,21 @@ async def call_ai(prompt: str, system_prompt: str, max_tokens: int = 500) -> str return "" data = response.json() - ai_response = data["choices"][0]["message"]["content"].strip() + + # 🔧 ФИКС (22.09.2026): безопасно извлекаем content. + # Together AI может вернуть reasoning_content или пустой content. + try: + message = data["choices"][0]["message"] + ai_response = message.get("content") or message.get("reasoning_content") or "" + except (KeyError, IndexError, TypeError) as e: + logger.error(f"AI response format error: {e}, data={str(data)[:300]}") + return "" + + if not ai_response: + logger.warning(f"AI returned empty content. finish_reason={data.get('choices', [{}])[0].get('finish_reason')}") + return "" + + ai_response = ai_response.strip() ai_response = re.sub(r'\s*\([a-z_]+_[a-z_]+\)', '', ai_response) return ai_response except Exception as e: @@ -461,9 +475,15 @@ async def teacher_resume(request: Request): if session["session_type"] == "teacher_test": questions = await get_test_questions(conn, session["topic_id"]) else: + # 🔧 ФИКС (22.09.2026): learning_sessions не хранит lesson_id. + # Берём lesson_id из первого вопроса урока через topic_id сессии. + # Находим первый урок темы, чтобы восстановить сессию. lesson_id = await conn.fetchval(""" - SELECT lesson_id FROM learning_sessions WHERE id = $1 - """, session["id"]) + SELECT lesson_id FROM learning_lessons + WHERE topic_id = $1 AND is_active = TRUE + ORDER BY order_number + LIMIT 1 + """, session["topic_id"]) if lesson_id: questions = await get_lesson_questions(conn, lesson_id) else: