-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentient_task.py
96 lines (83 loc) · 3.38 KB
/
sentient_task.py
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
import asyncio
import sys
import subprocess
import platform
import requests
import time
from sentient import sentient
import traceback
import pkg_resources
async def check_browser_running():
system = platform.system()
if system == "Darwin": # macOS
cmd = "pgrep -f 'Google Chrome|Brave Browser|Brave Browser Beta'"
elif system == "Linux":
cmd = "pgrep -f 'chrome|brave|brave-browser-beta'"
elif system == "Windows":
cmd = "tasklist | findstr /I \"chrome.exe brave.exe\""
else:
print(f"Unsupported operating system: {system}")
return False
try:
output = subprocess.check_output(cmd, shell=True).decode('utf-8')
return bool(output.strip())
except subprocess.CalledProcessError:
return False
async def check_browser_debugging_port():
max_retries = 10
for attempt in range(max_retries):
try:
response = requests.get("http://localhost:9222/json/version", timeout=5)
if response.status_code == 200:
return True
except requests.RequestException:
print(f"Waiting for debugging port to open... (attempt {attempt + 1})")
pass
time.sleep(2) # Wait for 2 seconds before retrying
return False
async def run_sentient(goal):
try:
browser_running = await check_browser_running()
debugging_port_open = await check_browser_debugging_port()
if not browser_running or not debugging_port_open:
print("Error: Browser is not running or debugging port is not open.")
print("Please ensure Chrome, Brave, or Brave Beta is running with remote debugging enabled on port 9222.")
return
print("Attempting to connect to browser...")
try:
sentient_version = pkg_resources.get_distribution("sentient").version
print(f"Sentient version: {sentient_version}")
except pkg_resources.DistributionNotFound:
print("Sentient version: Unable to determine")
print(f"Goal: {goal}")
try:
print(f"Executing goal: {goal}")
result = await sentient.invoke(goal=goal)
print(f"Execution result: {result}")
# Add more detailed logging
print("Current URL:", await sentient.get_current_url())
print("Page Title:", await sentient.get_page_title())
except Exception as e:
print(f"Error executing the command: {str(e)}")
print("\nFull traceback:")
traceback.print_exc()
print("\nDetailed error information:")
print(f"Error type: {type(e).__name__}")
print(f"Error message: {str(e)}")
if hasattr(e, 'args'):
print(f"Error arguments: {e.args}")
# Add more error context
print("Current URL:", await sentient.get_current_url())
print("Page Title:", await sentient.get_page_title())
print("Page Source:", await sentient.get_page_source())
except Exception as e:
print(f"Error occurred: {str(e)}")
print("\nFull traceback:")
traceback.print_exc()
# ... (rest of the error handling)
if __name__ == "__main__":
if len(sys.argv) > 1:
goal = " ".join(sys.argv[1:])
asyncio.run(run_sentient(goal))
else:
print("Please provide a goal for Sentient.")