# stagehand-plasmate Plasmate SOM integration for Stagehand. **10-100x token compression** for browser automation. [Stagehand](https://github.com/browserbase/stagehand) is a TypeScript SDK for browser automation with three core primitives: `act()`, `extract()`, and `observe()`. [Plasmate](https://plasmate.app) provides 10-100x token compression via its Semantic Object Model (SOM). This integration lets Stagehand use Plasmate's SOM instead of raw DOM, dramatically reducing token costs for LLM-powered browser automation. ## Installation ```bash npm install stagehand-plasmate @anthropic-ai/stagehand ``` You also need Plasmate installed: ```bash # macOS / Linux curl -fsSL https://plasmate.app/install.sh | sh # Or with cargo cargo install plasmate ``` ## Quick Start ```typescript import { Stagehand } from '@anthropic-ai/stagehand' import { PlasmatePage } from 'stagehand-plasmate' const stagehand = new Stagehand() await stagehand.init() // Wrap Stagehand's page with PlasmatePage const page = new PlasmatePage(stagehand.page) await page.goto('https://example.com') // Get SOM instead of raw DOM (10-100x smaller) const som = await page.toSOM() console.log(`Compression: ${som.meta.html_bytes / som.meta.som_bytes}x`) // Extract with token-efficient context const { context, stats } = await page.extract({ instruction: 'Extract the main heading and navigation links', }) console.log(`Saved ~${Math.round((1 - 1/stats.ratio) * 100)}% tokens`) // act() and observe() pass through to Stagehand await page.act('Click the login button') ``` ## Why Plasmate + Stagehand? | Metric | Raw DOM | With Plasmate | |--------|---------|---------------| | Token usage | ~50,000 | ~3,000 | | Cost per page | $0.15 | $0.01 | | Context window | Exceeds limits | Fits easily | Plasmate converts HTML to a **Semantic Object Model (SOM)** - a structured JSON format that preserves semantic meaning while stripping formatting noise. ### Before: Raw DOM (50KB+) ```html
``` ### After: Plasmate SOM (3KB) ```json { "regions": [{ "role": "navigation", "elements": [{ "id": "e1", "role": "link", "text": "Acme Inc", "attrs": { "href": "/" }, "actions": ["click"] }] }] } ``` ## API Reference ### PlasmatePage Wraps a Stagehand page with SOM-based methods. ```typescript const page = new PlasmatePage(stagehand.page, { binary: 'plasmate', // Path to plasmate binary enableCache: true, // Cache SOM results (default: true) cacheTTL: 30000, // Cache TTL in ms (default: 30s) // Or use Plasmate Cloud: apiEndpoint: 'https://api.plasmate.app', apiKey: 'your-api-key', }) ``` #### Methods | Method | Description | |--------|-------------| | `goto(url)` | Navigate to URL (clears cache) | | `toSOM()` | Get page as Plasmate SOM | | `getCompressionStats()` | Get compression statistics | | `extract(options)` | Extract structured context | | `getLinks()` | Get all links from SOM | | `getInteractiveElements()` | Get buttons, inputs, etc. | | `getText()` | Get page text content | | `findById(id)` | Find element by SOM ID | | `findByRole(role)` | Find elements by role | | `findByText(text)` | Find elements containing text | | `act(action)` | Pass-through to Stagehand act() | | `observe(instruction)` | Pass-through to Stagehand observe() | | `clearCache()` | Clear SOM cache | ### PlasmateClient Low-level client for Plasmate CLI or Cloud API. ```typescript import { PlasmateClient } from 'stagehand-plasmate' const client = new PlasmateClient() // Fetch URL directly const som = await client.fetchSOM('https://example.com') // Process HTML const som = await client.processHTML(html, 'https://example.com') ``` ### Extraction Helpers ```typescript import { getLinks, getInteractiveElements, extractAllText, findElementById, findElementsByRole, findElementsByText, } from 'stagehand-plasmate' const som = await page.toSOM() // Get all links const links = getLinks(som) // [{ id: "e1", text: "Home", href: "/" }, ...] // Get interactive elements const interactive = getInteractiveElements(som) // [{ id: "e5", role: "button", label: "Submit", actions: ["click"] }, ...] // Find elements const buttons = findElementsByRole(som, 'button') const loginButton = findElementsByText(som, 'Login')[0] ``` ## SOM Types ```typescript interface SOM { som_version: string url: string title: string lang: string regions: SOMRegion[] meta: SOMMeta structured_data?: StructuredData } interface SOMRegion { id: string role: 'navigation' | 'main' | 'aside' | 'header' | 'footer' | 'form' | 'dialog' | 'content' label?: string elements: SOMElement[] } interface SOMElement { id: string role: 'link' | 'button' | 'text_input' | 'textarea' | 'select' | 'checkbox' | 'radio' | 'heading' | 'image' | 'list' | 'table' | 'paragraph' | 'section' | 'separator' | 'details' text?: string label?: string actions?: string[] attrs?: Record