-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Create framework #25324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Create framework #25324
Changes from 10 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
c18c335
Create framework
kathayl 1f7daf1
Initial skeleton
ToriLindsay 6b00289
Delete src/content/docs/browser-rendering/platform/framework
ToriLindsay e1ecee5
Update stagehand.mdx
kathayl e176d50
docs: update Stagehand documentation with AI Gateway and custom model…
kathayl 0c49271
Update stagehand.mdx
kathayl 3471115
Update stagehand.mdx
kathayl ff3174e
Update Stagehand example with simplified code (#25375)
ruifigueira 0853068
Update stagehand.mdx
kathayl 2102ad0
Fix stagehand example code (#25392)
ruifigueira d57fda7
Add example image
ToriLindsay 7a58086
Update src/content/docs/browser-rendering/platform/stagehand.mdx
kathayl c62c6c5
Update src/content/docs/browser-rendering/platform/stagehand.mdx
kathayl 182a8a8
Update src/content/docs/browser-rendering/platform/stagehand.mdx
kathayl 46a0c60
Update src/content/docs/browser-rendering/platform/stagehand.mdx
kathayl d87ee57
Update src/content/docs/browser-rendering/platform/stagehand.mdx
kathayl 8594ed3
Update src/content/docs/browser-rendering/platform/stagehand.mdx
kathayl d505875
Update src/content/docs/browser-rendering/platform/stagehand.mdx
kathayl fe19053
Update src/content/docs/browser-rendering/platform/stagehand.mdx
kathayl 8076c90
Update src/content/docs/browser-rendering/platform/stagehand.mdx
kathayl 8b454ed
Update src/content/docs/browser-rendering/platform/stagehand.mdx
kathayl 04add45
Update src/content/docs/browser-rendering/platform/stagehand.mdx
kathayl 3878b40
Update src/content/docs/browser-rendering/platform/stagehand.mdx
kathayl 3a40045
Update stagehand.mdx
kathayl 9ed937a
Update stagehand.mdx
kathayl 683a458
Update stagehand.mdx
kathayl f17b96b
Update stagehand.mdx
kathayl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
223 changes: 223 additions & 0 deletions
223
src/content/docs/browser-rendering/platform/stagehand.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,223 @@ | ||
| --- | ||
| pcx_content_type: concept | ||
| title: Stagehand | ||
| description: Deploy a Stagehand server that uses Browser Rendering to provide browser automation capabilities to your agents. | ||
| sidebar: | ||
| order: 7 | ||
| badge: Beta | ||
| --- | ||
|
|
||
| import { | ||
| Render, | ||
| WranglerConfig, | ||
| TabItem, | ||
| Tabs, | ||
| PackageManagers, | ||
| } from "~/components"; | ||
|
|
||
| [Stagehand](https://www.stagehand.dev/) is an open-source, AI-powered browser automation library. Rather than dictating exact steps or specifying selectors, Stagehand enables developers to build more reliably and flexibly by combining code with natural-language instructions powered by AI. This makes your agents more resilient to website changes and easier to maintain. | ||
|
|
||
| This guide shows you how to deploy a Worker that uses Stagehand, Browser Rendering, and [Workers AI](/workers-ai/) to automate a web task. | ||
kathayl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Use Stagehand in a Worker with Workers AI | ||
|
|
||
| In this example, you will use Stagehand to search for a movie on [The Movie Database](https://www.themoviedb.org/), extract its details (title, year, and director), and return the information along with a screenshot of the webpage. | ||
kathayl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| If you want to skip the steps and get started quickly, select **Deploy to Cloudflare** below. | ||
kathayl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| [](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/playwright/tree/main/packages/playwright-cloudflare/examples/stagehand) | ||
|
|
||
| After you deploy, you can interact with the Worker using this URL pattern: | ||
| ``` | ||
| https://<your-worker>.workers.dev | ||
| ``` | ||
|
|
||
| #### 1. Set up your project | ||
kathayl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Install the necessary dependencies: | ||
kathayl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ```bash | ||
| npm ci | ||
| ``` | ||
|
|
||
| #### 2. Configure your Worker | ||
kathayl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Update your wrangler configuration file to include the bindings for Browser Rendering and [Workers AI](/workers-ai/): | ||
| <WranglerConfig> | ||
| ```jsonc | ||
| { | ||
| "name": "stagehand-example", | ||
| "main": "src/index.ts", | ||
| "compatibility_flags": ["nodejs_compat"], | ||
| "compatibility_date": "2025-09-17", | ||
| "observability": { | ||
| "enabled": true | ||
| }, | ||
| "browser": { | ||
| "binding": "BROWSER" | ||
| }, | ||
| "ai": { | ||
| "binding": "AI" | ||
| } | ||
| } | ||
| ``` | ||
| </WranglerConfig> | ||
|
|
||
| If you are using [Cloudflare Vite plugin](https://developers.cloudflare.com/workers/vite-plugin/), you need to include the following [alias](https://vite.dev/config/shared-options.html#resolve-alias) in `vite.config.ts`: | ||
kathayl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| ```ts | ||
| export default defineConfig({ | ||
| // ... | ||
| resolve: { | ||
| alias: { | ||
| 'playwright': '@cloudflare/playwright', | ||
| }, | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| If you're not using the Cloudflare Vite plugin, you need to include the following [module alias](https://developers.cloudflare.com/workers/wrangler/configuration/#module-aliasing) to wrangler configuration: | ||
kathayl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```jsonc | ||
| { | ||
| // ... | ||
| "alias": { | ||
| "playwright": "@cloudflare/playwright" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| #### 3. Write the Worker code | ||
kathayl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Copy [workersAIClient.ts](https://github.com/cloudflare/playwright/blob/main/packages/playwright-cloudflare/examples/stagehand/src/worker/workersAIClient.ts) to your project. | ||
kathayl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Then, in your worker code, import the `workersAIClient.ts` file and use it to configure a new `Stagehand` instance: | ||
kathayl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```ts title="src/index.ts" | ||
|
|
||
| import { Stagehand } from "@browserbasehq/stagehand"; | ||
| import { z } from "zod"; | ||
| import { endpointURLString } from "@cloudflare/playwright"; | ||
| import { WorkersAIClient } from "./workersAIClient"; | ||
|
|
||
| export default { | ||
| async fetch(request: Request, env: Env) { | ||
| if (new URL(request.url).pathname !== "/") | ||
| return new Response("Not found", { status: 404 }); | ||
|
|
||
| const stagehand = new Stagehand({ | ||
| env: "LOCAL", | ||
| localBrowserLaunchOptions: { cdpUrl: endpointURLString(env.BROWSER) }, | ||
| llmClient: new new WorkersAIClient(env.AI), | ||
| verbose: 1, | ||
| }); | ||
|
|
||
| await stagehand.init(); | ||
| const page = stagehand.page; | ||
|
|
||
| await page.goto('https://demo.playwright.dev/movies'); | ||
|
|
||
| // if search is a multi-step action, stagehand will return an array of actions it needs to act on | ||
| const actions = await page.observe('Search for "Furiosa"'); | ||
| for (const action of actions) | ||
| await page.act(action); | ||
|
|
||
| await page.act('Click the search result'); | ||
|
|
||
| // normal playwright functions work as expected | ||
| await page.waitForSelector('.info-wrapper .cast'); | ||
|
|
||
| let movieInfo = await page.extract({ | ||
| instruction: 'Extract movie information', | ||
| schema: z.object({ | ||
| title: z.string(), | ||
| year: z.number(), | ||
| rating: z.number(), | ||
| genres: z.array(z.string()), | ||
| duration: z.number().describe("Duration in minutes"), | ||
| }), | ||
| }); | ||
|
|
||
| await stagehand.close(); | ||
|
|
||
| return Response.json(movieInfo); | ||
| }, | ||
| }; | ||
| ``` | ||
|
|
||
| #### 4. Build the project | ||
kathayl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```bash | ||
| npm run build | ||
| ``` | ||
| #### 5. Deploy to Cloudflare Workers | ||
kathayl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| After you deploy, you can interact with the Worker using this URL pattern: | ||
| ``` | ||
| https://<your-worker>.workers.dev | ||
| ``` | ||
|
|
||
| ```bash | ||
| npm run deploy | ||
| ``` | ||
|
|
||
| ## Use Cloudflare AI Gateway with Workers AI | ||
|
|
||
| [AI Gateway](/ai-gateway/) is a service that adds observability to your AI applications. By routing your requests through AI Gateway, you can monitor and debug your AI applications. | ||
|
|
||
| To use AI Gateway, first create a gateway in the [Cloudflare dashboard](https://dash.cloudflare.com/?to=/:account/workers/ai-gateway). In this example, we've named the gateway `stagehand-example-gateway`. | ||
|
|
||
| ```typescript | ||
| const stagehand = new Stagehand({ | ||
| env: "LOCAL", | ||
| localBrowserLaunchOptions: { cdpUrl }, | ||
| llmClient: new WorkersAIClient(env.AI, { | ||
| gateway: { | ||
| id: "stagehand-example-gateway" | ||
| } | ||
| }), | ||
| }); | ||
| ``` | ||
|
|
||
| ## Use a third-party model | ||
|
|
||
| If you'd like to use a model outside of Workers AI, you can configure Stagehand to use models from supported [third-party providers](https://docs.stagehand.dev/configuration/models#supported-providers), including OpenAI and Anthropic, by providing your own credentials. | ||
kathayl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| In this example, you will configure Stagehand to use [OpenAI](https://openai.com/). You will need an OpenAI API key. Cloudflare recommends storing your API key as a [secret](/workers/configuration/secrets/). | ||
|
|
||
| ```bash | ||
| npx wrangler secret put OPENAI_API_KEY | ||
| ``` | ||
|
|
||
| Then, configure Stagehand with your provider, model, and API key. | ||
|
|
||
| ```typescript | ||
| const stagehand = new Stagehand({ | ||
| env: "LOCAL", | ||
| localBrowserLaunchOptions: { cdpUrl: endpointURLString(env.BROWSER) }, | ||
| modelName: "openai/gpt-4.1", | ||
| modelClientOptions: { | ||
| apiKey: env.OPENAI_API_KEY, | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| ## Use Cloudflare AI Gateway with a third-party model | ||
|
|
||
| [AI Gateway](/ai-gateway/) is a service that adds observability to your AI applications. By routing your requests through AI Gateway, you can monitor and debug your AI applications. | ||
|
|
||
| To use AI Gateway, first create a gateway in the [Cloudflare dashboard](https://dash.cloudflare.com/?to=/:account/workers/ai-gateway). In this example, we are using [OpenAI with AI Gateway](https://developers.cloudflare.com/ai-gateway/usage/providers/openai/). Make sure to add the `baseURL` as shown below, with your own Account ID and Gateway ID. | ||
kathayl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```typescript | ||
| const stagehand = new Stagehand({ | ||
| env: "LOCAL", | ||
| localBrowserLaunchOptions: { cdpUrl: endpointURLString(env.BROWSER) }, | ||
| modelName: "openai/gpt-4.1", | ||
| modelClientOptions: { | ||
| baseURL: `https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai`, | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| ## Stagehand API | ||
| For the full list of Stagehand methods and capabilities, refer to the official [Stagehand API documentation](https://docs.stagehand.dev/first-steps/introduction). | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.