-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
1045 lines (839 loc) · 28.9 KB
/
app.py
File metadata and controls
1045 lines (839 loc) · 28.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Flask应用主入口
统一使用ChatService处理对话:
- 支持流式和非流式输出
- 支持工具调用
- 支持命令确认机制(所有执行类命令需要用户确认)
- 支持会话管理(创建、删除、切换)
"""
import uuid
import json
import os
import asyncio
from flask import Flask, render_template, request, jsonify, session, Response
from werkzeug.utils import secure_filename
from config import Config
from services.chat_service import chat_service
from services.knowledge_base.knowledge_base_service import knowledge_base_service
from services.knowledge_base.document_service import document_service
# 创建Flask应用
app = Flask(__name__)
app.config.from_object(Config)
# 设置Secret Key(用于session)
app.secret_key = Config.SECRET_KEY
def get_session_id():
"""获取或创建会话ID"""
if 'session_id' not in session:
session['session_id'] = str(uuid.uuid4())
return session['session_id']
@app.route('/')
def index():
"""主页"""
return render_template('index.html')
# ==================== 会话管理 API ====================
@app.route('/sessions', methods=['GET'])
def get_sessions():
"""
获取所有会话列表
返回:
- sessions: 会话列表
- current_session_id: 当前会话ID(Flask session中存储的ID)
"""
try:
# 获取当前 Flask session 中的 session_id(不自动创建)
current_session_id = session.get('session_id', None)
sessions = chat_service.get_all_sessions()
return jsonify({
'sessions': sessions,
'current_session_id': current_session_id
})
except Exception as e:
return jsonify({
'error': f'获取会话列表失败: {str(e)}'
}), 500
@app.route('/sessions/new', methods=['POST'])
def new_session():
"""
创建新会话
返回:
- session_id: 新会话的ID
"""
try:
# 创建新会话
new_session_id = chat_service.create_session()
# 切换到新会话
session['session_id'] = new_session_id
return jsonify({
'status': 'ok',
'session_id': new_session_id,
'message': '新会话已创建'
})
except Exception as e:
return jsonify({
'error': f'创建会话失败: {str(e)}'
}), 500
@app.route('/sessions/<session_id>', methods=['DELETE'])
def delete_session(session_id: str):
"""
删除会话
Args:
session_id: 要删除的会话ID
返回:
- status: 操作状态
"""
try:
# 不允许删除当前会话,需要先切换
current_session_id = get_session_id()
if session_id == current_session_id:
return jsonify({
'error': '无法删除当前会话,请先切换到其他会话'
}), 400
# 删除会话
success = chat_service.delete_session(session_id)
if success:
return jsonify({
'status': 'ok',
'message': '会话已删除'
})
else:
return jsonify({
'error': '会话不存在'
}), 404
except Exception as e:
return jsonify({
'error': f'删除会话失败: {str(e)}'
}), 500
@app.route('/sessions/<session_id>/switch', methods=['POST'])
def switch_session(session_id: str):
"""
切换到指定会话
Args:
session_id: 目标会话ID
返回:
- status: 操作状态
- session_id: 当前会话ID
"""
try:
# 确保会话存在
session_info = chat_service.get_session_info(session_id)
if not session_info:
# 如果会话不存在,创建一个新的
chat_service.create_session()
# 使用传入的 session_id
session['session_id'] = session_id
else:
# 切换到已存在的会话
session['session_id'] = session_id
return jsonify({
'status': 'ok',
'session_id': session_id,
'session_info': session_info
})
except Exception as e:
return jsonify({
'error': f'切换会话失败: {str(e)}'
}), 500
@app.route('/sessions/<session_id>/info', methods=['GET'])
def get_session_detail(session_id: str):
"""
获取会话详情
Args:
session_id: 会话ID
返回:
- session_info: 会话信息
- messages: 消息历史
"""
try:
session_info = chat_service.get_session_info(session_id)
if not session_info:
return jsonify({
'error': '会话不存在'
}), 404
messages = chat_service.get_history(session_id)
return jsonify({
'session_info': session_info,
'messages': messages
})
except Exception as e:
return jsonify({
'error': f'获取会话详情失败: {str(e)}'
}), 500
# ==================== 联网搜索设置 API ====================
@app.route('/web-search/status', methods=['GET'])
def get_web_search_status():
"""
获取当前会话的联网搜索状态
返回:
- enabled: 是否启用联网搜索
- session_id: 当前会话ID
"""
try:
session_id = get_session_id()
enabled = chat_service.get_web_search_status(session_id)
return jsonify({
'enabled': enabled,
'session_id': session_id
})
except Exception as e:
return jsonify({
'error': f'获取联网搜索状态失败: {str(e)}'
}), 500
@app.route('/web-search/toggle', methods=['POST'])
def toggle_web_search():
"""
切换联网搜索开关
请求体:
- enabled: 是否启用联网搜索(boolean)
返回:
- enabled: 新的状态
- session_id: 当前会话ID
"""
try:
data = request.json
enabled = data.get('enabled', False)
session_id = get_session_id()
# 设置联网搜索状态
chat_service.set_web_search_enabled(session_id, enabled)
return jsonify({
'enabled': enabled,
'session_id': session_id,
'message': f'联网搜索已{"开启" if enabled else "关闭"}'
})
except Exception as e:
return jsonify({
'error': f'切换联网搜索失败: {str(e)}'
}), 500
# ==================== 对话 API ====================
@app.route('/chat', methods=['POST'])
def chat():
"""
处理聊天请求(非流式)
返回:
- type: "response" | "confirmation_required" | "onboarding_required" | "error"
- output: 助手回复(正常情况)
- command: 需要确认的命令
- operation: 操作类型
- working_dir: 执行路径
- message: 引导消息(onboarding_required 时)
"""
try:
data = request.json
user_input = data.get('message', '')
if not user_input:
return jsonify({'error': '消息不能为空'}), 400
session_id = get_session_id()
# 调用ChatService处理对话
result = chat_service.chat(user_input, session_id)
# 根据返回类型处理
if result['type'] == 'confirmation_required':
return jsonify({
'type': 'confirmation_required',
'command': result['command'],
'command_type': result.get('command_type', 'execute'),
'operation': result.get('operation', '执行命令'),
'working_dir': result.get('working_dir', ''),
'is_dangerous': result.get('is_dangerous', False),
'reason': result.get('reason', ''),
'message': result.get('message', '需要用户确认'),
'session_id': result['session_id']
})
# 首次对话引导
if result['type'] == 'onboarding_required':
return jsonify({
'type': 'onboarding_required',
'message': result['message'],
'session_id': result['session_id']
})
return jsonify(result)
except Exception as e:
return jsonify({
'type': 'error',
'output': f'服务器错误: {str(e)}'
}), 500
@app.route('/onboarding/reply', methods=['POST'])
def onboarding_reply():
"""
处理首次对话引导的用户回复
请求体:
- message: 用户回复内容
返回:
- type: "response"
- output: 确认消息
- onboarding_completed: true
"""
try:
data = request.json
user_reply = data.get('message', '')
if not user_reply:
return jsonify({'error': '回复不能为空'}), 400
session_id = get_session_id()
# 处理引导回复
result = chat_service.handle_onboarding_reply(user_reply, session_id)
return jsonify(result)
except Exception as e:
return jsonify({
'type': 'error',
'output': f'处理引导回复失败: {str(e)}'
}), 500
@app.route('/chat/stream', methods=['POST'])
def chat_stream():
"""
流式聊天接口
使用Server-Sent Events (SSE) 返回流式数据
"""
data = request.json
user_input = data.get('message', '')
session_id = get_session_id()
def generate():
try:
if not user_input:
yield f"data: {json.dumps({'type': 'error', 'content': '消息不能为空'}, ensure_ascii=False)}\n\n"
return
# 调用流式服务
for chunk in chat_service.chat_stream(user_input, session_id):
# 立即发送事件,不缓冲
event_data = f"data: {json.dumps(chunk, ensure_ascii=False)}\n\n"
yield event_data
except Exception as e:
yield f"data: {json.dumps({'type': 'error', 'content': f'服务器错误: {str(e)}'}, ensure_ascii=False)}\n\n"
return Response(
generate(),
mimetype='text/event-stream',
headers={
'Cache-Control': 'no-cache',
'X-Accel-Buffering': 'no',
'Connection': 'keep-alive',
'Transfer-Encoding': 'chunked'
}
)
@app.route('/confirm', methods=['POST'])
def confirm_command():
"""
确认并执行命令(非流式)
请求体:
- command: 要执行的命令
- user_message: 用户原始消息(可选,用于继续对话)
"""
try:
data = request.json
command = data.get('command', '')
user_message = data.get('user_message', '')
if not command:
return jsonify({'error': '命令不能为空'}), 400
session_id = get_session_id()
# 执行确认的命令
result = chat_service.confirm_command(
command,
session_id,
user_message
)
return jsonify(result)
except Exception as e:
return jsonify({
'type': 'error',
'output': f'执行命令失败: {str(e)}'
}), 500
@app.route('/confirm/stream', methods=['POST'])
def confirm_command_stream():
"""
确认并执行命令(流式)
请求体:
- command: 要执行的命令
- user_message: 用户原始消息(可选,用于继续对话)
"""
data = request.json
command = data.get('command', '')
user_message = data.get('user_message', '')
session_id = get_session_id()
def generate():
try:
if not command:
yield f"data: {json.dumps({'type': 'error', 'content': '命令不能为空'}, ensure_ascii=False)}\n\n"
return
# 流式执行确认的命令
for chunk in chat_service.confirm_command_stream(
command,
session_id,
user_message
):
yield f"data: {json.dumps(chunk, ensure_ascii=False)}\n\n"
except Exception as e:
yield f"data: {json.dumps({'type': 'error', 'content': f'执行命令失败: {str(e)}'}, ensure_ascii=False)}\n\n"
return Response(
generate(),
mimetype='text/event-stream',
headers={
'Cache-Control': 'no-cache',
'X-Accel-Buffering': 'no',
'Connection': 'keep-alive'
}
)
@app.route('/cancel', methods=['POST'])
def cancel_command():
"""
取消命令执行,返回上下文给 LLM 继续处理
请求体:
- command: 用户取消的命令
- user_message: 用户原始消息(可选,用于继续对话)
"""
data = request.json
command = data.get('command', '')
user_message = data.get('user_message', '')
session_id = get_session_id()
def generate():
try:
if not command:
yield f"data: {json.dumps({'type': 'done', 'content': '用户已取消'}, ensure_ascii=False)}\n\n"
return
# 流式处理取消的命令
for chunk in chat_service.cancel_command_stream(
command,
session_id,
user_message
):
yield f"data: {json.dumps(chunk, ensure_ascii=False)}\n\n"
except Exception as e:
yield f"data: {json.dumps({'type': 'error', 'content': f'处理取消失败: {str(e)}'}, ensure_ascii=False)}\n\n"
return Response(
generate(),
mimetype='text/event-stream',
headers={
'Cache-Control': 'no-cache',
'X-Accel-Buffering': 'no',
'Connection': 'keep-alive'
}
)
@app.route('/history', methods=['GET'])
def get_history():
"""获取对话历史"""
session_id = get_session_id()
history = chat_service.get_history(session_id)
return jsonify({
'messages': history,
'session_id': session_id
})
@app.route('/stop', methods=['POST'])
def stop_generation():
"""
取消当前流式输出
用户可以在模型输出期间调用此接口取消输出
"""
session_id = get_session_id()
chat_service.request_cancel(session_id)
return jsonify({
'status': 'ok',
'message': '已发送取消请求'
})
@app.route('/reset', methods=['POST'])
def reset():
"""
重置当前会话(清空消息历史)
注意:这不会删除会话,只是清空消息历史
"""
session_id = get_session_id()
chat_service.clear_history(session_id)
return jsonify({
'status': 'ok',
'message': '会话历史已清空'
})
# ==================== 递归限制继续执行 API ====================
@app.route('/continue', methods=['POST'])
def continue_task():
"""
继续执行因递归限制暂停的任务(非流式)
当任务执行达到步数限制时,用户可以选择继续执行。
这将重置计数器并继续之前的任务。
返回:
- type: "response" | "recursion_limit_reached" | "error"
- output: 助手回复
"""
try:
session_id = get_session_id()
result = chat_service.continue_task(session_id)
if result['type'] == 'confirmation_required':
return jsonify({
'type': 'confirmation_required',
'command': result['command'],
'command_type': result.get('command_type', 'execute'),
'operation': result.get('operation', '执行命令'),
'working_dir': result.get('working_dir', ''),
'is_dangerous': result.get('is_dangerous', False),
'reason': result.get('reason', ''),
'message': result.get('message', '需要用户确认'),
'session_id': result['session_id']
})
return jsonify(result)
except Exception as e:
return jsonify({
'type': 'error',
'output': f'继续执行失败: {str(e)}'
}), 500
@app.route('/continue/stream', methods=['POST'])
def continue_task_stream():
"""
继续执行因递归限制暂停的任务(流式)
使用 Server-Sent Events (SSE) 返回流式数据
"""
session_id = get_session_id()
def generate():
try:
for chunk in chat_service.continue_task_stream(session_id):
yield f"data: {json.dumps(chunk, ensure_ascii=False)}\n\n"
except Exception as e:
yield f"data: {json.dumps({'type': 'error', 'content': f'继续执行失败: {str(e)}'}, ensure_ascii=False)}\n\n"
return Response(
generate(),
mimetype='text/event-stream',
headers={
'Cache-Control': 'no-cache',
'X-Accel-Buffering': 'no',
'Connection': 'keep-alive'
}
)
# ==================== 知识库管理 API ====================
@app.route('/knowledge-bases', methods=['GET'])
def list_knowledge_bases():
"""
获取所有知识库列表
"""
try:
kbs = knowledge_base_service.list_knowledge_bases()
return jsonify({
'knowledge_bases': kbs
})
except Exception as e:
return jsonify({
'error': f'获取知识库列表失败: {str(e)}'
}), 500
@app.route('/knowledge-bases', methods=['POST'])
def create_knowledge_base():
"""
创建知识库
请求体:
- name: 知识库名称
- description: 描述(可选)
"""
try:
data = request.json
name = data.get('name', '').strip()
description = data.get('description', '').strip()
if not name:
return jsonify({'error': '知识库名称不能为空'}), 400
kb = knowledge_base_service.create_knowledge_base(name, description)
return jsonify({
'status': 'ok',
'knowledge_base': kb
})
except Exception as e:
return jsonify({
'error': f'创建知识库失败: {str(e)}'
}), 500
@app.route('/knowledge-bases/<kb_id>', methods=['GET'])
def get_knowledge_base(kb_id: str):
"""
获取知识库详情
"""
try:
kb = knowledge_base_service.get_knowledge_base(kb_id)
if not kb:
return jsonify({'error': '知识库不存在'}), 404
return jsonify({
'knowledge_base': kb
})
except Exception as e:
return jsonify({
'error': f'获取知识库详情失败: {str(e)}'
}), 500
@app.route('/knowledge-bases/<kb_id>', methods=['DELETE'])
def delete_knowledge_base(kb_id: str):
"""
删除知识库
"""
try:
success = knowledge_base_service.delete_knowledge_base(kb_id)
if not success:
return jsonify({'error': '知识库不存在'}), 404
return jsonify({
'status': 'ok',
'message': '知识库已删除'
})
except Exception as e:
return jsonify({
'error': f'删除知识库失败: {str(e)}'
}), 500
@app.route('/knowledge-bases/<kb_id>/documents', methods=['GET'])
def list_documents(kb_id: str):
"""
获取知识库下的文档列表
"""
try:
docs = knowledge_base_service.get_documents(kb_id)
return jsonify({
'documents': docs
})
except Exception as e:
return jsonify({
'error': f'获取文档列表失败: {str(e)}'
}), 500
@app.route('/knowledge-bases/<kb_id>/documents', methods=['POST'])
def upload_document(kb_id: str):
"""
上传文档到知识库
支持的文件类型:.txt, .md, .json, .csv, .pdf, .doc, .docx
"""
try:
# 检查是否有文件
if 'file' not in request.files:
return jsonify({'error': '没有上传文件'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': '没有选择文件'}), 400
# 检查文件类型
filename = secure_filename(file.filename)
file_ext = os.path.splitext(filename)[1].lower()
if file_ext not in document_service.get_supported_file_types():
return jsonify({
'error': f'不支持的文件类型: {file_ext}。支持的类型: {document_service.get_supported_file_types()}'
}), 400
# 检查文件大小
file.seek(0, 2) # 移动到文件末尾
file_size = file.tell()
file.seek(0) # 重置到开头
if file_size > Config.MAX_FILE_SIZE:
return jsonify({
'error': f'文件大小超过限制 ({Config.MAX_FILE_SIZE / 1024 / 1024}MB)'
}), 400
# 检查知识库是否存在
kb = knowledge_base_service.get_knowledge_base(kb_id)
if not kb:
return jsonify({'error': '知识库不存在'}), 404
# 保存文件
upload_dir = os.path.join(Config.UPLOAD_FOLDER, str(kb_id))
os.makedirs(upload_dir, exist_ok=True)
# 使用UUID作为文件名,避免冲突
file_id = str(uuid.uuid4())
file_path = os.path.join(upload_dir, f"{file_id}{file_ext}")
file.save(file_path)
# 异步处理文档
async def process_doc():
return await document_service.process_document(file_path, filename, kb_id)
# 运行异步处理
try:
loop = asyncio.get_event_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
result = loop.run_until_complete(process_doc())
except Exception as e:
# 数据库操作异常
return jsonify({
'error': f'文档处理失败: {str(e)}'
}), 500
if result.get('status') == 'completed':
return jsonify({
'status': 'ok',
'message': '文档上传成功',
'document_id': result.get('document_id'),
'chunk_count': result.get('chunk_count')
})
else:
return jsonify({
'error': result.get('error', '文档处理失败')
}), 500
except Exception as e:
return jsonify({
'error': f'上传文档失败: {str(e)}'
}), 500
@app.route('/documents/<doc_id>', methods=['DELETE'])
def delete_document(doc_id: str):
"""
删除文档
"""
try:
success = knowledge_base_service.delete_document(doc_id)
if not success:
return jsonify({'error': '文档不存在'}), 404
return jsonify({
'status': 'ok',
'message': '文档已删除'
})
except Exception as e:
return jsonify({
'error': f'删除文档失败: {str(e)}'
}), 500
# ==================== 会话知识库设置 API ====================
@app.route('/sessions/<session_id>/knowledge-base', methods=['GET'])
def get_session_knowledge_base(session_id: str):
"""
获取会话当前选中的知识库
"""
try:
kb_id = chat_service.get_knowledge_base_id(session_id)
kb_info = None
if kb_id:
kb_info = knowledge_base_service.get_knowledge_base(kb_id)
return jsonify({
'knowledge_base_id': kb_id,
'knowledge_base': kb_info
})
except Exception as e:
return jsonify({
'error': f'获取知识库设置失败: {str(e)}'
}), 500
@app.route('/sessions/<session_id>/knowledge-base', methods=['PUT'])
def set_session_knowledge_base(session_id: str):
"""
设置会话的知识库
请求体:
- knowledge_base_id: 知识库ID(null表示不使用知识库)
"""
try:
data = request.json
kb_id = data.get('knowledge_base_id') # 可以是 None
# 验证知识库是否存在(如果不是 None)
if kb_id:
kb = knowledge_base_service.get_knowledge_base(kb_id)
if not kb:
return jsonify({'error': '知识库不存在'}), 404
chat_service.set_knowledge_base_id(session_id, kb_id)
return jsonify({
'status': 'ok',
'knowledge_base_id': kb_id,
'message': '知识库设置已更新'
})
except Exception as e:
return jsonify({
'error': f'设置知识库失败: {str(e)}'
}), 500
# ==================== 团队模式状态查询 API ====================
@app.route('/team/status', methods=['GET'])
def get_team_status():
"""
获取当前会话的团队执行状态
用于前端轮询获取任务进度
"""
try:
session_id = get_session_id()
status = chat_service.get_team_status(session_id)
if status is None:
return jsonify({
'status': 'idle',
'message': '当前没有团队执行'
})
return jsonify({
'status': 'ok',
'team_status': status
})
except Exception as e:
return jsonify({
'error': f'获取团队状态失败: {str(e)}'
}), 500
@app.route('/team/task-board', methods=['GET'])
def get_task_board():
"""
获取任务看板视图
"""
try:
session_id = get_session_id()
view = chat_service.get_task_board_view(session_id)
return jsonify({
'status': 'ok',
'task_board': view
})
except Exception as e:
return jsonify({
'error': f'获取任务看板失败: {str(e)}'
}), 500
# ==================== 监控指标 API ====================
@app.route('/monitoring/bootstrap', methods=['GET'])
def get_bootstrap_metrics():
"""
获取 Bootstrap 加载指标
查询参数:
- hours: 统计时间窗口(默认24小时)
"""
try:
hours = request.args.get('hours', 24, type=int)
from services.monitoring import metrics_collector
stats = metrics_collector.get_bootstrap_stats(hours=hours)
return jsonify({
'status': 'ok',
'bootstrap_stats': stats
})
except Exception as e:
return jsonify({
'error': f'获取Bootstrap指标失败: {str(e)}'
}), 500
@app.route('/monitoring/retrieval', methods=['GET'])
def get_retrieval_metrics():
"""
获取检索指标
查询参数:
- hours: 统计时间窗口(默认24小时)
"""
try:
hours = request.args.get('hours', 24, type=int)
from services.monitoring import metrics_collector
stats = metrics_collector.get_retrieval_stats(hours=hours)
return jsonify({
'status': 'ok',
'retrieval_stats': stats
})
except Exception as e:
return jsonify({
'error': f'获取检索指标失败: {str(e)}'
}), 500
@app.route('/monitoring/knowledge', methods=['GET'])
def get_knowledge_metrics():
"""
获取知识库状态指标
"""
try:
from services.monitoring import metrics_collector, collect_knowledge_metrics
from services.db_manager import db_manager
# 收集最新指标
with db_manager.get_session() as db_session:
metrics = collect_knowledge_metrics(db_session)
return jsonify({
'status': 'ok',
'knowledge_metrics': metrics.to_dict()
})
except Exception as e:
return jsonify({
'error': f'获取知识库指标失败: {str(e)}'
}), 500