mobile-mcp-ai 2.6.10__py3-none-any.whl → 2.6.12__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/config.py +32 -0
- mobile_mcp/core/basic_tools_lite.py +801 -1823
- mobile_mcp/core/tool_selection_helper.py +168 -0
- mobile_mcp/mcp_tools/mcp_server.py +337 -455
- {mobile_mcp_ai-2.6.10.dist-info → mobile_mcp_ai-2.6.12.dist-info}/METADATA +19 -12
- {mobile_mcp_ai-2.6.10.dist-info → mobile_mcp_ai-2.6.12.dist-info}/RECORD +10 -9
- {mobile_mcp_ai-2.6.10.dist-info → mobile_mcp_ai-2.6.12.dist-info}/WHEEL +0 -0
- {mobile_mcp_ai-2.6.10.dist-info → mobile_mcp_ai-2.6.12.dist-info}/entry_points.txt +0 -0
- {mobile_mcp_ai-2.6.10.dist-info → mobile_mcp_ai-2.6.12.dist-info}/licenses/LICENSE +0 -0
- {mobile_mcp_ai-2.6.10.dist-info → mobile_mcp_ai-2.6.12.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
工具选择辅助函数 - 帮助 AI 选择正确的工具
|
|
5
|
+
|
|
6
|
+
这个模块提供工具选择建议,帮助 AI 在执行用例时遵循最佳实践。
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from typing import Dict, List, Optional, Tuple
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class ToolSelectionHelper:
|
|
13
|
+
"""工具选择辅助类"""
|
|
14
|
+
|
|
15
|
+
@staticmethod
|
|
16
|
+
def should_use_list_elements(scenario: str) -> Tuple[bool, str]:
|
|
17
|
+
"""判断是否应该使用 mobile_list_elements()
|
|
18
|
+
|
|
19
|
+
Args:
|
|
20
|
+
scenario: 使用场景描述
|
|
21
|
+
|
|
22
|
+
Returns:
|
|
23
|
+
(should_use, reason) 元组
|
|
24
|
+
"""
|
|
25
|
+
scenarios_should_use = [
|
|
26
|
+
"查找元素", "定位元素", "点击元素", "查找按钮", "查找文本",
|
|
27
|
+
"检查元素", "验证元素", "确认元素", "获取元素",
|
|
28
|
+
"进入页面", "新页面", "页面加载",
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
scenario_lower = scenario.lower()
|
|
32
|
+
for keyword in scenarios_should_use:
|
|
33
|
+
if keyword in scenario_lower:
|
|
34
|
+
return True, f"场景'{scenario}'应该优先使用 mobile_list_elements() 获取元素列表"
|
|
35
|
+
|
|
36
|
+
return False, ""
|
|
37
|
+
|
|
38
|
+
@staticmethod
|
|
39
|
+
def should_use_screenshot(scenario: str) -> Tuple[bool, str]:
|
|
40
|
+
"""判断是否应该使用截图
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
scenario: 使用场景描述
|
|
44
|
+
|
|
45
|
+
Returns:
|
|
46
|
+
(should_use, reason) 元组
|
|
47
|
+
"""
|
|
48
|
+
scenarios_should_use = [
|
|
49
|
+
"视觉确认", "查看页面", "页面截图", "展示页面",
|
|
50
|
+
"控件树找不到", "元素不存在", "游戏", "Unity",
|
|
51
|
+
]
|
|
52
|
+
|
|
53
|
+
scenario_lower = scenario.lower()
|
|
54
|
+
for keyword in scenarios_should_use:
|
|
55
|
+
if keyword in scenario_lower:
|
|
56
|
+
return True, f"场景'{scenario}'可以使用截图作为兜底方案"
|
|
57
|
+
|
|
58
|
+
return False, ""
|
|
59
|
+
|
|
60
|
+
@staticmethod
|
|
61
|
+
def get_tool_selection_advice(action: str, elements: Optional[List[Dict]] = None) -> str:
|
|
62
|
+
"""获取工具选择建议
|
|
63
|
+
|
|
64
|
+
Args:
|
|
65
|
+
action: 要执行的操作(如"点击设置按钮")
|
|
66
|
+
elements: 已有的元素列表(如果有)
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
工具选择建议文本
|
|
70
|
+
"""
|
|
71
|
+
advice = []
|
|
72
|
+
|
|
73
|
+
# 如果还没有元素列表,建议先获取
|
|
74
|
+
if not elements:
|
|
75
|
+
advice.append("💡 建议:先调用 mobile_list_elements() 获取元素列表")
|
|
76
|
+
advice.append(" 然后使用 mobile_click_by_text() 或 mobile_click_by_id() 点击")
|
|
77
|
+
advice.append(" 仅在控件树找不到元素时,才使用 mobile_screenshot_with_som()")
|
|
78
|
+
else:
|
|
79
|
+
advice.append("✅ 已有元素列表,可以直接使用 mobile_click_by_text() 或 mobile_click_by_id()")
|
|
80
|
+
advice.append(" 无需再次调用 mobile_list_elements() 或截图")
|
|
81
|
+
|
|
82
|
+
return "\n".join(advice)
|
|
83
|
+
|
|
84
|
+
@staticmethod
|
|
85
|
+
def find_element_in_list(elements: List[Dict],
|
|
86
|
+
text: Optional[str] = None,
|
|
87
|
+
resource_id: Optional[str] = None,
|
|
88
|
+
content_desc: Optional[str] = None) -> Optional[Dict]:
|
|
89
|
+
"""在元素列表中查找元素
|
|
90
|
+
|
|
91
|
+
Args:
|
|
92
|
+
elements: 元素列表
|
|
93
|
+
text: 要查找的文本
|
|
94
|
+
resource_id: 要查找的 resource_id
|
|
95
|
+
content_desc: 要查找的 content_desc
|
|
96
|
+
|
|
97
|
+
Returns:
|
|
98
|
+
找到的元素,如果未找到返回 None
|
|
99
|
+
"""
|
|
100
|
+
for elem in elements:
|
|
101
|
+
if text and elem.get("text") == text:
|
|
102
|
+
return elem
|
|
103
|
+
if resource_id and elem.get("resource_id") == resource_id:
|
|
104
|
+
return elem
|
|
105
|
+
if content_desc and elem.get("content_desc") == content_desc:
|
|
106
|
+
return elem
|
|
107
|
+
|
|
108
|
+
return None
|
|
109
|
+
|
|
110
|
+
@staticmethod
|
|
111
|
+
def suggest_click_method(elements: List[Dict], target_text: str) -> Dict:
|
|
112
|
+
"""建议点击方法
|
|
113
|
+
|
|
114
|
+
Args:
|
|
115
|
+
elements: 元素列表
|
|
116
|
+
target_text: 目标文本
|
|
117
|
+
|
|
118
|
+
Returns:
|
|
119
|
+
建议信息字典
|
|
120
|
+
"""
|
|
121
|
+
# 查找元素
|
|
122
|
+
target_elem = ToolSelectionHelper.find_element_in_list(elements, text=target_text)
|
|
123
|
+
|
|
124
|
+
if target_elem:
|
|
125
|
+
# 元素存在,建议使用文本点击
|
|
126
|
+
return {
|
|
127
|
+
"found": True,
|
|
128
|
+
"method": "mobile_click_by_text",
|
|
129
|
+
"params": {"text": target_text},
|
|
130
|
+
"reason": f"元素'{target_text}'在控件树中找到,建议使用 mobile_click_by_text('{target_text}')"
|
|
131
|
+
}
|
|
132
|
+
else:
|
|
133
|
+
# 元素不存在,建议使用截图
|
|
134
|
+
return {
|
|
135
|
+
"found": False,
|
|
136
|
+
"method": "mobile_screenshot_with_som",
|
|
137
|
+
"params": {},
|
|
138
|
+
"reason": f"元素'{target_text}'在控件树中未找到,建议使用 mobile_screenshot_with_som() 作为兜底方案"
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
# 导出辅助函数
|
|
143
|
+
def should_use_list_elements_first(action: str) -> bool:
|
|
144
|
+
"""判断是否应该优先使用 list_elements
|
|
145
|
+
|
|
146
|
+
Args:
|
|
147
|
+
action: 要执行的操作描述
|
|
148
|
+
|
|
149
|
+
Returns:
|
|
150
|
+
是否应该优先使用 list_elements
|
|
151
|
+
"""
|
|
152
|
+
helper = ToolSelectionHelper()
|
|
153
|
+
should_use, _ = helper.should_use_list_elements(action)
|
|
154
|
+
return should_use
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def get_click_suggestion(elements: List[Dict], target_text: str) -> Dict:
|
|
158
|
+
"""获取点击建议
|
|
159
|
+
|
|
160
|
+
Args:
|
|
161
|
+
elements: 元素列表
|
|
162
|
+
target_text: 目标文本
|
|
163
|
+
|
|
164
|
+
Returns:
|
|
165
|
+
点击建议字典
|
|
166
|
+
"""
|
|
167
|
+
helper = ToolSelectionHelper()
|
|
168
|
+
return helper.suggest_click_method(elements, target_text)
|