Class: Stagehand::Client

Inherits:
Internal::Transport::BaseClient show all
Includes:
Local::ClientPatch
Defined in:
lib/stagehand/client.rb

Constant Summary collapse

DEFAULT_MAX_RETRIES =

Default max number of retries to attempt after a failed retryable request.

2
DEFAULT_TIMEOUT_IN_SECONDS =

Default per-request timeout.

60.0
DEFAULT_INITIAL_RETRY_DELAY =

Default initial retry delay in seconds. Overall delay is calculated using exponential backoff + jitter.

0.5
DEFAULT_MAX_RETRY_DELAY =

Default max retry delay in seconds.

8.0

Constants inherited from Internal::Transport::BaseClient

Internal::Transport::BaseClient::MAX_REDIRECTS, Internal::Transport::BaseClient::PLATFORM_HEADERS

Instance Attribute Summary collapse

Attributes inherited from Internal::Transport::BaseClient

#base_url, #headers, #idempotency_header, #initial_retry_delay, #max_retries, #max_retry_delay, #requester, #timeout

Instance Method Summary collapse

Methods included from Local::ClientPatch

#close, #request

Methods inherited from Internal::Transport::BaseClient

follow_redirect, #inspect, reap_connection!, #request, #send_request, should_retry?, validate!

Methods included from Internal::Util::SorbetRuntimeSupport

#const_missing, #define_sorbet_constant!, #sorbet_constant_defined?, #to_sorbet_type, to_sorbet_type

Constructor Details

#initialize(browserbase_api_key: ENV["BROWSERBASE_API_KEY"], browserbase_project_id: nil, model_api_key: ENV["MODEL_API_KEY"], server: "remote", base_url: ENV["STAGEHAND_API_URL"] || ENV["STAGEHAND_BASE_URL"], max_retries: self.class::DEFAULT_MAX_RETRIES, timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS, initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY, max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY) ⇒ Client

Creates and returns a new client for interacting with the API.

ENV`

this value is accepted for backwards compatibility and ignored.

Defaults to ‘ENV`

‘“api.example.com/v2/”`. Defaults to `ENV`, then `ENV`

Parameters:

  • browserbase_api_key (String, nil) (defaults to: ENV["BROWSERBASE_API_KEY"])

    Your [Browserbase API Key](www.browserbase.com/settings) Defaults to

  • browserbase_project_id (String, nil) (defaults to: nil)

    Deprecated. Browserbase API keys are now project-scoped, so

  • model_api_key (String, nil) (defaults to: ENV["MODEL_API_KEY"])

    Your LLM provider API key (e.g. OPENAI_API_KEY, ANTHROPIC_API_KEY, etc.)

  • server (String) (defaults to: "remote")

    Server mode to use (“remote” or “local”). Defaults to “remote”

  • base_url (String, nil) (defaults to: ENV["STAGEHAND_API_URL"] || ENV["STAGEHAND_BASE_URL"])

    Override the default base URL for the API, e.g.,

  • max_retries (Integer) (defaults to: self.class::DEFAULT_MAX_RETRIES)

    Max number of retries to attempt after a failed retryable request.

  • timeout (Float) (defaults to: self.class::DEFAULT_TIMEOUT_IN_SECONDS)
  • initial_retry_delay (Float) (defaults to: self.class::DEFAULT_INITIAL_RETRY_DELAY)
  • max_retry_delay (Float) (defaults to: self.class::DEFAULT_MAX_RETRY_DELAY)


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
# File 'lib/stagehand/client.rb', line 86

def initialize(
  browserbase_api_key: ENV["BROWSERBASE_API_KEY"],
  browserbase_project_id: nil,
  model_api_key: ENV["MODEL_API_KEY"],
  server: "remote",
  base_url: ENV["STAGEHAND_API_URL"] || ENV["STAGEHAND_BASE_URL"],
  max_retries: self.class::DEFAULT_MAX_RETRIES,
  timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS,
  initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY,
  max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY
)
  @server = server
  base_url ||= "https://api.stagehand.browserbase.com"

  if browserbase_api_key.nil?
    raise ArgumentError,
          "browserbase_api_key is required, and can be set via environ: \"BROWSERBASE_API_KEY\""
  end
  if model_api_key.nil?
    raise ArgumentError,
          "model_api_key is required, and can be set via environ: \"MODEL_API_KEY\""
  end

  headers = {}
  custom_headers_env = ENV["STAGEHAND_CUSTOM_HEADERS"]
  unless custom_headers_env.nil?
    parsed = {}
    custom_headers_env.split("\n").each do |line|
      colon = line.index(":")
      unless colon.nil?
        parsed[line[0...colon].strip] = line[(colon + 1)..].strip
      end
    end
    headers = parsed.merge(headers)
  end

  @browserbase_api_key = browserbase_api_key.to_s
  @browserbase_project_id = browserbase_project_id.to_s
  @model_api_key = model_api_key.to_s

  super(
    base_url: base_url,
    timeout: timeout,
    max_retries: max_retries,
    initial_retry_delay: initial_retry_delay,
    max_retry_delay: max_retry_delay,
    headers: headers
  )

  @sessions = Stagehand::Resources::Sessions.new(client: self)
end

Instance Attribute Details

#browserbase_api_keyString (readonly)

Your [Browserbase API Key](www.browserbase.com/settings)

Returns:

  • (String)


20
21
22
# File 'lib/stagehand/client.rb', line 20

def browserbase_api_key
  @browserbase_api_key
end

#browserbase_project_idString (readonly)

Deprecated. Browserbase API keys are now project-scoped, so this value is accepted for backwards compatibility and ignored.

Returns:

  • (String)


25
26
27
# File 'lib/stagehand/client.rb', line 25

def browserbase_project_id
  @browserbase_project_id
end

#model_api_keyString (readonly)

Your LLM provider API key (e.g. OPENAI_API_KEY, ANTHROPIC_API_KEY, etc.)

Returns:

  • (String)


29
30
31
# File 'lib/stagehand/client.rb', line 29

def model_api_key
  @model_api_key
end

#sessionsStagehand::Resources::Sessions (readonly)



32
33
34
# File 'lib/stagehand/client.rb', line 32

def sessions
  @sessions
end