-
Notifications
You must be signed in to change notification settings - Fork 13
Stagehand migration v2 -> v3 #6
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
base: main
Are you sure you want to change the base?
Conversation
|
@AnSingh1 is attempting to deploy a commit to the browserbase Team on Vercel. A member of the Team first needs to authorize it. |
Kylejeong2
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
left some comments about use of "any" typing & some other code quality things. after fixing I'll deploy a preview to vercel to test
app/api/chat/route.ts
Outdated
| console.log("Connecting to existing Browserbase session:", sessionId); | ||
| } | ||
|
|
||
| console.log("Stagehand config:", { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove log we don't need
app/api/chat/route.ts
Outdated
| apiKey: process.env.BROWSERBASE_API_KEY, | ||
| projectId: process.env.BROWSERBASE_PROJECT_ID, | ||
| domSettleTimeout: 30000, | ||
| model: "anthropic/claude-3-5-sonnet-20241022", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think this model is deprecated can upgrade to 4.5 sonnet
app/api/chat/route.ts
Outdated
| stagehand = new Stagehand(StagehandConfig); | ||
| const stagehandConfig: any = { | ||
| env: | ||
| process.env.BROWSERBASE_API_KEY && process.env.BROWSERBASE_PROJECT_ID |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
set to "BROWSERBASE" always, this is not meant to run locally
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ignore the original impl. being like that
app/api/chat/route.ts
Outdated
|
|
||
| console.log("Initializing Stagehand instance..."); | ||
| await stagehand.init(); | ||
| console.log("Available pages in context:", stagehand.context.pages().length); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dont need this log
app/api/chat/route.ts
Outdated
|
|
||
| if (pages.length === 0) { | ||
| throw new Error("No pages available in context after init"); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we only need a single page object so instead of this code we only need one line
const page = stagehand.context.pages()[0];
app/api/chat/route.ts
Outdated
| const page = await stagehand.page; | ||
|
|
||
| // Wait a bit for the session to be fully ready | ||
| await new Promise(resolve => setTimeout(resolve, 2000)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we shouldn't need this either, was it not working previously with no extra timeout?
app/api/chat/route.ts
Outdated
| templateInfo = await page.act({ | ||
| action: `Look at the meme templates on the page. Find a template that would work well with the message "${message}". Click on "Add Caption" for the template you think is the best match.`, | ||
| }); | ||
| console.log("About to call stagehand.act()..."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can remove log
app/api/chat/route.ts
Outdated
|
|
||
| console.log("Template name:", templateInfo.name); | ||
| templateInfo = extractedData; | ||
| console.log("Template name:", (templateInfo as any).name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should not use any here, we can use object destructuring above here to get { name } from the stagehand extract call and then log just the name instead
app/api/chat/route.ts
Outdated
| } catch (error) { | ||
| console.log(`Error finding template in ${source.description}:`, error); | ||
| console.error(`Error finding template in ${source.description}:`, error); | ||
| console.error("Full error stack:", (error as any).stack); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above (don't use any)
| import type { NextConfig } from "next"; | ||
|
|
||
| const nextConfig: NextConfig = { | ||
| serverExternalPackages: [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a reason we need to do this ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removing breaks Stagehand + Pino in App Router routes
Next.js bundles thread-stream/pino (Stagehand dependencies), which corrupts worker paths
leads to error code: ERR_WORKER_PATH
more: https://nextjs.org/docs/app/api-reference/next-config-js/serverExternalPackages
This pull request refactors the
app/api/chat/route.tsAPI handler to stagehand v3 (originally stagehand v2) for meme generation to improve reliability, error handling, and integration with theStagehandautomation library. It also updates dependencies and configuration to support these changes. The main improvements include switching to the latest@browserbasehq/stagehandpackage, restructuring the session and page management logic, and enhancing error reporting for easier debugging.Also updates from depreciated sonnet3.5 -> sonnet4.5
API handler improvements:
Stagehandwith a new configuration object, connects to an existing Browserbase session if available, and manages pages directly from thestagehand.context.pages()array for better reliability.page.actandpage.extractcalls withstagehand.actandstagehand.extractmethods, aligning with the updated Stagehand API and ensuring consistent automation steps.stagehand.close()is called reliably in thefinallyblock.Dependency and configuration updates:
@browserbasehq/stagehandto version^3.0.7and@browserbasehq/sdkto^2.6.0inpackage.jsonto support new API features and stability improvements.serverExternalPackagestonext.config.tsto ensure required packages are bundled for server-side execution.Note:
Debugging connection was closed. Reason: WebSocket disconnected. Reconnect when ready by reopening DevTools error STILL persists.