@browserbasehq/convex-stagehand

中文文档
0.1.1发布于 2026/05/21MITTypeScriptES Module构建来源已验证近 30 天下载 9604

Convex component for AI-powered browser automation with Stagehand

原始使用文档
文档目录27 节
  1. Features
  2. Quick Start
  3. 1. Install the Component
  4. 2. Configure Convex
  5. 3. Set Up Environment Variables
  6. 4. Use the Component
  7. API Reference
  8. startSession(ctx, args)
  9. endSession(ctx, args)
  10. extract(ctx, args)
  11. act(ctx, args)
  12. observe(ctx, args)
  13. agent(ctx, args)
  14. Examples
  15. Simple extraction (automatic session)
  16. Manual session management
  17. Autonomous agent
  18. Resume session across Convex actions
  19. Configuration Options
  20. AI Model
  21. Requirements
  22. How It Works
  23. Development
  24. Component Structure
  25. Building the Component
  26. Example App
  27. License

convex-stagehand

AI-powered browser automation for Convex applications. Extract data, perform actions, and automate workflows using natural language - no Playwright knowledge required.

Features

  • Simple API - Describe what you want in plain English
  • Type-safe - Full TypeScript support with Zod schemas
  • Session management - Reuse browser sessions across multiple operations
  • Agent mode - Autonomous multi-step task execution
  • Powered by Stagehand - Uses the Stagehand REST API

Quick Start

1. Install the Component

npm install @browserbasehq/convex-stagehand zod

2. Configure Convex

Add the component to your convex/convex.config.ts:

import { defineApp } from "convex/server";
import stagehand from "convex-stagehand/convex.config";

const app = defineApp();
app.use(stagehand, { name: "stagehand" });

export default app;

3. Set Up Environment Variables

Add these to your Convex Dashboard → Settings → Environment Variables:

VariableDescription
BROWSERBASE_API_KEYYour Browserbase API key
BROWSERBASE_PROJECT_IDYour Browserbase project ID
MODEL_API_KEYYour LLM provider API key (OpenAI, Anthropic, etc.)

4. Use the Component

import { action } from "./_generated/server";
import { Stagehand } from "convex-stagehand";
import { components } from "./_generated/api";
import { z } from "zod";

const stagehand = new Stagehand(components.stagehand, {
  browserbaseApiKey: process.env.BROWSERBASE_API_KEY!,
  browserbaseProjectId: process.env.BROWSERBASE_PROJECT_ID!,
  modelApiKey: process.env.MODEL_API_KEY!,
});

export const scrapeHackerNews = action({
  handler: async (ctx) => {
    return await stagehand.extract(ctx, {
      url: "https://news.ycombinator.com",
      instruction: "Extract the top 5 stories with title, score, and link",
      schema: z.object({
        stories: z.array(z.object({
          title: z.string(),
          score: z.string(),
          link: z.string(),
        }))
      })
    });
  }
});

API Reference

startSession(ctx, args)

Start a new browser session. Returns session info for use with other operations.

const session = await stagehand.startSession(ctx, {
  url: "https://example.com",
  browserbaseSessionID: "optional-existing-session-id",
  options: {
    timeout: 30000,
    waitUntil: "networkidle",
    domSettleTimeoutMs: 2000,
    selfHeal: true,
    systemPrompt: "Custom system prompt for the session",
  }
});
// { sessionId: "...", cdpUrl: "wss://..." }

Parameters:

  • url - The URL to navigate to
  • browserbaseSessionID - Optional: Resume an existing Browserbase session
  • options.timeout - Navigation timeout in milliseconds
  • options.waitUntil - When to consider navigation complete: "load", "domcontentloaded", or "networkidle"
  • options.domSettleTimeoutMs - Timeout for DOM to settle before considering page loaded
  • options.selfHeal - Enable self-healing capabilities for more robust automation
  • options.systemPrompt - Custom system prompt to guide the AI's behavior during the session

Returns:

{
  sessionId: string; // Use with other operations
  cdpUrl?: string;   // For advanced Playwright/Puppeteer usage
}

endSession(ctx, args)

End a browser session.

await stagehand.endSession(ctx, { sessionId: session.sessionId });

Parameters:

  • sessionId - The session to end

Returns: { success: boolean }


extract(ctx, args)

Extract structured data from a web page using AI.

// Without session (creates and destroys its own)
const data = await stagehand.extract(ctx, {
  url: "https://example.com",
  instruction: "Extract all product names and prices",
  schema: z.object({
    products: z.array(z.object({
      name: z.string(),
      price: z.string(),
    }))
  }),
});

// With existing session (reuses session, doesn't end it)
const data = await stagehand.extract(ctx, {
  sessionId: session.sessionId,
  instruction: "Extract all product names and prices",
  schema: z.object({ ... }),
});

Parameters:

  • sessionId - Optional: Use an existing session
  • url - The URL to navigate to (required if no sessionId)
  • instruction - Natural language description of what to extract
  • schema - Zod schema defining the expected output structure
  • options.timeout - Navigation timeout in milliseconds
  • options.waitUntil - When to consider navigation complete: "load", "domcontentloaded", or "networkidle"

Returns: Data matching your Zod schema


act(ctx, args)

Execute browser actions using natural language.

// Without session
const result = await stagehand.act(ctx, {
  url: "https://example.com/login",
  action: "Click the login button and wait for the page to load",
});

// With existing session
const result = await stagehand.act(ctx, {
  sessionId: session.sessionId,
  action: "Fill in the email field with '[email protected]'",
});

Parameters:

  • sessionId - Optional: Use an existing session
  • url - The URL to navigate to (required if no sessionId)
  • action - Natural language description of the action to perform
  • options.timeout - Navigation timeout in milliseconds
  • options.waitUntil - When to consider navigation complete

Returns:

{
  success: boolean;
  message: string;
  actionDescription: string;
}

observe(ctx, args)

Find available actions on a web page.

const actions = await stagehand.observe(ctx, {
  url: "https://example.com",
  instruction: "Find all clickable navigation links",
});
// [{ description: "Home link", selector: "a.nav-home", method: "click" }, ...]

Parameters:

  • sessionId - Optional: Use an existing session
  • url - The URL to navigate to (required if no sessionId)
  • instruction - Natural language description of what actions to find
  • options.timeout - Navigation timeout in milliseconds
  • options.waitUntil - When to consider navigation complete

Returns:

Array<{
  description: string;
  selector: string;
  method: string;
  arguments?: string[];
}>

agent(ctx, args)

Execute autonomous multi-step browser automation using an AI agent. The agent interprets the instruction and decides what actions to take.

// Agent creates its own session
const result = await stagehand.agent(ctx, {
  url: "https://google.com",
  instruction: "Search for 'convex database' and extract the top 3 results with title and URL",
  options: { maxSteps: 10 },
});

// Agent with existing session
const result = await stagehand.agent(ctx, {
  sessionId: session.sessionId,
  instruction: "Fill out the contact form and submit",
  options: { maxSteps: 5 },
});

Parameters:

  • sessionId - Optional: Use an existing session
  • url - The URL to navigate to (required if no sessionId)
  • instruction - Natural language description of the task to complete
  • options.cua - Enable Computer Use Agent mode
  • options.maxSteps - Maximum steps the agent can take
  • options.systemPrompt - Custom system prompt for the agent
  • options.timeout - Navigation timeout in milliseconds
  • options.waitUntil - When to consider navigation complete

Returns:

{
  actions: Array<{
    type: string;
    action?: string;
    reasoning?: string;
    timeMs?: number;
  }>;
  completed: boolean;
  message: string;
  success: boolean;
}

Examples

Simple extraction (automatic session)

const news = await stagehand.extract(ctx, {
  url: "https://news.ycombinator.com",
  instruction: "Get the top 10 stories with title, points, and comment count",
  schema: z.object({
    stories: z.array(z.object({
      title: z.string(),
      points: z.string(),
      comments: z.string(),
    }))
  })
});

Manual session management

Use session management when you need to perform multiple operations while preserving browser state (cookies, login, etc.):

// Start a session
const session = await stagehand.startSession(ctx, {
  url: "https://google.com"
});

// Perform multiple operations in the same session
await stagehand.act(ctx, {
  sessionId: session.sessionId,
  action: "Search for 'convex database'"
});

const data = await stagehand.extract(ctx, {
  sessionId: session.sessionId,
  instruction: "Extract the top 3 results",
  schema: z.object({
    results: z.array(z.object({
      title: z.string(),
      url: z.string(),
    }))
  })
});

// End the session when done
await stagehand.endSession(ctx, { sessionId: session.sessionId });

Autonomous agent

Let the AI agent figure out how to complete a complex task:

const result = await stagehand.agent(ctx, {
  url: "https://www.google.com",
  instruction: "Search for 'best pizza in NYC', click on the first result, and extract the restaurant name and address",
  options: { maxSteps: 10 }
});

console.log(result.message); // Summary of what the agent did
console.log(result.actions); // Detailed log of each action taken

Resume session across Convex actions

Store the sessionId to continue working with the same Stagehand session across different Convex action calls:

// Action 1: Start session and return sessionId
export const startBrowsing = action({
  handler: async (ctx) => {
    const session = await stagehand.startSession(ctx, {
      url: "https://example.com/login"
    });
    // Store sessionId in your database
    return session.sessionId;
  }
});

// Action 2: Continue same session later
export const continueBrowsing = action({
  args: { sessionId: v.string() },
  handler: async (ctx, args) => {
    await stagehand.act(ctx, {
      sessionId: args.sessionId,
      action: "Navigate to the dashboard page",
    });

    return await stagehand.extract(ctx, {
      sessionId: args.sessionId,
      instruction: "Extract user data",
      schema: z.object({ ... }),
    });
  }
});

Configuration Options

AI Model

By default, the component uses openai/gpt-4o. You can use any model supported by the Vercel AI SDK that supports structured outputs:

const stagehand = new Stagehand(components.stagehand, {
  browserbaseApiKey: process.env.BROWSERBASE_API_KEY!,
  browserbaseProjectId: process.env.BROWSERBASE_PROJECT_ID!,
  modelApiKey: process.env.ANTHROPIC_API_KEY!, // Use Anthropic
  modelName: "anthropic/claude-3-5-sonnet-20241022",
});

For the full list of supported models and providers, see the Stagehand Models documentation.

Requirements

How It Works

This component uses the Stagehand REST API to power browser automation. Each operation:

  1. Starts a cloud browser session via Browserbase (or reuses an existing one)
  2. Navigates to the target URL
  3. Uses AI to understand the page and perform the requested operation
  4. Optionally ends the session and returns results

With session management, you control when sessions start and end, allowing you to maintain browser state across multiple operations.

Development

Component Structure

The component exposes its API through Convex's component system. All functions are in a single lib.ts module:

component.lib.<function>

For example:

  • component.lib.startSession - Start a browser session
  • component.lib.endSession - End a browser session
  • component.lib.extract - Extract data from web pages
  • component.lib.act - Perform browser actions
  • component.lib.observe - Find interactive elements
  • component.lib.agent - Autonomous multi-step automation

The Stagehand client class wraps these internal paths to provide a clean user API:

// User calls:
stagehand.extract(ctx, {...})

// Internally calls:
ctx.runAction(component.lib.extract, {...})

Building the Component

To build the component locally:

# Install dependencies
npm install

# Build with Convex codegen (generates component API)
npm run build:codegen

# Or just build TypeScript
npm run build:esm

The component requires a Convex deployment to generate proper component API types (_generated/component.ts).

Example App

Check out the full example app in the example/ directory:

git clone https://github.com/browserbase/integrations
cd integrations/packages/convex-stagehand/example
npm install
npm run dev

The example includes:

  • HackerNews story extraction with AI
  • Type-safe data extraction using Zod schemas
  • Database persistence with Convex
  • Real-time updates and automatic refresh

License

MIT

版本与发布记录

@browserbasehq/convex-stagehand 共发布 5 个版本,首次发布于 2026/01/27;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.1.1latest2026/05/215 个月前
0.1.02026/03/068 个月前
0.0.32026/02/148 个月前
0.0.22026/02/049 个月前
0.0.12026/01/279 个月前

关于 @browserbasehq/convex-stagehand 的常见问题

@browserbasehq/convex-stagehand 是什么?

Convex component for AI-powered browser automation with Stagehand

如何安装 @browserbasehq/convex-stagehand?

在项目目录运行 npm install @browserbasehq/convex-stagehand,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:zod(^3.23.0)、convex(^1.29.3)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@browserbasehq/convex-stagehand 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@browserbasehq/convex-stagehand 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@browserbasehq/convex-stagehand 可以商用吗?

@browserbasehq/convex-stagehand 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@browserbasehq/convex-stagehand 的下载量有多少?

根据 npm 官方下载统计,@browserbasehq/convex-stagehand 在 2026/10/09 – 2026/11/07 这 30 天内被下载 9,604 次,日均约 320 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@browserbasehq/convex-stagehand 还在维护吗?

最新版本 0.1.1 发布于 2026/05/21(5 个月前);至今共发布 5 个版本,其中过去 12 个月发布了 5 个;首次发布于 2026/01/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。