-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.py
More file actions
1366 lines (1252 loc) · 56.4 KB
/
Copy pathplugin.py
File metadata and controls
1366 lines (1252 loc) · 56.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
# -*- coding: utf-8 -*-
"""
界 (Jie) v2.0.2 — AI 浏览器
让 AI 像人一样「看」界面、「操作」界面、「理解」界面
跨平台桌面自动化中枢 (Windows + Linux + macOS)
v2.0.2 新增: OCR识图、UI元素识别(Accessibility)、录制回放持久化、扩展键码(含多媒体键)
"""
import base64
import io
import json
import logging
import os
import platform
import subprocess
import time
import traceback
from pathlib import Path
from typing import Any, Dict, List, Optional
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
logger = logging.getLogger("qwenpaw.界")
# ── v2.0.2 可选导入:OCR ──────────────────────────────
try:
import pytesseract
HAS_TESSERACT = True
# 尝试自动定位 tesseract 安装路径
import shutil
if not shutil.which('tesseract'):
_common_paths = [
r'C:\Program Files\Tesseract-OCR\tesseract.exe',
r'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe',
]
for _p in _common_paths:
if os.path.exists(_p):
pytesseract.pytesseract.tesseract_cmd = _p
break
except ImportError:
HAS_TESSERACT = False
logger.info("pytesseract 未安装,OCR功能不可用")
# ── v2.0.2 可选导入:UI Automation ─────────────────────
try:
import uiautomation as auto
HAS_UI_AUTO = True
except ImportError:
HAS_UI_AUTO = False
logger.info("uiautomation 未安装,UI元素识别不可用")
# ── v2.0.2 可选导入:pywinauto 备用 ────────────────────
try:
import pywinauto
HAS_PYWINAUTO = True
except ImportError:
HAS_PYWINAUTO = False
# ── 平台检测 ───────────────────────────────────────────
IS_WINDOWS = platform.system() == 'Windows'
IS_LINUX = platform.system() == 'Linux'
IS_MAC = platform.system() == 'Darwin'
logger.info(f"🖥️ 检测到平台: {platform.system()}")
# ── 平台特定导入 ───────────────────────────────────────
if IS_WINDOWS:
try:
import ctypes
import ctypes.wintypes
user32 = ctypes.windll.user32
gdi32 = ctypes.windll.gdi32
HAS_CTYPES = True
except Exception as e:
logger.warning(f"Windows ctypes 导入失败: {e}")
HAS_CTYPES = False
try:
import win32gui
import win32con
import win32api
import win32clipboard as clip
import win32ui
HAS_PYWIN32 = True
except ImportError:
HAS_PYWIN32 = False
logger.warning("pywin32 未安装,部分功能受限")
else:
HAS_CTYPES = False
HAS_PYWIN32 = False
# Linux 工具检测
if IS_LINUX:
def _check_linux_tool(tool: str) -> bool:
"""检查 Linux 工具是否可用"""
try:
subprocess.run(['which', tool], capture_output=True, check=True)
return True
except:
return False
HAS_XDOTOOL = _check_linux_tool('xdotool')
HAS_SCROT = _check_linux_tool('scrot')
HAS_GNOME_SCREENSHOT = _check_linux_tool('gnome-screenshot')
HAS_XCLIP = _check_linux_tool('xclip')
logger.info(f"Linux 工具检测: xdotool={HAS_XDOTOOL}, scrot={HAS_SCROT}, gnome-screenshot={HAS_GNOME_SCREENSHOT}, xclip={HAS_XCLIP}")
# ── macOS CoreGraphics 设置 ─────────────────────────────
if IS_MAC:
import ctypes.util
import Quartz
_cg = ctypes.cdll.LoadLibrary(ctypes.util.find_library('CoreGraphics') or '/System/Library/Frameworks/ApplicationServices.framework/Frameworks/CoreGraphics.framework/CoreGraphics')
_kcore = ctypes.cdll.LoadLibrary(ctypes.util.find_library('CoreFoundation') or '/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation')
# CGEventType
_kCGEventLeftMouseDown = 1
_kCGEventLeftMouseUp = 2
_kCGEventRightMouseDown = 3
_kCGEventRightMouseUp = 4
_kCGEventOtherMouseDown = 25
_kCGEventOtherMouseUp = 26
_kCGEventMouseMoved = 5
_kCGEventScrollWheel = 22
_kCGEventKeyDown = 10
_kCGEventKeyUp = 11
_kCGEventFlagMaskShift = 0x020000
_kCGEventFlagMaskControl = 0x040000
_kCGEventFlagMaskAlternate = 0x080000
_kCGEventFlagMaskCommand = 0x100000
_kCGHIDEventTap = 0
# CGMouseButton
_kCGMouseButtonLeft = 0
_kCGMouseButtonRight = 1
_kCGMouseButtonCenter = 2
# CGScrollEventUnit
_kCGScrollEventUnitPixel = 0
_kCGScrollEventUnitLine = 1
# --- CGSPoint / CGPoint ---
class _CGS(ctypes.Structure):
_fields_ = [("x", ctypes.c_double), ("y", ctypes.c_double)]
# --- CGEvent ref type ---
class _CGE(ctypes.Structure): pass
_CGEventRef = ctypes.POINTER(_CGE)
# --- CG function signatures ---
_cg.CGDisplayPixelsWide.argtypes = [ctypes.c_uint32]
_cg.CGDisplayPixelsWide.restype = ctypes.c_size_t
_cg.CGDisplayPixelsHigh.argtypes = [ctypes.c_uint32]
_cg.CGDisplayPixelsHigh.restype = ctypes.c_size_t
_cg.CGMainDisplayID.argtypes = []
_cg.CGMainDisplayID.restype = ctypes.c_uint32
_cg.CGEventCreateMouseEvent.argtypes = [ctypes.c_void_p, ctypes.c_uint32, _CGS, ctypes.c_uint32]
_cg.CGEventCreateMouseEvent.restype = _CGEventRef
_cg.CGEventPost.argtypes = [ctypes.c_uint32, _CGEventRef]
_cg.CGEventPost.restype = None
_cg.CGEventCreateKeyboardEvent.argtypes = [ctypes.c_void_p, ctypes.c_uint16, ctypes.c_bool]
_cg.CGEventCreateKeyboardEvent.restype = _CGEventRef
_cg.CGEventSetFlags.argtypes = [_CGEventRef, ctypes.c_uint64]
_cg.CGEventSetFlags.restype = None
_cg.CGEventCreateScrollWheelEvent.argtypes = [ctypes.c_void_p, ctypes.c_uint32, ctypes.c_uint32, ctypes.c_int32, ctypes.c_int32]
_cg.CGEventCreateScrollWheelEvent.restype = _CGEventRef
_cg.CGEventSetIntegerValueField.argtypes = [_CGEventRef, ctypes.c_int32, ctypes.c_int64]
_cg.CGEventSetIntegerValueField.restype = None
_cg.CGEventGetLocation.argtypes = [_CGEventRef]
_cg.CGEventGetLocation.restype = _CGS
_cg.CGEventCreate.argtypes = [ctypes.c_void_p]
_cg.CGEventCreate.restype = _CGEventRef
_cg.CFRelease.argtypes = [ctypes.c_void_p]
_cg.CFRelease.restype = None
# --- macOS 工具检测 ---
def _check_mac_tool(tool):
try:
subprocess.run(['which', tool], capture_output=True, check=True)
return True
except:
return False
HAS_CLICLICK = _check_mac_tool('cliclick')
logger.info(f"macOS 工具检测: CoreGraphics=✅, cliclick={HAS_CLICLICK}")
# --- macOS 辅助函数 ---
_MAC_KEY_CODES = {
'return': 0x24, 'enter': 0x24, 'tab': 0x30, 'space': 0x31,
'delete': 0x33, 'escape': 0x35, 'esc': 0x35,
'command': 0x37, 'cmd': 0x37,
'shift': 0x38, 'capslock': 0x39, 'option': 0x3A, 'alt': 0x3A,
'control': 0x3B, 'ctrl': 0x3B,
'rightshift': 0x3C, 'rightoption': 0x3D, 'rightcontrol': 0x3E,
'left': 0x7B, 'right': 0x7C, 'down': 0x7D, 'up': 0x7E,
'f1': 0x7A, 'f2': 0x78, 'f3': 0x63, 'f4': 0x76, 'f5': 0x60,
'f6': 0x61, 'f7': 0x62, 'f8': 0x64, 'f9': 0x65, 'f10': 0x6D,
'f11': 0x67, 'f12': 0x6F,
'a': 0x00, 'b': 0x0B, 'c': 0x08, 'd': 0x02, 'e': 0x0E,
'f': 0x03, 'g': 0x05, 'h': 0x04, 'i': 0x22, 'j': 0x26,
'k': 0x28, 'l': 0x25, 'm': 0x2E, 'n': 0x2D, 'o': 0x1F,
'p': 0x23, 'q': 0x0C, 'r': 0x0F, 's': 0x01, 't': 0x11,
'u': 0x20, 'v': 0x09, 'w': 0x0D, 'x': 0x07, 'y': 0x10, 'z': 0x06,
'0': 0x1D, '1': 0x12, '2': 0x13, '3': 0x14, '4': 0x15,
'5': 0x17, '6': 0x16, '7': 0x1A, '8': 0x1C, '9': 0x19,
}
_MAC_MOD_FLAGS = {
'shift': _kCGEventFlagMaskShift,
'ctrl': _kCGEventFlagMaskControl,
'control': _kCGEventFlagMaskControl,
'alt': _kCGEventFlagMaskAlternate,
'option': _kCGEventFlagMaskAlternate,
'cmd': _kCGEventFlagMaskCommand,
'command': _kCGEventFlagMaskCommand,
}
def _mac_mouse_event(evt_type, x, y, button):
"""macOS 鼠标事件底层"""
pt = _CGS(float(x), float(y))
evt = _cg.CGEventCreateMouseEvent(None, evt_type, pt, button)
_cg.CGEventPost(_kCGHIDEventTap, evt)
_cg.CFRelease(evt)
def _mac_key_event(keycode, flags, down):
"""macOS 键盘事件底层"""
evt = _cg.CGEventCreateKeyboardEvent(None, keycode, down)
if flags:
_cg.CGEventSetFlags(evt, flags)
_cg.CGEventPost(_kCGHIDEventTap, evt)
_cg.CFRelease(evt)
try:
from PIL import Image, ImageGrab
HAS_PIL = True
except ImportError:
HAS_PIL = False
logger.warning("Pillow 未安装,截图功能受限")
# ── 常量 ──────────────────────────────────────────────
if IS_WINDOWS:
MOUSEEVENTF_LEFTDOWN = 0x0002
MOUSEEVENTF_LEFTUP = 0x0004
MOUSEEVENTF_RIGHTDOWN = 0x0008
MOUSEEVENTF_RIGHTUP = 0x0010
MOUSEEVENTF_MIDDLEDOWN = 0x0020
MOUSEEVENTF_MIDDLEUP = 0x0040
MOUSEEVENTF_WHEEL = 0x0800
KEYEVENTF_KEYDOWN = 0x0000
KEYEVENTF_KEYUP = 0x0002
# ── 动态路径检测 ──────────────────────────────────────
def _get_qwenpaw_home() -> Path:
"""自动检测QwenPaw主目录 - 支持多种安装方式"""
if "QWENPAW_HOME" in os.environ:
path = Path(os.environ["QWENPAW_HOME"])
if path.exists():
return path
try:
current_file = Path(__file__).resolve()
qwenpaw_home = current_file.parent.parent.parent
if (qwenpaw_home / "plugins").exists():
return qwenpaw_home
except Exception:
pass
user_home = Path.home()
for name in [".copaw", ".qwenpaw", "qwenpaw"]:
possible_path = user_home / name
if possible_path.exists():
return possible_path
return user_home / ".qwenpaw"
QWENPAW_HOME = _get_qwenpaw_home()
PLUGINS_DIR = QWENPAW_HOME / "plugins"
# ── Pydantic 模型 ─────────────────────────────────────
class ClickRequest(BaseModel):
x: int
y: int
button: str = "left"
clicks: int = 1
class TypeRequest(BaseModel):
text: str
clear: bool = False
press_enter: bool = False
x: Optional[int] = None
y: Optional[int] = None
class ScrollRequest(BaseModel):
x: int
y: int
direction: str = "down"
wheel_times: int = 1
class MoveRequest(BaseModel):
x: int
y: int
class ShortcutRequest(BaseModel):
keys: str
class AppRequest(BaseModel):
name: str
class ExecuteRequest(BaseModel):
steps: List[Dict[str, Any]]
class ScheduleRequest(BaseModel):
action: str
interval: int
count: int = -1
params: Optional[Dict[str, Any]] = None
class OCRRequest(BaseModel):
x: Optional[int] = None
y: Optional[int] = None
width: Optional[int] = None
height: Optional[int] = None
lang: str = "chi_sim+eng"
class SmartActionRequest(BaseModel):
condition: str
action_if_true: Optional[Dict[str, Any]] = None
action_if_false: Optional[Dict[str, Any]] = None
check_interval: float = 1.0
timeout: float = 30.0
class RecordStartRequest(BaseModel):
name: str
class PlaybackRequest(BaseModel):
name: str
speed: float = 1.0
# ── v2.0.2 新增模型 ──────────────────────────────────
class FindTextRequest(BaseModel):
text: str
region: Optional[str] = None # "x,y,w,h" or "full"
class UIElementRequest(BaseModel):
name: str = ""
control_type: str = "" # Button, Edit, Text, ListItem, etc.
automation_id: str = ""
class UIActionRequest(BaseModel):
name: str = ""
control_type: str = ""
action: str = "click" # click, get_text, focus, double_click
class RecordSaveRequest(BaseModel):
name: str = "recording"
# ── 跨平台实现 ──────────────────────────────────────────
def _get_screen_size() -> tuple:
"""获取屏幕尺寸 - 跨平台"""
if IS_WINDOWS and HAS_CTYPES:
return (user32.GetSystemMetrics(0), user32.GetSystemMetrics(1))
elif IS_MAC:
try:
did = _cg.CGMainDisplayID()
return (_cg.CGDisplayPixelsWide(did), _cg.CGDisplayPixelsHigh(did))
except:
pass
elif IS_LINUX:
try:
# 使用 xdpyinfo 或 xrandr
result = subprocess.run(['xdpyinfo'], capture_output=True, text=True)
for line in result.stdout.split('\n'):
if 'dimensions:' in line:
parts = line.split()
for part in parts:
if 'x' in part and part[0].isdigit():
w, h = part.split('x')
return (int(w), int(h))
except:
pass
return (1920, 1080) # 默认
def _capture_screenshot() -> Dict[str, Any]:
"""截图 - 跨平台"""
try:
if IS_WINDOWS:
if HAS_PIL:
img = ImageGrab.grab()
buffered = io.BytesIO()
img.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue()).decode()
return {"success": True, "image": img_str, "width": img.width, "height": img.height}
else:
return {"success": False, "error": "Pillow 未安装"}
elif IS_LINUX:
# Linux 截图
timestamp = int(time.time())
screenshot_path = f"/tmp/jie_screenshot_{timestamp}.png"
if HAS_SCROT:
subprocess.run(['scrot', screenshot_path], check=True)
elif HAS_GNOME_SCREENSHOT:
subprocess.run(['gnome-screenshot', '-f', screenshot_path], check=True)
else:
return {"success": False, "error": "未找到截图工具 (请安装 scrot 或 gnome-screenshot)"}
with open(screenshot_path, 'rb') as f:
img_str = base64.b64encode(f.read()).decode()
img = Image.open(screenshot_path)
os.remove(screenshot_path)
return {"success": True, "image": img_str, "width": img.width, "height": img.height}
elif IS_MAC:
# macOS: screencapture 命令
ts = int(time.time())
path = f"/tmp/jie_screenshot_{ts}.png"
subprocess.run(['screencapture', '-x', path], check=True)
with open(path, 'rb') as f:
img_data = f.read()
os.remove(path)
img = Image.open(io.BytesIO(img_data))
return {"success": True, "image": base64.b64encode(img_data).decode(), "width": img.width, "height": img.height}
else:
return {"success": False, "error": f"不支持的平台: {platform.system()}"}
except Exception as e:
return {"success": False, "error": str(e)}
# ── v2.0.2 OCR 识别 ──────────────────────────────────
def _ocr_screen(region: Optional[str] = None, lang: str = "chi_sim+eng") -> Dict[str, Any]:
"""OCR识别屏幕文字 - 支持全屏或指定区域"""
if not HAS_TESSERACT:
return {"success": False, "error": "pytesseract 未安装,请执行: pip install pytesseract"}
try:
if not HAS_PIL:
return {"success": False, "error": "Pillow 未安装"}
img = ImageGrab.grab()
if region and region != "full":
parts = region.replace(',', ' ').split()
if len(parts) == 4:
rx, ry, rw, rh = map(int, parts)
img = img.crop((rx, ry, rx+rw, ry+rh))
data = pytesseract.image_to_data(img, lang=lang, output_type=pytesseract.Output.DICT)
words = []
for i in range(len(data['text'])):
text = data['text'][i].strip()
if text:
words.append({
"text": text,
"confidence": int(data['conf'][i]) if data['conf'][i] != '-1' else 0,
"x": data['left'][i],
"y": data['top'][i],
"w": data['width'][i],
"h": data['height'][i],
})
full_text = pytesseract.image_to_string(img, lang=lang).strip()
return {
"success": True,
"text": full_text,
"words": words,
"word_count": len(words),
}
except Exception as e:
return {"success": False, "error": f"OCR识别失败: {str(e)}"}
def _find_text_on_screen(target: str, region: Optional[str] = None) -> Dict[str, Any]:
"""在屏幕查找指定文字,返回坐标"""
ocr_result = _ocr_screen(region)
if not ocr_result['success']:
return ocr_result
matches = []
for w in ocr_result.get('words', []):
if target.lower() in w['text'].lower():
matches.append({
"text": w['text'],
"confidence": w['confidence'],
"center_x": w['x'] + w['w'] // 2,
"center_y": w['y'] + w['h'] // 2,
"x": w['x'],
"y": w['y'],
"w": w['w'],
"h": w['h'],
})
return {
"success": True,
"found": len(matches) > 0,
"matches": matches,
"count": len(matches),
}
def _click_text(text: str, region: Optional[str] = None) -> Dict[str, Any]:
"""查找文字并点击"""
result = _find_text_on_screen(text, region)
if not result['success']:
return result
if not result['found']:
return {"success": False, "found": False, "error": f"未找到文字: {text}"}
match = result['matches'][0]
return _click(match['center_x'], match['center_y'])
def _click(x: int, y: int, button: str = "left", clicks: int = 1) -> Dict[str, Any]:
"""点击 - 跨平台"""
try:
if IS_WINDOWS:
if HAS_PYWIN32:
win32api.SetCursorPos((x, y))
button_code = win32con.MOUSEEVENTF_LEFTDOWN if button == "left" else \
win32con.MOUSEEVENTF_RIGHTDOWN if button == "right" else \
win32con.MOUSEEVENTF_MIDDLEDOWN
button_up = win32con.MOUSEEVENTF_LEFTUP if button == "left" else \
win32con.MOUSEEVENTF_RIGHTUP if button == "right" else \
win32con.MOUSEEVENTF_MIDDLEUP
for _ in range(clicks):
win32api.mouse_event(button_code, 0, 0, 0, 0)
win32api.mouse_event(button_up, 0, 0, 0, 0)
return {"success": True, "x": x, "y": y, "button": button, "clicks": clicks}
else:
return {"success": False, "error": "pywin32 未安装"}
elif IS_LINUX:
if HAS_XDOTOOL:
button_num = "1" if button == "left" else "3" if button == "right" else "2"
for _ in range(clicks):
subprocess.run(['xdotool', 'mousemove', str(x), str(y)], check=True)
subprocess.run(['xdotool', 'click', button_num], check=True)
return {"success": True, "x": x, "y": y, "button": button, "clicks": clicks}
else:
return {"success": False, "error": "xdotool 未安装 (sudo apt install xdotool)"}
elif IS_MAC:
# macOS: CoreGraphics 鼠标事件
btn_map = {"left": _kCGMouseButtonLeft, "right": _kCGMouseButtonRight, "middle": _kCGMouseButtonCenter}
cg_btn = btn_map.get(button, _kCGMouseButtonLeft)
down_evt = _kCGEventLeftMouseDown if button == "left" else _kCGEventRightMouseDown if button == "right" else _kCGEventOtherMouseDown
up_evt = _kCGEventLeftMouseUp if button == "left" else _kCGEventRightMouseUp if button == "right" else _kCGEventOtherMouseUp
for _ in range(clicks):
_mac_mouse_event(down_evt, x, y, cg_btn)
_mac_mouse_event(up_evt, x, y, cg_btn)
return {"success": True, "x": x, "y": y, "button": button, "clicks": clicks}
else:
return {"success": False, "error": f"不支持的平台: {platform.system()}"}
except Exception as e:
return {"success": False, "error": str(e)}
def _type_text(text: str) -> Dict[str, Any]:
"""输入文字 - 跨平台"""
try:
if IS_WINDOWS:
if HAS_PYWIN32:
# 用 win32clipboard 替代 pyperclip (零额外依赖)
clip.OpenClipboard()
clip.EmptyClipboard()
clip.SetClipboardText(text)
clip.CloseClipboard()
win32api.keybd_event(0x11, 0, 0, 0) # Ctrl
win32api.keybd_event(0x56, 0, 0, 0) # V
win32api.keybd_event(0x56, 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)
return {"success": True, "text_length": len(text)}
else:
return {"success": False, "error": "pywin32 未安装"}
elif IS_LINUX:
if HAS_XDOTOOL and HAS_XCLIP:
# 使用 xclip 复制到剪贴板
proc = subprocess.Popen(['xclip', '-selection', 'clipboard'], stdin=subprocess.PIPE)
proc.communicate(text.encode())
subprocess.run(['xdotool', 'key', 'ctrl+v'], check=True)
return {"success": True, "text_length": len(text)}
else:
# 直接输入
subprocess.run(['xdotool', 'type', text], check=True)
return {"success": True, "text_length": len(text)}
elif IS_MAC:
# macOS: pbcopy + Cmd+V
proc = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE)
proc.communicate(text.encode('utf-8'))
time.sleep(0.05)
# 模拟 Cmd+V
_mac_key_event(0x09, _kCGEventFlagMaskCommand, True) # V down
time.sleep(0.02)
_mac_key_event(0x09, _kCGEventFlagMaskCommand, False) # V up
return {"success": True, "text_length": len(text)}
else:
return {"success": False, "error": f"不支持的平台: {platform.system()}"}
except Exception as e:
return {"success": False, "error": str(e)}
def _shortcut(keys: str) -> Dict[str, Any]:
"""快捷键 - 跨平台"""
try:
key_map = {
'ctrl': 'Control',
'alt': 'Alt',
'shift': 'Shift',
'enter': 'Return',
'esc': 'Escape',
'tab': 'Tab',
'space': 'space',
}
if IS_WINDOWS and HAS_PYWIN32:
# Windows 快捷键实现 — 真实按键
_WIN_VK = {
'ctrl': 0x11, 'control': 0x11,
'alt': 0x12, 'menu': 0x12,
'shift': 0x10,
'win': 0x5B, 'lwin': 0x5B, 'rwin': 0x5C,
'tab': 0x09, 'enter': 0x0D, 'return': 0x0D,
'esc': 0x1B, 'escape': 0x1B,
'space': 0x20, 'back': 0x08, 'backspace': 0x08,
'delete': 0x2E, 'del': 0x2E, 'insert': 0x2D, 'ins': 0x2D,
'home': 0x24, 'end': 0x23, 'pageup': 0x21, 'pagedown': 0x22,
'up': 0x26, 'down': 0x28, 'left': 0x25, 'right': 0x27,
'caps': 0x14, 'capslock': 0x14, 'numlock': 0x90,
'printscreen': 0x2C, 'prtsc': 0x2C, 'pause': 0x13,
'f1': 0x70, 'f2': 0x71, 'f3': 0x72, 'f4': 0x73,
'f5': 0x74, 'f6': 0x75, 'f7': 0x76, 'f8': 0x77,
'f9': 0x78, 'f10': 0x79, 'f11': 0x7A, 'f12': 0x7B,
'f13': 0x7C, 'f14': 0x7D, 'f15': 0x7E, 'f16': 0x7F,
'f17': 0x80, 'f18': 0x81, 'f19': 0x82, 'f20': 0x83,
'f21': 0x84, 'f22': 0x85, 'f23': 0x86, 'f24': 0x87,
'plus': 0xBB, 'equal': 0xBB, 'minus': 0xBD,
'comma': 0xBC, 'period': 0xBE, 'dot': 0xBE,
'semicolon': 0xBA, 'colon': 0xBA,
'quote': 0xDE, 'apostrophe': 0xDE,
'backslash': 0xDC, 'slash': 0xBF, 'pipe': 0xDC,
'lbracket': 0xDB, 'rbracket': 0xDD,
'lbrace': 0xDB, 'rbrace': 0xDD,
'tilde': 0xC0, 'backtick': 0xC0,
# 多媒体键 v2.0.2
'volumemute': 0xAD, 'volume_mute': 0xAD,
'volumedown': 0xAE, 'volume_down': 0xAE,
'volumeup': 0xAF, 'volume_up': 0xAF,
'nexttrack': 0xB0, 'next_track': 0xB0,
'prevtrack': 0xB1, 'prev_track': 0xB1,
'media_stop': 0xB2, 'mediastop': 0xB2,
'playpause': 0xB3, 'play_pause': 0xB3,
'launchmail': 0xB4, 'launch_mail': 0xB4,
'launchmedia': 0xB5, 'launch_media': 0xB5,
'launchapp1': 0xB6, 'launch_app1': 0xB6,
'launchapp2': 0xB7, 'launch_app2': 0xB7,
# 浏览器键
'browser_back': 0xA6, 'browserback': 0xA6,
'browser_forward': 0xA7, 'browserforward': 0xA7,
'browser_refresh': 0xA8, 'browserrefresh': 0xA8,
'browser_stop': 0xA9, 'browserstop': 0xA9,
'browser_search': 0xAA, 'browsersearch': 0xAA,
'browser_favorites': 0xAB, 'browserfavorites': 0xAB,
'browser_home': 0xAC, 'browserhome': 0xAC,
# 功能键
'sleep': 0x5F,
'zoomin': 0x5F, # 缩放未映射到标准 VK,用 keybd_event 较复杂
}
parts = keys.lower().split('+')
mod_keys = [p for p in parts if p in ('ctrl','control','alt','menu','shift','win','lwin')]
normal_keys = [p for p in parts if p not in ('ctrl','control','alt','menu','shift','win','lwin')]
# 按下修饰键
for mk in mod_keys:
vk = _WIN_VK.get(mk, 0)
if vk:
win32api.keybd_event(vk, 0, 0, 0)
time.sleep(0.03)
# 按下并释放普通键
for nk in normal_keys:
if nk in _WIN_VK:
vk = _WIN_VK[nk]
elif len(nk) == 1 and 'a' <= nk <= 'z':
vk = 0x41 + (ord(nk) - ord('a'))
elif len(nk) == 1 and '0' <= nk <= '9':
vk = 0x30 + int(nk)
else:
continue
win32api.keybd_event(vk, 0, 0, 0)
time.sleep(0.02)
win32api.keybd_event(vk, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(0.03)
# 释放修饰键(逆序)
for mk in reversed(mod_keys):
vk = _WIN_VK.get(mk, 0)
if vk:
win32api.keybd_event(vk, 0, win32con.KEYEVENTF_KEYUP, 0)
return {"success": True, "keys": keys}
elif IS_LINUX and HAS_XDOTOOL:
# Linux 快捷键
xdo_keys = []
for part in keys.lower().split('+'):
xdo_keys.append(key_map.get(part, part))
subprocess.run(['xdotool', 'key', '+'.join(xdo_keys)], check=True)
return {"success": True, "keys": keys}
elif IS_MAC:
# macOS: CoreGraphics 快捷键
parts = keys.lower().split('+')
mods = [p for p in parts if p in _MAC_MOD_FLAGS]
non_mods = [p for p in parts if p not in _MAC_MOD_FLAGS]
flags = 0
for m in mods:
flags |= _MAC_MOD_FLAGS[m]
# 按下修饰键
for m in mods:
kc = _MAC_KEY_CODES.get(m, 0)
_mac_key_event(kc, flags, True)
time.sleep(0.02)
# 按下并释放目标键
for k in non_mods:
kc = _MAC_KEY_CODES.get(k, 0)
_mac_key_event(kc, flags, True)
time.sleep(0.02)
_mac_key_event(kc, flags, False)
time.sleep(0.02)
# 释放修饰键
for m in mods:
kc = _MAC_KEY_CODES.get(m, 0)
_mac_key_event(kc, flags, False)
return {"success": True, "keys": keys}
else:
return {"success": False, "error": f"不支持的平台或工具: {platform.system()}"}
except Exception as e:
return {"success": False, "error": str(e)}
def _scroll(x: int, y: int, direction: str, wheel_times: int) -> Dict[str, Any]:
"""滚动 - 跨平台"""
try:
if IS_WINDOWS and HAS_PYWIN32:
win32api.SetCursorPos((x, y))
delta = 120 * wheel_times if direction == "down" else -120 * wheel_times
win32api.mouse_event(MOUSEEVENTF_WHEEL, 0, 0, delta, 0)
return {"success": True, "x": x, "y": y, "direction": direction, "wheel_times": wheel_times}
elif IS_LINUX and HAS_XDOTOOL:
subprocess.run(['xdotool', 'mousemove', str(x), str(y)], check=True)
button = "4" if direction == "up" else "5"
for _ in range(wheel_times):
subprocess.run(['xdotool', 'click', button], check=True)
return {"success": True, "x": x, "y": y, "direction": direction, "wheel_times": wheel_times}
elif IS_MAC:
# macOS: CoreGraphics 滚轮事件
delta = -3 * wheel_times if direction == "down" else 3 * wheel_times
evt = _cg.CGEventCreateScrollWheelEvent(None, _kCGScrollEventUnitPixel, 1, delta, 0)
_cg.CGEventPost(_kCGHIDEventTap, evt)
_cg.CFRelease(evt)
return {"success": True, "x": x, "y": y, "direction": direction, "wheel_times": wheel_times}
else:
return {"success": False, "error": f"不支持的平台: {platform.system()}"}
except Exception as e:
return {"success": False, "error": str(e)}
def _move_mouse(x: int, y: int) -> Dict[str, Any]:
"""移动鼠标 - 跨平台"""
try:
if IS_WINDOWS and HAS_PYWIN32:
win32api.SetCursorPos((x, y))
return {"success": True, "x": x, "y": y}
elif IS_LINUX and HAS_XDOTOOL:
subprocess.run(['xdotool', 'mousemove', str(x), str(y)], check=True)
return {"success": True, "x": x, "y": y}
elif IS_MAC:
# macOS: CoreGraphics 鼠标移动
pt = _CGS(float(x), float(y))
evt = _cg.CGEventCreateMouseEvent(None, _kCGEventMouseMoved, pt, 0)
_cg.CGEventPost(_kCGHIDEventTap, evt)
_cg.CFRelease(evt)
return {"success": True, "x": x, "y": y}
else:
return {"success": False, "error": f"不支持的平台: {platform.system()}"}
except Exception as e:
return {"success": False, "error": str(e)}
def _get_cursor_pos() -> Dict[str, Any]:
"""获取鼠标位置 - 跨平台"""
try:
if IS_WINDOWS and HAS_CTYPES:
pt = ctypes.wintypes.POINT()
user32.GetCursorPos(ctypes.byref(pt))
return {"success": True, "x": pt.x, "y": pt.y}
elif IS_LINUX and HAS_XDOTOOL:
result = subprocess.run(['xdotool', 'getmouselocation'], capture_output=True, text=True)
parts = result.stdout.strip().split()
x = int(parts[0].split(':')[1])
y = int(parts[1].split(':')[1])
return {"success": True, "x": x, "y": y}
elif IS_MAC:
# macOS: CoreGraphics 获取鼠标位置
evt = _cg.CGEventCreate(None)
loc = _cg.CGEventGetLocation(evt)
_cg.CFRelease(evt)
return {"success": True, "x": int(loc.x), "y": int(loc.y)}
else:
return {"success": False, "error": f"不支持的平台: {platform.system()}"}
except Exception as e:
return {"success": False, "error": str(e)}
def _launch_app(name: str) -> Dict[str, Any]:
"""启动应用 - 跨平台"""
try:
if IS_WINDOWS:
app_map = {
"notepad": "notepad.exe",
"calc": "calc.exe",
"explorer": "explorer.exe",
}
# 浏览器常见安装路径
_KNOWN_PATHS = [
(r"C:\Program Files\Google\Chrome\Application\chrome.exe", "chrome.exe"),
(r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", "chrome.exe"),
(r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe", "msedge.exe"),
(r"C:\Program Files\Microsoft\Edge\Application\msedge.exe", "msedge.exe"),
(r"C:\Program Files\Mozilla Firefox\firefox.exe", "firefox.exe"),
(r"C:\Program Files (x86)\Mozilla Firefox\firefox.exe", "firefox.exe"),
]
app_path = app_map.get(name, name)
# 检查是否是已知浏览器但不在 PATH 中
if name in ("chrome", "msedge", "firefox"):
import os as _os
for full_path, exe_name in _KNOWN_PATHS:
if exe_name.lower().startswith(name) and _os.path.exists(full_path):
app_path = full_path
break
subprocess.Popen(app_path, shell=True)
return {"success": True, "action": "launch", "name": name}
elif IS_LINUX:
app_map = {
"notepad": "gedit",
"calc": "gnome-calculator",
"firefox": "firefox",
"chrome": "google-chrome",
}
app_path = app_map.get(name, name)
subprocess.Popen([app_path])
return {"success": True, "action": "launch", "name": name}
elif IS_MAC:
# macOS: open 命令
app_map = {
"notepad": "TextEdit",
"calc": "Calculator",
"explorer": "Finder",
"chrome": "Google Chrome",
"firefox": "Firefox",
"safari": "Safari",
"terminal": "Terminal",
}
app_name = app_map.get(name.lower(), name)
subprocess.Popen(['open', '-a', app_name])
return {"success": True, "action": "launch", "name": name}
else:
return {"success": False, "error": f"不支持的平台: {platform.system()}"}
except Exception as e:
return {"success": False, "error": str(e)}
def _get_window_info() -> List[Dict[str, Any]]:
"""获取窗口信息 - 跨平台"""
try:
if IS_WINDOWS and HAS_PYWIN32:
windows = []
def callback(hwnd, extra):
if win32gui.IsWindowVisible(hwnd):
title = win32gui.GetWindowText(hwnd)
if title:
rect = win32gui.GetWindowRect(hwnd)
windows.append({
"hwnd": hwnd,
"title": title,
"x": rect[0],
"y": rect[1],
"width": rect[2] - rect[0],
"height": rect[3] - rect[1]
})
win32gui.EnumWindows(callback, None)
return windows
elif IS_LINUX:
# Linux 窗口列表简化实现
return [{"title": "Linux 窗口列表", "note": "请安装 wmctrl 获取完整支持"}]
elif IS_MAC:
# macOS: osascript (AppleScript) 获取窗口列表
script = '''
tell application "System Events"
set windowList to {}
set procs to (every process whose visible is true)
repeat with proc in procs
set procName to name of proc
try
set wins to (every window of proc)
repeat with w in wins
set winName to name of w
set winPos to position of w
set winSize to size of w
set end of windowList to procName & " | " & winName & " | " & (item 1 of winPos as text) & "," & (item 2 of winPos as text) & " | " & (item 1 of winSize as text) & "x" & (item 2 of winSize as text)
end repeat
end try
end repeat
return windowList
end tell
'''
result = subprocess.run(['osascript', '-e', script], capture_output=True, text=True, timeout=5)
windows = []
for line in result.stdout.strip().split(', '):
parts = line.split(' | ')
if len(parts) >= 4:
pos_parts = parts[2].split(',')
size_parts = parts[3].split('x')
try:
windows.append({
"title": parts[1],
"app": parts[0],
"x": int(pos_parts[0]),
"y": int(pos_parts[1]),
"width": int(size_parts[0]),
"height": int(size_parts[1])
})
except (ValueError, IndexError):
pass
return windows
else:
return []
except Exception as e:
logger.error(f"获取窗口信息失败: {e}")
return []
def _activate_window(title: str) -> Dict[str, Any]:
"""激活窗口(按标题匹配)- 跨平台"""
try:
if IS_WINDOWS and HAS_PYWIN32:
hwnd = win32gui.FindWindow(None, title)
if hwnd:
win32gui.SetForegroundWindow(hwnd)
return {"success": True, "title": title}
# 模糊匹配
def callback(hwnd, extra):
if win32gui.IsWindowVisible(hwnd):
wtitle = win32gui.GetWindowText(hwnd)
if title.lower() in wtitle.lower():
win32gui.SetForegroundWindow(hwnd)
return False
return True
win32gui.EnumWindows(callback, None)
return {"success": True, "title": title, "note": "模糊匹配"}
elif IS_MAC:
# macOS: osascript 激活窗口
script = f'''
tell application "System Events"
set procs to (every process whose visible is true)
repeat with proc in procs
try
set wins to (every window of proc)
repeat with w in wins
if name of w contains "{title}" then
set frontmost of proc to true
return "found"
end if
end repeat
end try
end repeat
end tell
return "not found"
'''
result = subprocess.run(['osascript', '-e', script], capture_output=True, text=True, timeout=5)
if "found" in result.stdout:
return {"success": True, "title": title}
return {"success": False, "error": f"未找到窗口: {title}"}
elif IS_LINUX and HAS_XDOTOOL:
subprocess.run(['xdotool', 'search', '--name', title, 'windowactivate'], check=True)
return {"success": True, "title": title}
else:
return {"success": False, "error": f"不支持的平台: {platform.system()}"}
except Exception as e:
return {"success": False, "error": str(e)}
# ── v2.0.2 UI Automation 元素识别 ──────────────────────
def _get_ui_elements() -> Dict[str, Any]:
"""获取当前活动窗口的 Accessibility UI 元素树"""
if not HAS_UI_AUTO:
return {"success": False, "error": "uiautomation 未安装,请执行: pip install uiautomation"}
try:
root = auto.GetRootControl()
active = auto.GetForegroundControl()
elements = []
def walk(node, depth=0):
if depth > 4:
return
try:
name = node.Name or ""
ctype = node.ControlTypeName or ""
rect = node.BoundingRectangle
elements.append({
"name": name[:60],
"control_type": ctype,
"automation_id": node.AutomationId or "",
"x": int(rect.left) if rect else 0,
"y": int(rect.top) if rect else 0,
"width": int(rect.width()) if rect else 0,
"height": int(rect.height()) if rect else 0,
"depth": depth,
})
for child in node.GetChildren():
walk(child, depth + 1)
except Exception:
pass
if active: