|
| 1 | +# Stagehand & Mastra Integration |
| 2 | + |
| 3 | +A powerful integration that combines the capabilities of [Browserbase's Stagehand](https:/stagehand.dev) with [Mastra](https://mastra.ai/) for advanced web automation, scraping, and AI-powered web interactions. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This project enables AI agents to interact with web pages through the Mastra framework using Stagehand's browser automation capabilities. It provides tools for web navigation, element observation, data extraction, and action execution, all orchestrated through Mastra's agent system. |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- **Web Navigation**: Navigate to websites programmatically |
| 12 | +- **Element Observation**: Identify and locate elements on web pages |
| 13 | +- **Action Execution**: Perform actions like clicking buttons or filling forms |
| 14 | +- **Data Extraction**: Extract structured data from web pages |
| 15 | +- **Session Management**: Smart session handling with automatic timeouts and reconnection |
| 16 | +- **AI-Powered Interactions**: Leverage OpenAI models for intelligent web interactions |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +### Prerequisites |
| 21 | + |
| 22 | +- Node.js (v18+) |
| 23 | +- npm or yarn |
| 24 | +- Browserbase account |
| 25 | +- OpenAI API access |
| 26 | + |
| 27 | +### Setup |
| 28 | + |
| 29 | +1. Clone the repository: |
| 30 | + ``` |
| 31 | + git clone https://github.com/browserbase/Stagehand-Mastra-App.git |
| 32 | + cd Stagehand-Mastra-App |
| 33 | + ``` |
| 34 | + |
| 35 | +2. Install dependencies: |
| 36 | + ``` |
| 37 | + npm install |
| 38 | + ``` |
| 39 | + |
| 40 | +3. Create a `.env` file with your API keys: |
| 41 | + ``` |
| 42 | + BROWSERBASE_PROJECT_ID=your_project_id |
| 43 | + BROWSERBASE_API_KEY=your_api_key |
| 44 | + OPENAI_API_KEY=your_openai_key |
| 45 | + ``` |
| 46 | + |
| 47 | +## Usage |
| 48 | + |
| 49 | +### Running the development server |
| 50 | + |
| 51 | +``` |
| 52 | +npm run dev |
| 53 | +``` |
| 54 | + |
| 55 | +This will start the Mastra development server, giving you access to the integrated web agent. |
| 56 | + |
| 57 | +## Architecture |
| 58 | + |
| 59 | +### Core Components |
| 60 | + |
| 61 | +1. **Stagehand Session Manager** |
| 62 | + - Handles browser session initialization and management |
| 63 | + - Implements automatic session timeouts |
| 64 | + - Provides error recovery and reconnection logic |
| 65 | + |
| 66 | +2. **Mastra Tools** |
| 67 | + - `stagehandActTool`: Performs actions on web pages |
| 68 | + - `stagehandObserveTool`: Identifies elements on web pages |
| 69 | + - `stagehandExtractTool`: Extracts data from web pages |
| 70 | + |
| 71 | +3. **Web Agent** |
| 72 | + - AI-powered agent using OpenAI's GPT-4 |
| 73 | + - Provides natural language interface to web automation |
| 74 | + - Integrates all tools into a unified experience |
| 75 | + |
| 76 | +### Flow Diagram |
| 77 | + |
| 78 | +``` |
| 79 | +User Query → Mastra Agent → Stagehand Tools → Browser Interaction → Web Page → Data/Results → Agent Response |
| 80 | +``` |
| 81 | + |
| 82 | +## Configuration |
| 83 | + |
| 84 | +The project can be configured through the `.env` file and by modifying the agent instructions in `src/mastra/agents/index.ts`. |
| 85 | + |
| 86 | +## Credits |
| 87 | + |
| 88 | +This project is built with: |
| 89 | +- [Mastra](https://mastra.ai) - AI Agent framework |
| 90 | +- [Stagehand by Browserbase](https:/stagehand.dev) - Browser automation |
| 91 | +- [OpenAI](https://openai.com/) - AI models |
0 commit comments