-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
470 lines (370 loc) · 15.1 KB
/
cli.py
File metadata and controls
470 lines (370 loc) · 15.1 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
"""
GUI Agent - Command Line Interface Module
Provides a professional, colorful terminal interface with:
- Colored output for different log levels
- Progress bars and spinners
- Interactive tables for history
- Rich user input experience
"""
from datetime import datetime
from typing import Optional, List, Dict, Any
from rich.console import Console
from rich.panel import Panel
from rich.progress import Progress, SpinnerColumn, TextColumn, BarColumn, TaskProgressColumn, TaskID
from rich.table import Table
from rich.prompt import Prompt, Confirm
from rich.text import Text
from rich.layout import Layout
# Initialize console with emoji support
console = Console()
# ============================================================================
# Logging Functions
# ============================================================================
def log_info(message: str, title: str = "INFO") -> None:
"""Log an informational message with blue styling."""
timestamp = datetime.now().strftime("%H:%M:%S")
console.print(f"[dim]{timestamp}[/dim] [bold blue][{title}][/bold blue] {message}")
def log_success(message: str, title: str = "SUCCESS") -> None:
"""Log a success message with green styling."""
timestamp = datetime.now().strftime("%H:%M:%S")
console.print(f"[dim]{timestamp}[/dim] [bold green][{title}][/bold green] {message}")
def log_warning(message: str, title: str = "WARNING") -> None:
"""Log a warning message with yellow styling."""
timestamp = datetime.now().strftime("%H:%M:%S")
console.print(f"[dim]{timestamp}[/dim] [bold yellow][{title}][/bold yellow] {message}")
def log_error(message: str, title: str = "ERROR") -> None:
"""Log an error message with red styling."""
timestamp = datetime.now().strftime("%H:%M:%S")
console.print(f"[dim]{timestamp}[/dim] [bold red][{title}][/bold red] {message}")
def log_debug(message: str, title: str = "DEBUG") -> None:
"""Log a debug message with dim styling."""
timestamp = datetime.now().strftime("%H:%M:%S")
console.print(f"[dim]{timestamp}[/dim] [dim][{title}][/dim] {message}")
# ============================================================================
# Panel Displays
# ============================================================================
def show_welcome_panel() -> None:
"""Display the welcome panel with project information."""
welcome_text = """[bold blue]GUI Agent[/bold blue] - Desktop Automation powered by Qwen-VL
[dim]Turn natural language instructions into mouse and keyboard actions[/dim]
[bold]Quick Commands:[/bold]
[green]help[/green] - Show this help message
[green]history[/green] - View action history
[green]quit/exit[/green] - Exit the application
[bold]Example Instructions:[/bold]
- "Open Calculator"
- "Search for Python tutorials on Google"
- "Type 'Hello World' in the active window"
- "Scroll down the page"
"""
console.print(Panel(
welcome_text,
title="[bold blue]Welcome to GUI Agent[/bold blue]",
border_style="blue",
padding=(1, 2),
))
console.print()
def show_safety_warning() -> None:
"""Display important safety warnings."""
warning_text = """[yellow]SAFETY REMINDER:[/yellow]
[bold]Always monitor the agent's actions![/bold]
- The agent can control your mouse and keyboard
- Stay ready to interrupt with Ctrl+C if needed
- Don't run on sensitive data or critical systems
- Test in a safe environment first
[dim]PyAutoGUI failsafe: Move mouse to screen corner to stop[/dim]
"""
console.print(Panel(
warning_text,
title="[bold yellow]Safety Notice[/bold yellow]",
border_style="yellow",
padding=(1, 2),
))
console.print()
def show_config_panel(config: Dict[str, Any]) -> None:
"""Display current configuration."""
config_text = f"""[bold]Model:[/bold] {config.get('model', 'N/A')}
[bold]Base URL:[/bold] {config.get('base_url', 'N/A')}
[bold]Max Iterations:[/bold] {config.get('max_iterations', 50)}
[bold]Screen Capture:[/bold] Grid overlay enabled
"""
console.print(Panel(
config_text,
title="[bold]Current Configuration[/bold]",
border_style="green",
padding=(0, 1),
))
# ============================================================================
# Progress Indicators
# ============================================================================
class TaskProgress:
"""Context manager for displaying task progress with spinner."""
def __init__(self, description: str = "Processing"):
self.description = description
self.progress: Optional[Progress] = None
self.task_id: Optional[TaskID] = None
def __enter__(self):
self.progress = Progress(
SpinnerColumn(spinner_name="dots"),
TextColumn("[bold blue]{task.description}"),
BarColumn(bar_width=40),
TaskProgressColumn(),
console=console,
)
self.task_id = self.progress.add_task(self.description, total=None)
self.progress.start()
return self
def __exit__(self, *args):
if self.progress and self.task_id is not None:
self.progress.update(self.task_id, completed=100)
self.progress.stop()
class IterationProgress:
"""Context manager for displaying iteration progress."""
def __init__(self, current: int, max_iterations: int):
self.current = current
self.max = max_iterations
def __enter__(self):
self.progress = Progress(
SpinnerColumn(spinner_name="simpleDotsScrolling"),
TextColumn(f"[bold cyan]Iteration {self.current}/{self.max}[/bold cyan]"),
BarColumn(bar_width=30, complete_style="green", finished_style="green"),
console=console,
)
self.task_id = self.progress.add_task("", total=self.max, completed=self.current)
self.progress.start()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if self.progress:
self.progress.stop()
def create_progress(description: str = "Working...") -> Progress:
"""Create a progress object with spinner."""
return Progress(
SpinnerColumn(),
TextColumn("[bold blue]{task.description}"),
console=console,
transient=True,
)
# ============================================================================
# Status Display
# ============================================================================
class StatusDisplay:
"""Display real-time status information."""
def __init__(self):
self.layout = Layout()
self._setup_layout()
def _setup_layout(self):
self.layout.split(
Layout(name="header", size=3),
Layout(name="status"),
)
def update_status(self, status: str, action: str = "", iteration: int = 0,
max_iterations: int = 50) -> None:
"""Update the status display."""
status_text = Text()
status_text.append(f"Status: {status}\n", style="bold")
if action:
status_text.append(f"Action: {action}\n", style="cyan")
status_text.append(f"Iteration: {iteration}/{max_iterations}", style="dim")
self.layout["status"].update(Panel(status_text, title="Current Status"))
# ============================================================================
# Action History Table
# ============================================================================
def show_action_history(history: List[Dict[str, Any]]) -> None:
"""Display action history in a formatted table."""
if not history:
console.print("[dim]No actions recorded yet.[/dim]")
return
table = Table(
title="Action History",
border_style="blue",
show_header=True,
header_style="bold magenta",
)
table.add_column("#", style="dim", width=4)
table.add_column("Action", style="cyan", width=12)
table.add_column("Details", style="green", max_width=50)
table.add_column("Status", width=10)
for idx, action_record in enumerate(history, 1):
action_type = action_record.get('action', 'UNKNOWN').upper()
details = action_record.get('details', '')
status = action_record.get('status', 'pending')
# Color code by status
if status == 'success':
status_style = "green"
elif status == 'failed':
status_style = "red"
elif status == 'pending':
status_style = "yellow"
else:
status_style = "dim"
table.add_row(
str(idx),
action_type,
details[:50] + "..." if len(details) > 50 else details,
f"[{status_style}]{status}[/{status_style}]",
)
console.print()
console.print(table)
console.print()
def show_action_summary(total_actions: int, successful: int, failed: int) -> None:
"""Display action summary statistics."""
summary_text = f"""[bold]Total Actions:[/bold] {total_actions}
[green]Successful:[/green] {successful}
[red]Failed:[/red] {failed}
[dim]Success Rate: {successful/total_actions*100:.1f}%[/dim]""" if total_actions > 0 else "[dim]No actions recorded[/dim]"
console.print(Panel(
summary_text,
title="[bold]Session Summary[/bold]",
border_style="green",
))
# ============================================================================
# User Input Functions
# ============================================================================
class Command:
"""Built-in commands for the CLI."""
HELP = ['help', 'h', '?']
QUIT = ['quit', 'exit', 'q']
HISTORY = ['history', 'hist', 'hst']
CLEAR = ['clear', 'cls']
CONFIG = ['config', 'cfg']
@classmethod
def is_command(cls, text: str) -> bool:
"""Check if input is a built-in command."""
text_lower = text.strip().lower()
return any(text_lower in commands for commands in [
cls.HELP, cls.QUIT, cls.HISTORY, cls.CLEAR, cls.CONFIG
])
@classmethod
def get_command_type(cls, text: str) -> Optional[str]:
"""Get the command type from input."""
text_lower = text.strip().lower()
if text_lower in cls.HELP:
return 'help'
elif text_lower in cls.QUIT:
return 'quit'
elif text_lower in cls.HISTORY:
return 'history'
elif text_lower in cls.CLEAR:
return 'clear'
elif text_lower in cls.CONFIG:
return 'config'
return None
def get_user_instruction(prompt_text: str = "Enter your instruction") -> Optional[str]:
"""Get user instruction with styled prompt."""
try:
instruction = Prompt.ask(
f"\n[bold green]{prompt_text}[/bold green]",
default="",
)
return instruction.strip() if instruction else None
except (KeyboardInterrupt, EOFError):
return None
def confirm_action(message: str, default: bool = False) -> bool:
"""Ask for user confirmation."""
return Confirm.ask(f"[yellow]{message}[/yellow]", default=default)
def show_help() -> None:
"""Display help information."""
help_text = """[bold]Available Commands:[/bold]
[green]help, h, ?[/green] - Show this help message
[green]quit, exit, q[/green] - Exit the application
[green]history, hist[/green] - View action history
[green]clear, cls[/green] - Clear screen
[green]config, cfg[/green] - Show current configuration
[bold]Example Instructions:[/bold]
[green]"Open Calculator"[/green]
Opens the Windows Calculator application
[green]"Search for Python tutorials on Google"[/green]
Opens browser and searches Google
[green]"Type 'Hello World'"[/green]
Types the specified text in the active window
[green]"Click the OK button"[/green]
Clicks on the OK button
[green]"Scroll down"[/green]
Scrolls the current window down
[dim]Tip: Be specific and descriptive in your instructions.[/dim]
"""
console.print(Panel(
help_text,
title="[bold blue]Help[/bold blue]",
border_style="blue",
padding=(1, 2),
))
# ============================================================================
# Main CLI Class
# ============================================================================
class CLI:
"""Main CLI controller for GUI Agent."""
def __init__(self):
self.console = console
self.history: List[Dict[str, Any]] = []
self.running = True
def start(self) -> None:
"""Start the CLI main loop."""
show_welcome_panel()
show_safety_warning()
while self.running:
instruction = get_user_instruction("Enter your instruction")
if instruction is None:
self.running = False
continue
# Check for built-in commands
if Command.is_command(instruction):
cmd_type = Command.get_command_type(instruction)
if cmd_type:
self._handle_command(cmd_type)
continue
# Process the instruction
self.on_instruction_received(instruction)
def _handle_command(self, cmd_type: str) -> None:
"""Handle built-in commands."""
if cmd_type == 'help':
show_help()
elif cmd_type == 'quit':
self.running = False
console.print("[bold blue]Goodbye![/bold blue]")
elif cmd_type == 'history':
show_action_history(self.history)
elif cmd_type == 'clear':
self.console.clear()
elif cmd_type == 'config':
console.print("[yellow]Configuration not loaded.[/yellow]")
def on_instruction_received(self, instruction: str) -> None:
"""Override this method in subclass to handle instructions."""
# Default implementation - should be overridden
console.print(f"[dim]Instruction received: {instruction}[/dim]")
def add_action_to_history(self, action: str, details: str, status: str = 'pending') -> None:
"""Add an action to the history."""
self.history.append({
'action': action,
'details': details,
'status': status,
'timestamp': datetime.now().isoformat(),
})
def clear_history(self) -> None:
"""Clear the action history."""
self.history.clear()
# ============================================================================
# Convenience Functions
# ============================================================================
def print_divider(title: str = "") -> None:
"""Print a styled divider."""
if title:
console.print(f"\n[bold cyan]{'=' * 20} {title} {'=' * 20}[/bold cyan]\n")
else:
console.print(f"\n[dim]{'=' * 50}[/dim]\n")
def print_banner(text: str) -> None:
"""Print a banner with the given text."""
console.print()
console.print(Panel(
f"[bold white]{text}[/bold white]",
border_style="cyan",
padding=(0, 2),
))
console.print()
# ============================================================================
# Entry Point
# ============================================================================
if __name__ == "__main__":
# Test the CLI module
cli = CLI()
cli.start()