-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenhanced_system.py
More file actions
287 lines (193 loc) · 7.57 KB
/
enhanced_system.py
File metadata and controls
287 lines (193 loc) · 7.57 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
import asyncio
import logging
import pyautogui
import time
from typing import Dict, List, Optional, Any
import desktop_state
import task_planner
import cv_ui_integration
_desktop_state = None
_enhanced_planner = None
_robust_clicker = None
def initialize_enhanced_system():
global _desktop_state, _enhanced_planner, _robust_clicker
_desktop_state = desktop_state.get_desktop_state()
_desktop_state.update(force=True)
_enhanced_planner = task_planner.get_task_planner()
_robust_clicker = cv_ui_integration.get_robust_clicker()
_robust_clicker.initialize(use_local_cv=False)
_robust_clicker.detector.initialize(use_local_cv=False)
_desktop_state.start_monitoring(interval=0.5)
logging.info("Enhanced system initialized")
async def execute_complex_task(goal: str) -> Dict[str, Any]:
if not _enhanced_planner:
initialize_enhanced_system()
_desktop_state.update(force=True)
context = {"desktop_state": _desktop_state.get_state_summary()}
result = await _enhanced_planner.react_loop(goal, max_iterations=20)
return result
async def robust_desktop_click(description: str, max_retries: int = 3) -> Dict[str, Any]:
if not _robust_clicker:
initialize_enhanced_system()
result = await _robust_clicker.find_and_click(description)
_desktop_state.update(force=True)
return result
async def execute_planned_steps(goal: str, context: Optional[Dict] = None) -> Dict[str, Any]:
if not _enhanced_planner:
initialize_enhanced_system()
_desktop_state.update(force=True)
tasks = await _enhanced_planner.decompose(goal, context)
if not tasks:
return {
"status": "failed",
"reason": "Could not decompose task",
"completed": [],
"failed": []
}
result = await _enhanced_planner.execute_plan(tasks, stop_on_failure=True)
return result
def get_desktop_snapshot() -> Dict[str, Any]:
if not _desktop_state:
initialize_enhanced_system()
_desktop_state.update(force=True)
snapshot = {
"active_window": None,
"windows": [],
"buttons": [],
"inputs": [],
"summary": ""
}
if _desktop_state.active_window:
snapshot["active_window"] = {
"title": _desktop_state.active_window.title,
"process": _desktop_state.active_window.process_name,
"rect": _desktop_state.active_window.rect
}
buttons = _desktop_state.get_buttons()
inputs = _desktop_state.get_inputs()
snapshot["buttons"] = [
{"title": b.title, "center": b.center, "rect": b.rect}
for b in buttons[:10]
]
snapshot["inputs"] = [
{"title": i.title, "center": i.center, "rect": i.rect}
for i in inputs[:10]
]
for win_title, win_info in _desktop_state.windows.items():
snapshot["windows"].append({
"title": win_title,
"process": win_info.process_name
})
snapshot["summary"] = _desktop_state.get_state_summary()
return snapshot
async def enhanced_element_find(description: str) -> Optional[Dict[str, Any]]:
if not _desktop_state:
initialize_enhanced_system()
_desktop_state.update(force=True)
elem = _desktop_state.find_element(description, fuzzy=True)
if elem:
return {
"found": True,
"title": elem.title,
"type": elem.element_type.value,
"center": elem.center,
"rect": elem.rect,
"automation_id": elem.automation_id
}
return {"found": False, "description": description}
async def robust_drag_and_drop(source: str, target: str) -> Dict[str, Any]:
if not _robust_clicker:
initialize_enhanced_system()
result = await _robust_clicker.find_and_drag(source, target)
_desktop_state.update(force=True)
return result
async def wait_for_element(description: str, timeout: float = 10.0) -> Dict[str, Any]:
if not _robust_clicker:
initialize_enhanced_system()
result = await _robust_clicker.wait_for_element(description, timeout=timeout)
return result
async def wait_for_state_change(expected: str, timeout: float = 10.0) -> Dict[str, Any]:
if not _robust_clicker:
initialize_enhanced_system()
result = await _robust_clicker.wait_for_state_change(expected, timeout=timeout)
return result
def press_keyboard_shortcut(shortcut: str) -> bool:
if not _robust_clicker:
_robust_clicker = cv_ui_integration.get_robust_clicker()
return _robust_clicker._keyboard.press(shortcut)
def type_text_async(text: str, delay: float = 0.05) -> bool:
if not _robust_clicker:
_robust_clicker = cv_ui_integration.get_robust_clicker()
return _robust_clicker._keyboard.type_text(text, delay=delay)
def activate_window(title: str, fuzzy: bool = True) -> bool:
if not _desktop_state:
initialize_enhanced_system()
return _desktop_state.activate_window(title, fuzzy=fuzzy)
def minimize_window(title: str = None) -> bool:
if not _desktop_state:
initialize_enhanced_system()
return _desktop_state.minimize_window(title)
def maximize_window(title: str = None) -> bool:
if not _desktop_state:
initialize_enhanced_system()
return _desktop_state.maximize_window(title)
def close_window(title: str = None) -> bool:
if not _desktop_state:
initialize_enhanced_system()
return _desktop_state.close_window(title)
def get_window_stack_order() -> List[str]:
if not _desktop_state:
initialize_enhanced_system()
return _desktop_state.get_window_stack_order()
def get_app_windows(app_name: str) -> List[Dict[str, Any]]:
if not _desktop_state:
initialize_enhanced_system()
windows = _desktop_state.get_app_windows(app_name)
return [
{"title": w.title, "process": w.process_name, "rect": w.rect}
for w in windows
]
async def verify_ui_state(expected_state: str) -> bool:
if not _desktop_state:
initialize_enhanced_system()
_desktop_state.update(force=True)
expected_lower = expected_state.lower()
if _desktop_state.active_window:
title_lower = _desktop_state.active_window.title.lower()
if expected_lower in title_lower:
return True
for win_title in _desktop_state.windows.keys():
if expected_lower in win_title.lower():
return True
return False
class StateChangeDetector:
def __init__(self):
self._desktop_state = None
self._previous_summary = None
def initialize(self):
global _desktop_state
if not _desktop_state:
_desktop_state = desktop_state.get_desktop_state()
self._desktop_state = _desktop_state
self._desktop_state.update(force=True)
self._previous_summary = self._desktop_state.get_state_summary()
def detect_change(self) -> Dict[str, Any]:
if not self._desktop_state:
self.initialize()
current_summary = self._desktop_state.get_state_summary()
if current_summary != self._previous_summary:
change = {
"changed": True,
"previous": self._previous_summary,
"current": current_summary
}
self._previous_summary = current_summary
return change
return {"changed": False}
_state_detector = None
def get_state_detector() -> StateChangeDetector:
global _state_detector
if _state_detector is None:
_state_detector = StateChangeDetector()
_state_detector.initialize()
return _state_detector