-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain_ui.py
More file actions
1235 lines (1000 loc) · 45.4 KB
/
main_ui.py
File metadata and controls
1235 lines (1000 loc) · 45.4 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
#!/usr/bin/env python3
"""
Streamlit 配置界面
用于知识库管理的图形化界面
"""
import streamlit as st
import pandas as pd
import json
import os
import sys
import asyncio
import logging
import subprocess
from pathlib import Path
from datetime import datetime
from typing import List, Dict
# 添加项目根目录到路径
sys.path.append(str(Path(__file__).parent))
from core.Base.vector_db import MobileAgentHelper, MobileAgentVectorDB
from knowledge_manager import KnowledgeManager
from core.phone import Phone
from core.Base.AgentBase import MCPClient
from core.Agent.KnowledgeAssistant import KnowledgeAssistant
from core.Agent.ActionAgent import ActionAssistant
from config_loader import load_config_to_env, check_config
from dotenv import load_dotenv
# 配置文件路径
CONFIG_FILE = "config.json"
def load_config():
"""加载配置文件"""
if os.path.exists(CONFIG_FILE):
try:
with open(CONFIG_FILE, 'r', encoding='utf-8') as f:
return json.load(f)
except Exception as e:
st.error(f"加载配置文件失败: {e}")
return {}
return {}
def save_config(config):
"""保存配置文件"""
try:
with open(CONFIG_FILE, 'w', encoding='utf-8') as f:
json.dump(config, f, ensure_ascii=False, indent=2)
return True
except Exception as e:
st.error(f"保存配置文件失败: {e}")
return False
def apply_env_variables(config):
"""将配置应用到环境变量"""
for key, value in config.items():
if value: # 只设置非空值
os.environ[key] = value
def check_required_config():
"""检查必需的配置是否完整"""
required_vars = ["openai_baseurl", "openai_key"]
config = load_config()
missing_vars = []
for var in required_vars:
if not config.get(var) and not os.getenv(var):
missing_vars.append(var)
return len(missing_vars) == 0, missing_vars
def config_setup_page():
"""配置设置页面"""
st.title("⚙️ 系统配置")
st.markdown("请配置必要的环境变量以使用知识库管理系统")
# 加载现有配置
config = load_config()
# 配置表单
with st.form("config_form"):
st.subheader("🔧 API 配置")
col1, col2 = st.columns(2)
with col1:
openai_baseurl = st.text_input(
"OpenAI Base URL *",
value=config.get("openai_baseurl", ""),
placeholder="https://api.siliconflow.cn/v1",
help="OpenAI API 的基础 URL"
)
openai_key = st.text_input(
"OpenAI API Key *",
value=config.get("openai_key", ""),
placeholder="sk-...",
type="password",
help="OpenAI API 密钥"
)
with col2:
adb_path = st.text_input(
"ADB Path",
value=config.get("ADB_PATH", "adb"),
placeholder="adb",
help="ADB 工具的路径(可选)"
)
st.subheader("🤖 模型配置")
col3, col4 = st.columns(2)
with col3:
knowledge_assistant = st.text_input(
"知识库模型",
value=config.get("KnowledgeAssistant", "zai-org/GLM-4.5-Air"),
placeholder="zai-org/GLM-4.5-Air",
help="用于从知识库获取信息的模型"
)
check_assistant = st.text_input(
"检查模型",
value=config.get("CheckAssistant", "zai-org/GLM-4.5-Air"),
placeholder="zai-org/GLM-4.5-Air",
help="用于描述手机状态的模型"
)
action_assistant = st.text_input(
"动作模型",
value=config.get("ActionAssistant", "zai-org/GLM-4.5-Air"),
placeholder="zai-org/GLM-4.5-Air",
help="用于执行手机指令的模型"
)
with col4:
embeding_model = st.text_input(
"嵌入模型",
value=config.get("embeding_model", "BAAI/bge-m3"),
placeholder="BAAI/bge-m3",
help="用于向量化知识库的模型"
)
reranker_model = st.text_input(
"重排序模型",
value=config.get("reranker_model", "BAAI/bge-reranker-v2-m3"),
placeholder="BAAI/bge-reranker-v2-m3",
help="用于排序检索结果的模型"
)
# 兼容旧版本的 actions_model 配置
actions_model = st.text_input(
"Actions Model (兼容)",
value=config.get("actions_model", "zai-org/GLM-4.5-Air"),
placeholder="zai-org/GLM-4.5-Air",
help="用于动作执行的模型(兼容旧版本)"
)
st.subheader("🗄️ 数据库配置")
db_path = st.text_input(
"数据库路径",
value=config.get("db_path", "mobile_agent_help.db"),
placeholder="mobile_agent_help.db",
help="SQLite 数据库文件路径"
)
# 提交按钮
submitted = st.form_submit_button("💾 保存配置", type="primary")
if submitted:
# 验证必需字段
if not openai_baseurl or not openai_key:
st.error("❌ OpenAI Base URL 和 API Key 是必需的!")
return False
# 保存配置
new_config = {
"openai_baseurl": openai_baseurl,
"openai_key": openai_key,
"ADB_PATH": adb_path,
"KnowledgeAssistant": knowledge_assistant,
"CheckAssistant": check_assistant,
"ActionAssistant": action_assistant,
"embeding_model": embeding_model,
"reranker_model": reranker_model,
"actions_model": actions_model, # 兼容旧版本
"db_path": db_path
}
if save_config(new_config):
# 应用到环境变量
apply_env_variables(new_config)
st.success("✅ 配置保存成功!")
st.session_state.config_completed = True
st.rerun()
return True
else:
st.error("❌ 配置保存失败!")
return False
# 显示当前配置状态
st.markdown("---")
st.subheader("📊 当前配置状态")
status_col1, status_col2, status_col3 = st.columns(3)
with status_col1:
st.markdown("**🔧 API 配置**")
if config.get("openai_baseurl"):
st.success(f"✅ OpenAI Base URL: {config['openai_baseurl']}")
else:
st.error("❌ OpenAI Base URL: 未配置")
if config.get("openai_key"):
masked_key = "*" * (len(config["openai_key"]) - 4) + config["openai_key"][-4:]
st.success(f"✅ OpenAI API Key: {masked_key}")
else:
st.error("❌ OpenAI API Key: 未配置")
with status_col2:
st.markdown("**🤖 模型配置**")
model_configs = [
("KnowledgeAssistant", "知识库模型"),
("CheckAssistant", "检查模型"),
("ActionAssistant", "动作模型"),
("embeding_model", "嵌入模型"),
("reranker_model", "重排序模型")
]
for key, name in model_configs:
if config.get(key):
st.info(f"ℹ️ {name}: {config[key]}")
else:
st.warning(f"⚠️ {name}: 使用默认值")
with status_col3:
st.markdown("**🛠️ 其他配置**")
if config.get("ADB_PATH"):
st.info(f"ℹ️ ADB Path: {config['ADB_PATH']}")
if config.get("db_path"):
st.info(f"ℹ️ Database Path: {config['db_path']}")
if config.get("actions_model"):
st.info(f"ℹ️ Actions Model (兼容): {config['actions_model']}")
return False
def init_session_state():
"""初始化 session state"""
# 加载并应用配置
config = load_config()
apply_env_variables(config)
# 检查配置是否完整
config_complete, missing_vars = check_required_config()
if 'config_completed' not in st.session_state:
st.session_state.config_completed = config_complete
if 'missing_config_vars' not in st.session_state:
st.session_state.missing_config_vars = missing_vars
if 'km' not in st.session_state and st.session_state.config_completed:
try:
# 使用配置的数据库路径
db_path = config.get("db_path", "mobile_agent_help.db")
st.session_state.km = KnowledgeManager(db_path)
except Exception as e:
st.error(f"初始化知识库管理器失败: {e}")
st.session_state.config_completed = False
if 'current_tab' not in st.session_state:
st.session_state.current_tab = "手机助手"
if 'refresh_data' not in st.session_state:
st.session_state.refresh_data = False
def display_header():
"""显示页面头部"""
st.set_page_config(
page_title="手机助手知识库管理",
page_icon="📱",
layout="wide",
initial_sidebar_state="expanded"
)
st.title("📱 手机助手知识库管理系统")
st.markdown("---")
def sidebar_navigation():
"""侧边栏导航"""
st.sidebar.title("🎛️ 功能导航")
tabs = ["手机助手", "应用管理", "文档管理", "搜索测试", "数据导入导出", "系统信息"]
selected_tab = st.sidebar.radio("选择功能", tabs)
st.session_state.current_tab = selected_tab
st.sidebar.markdown("---")
# 显示数据库状态
st.sidebar.subheader("📊 数据库状态")
try:
if hasattr(st.session_state, 'km') and st.session_state.km:
packages = st.session_state.km.vector_db.get_all_packages()
total_apps = len(packages) if packages else 0
total_docs = 0
for pkg in packages:
docs = st.session_state.km.vector_db.search_help_documents(package_name=pkg, k=1000)
total_docs += len(docs)
st.sidebar.metric("应用数量", total_apps)
st.sidebar.metric("文档数量", total_docs)
else:
st.sidebar.warning("⚠️ 知识库未初始化")
except Exception as e:
st.sidebar.error(f"获取数据库状态失败: {e}")
# 显示配置状态
st.sidebar.markdown("---")
st.sidebar.subheader("🔧 配置状态")
config = load_config()
if config.get("openai_baseurl") and config.get("openai_key"):
st.sidebar.success("✅ 配置完整")
else:
st.sidebar.error("❌ 配置不完整")
# 快速重新配置按钮
if st.sidebar.button("🔧 重新配置", key="sidebar_reconfig"):
st.session_state.config_completed = False
st.rerun()
def app_management_tab():
"""应用管理标签页"""
st.header("📦 应用管理")
# 应用添加表单
with st.expander("➕ 添加新应用", expanded=True):
with st.form("add_app_form"):
col1, col2 = st.columns(2)
with col1:
package_name = st.text_input(
"包名 *",
placeholder="例如: com.tencent.mm",
help="Android 应用的包名,必须唯一"
)
app_name = st.text_input(
"应用名称 *",
placeholder="例如: 微信",
help="应用的中文名称"
)
with col2:
app_name_en = st.text_input(
"英文名称",
placeholder="例如: WeChat",
help="应用的英文名称(可选)"
)
description = st.text_area(
"应用描述",
placeholder="简要描述这个应用的功能",
help="应用的详细描述(可选)"
)
submitted = st.form_submit_button("添加应用", type="primary")
if submitted:
if package_name and app_name:
try:
st.session_state.km.add_package_mapping(
package_name, app_name, app_name_en or None, description or None
)
st.success(f"✅ 成功添加应用: {app_name}")
st.session_state.refresh_data = True
st.rerun()
except Exception as e:
st.error(f"❌ 添加失败: {e}")
else:
st.error("❌ 包名和应用名称不能为空")
# 显示现有应用
st.subheader("📋 现有应用列表")
try:
packages = st.session_state.km.vector_db.get_all_packages()
if packages:
apps_data = []
for pkg in packages:
app_info = st.session_state.km.vector_db.get_app_by_package(pkg)
if app_info:
# 获取文档数量
docs = st.session_state.km.vector_db.search_help_documents(package_name=pkg, k=1000)
doc_count = len(docs)
apps_data.append({
"包名": pkg,
"应用名称": app_info.get('app_name', ''),
"英文名称": app_info.get('app_name_en', '') or '-',
"描述": app_info.get('description', '') or '-',
"文档数量": doc_count
})
if apps_data:
df = pd.DataFrame(apps_data)
# 使用 data_editor 显示可编辑表格
st.markdown("💡 **提示**: 可以直接在表格中编辑应用信息")
edited_df = st.data_editor(
df,
use_container_width=True,
num_rows="dynamic",
column_config={
"包名": st.column_config.TextColumn("包名", disabled=True),
"应用名称": st.column_config.TextColumn("应用名称", required=True),
"英文名称": st.column_config.TextColumn("英文名称"),
"描述": st.column_config.TextColumn("描述"),
"文档数量": st.column_config.NumberColumn("文档数量", disabled=True)
}
)
# 保存更改按钮
if st.button("💾 保存更改", type="primary"):
st.info("⚠️ 表格编辑功能需要进一步开发,目前仅支持查看")
else:
st.info("📝 还没有添加任何应用")
else:
st.info("📝 数据库中没有应用信息")
except Exception as e:
st.error(f"❌ 获取应用列表失败: {e}")
def document_management_tab():
"""文档管理标签页"""
st.header("📚 文档管理")
# 获取应用列表用于选择
packages = st.session_state.km.vector_db.get_all_packages()
if not packages:
st.warning("⚠️ 请先添加应用信息")
return
app_options = {}
for pkg in packages:
app_info = st.session_state.km.vector_db.get_app_by_package(pkg)
if app_info:
app_options[f"{app_info['app_name']} ({pkg})"] = pkg
# 文档添加表单
with st.expander("➕ 添加新文档", expanded=True):
with st.form("add_doc_form"):
col1, col2 = st.columns([2, 1])
with col1:
selected_app = st.selectbox(
"选择应用 *",
options=list(app_options.keys()),
help="选择要添加文档的应用"
)
title = st.text_input(
"文档标题 *",
placeholder="例如: 发送朋友圈",
help="文档的标题,用于识别和搜索"
)
with col2:
category = st.text_input(
"分类",
placeholder="例如: 基础操作",
help="文档的分类(可选)"
)
tags_input = st.text_input(
"标签",
placeholder="用逗号分隔,例如: 朋友圈,发送,分享",
help="文档的标签,用逗号分隔(可选)"
)
content = st.text_area(
"文档内容 *",
placeholder="详细描述操作步骤...",
height=150,
help="详细的操作步骤和说明"
)
submitted = st.form_submit_button("添加文档", type="primary")
if submitted:
if selected_app and title and content:
try:
package_name = app_options[selected_app]
app_info = st.session_state.km.vector_db.get_app_by_package(package_name)
app_name = app_info['app_name']
tags = [tag.strip() for tag in tags_input.split(',') if tag.strip()] if tags_input else None
st.session_state.km.add_help_document(
package_name, app_name, title, content, category or None, tags
)
st.success(f"✅ 成功添加文档: {title}")
st.session_state.refresh_data = True
st.rerun()
except Exception as e:
st.error(f"❌ 添加失败: {e}")
else:
st.error("❌ 应用、标题和内容不能为空")
# 显示现有文档
st.subheader("📄 现有文档列表")
# 应用过滤器
filter_col1, filter_col2 = st.columns([1, 1])
with filter_col1:
filter_app = st.selectbox(
"筛选应用",
options=["全部"] + list(app_options.keys()),
help="选择要查看的应用文档"
)
try:
if filter_app == "全部":
# 显示所有文档
all_docs = []
for pkg in packages:
docs = st.session_state.km.vector_db.search_help_documents(package_name=pkg, k=1000)
all_docs.extend(docs)
else:
# 显示特定应用的文档
pkg = app_options[filter_app]
all_docs = st.session_state.km.vector_db.search_help_documents(package_name=pkg, k=1000)
if all_docs:
docs_data = []
for doc in all_docs:
docs_data.append({
"应用": doc['app_name'],
"标题": doc['title'],
"分类": doc.get('category', '') or '-',
"标签": ', '.join(doc.get('tags', [])) if doc.get('tags') else '-',
"内容预览": doc['content'][:100] + "..." if len(doc['content']) > 100 else doc['content']
})
df = pd.DataFrame(docs_data)
# 显示文档表格
st.dataframe(
df,
use_container_width=True,
column_config={
"应用": st.column_config.TextColumn("应用", width="small"),
"标题": st.column_config.TextColumn("标题", width="medium"),
"分类": st.column_config.TextColumn("分类", width="small"),
"标签": st.column_config.TextColumn("标签", width="medium"),
"内容预览": st.column_config.TextColumn("内容预览", width="large")
}
)
st.info(f"📊 共找到 {len(all_docs)} 个文档")
else:
st.info("📝 没有找到文档")
except Exception as e:
st.error(f"❌ 获取文档列表失败: {e}")
def search_test_tab():
"""搜索测试标签页"""
st.header("🔍 搜索测试")
# 搜索表单
with st.form("search_form"):
col1, col2, col3 = st.columns([2, 1, 1])
with col1:
search_query = st.text_input(
"搜索关键词",
placeholder="输入要搜索的内容...",
help="输入关键词搜索相关文档"
)
with col2:
# 应用过滤
packages = st.session_state.km.vector_db.get_all_packages()
app_options = {"全部": None}
for pkg in packages:
app_info = st.session_state.km.vector_db.get_app_by_package(pkg)
if app_info:
app_options[f"{app_info['app_name']} ({pkg})"] = pkg
selected_app = st.selectbox("筛选应用", options=list(app_options.keys()))
with col3:
max_results = st.number_input("最大结果数", min_value=1, max_value=50, value=5)
search_submitted = st.form_submit_button("🔍 搜索", type="primary")
if search_submitted and search_query:
try:
package_filter = app_options[selected_app]
results = st.session_state.km.search_documents(package_filter, search_query, k=max_results)
if results:
st.subheader(f"📋 搜索结果 ({len(results)} 条)")
for i, result in enumerate(results, 1):
with st.expander(f"{i}. {result['title']} - {result['app_name']}", expanded=i<=3):
col1, col2 = st.columns([3, 1])
with col1:
st.markdown(f"**内容**: {result['content']}")
if result.get('category'):
st.markdown(f"**分类**: {result['category']}")
if result.get('tags'):
st.markdown(f"**标签**: {', '.join(result['tags'])}")
with col2:
score = result.get('rerank_score', result.get('similarity', 0))
st.metric("相似度", f"{score:.3f}")
st.markdown(f"**应用**: {result['app_name']}")
st.markdown(f"**包名**: {result['package_name']}")
else:
st.warning("❌ 没有找到相关文档")
except Exception as e:
st.error(f"❌ 搜索失败: {e}")
elif search_submitted:
st.warning("⚠️ 请输入搜索关键词")
def import_export_tab():
"""数据导入导出标签页"""
st.header("📂 数据导入导出")
col1, col2 = st.columns(2)
# 导入功能
with col1:
st.subheader("📥 数据导入")
# 文件上传
uploaded_file = st.file_uploader(
"选择 JSON 文件",
type=['json'],
help="上传包含应用和文档数据的 JSON 文件"
)
if uploaded_file is not None:
try:
# 预览文件内容
json_data = json.load(uploaded_file)
st.subheader("📄 文件预览")
# 显示应用数量
apps_count = len(json_data.get('apps', []))
docs_count = len(json_data.get('documents', []))
col_a, col_b = st.columns(2)
col_a.metric("应用数量", apps_count)
col_b.metric("文档数量", docs_count)
# 显示详细信息
if json_data.get('apps'):
with st.expander("📱 应用列表"):
for app in json_data['apps']:
st.markdown(f"- **{app['app_name']}** ({app['package_name']})")
if json_data.get('documents'):
with st.expander("📚 文档列表"):
for doc in json_data['documents'][:10]: # 只显示前10个
st.markdown(f"- **{doc['title']}** - {doc['app_name']}")
if len(json_data['documents']) > 10:
st.markdown(f"... 还有 {len(json_data['documents']) - 10} 个文档")
# 导入按钮
if st.button("📥 确认导入", type="primary"):
try:
# 保存到临时文件
temp_file = "temp_import.json"
with open(temp_file, 'w', encoding='utf-8') as f:
json.dump(json_data, f, ensure_ascii=False, indent=2)
# 导入数据
st.session_state.km.import_from_json(temp_file)
# 清理临时文件
os.remove(temp_file)
st.success("✅ 数据导入成功!")
st.session_state.refresh_data = True
st.rerun()
except Exception as e:
st.error(f"❌ 导入失败: {e}")
except json.JSONDecodeError:
st.error("❌ JSON 文件格式错误")
except Exception as e:
st.error(f"❌ 文件读取失败: {e}")
# 导出功能
with col2:
st.subheader("📤 数据导出")
# 导出选项
export_app = st.selectbox(
"选择导出范围",
["全部应用"] + [f"{app_info['app_name']} ({pkg})"
for pkg in st.session_state.km.vector_db.get_all_packages()
for app_info in [st.session_state.km.vector_db.get_app_by_package(pkg)]
if app_info]
)
export_filename = st.text_input(
"导出文件名",
value=f"knowledge_export_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
)
if st.button("📤 导出数据", type="primary"):
try:
package_filter = None
if export_app != "全部应用":
# 提取包名
package_filter = export_app.split("(")[-1].rstrip(")")
# 导出数据
st.session_state.km.export_to_json(export_filename, package_filter)
# 提供下载链接
if os.path.exists(export_filename):
with open(export_filename, 'r', encoding='utf-8') as f:
json_str = f.read()
st.download_button(
label="⬇️ 下载导出文件",
data=json_str,
file_name=export_filename,
mime="application/json"
)
# 清理文件
os.remove(export_filename)
st.success("✅ 数据导出成功!")
except Exception as e:
st.error(f"❌ 导出失败: {e}")
# 示例模板
st.markdown("---")
st.subheader("📋 JSON 格式示例")
sample_data = {
"apps": [
{
"package_name": "com.tencent.mm",
"app_name": "微信",
"app_name_en": "WeChat",
"description": "腾讯公司开发的即时通讯软件"
}
],
"documents": [
{
"package_name": "com.tencent.mm",
"app_name": "微信",
"category": "基础操作",
"title": "发送朋友圈",
"content": "发送微信朋友圈需要以下步骤:1.点击导航栏底部的发现按钮,2.点击朋友圈,3.点击右上角拍照分享按钮...",
"tags": ["朋友圈", "发送", "分享"]
}
]
}
st.code(json.dumps(sample_data, ensure_ascii=False, indent=2), language="json")
def system_info_tab():
"""系统信息标签页"""
st.header("⚙️ 系统信息")
# 配置管理区域
st.subheader("🔧 配置管理")
col_config1, col_config2 = st.columns([1, 1])
with col_config1:
if st.button("⚙️ 重新配置环境变量", type="secondary"):
st.session_state.config_completed = False
st.rerun()
with col_config2:
config = load_config()
if config:
config_json = json.dumps(config, ensure_ascii=False, indent=2)
st.download_button(
"📥 下载配置文件",
data=config_json,
file_name="config.json",
mime="application/json"
)
st.markdown("---")
col1, col2 = st.columns(2)
with col1:
st.subheader("🗄️ 数据库信息")
try:
# 数据库文件信息
if hasattr(st.session_state, 'km') and st.session_state.km:
db_path = st.session_state.km.vector_db.db_path
if os.path.exists(db_path):
file_size = os.path.getsize(db_path) / 1024 / 1024 # MB
modified_time = datetime.fromtimestamp(os.path.getmtime(db_path))
st.metric("数据库文件大小", f"{file_size:.2f} MB")
st.metric("最后修改时间", modified_time.strftime("%Y-%m-%d %H:%M:%S"))
st.metric("数据库路径", db_path)
else:
st.warning("⚠️ 数据库文件不存在")
# 统计信息
packages = st.session_state.km.vector_db.get_all_packages()
total_apps = len(packages) if packages else 0
total_docs = 0
for pkg in packages:
docs = st.session_state.km.vector_db.search_help_documents(package_name=pkg, k=1000)
total_docs += len(docs)
st.metric("应用总数", total_apps)
st.metric("文档总数", total_docs)
else:
st.warning("⚠️ 知识库管理器未初始化")
except Exception as e:
st.error(f"❌ 获取数据库信息失败: {e}")
with col2:
st.subheader("🔧 环境配置")
# 环境变量检查
config = load_config()
env_vars = {
"openai_baseurl": "OpenAI Base URL",
"openai_key": "OpenAI API Key",
"KnowledgeAssistant": "知识库模型",
"CheckAssistant": "检查模型",
"ActionAssistant": "动作模型",
"embeding_model": "嵌入模型",
"reranker_model": "重排序模型",
"actions_model": "Actions Model (兼容)",
"ADB_PATH": "ADB Path"
}
for var, display_name in env_vars.items():
# 优先使用配置文件中的值,其次是环境变量
value = config.get(var) or os.getenv(var)
if value:
if "key" in var.lower():
# API Key 类型的值进行掩码处理
masked_value = "*" * (len(value) - 4) + value[-4:] if len(value) > 4 else "***"
st.success(f"✅ {display_name}: {masked_value}")
else:
st.success(f"✅ {display_name}: {value}")
else:
st.error(f"❌ {display_name}: 未设置")
# 系统信息
st.subheader("💻 系统信息")
st.info(f"Python 版本: {sys.version}")
st.info(f"工作目录: {os.getcwd()}")
st.info(f"配置文件: {os.path.abspath(CONFIG_FILE)}")
# 危险操作区域
st.markdown("---")
st.subheader("⚠️ 危险操作")
danger_col1, danger_col2 = st.columns(2)
with danger_col1:
with st.expander("🗑️ 清空数据库", expanded=False):
st.warning("⚠️ 此操作将删除所有应用和文档数据,且不可恢复!")
confirm_text = st.text_input("输入 'DELETE ALL' 确认删除", key="delete_db_confirm")
if st.button("🗑️ 确认清空数据库", type="secondary", key="delete_db_btn"):
if confirm_text == "DELETE ALL":
try:
# 这里需要实现清空数据库的功能
st.error("❌ 清空功能尚未实现")
except Exception as e:
st.error(f"❌ 清空失败: {e}")
else:
st.error("❌ 确认文字输入错误")
with danger_col2:
with st.expander("🔧 重置配置", expanded=False):
st.warning("⚠️ 此操作将删除所有配置信息,需要重新设置!")
confirm_reset = st.text_input("输入 'RESET CONFIG' 确认重置", key="reset_config_confirm")
if st.button("🔧 确认重置配置", type="secondary", key="reset_config_btn"):
if confirm_reset == "RESET CONFIG":
try:
if os.path.exists(CONFIG_FILE):
os.remove(CONFIG_FILE)
st.success("✅ 配置已重置")
st.session_state.config_completed = False
st.rerun()
except Exception as e:
st.error(f"❌ 重置失败: {e}")
else:
st.error("❌ 确认文字输入错误")
def init_phone():
"""
初始化一个 Phone 实例:
1. 获取所有通过 ADB 连接的设备
2. 若有多个设备,提示用户选择其中一个
3. 使用选择的设备 ID 和环境变量指定的 adb_path 创建 Phone 实例
4. 返回 Phone 实例
"""
try:
# 获取 ADB 设备列表
result = subprocess.run(['adb', 'devices'], capture_output=True, text=True, check=True)
lines = result.stdout.strip().split('\n')[1:] # 跳过第一行 header
# 解析在线设备
device_lines = [line for line in lines if line.strip() and '\tdevice' in line]
device_ids = [line.split('\t')[0] for line in device_lines]
if not device_ids:
return None, "❌ 未检测到任何连接的 ADB 设备。"
# 如果只有一个设备,直接使用
if len(device_ids) == 1:
selected_id = device_ids[0]
message = f"✅ 唯一设备已自动选择: {selected_id}"
else:
# 对于多个设备,返回设备列表让用户在UI中选择
return device_ids, "检测到多个设备"
# 获取 adb 路径(支持环境变量或默认 'adb')
adb_path = os.getenv("ADB_PATH", "adb")
# 初始化 Phone 实例
myphone = Phone(id=selected_id, adb_path=adb_path)
return myphone, message
except subprocess.CalledProcessError as e:
return None, f"❌ 执行 ADB 命令失败: {e}"
except Exception as e:
return None, f"❌ 初始化设备时发生未知错误: {e}"
async def execute_phone_task(user_input, selected_device_id=None):
"""
执行手机任务的异步函数
"""
try:
# 首先加载配置文件中的环境变量
load_config_to_env()
# 然后加载 .env 文件(会覆盖配置文件中的同名变量)
load_dotenv()
# 检查配置完整性
if not check_config():
return False, "❌ 系统配置不完整,请先完成系统配置"
# 初始化手机
if selected_device_id:
# 使用指定的设备ID
adb_path = os.getenv("ADB_PATH", "adb")
myphone = Phone(id=selected_device_id, adb_path=adb_path)
phone_status = f"✅ 使用设备: {selected_device_id}"
else:
# 自动检测设备
result = init_phone()
if isinstance(result[0], list):