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75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
from flask import Flask, request, jsonify, send_from_directory, render_template, redirect, url_for, session
from flask_cors import CORS
import os
import time
import traceback
import json
import re
import sys
import io
import threading
import queue
import contextlib
import signal
import psutil
from dotenv import load_dotenv

# 导入模块路由
from modules.knowledge_base.routes import knowledge_bp
from modules.code_executor.routes import code_executor_bp
from modules.visualization.routes import visualization_bp
from modules.agent_builder.routes import agent_builder_bp

# 加载环境变量
load_dotenv()

app = Flask(__name__)
CORS(app)

# 设置session密钥
app.secret_key = os.getenv("SECRET_KEY", "your_secret_key_here")

# 注册蓝图
app.register_blueprint(knowledge_bp, url_prefix='/api/knowledge')
app.register_blueprint(code_executor_bp, url_prefix='/api/code')
app.register_blueprint(visualization_bp, url_prefix='/api/visualization')
app.register_blueprint(agent_builder_bp, url_prefix='/api/agent')

# 确保目录存在
os.makedirs('static', exist_ok=True)
os.makedirs('uploads', exist_ok=True)
os.makedirs('agents', exist_ok=True)

# 用于代码执行的上下文
execution_contexts = {}

# 硬编码用户(仅用于演示)
users = {
    "teachers": [
        {"username": "teacher", "password": "123456", "name": "李志刚"},
        {"username": "admin", "password": "admin123", "name": "管理员"}
    ],
    "students": [
        {"username": "student1", "password": "123456", "name": "张三"},
        {"username": "student2", "password": "123456", "name": "李四"}
    ]
}

# 学生活动记录存储(实际应用中应使用数据库)
student_activities = {}

def get_memory_usage():
    """获取当前进程的内存使用情况"""
    process = psutil.Process(os.getpid())
    return f"{process.memory_info().rss / 1024 / 1024:.1f} MB"

class CustomStdin:
    def __init__(self, input_queue):
        self.input_queue = input_queue
        self.buffer = ""
    
    def readline(self):
        if not self.buffer:
            self.buffer = self.input_queue.get() + "\n"
        
        result = self.buffer
        self.buffer = ""
        return result

class InteractiveExecution:
    """管理Python代码的交互式执行"""
    def __init__(self, code):
        self.code = code
        self.context_id = str(time.time())
        self.is_complete = False
        self.is_waiting_for_input = False
        self.stdout_buffer = io.StringIO()
        self.last_read_position = 0
        self.input_queue = queue.Queue()
        self.error = None
        self.thread = None
        self.should_terminate = False
    
    def run(self):
        """在单独的线程中启动执行"""
        self.thread = threading.Thread(target=self._execute)
        self.thread.daemon = True
        self.thread.start()
        
        # 给执行一点时间开始
        time.sleep(0.1)
        return self.context_id
    
    def _execute(self):
        """执行代码,处理标准输入输出"""
        try:
            # 保存原始的stdin/stdout
            orig_stdin = sys.stdin
            orig_stdout = sys.stdout
            
            # 创建自定义stdin
            custom_stdin = CustomStdin(self.input_queue)
            
            # 重定向stdin和stdout
            sys.stdin = custom_stdin
            sys.stdout = self.stdout_buffer
            
            try:
                # 检查终止的函数
                self._last_check_time = 0
                
                def check_termination():
                    if self.should_terminate:
                        raise KeyboardInterrupt("Execution terminated by user")
                
                # 设置一个模拟__main__模块的命名空间
                shared_namespace = {
                    "__builtins__": __builtins__,
                    "_check_termination": check_termination,
                    "time": time, 
                    "__name__": "__main__"
                }
                
                # 在这个命名空间中执行用户代码
                try:
                    exec(self.code, shared_namespace)
                except KeyboardInterrupt:
                    print("\nExecution terminated by user")
                
            except Exception as e:
                self.error = {
                    "error": str(e),
                    "traceback": traceback.format_exc()
                }
            
            finally:
                # 恢复原始stdin/stdout
                sys.stdin = orig_stdin
                sys.stdout = orig_stdout
                
                # 标记执行完成
                self.is_complete = True
                
        except Exception as e:
            self.error = {
                "error": str(e),
                "traceback": traceback.format_exc()
            }
            self.is_complete = True
    
    def terminate(self):
        """终止执行"""
        self.should_terminate = True
        
        # 如果在等待输入,放入一些内容以解除阻塞
        if self.is_waiting_for_input:
            self.input_queue.put("\n")
        
        # 给执行一点时间终止
        time.sleep(0.2)
        
        # 标记为完成
        self.is_complete = True
        
        return True
    
    def provide_input(self, user_input):
        """为运行的代码提供输入"""
        self.input_queue.put(user_input)
        self.is_waiting_for_input = False
        return True
    
    def get_output(self):
        """获取stdout缓冲区的当前内容"""
        output = self.stdout_buffer.getvalue()
        return output
        
    def get_new_output(self):
        """只获取自上次读取以来的新输出"""
        current_value = self.stdout_buffer.getvalue()
        if self.last_read_position < len(current_value):
            new_output = current_value[self.last_read_position:]
            self.last_read_position = len(current_value)
            return new_output
        return ""

# 记录活动函数(可在各个操作点调用)
def record_student_activity(username, activity_type, title, agent_id=None, agent_name=None):
    """记录学生活动"""
    if username not in student_activities:
        student_activities[username] = []
    
    # 创建活动记录
    activity = {
        "type": activity_type,  # 'chat', 'code', 'viz', 'mindmap'
        "title": title,
        "timestamp": int(time.time()),
        "agent_id": agent_id,
        "agent_name": agent_name
    }
    
    # 添加到用户活动列表(最多保存20条记录)
    student_activities[username].insert(0, activity)
    if len(student_activities[username]) > 20:
        student_activities[username] = student_activities[username][:20]
    
    return activity

# 登录相关路由
@app.route('/login.html')
def login_page():
    """登录页面"""
    return render_template('login.html')

@app.route('/api/auth/login', methods=['POST'])
def login():
    """处理登录请求"""
    data = request.json
    username = data.get('username')
    password = data.get('password')
    user_type = data.get('type', 'teacher')  # 默认为教师
    
    if user_type == 'teacher':
        user_list = users['teachers']
    else:
        user_list = users['students']
    
    for user in user_list:
        if user['username'] == username and user['password'] == password:
            # 设置session
            session['logged_in'] = True
            session['username'] = username
            session['user_type'] = user_type
            session['user_name'] = user['name']
            
            return jsonify({
                'success': True,
                'user': {
                    'name': user['name'],
                    'type': user_type
                }
            })
    
    return jsonify({
        'success': False,
        'message': '用户名或密码错误'
    }), 401

@app.route('/api/auth/logout', methods=['POST'])
def logout():
    """处理登出请求"""
    session.clear()
    return jsonify({
        'success': True
    })

@app.route('/api/auth/check', methods=['GET'])
def check_auth():
    """检查用户是否已登录"""
    if session.get('logged_in'):
        return jsonify({
            'success': True,
            'user': {
                'name': session.get('user_name'),
                'type': session.get('user_type')
            }
        })
    
    return jsonify({
        'success': False
    }), 401

# 登录验证装饰器
def login_required(f):
    def decorated_function(*args, **kwargs):
        if not session.get('logged_in'):
            return redirect(url_for('login_page'))
        return f(*args, **kwargs)
    decorated_function.__name__ = f.__name__
    return decorated_function

# 教师角色验证装饰器
def teacher_required(f):
    def decorated_function(*args, **kwargs):
        if not session.get('logged_in') or session.get('user_type') != 'teacher':
            return jsonify({
                'success': False,
                'message': '需要教师权限'
            }), 403
        return f(*args, **kwargs)
    decorated_function.__name__ = f.__name__
    return decorated_function

# 学生角色验证装饰器
def student_required(f):
    def decorated_function(*args, **kwargs):
        if not session.get('logged_in') or session.get('user_type') != 'student':
            return jsonify({
                'success': False,
                'message': '需要学生权限'
            }), 403
        return f(*args, **kwargs)
    decorated_function.__name__ = f.__name__
    return decorated_function

# 首页路由
@app.route('/')
def root():
    """重定向到登录页面或主界面"""
    if session.get('logged_in'):
        if session.get('user_type') == 'teacher':
            return redirect('/index.html')
        else:
            return redirect('/student_portal.html')
    return redirect('/login.html')

@app.route('/index.html')
@login_required
def index():
    """教师端主界面"""
    if session.get('user_type') != 'teacher':
        return redirect('/student_portal.html')
    return render_template('index.html')

@app.route('/student_portal.html')
@login_required
def student_portal():
    """学生端门户"""
    if session.get('user_type') != 'student':
        return redirect('/index.html')
    return render_template('student_portal.html')

@app.route('/code_execution.html')
def code_execution_page():
    """代码执行页面"""
    return send_from_directory(os.path.dirname(os.path.abspath(__file__)), 'templates/code_execution.html')

@app.route('/verify_token.html')
def verify_token_page():
    """令牌验证页面"""
    return render_template('token_verification.html')

@app.route('/api/progress/<task_id>', methods=['GET'])
def get_progress(task_id):
    """获取文档处理进度"""
    try:
        # 从知识库模块访问处理任务
        from modules.knowledge_base.routes import processing_tasks
        
        progress_data = processing_tasks.get(task_id, {
            'progress': 0,
            'status': '未找到任务',
            'error': True
        })
        
        return jsonify({"success": True, "data": progress_data})
    except Exception as e:
        traceback.print_exc()
        return jsonify({"success": False, "message": str(e)}), 500

@app.route('/student/<agent_id>')
def student_view(agent_id):
    """学生访问Agent界面"""
    token = request.args.get('token', '')
    
    # 验证Agent存在
    agent_path = os.path.join('agents', f"{agent_id}.json")
    if not os.path.exists(agent_path):
        return render_template('error.html', 
                              message="找不到指定的Agent",
                              error_code=404)
    
    # 加载Agent配置
    with open(agent_path, 'r', encoding='utf-8') as f:
        try:
            agent_config = json.load(f)
        except:
            return render_template('error.html', 
                                  message="Agent配置无效",
                                  error_code=500)
    
    # 验证访问令牌
    if token:
        valid_token = False
        if "distributions" in agent_config:
            for dist in agent_config["distributions"]:
                if dist.get("token") == token:
                    valid_token = True
                    break
        
        if not valid_token:
            return render_template('token_verification.html', 
                                  message="访问令牌无效",
                                  error_code=403)
    
    # 更新使用统计
    if "distributions" in agent_config:
        for dist in agent_config["distributions"]:
            if dist.get("token") == token:
                # 更新分发使用次数
                dist["usage_count"] = dist.get("usage_count", 0) + 1
                
                # 更新Agent使用统计
                if "stats" not in agent_config:
                    agent_config["stats"] = {}
                
                agent_config["stats"]["usage_count"] = agent_config["stats"].get("usage_count", 0) + 1
                agent_config["stats"]["last_used"] = int(time.time())
                
                # 保存更新后的Agent配置
                with open(agent_path, 'w', encoding='utf-8') as f:
                    json.dump(agent_config, f, ensure_ascii=False, indent=2)
                
                break
    
    # 渲染学生页面
    return render_template('student.html', 
                          agent_id=agent_id,
                          agent_name=agent_config.get('name', 'AI学习助手'),
                          agent_description=agent_config.get('description', ''),
                          token=token)

@app.route('/api/student/chat/<agent_id>', methods=['POST'])
def student_chat(agent_id):
    """学生与Agent聊天的API"""
    try:
        data = request.json
        message = data.get('message', '')
        token = data.get('token', '')
        
        if not message:
            return jsonify({"success": False, "message": "消息不能为空"}), 400
        
        # 验证Agent和令牌
        agent_path = os.path.join('agents', f"{agent_id}.json")
        if not os.path.exists(agent_path):
            return jsonify({"success": False, "message": "Agent不存在"}), 404
        
        with open(agent_path, 'r', encoding='utf-8') as f:
            agent_config = json.load(f)
        
        # 验证令牌(如果提供)
        if token and "distributions" in agent_config:
            valid_token = False
            for dist in agent_config["distributions"]:
                if dist.get("token") == token:
                    valid_token = True
                    
                    # 更新使用计数
                    dist["usage_count"] = dist.get("usage_count", 0) + 1
                    break
            
            if not valid_token:
                return jsonify({"success": False, "message": "访问令牌无效"}), 403
                
        # 更新Agent使用统计
        if "stats" not in agent_config:
            agent_config["stats"] = {}
        
        agent_config["stats"]["usage_count"] = agent_config["stats"].get("usage_count", 0) + 1
        agent_config["stats"]["last_used"] = int(time.time())
        
        # 保存更新后的Agent配置
        with open(agent_path, 'w', encoding='utf-8') as f:
            json.dump(agent_config, f, ensure_ascii=False, indent=2)
        
        # 获取Agent关联的知识库和插件
        knowledge_bases = agent_config.get('knowledge_bases', [])
        plugins = agent_config.get('plugins', [])
        
        # 获取学科和指导者信息
        subject = agent_config.get('subject', agent_config.get('name', '通用学科'))
        instructor = agent_config.get('instructor', '教师')
        
        # 创建Generator实例,传入学科和指导者信息
        from modules.knowledge_base.generator import Generator
        generator = Generator(subject=subject, instructor=instructor)
        
        # 检测需要使用的插件
        suggested_plugins = []
            
        # 检测是否需要代码执行插件
        if 'code' in plugins and ('代码' in message or 'python' in message.lower() or '编程' in message or 'code' in message.lower() or 'program' in message.lower()):
            suggested_plugins.append('code')
                
        # 检测是否需要3D可视化插件
        if 'visualization' in plugins and ('3d' in message.lower() or '可视化' in message or '图形' in message):
            suggested_plugins.append('visualization')
                
        # 检测是否需要思维导图插件
        if 'mindmap' in plugins and ('思维导图' in message or 'mindmap' in message.lower()):
            suggested_plugins.append('mindmap')
        
        # 记录活动(添加此部分代码)
        if session.get('logged_in'):
            username = session.get('username')
            # 记录对话活动
            record_student_activity(
                username=username,
                activity_type='chat',
                title=f'与 {agent_config.get("name", "AI助手")} 进行了对话',
                agent_id=agent_id,
                agent_name=agent_config.get('name')
            )
            
            # 如果使用了插件,记录相应的插件活动
            if 'code' in suggested_plugins:
                record_student_activity(
                    username=username,
                    activity_type='code',
                    title='执行了Python代码',
                    agent_id=agent_id,
                    agent_name=agent_config.get('name')
                )
            
            if 'visualization' in suggested_plugins:
                record_student_activity(
                    username=username,
                    activity_type='viz',
                    title='查看了3D可视化图形',
                    agent_id=agent_id,
                    agent_name=agent_config.get('name')
                )
                
            if 'mindmap' in suggested_plugins:
                record_student_activity(
                    username=username,
                    activity_type='mindmap',
                    title='生成了思维导图',
                    agent_id=agent_id,
                    agent_name=agent_config.get('name')
                )
        
        # 检查是否有配置知识库
        if not knowledge_bases:
            # 没有知识库,直接使用模型进行回答
            print(f"\n=== 处理查询: {message} (无知识库) ===")
            
            # 使用空的文档列表调用生成器进行回答
            final_response = ""
            for chunk in generator.generate_stream(message, []):
                if isinstance(chunk, dict):
                    continue  # 跳过处理数据
                final_response += chunk
            
            # 返回生成的回答
            return jsonify({
                "success": True,
                "message": final_response,
                "tools": suggested_plugins
            })
        
        # 有知识库配置,执行知识库查询流程
        try:
            # 导入RAG系统组件
            from modules.knowledge_base.retriever import Retriever
            from modules.knowledge_base.reranker import Reranker
            
            retriever = Retriever()
            reranker = Reranker()
            
            # 构建工具定义 - 将所有知识库作为工具
            tools = []
            
            # 创建工具名称到索引的映射
            tool_to_index = {}
            
            for i, index in enumerate(knowledge_bases):
                display_name = index[4:] if index.startswith('rag_') else index
                
                # 判断是否是视频知识库
                is_video = "视频" in display_name or "video" in display_name.lower()
                
                # 根据内容类型生成适当的工具名称
                if is_video:
                    tool_name = f"video_knowledge_base_{i+1}"
                    description = f"在'{display_name}'视频知识库中搜索,返回带时间戳的视频链接。适用于需要视频讲解的问题。"
                else:
                    tool_name = f"knowledge_base_{i+1}"
                    description = f"在'{display_name}'知识库中搜索专业知识、概念和原理。适用于需要文本说明的问题。"
                
                # 添加工具名到索引的映射
                tool_to_index[tool_name] = index
                
                tools.append({
                    "type": "function",
                    "function": {
                        "name": tool_name,
                        "description": description,
                        "parameters": {
                            "type": "object",
                            "properties": {
                                "keywords": {
                                    "type": "array",
                                    "items": {"type": "string"},
                                    "description": "搜索的关键词列表"
                                }
                            },
                            "required": ["keywords"],
                            "additionalProperties": False
                        },
                        "strict": True
                    }
                })
            
            # 第一阶段:工具选择决策
            print(f"\n=== 处理查询: {message} ===")
            tool_calls = generator.extract_keywords_with_tools(message, tools)
            
            # 如果不需要调用工具,直接回答
            if not tool_calls:
                print("未检测到需要使用知识库,直接回答")
                final_response = ""
                for chunk in generator.generate_stream(message, []):
                    if isinstance(chunk, dict):
                        continue  # 跳过处理数据
                    final_response += chunk
                
                return jsonify({
                    "success": True,
                    "message": final_response,
                    "tools": suggested_plugins
                })
            
            # 收集来自工具执行的所有文档
            all_docs = []
            
            # 执行每个工具调用
            for tool_call in tool_calls:
                try:
                    tool_name = tool_call["function"]["name"]
                    actual_index = tool_to_index.get(tool_name)
                    
                    if not actual_index:
                        print(f"找不到工具名称 '{tool_name}' 对应的索引")
                        continue
                    
                    print(f"\n执行工具 '{tool_name}' -> 使用索引 '{actual_index}'")
                    
                    arguments = json.loads(tool_call["function"]["arguments"])
                    keywords = " ".join(arguments.get("keywords", []))
                    
                    if not keywords:
                        print("没有提供关键词,跳过检索")
                        continue
                    
                    print(f"检索关键词: {keywords}")
                    
                    # 执行检索
                    retrieved_docs, _ = retriever.retrieve(keywords, specific_index=actual_index)
                    print(f"检索到 {len(retrieved_docs)} 个文档")
                    
                    # 重排序文档
                    reranked_docs = reranker.rerank(message, retrieved_docs, actual_index)
                    print(f"重排序完成,排序后有 {len(reranked_docs)} 个文档")
                    
                    # 添加结果
                    all_docs.extend(reranked_docs)
                    
                except Exception as e:
                    print(f"执行工具 '{tool_call.get('function', {}).get('name', '未知')}' 调用时出错: {str(e)}")
                    import traceback
                    traceback.print_exc()
            
            # 如果没有检索到任何文档,直接回答
            if not all_docs:
                print("未检索到任何相关文档,直接回答")
                final_response = ""
                for chunk in generator.generate_stream(message, []):
                    if isinstance(chunk, dict):
                        continue  # 跳过处理数据
                    final_response += chunk
                
                return jsonify({
                    "success": True,
                    "message": final_response,
                    "tools": suggested_plugins
                })
            
            # 按相关性排序
            all_docs.sort(key=lambda x: x.get('rerank_score', 0), reverse=True)
            print(f"\n最终收集到 {len(all_docs)} 个文档用于生成回答")
            
            # 提取参考信息
            references = []
            for i, doc in enumerate(all_docs[:3], 1):  # 只展示前3个参考来源
                file_name = doc['metadata'].get('file_name', '未知文件')
                content = doc['content']
                
                # 提取大约前100字符作为摘要
                summary = content[:100] + ('...' if len(content) > 100 else '')
                
                references.append({
                    'index': i,
                    'file_name': file_name,
                    'content': content,
                    'summary': summary
                })
            
            # 第二阶段:生成最终答案
            final_response = ""
            for chunk in generator.generate_stream(message, all_docs):
                if isinstance(chunk, dict):
                    continue  # 跳过处理数据
                final_response += chunk
            
            # 构建回复
            return jsonify({
                "success": True,
                "message": final_response,
                "tools": suggested_plugins,
                "references": references
            })
            
        except Exception as e:
            import traceback
            traceback.print_exc()
            return jsonify({
                "success": False, 
                "message": f"处理查询时出错: {str(e)}"
            }), 500
            
    except Exception as e:
        import traceback
        traceback.print_exc()
        return jsonify({"success": False, "message": str(e)}), 500

# API端点:获取学生活动记录
@app.route('/api/student/activities', methods=['GET'])
@student_required
def get_student_activities():
    """获取学生活动记录"""
    try:
        username = session.get('username')
        
        # 获取该学生的活动记录
        activities = student_activities.get(username, [])
        
        # 格式化输出
        formatted_activities = []
        for activity in activities:
            # 格式化时间显示
            timestamp = activity['timestamp']
            current_time = int(time.time())
            
            if current_time - timestamp < 86400:  # 24小时内
                if current_time - timestamp < 3600:  # 1小时内
                    time_text = f"{(current_time - timestamp) // 60}分钟前"
                else:
                    time_text = f"今天 {time.strftime('%H:%M', time.localtime(timestamp))}"
            elif current_time - timestamp < 172800:  # 48小时内
                time_text = f"昨天 {time.strftime('%H:%M', time.localtime(timestamp))}"
            else:
                time_text = time.strftime('%m月%d日 %H:%M', time.localtime(timestamp))
            
            formatted_activities.append({
                "type": activity['type'],
                "title": activity['title'],
                "time": time_text,
                "agent_id": activity.get('agent_id'),
                "agent_name": activity.get('agent_name')
            })
        
        return jsonify({
            "success": True,
            "activities": formatted_activities
        })
    
    except Exception as e:
        import traceback
        traceback.print_exc()
        return jsonify({
            "success": False,
            "message": str(e)
        }), 500

# API端点:验证访问令牌
@app.route('/api/verify_token', methods=['POST'])
def verify_token():
    """验证访问令牌有效性"""
    try:
        data = request.json
        token = data.get('token', '')
        agent_id = data.get('agent_id', '')
        
        if not token:
            return jsonify({
                "success": False,
                "message": "未提供访问令牌"
            }), 400
        
        # 如果提供了agent_id,验证特定Agent的令牌
        if agent_id:
            agent_path = os.path.join('agents', f"{agent_id}.json")
            if not os.path.exists(agent_path):
                return jsonify({
                    "success": False,
                    "message": "Agent不存在"
                }), 404
            
            with open(agent_path, 'r', encoding='utf-8') as f:
                agent_config = json.load(f)
            
            # 验证令牌
            if "distributions" in agent_config:
                for dist in agent_config["distributions"]:
                    if dist.get("token") == token:
                        # 检查是否过期
                        if dist.get("expires_at", 0) > 0 and dist.get("expires_at", 0) < time.time():
                            return jsonify({
                                "success": False,
                                "message": "访问令牌已过期"
                            })
                        
                        return jsonify({
                            "success": True,
                            "agent": {
                                "id": agent_id,
                                "name": agent_config.get('name', 'AI学习助手'),
                                "description": agent_config.get('description', ''),
                                "subject": agent_config.get('subject', ''),
                                "instructor": agent_config.get('instructor', '教师')
                            }
                        })
            
            return jsonify({
                "success": False,
                "message": "访问令牌无效"
            })
        
        # 如果没有提供agent_id,搜索所有Agent
        valid_agent = None
        
        for filename in os.listdir('agents'):
            if filename.endswith('.json'):
                agent_path = os.path.join('agents', filename)
                with open(agent_path, 'r', encoding='utf-8') as f:
                    agent_config = json.load(f)
                
                # 验证令牌
                if "distributions" in agent_config:
                    for dist in agent_config["distributions"]:
                        if dist.get("token") == token:
                            # 检查是否过期
                            if dist.get("expires_at", 0) > 0 and dist.get("expires_at", 0) < time.time():
                                continue
                            
                            valid_agent = {
                                "id": agent_config.get('id'),
                                "name": agent_config.get('name', 'AI学习助手'),
                                "description": agent_config.get('description', ''),
                                "subject": agent_config.get('subject', ''),
                                "instructor": agent_config.get('instructor', '教师')
                            }
                            break
                
                if valid_agent:
                    break
        
        if valid_agent:
            return jsonify({
                "success": True,
                "agent": valid_agent
            })
        
        return jsonify({
            "success": False,
            "message": "未找到匹配的访问令牌"
        })
    
    except Exception as e:
        import traceback
        traceback.print_exc()
        return jsonify({
            "success": False,
            "message": f"验证访问令牌时出错: {str(e)}"
        }), 500

# API端点:获取学生的Agent列表
@app.route('/api/student/agents', methods=['GET'])
@student_required
def get_student_agents():
    """获取学生可访问的Agent列表"""
    try:
        # 实际应用中应根据学生ID过滤
        # 这里简化为获取所有Agent
        agents = []
        
        for filename in os.listdir('agents'):
            if filename.endswith('.json'):
                agent_path = os.path.join('agents', filename)
                with open(agent_path, 'r', encoding='utf-8') as f:
                    agent_config = json.load(f)
                
                # 简化信息
                agent_info = {
                    "id": agent_config.get('id'),
                    "name": agent_config.get('name', 'AI学习助手'),
                    "description": agent_config.get('description', ''),
                    "subject": agent_config.get('subject', ''),
                    "instructor": agent_config.get('instructor', '教师'),
                    "plugins": agent_config.get('plugins', []),
                    "last_used": agent_config.get('stats', {}).get('last_used')
                }
                
                # 添加TOKEN(实际应用中应严格控制令牌访问)
                if "distributions" in agent_config and agent_config["distributions"]:
                    # 仅添加第一个分发的令牌
                    agent_info["token"] = agent_config["distributions"][0].get("token")
                
                agents.append(agent_info)
        
        # 按最后使用时间排序
        agents.sort(key=lambda x: x.get('last_used', 0) or 0, reverse=True)
        
        return jsonify({
            "success": True,
            "agents": agents
        })
    
    except Exception as e:
        import traceback
        traceback.print_exc()
        return jsonify({
            "success": False,
            "message": str(e)
        }), 500

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=7860)