@@ -28,15 +28,134 @@ gem "stagehand", "~> 0.6.0"
2828require " bundler/setup"
2929require " stagehand"
3030
31- stagehand = Stagehand ::Client .new (
32- browserbase_api_key: ENV [" BROWSERBASE_API_KEY" ], # This is the default and can be omitted
33- browserbase_project_id: ENV [" BROWSERBASE_PROJECT_ID" ], # This is the default and can be omitted
34- model_api_key: ENV [" MODEL_API_KEY" ] # This is the default and can be omitted
31+ # Create a new Stagehand client with your credentials
32+ client = Stagehand ::Client .new (
33+ browserbase_api_key: ENV [" BROWSERBASE_API_KEY" ], # defaults to ENV["BROWSERBASE_API_KEY"]
34+ browserbase_project_id: ENV [" BROWSERBASE_PROJECT_ID" ], # defaults to ENV["BROWSERBASE_PROJECT_ID"]
35+ model_api_key: ENV [" MODEL_API_KEY" ] # defaults to ENV["MODEL_API_KEY"]
36+ )
37+
38+ # Start a new browser session
39+ # x_language and x_sdk_version headers are required for the v3 API
40+ start_response = client.sessions.start(
41+ model_name: " openai/gpt-5-nano" ,
42+ x_language: :typescript ,
43+ x_sdk_version: " 3.0.6"
44+ )
45+ puts " Session started: #{ start_response.data.session_id } "
46+
47+ session_id = start_response.data.session_id
48+
49+ # Navigate to a webpage
50+ # frame_id is required - use empty string for the main frame
51+ client.sessions.navigate(
52+ session_id,
53+ url: " https://news.ycombinator.com" ,
54+ frame_id: " " ,
55+ x_language: :typescript ,
56+ x_sdk_version: " 3.0.6"
57+ )
58+ puts " Navigated to Hacker News"
59+
60+ # Use Observe to find possible actions on the page
61+ observe_response = client.sessions.observe(
62+ session_id,
63+ instruction: " find the link to view comments for the top post" ,
64+ x_language: :typescript ,
65+ x_sdk_version: " 3.0.6"
66+ )
67+
68+ actions = observe_response.data.result
69+ puts " Found #{ actions.length } possible actions"
70+
71+ # Take the first action returned by Observe
72+ action = actions.first
73+ puts " Acting on: #{ action.description } "
74+
75+ # Pass the structured action to Act
76+ # Convert the observe result to a hash and ensure method is set to "click"
77+ act_response = client.sessions.act(
78+ session_id,
79+ input: action.to_h.merge(method: " click" ),
80+ x_language: :typescript ,
81+ x_sdk_version: " 3.0.6"
82+ )
83+ puts " Act completed: #{ act_response.data.result[:message ] } "
84+
85+ # Extract data from the page
86+ # We're now on the comments page, so extract the top comment text
87+ extract_response = client.sessions.extract(
88+ session_id,
89+ instruction: " extract the text of the top comment on this page" ,
90+ schema: {
91+ type: " object" ,
92+ properties: {
93+ comment_text: {
94+ type: " string" ,
95+ description: " The text content of the top comment"
96+ },
97+ author: {
98+ type: " string" ,
99+ description: " The username of the comment author"
100+ }
101+ },
102+ required: [" comment_text" ]
103+ },
104+ x_language: :typescript ,
105+ x_sdk_version: " 3.0.6"
106+ )
107+ puts " Extracted data: #{ extract_response.data.result } "
108+
109+ # Get the author from the extracted data
110+ extracted_data = extract_response.data.result
111+ author = extracted_data[:author ]
112+ puts " Looking up profile for author: #{ author } "
113+
114+ # Use the Agent to find the author's profile
115+ # Execute runs an autonomous agent that can navigate and interact with pages
116+ execute_response = client.sessions.execute(
117+ session_id,
118+ execute_options: {
119+ instruction: " Find any personal website, GitHub, LinkedIn, or other best profile URL for the Hacker News user '#{ author } '. " \
120+ " Click on their username to go to their profile page and look for any links they have shared." ,
121+ max_steps: 15
122+ },
123+ agent_config: {
124+ model: Stagehand ::ModelConfig ::ModelConfigObject .new (
125+ model_name: " openai/gpt-5-nano" ,
126+ api_key: ENV [" MODEL_API_KEY" ]
127+ ),
128+ cua: false
129+ },
130+ x_language: :typescript ,
131+ x_sdk_version: " 3.0.6"
35132)
133+ puts " Agent completed: #{ execute_response.data.result[:message ] } "
134+ puts " Agent success: #{ execute_response.data.result[:success ] } "
135+ puts " Agent actions taken: #{ execute_response.data.result[:actions ]&.length || 0 } "
136+
137+ # End the session to cleanup browser resources
138+ client.sessions.end_(
139+ session_id,
140+ x_language: :typescript ,
141+ x_sdk_version: " 3.0.6"
142+ )
143+ puts " Session ended"
144+ ```
145+
146+ ### Running the Example
147+
148+ Set the required environment variables and run the example script:
36149
37- response = stagehand.sessions.act(" 00000000-your-session-id-000000000000" , input: " click the first link on the page" )
150+ ``` bash
151+ # Set your credentials
152+ export BROWSERBASE_API_KEY=" your-browserbase-api-key"
153+ export BROWSERBASE_PROJECT_ID=" your-browserbase-project-id"
154+ export MODEL_API_KEY=" your-openai-api-key"
38155
39- puts (response.data)
156+ # Install dependencies and run
157+ bundle install
158+ bundle exec ruby examples/basic.rb
40159```
41160
42161### Streaming
0 commit comments