Skip to content

Commit ed2cae0

Browse files
committed
Add remote and local browser examples
1 parent 34dc396 commit ed2cae0

3 files changed

Lines changed: 118 additions & 3 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,20 +186,20 @@ Install dependencies, set credentials, and run the scripts below.
186186
bundle install
187187
```
188188

189-
Remote API example:
189+
Remote browser example:
190190

191191
```bash
192192
export BROWSERBASE_API_KEY="your-browserbase-api-key"
193193
export BROWSERBASE_PROJECT_ID="your-browserbase-project-id"
194194
export MODEL_API_KEY="your-openai-api-key"
195-
bundle exec ruby examples/basic.rb
195+
bundle exec ruby examples/remote_browser_example.rb
196196
```
197197

198198
Local mode example (embedded server, local Chrome/Chromium):
199199

200200
```bash
201201
export MODEL_API_KEY="your-openai-api-key"
202-
bundle exec ruby examples/local.rb
202+
bundle exec ruby examples/local_browser_example.rb
203203
```
204204

205205
Playwright local example:

examples/remote_browser_example.rb

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require "bundler/setup"
5+
require "stagehand"
6+
7+
# Remote browser example using Browserbase + Stagehand.
8+
# Requires BROWSERBASE_API_KEY, BROWSERBASE_PROJECT_ID, and MODEL_API_KEY.
9+
10+
browserbase_api_key = ENV["BROWSERBASE_API_KEY"].to_s
11+
browserbase_project_id = ENV["BROWSERBASE_PROJECT_ID"].to_s
12+
model_key = ENV["MODEL_API_KEY"].to_s
13+
14+
missing = []
15+
missing << "BROWSERBASE_API_KEY" if browserbase_api_key.empty?
16+
missing << "BROWSERBASE_PROJECT_ID" if browserbase_project_id.empty?
17+
missing << "MODEL_API_KEY" if model_key.empty?
18+
19+
unless missing.empty?
20+
warn "Set #{missing.join(', ')} to run the remote browser example."
21+
exit 1
22+
end
23+
24+
client = Stagehand::Client.new(
25+
browserbase_api_key: browserbase_api_key,
26+
browserbase_project_id: browserbase_project_id,
27+
model_api_key: model_key
28+
)
29+
30+
session_id = nil
31+
32+
begin
33+
start_response = client.sessions.start(
34+
model_name: "openai/gpt-5-nano"
35+
)
36+
session_id = start_response.data.session_id
37+
puts("Session started: #{session_id}")
38+
39+
client.sessions.navigate(session_id, url: "https://news.ycombinator.com")
40+
puts("Navigated to Hacker News")
41+
42+
observe_response = client.sessions.observe(
43+
session_id,
44+
instruction: "find the link to view comments for the top post"
45+
)
46+
47+
actions = observe_response.data.result
48+
puts("Found #{actions.length} possible actions")
49+
50+
action = actions.first
51+
unless action
52+
warn("No actions found")
53+
exit(1)
54+
end
55+
56+
puts("Acting on: #{action.description}")
57+
58+
act_response = client.sessions.act(
59+
session_id,
60+
input: action.to_h.merge(method: "click")
61+
)
62+
puts("Act completed: #{act_response.data.result[:message]}")
63+
64+
extract_response = client.sessions.extract(
65+
session_id,
66+
instruction: "extract the text of the top comment on this page",
67+
schema: {
68+
type: "object",
69+
properties: {
70+
comment_text: {
71+
type: "string",
72+
description: "The text content of the top comment"
73+
},
74+
author: {
75+
type: "string",
76+
description: "The username of the comment author"
77+
}
78+
},
79+
required: ["comment_text"]
80+
}
81+
)
82+
puts("Extracted data: #{extract_response.data.result}")
83+
84+
extracted_data = extract_response.data.result
85+
author = extracted_data.is_a?(Hash) ? extracted_data[:author] : nil
86+
author ||= "unknown"
87+
puts("Looking up profile for author: #{author}")
88+
89+
instruction = [
90+
"Find any personal website, GitHub, or LinkedIn for the Hacker News user '#{author}'.",
91+
"Click their username to open the profile and look for shared links."
92+
].join(" ")
93+
94+
execute_response = client.sessions.execute(
95+
session_id,
96+
execute_options: {
97+
instruction: instruction,
98+
max_steps: 15
99+
},
100+
agent_config: {
101+
model: Stagehand::ModelConfig.new(
102+
model_name: "openai/gpt-5-nano",
103+
api_key: model_key
104+
),
105+
cua: false
106+
}
107+
)
108+
109+
puts("Agent completed: #{execute_response.data.result[:message]}")
110+
puts("Agent success: #{execute_response.data.result[:success]}")
111+
puts("Agent actions taken: #{execute_response.data.result[:actions]&.length || 0}")
112+
ensure
113+
client.sessions.end_(session_id) if session_id
114+
client.close
115+
end

0 commit comments

Comments
 (0)