-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcli.py
More file actions
198 lines (158 loc) · 7.13 KB
/
cli.py
File metadata and controls
198 lines (158 loc) · 7.13 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
from __future__ import annotations
import os
import asyncio
import logging
import argparse
from typing import Dict, List, Optional, Any
import readline
from pathlib import Path
from dataclasses import dataclass
from rich.console import Console
from rich.panel import Panel
from rich.progress import Progress
from rich.syntax import Syntax
from rich.prompt import Prompt
from rich import print as rprint
from rich.theme import Theme
from rich.table import Table
from pydantic_ai import Agent, Tool
from agent import (
configure_logging
)
from agent_manager import AgentManager
from dotenv import load_dotenv
from pydantic_ai.models.openai import OpenAIModel
parser = argparse.ArgumentParser()
parser.add_argument('--debug', action='store_true', help='Enable debug logging')
args = parser.parse_args()
logging.basicConfig(
level=logging.DEBUG if args.debug else logging.ERROR,
format='%(asctime)s - %(levelname)s - %(name)s - %(message)s'
)
for logger_name in logging.root.manager.loggerDict:
logging.getLogger(logger_name).setLevel(logging.DEBUG if args.debug else logging.ERROR)
custom_theme = Theme({
"info": "grey70",
"warning": "yellow",
"error": "red",
"success": "grey74",
"command": "bold blue",
"highlight": "dark_orange3",
})
console = Console(theme=custom_theme)
class CLI:
"""Handles CLI interaction and display."""
def __init__(self):
self.console = Console(theme=custom_theme)
self.history_file = os.path.join(os.getcwd(), ".compuse_history")
self.commands = ["/help", "/tools", "/history", "/clear", "/reset", "/exit", "/quit", "/bye"]
def setup_history(self):
"""Setup command history and autocompletion."""
try:
if not os.path.exists(self.history_file):
Path(self.history_file).touch()
readline.read_history_file(self.history_file)
readline.set_history_length(1000)
except (FileNotFoundError, PermissionError, OSError) as e:
self.console.print(f"[warning]Could not access history file: {e}[/warning]")
self.console.print("[info]Using temporary command history for this session[/info]")
def completer(text, state):
options = [cmd for cmd in self.commands if cmd.startswith(text)]
return options[state] if state < len(options) else None
readline.set_completer(completer)
readline.parse_and_bind("tab: complete")
def display_help(self):
"""Display help information."""
help_table = Table(title="CompUse CLI Commands")
help_table.add_column("Command", style="command")
help_table.add_column("Description", style="info")
help_table.add_row("/help", "Display this help message")
help_table.add_row("/tools", "List all available tools")
help_table.add_row("/history", "Show conversation history")
help_table.add_row("/clear", "Clear the screen")
help_table.add_row("/reset", "Reset the conversation history")
help_table.add_row("/exit, /quit, /bye", "Exit the application")
self.console.print(help_table)
async def run_cli():
"""Main CLI function to set up a server with both GUI and MCP tools."""
load_dotenv()
# Initialize CLI
cli = CLI()
cli.setup_history()
# Display welcome banner
cli.console.print(Panel.fit(
"[grey70]CompUse CLI[/grey70]\n"
"[grey70]Desktop & Browser Automation Assistant[/grey70]",
border_style="grey70"
))
agent_manager = None
try:
# Initialize agent manager
with cli.console.status("[dark_orange3]Initializing...", spinner="dots"):
agent_manager = await AgentManager.initialize()
# Display initial information
cli.console.print(agent_manager.get_tools_table())
cli.console.print("[success]Combined Agent initialized with both PyAutoGUI and MCP tools![/success]")
cli.console.print("[info]You can now give commands to control your computer and browser.[/info]")
cli.console.print("[info]Type [bold]/help[/bold] for available commands or [bold]/exit[/bold] to quit.[/info]")
while True:
try:
# Save history before each command
try:
readline.write_history_file(cli.history_file)
except (PermissionError, OSError):
pass
print()
prompt = "\033[1;32m > \033[0m"
user_input = input(prompt)
if user_input.lower() in ['/exit', '/quit', '/bye', 'exit', 'quit', 'bye']:
cli.console.print("[info]Shutting down...[/info]")
break
elif user_input.lower() == '/help':
cli.display_help()
continue
elif user_input.lower() == '/clear':
cli.console.clear()
continue
elif user_input.lower() == '/reset':
agent_manager.reset_history()
cli.console.print("[success]Conversation history has been reset[/success]")
continue
elif user_input.lower() == '/tools':
cli.console.print(agent_manager.get_tools_table())
continue
elif user_input.lower() == '/history':
cli.console.print(agent_manager.get_history_table())
continue
elif user_input.startswith('/'):
cli.console.print(f"[error]Command not found: {user_input}[/error]")
cli.console.print("[info]Type [bold]/help[/bold] to see available commands[/info]")
continue
# Process regular commands
with cli.console.status("[dark_orange3] ", spinner="point") as status:
result, elapsed = await agent_manager.run_command(user_input)
# status.update("[bold cyan] Processed.")
await asyncio.sleep(0.5)
# Display result
# cli.console.print(f"[bold cyan]Response ({elapsed:.2f}s):[/bold cyan]")
cli.console.print(f"{result}")
except KeyboardInterrupt:
break
except Exception as e:
cli.console.print(f"[error]Error: {str(e)}[/error]")
finally:
# Clean up agent manager
if agent_manager:
with cli.console.status("[dark_orange3] Exiting...", spinner="dots"):
await agent_manager.cleanup()
cli.console.print("[success]Resources cleaned up successfully[/success]")
# Save history
try:
readline.write_history_file(cli.history_file)
except (PermissionError, OSError, Exception):
pass
cli.console.print("[info]Goodbye![/info]")
def main():
asyncio.run(run_cli())
if __name__ == "__main__":
main()