import { Stagehand } from "@browserbasehq/stagehand";
import { chromium } from "playwright-core";
import { z } from "zod/v3";
// Initialize Stagehand
const stagehand = new Stagehand({
env: "BROWSERBASE",
model: "openai/gpt-5",
});
await stagehand.init();
// Connect Playwright
const browser = await chromium.connectOverCDP({
wsEndpoint: stagehand.connectURL(),
});
const pwContext = browser.contexts()[0];
const pwPage1 = pwContext.pages()[0];
// Create a second page
const pwPage2 = await pwContext.newPage();
// Navigate both pages
await pwPage1.goto("https://docs.stagehand.dev/first-steps/introduction");
await pwPage2.goto("https://docs.stagehand.dev/configuration/observability");
// Extract data from both pages concurrently
const [page1Data, page2Data] = await Promise.all([
stagehand.extract(
"extract the names of the four stagehand primitives",
z.array(z.string()),
{ page: pwPage1 }
),
stagehand.extract(
"extract the list of session dashboard features",
z.array(z.string()),
{ page: pwPage2 }
),
]);
console.log("Page 1 primitives:", page1Data);
console.log("Page 2 features:", page2Data);