import { StagehandToolkit } from "langchain/community/agents/toolkits/stagehand";import { ChatOpenAI } from "@langchain/openai";import { Stagehand } from "@browserbasehq/stagehand";// Specify your Browserbase credentials.process.env.BROWSERBASE_API_KEY = "";process.env.BROWSERBASE_PROJECT_ID = "";// Specify OpenAI API key.process.env.OPENAI_API_KEY = "";const stagehand = new Stagehand({ env: "LOCAL", headless: false, verbose: 2, debugDom: true, enableCaching: false,});// Create a Stagehand Toolkit with all the available actions from the Stagehand.const stagehandToolkit = await StagehandToolkit.fromStagehand(stagehand);const navigateTool = stagehandToolkit.tools.find( (t) => t.name === "stagehand_navigate");if (!navigateTool) { throw new Error("Navigate tool not found");}await navigateTool.invoke("https://www.google.com");const actionTool = stagehandToolkit.tools.find( (t) => t.name === "stagehand_act");if (!actionTool) { throw new Error("Action tool not found");}await actionTool.invoke('Search for "OpenAI"');const observeTool = stagehandToolkit.tools.find( (t) => t.name === "stagehand_observe");if (!observeTool) { throw new Error("Observe tool not found");}const result = await observeTool.invoke( "What actions can be performed on the current page?");const observations = JSON.parse(result);// Handle observations as neededconsole.log(observations);const currentUrl = stagehand.page.url();expect(currentUrl).toContain("google.com/search?q=OpenAI");
If you want to run the browser remotely, you can use the Browserbase platform.You need to set the BROWSERBASE_API_KEY environment variable to your Browserbase API key.