Initialize Stagehand with your preferred configuration:
import { Stagehand } from "@browserbasehq/stagehand";// For local developmentconst stagehand = new Stagehand({ env: "LOCAL", verbose: 2, enableCaching: false,});// For production with Browserbaseconst stagehand = new Stagehand({ env: "BROWSERBASE", verbose: 1, enableCaching: true,});
Integrate with LangGraph for complex automation workflows:
import { createReactAgent } from "@langchain/langgraph/prebuilt";// Create an LLMconst llm = new ChatOpenAI({ model: "gpt-4", temperature: 0,});// Create an agent with Stagehand toolsconst agent = createReactAgent({ llm, tools: stagehandToolkit.tools,});// Execute a complex workflowconst result = await agent.invoke({ messages: [ { role: "user", content: "Go to example.com, find the contact form, and extract all the form fields" } ]});
const extractionAgent = createReactAgent({ llm, tools: stagehandToolkit.tools,});const result = await extractionAgent.invoke({ messages: [{ role: "user", content: ` Go to news-website.com and extract: 1. All article headlines 2. Publication dates 3. Author names Format as structured JSON ` }]});
const formAgent = createReactAgent({ llm, tools: stagehandToolkit.tools,});const result = await formAgent.invoke({ messages: [{ role: "user", content: ` Navigate to contact-form.com and: 1. Fill out the contact form with: - Name: John Doe - Email: john@example.com - Message: Inquiry about services 2. Submit the form 3. Confirm submission success ` }]});
const researchAgent = createReactAgent({ llm, tools: stagehandToolkit.tools,});const result = await researchAgent.invoke({ messages: [{ role: "user", content: ` Research product pricing by: 1. Visit competitor1.com and extract pricing info 2. Visit competitor2.com and extract pricing info 3. Compare features and prices 4. Provide summary analysis ` }]});
LangChain JS Documentation
Official LangChain JS documentation for the Stagehand integration