Skip to content

Commit 67215a5

Browse files
committed
Merge branch 'main' into laminar-example
2 parents d1d3ccc + 98e1356 commit 67215a5

File tree

302 files changed

+28970
-18474
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

302 files changed

+28970
-18474
lines changed

.changeset/all-dots-shop.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.changeset/blue-beds-thank.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@browserbasehq/stagehand-lib": minor
3+
---
4+
5+
make extract() with no arguments return the hybrid tree instead of text-rendered webpage

.changeset/brave-eggs-double.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@browserbasehq/stagehand-lib": patch
3+
"@browserbasehq/stagehand": patch
4+
---
5+
6+
use safeparse for zod validation

.changeset/bright-beans-smile.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@browserbasehq/stagehand-lib": patch
3+
---
4+
5+
dont log deprecation warning when onlyVisible is undefined

.changeset/clean-cups-join.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.changeset/cool-lemons-report.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.changeset/curly-rules-build.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.changeset/deep-apes-hug.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@browserbasehq/stagehand-lib": patch
3+
---
4+
5+
optimize CDP calls when building hybrid tree

.changeset/empty-spoons-float.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.changeset/fifty-crabs-arrive.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.changeset/four-hoops-mix.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.changeset/free-pots-move.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.changeset/full-trams-learn.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.changeset/metal-clowns-cheat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@browserbasehq/stagehand-lib": patch
3+
---
4+
5+
fix `stagehand.history`

.changeset/metal-pets-try.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@browserbasehq/stagehand-lib": patch
3+
---
4+
5+
Add an identifier for client language/runtime

.changeset/petite-donuts-lead.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.changeset/plenty-ties-float.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.changeset/puny-garlics-join.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.changeset/quiet-rooms-relate.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@browserbasehq/stagehand-lib": minor
3+
"@browserbasehq/stagehand": minor
4+
---
5+
6+
deprecate useTextExtract and remove functionality

.changeset/rare-tires-turn.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.changeset/rude-ideas-double.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@browserbasehq/stagehand-lib": patch
3+
---
4+
5+
rm unused functions

.changeset/shiny-windows-attack.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.changeset/six-lies-lie.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.changeset/tender-rats-cheat.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.changeset/twenty-cooks-perform.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.changeset/wise-worlds-pull.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.changeset/young-dots-fry.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.cursorrules

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# Stagehand Project
2+
3+
This is a project that uses Stagehand, which amplifies Playwright with `act`, `extract`, and `observe` added to the Page class.
4+
5+
`Stagehand` is a class that provides config, a `StagehandPage` object via `stagehand.page`, and a `StagehandContext` object via `stagehand.context`.
6+
7+
`Page` is a class that extends the Playwright `Page` class and adds `act`, `extract`, and `observe` methods.
8+
`Context` is a class that extends the Playwright `BrowserContext` class.
9+
10+
Use the following rules to write code for this project.
11+
12+
- To take an action on the page like "click the sign in button", use Stagehand `act` like this:
13+
14+
```typescript
15+
await page.act("Click the sign in button");
16+
```
17+
18+
- To plan an instruction before taking an action, use Stagehand `observe` to get the action to execute.
19+
20+
```typescript
21+
const [action] = await page.observe("Click the sign in button");
22+
```
23+
24+
- The result of `observe` is an array of `ObserveResult` objects that can directly be used as params for `act` like this:
25+
26+
```typescript
27+
const [action] = await page.observe("Click the sign in button");
28+
await page.act(action);
29+
```
30+
31+
- When writing code that needs to extract data from the page, use Stagehand `extract`. Explicitly pass the following params by default:
32+
33+
```typescript
34+
const { someValue } = await page.extract({
35+
instruction: the instruction to execute,
36+
schema: z.object({
37+
someValue: z.string(),
38+
}), // The schema to extract
39+
});
40+
```
41+
42+
## Initialize
43+
44+
```typescript
45+
import { Stagehand } from "@browserbasehq/stagehand";
46+
import StagehandConfig from "./stagehand.config";
47+
48+
const stagehand = new Stagehand(StagehandConfig);
49+
await stagehand.init();
50+
51+
const page = stagehand.page; // Playwright Page with act, extract, and observe methods
52+
const context = stagehand.context; // Playwright BrowserContext
53+
```
54+
55+
## Act
56+
57+
You can cache the results of `observe` and use them as params for `act` like this:
58+
59+
```typescript
60+
const instruction = "Click the sign in button";
61+
const cachedAction = await getCache(instruction);
62+
63+
if (cachedAction) {
64+
await page.act(cachedAction);
65+
} else {
66+
try {
67+
const results = await page.observe(instruction);
68+
await setCache(instruction, results);
69+
await page.act(results[0]);
70+
} catch (error) {
71+
await page.act(instruction); // If the action is not cached, execute the instruction directly
72+
}
73+
}
74+
```
75+
76+
Be sure to cache the results of `observe` and use them as params for `act` to avoid unexpected DOM changes. Using `act` without caching will result in more unpredictable behavior.
77+
78+
Act `action` should be as atomic and specific as possible, i.e. "Click the sign in button" or "Type 'hello' into the search input".
79+
AVOID actions that are more than one step, i.e. "Order me pizza" or "Type in the search bar and hit enter".
80+
81+
## Extract
82+
83+
If you are writing code that needs to extract data from the page, use Stagehand `extract`.
84+
85+
```typescript
86+
const signInButtonText = await page.extract("extract the sign in button text");
87+
```
88+
89+
You can also pass in params like an output schema in Zod, and a flag to use text extraction:
90+
91+
```typescript
92+
const data = await page.extract({
93+
instruction: "extract the sign in button text",
94+
schema: z.object({
95+
text: z.string(),
96+
}),
97+
});
98+
```
99+
100+
`schema` is a Zod schema that describes the data you want to extract. To extract an array, make sure to pass in a single object that contains the array, as follows:
101+
102+
```typescript
103+
const data = await page.extract({
104+
instruction: "extract the text inside all buttons",
105+
schema: z.object({
106+
text: z.array(z.string()),
107+
}),
108+
});
109+
```
110+
111+
## Agent
112+
113+
Use the `agent` method to automonously execute larger tasks like "Get the stock price of NVDA"
114+
115+
```typescript
116+
// Navigate to a website
117+
await stagehand.page.goto("https://www.google.com");
118+
119+
const agent = stagehand.agent({
120+
// You can use either OpenAI or Anthropic
121+
provider: "openai",
122+
// The model to use (claude-3-7-sonnet-20250219 or claude-3-5-sonnet-20240620 for Anthropic)
123+
model: "computer-use-preview",
124+
125+
// Customize the system prompt
126+
instructions: `You are a helpful assistant that can use a web browser.
127+
Do not ask follow up questions, the user will trust your judgement.`,
128+
129+
// Customize the API key
130+
options: {
131+
apiKey: process.env.OPENAI_API_KEY,
132+
},
133+
});
134+
135+
// Execute the agent
136+
await agent.execute(
137+
"Apply for a library card at the San Francisco Public Library"
138+
);
139+
```

0 commit comments

Comments
 (0)