-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmain.py
More file actions
110 lines (87 loc) · 4.85 KB
/
main.py
File metadata and controls
110 lines (87 loc) · 4.85 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
# Stagehand + Browserbase: Computer Use Agent (CUA) Example - See README.md for full documentation
import asyncio
import os
from dotenv import load_dotenv
from stagehand import Stagehand, StagehandConfig
# Load environment variables
load_dotenv()
# ============================================================================
# EXAMPLE INSTRUCTIONS - Choose one to test different scenarios
# ============================================================================
# Example 1: Learning Plan Creation
# instruction = """I want to learn more about Sourdough Bread Making. It's my first time learning about it, and want to get a good grasp by investing 1 hour a day for the next 2 months. Go find online courses/resources, create a plan cross-referencing the time I want to invest with the modules/timelines of the courses and return the plan"""
# Example 2: Flight Search
# instruction = """Use flights.google.com to find the lowest fare from all eligible one-way flights for 1 adult from JFK to Heathrow in the next 30 days."""
# Example 3: Solar Eclipse Research
instruction = """Search for the next visible solar eclipse in North America and its expected date, and what about the one after that."""
# Example 4: GitHub PR Verification
# instruction = """Find the most recently opened non-draft PR on Github for Browserbase's Stagehand project and make sure the combination-evals in the PR validation passed."""
# ============================================================================
async def main():
print("Starting Computer Use Agent Example...")
# Initialize Stagehand with Browserbase for cloud-based browser automation.
config = StagehandConfig(
env="BROWSERBASE",
api_key=os.environ.get("BROWSERBASE_API_KEY"),
model_api_key=os.environ.get(
"GOOGLE_API_KEY"
), # this is the model stagehand uses in act, observe, extract (not agent)
browserbase_session_create_params={
"proxies": True, # Using proxies will give the agent a better chance of success - requires Developer Plan or higher, comment out if you don't have access
"region": "us-west-2",
"browser_settings": {"block_ads": True, "viewport": {"width": 1288, "height": 711}},
},
verbose=1, # 0 = errors only, 1 = info, 2 = debug
# (When handling sensitive data like passwords or API keys, set verbose: 0 to prevent secrets from appearing in logs.)
# https://docs.stagehand.dev/configuration/logging
)
try:
async with Stagehand(config) as stagehand:
print("Stagehand initialized successfully!")
print(f"Live View Link: https://browserbase.com/sessions/{stagehand.session_id}")
page = stagehand.page
# Navigate to search engine with extended timeout for slow-loading sites.
print("Navigating to Google search...")
await page.goto(
"https://www.google.com/",
wait_until="domcontentloaded",
timeout=60000, # Extended timeout for reliable page loading
)
# Create agent with computer use capabilities for autonomous web browsing.
print("Creating Computer Use Agent...")
agent = stagehand.agent(
provider="google",
model="gemini-2.5-computer-use-preview-10-2025",
instructions=f"""You are a helpful assistant that can use a web browser.
You are currently on the following page: {page.url}.
Do not ask follow up questions, the user will trust your judgement. If you are getting blocked on google, try another search engine.""",
options={
"api_key": os.getenv("GOOGLE_API_KEY"),
},
)
# Execute the autonomous task with the Computer Use Agent
print("Executing instruction:", instruction)
result = await agent.execute(
instruction=instruction,
max_steps=30, # The maximum number of steps the agent can take to complete the task
auto_screenshot=True,
)
if result.success == True:
print("Task completed successfully!")
print("Result:", result)
else:
print("Task failed or was incomplete")
print("Session closed successfully")
except Exception as error:
print(f"Error executing computer use agent: {error}")
raise
if __name__ == "__main__":
try:
asyncio.run(main())
except Exception as err:
print(f"Error in computer use agent example: {err}")
print("Common issues:")
print(" - Check .env file has BROWSERBASE_API_KEY")
print(" - Verify GOOGLE_API_KEY is set for the agent")
print("Docs: https://docs.stagehand.dev/v3/first-steps/introduction")
exit(1)