Skip to content

Conversation

@AnSingh1
Copy link

@AnSingh1 AnSingh1 commented Jan 9, 2026

This pull request refactors the app/api/chat/route.ts API handler to stagehand v3 (originally stagehand v2) for meme generation to improve reliability, error handling, and integration with the Stagehand automation library. It also updates dependencies and configuration to support these changes. The main improvements include switching to the latest @browserbasehq/stagehand package, 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:

  • Refactored session and page management: The handler now initializes Stagehand with a new configuration object, connects to an existing Browserbase session if available, and manages pages directly from the stagehand.context.pages() array for better reliability.
  • Replaced direct page.act and page.extract calls with stagehand.act and stagehand.extract methods, aligning with the updated Stagehand API and ensuring consistent automation steps.
  • Improved error handling: Added detailed error logging, including stack traces and serialized error details, to make debugging failures much easier. Error responses now include the error message for client visibility.
  • Cleaned up resource management: Removed redundant page closing logic and ensured stagehand.close() is called reliably in the finally block.

Dependency and configuration updates:

  • Updated @browserbasehq/stagehand to version ^3.0.7 and @browserbasehq/sdk to ^2.6.0 in package.json to support new API features and stability improvements.
  • Added serverExternalPackages to next.config.ts to 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.

@vercel
Copy link
Contributor

vercel bot commented Jan 9, 2026

@AnSingh1 is attempting to deploy a commit to the browserbase Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Member

@Kylejeong2 Kylejeong2 left a 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

console.log("Connecting to existing Browserbase session:", sessionId);
}

console.log("Stagehand config:", {
Copy link
Member

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

apiKey: process.env.BROWSERBASE_API_KEY,
projectId: process.env.BROWSERBASE_PROJECT_ID,
domSettleTimeout: 30000,
model: "anthropic/claude-3-5-sonnet-20241022",
Copy link
Member

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

stagehand = new Stagehand(StagehandConfig);
const stagehandConfig: any = {
env:
process.env.BROWSERBASE_API_KEY && process.env.BROWSERBASE_PROJECT_ID
Copy link
Member

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

Copy link
Member

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


console.log("Initializing Stagehand instance...");
await stagehand.init();
console.log("Available pages in context:", stagehand.context.pages().length);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont need this log


if (pages.length === 0) {
throw new Error("No pages available in context after init");
}
Copy link
Member

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];

const page = await stagehand.page;

// Wait a bit for the session to be fully ready
await new Promise(resolve => setTimeout(resolve, 2000));
Copy link
Member

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?

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()...");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can remove log


console.log("Template name:", templateInfo.name);
templateInfo = extractedData;
console.log("Template name:", (templateInfo as any).name);
Copy link
Member

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

} 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);
Copy link
Member

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: [
Copy link
Member

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 ?

Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants