mobile-mcp-ai 2.1.2__py3-none-any.whl → 2.5.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mobile_mcp/__init__.py +34 -0
- mobile_mcp/config.py +142 -0
- mobile_mcp/core/basic_tools_lite.py +3266 -0
- {core → mobile_mcp/core}/device_manager.py +2 -2
- mobile_mcp/core/dynamic_config.py +272 -0
- mobile_mcp/core/ios_client_wda.py +569 -0
- mobile_mcp/core/ios_device_manager_wda.py +306 -0
- {core → mobile_mcp/core}/mobile_client.py +279 -39
- mobile_mcp/core/template_matcher.py +429 -0
- mobile_mcp/core/templates/close_buttons/auto_x_0112_151217.png +0 -0
- mobile_mcp/core/templates/close_buttons/auto_x_0112_152037.png +0 -0
- mobile_mcp/core/templates/close_buttons/auto_x_0112_152840.png +0 -0
- mobile_mcp/core/templates/close_buttons/auto_x_0112_153256.png +0 -0
- mobile_mcp/core/templates/close_buttons/auto_x_0112_154847.png +0 -0
- mobile_mcp/core/templates/close_buttons/gray_x_stock_ad.png +0 -0
- {core → mobile_mcp/core}/utils/smart_wait.py +3 -3
- mobile_mcp/mcp_tools/__init__.py +10 -0
- mobile_mcp/mcp_tools/mcp_server.py +1071 -0
- mobile_mcp_ai-2.5.8.dist-info/METADATA +469 -0
- mobile_mcp_ai-2.5.8.dist-info/RECORD +32 -0
- mobile_mcp_ai-2.5.8.dist-info/entry_points.txt +2 -0
- mobile_mcp_ai-2.5.8.dist-info/licenses/LICENSE +201 -0
- mobile_mcp_ai-2.5.8.dist-info/top_level.txt +1 -0
- core/ai/__init__.py +0 -11
- core/ai/ai_analyzer.py +0 -197
- core/ai/ai_config.py +0 -116
- core/ai/ai_platform_adapter.py +0 -399
- core/ai/smart_test_executor.py +0 -520
- core/ai/test_generator.py +0 -365
- core/ai/test_generator_from_history.py +0 -391
- core/ai/test_generator_standalone.py +0 -293
- core/assertion/__init__.py +0 -9
- core/assertion/smart_assertion.py +0 -341
- core/basic_tools.py +0 -377
- core/h5/__init__.py +0 -10
- core/h5/h5_handler.py +0 -548
- core/ios_client.py +0 -219
- core/ios_device_manager.py +0 -252
- core/locator/__init__.py +0 -10
- core/locator/cursor_ai_auto_analyzer.py +0 -119
- core/locator/cursor_vision_helper.py +0 -414
- core/locator/mobile_smart_locator.py +0 -1640
- core/locator/position_analyzer.py +0 -813
- core/locator/script_updater.py +0 -157
- core/nl_test_runner.py +0 -585
- core/smart_app_launcher.py +0 -334
- core/smart_tools.py +0 -311
- mcp/__init__.py +0 -8
- mcp/mcp_server.py +0 -1919
- mcp/mcp_server_simple.py +0 -476
- mobile_mcp_ai-2.1.2.dist-info/METADATA +0 -567
- mobile_mcp_ai-2.1.2.dist-info/RECORD +0 -45
- mobile_mcp_ai-2.1.2.dist-info/entry_points.txt +0 -2
- mobile_mcp_ai-2.1.2.dist-info/top_level.txt +0 -4
- vision/__init__.py +0 -10
- vision/vision_locator.py +0 -404
- {core → mobile_mcp/core}/__init__.py +0 -0
- {core → mobile_mcp/core}/utils/__init__.py +0 -0
- {core → mobile_mcp/core}/utils/logger.py +0 -0
- {core → mobile_mcp/core}/utils/operation_history_manager.py +0 -0
- {utils → mobile_mcp/utils}/__init__.py +0 -0
- {utils → mobile_mcp/utils}/logger.py +0 -0
- {utils → mobile_mcp/utils}/xml_formatter.py +0 -0
- {utils → mobile_mcp/utils}/xml_parser.py +0 -0
- {mobile_mcp_ai-2.1.2.dist-info → mobile_mcp_ai-2.5.8.dist-info}/WHEEL +0 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
mobile_mcp
|
core/ai/__init__.py
DELETED
core/ai/ai_analyzer.py
DELETED
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
# -*- coding: utf-8 -*-
|
|
3
|
-
"""
|
|
4
|
-
AI分析器 - 智能兜底,分析候选元素
|
|
5
|
-
"""
|
|
6
|
-
import sys
|
|
7
|
-
import json
|
|
8
|
-
from typing import Dict, List, Optional
|
|
9
|
-
from .ai_config import ai_config
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
class AIAnalyzer:
|
|
13
|
-
"""AI分析器 - 用于智能兜底"""
|
|
14
|
-
|
|
15
|
-
def __init__(self):
|
|
16
|
-
"""初始化AI分析器"""
|
|
17
|
-
self.config = ai_config
|
|
18
|
-
|
|
19
|
-
async def analyze_candidates(self, query: str, candidates: List[Dict], context: str = "") -> Optional[Dict]:
|
|
20
|
-
"""
|
|
21
|
-
分析候选元素,选择最佳匹配
|
|
22
|
-
|
|
23
|
-
Args:
|
|
24
|
-
query: 用户查询
|
|
25
|
-
candidates: 候选元素列表
|
|
26
|
-
context: 上下文信息(可选)
|
|
27
|
-
|
|
28
|
-
Returns:
|
|
29
|
-
最佳匹配的元素信息
|
|
30
|
-
"""
|
|
31
|
-
if not self.config.is_configured():
|
|
32
|
-
print(" ⚠️ AI未配置,跳过AI分析", file=sys.stderr)
|
|
33
|
-
return None
|
|
34
|
-
|
|
35
|
-
if not candidates:
|
|
36
|
-
print(" ⚠️ 没有候选元素,跳过AI分析", file=sys.stderr)
|
|
37
|
-
return None
|
|
38
|
-
|
|
39
|
-
try:
|
|
40
|
-
import httpx
|
|
41
|
-
|
|
42
|
-
# 构建提示词
|
|
43
|
-
prompt = self._build_prompt(query, candidates, context)
|
|
44
|
-
|
|
45
|
-
print(f" 🤖 调用AI分析(模型: {self.config.model})...", file=sys.stderr)
|
|
46
|
-
|
|
47
|
-
# 调用通义千问API
|
|
48
|
-
async with httpx.AsyncClient(timeout=self.config.timeout) as client:
|
|
49
|
-
response = await client.post(
|
|
50
|
-
f"{self.config.api_base}/chat/completions",
|
|
51
|
-
headers={
|
|
52
|
-
"Authorization": f"Bearer {self.config.api_key}",
|
|
53
|
-
"Content-Type": "application/json"
|
|
54
|
-
},
|
|
55
|
-
json={
|
|
56
|
-
"model": self.config.model,
|
|
57
|
-
"messages": [
|
|
58
|
-
{
|
|
59
|
-
"role": "system",
|
|
60
|
-
"content": "你是一个专业的移动端UI元素分析助手。请根据用户查询和候选元素,选择最匹配的元素,并返回JSON格式的结果。"
|
|
61
|
-
},
|
|
62
|
-
{
|
|
63
|
-
"role": "user",
|
|
64
|
-
"content": prompt
|
|
65
|
-
}
|
|
66
|
-
],
|
|
67
|
-
"temperature": 0.1, # 低温度,更确定性
|
|
68
|
-
"response_format": {"type": "json_object"} # 强制返回JSON
|
|
69
|
-
}
|
|
70
|
-
)
|
|
71
|
-
|
|
72
|
-
if response.status_code != 200:
|
|
73
|
-
print(f" ❌ AI调用失败: HTTP {response.status_code}", file=sys.stderr)
|
|
74
|
-
print(f" {response.text}", file=sys.stderr)
|
|
75
|
-
return None
|
|
76
|
-
|
|
77
|
-
result = response.json()
|
|
78
|
-
ai_response = result['choices'][0]['message']['content']
|
|
79
|
-
|
|
80
|
-
# 解析AI返回的JSON
|
|
81
|
-
ai_result = json.loads(ai_response)
|
|
82
|
-
|
|
83
|
-
if not ai_result.get('selected_index'):
|
|
84
|
-
print(f" ⚠️ AI未能选择元素", file=sys.stderr)
|
|
85
|
-
return None
|
|
86
|
-
|
|
87
|
-
selected_index = ai_result['selected_index'] - 1 # 转换为0-based索引
|
|
88
|
-
|
|
89
|
-
if selected_index < 0 or selected_index >= len(candidates):
|
|
90
|
-
print(f" ⚠️ AI返回的索引无效: {selected_index + 1}", file=sys.stderr)
|
|
91
|
-
return None
|
|
92
|
-
|
|
93
|
-
selected = candidates[selected_index]
|
|
94
|
-
confidence = ai_result.get('confidence', 85)
|
|
95
|
-
reason = ai_result.get('reason', '未提供原因')
|
|
96
|
-
|
|
97
|
-
print(f" ✅ AI选择: 候选{selected_index + 1}/{len(candidates)}", file=sys.stderr)
|
|
98
|
-
print(f" 元素: {selected.get('text') or selected.get('content_desc') or selected.get('class_name')}", file=sys.stderr)
|
|
99
|
-
print(f" 置信度: {confidence}%", file=sys.stderr)
|
|
100
|
-
print(f" 理由: {reason}", file=sys.stderr)
|
|
101
|
-
|
|
102
|
-
return {
|
|
103
|
-
'element': selected.get('text') or selected.get('content_desc') or query,
|
|
104
|
-
'ref': self._get_ref(selected),
|
|
105
|
-
'confidence': confidence,
|
|
106
|
-
'method': 'ai_analysis',
|
|
107
|
-
'reason': reason
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
except Exception as e:
|
|
111
|
-
print(f" ❌ AI分析异常: {e}", file=sys.stderr)
|
|
112
|
-
import traceback
|
|
113
|
-
traceback.print_exc()
|
|
114
|
-
return None
|
|
115
|
-
|
|
116
|
-
def _build_prompt(self, query: str, candidates: List[Dict], context: str) -> str:
|
|
117
|
-
"""构建AI提示词"""
|
|
118
|
-
# 格式化候选元素
|
|
119
|
-
candidates_text = []
|
|
120
|
-
for i, elem in enumerate(candidates, 1):
|
|
121
|
-
text = elem.get('text', '')
|
|
122
|
-
desc = elem.get('content_desc', '')
|
|
123
|
-
resource_id = elem.get('resource_id', '')
|
|
124
|
-
class_name = elem.get('class_name', '')
|
|
125
|
-
bounds = elem.get('bounds', '')
|
|
126
|
-
clickable = elem.get('clickable', False)
|
|
127
|
-
focusable = elem.get('focusable', False)
|
|
128
|
-
|
|
129
|
-
# 计算位置
|
|
130
|
-
position = "未知"
|
|
131
|
-
if bounds:
|
|
132
|
-
try:
|
|
133
|
-
# bounds格式: "[x1,y1][x2,y2]"
|
|
134
|
-
coords = bounds.replace('[', '').replace(']', ',').split(',')
|
|
135
|
-
y1 = int(coords[1])
|
|
136
|
-
if y1 < 400:
|
|
137
|
-
position = "顶部"
|
|
138
|
-
elif y1 < 800:
|
|
139
|
-
position = "中部"
|
|
140
|
-
else:
|
|
141
|
-
position = "底部"
|
|
142
|
-
except:
|
|
143
|
-
pass
|
|
144
|
-
|
|
145
|
-
parts = [f"候选{i}:"]
|
|
146
|
-
if text:
|
|
147
|
-
parts.append(f"文本=\"{text[:50]}\"")
|
|
148
|
-
if desc:
|
|
149
|
-
parts.append(f"描述=\"{desc[:50]}\"")
|
|
150
|
-
if resource_id:
|
|
151
|
-
parts.append(f"ID={resource_id}")
|
|
152
|
-
parts.append(f"类型={class_name}")
|
|
153
|
-
parts.append(f"位置={position}")
|
|
154
|
-
if clickable:
|
|
155
|
-
parts.append("可点击")
|
|
156
|
-
if focusable:
|
|
157
|
-
parts.append("可聚焦")
|
|
158
|
-
|
|
159
|
-
candidates_text.append(" | ".join(parts))
|
|
160
|
-
|
|
161
|
-
prompt = f"""
|
|
162
|
-
用户查询: "{query}"
|
|
163
|
-
|
|
164
|
-
页面上有以下候选元素:
|
|
165
|
-
{chr(10).join(candidates_text)}
|
|
166
|
-
|
|
167
|
-
{f"上下文信息: {context}" if context else ""}
|
|
168
|
-
|
|
169
|
-
请分析哪个元素最匹配用户查询,并返回JSON格式:
|
|
170
|
-
{{
|
|
171
|
-
"selected_index": <1到{len(candidates)}的数字>,
|
|
172
|
-
"confidence": <置信度0-100>,
|
|
173
|
-
"reason": "<选择理由>"
|
|
174
|
-
}}
|
|
175
|
-
|
|
176
|
-
分析要点:
|
|
177
|
-
1. 优先匹配文本/描述的语义
|
|
178
|
-
2. 考虑元素类型是否合理(如输入框应该是EditText)
|
|
179
|
-
3. 考虑元素位置(如"顶部按钮"、"底部输入框")
|
|
180
|
-
4. 考虑用户意图(如"点击"需要可点击元素)
|
|
181
|
-
"""
|
|
182
|
-
return prompt
|
|
183
|
-
|
|
184
|
-
def _get_ref(self, element: Dict) -> str:
|
|
185
|
-
"""获取元素引用"""
|
|
186
|
-
# 优先级: resource_id > content_desc > text > bounds
|
|
187
|
-
return (
|
|
188
|
-
element.get('resource_id') or
|
|
189
|
-
element.get('content_desc') or
|
|
190
|
-
element.get('text') or
|
|
191
|
-
element.get('bounds', '')
|
|
192
|
-
)
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
# 全局实例
|
|
196
|
-
ai_analyzer = AIAnalyzer()
|
|
197
|
-
|
core/ai/ai_config.py
DELETED
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
# -*- coding: utf-8 -*-
|
|
3
|
-
"""
|
|
4
|
-
AI配置模块 - 从根目录.env读取配置
|
|
5
|
-
支持通义千问API
|
|
6
|
-
"""
|
|
7
|
-
import os
|
|
8
|
-
from pathlib import Path
|
|
9
|
-
from typing import Optional
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
class AIConfig:
|
|
13
|
-
"""AI配置类"""
|
|
14
|
-
|
|
15
|
-
def __init__(self):
|
|
16
|
-
"""初始化配置"""
|
|
17
|
-
self._load_env()
|
|
18
|
-
|
|
19
|
-
def _load_env(self):
|
|
20
|
-
"""从根目录.env加载配置"""
|
|
21
|
-
# 查找根目录.env文件
|
|
22
|
-
# 当前路径: backend/mobile_mcp/core/ai/ai_config.py
|
|
23
|
-
# 项目根目录: douzi-ai/
|
|
24
|
-
current_file = Path(__file__)
|
|
25
|
-
# 向上5层: ai/ -> core/ -> mobile_mcp/ -> backend/ -> douzi-ai/
|
|
26
|
-
project_root = current_file.parent.parent.parent.parent.parent
|
|
27
|
-
env_file = project_root / '.env'
|
|
28
|
-
|
|
29
|
-
# 如果项目根目录没有,尝试backend目录
|
|
30
|
-
if not env_file.exists():
|
|
31
|
-
backend_root = current_file.parent.parent.parent.parent
|
|
32
|
-
env_file = backend_root / '.env'
|
|
33
|
-
|
|
34
|
-
if not env_file.exists():
|
|
35
|
-
print(f"⚠️ 未找到.env文件: {env_file}")
|
|
36
|
-
return
|
|
37
|
-
|
|
38
|
-
# 读取.env文件
|
|
39
|
-
with open(env_file, 'r', encoding='utf-8') as f:
|
|
40
|
-
for line in f:
|
|
41
|
-
line = line.strip()
|
|
42
|
-
if not line or line.startswith('#'):
|
|
43
|
-
continue
|
|
44
|
-
|
|
45
|
-
# 解析键值对
|
|
46
|
-
if '=' in line:
|
|
47
|
-
key, value = line.split('=', 1)
|
|
48
|
-
key = key.strip()
|
|
49
|
-
value = value.strip().strip('"').strip("'")
|
|
50
|
-
|
|
51
|
-
# 设置环境变量(如果尚未设置)
|
|
52
|
-
if key not in os.environ:
|
|
53
|
-
os.environ[key] = value
|
|
54
|
-
|
|
55
|
-
@property
|
|
56
|
-
def api_key(self) -> Optional[str]:
|
|
57
|
-
"""获取API密钥"""
|
|
58
|
-
# 优先级:QWEN_API_KEY > OPENAI_API_KEY
|
|
59
|
-
return (
|
|
60
|
-
os.getenv('QWEN_API_KEY') or
|
|
61
|
-
os.getenv('DASHSCOPE_API_KEY') or
|
|
62
|
-
os.getenv('OPENAI_API_KEY')
|
|
63
|
-
)
|
|
64
|
-
|
|
65
|
-
@property
|
|
66
|
-
def api_base(self) -> Optional[str]:
|
|
67
|
-
"""获取API基础URL"""
|
|
68
|
-
return (
|
|
69
|
-
os.getenv('QWEN_API_BASE') or
|
|
70
|
-
os.getenv('OPENAI_API_BASE') or
|
|
71
|
-
'https://dashscope.aliyuncs.com/compatible-mode/v1' # 通义千问默认地址
|
|
72
|
-
)
|
|
73
|
-
|
|
74
|
-
@property
|
|
75
|
-
def model(self) -> str:
|
|
76
|
-
"""获取模型名称"""
|
|
77
|
-
return (
|
|
78
|
-
os.getenv('QWEN_MODEL') or
|
|
79
|
-
os.getenv('OPENAI_MODEL') or
|
|
80
|
-
'qwen-plus' # 通义千问默认模型
|
|
81
|
-
)
|
|
82
|
-
|
|
83
|
-
@property
|
|
84
|
-
def timeout(self) -> int:
|
|
85
|
-
"""获取超时时间(秒)"""
|
|
86
|
-
return int(os.getenv('AI_TIMEOUT', '30'))
|
|
87
|
-
|
|
88
|
-
def is_configured(self) -> bool:
|
|
89
|
-
"""检查是否已配置"""
|
|
90
|
-
return bool(self.api_key)
|
|
91
|
-
|
|
92
|
-
def __repr__(self):
|
|
93
|
-
"""字符串表示"""
|
|
94
|
-
return (
|
|
95
|
-
f"AIConfig(\n"
|
|
96
|
-
f" api_base={self.api_base}\n"
|
|
97
|
-
f" model={self.model}\n"
|
|
98
|
-
f" api_key={'***' + self.api_key[-4:] if self.api_key else 'None'}\n"
|
|
99
|
-
f" timeout={self.timeout}s\n"
|
|
100
|
-
f")"
|
|
101
|
-
)
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
# 全局配置实例
|
|
105
|
-
ai_config = AIConfig()
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
if __name__ == '__main__':
|
|
109
|
-
# 测试配置
|
|
110
|
-
print("=" * 60)
|
|
111
|
-
print("AI配置测试")
|
|
112
|
-
print("=" * 60)
|
|
113
|
-
print(ai_config)
|
|
114
|
-
print()
|
|
115
|
-
print(f"是否已配置: {ai_config.is_configured()}")
|
|
116
|
-
|