-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgui_tools.py
More file actions
509 lines (426 loc) · 15.4 KB
/
Copy pathgui_tools.py
File metadata and controls
509 lines (426 loc) · 15.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
from __future__ import annotations
import asyncio
import os
import time
from dataclasses import dataclass
from typing import List, Optional, Dict, Any
import pyautogui
from PIL import Image
from pydantic_ai import Agent, RunContext, ImageUrl
import platform
import logging
# Make PyAutoGUI safe
pyautogui.PAUSE = 0.1
# Fail-safe: move mouse to upper left to abort
pyautogui.FAILSAFE = True
# Set up logging
logger = logging.getLogger(__name__)
if platform.system() == 'Darwin':
try:
from AppKit import NSWorkspace, NSApplicationActivateIgnoringOtherApps
except ImportError:
logger.warning("AppKit not available. macOS app functions will not work.")
@dataclass
class GuiToolDeps:
"""Dependencies for GUI tools."""
pass
gui_agent = Agent(
'openai:gpt-4o',
system_prompt=(
'Use the provided GUI tools to control the computer. '
'Capture screenshots to see what is on screen, then use mouse and keyboard tools to interact.'
),
deps_type=GuiToolDeps,
retries=1,
)
@gui_agent.tool
async def screenshot(ctx: RunContext[GuiToolDeps],
region: Optional[List[int]] = None,
file_dir: Optional[str] = None,
file_name: Optional[str] = None
) -> Dict[str, Any]:
"""Take a screenshot and save it to a local file for viewing.
Args:
ctx: The context.
region: Optional region as [x, y, width, height]
file_path: Optional file path to save the screenshot
"""
try:
if region and len(region) == 4:
x, y, width, height = region
screenshot = pyautogui.screenshot(region=(x, y, width, height))
else:
screenshot = pyautogui.screenshot()
temp_dir = file_dir
if not temp_dir:
temp_dir = os.path.join(os.getcwd(), "screenshots")
os.makedirs(temp_dir, exist_ok=True)
if not file_name:
timestamp = int(time.time())
file_path = os.path.join(temp_dir, f"screenshot_{timestamp}.png")
else:
file_path = os.path.join(temp_dir, file_name)
screenshot.save(file_path)
return {
"file_path": file_path,
"width": screenshot.width,
"height": screenshot.height,
"message": f"Screenshot saved to {file_path}."
}
except Exception as e:
return {"error": str(e)}
@gui_agent.tool
async def mouse_move(ctx: RunContext[GuiToolDeps], x: int, y: int, duration: float = 0.5) -> Dict[str, Any]:
"""Move mouse to x, y coordinates with smooth motion.
Args:
ctx: The context.
x: X coordinate
y: Y coordinate
duration: Duration of move in seconds
"""
try:
pyautogui.moveTo(x, y, duration=duration)
return {
"success": True,
"position": {"x": x, "y": y}
}
except Exception as e:
return {"error": str(e)}
@gui_agent.tool
async def mouse_click(ctx: RunContext[GuiToolDeps],
button: str = "left",
x: Optional[int] = None,
y: Optional[int] = None,
clicks: int = 1) -> Dict[str, Any]:
"""Click at current position or specified x, y coordinates.
Args:
ctx: The context.
button: Mouse button to click ("left", "right", or "middle")
x: Optional X coordinate
y: Optional Y coordinate
clicks: Number of clicks
"""
try:
if x is not None and y is not None:
pyautogui.click(x=x, y=y, button=button, clicks=clicks)
position = (x, y)
else:
# Click at current position
pyautogui.click(button=button, clicks=clicks)
position = pyautogui.position()
return {
"success": True,
"action": f"{button} click x{clicks}",
"position": {"x": position[0], "y": position[1]}
}
except Exception as e:
return {"error": str(e)}
@gui_agent.tool
async def keyboard_type(ctx: RunContext[GuiToolDeps], text: str, interval: float = 0.05) -> Dict[str, Any]:
"""Type text at current cursor position.
Args:
ctx: The context.
text: Text to type
interval: Interval between keystrokes
"""
try:
pyautogui.write(text, interval=interval)
return {
"success": True,
"text": text,
"chars_typed": len(text)
}
except Exception as e:
return {"error": str(e)}
@gui_agent.tool
async def key_press(ctx: RunContext[GuiToolDeps], keys: List[str]) -> Dict[str, Any]:
"""Press specified keys (can be combinations like ['ctrl', 'c']).
Args:
ctx: The context.
keys: Keys to press, can be single key or combination like ['ctrl', 'c']
"""
try:
if len(keys) == 1:
pyautogui.press(keys[0])
else:
# For combinations like ctrl+c
pyautogui.hotkey(*keys)
return {
"success": True,
"keys_pressed": keys
}
except Exception as e:
return {"error": str(e)}
@gui_agent.tool
async def image_locate(ctx: RunContext[GuiToolDeps],
image_path: str,
confidence: float = 0.9) -> Dict[str, Any]:
"""Find an image on screen and return its position.
Args:
ctx: The context.
image_path: Path to image file to locate on screen
confidence: Confidence threshold (0.0-1.0)
"""
try:
result = pyautogui.locateOnScreen(image_path, confidence=confidence)
if result:
return {
"found": True,
"position": {
"x": result.left + result.width // 2,
"y": result.top + result.height // 2,
"left": result.left,
"top": result.top,
"width": result.width,
"height": result.height
}
}
else:
return {"found": False}
except Exception as e:
return {"error": str(e)}
@gui_agent.tool
async def drag_and_drop(ctx: RunContext[GuiToolDeps],
start_x: int,
start_y: int,
end_x: int,
end_y: int,
duration: float = 0.5) -> Dict[str, Any]:
"""Drag from one position to another.
Args:
ctx: The context.
start_x: Starting X coordinate
start_y: Starting Y coordinate
end_x: Ending X coordinate
end_y: Ending Y coordinate
duration: Duration of drag in seconds
"""
try:
pyautogui.moveTo(start_x, start_y, duration=duration/2)
pyautogui.dragTo(end_x, end_y, duration=duration/2)
return {
"success": True,
"start": {"x": start_x, "y": start_y},
"end": {"x": end_x, "y": end_y}
}
except Exception as e:
return {"error": str(e)}
@gui_agent.tool
async def scroll(ctx: RunContext[GuiToolDeps], amount: int, direction: str = "down") -> Dict[str, Any]:
"""Scroll up or down.
Args:
ctx: The context.
amount: Number of "clicks" to scroll
direction: "up" or "down"
"""
try:
clicks = amount * (-1 if direction == "up" else 1)
pyautogui.scroll(clicks)
return {
"success": True,
"direction": direction,
"amount": amount
}
except Exception as e:
return {"error": str(e)}
@gui_agent.tool
async def get_screen_size(ctx: RunContext[GuiToolDeps]) -> Dict[str, Any]:
"""Get the size of the screen.
Args:
ctx: The context.
"""
try:
width, height = pyautogui.size()
return {
"width": width,
"height": height
}
except Exception as e:
return {"error": str(e)}
@gui_agent.tool
async def get_mouse_position(ctx: RunContext[GuiToolDeps]) -> Dict[str, Any]:
"""Get the current mouse position.
Args:
ctx: The context.
"""
try:
x, y = pyautogui.position()
return {
"x": x,
"y": y
}
except Exception as e:
return {"error": str(e)}
@gui_agent.tool
async def switch_window(ctx: RunContext[GuiToolDeps]) -> Dict[str, Any]:
"""Switch to the next window using Alt+Tab (Windows) or Command+Tab (Mac).
Args:
ctx: The context.
"""
try:
import platform
system = platform.system()
if system == 'Darwin': # macOS
pyautogui.hotkey('command', 'tab')
return {"success": True, "action": "Switched window using Command+Tab (Mac)"}
elif system == 'Windows':
pyautogui.hotkey('alt', 'tab')
return {"success": True, "action": "Switched window using Alt+Tab (Windows)"}
elif system == 'Linux':
pyautogui.hotkey('alt', 'tab')
return {"success": True, "action": "Switched window using Alt+Tab (Linux)"}
else:
return {"error": f"Unsupported platform: {system}"}
except Exception as e:
return {"error": str(e)}
@gui_agent.tool
async def focus_application(ctx: RunContext[GuiToolDeps], app_name: str) -> Dict[str, Any]:
"""Focus a specific application by name (works best on macOS).
Args:
ctx: The context.
app_name: Name of the application to focus (e.g., "Chrome", "Terminal", "Finder")
"""
try:
import platform
system = platform.system()
if system == 'Darwin':
import subprocess
# AppleScript to focus application
script = f'''
tell application "{app_name}"
activate
end tell
'''
result = subprocess.run(['osascript', '-e', script],
capture_output=True,
text=True)
if result.returncode == 0:
return {
"success": True,
"action": f"Focused application: {app_name}"
}
else:
return {
"success": False,
"error": f"Failed to focus {app_name}: {result.stderr}"
}
elif system == 'Windows':
# On Windows, we can try with the window title
# This is less reliable but can work for some apps
try:
import win32gui
import win32con
def window_enum_callback(hwnd, results):
if win32gui.IsWindowVisible(hwnd) and app_name.lower() in win32gui.GetWindowText(hwnd).lower():
results.append(hwnd)
results = []
win32gui.EnumWindows(window_enum_callback, results)
if results:
win32gui.SetForegroundWindow(results[0])
return {
"success": True,
"action": f"Focused window containing: {app_name}"
}
else:
return {
"success": False,
"error": f"No visible window found containing: {app_name}"
}
except ImportError:
return {
"success": False,
"error": "win32gui not available. Install with: pip install pywin32"
}
else:
return {
"success": False,
"error": f"Application focusing not implemented for {system}, try using switch_window instead"
}
except Exception as e:
return {"error": str(e)}
@gui_agent.tool
async def get_frontmost_app(ctx: RunContext[GuiToolDeps]) -> Dict[str, Any]:
"""Get the currently frontmost application (macOS only).
Args:
ctx: The context.
Returns:
Information about the frontmost app including name and bundle ID.
"""
try:
if platform.system() != 'Darwin':
return {"error": "This function is only available on macOS"}
ws = NSWorkspace.sharedWorkspace()
frontmost_app = ws.frontmostApplication()
if frontmost_app:
return {
"success": True,
"name": frontmost_app.localizedName(),
"bundle_id": frontmost_app.bundleIdentifier()
}
return {"success": False, "error": "No frontmost application found"}
except Exception as e:
return {"error": str(e)}
@gui_agent.tool
async def activate_app_by_name(ctx: RunContext[GuiToolDeps], app_name: str) -> Dict[str, Any]:
"""Activate (bring to foreground) an application by name (macOS only).
Args:
ctx: The context.
app_name: Name of the application to activate.
"""
try:
if platform.system() != 'Darwin':
return {"error": "This function is only available on macOS"}
apps = NSWorkspace.sharedWorkspace().runningApplications()
# Try exact match first
for app in apps:
if app.localizedName() == app_name:
logger.debug(f"Focusing {app_name} application")
app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps)
return {"success": True, "name": app_name, "match_type": "exact"}
# If exact match fails, try case-insensitive match or contains
for app in apps:
if app_name.lower() in app.localizedName().lower():
app_name_found = app.localizedName()
logger.debug(
f"Focusing app with name containing '{app_name}': {app_name_found}"
)
app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps)
return {"success": True, "name": app_name_found, "match_type": "partial"}
return {"success": False, "error": f"No application named '{app_name}' found"}
except Exception as e:
return {"error": str(e)}
@gui_agent.tool
async def activate_app_by_bundle_id(ctx: RunContext[GuiToolDeps], bundle_id: str) -> Dict[str, Any]:
"""Activate (bring to foreground) an application by bundle ID (macOS only).
Args:
ctx: The context.
bundle_id: Bundle ID of the application to activate.
"""
try:
if platform.system() != 'Darwin':
return {"error": "This function is only available on macOS"}
apps = NSWorkspace.sharedWorkspace().runningApplications()
for app in apps:
if app.bundleIdentifier() == bundle_id:
logger.debug(f"Focusing application with bundle ID {bundle_id}")
app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps)
return {"success": True, "bundle_id": bundle_id}
return {"success": False, "error": f"No application with bundle ID '{bundle_id}' found"}
except Exception as e:
return {"error": str(e)}
async def main():
"""Example usage of GUI tools."""
deps = GuiToolDeps()
# Example: Take a screenshot, then click somewhere
result = await gui_agent.run(
"Take a screenshot, then click in the middle of the screen",
deps=deps
)
print("Response:", result.data)
result = await gui_agent.run(
"Focus on the Firefox browser and then go to Cursor app",
deps=deps
)
print("Response:", result.data)
if __name__ == "__main__":
asyncio.run(main())