-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.py
More file actions
100 lines (76 loc) · 2.86 KB
/
Copy pathexecutor.py
File metadata and controls
100 lines (76 loc) · 2.86 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
import pyautogui
import time
pyautogui.FAILSAFE = True # move mouse to top-left corner to abort
pyautogui.PAUSE = 0.3
def click(x: int, y: int, button: str = "left", double: bool = False):
pyautogui.moveTo(x, y, duration=0.3)
if double:
pyautogui.doubleClick(x, y)
else:
pyautogui.click(x, y, button=button)
def type_text(text: str, interval: float = 0.05):
# pyautogui.typewrite silently drops non-ASCII; use clipboard paste for Unicode
try:
import pyperclip
pyperclip.copy(text)
pyautogui.hotkey("ctrl", "v")
time.sleep(0.1)
except Exception:
pyautogui.typewrite(text, interval=interval)
def press_key(key: str):
pyautogui.press(key)
def hotkey(*keys: str):
pyautogui.hotkey(*keys)
def scroll(x: int, y: int, clicks: int):
pyautogui.scroll(clicks, x=x, y=y)
def move_to(x: int, y: int):
pyautogui.moveTo(x, y, duration=0.3)
SAFE_GUARD_KEYWORDS = ["delete", "format", "rm -rf", "drop table", "shutdown", "uninstall"]
def is_dangerous(action_desc: str) -> bool:
return any(kw in action_desc.lower() for kw in SAFE_GUARD_KEYWORDS)
def execute_action(action: dict, auto_approve: bool = False, logger=None, dry_run: bool = False) -> bool:
atype = action.get("type", "")
desc = action.get("reasoning", "")
if dry_run:
print(f"\n[DRY RUN] Would execute: {action}")
if logger:
logger.blocked(action, "dry run — not executed")
return atype != "done"
if is_dangerous(desc) or is_dangerous(str(action)):
print(f"\n⚠️ DANGEROUS ACTION DETECTED: {action}")
confirm = input("Allow? [y/N]: ").strip().lower()
if confirm != "y":
print("Action blocked.")
if logger:
logger.blocked(action, "dangerous action denied by user")
return False
if not auto_approve:
print(f"\n🤖 Proposed action: {action}")
confirm = input("Execute? [Y/n/s(skip)]: ").strip().lower()
if confirm == "n":
if logger:
logger.blocked(action, "user denied")
return False
if confirm == "s":
if logger:
logger.blocked(action, "user skipped")
return True
if atype == "click":
click(action["x"], action["y"], action.get("button", "left"), action.get("double", False))
elif atype == "type":
type_text(action["text"])
elif atype == "press":
press_key(action["key"])
elif atype == "hotkey":
hotkey(*action["keys"])
elif atype == "scroll":
scroll(action["x"], action["y"], action["clicks"])
elif atype == "wait":
time.sleep(action.get("seconds", 1))
elif atype == "done":
print("\n✅ Goal completed!")
return False
else:
print(f"Unknown action type: {atype}")
time.sleep(0.5)
return True