-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
199 lines (171 loc) · 6.25 KB
/
Copy pathconfig.py
File metadata and controls
199 lines (171 loc) · 6.25 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
import os
import sys
import yaml
def _get_android_external_files_dir():
try:
from jnius import autoclass
PythonActivity = autoclass("org.kivy.android.PythonActivity")
activity = PythonActivity.mActivity
d = activity.getExternalFilesDir(None)
if d is not None:
p = str(d.getAbsolutePath())
if p:
return p
except Exception:
return None
return None
def _yaml_safe_load_file(path):
try:
with open(path, "r", encoding="utf-8") as f:
data = yaml.safe_load(f) or {}
return data if isinstance(data, dict) else {}
except Exception:
return {}
def _is_missing_key(api_key):
value = (api_key or "").strip()
return (not value) or (value.lower() == "your_api_key_here") or (value == "sk-")
def _is_placeholder_cfg(cfg):
try:
model = (cfg.get("MODEL") or "").strip()
if model == "OpenAI":
api_key = (cfg.get("OPENAI_API_KEY") or "").strip()
return _is_missing_key(api_key)
if model == "Qwen":
api_key = (cfg.get("DASHSCOPE_API_KEY") or "").strip()
return _is_missing_key(api_key)
if model == "Doubao":
api_key = (cfg.get("ARK_API_KEY") or "").strip()
return _is_missing_key(api_key)
except Exception:
return True
return False
def _has_real_key_cfg(cfg):
try:
model = (cfg.get("MODEL") or "").strip()
if model == "OpenAI":
api_key = (cfg.get("OPENAI_API_KEY") or "").strip()
return not _is_missing_key(api_key)
if model == "Qwen":
api_key = (cfg.get("DASHSCOPE_API_KEY") or "").strip()
return not _is_missing_key(api_key)
if model == "Doubao":
api_key = (cfg.get("ARK_API_KEY") or "").strip()
return not _is_missing_key(api_key)
except Exception:
return False
return False
def _sync_seed_config(src_path, dst_paths):
if not src_path or not os.path.exists(src_path):
return
try:
with open(src_path, "rb") as rf:
content = rf.read()
except Exception:
return
for dst_path in dst_paths:
if not dst_path:
continue
try:
if os.path.abspath(dst_path) == os.path.abspath(src_path):
continue
except Exception:
pass
try:
os.makedirs(os.path.dirname(dst_path), exist_ok=True)
except Exception:
pass
try:
if os.path.exists(dst_path):
with open(dst_path, "rb") as rf:
if rf.read() == content:
continue
except Exception:
pass
try:
with open(dst_path, "wb") as wf:
wf.write(content)
except Exception:
pass
def load_config():
# PyInstaller打包后的临时目录
if getattr(sys, 'frozen', False):
# 打包后的环境
base_path = sys._MEIPASS
config_path = os.path.join(base_path, 'config.yaml')
else:
app_agent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
seed_candidates = [
os.path.join(app_agent_dir, "config.yml"),
os.path.join(app_agent_dir, "config.yaml"),
]
seed_config_path = None
for p in seed_candidates:
try:
if os.path.exists(p):
seed_config_path = p
break
except Exception:
continue
env_path = (os.environ.get("APPAGENT_CONFIG_PATH") or "").strip()
android_private = (os.environ.get("ANDROID_PRIVATE") or "").strip()
android_external = _get_android_external_files_dir()
config_path = None
if env_path:
try:
if os.path.exists(env_path):
config_path = env_path
except Exception:
config_path = None
if config_path is None and seed_config_path:
config_path = seed_config_path
sync_targets = [
os.path.join("/storage/emulated/0", "AppAgent", "config.yml"),
os.path.join("/storage/emulated/0", "AppAgent", "config.yaml"),
os.path.join("/sdcard", "AppAgent", "config.yml"),
os.path.join("/sdcard", "AppAgent", "config.yaml"),
]
if android_private:
sync_targets.extend([
os.path.join(android_private, "config.yml"),
os.path.join(android_private, "config.yaml"),
])
if android_external:
sync_targets.extend([
os.path.join(android_external, "config.yml"),
os.path.join(android_external, "config.yaml"),
])
if seed_config_path:
_sync_seed_config(seed_config_path, sync_targets)
if config_path is None:
candidate_paths = []
if android_external:
candidate_paths.extend([
os.path.join(android_external, "config.yml"),
os.path.join(android_external, "config.yaml"),
])
candidate_paths.extend([
os.path.join("/storage/emulated/0", "AppAgent", "config.yml"),
os.path.join("/storage/emulated/0", "AppAgent", "config.yaml"),
os.path.join("/sdcard", "AppAgent", "config.yml"),
os.path.join("/sdcard", "AppAgent", "config.yaml"),
])
if android_private:
candidate_paths.extend([
os.path.join(android_private, "config.yml"),
os.path.join(android_private, "config.yaml"),
])
for p in candidate_paths:
try:
if p and os.path.exists(p):
config_path = p
break
except Exception:
continue
if config_path is None:
config_path = os.path.join(app_agent_dir, "config.yaml")
configs = dict(os.environ)
with open(config_path, "r", encoding="utf-8") as file:
yaml_data = yaml.safe_load(file) or {}
configs.update(yaml_data)
configs["_CONFIG_PATH"] = config_path
return configs