Skip to content

Commit 34dc396

Browse files
committed
Fix local cache handling and examples
1 parent 97e6428 commit 34dc396

10 files changed

Lines changed: 82 additions & 71 deletions

File tree

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -395,25 +395,25 @@ stagehand.sessions.act("00000000-your-session-id-000000000000", **params)
395395
Since this library does not depend on `sorbet-runtime`, it cannot provide [`T::Enum`](https://sorbet.org/docs/tenum) instances. Instead, we provide "tagged symbols" instead, which is always a primitive at runtime:
396396

397397
```ruby
398-
# :typescript
399-
puts(Stagehand::SessionActParams::XLanguage::TYPESCRIPT)
398+
# :true
399+
puts(Stagehand::SessionActParams::XStreamResponse::TRUE)
400400

401-
# Revealed type: `T.all(Stagehand::SessionActParams::XLanguage, Symbol)`
402-
T.reveal_type(Stagehand::SessionActParams::XLanguage::TYPESCRIPT)
401+
# Revealed type: `T.all(Stagehand::SessionActParams::XStreamResponse, Symbol)`
402+
T.reveal_type(Stagehand::SessionActParams::XStreamResponse::TRUE)
403403
```
404404

405405
Enum parameters have a "relaxed" type, so you can either pass in enum constants or their literal value:
406406

407407
```ruby
408408
# Using the enum constants preserves the tagged type information:
409409
stagehand.sessions.act(
410-
x_language: Stagehand::SessionActParams::XLanguage::TYPESCRIPT,
410+
x_stream_response: Stagehand::SessionActParams::XStreamResponse::TRUE,
411411
#
412412
)
413413

414414
# Literal values are also permissible:
415415
stagehand.sessions.act(
416-
x_language: :typescript,
416+
x_stream_response: :true,
417417
#
418418
)
419419
```

examples/local.rb

100644100755
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,40 @@
2222
begin
2323
start_response = client.sessions.start(
2424
model_name: "openai/gpt-5-nano",
25-
browser: { type: :local }
25+
browser: {type: :local}
2626
)
2727
session_id = start_response.data.session_id
28-
puts "Session started: #{session_id}"
28+
puts("Session started: #{session_id}")
2929

3030
client.sessions.navigate(session_id, url: "https://example.com")
31-
puts "Navigated to example.com"
31+
puts("Navigated to example.com")
3232

3333
observe_response = client.sessions.observe(
3434
session_id,
3535
instruction: "Find all clickable links on this page"
3636
)
37-
puts "Found #{observe_response.data.result.length} possible actions"
37+
puts("Found #{observe_response.data.result.length} possible actions")
3838

3939
action = observe_response.data.result.first
4040
act_input = action ? action.to_h.merge(method: "click") : "Click the 'Learn more' link"
4141
act_response = client.sessions.act(
4242
session_id,
4343
input: act_input
4444
)
45-
puts "Act completed: #{act_response.data.result[:message]}"
45+
puts("Act completed: #{act_response.data.result[:message]}")
4646

4747
extract_response = client.sessions.extract(
4848
session_id,
4949
instruction: "extract the main heading and any links on this page",
5050
schema: {
5151
type: "object",
5252
properties: {
53-
heading: { type: "string" },
54-
links: { type: "array", items: { type: "string" } }
53+
heading: {type: "string"},
54+
links: {type: "array", items: {type: "string"}}
5555
}
5656
}
5757
)
58-
puts "Extracted: #{extract_response.data.result}"
58+
puts("Extracted: #{extract_response.data.result}")
5959

6060
execute_response = client.sessions.execute(
6161
session_id,
@@ -64,15 +64,15 @@
6464
max_steps: 3
6565
},
6666
agent_config: {
67-
model: Stagehand::ModelConfig::ModelConfigObject.new(
67+
model: Stagehand::ModelConfig.new(
6868
model_name: "openai/gpt-5-nano",
6969
api_key: model_key
7070
),
7171
cua: false
7272
}
7373
)
74-
puts "Agent completed: #{execute_response.data.result[:message]}"
75-
puts "Agent success: #{execute_response.data.result[:success]}"
74+
puts("Agent completed: #{execute_response.data.result[:message]}")
75+
puts("Agent success: #{execute_response.data.result[:success]}")
7676
ensure
7777
client.sessions.end_(session_id) if session_id
7878
client.close

examples/playwright_local_example.rb

100644100755
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env ruby
2+
# typed: ignore
23
# frozen_string_literal: true
34

45
require "bundler/setup"
@@ -23,10 +24,10 @@
2324
# bundle exec ruby examples/playwright_local_example.rb
2425

2526
begin
26-
require "playwright"
27+
require("playwright")
2728
rescue LoadError
28-
warn "Playwright is not installed. Run: gem install playwright-ruby-client"
29-
exit 1
29+
warn("Playwright is not installed. Run: gem install playwright-ruby-client")
30+
exit(1)
3031
end
3132

3233
DEBUG_PORT = 9222
@@ -74,6 +75,7 @@ def fetch_page_target_id(port, page_url)
7475
exit 1
7576
end
7677

78+
# rubocop:disable Metrics/BlockLength
7779
Playwright.create(playwright_cli_executable_path: "./node_modules/.bin/playwright") do |playwright|
7880
browser = playwright.chromium.launch(
7981
headless: false,
@@ -106,14 +108,14 @@ def fetch_page_target_id(port, page_url)
106108
}
107109
)
108110
session_id = start_response.data.session_id
109-
puts "Session started: #{session_id}"
111+
puts("Session started: #{session_id}")
110112

111113
observe_response = client.sessions.observe(
112114
session_id,
113115
frame_id: page_target_id,
114116
instruction: "Find all clickable links on this page"
115117
)
116-
puts "Found #{observe_response.data.result.length} possible actions"
118+
puts("Found #{observe_response.data.result.length} possible actions")
117119

118120
action = observe_response.data.result.first
119121
act_input = action ? action.to_h.merge(method: "click") : "Click the 'Learn more' link"
@@ -122,7 +124,7 @@ def fetch_page_target_id(port, page_url)
122124
frame_id: page_target_id,
123125
input: act_input
124126
)
125-
puts "Act completed: #{act_response.data.result[:message]}"
127+
puts("Act completed: #{act_response.data.result[:message]}")
126128

127129
extract_response = client.sessions.extract(
128130
session_id,
@@ -131,12 +133,12 @@ def fetch_page_target_id(port, page_url)
131133
schema: {
132134
type: "object",
133135
properties: {
134-
heading: { type: "string" },
135-
links: { type: "array", items: { type: "string" } }
136+
heading: {type: "string"},
137+
links: {type: "array", items: {type: "string"}}
136138
}
137139
}
138140
)
139-
puts "Extracted: #{extract_response.data.result}"
141+
puts("Extracted: #{extract_response.data.result}")
140142

141143
execute_response = client.sessions.execute(
142144
session_id,
@@ -146,22 +148,23 @@ def fetch_page_target_id(port, page_url)
146148
max_steps: 3
147149
},
148150
agent_config: {
149-
model: Stagehand::ModelConfig::ModelConfigObject.new(
151+
model: Stagehand::ModelConfig.new(
150152
model_name: "openai/gpt-5-nano",
151153
api_key: model_key
152154
),
153155
cua: false
154156
}
155157
)
156-
puts "Agent completed: #{execute_response.data.result[:message]}"
157-
puts "Agent success: #{execute_response.data.result[:success]}"
158+
puts("Agent completed: #{execute_response.data.result[:message]}")
159+
puts("Agent success: #{execute_response.data.result[:success]}")
158160

159161
page.wait_for_load_state(state: "domcontentloaded")
160162
page.screenshot(path: "screenshot_local_playwright.png", fullPage: true)
161-
puts "Screenshot saved to: screenshot_local_playwright.png"
163+
puts("Screenshot saved to: screenshot_local_playwright.png")
162164
ensure
163165
client.sessions.end_(session_id) if session_id
164166
client.close
165167
browser.close
166168
end
167169
end
170+
# rubocop:enable Metrics/BlockLength

examples/watir_local_example.rb

100644100755
Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env ruby
2+
# typed: ignore
23
# frozen_string_literal: true
34

45
require "bundler/setup"
@@ -21,10 +22,10 @@
2122
# bundle exec ruby examples/watir_local_example.rb
2223

2324
begin
24-
require "watir"
25+
require("watir")
2526
rescue LoadError
26-
warn "Watir is not installed. Run: gem install watir"
27-
exit 1
27+
warn("Watir is not installed. Run: gem install watir")
28+
exit(1)
2829
end
2930

3031
DEBUG_PORT = 9222
@@ -65,7 +66,7 @@ def fetch_cdp_websocket_url(port)
6566
browser.goto("https://example.com")
6667

6768
ws_url = fetch_cdp_websocket_url(DEBUG_PORT)
68-
puts "Browser WebSocket URL: #{ws_url}"
69+
puts("Browser WebSocket URL: #{ws_url}")
6970

7071
start_response = client.sessions.start(
7172
model_name: "openai/gpt-5-nano",
@@ -77,36 +78,36 @@ def fetch_cdp_websocket_url(port)
7778
}
7879
)
7980
session_id = start_response.data.session_id
80-
puts "Session started: #{session_id}"
81+
puts("Session started: #{session_id}")
8182

8283
client.sessions.navigate(session_id, url: "https://example.com")
8384

8485
observe_response = client.sessions.observe(
8586
session_id,
8687
instruction: "Find all clickable links on this page"
8788
)
88-
puts "Found #{observe_response.data.result.length} possible actions"
89+
puts("Found #{observe_response.data.result.length} possible actions")
8990

9091
action = observe_response.data.result.first
9192
act_input = action ? action.to_h.merge(method: "click") : "Click the 'Learn more' link"
9293
act_response = client.sessions.act(
9394
session_id,
9495
input: act_input
9596
)
96-
puts "Act completed: #{act_response.data.result[:message]}"
97+
puts("Act completed: #{act_response.data.result[:message]}")
9798

9899
extract_response = client.sessions.extract(
99100
session_id,
100101
instruction: "Extract the main heading and any links on this page",
101102
schema: {
102103
type: "object",
103104
properties: {
104-
heading: { type: "string" },
105-
links: { type: "array", items: { type: "string" } }
105+
heading: {type: "string"},
106+
links: {type: "array", items: {type: "string"}}
106107
}
107108
}
108109
)
109-
puts "Extracted: #{extract_response.data.result}"
110+
puts("Extracted: #{extract_response.data.result}")
110111

111112
execute_response = client.sessions.execute(
112113
session_id,
@@ -115,18 +116,18 @@ def fetch_cdp_websocket_url(port)
115116
max_steps: 3
116117
},
117118
agent_config: {
118-
model: Stagehand::ModelConfig::ModelConfigObject.new(
119+
model: Stagehand::ModelConfig.new(
119120
model_name: "openai/gpt-5-nano",
120121
api_key: model_key
121122
),
122123
cua: false
123124
}
124125
)
125-
puts "Agent completed: #{execute_response.data.result[:message]}"
126-
puts "Agent success: #{execute_response.data.result[:success]}"
126+
puts("Agent completed: #{execute_response.data.result[:message]}")
127+
puts("Agent success: #{execute_response.data.result[:success]}")
127128

128129
browser.screenshot.save("screenshot_local_watir.png")
129-
puts "Screenshot saved to: screenshot_local_watir.png"
130+
puts("Screenshot saved to: screenshot_local_watir.png")
130131
ensure
131132
client.sessions.end_(session_id) if session_id
132133
client.close

lib/stagehand/client.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,20 @@ def initialize(
9292
initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY,
9393
max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY
9494
)
95+
@server = server
9596
base_url ||= "https://api.stagehand.browserbase.com"
9697

9798
if browserbase_api_key.nil?
98-
raise ArgumentError.new("browserbase_api_key is required, and can be set via environ: \"BROWSERBASE_API_KEY\"")
99+
raise ArgumentError,
100+
"browserbase_api_key is required, and can be set via environ: \"BROWSERBASE_API_KEY\""
99101
end
100102
if browserbase_project_id.nil?
101-
raise ArgumentError.new("browserbase_project_id is required, and can be set via environ: \"BROWSERBASE_PROJECT_ID\"")
103+
raise ArgumentError,
104+
"browserbase_project_id is required, and can be set via environ: \"BROWSERBASE_PROJECT_ID\""
102105
end
103106
if model_api_key.nil?
104-
raise ArgumentError.new("model_api_key is required, and can be set via environ: \"MODEL_API_KEY\"")
107+
raise ArgumentError,
108+
"model_api_key is required, and can be set via environ: \"MODEL_API_KEY\""
105109
end
106110

107111
@browserbase_api_key = browserbase_api_key.to_s

0 commit comments

Comments
 (0)