Update stagehand template to v4 - #221
Open
dprevoznik wants to merge 2 commits into
Open
Conversation
Stagehand v4 runs as a Chrome extension next to the browser rather than
driving it purely over CDP, so the template now preloads that extension
into the Kernel browser and connects to it.
Changes to pkg/templates/typescript/stagehand:
- Bump @browserbasehq/stagehand to ^4.0.0; add ws (+ @types/ws).
- Replace `new Stagehand({ env: "LOCAL", localBrowserLaunchOptions })` +
`init()` with `localBrowser.connect({ cdpUrl, extensionId })` +
`Stagehand.create({ browser })`.
- Upload the Stagehand extension (shipped in the npm package) to the
project, create the browser with `extensions: [{ name }]`, and discover
the extension's runtime id from its service worker over CDP.
- Use `await browser.context.activePage()` (page access is async in v4)
and unwrap `stagehand.extract(...)` via `{ data }`.
- Close the connected browser explicitly (v4 only closes browsers it
launched).
- Read MODEL / MODEL_API_KEY from env, defaulting to openai/gpt-4.1.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 2 potential issues.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit ca622fd. Configure here.
- discoverExtensionId: settle on an overall timeout, socket close, and errors, and guard JSON parsing, so a run can no longer hang past the timeout without reaching cleanup. - Move browser teardown (stagehand.close, browser.close, deleteByID) into a finally block so a failed run no longer leaks the Kernel browser, matching the sibling templates. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

Migrates
pkg/templates/typescript/stagehandfrom Stagehand v3 to v4.Why
Stagehand v4 runs as a Chrome extension next to the browser instead of driving it purely over CDP. The v3 init path (
new Stagehand({ env: "LOCAL", localBrowserLaunchOptions: { cdpUrl } })+init()) no longer exists. For a remote Kernel browser,localBrowser.connect({ cdpUrl })alone does not work: with noextensionIdit falls back toExtensions.loadUnpacked, whose path is resolved on the browser's filesystem, not the app's. The extension must be preloaded into the Kernel browser and referenced by its runtime id.What changed
package.json:@browserbasehq/stagehand^3.5.0→^4.0.0; addwsand@types/ws. (zod^4.2.0already satisfies Stagehand v4'szod@4.4.3.)index.ts:dist/assets/stagehand-extension.zipinside the package) to the project once viakernel.extensions.upload, reused on later runs.extensions: [{ name }].Target.getTargets).localBrowser.connect({ cdpUrl, extensionId })andStagehand.create({ browser }).await browser.context.activePage()(page access is async in v4).stagehand.extract(...)via{ data }(v4 primitives return{ data, metadata }).MODEL/MODEL_API_KEYfrom env, defaulting toopenai/gpt-4.1..env.example,README.md: documentMODEL/MODEL_API_KEYand the extension-based connection flow.pnpm-lock.yaml: regenerated.Testing
tsc --noEmitpasses with the template's resolved deps (stagehand 4.0.0, @onkernel/sdk 0.23.0, zod 4.4.3, ws 8.21.3).kernel deployand rankernel invoke ts-stagehand teamsize-task --payload '{"company":"kernel"}'; it returned{"teamSize":"6"}. Deploy was run withMODEL=google/gemini-2.5-flashsince that was the model key available in the test environment; the default OpenAI path is unchanged in code.🤖 Generated with Claude Code
Note
Low Risk
Sample template and documentation only; no production service paths. Deploy env var rename may affect users still using OPENAI_API_KEY-only docs.
Overview
Migrates
pkg/templates/typescript/stagehandfrom Stagehand v3 → v4, which no longer uses CDP-onlyinit()and instead runs as a Chrome extension on the browser.The sample now uploads
stagehand-extension.zipto the Kernel project once, creates browsers with that extension preloaded, discovers the runtime extension id via CDPTarget.getTargets, and connects withlocalBrowser.connect({ cdpUrl, extensionId })plusStagehand.create({ browser }). Automation uses asyncactivePage(), unwrapsextractresults from{ data }, andfinallycloses the connected browser and deletes the Kernel session.Env/docs switch from
OPENAI_API_KEYtoMODEL/MODEL_API_KEY(withOPENAI_API_KEYfallback).@browserbasehq/stagehandis bumped to ^4.0.0;wsis added for extension discovery; lockfile is regenerated.Reviewed by Cursor Bugbot for commit 0c321ef. Bugbot is set up for automated code reviews on this repo. Configure here.