Skip to content

Commit 8050402

Browse files
committed
[STG-1808] Prefer STAGEHAND_API_URL env fallback
1 parent 06eeea6 commit 8050402

4 files changed

Lines changed: 62 additions & 8 deletions

File tree

examples/env.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ def self.load!
2323
end
2424

2525
missing = REQUIRED_KEYS.select { |key| ENV[key].to_s.empty? }
26-
unless missing.empty?
27-
raise "Missing required env vars: #{missing.join(', ')} (from examples/.env)"
28-
end
26+
return if missing.empty?
2927

30-
ENV["STAGEHAND_BASE_URL"] ||= ENV["STAGEHAND_API_URL"]
28+
raise "Missing required env vars: #{missing.join(', ')} (from examples/.env)"
3129
end
3230

3331
def self.find_env_path

lib/stagehand/client.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class Client < Stagehand::Internal::Transport::BaseClient
7272
# @param server [String] Server mode to use ("remote" or "local"). Defaults to "remote"
7373
#
7474
# @param base_url [String, nil] Override the default base URL for the API, e.g.,
75-
# `"https://api.example.com/v2/"`. Defaults to `ENV["STAGEHAND_BASE_URL"]`
75+
# `"https://api.example.com/v2/"`. Defaults to `ENV["STAGEHAND_API_URL"]`
7676
#
7777
# @param max_retries [Integer] Max number of retries to attempt after a failed retryable request.
7878
#
@@ -86,7 +86,7 @@ def initialize(
8686
browserbase_project_id: ENV["BROWSERBASE_PROJECT_ID"],
8787
model_api_key: ENV["MODEL_API_KEY"],
8888
server: "remote",
89-
base_url: ENV["STAGEHAND_BASE_URL"],
89+
base_url: ENV["STAGEHAND_API_URL"] || ENV["STAGEHAND_BASE_URL"],
9090
max_retries: self.class::DEFAULT_MAX_RETRIES,
9191
timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS,
9292
initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY,

rbi/stagehand/client.rbi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ module Stagehand
7272
# Server mode to use ("remote" or "local"). Defaults to "remote"
7373
server: "remote",
7474
# Override the default base URL for the API, e.g.,
75-
# `"https://api.example.com/v2/"`. Defaults to `ENV["STAGEHAND_BASE_URL"]`
76-
base_url: ENV["STAGEHAND_BASE_URL"],
75+
# `"https://api.example.com/v2/"`. Defaults to `ENV["STAGEHAND_API_URL"]`
76+
base_url: ENV["STAGEHAND_API_URL"] || ENV["STAGEHAND_BASE_URL"],
7777
# Max number of retries to attempt after a failed retryable request.
7878
max_retries: Stagehand::Client::DEFAULT_MAX_RETRIES,
7979
timeout: Stagehand::Client::DEFAULT_TIMEOUT_IN_SECONDS,

test/stagehand/client_test.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,62 @@ def test_raises_on_missing_non_nullable_opts
3434
assert_match(/is required/, e.message)
3535
end
3636

37+
def test_client_base_url_from_stagehand_api_url
38+
with_env("STAGEHAND_API_URL" => "http://localhost:5000/from/api/env", "STAGEHAND_BASE_URL" => nil) do
39+
stagehand =
40+
Stagehand::Client.new(
41+
browserbase_api_key: "My Browserbase API Key",
42+
browserbase_project_id: "My Browserbase Project ID",
43+
model_api_key: "My Model API Key"
44+
)
45+
46+
assert_equal("http://localhost:5000/from/api/env", stagehand.base_url.to_s)
47+
end
48+
end
49+
50+
def test_client_base_url_from_legacy_stagehand_base_url
51+
with_env("STAGEHAND_API_URL" => nil, "STAGEHAND_BASE_URL" => "http://localhost:5000/from/base/env") do
52+
stagehand =
53+
Stagehand::Client.new(
54+
browserbase_api_key: "My Browserbase API Key",
55+
browserbase_project_id: "My Browserbase Project ID",
56+
model_api_key: "My Model API Key"
57+
)
58+
59+
assert_equal("http://localhost:5000/from/base/env", stagehand.base_url.to_s)
60+
end
61+
end
62+
63+
def test_client_base_url_prefers_stagehand_api_url
64+
with_env(
65+
"STAGEHAND_API_URL" => "http://localhost:5000/from/api/env",
66+
"STAGEHAND_BASE_URL" => "http://localhost:5000/from/base/env"
67+
) do
68+
stagehand =
69+
Stagehand::Client.new(
70+
browserbase_api_key: "My Browserbase API Key",
71+
browserbase_project_id: "My Browserbase Project ID",
72+
model_api_key: "My Model API Key"
73+
)
74+
75+
assert_equal("http://localhost:5000/from/api/env", stagehand.base_url.to_s)
76+
end
77+
end
78+
79+
def with_env(vars)
80+
old_values = vars.keys.to_h { |key| [key, ENV.fetch(key, nil)] }
81+
82+
vars.each do |key, value|
83+
value.nil? ? ENV.delete(key) : ENV[key] = value
84+
end
85+
86+
yield
87+
ensure
88+
old_values.each do |key, value|
89+
value.nil? ? ENV.delete(key) : ENV[key] = value
90+
end
91+
end
92+
3793
def test_client_default_request_default_retry_attempts
3894
stub_request(:post, "http://localhost/v1/sessions/start").to_return_json(status: 500, body: {})
3995

0 commit comments

Comments
 (0)