-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbrowserbase_live.rs
More file actions
178 lines (158 loc) · 5.99 KB
/
browserbase_live.rs
File metadata and controls
178 lines (158 loc) · 5.99 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
use stagehand_sdk::{Stagehand, V3Options, Env, Model, TransportChoice, AgentConfig, AgentExecuteOptions, ModelConfiguration};
use stagehand_sdk::{ActResponseEvent, ExtractResponseEvent, ExecuteResponseEvent, NavigateResponseEvent, ObserveResponseEvent};
use futures::StreamExt;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Serialize, Deserialize, Debug)]
struct PageInfo {
title: String,
description: String,
}
#[tokio::test]
async fn test_browserbase_live() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
// Load environment variables from .env
dotenvy::dotenv().ok();
// 1. Create client, specifying REST transport (uses STAGEHAND_BASE_URL env var or default)
let mut stagehand = Stagehand::connect(TransportChoice::default_rest()).await?;
// 2. Configure V3 Options
let opts = V3Options {
env: Some(Env::Browserbase),
verbose: Some(2),
model: Some(Model::String("openai/gpt-5-nano".into())),
..Default::default()
};
// 3. Start session
println!("Starting session...");
stagehand.start(opts).await?;
println!("Session ID: {:?}", stagehand.session_id());
// 4. Navigate to example.com
println!("\n=== NAVIGATE ===");
let mut nav_stream = stagehand.navigate("https://example.com", Some(30_000), None).await?;
while let Some(msg) = nav_stream.next().await {
if let Ok(event) = msg {
match event.event {
Some(NavigateResponseEvent::Log(log_msg)) => println!("[NAV LOG] {:?}", log_msg),
Some(NavigateResponseEvent::Success(s)) => println!("[NAV RESULT] Success: {}", s),
_ => {}
}
} else if let Err(e) = msg {
eprintln!("Navigate stream error: {:?}", e);
}
}
// 5. Observe - find elements on the page
println!("\n=== OBSERVE ===");
let mut observe_stream = stagehand.observe(
Some("Find the main heading and any links on the page".to_string()),
Some(Model::String("openai/gpt-5-nano".into())),
Some(30_000),
None,
None,
).await?;
while let Some(msg) = observe_stream.next().await {
if let Ok(event) = msg {
match event.event {
Some(ObserveResponseEvent::Log(l)) => println!("[OBSERVE LOG] {:?}", l),
Some(ObserveResponseEvent::ElementsJson(json)) => {
println!("[OBSERVE RESULT] Elements: {}", json);
}
_ => {}
}
} else if let Err(e) = msg {
eprintln!("Observe stream error: {:?}", e);
}
}
// 6. Extract - get page info
println!("\n=== EXTRACT ===");
// Schema must be in JSON Schema format, not a template object
let schema = serde_json::json!({
"type": "object",
"properties": {
"title": { "type": "string" },
"description": { "type": "string" }
}
});
let mut extract_stream = stagehand.extract(
"Extract the page title and description text",
schema,
Some(Model::String("openai/gpt-5-nano".into())),
Some(30_000),
None,
None,
).await?;
while let Some(msg) = extract_stream.next().await {
if let Ok(event) = msg {
match event.event {
Some(ExtractResponseEvent::Log(l)) => println!("[EXTRACT LOG] {:?}", l),
Some(ExtractResponseEvent::DataJson(json)) => {
if json == "null" || json.is_empty() {
println!("[EXTRACT RESULT] No data extracted");
} else {
match serde_json::from_str::<PageInfo>(&json) {
Ok(info) => println!("[EXTRACT RESULT] Page Info: {:?}", info),
Err(e) => println!("[EXTRACT RESULT] Parse error: {} - Raw: {}", e, json),
}
}
}
_ => {}
}
} else if let Err(e) = msg {
eprintln!("Extract stream error: {:?}", e);
}
}
// 7. Act - click on the "More information" link
println!("\n=== ACT ===");
let mut act_stream = stagehand.act(
"Click on the 'More information...' link",
Some(Model::String("openai/gpt-5-nano".into())),
HashMap::new(),
Some(30_000),
None,
).await?;
while let Some(msg) = act_stream.next().await {
if let Ok(event) = msg {
match event.event {
Some(ActResponseEvent::Log(log_msg)) => println!("[ACT LOG] {:?}", log_msg),
Some(ActResponseEvent::Success(s)) => println!("[ACT RESULT] Success: {}", s),
_ => {}
}
} else if let Err(e) = msg {
eprintln!("Act stream error: {:?}", e);
}
}
// 8. Execute with agent - verify where we are
println!("\n=== EXECUTE ===");
let agent_config = AgentConfig {
provider: None,
model: Some(ModelConfiguration::String("openai/gpt-5-nano".into())),
system_prompt: None,
cua: None,
};
let execute_options = AgentExecuteOptions {
instruction: "What is the current page URL and title?".to_string(),
max_steps: Some(5),
highlight_cursor: None,
};
let mut execute_stream = stagehand.execute(
agent_config,
execute_options,
None,
).await?;
while let Some(msg) = execute_stream.next().await {
if let Ok(event) = msg {
match event.event {
Some(ExecuteResponseEvent::Log(l)) => println!("[EXECUTE LOG] {:?}", l),
Some(ExecuteResponseEvent::ResultJson(r)) => {
println!("[EXECUTE RESULT] {}", r);
},
_ => {}
}
} else if let Err(e) = msg {
eprintln!("Execute stream error: {:?}", e);
}
}
// 9. Close
println!("\n=== CLOSE ===");
stagehand.end().await?;
println!("Session closed successfully");
Ok(())
}