|
33 | 33 | quiet_dependencies=True, # Reduce noise from dependencies |
34 | 34 | ) |
35 | 35 |
|
36 | | -console.print( |
37 | | - Panel.fit( |
38 | | - "[yellow]Logging Levels:[/]\n" |
39 | | - "[white]- Set [bold]verbose=0[/] for errors (ERROR)[/]\n" |
40 | | - "[white]- Set [bold]verbose=1[/] for minimal logs (INFO)[/]\n" |
41 | | - "[white]- Set [bold]verbose=2[/] for medium logs (WARNING)[/]\n" |
42 | | - "[white]- Set [bold]verbose=3[/] for detailed logs (DEBUG)[/]", |
43 | | - title="Verbosity Options", |
44 | | - border_style="blue", |
45 | | - ) |
46 | | -) |
47 | | - |
48 | 36 | async def main(): |
49 | 37 | # Build a unified configuration object for Stagehand |
50 | 38 | config = StagehandConfig( |
51 | 39 | env="BROWSERBASE", |
| 40 | + # env="LOCAL", |
52 | 41 | api_key=os.getenv("BROWSERBASE_API_KEY"), |
53 | 42 | project_id=os.getenv("BROWSERBASE_PROJECT_ID"), |
54 | | - headless=False, |
55 | | - dom_settle_timeout_ms=3000, |
56 | 43 | model_name="gpt-4o", |
57 | 44 | self_heal=True, |
58 | | - wait_for_captcha_solves=True, |
59 | 45 | system_prompt="You are a browser automation assistant that helps users navigate websites effectively.", |
60 | 46 | model_client_options={"apiKey": os.getenv("MODEL_API_KEY")}, |
61 | | - verbose=2, |
| 47 | + verbose=1, |
62 | 48 | ) |
63 | 49 |
|
64 | 50 | # Create a Stagehand client using the configuration object. |
65 | | - stagehand = Stagehand( |
66 | | - config=config, |
67 | | - api_url=os.getenv("STAGEHAND_API_URL"), |
68 | | - ) |
| 51 | + stagehand = Stagehand(config) |
69 | 52 |
|
70 | 53 | # Initialize - this creates a new session automatically. |
71 | 54 | console.print("\n🚀 [info]Initializing Stagehand...[/]") |
72 | 55 | await stagehand.init() |
73 | | - console.print(f"\n[yellow]Created new session:[/] {stagehand.session_id}") |
74 | | - console.print( |
75 | | - f"🌐 [white]View your live browser:[/] [url]https://www.browserbase.com/sessions/{stagehand.session_id}[/]" |
76 | | - ) |
| 56 | + if stagehand.env == "BROWSERBASE": |
| 57 | + console.print(f"\n[yellow]Created new session:[/] {stagehand.session_id}") |
| 58 | + console.print( |
| 59 | + f"🌐 [white]View your live browser:[/] [url]https://www.browserbase.com/sessions/{stagehand.session_id}[/]" |
| 60 | + ) |
| 61 | + |
| 62 | + console.print("\n▶️ [highlight] Navigating[/] to Google") |
| 63 | + await stagehand.page.goto("https://google.com/") |
| 64 | + console.print("✅ [success]Navigated to Google[/]") |
77 | 65 |
|
78 | | - # Configure the agent |
79 | | - agent_config = AgentConfig( |
80 | | - provider=AgentProvider.OPENAI, |
| 66 | + console.print("\n▶️ [highlight] Using Agent to perform a task[/]: playing a game of 2048") |
| 67 | + agent = stagehand.agent( |
81 | 68 | model="computer-use-preview", |
82 | 69 | instructions="You are a helpful web navigation assistant that helps users find information. You are currently on the following page: google.com. Do not ask follow up questions, the user will trust your judgement.", |
83 | 70 | options={"apiKey": os.getenv("MODEL_API_KEY")} |
84 | 71 | ) |
85 | | - |
86 | | - # Define the task for the agent |
87 | | - execute_options = AgentExecuteOptions( |
| 72 | + agent_result = await agent.execute( |
88 | 73 | instruction="Play a game of 2048", |
89 | 74 | max_steps=20, |
90 | 75 | auto_screenshot=True, |
91 | 76 | ) |
92 | 77 |
|
93 | | - console.print("\n▶️ [highlight] Navigating[/] to Google") |
94 | | - await stagehand.page.goto("https://google.com/") |
95 | | - console.print("✅ [success]Navigated to Google[/]") |
96 | | - |
97 | | - console.print("\n▶️ [highlight] Using Agent to perform a task[/]: playing a game of 2048") |
98 | | - agent = stagehand.agent(agent_config) |
99 | | - agent_result = await agent.execute(execute_options) |
100 | | - |
101 | 78 | console.print("📊 [info]Agent execution result:[/]") |
102 | 79 | console.print(f"✅ Success: [bold]{'Yes' if agent_result.success else 'No'}[/]") |
103 | 80 | console.print(f"🎯 Completed: [bold]{'Yes' if agent_result.completed else 'No'}[/]") |
@@ -125,7 +102,7 @@ async def main(): |
125 | 102 | console.print( |
126 | 103 | "\n", |
127 | 104 | Panel.fit( |
128 | | - "[light_gray]Stagehand 🤘 Async Agent Example[/]", |
| 105 | + "[light_gray]Stagehand 🤘 Agent Example[/]", |
129 | 106 | border_style="green", |
130 | 107 | padding=(1, 10), |
131 | 108 | ), |
|
0 commit comments