MastraBrowser 類別
MastraBrowser 類別是瀏覽器自動化 Provider 的抽象基底類別。其共用 interface 涵蓋瀏覽器啟動與 thread 隔離,以及 screencast 串流與輸入 event。
請勿直接建立 MastraBrowser instance,改用 Provider 實作:
AgentBrowser:使用 ref 的確定性瀏覽器自動化StagehandBrowser:使用自然語言的 AI 驅動瀏覽器自動化BrowserViewer:透過 CDP URL injection 操作的 CLI 型瀏覽器自動化
使用範例「使用範例」的直接連結
import { Agent } from '@mastra/core/agent'
import { AgentBrowser } from '@mastra/agent-browser'
const browser = new AgentBrowser({
headless: true,
viewport: { width: 1280, height: 720 },
scope: 'thread',
})
export const browserAgent = new Agent({
id: 'browser-agent',
name: 'Browser Agent',
instructions: 'You can browse the web to find information.',
model: 'openai/gpt-5.6-sol',
browser,
})
Constructor 參數「Constructor 參數」的直接連結
headless?:
viewport?:
timeout?:
cdpUrl?:
scope?:
onLaunch?:
onClose?:
screencast?:
format?:
quality?:
maxWidth?:
maxHeight?:
everyNthFrame?:
屬性「屬性」的直接連結
以下屬性(id、name、provider)為 abstract,必須由具體 Provider 實作定義:
id:
name:
provider:
headless:
status:
方法「方法」的直接連結
生命週期「生命週期」的直接連結
ensureReady()「ensureready」的直接連結
確保瀏覽器已啟動並可供使用。在 Tool 執行前自動呼叫。實作位於基底類別。
await browser.ensureReady()
close()「close」的直接連結
關閉瀏覽器並清理所有資源。實作位於基底類別,且能安全處理 race condition。
await browser.close()
isBrowserRunning()「isbrowserrunning」的直接連結
檢查瀏覽器目前是否正在執行。
const isRunning = browser.isBrowserRunning()
傳回: boolean
Thread 管理「Thread 管理」的直接連結
setCurrentThread(threadId)「setcurrentthreadthreadid」的直接連結
設定瀏覽器操作目前使用的 thread ID。由 Agent runtime 內部使用。
browser.setCurrentThread('thread-123')
getCurrentThread()「getcurrentthread」的直接連結
取得目前的 thread ID。
const threadId = browser.getCurrentThread()
傳回: string
hasThreadSession(threadId)「hasthreadsessionthreadid」的直接連結
檢查 thread 是否具有有效的瀏覽器 session。
const hasSession = browser.hasThreadSession('thread-123')
傳回: boolean
closeThreadSession(threadId)「closethreadsessionthreadid」的直接連結
關閉特定 thread 的瀏覽器 session。使用 'thread' scope 時,會關閉該 thread 的瀏覽器 instance;使用 'shared' scope 時,會清除 thread state。
await browser.closeThreadSession('thread-123')
Tool「Tool」的直接連結
getTools()「gettools」的直接連結
傳回供 Agent 使用的瀏覽器 Tool。各 Provider 會依模型傳回不同 Tool。
const tools = browser.getTools()
傳回: Record<string, Tool>
Screencast「Screencast」的直接連結
startScreencast(options?, threadId?)「startscreencastoptions-threadid」的直接連結
開始串流瀏覽器 frame。傳回會發出 frame event 的 ScreencastStream。
const stream = await browser.startScreencast({ format: 'jpeg', quality: 80 }, 'thread-123')
stream.on('frame', frame => {
console.log('Frame received:', frame.data.length, 'bytes')
})
stream.on('stop', reason => {
console.log('Screencast stopped:', reason)
})
傳回: Promise<ScreencastStream>
輸入 injection「輸入 injection」的直接連結
injectMouseEvent(params, threadId?)「injectmouseeventparams-threadid」的直接連結
將滑鼠 event 注入瀏覽器。Studio 使用此方法進行即時互動。
await browser.injectMouseEvent({
type: 'mousePressed',
x: 100,
y: 200,
button: 'left',
clickCount: 1,
})
injectKeyboardEvent(params, threadId?)「injectkeyboardeventparams-threadid」的直接連結
將鍵盤 event 注入瀏覽器。Studio 使用此方法進行即時互動。
await browser.injectKeyboardEvent({
type: 'keyDown',
key: 'Enter',
code: 'Enter',
})
State「State」的直接連結
getState(threadId?)「getstatethreadid」的直接連結
取得目前的瀏覽器 state,包括 URL 與分頁。
const state = await browser.getState('thread-123')
console.log('Current URL:', state.currentUrl)
console.log('Tabs:', state.tabs)
傳回: Promise<BrowserState>
interface BrowserState {
currentUrl: string | null
tabs: BrowserTabState[]
activeTabIndex: number
}
interface BrowserTabState {
id: string
url: string
title: string
}
getCurrentUrl(threadId?)「getcurrenturlthreadid」的直接連結
取得目前頁面 URL。
const url = await browser.getCurrentUrl()
傳回: Promise<string | null>
Browser scope「Browser scope」的直接連結
scope 選項控制瀏覽器 instance 在對話 thread 之間的共用方式:
| Scope | 說明 | 使用情境 |
|---|---|---|
'shared' | 所有 thread 共用單一瀏覽器 instance | 適用於不互相衝突的任務,可節省成本 |
'thread' | 每個 thread 都有自己的瀏覽器 instance | 為並行使用者提供完整隔離 |
// Shared browser for all threads
const sharedBrowser = new AgentBrowser({
scope: 'shared',
})
// Isolated browser per thread
const isolatedBrowser = new AgentBrowser({
scope: 'thread',
})
使用 cdpUrl 連線至外部瀏覽器時,scope 會自動改用 'shared',因為無法建立新的瀏覽器 instance。
雲端瀏覽器 Provider「雲端瀏覽器 Provider」的直接連結
使用 cdpUrl 選項連線至雲端瀏覽器服務:
// Static CDP URL
const browser = new AgentBrowser({
cdpUrl: 'wss://browser.example.com/ws',
})
// Dynamic CDP URL (e.g., session-based)
const browser = new AgentBrowser({
cdpUrl: async () => {
const session = await createBrowserSession()
return session.wsUrl
},
})
相關內容「相關內容」的直接連結
- AgentBrowser:確定性瀏覽器自動化
- StagehandBrowser:AI 驅動的瀏覽器自動化
- Browser 概觀:瀏覽器自動化概念指南