-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
277 lines (236 loc) · 10.2 KB
/
Copy pathmain.py
File metadata and controls
277 lines (236 loc) · 10.2 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
import os
import time
import asyncio
import requests
from dotenv import load_dotenv
from playwright.async_api import async_playwright
# Load environment variables
load_dotenv()
# Configuration constants
CONFIG = {
'API_BASE_URL': 'https://api.agentstation.ai/v1',
'VIEWPORT': {'width': 1280, 'height': 720},
'TIMEOUTS': {
'PAGE_LOAD': 120000,
'NAVIGATION': 60000,
'TYPE_DELAY': 20,
'PAGE_PAUSE': 2,
'LONG_PAUSE': 5,
'WORKSTATION_SETUP': 10
}
}
class WorkstationManager:
def __init__(self, api_key):
self.api_key = api_key
self.workstation = None
self.browser = None
self.context = None
self.playwright = None
async def create_workstation(self):
print('🚀 Creating new workstation...')
response = requests.post(
f"{CONFIG['API_BASE_URL']}/workstations",
json={'name': 'playwright-workstation', 'type': 'default'},
headers=self.get_headers()
)
response.raise_for_status()
self.workstation = response.json()
print('✅ Workstation created:', self.workstation)
print('🔗 You can go to https://app.agentstation.ai/workstations to see the workstation')
async def connect_browser(self, playwright):
print('🔌 Getting browser WebSocket URL...')
try:
response = requests.post(
f"{CONFIG['API_BASE_URL']}/workstations/{self.workstation['id']}/browser/cdp",
json={},
headers=self.get_headers()
)
response.raise_for_status()
browser_data = response.json()
endpoint_url = browser_data['url']
print('🔍 Connecting to browser...')
try:
self.browser = await playwright.chromium.connect_over_cdp(
endpoint_url,
timeout=CONFIG['TIMEOUTS']['PAGE_LOAD']
)
print('✅ Initial browser connection established')
except Exception as e:
print(f'❌ Failed to connect to browser: {str(e)}')
print(f'🔍 Attempted to connect to: {endpoint_url}')
print(f'⏱️ Timeout was set to: {CONFIG["TIMEOUTS"]["PAGE_LOAD"]}ms')
raise
# Verify connection is stable
await asyncio.sleep(2)
print('✅ Browser connection verified and stable')
return True
except Exception as error:
print('⚠️ Browser connection failed:', str(error))
# Attempt cleanup if connection fails
if self.browser:
try:
await self.browser.close()
except:
pass
self.browser = None
raise
async def cleanup(self):
try:
cleanup_task = asyncio.create_task(self._do_cleanup())
await asyncio.wait_for(cleanup_task, timeout=CONFIG['TIMEOUTS']['LONG_PAUSE'])
except asyncio.TimeoutError:
print('⚠️ Cleanup operation timed out')
async def _do_cleanup(self):
try:
if self.browser:
try:
print('🔒 Closing browser...')
await self.browser.close()
except Exception as err:
print('⚠️ Error closing browser:', str(err))
if self.workstation:
try:
print('🧹 Cleaning up workstation...')
response = requests.delete(
f"{CONFIG['API_BASE_URL']}/workstations/{self.workstation['id']}",
headers=self.get_headers()
)
response.raise_for_status()
print('✨ All done! Goodbye!')
except Exception as err:
print('⚠️ Error cleaning up workstation:', str(err))
except Exception as err:
print('⚠️ Error during cleanup:', str(err))
def get_headers(self):
return {
'Content-Type': 'application/json',
'Authorization': f"Bearer {self.api_key}"
}
async def setup_browser(self, playwright):
"""Set up the browser and return a configured page"""
await self.create_workstation()
await asyncio.sleep(CONFIG['TIMEOUTS']['WORKSTATION_SETUP'])
print('🔌 Setting up browser connection...')
await self._connect_browser_with_retry(playwright)
# Get the default context and page that's already open
self.context = self.browser.contexts[0]
page = self.context.pages[0]
# Configure viewport if needed
await page.set_viewport_size(CONFIG['VIEWPORT'])
await asyncio.sleep(1) # Short stabilization pause
return page
async def _connect_browser_with_retry(self, playwright, max_retries=3):
"""Attempt to connect to browser with retries"""
for attempt in range(max_retries):
try:
await self.connect_browser(playwright)
return
except Exception as e:
if attempt == max_retries - 1:
raise
print(f'Retrying browser connection ({attempt + 1}/{max_retries})...')
await asyncio.sleep(2)
class DemoRunner:
def __init__(self, page):
if not page:
raise ValueError('Page instance is required')
self.page = page
async def perform_google_search(self):
print('🌟 Starting Google search demonstration...')
await self.page.goto('https://google.com')
print('✅ Google homepage loaded successfully')
print('🔍 Preparing to search for "agentstation.ai"...')
await self.page.wait_for_selector('textarea[name="q"]')
await self.page.fill('textarea[name="q"]', 'agentstation.ai')
await self.page.wait_for_timeout(CONFIG['TIMEOUTS']['PAGE_PAUSE'] * 250)
print('🚀 Submitting search query...')
await self.page.keyboard.press('Enter')
async def click_search_result(self):
print('⏳ Waiting for search results to load...')
try:
await self.page.wait_for_selector('h3.LC20lb')
await self.page.wait_for_timeout(CONFIG['TIMEOUTS']['PAGE_PAUSE'] * 1000)
print('✨ Clicking first search result (AgentStation website)...')
async with self.page.expect_navigation(
timeout=CONFIG['TIMEOUTS']['NAVIGATION'],
wait_until='domcontentloaded'
):
await self.page.click('h3.LC20lb')
except Exception as error:
print('⚠️ Error finding or clicking search result:', str(error))
raise
async def navigate_to_launch_page(self):
print('⌛ Waiting for AgentStation page to fully load...')
await self.page.wait_for_timeout(CONFIG['TIMEOUTS']['LONG_PAUSE'] * 1000)
print('✅ AgentStation page loaded successfully')
print('🎯 Preparing to navigate to AgentStation launch page...')
await self.page.wait_for_timeout(CONFIG['TIMEOUTS']['PAGE_PAUSE'] * 1000)
try:
await self.page.goto(
'https://agentstation.ai/launch',
timeout=CONFIG['TIMEOUTS']['PAGE_LOAD'],
wait_until='domcontentloaded'
)
await self.page.wait_for_timeout(CONFIG['TIMEOUTS']['PAGE_PAUSE'] * 1000)
print('🎉 Successfully arrived at AgentStation launch page')
except Exception as error:
print('⚠️ Navigation to launch page encountered an issue:', str(error))
print('🔄 Continuing with the demonstration...')
async def run_full_demo(self):
"""Execute the complete demo sequence"""
print('🌟 Starting demonstration sequence...')
await self.perform_google_search()
await self.click_search_result()
await self.navigate_to_launch_page()
print('✨ Demo sequence completed successfully!')
async def run_single_demo(workstation_manager=None, existing_page=None, playwright=None):
"""Run a single iteration of the demo"""
api_key = os.getenv('AGENTSTATION_API_KEY')
if not api_key:
raise ValueError('🔑 Please set your AGENTSTATION_API_KEY environment variable.')
try:
if not existing_page:
workstation_manager = WorkstationManager(api_key)
page = await workstation_manager.setup_browser(playwright)
else:
page = existing_page
print('♻️ Reusing existing browser session...')
demo = DemoRunner(page)
await demo.run_full_demo()
return {'page': page, 'workstation_manager': workstation_manager}
except Exception as error:
print('❌ An error occurred:', str(error))
if not existing_page and workstation_manager:
await workstation_manager.cleanup()
raise
async def run_demo():
"""Main demo loop with user interaction"""
workstation_manager = None
current_page = None
try:
async with async_playwright() as playwright:
while True:
try:
result = await run_single_demo(workstation_manager, current_page, playwright)
current_page = result['page']
workstation_manager = result['workstation_manager']
except Exception as error:
print('❌ Demo run failed:', str(error))
if not workstation_manager:
raise
if not await should_continue():
break
finally:
if workstation_manager:
await workstation_manager.cleanup()
async def should_continue():
"""Ask user if they want to continue"""
print('\n🤔 Would you like to run another demo?')
answer = input('Please enter (y/n): ')
if answer.lower() != 'y':
print('👋 Thanks for trying the demo!')
return False
print('\n🔄 Starting another demo run...\n')
return True
if __name__ == '__main__':
asyncio.run(run_demo())