-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselenium_cli.py
More file actions
132 lines (109 loc) Β· 5.28 KB
/
selenium_cli.py
File metadata and controls
132 lines (109 loc) Β· 5.28 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
#!/usr/bin/env python3
"""
Natural Selenium Agent CLI - Feels like chatting with AI directly
"""
import requests
import time
import json
def check_server():
"""Check if the selenium server is running"""
try:
response = requests.get("http://localhost:8001/health")
return response.status_code == 200
except:
return False
def start_chat():
"""Start a natural chat session"""
base_url = "http://localhost:8001"
current_task_id = None
print("π€ **AI Browser Assistant**")
print("=" * 50)
print("Hi! I'm your AI browser automation assistant.")
print("Tell me what you'd like me to do, and I'll handle it step by step.")
print("(Type 'quit' to exit)")
print("=" * 50)
while True:
try:
# Get user input
user_input = input("\n㪠You: ").strip()
if user_input.lower() in ['quit', 'exit', 'bye']:
if current_task_id:
print("π Cleaning up...")
try:
requests.delete(f"{base_url}/api/selenium/task/{current_task_id}")
except:
pass
print("π See you later!")
break
if not user_input:
continue
# If no active task, start a new one
if not current_task_id:
print("π€ AI: Let me help you with that! Starting up my browser...")
# Create new task
task_resp = requests.post(f"{base_url}/api/selenium/task",
json={"task": user_input})
if task_resp.status_code == 200:
task_data = task_resp.json()
current_task_id = task_data["task_id"]
# Get initial AI response by checking task status
status_resp = requests.get(f"{base_url}/api/selenium/task/{current_task_id}/status")
if status_resp.status_code == 200:
status_data = status_resp.json()
if status_data.get('chat_history'):
last_message = status_data['chat_history'][-1]
if last_message['role'] == 'assistant':
print(f"π€ AI: {last_message['content']}")
else:
print("π€ AI: I'm ready to help! What would you like me to do?")
else:
print("π€ AI: I'm ready to help! What would you like me to do?")
else:
print("π€ AI: Task started! How can I help you?")
else:
print(f"β Sorry, I had trouble starting: {task_resp.text}")
continue
else:
# Send message to existing task
print("π€ AI: Let me work on that...")
msg_resp = requests.post(f"{base_url}/api/selenium/task/{current_task_id}/message",
json={"message": user_input})
if msg_resp.status_code == 200:
msg_data = msg_resp.json()
ai_response = msg_data.get("response", "Working on it...")
# Show AI response naturally
print(f"π€ AI: {ai_response}")
# Show current status naturally
status = msg_data.get("status", "")
if status == "completed":
print("\nβ
Task completed! Want me to help with anything else?")
current_task_id = None # Ready for new task
elif status == "failed":
print("\nβ I ran into some trouble. Let me know what else you'd like to try!")
current_task_id = None # Ready for new task
elif status == "user_intervention":
print("\nπ€ I could use some guidance - what should I do next?")
# Optionally show URL if it's interesting
current_url = msg_data.get("current_url", "")
if current_url and current_url != "data:," and "google.com" in current_url or "youtube.com" in current_url:
print(f"π Currently viewing: {current_url[:80]}...")
else:
print(f"β I had trouble processing that: {msg_resp.text}")
except KeyboardInterrupt:
print("\nπ Goodbye!")
if current_task_id:
try:
requests.delete(f"{base_url}/api/selenium/task/{current_task_id}")
except:
pass
break
except Exception as e:
print(f"β Something went wrong: {e}")
def main():
if not check_server():
print("β Selenium server not running!")
print("Please start it with: python selenium_server.py")
return
start_chat()
if __name__ == "__main__":
main()