diff --git a/Html/apps/services/course_services/learning_progess_process.py b/Html/apps/services/course_services/learning_progess_process.py index f1f6801..ff0cc1c 100644 --- a/Html/apps/services/course_services/learning_progess_process.py +++ b/Html/apps/services/course_services/learning_progess_process.py @@ -100,7 +100,7 @@ def get_multiagents_dialog_list(user_id: str) -> List[Dict]: current_app.logger.error(f"获取MultiAgents对话列表异常: {str(e)}") return [] -def load_learning_progress(user_id: str, material_id: str, chapter_name: str, lesson_name: str) -> Optional[Dict]: +def load_learning_progress(user_id: str, material_id: str, chapter_name: str, lesson_name: str) -> Dict: """ 加载用户学习进度 @@ -111,7 +111,10 @@ def load_learning_progress(user_id: str, material_id: str, chapter_name: str, le - lesson_name: 课时名 返回: - - Optional[Dict]: 学习进度数据,如果不存在则返回None + - Dict: 学习进度数据,包含以下字段: + - exists: bool,表示学习记录是否存在 + - data: Dict,学习进度详细数据,包含chat_historys、scores、chapter_chain_now等字段 + - message: str,操作结果描述 """ try: mongo = current_app.extensions["mongo"] @@ -126,11 +129,44 @@ def load_learning_progress(user_id: str, material_id: str, chapter_name: str, le # 转换ObjectId为字符串 if "_id" in progress: progress["_id"] = str(progress["_id"]) - return progress - return None + return { + "exists": True, + "data": progress, + "message": "学习记录加载成功" + } + # 返回默认值,确保即使没有学习记录也有完整的结构 + return { + "exists": False, + "data": { + "user_id": user_id, + "material_id": material_id, + "chapter_name": chapter_name, + "lesson_name": lesson_name, + "chat_historys": [], + "scores": [], + "chapter_chain_now": None, + "updated_at": datetime.now().isoformat() + }, + "message": "未找到学习记录,返回默认值" + } except Exception as e: - current_app.logger.error(f"加载学习进度失败: {str(e)}") - return None + error_msg = f"加载学习进度失败: {str(e)}" + current_app.logger.error(error_msg) + # 发生错误时返回默认结构,确保系统稳定性 + return { + "exists": False, + "data": { + "user_id": user_id, + "material_id": material_id, + "chapter_name": chapter_name, + "lesson_name": lesson_name, + "chat_historys": [], + "scores": [], + "chapter_chain_now": None, + "updated_at": datetime.now().isoformat() + }, + "message": error_msg + } def upload_learning_progress_to_cloud(progress: Dict) -> Dict: diff --git a/Html/apps/sockets/namespaces.py b/Html/apps/sockets/namespaces.py index 9749784..6ead01a 100644 --- a/Html/apps/sockets/namespaces.py +++ b/Html/apps/sockets/namespaces.py @@ -86,10 +86,11 @@ class AgentNamespace(Namespace): chatmanager.function_manager.add_function(next_chapter, {'user_uuid': user_uuid}) if not chatmanager.restart:# 尝试加载用户学习进度 - progress = load_learning_progress(user_id, course_id, chapter_name, lesson_name) + progress_result = load_learning_progress(user_id, course_id, chapter_name, lesson_name) need_skip_chapters = 0 - if progress: + if progress_result['exists']: + progress = progress_result['data'] # 恢复学习进度数据 if 'scores' in progress: # 根据scores的数量确定需要跳过的章节数 @@ -108,8 +109,10 @@ class AgentNamespace(Namespace): chatmanager.chapter_chain_now = progress['chapter_chain_now'] upload_learning_progress_to_cloud(progress) else: - chatmanager.chat_historys = [] - chatmanager.scores = [] + # 使用默认值,确保有完整的结构 + chatmanager.chat_historys = progress_result['data']['chat_historys'] + chatmanager.scores = progress_result['data']['scores'] + print(f"未找到学习记录,使用默认值: {progress_result['message']}") fmd,fmdp,fsp = load_full_markdown_file(course_id, chapter_name, lesson_name) user_uuid2ase_client[user_uuid] = HSAEngineClient(current_app.config["ASE_ENGINE_URL"], current_app.config["ASE_ENGINE_NAMESPACE"],id=user_id, chatmanager = chatmanager) diff --git a/Html/test_progress.py b/Html/test_progress.py new file mode 100644 index 0000000..4839f60 --- /dev/null +++ b/Html/test_progress.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +import sys +import os + +# Add the project root to the Python path +sys.path.insert(0, '/Users/cakecai/Documents/gitea/hsa/Html') + +from flask import Flask +from flask_pymongo import PyMongo + +# Create a minimal Flask app for testing +app = Flask(__name__) +app.config['MONGO_URI'] = 'mongodb://localhost:27017/test_db' +mongo = PyMongo(app) + +# Register the mongo extension with the app +app.extensions = {'mongo': mongo} + +# Import the function to test +with app.app_context(): + from apps.services.course_services.learning_progess_process import load_learning_progress + + # Test with non-existent record + print("Testing with non-existent record:") + result = load_learning_progress("test_user", "test_course", "test_chapter", "test_lesson") + print(f"Result: {result}") + print(f"Exists: {result['exists']}") + print(f"Data keys: {list(result['data'].keys())}") + print(f"Message: {result['message']}") + print("\n") + + # Test that default values are correctly set + print("Testing default values:") + print(f"Chat historys: {result['data']['chat_historys']}") + print(f"Scores: {result['data']['scores']}") + print(f"Chapter chain now: {result['data']['chapter_chain_now']}") + print(f"Updated at: {result['data']['updated_at']}") + print("\n") + + print("Test completed successfully!")