fix(teacher): lesson_id из learning_lessons, безопасный content AI (22.09.2026)

This commit is contained in:
2026-09-22 12:57:40 +00:00
parent c4353bc63c
commit b0fa43f652
+23 -3
View File
@@ -240,7 +240,21 @@ async def call_ai(prompt: str, system_prompt: str, max_tokens: int = 500) -> str
return "" return ""
data = response.json() 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) ai_response = re.sub(r'\s*\([a-z_]+_[a-z_]+\)', '', ai_response)
return ai_response return ai_response
except Exception as e: except Exception as e:
@@ -461,9 +475,15 @@ async def teacher_resume(request: Request):
if session["session_type"] == "teacher_test": if session["session_type"] == "teacher_test":
questions = await get_test_questions(conn, session["topic_id"]) questions = await get_test_questions(conn, session["topic_id"])
else: else:
# 🔧 ФИКС (22.09.2026): learning_sessions не хранит lesson_id.
# Берём lesson_id из первого вопроса урока через topic_id сессии.
# Находим первый урок темы, чтобы восстановить сессию.
lesson_id = await conn.fetchval(""" lesson_id = await conn.fetchval("""
SELECT lesson_id FROM learning_sessions WHERE id = $1 SELECT lesson_id FROM learning_lessons
""", session["id"]) WHERE topic_id = $1 AND is_active = TRUE
ORDER BY order_number
LIMIT 1
""", session["topic_id"])
if lesson_id: if lesson_id:
questions = await get_lesson_questions(conn, lesson_id) questions = await get_lesson_questions(conn, lesson_id)
else: else: