Jarvis-Brain 0.1.11.2__tar.gz → 0.1.11.10__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: Jarvis_Brain
3
- Version: 0.1.11.2
3
+ Version: 0.1.11.10
4
4
  Summary: Jarvis brain mcp
5
5
  Requires-Python: >=3.10
6
6
  Requires-Dist: beautifulsoup4
@@ -7,6 +7,7 @@ import os
7
7
  import time
8
8
  from typing import Any
9
9
 
10
+ import DrissionPage
10
11
  from fastmcp import FastMCP
11
12
 
12
13
  from tools.browser_manager import BrowserManager
@@ -162,13 +163,13 @@ def register_check_selector(mcp: FastMCP, browser_manager):
162
163
  attr_output = json.dumps(ele_attr_list, ensure_ascii=False)
163
164
  # 对attr_output逐个截断,截断的长度为:一轮最大token除以元素个数+3个点+两个引号和逗号
164
165
  return dp_mcp_message_pack(
165
- f"已完成tab页:【{tab_id}】对:【{css_selector}】的检查",
166
+ f"已完成tab页:【{tab_id}】对:【{css_selector}】的检查,当前选中了 {len(target_eles)} 个元素",
166
167
  tab_id=tab_id,
167
168
  selector=css_selector,
168
169
  selector_ele_exist=exist_flag,
169
170
  page_size=page_size,
170
171
  offset=offset,
171
- attr_output=attr_output
172
+ attr_output=attr_output,
172
173
  )
173
174
 
174
175
 
@@ -236,27 +237,33 @@ def register_assert_waf(mcp: FastMCP, browser_manager):
236
237
 
237
238
 
238
239
  def register_click_action(mcp: FastMCP, browser_manager):
239
- @mcp.tool(name="click_action", description="尝试点击tab页中的元素,返回元素是否可以被点击,以及是否点击成功。")
240
- async def click_action(browser_port: int, tab_id: str, css_selector: str) -> dict[str, Any]:
240
+ @mcp.tool(name="click_action",
241
+ description="尝试点击tab页中的元素,返回元素是否可以被点击,以及是否点击成功。"
242
+ "其中target_element_index默认为0,当传入的Selector可以定位到多个元素时,需要传入target_element_index指定具体点击目标 ")
243
+ async def click_action(browser_port: int, tab_id: str, css_selector: str, target_element_index: int = 0) -> dict[
244
+ str, Any]:
241
245
  _browser = browser_manager.get_browser(browser_port)
242
246
  target_tab = _browser.get_tab(tab_id)
243
247
  css_selector = css_selector
244
248
  if "css:" not in css_selector:
245
249
  css_selector = "css:" + css_selector
246
250
  target_eles = target_tab.eles(css_selector)
247
- click_success = False
248
- element_clickable = False
249
- if len(target_eles) == 1:
250
- target_element = target_eles[0]
251
- element_clickable = target_element.states.is_clickable
252
- try:
253
- target_element.click()
254
- click_success = True
255
- except Exception as e:
256
- click_success = False
257
- message = f"tab页:【{tab_id}】点击【{css_selector}】 {'成功' if click_success else '失败'} 了"
251
+ # click_success = False
252
+ # element_clickable = False
253
+ # if len(target_eles) == 1:
254
+ target_element = target_eles[target_element_index]
255
+ element_clickable = target_element.states.is_clickable
256
+ try:
257
+ target_element.click()
258
+ click_success = True
259
+ except Exception as e:
260
+ click_success = False
261
+ if target_element_index > 0:
262
+ message = f"tab页:【{tab_id}】点击【{css_selector}】【index={target_element_index}】的元素 {'成功' if click_success else '失败'} 了"
258
263
  else:
259
- message = f"tab页:【{tab_id}】传入的css_selector找到了{len(target_eles)}个元素,请确保传入的css_selector可以找到唯一的一个元素"
264
+ message = f"tab页:【{tab_id}】点击【{css_selector}】 {'成功' if click_success else '失败'}"
265
+ # else:
266
+ # message = f"tab页:【{tab_id}】传入的css_selector找到了{len(target_eles)}个元素,请确保传入的css_selector可以找到唯一的一个元素"
260
267
  return dp_mcp_message_pack(
261
268
  message=message,
262
269
  browser_port=browser_port,
@@ -308,16 +315,19 @@ def register_scroll_action(mcp: FastMCP, browser_manager):
308
315
 
309
316
 
310
317
  def register_get_screenshot(mcp: FastMCP, browser_manager):
311
- @mcp.tool(name="get_tab_screenshot", description="尝试对传入tab页进行截图,并将截图压缩为1M大小png图片,会返回截图保存路径")
318
+ @mcp.tool(name="get_tab_screenshot",
319
+ description="尝试对传入tab页进行截图,并将截图压缩为1M大小png图片,会返回截图保存路径")
312
320
  async def get_tab_screenshot(browser_port: int, tab_id: str) -> dict[str, Any]:
313
321
  _browser = browser_manager.get_browser(browser_port)
314
322
  target_tab = _browser.get_tab(tab_id)
323
+ target_tab.wait.doc_loaded()
315
324
  if not os.path.exists(html_source_code_local_save_path):
316
325
  os.makedirs(html_source_code_local_save_path)
317
326
  timestamp = int(time.time() * 1000)
318
- origin_png = target_tab.get_screenshot(as_bytes="png")
319
- compress_png = compress_image_bytes(origin_png)
320
- image_path = os.path.join(html_source_code_local_save_path, f"{browser_port}_{tab_id}_{timestamp}.png")
327
+ # time.sleep(1)
328
+ origin_png = target_tab.get_screenshot(as_bytes="jpg", full_page=True)
329
+ compress_png = compress_image_bytes(origin_png, 0.5)
330
+ image_path = os.path.join(html_source_code_local_save_path, f"{browser_port}_{tab_id}_{timestamp}.jpg")
321
331
  with open(image_path, "wb") as f:
322
332
  f.write(compress_png)
323
333
  return dp_mcp_message_pack(
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "Jarvis_Brain" # 别人下载时用的名字,必须在 PyPI 上唯一
3
- version = "0.1.11.2"
3
+ version = "0.1.11.10"
4
4
  description = "Jarvis brain mcp"
5
5
  dependencies = [
6
6
  "fastmcp",
@@ -140,7 +140,10 @@ def check_data_packet(packet: DataPacket, client: DPProxyClient):
140
140
  data = packet.request.postData
141
141
  domain = urlparse(url).netloc
142
142
  body = packet.response.body
143
- body_str = json.dumps(body, ensure_ascii=False, separators=(',', ':'))
143
+ if isinstance(body, dict):
144
+ body_str = json.dumps(body, ensure_ascii=False, separators=(',', ':'))
145
+ else:
146
+ body_str = str(body)
144
147
  body_str_list = [body_str[i:i + one_turn_max_token] for i in range(0, len(body_str), one_turn_max_token)]
145
148
  body_completed = True
146
149
  packet_filter = client.packet_filter
@@ -155,9 +158,9 @@ def check_data_packet(packet: DataPacket, client: DPProxyClient):
155
158
  continue
156
159
  if (index + 1) != len(body_str_list):
157
160
  body_completed = False
158
- if packet.response:
161
+ try:
159
162
  response_headers = packet.response.headers
160
- else:
163
+ except TypeError:
161
164
  response_headers = {}
162
165
  temp_dict = {
163
166
  "url": url,
@@ -171,59 +174,4 @@ def check_data_packet(packet: DataPacket, client: DPProxyClient):
171
174
  client.packet_queue.append(temp_dict)
172
175
 
173
176
 
174
- def check_data_packet(packet: DataPacket, client: DPProxyClient):
175
- """
176
- 封装监听到的数据包,并将其存放在client的packet_queue中
177
- :param packet:
178
- :param client:
179
- :return:
180
- """
181
- url = packet.url
182
- method = packet.request.method
183
- data = None
184
- if packet.request.hasPostData:
185
- data = packet.request.postData
186
- domain = urlparse(url).netloc
187
- body = packet.response.body
188
- body_str = json.dumps(body, ensure_ascii=False, separators=(',', ':'))
189
- body_str_list = [body_str[i:i + one_turn_max_token] for i in range(0, len(body_str), one_turn_max_token)]
190
- body_completed = True
191
- packet_filter = client.packet_filter
192
- domain_filter = packet_filter.get("domain_filter", None)
193
- method_filter = packet_filter.get("method_filter", ["GET", "POST"])
194
- for index, body_str in enumerate(body_str_list):
195
- # 如果给了domain_filter并且domain没有在domain_filter中时跳过该数据包
196
- if domain_filter and domain not in domain_filter:
197
- continue
198
- # 如果method没有在method_filter中,则跳过该数据包
199
- if method not in method_filter:
200
- continue
201
- if (index + 1) != len(body_str_list):
202
- body_completed = False
203
- temp_dict = {
204
- "url": url,
205
- "body_completed": body_completed,
206
- "method": method,
207
- "request_data": data,
208
- "request_headers": dict(packet.request.headers),
209
- "response_headers": dict(packet.response.headers),
210
- "response_body_segment": body_str.replace("\\", ""),
211
- }
212
- client.packet_queue.append(temp_dict)
213
-
214
-
215
177
  client_manager = DPProxyClientManager()
216
-
217
- # if __name__ == '__main__':
218
- # co = ChromiumOptions().set_user_agent(
219
- # "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Mobile Safari/537.36")
220
- # tab = ChromiumPage(co).latest_tab
221
- # client = DPProxyClient(tab, self_kill=False)
222
- # # client = CaptchaClient(tab, self_kill=True)
223
- # tab = client.get_driver(True)
224
- # url = "https://api.toutiaoapi.com/feoffline/hotspot_and_local/html/hot_list/index.html?client_extra_params=%7B%22custom_log_pb%22%3A%22%7B%5C%22style_id%5C%22%3A%5C%2240030%5C%22%2C%5C%22entrance_hotspot%5C%22%3A%5C%22search%5C%22%2C%5C%22location%5C%22%3A%5C%22hot_board%5C%22%2C%5C%22category_name%5C%22%3A%5C%22hotboard_light%5C%22%7D%22%7D&count=50&log_pb=%7B%22style_id%22%3A%2240030%22%2C%22entrance_hotspot%22%3A%22search%22%2C%22location%22%3A%22hot_board%22%2C%22category_name%22%3A%22hotboard_light%22%7D&only_hot_list=1&tab_name=stream&enter_keyword=%23%E7%BE%8E%E5%9B%BD%E9%80%80%E5%87%BA66%E4%B8%AA%E5%9B%BD%E9%99%85%E7%BB%84%E7%BB%87%23"
225
- # tab.get(url)
226
- # for _ in range(5056):
227
- # new_packet = client.pop_first_packet()
228
- # print(new_packet, "23")
229
- # time.sleep(1)
@@ -11,84 +11,95 @@ from PIL import Image
11
11
  import io
12
12
 
13
13
  compress_html_js = """
14
- function getSimplifiedDOM(node) {
15
- // 1. 处理文本节点
16
- if (node.nodeType === Node.TEXT_NODE) {
17
- const text = node.textContent.trim();
18
- // 限制文本长度,避免大段文章消耗 token,保留前100个字符通常足够定位
19
- return text ? text.slice(0, 100) + (text.length > 100 ? '...' : '') : null;
20
- }
21
-
22
- // 2. 过滤无用标签
23
- const ignoreTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'IFRAME', 'SVG', 'LINK', 'META'];
24
- if (ignoreTags.includes(node.tagName)) return null;
25
- if (node.nodeType !== Node.ELEMENT_NODE) return null;
26
-
27
- // 3. 过滤不可见元素
28
- const style = window.getComputedStyle(node);
29
- if (style.display === 'none' || style.visibility === 'hidden' || style.opacity === '0') return null;
30
- // 过滤宽高太小的元素(往往是埋点空像素)
31
- const rect = node.getBoundingClientRect();
32
- if (rect.width === 0 || rect.height === 0) return null;
33
-
34
- // --- 开始构建标签字符串 ---
35
- const tagName = node.tagName.toLowerCase();
36
- let tagStr = tagName;
37
-
38
- // A. 基础标识符 (ID 和 Class)
39
- if (node.id) tagStr += `#${node.id}`;
40
- if (node.className && typeof node.className === 'string') {
41
- // 过滤掉 Tailwind 等太长且无语义的 class,保留有意义的业务 class
42
- // 这里简单处理,全部保留,让 LLM 自己判断
43
- const classes = node.className.trim().split(/\s+/);
44
- if (classes.length > 0) tagStr += `.${classes.join('.')}`;
45
- }
46
-
47
- // B. 关键属性白名单 (这是你指出问题的核心修复)
48
- const props = [];
49
-
50
- // 通用重要属性
51
- if (node.getAttribute('role')) props.push(`role="${node.getAttribute('role')}"`);
52
- if (node.getAttribute('aria-label')) props.push(`aria-label="${node.getAttribute('aria-label')}"`);
53
- if (node.getAttribute('title')) props.push(`title="${node.getAttribute('title')}"`);
54
-
55
- // 特定标签的特定属性
56
- if (tagName === 'a') {
57
- const href = node.getAttribute('href');
58
- // 只保留有意义的链接,忽略 javascript:;
59
- if (href && !href.startsWith('javascript')) props.push(`href="${href}"`);
60
- } else if (tagName === 'input' || tagName === 'textarea' || tagName === 'select') {
61
- if (node.getAttribute('type')) props.push(`type="${node.getAttribute('type')}"`);
62
- if (node.getAttribute('name')) props.push(`name="${node.getAttribute('name')}"`);
63
- if (node.getAttribute('placeholder')) props.push(`placeholder="${node.getAttribute('placeholder')}"`);
64
- if (node.disabled) props.push('disabled');
65
- if (node.checked) props.push('checked');
66
- } else if (tagName === 'button') {
67
- if (node.getAttribute('type')) props.push(`type="${node.getAttribute('type')}"`);
68
- } else if (tagName === 'img') {
69
- if (node.getAttribute('alt')) props.push(`alt="${node.getAttribute('alt')}"`);
70
- }
71
-
72
- if (props.length > 0) {
73
- tagStr += ` ${props.join(' ')}`;
74
- }
75
-
76
- // 4. 递归子节点
77
- const children = Array.from(node.childNodes)
78
- .map(getSimplifiedDOM)
79
- .filter(n => n !== null);
80
-
81
- // 5. 组装输出
82
- // 如果没有子节点,也没有ID/Class,也不是输入框/图片/链接,那这个标签可能只是布局用的 div,可以考虑跳过它直接返回子节点内容
83
- // 但为了保持结构完整,我们暂时保留它
84
- if (children.length === 0) {
85
- // 自闭合标签或空标签
86
- return `<${tagStr} />`;
87
- }
88
- return `<${tagStr}>${children.join('')}</${tagName}>`; // 结束标签只保留 tagName 节省 token
14
+ function getSimplifiedDOM(node) {
15
+ // 1. 处理文本节点
16
+ if (node.nodeType === Node.TEXT_NODE) {
17
+ const text = node.textContent.trim();
18
+ return text ? text.slice(0, 100) + (text.length > 100 ? '...' : '') : null;
89
19
  }
90
20
 
91
- return getSimplifiedDOM(document.body);
21
+ // 2. 过滤无用标签
22
+ const ignoreTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'IFRAME', 'SVG', 'LINK', 'META'];
23
+ if (ignoreTags.includes(node.tagName)) return null;
24
+ if (node.nodeType !== Node.ELEMENT_NODE) return null;
25
+
26
+ // 3. 过滤不可见元素
27
+ // 【注意】这里声明了第一次 style
28
+ const style = window.getComputedStyle(node);
29
+
30
+ if (style.display === 'none' || style.visibility === 'hidden' || style.opacity === '0') return null;
31
+
32
+ // 过滤宽高太小的元素(往往是埋点空像素)
33
+ const rect = node.getBoundingClientRect();
34
+
35
+ // 【修复点】删除了这里重复的 const style = ... 代码
36
+ // 直接使用上面已经定义好的 style 变量即可
37
+
38
+ // 如果宽高为0,但溢出可见,说明可能有定位的子元素显示在外面
39
+ if ((rect.width === 0 || rect.height === 0) && style.overflow !== 'visible') return null;
40
+
41
+ // --- 开始构建标签字符串 ---
42
+ const tagName = node.tagName.toLowerCase();
43
+ let tagStr = tagName;
44
+
45
+ // A. 基础标识符 (ID 和 Class)
46
+ if (node.id) tagStr += `#${node.id}`;
47
+ if (node.className && typeof node.className === 'string') {
48
+ const classes = node.className.trim().split(/\s+/);
49
+ if (classes.length > 0) tagStr += `.${classes.join('.')}`;
50
+ }
51
+
52
+ // B. 关键属性白名单
53
+ const props = [];
54
+
55
+ // 通用重要属性
56
+ if (node.getAttribute('role')) props.push(`role="${node.getAttribute('role')}"`);
57
+ if (node.getAttribute('aria-label')) props.push(`aria-label="${node.getAttribute('aria-label')}"`);
58
+ if (node.getAttribute('title')) props.push(`title="${node.getAttribute('title')}"`);
59
+ // 建议增加这个,很多弹窗用这个属性
60
+ if (node.getAttribute('aria-modal')) props.push(`aria-modal="${node.getAttribute('aria-modal')}"`);
61
+
62
+ // 特定标签的特定属性
63
+ if (tagName === 'a') {
64
+ const href = node.getAttribute('href');
65
+ if (href && !href.startsWith('javascript')) props.push(`href="${href}"`);
66
+ } else if (tagName === 'input' || tagName === 'textarea' || tagName === 'select') {
67
+ if (node.getAttribute('type')) props.push(`type="${node.getAttribute('type')}"`);
68
+ if (node.getAttribute('name')) props.push(`name="${node.getAttribute('name')}"`);
69
+ if (node.getAttribute('placeholder')) props.push(`placeholder="${node.getAttribute('placeholder')}"`);
70
+ if (node.disabled) props.push('disabled');
71
+ if (node.checked) props.push('checked');
72
+ } else if (tagName === 'button') {
73
+ if (node.getAttribute('type')) props.push(`type="${node.getAttribute('type')}"`);
74
+ } else if (tagName === 'img') {
75
+ if (node.getAttribute('alt')) props.push(`alt="${node.getAttribute('alt')}"`);
76
+ } else if (tagName === 'dialog') {
77
+ // 保留 open 属性
78
+ if (node.open) props.push('open');
79
+ }
80
+
81
+ if (props.length > 0) {
82
+ tagStr += ` ${props.join(' ')}`;
83
+ }
84
+
85
+ // 4. 递归子节点 (包含 Shadow DOM 处理)
86
+ let childNodes = Array.from(node.childNodes);
87
+ if (node.shadowRoot) {
88
+ childNodes = [...childNodes, ...Array.from(node.shadowRoot.childNodes)];
89
+ }
90
+
91
+ const children = childNodes
92
+ .map(getSimplifiedDOM)
93
+ .filter(n => n !== null);
94
+
95
+ // 5. 组装输出
96
+ if (children.length === 0) {
97
+ return `<${tagStr} />`;
98
+ }
99
+ return `<${tagStr}>${children.join('')}</${tagName}>`;
100
+ }
101
+
102
+ return getSimplifiedDOM(document.body);
92
103
  """
93
104
 
94
105