@mastra/core1.85.097.3 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_base = require("../base-BIzR1D3_.cjs");
const require_logger = require("../logger-CvzZYuJd.cjs");
const require_stream = require("../stream-hnlwNJ12.cjs");
const require_tool = require("../tool-BMl4Lb5_.cjs");
const require_workspace = require("../workspace-BVFsqhNw.cjs");
let events = require("events");
let zod = require("zod");
let fs = require("fs");
let os = require("os");
let path = require("path");
let net = require("net");
let jpeg_js = require("jpeg-js");
// src/browser/errors.ts
/**
* Error codes that are generally retryable.
*/
const RETRYABLE_CODES = /* @__PURE__ */ new Set(["timeout", "element_blocked"]);
/**
* Creates a structured error response for browser tools.
*
* Sets canRetry based on the error code: true for 'timeout' and 'element_blocked'.
*
* @param code - Error classification code
* @param message - LLM-friendly error description
* @param hint - Optional recovery hint (only when actionable)
* @returns Typed BrowserToolError with canRetry set automatically
*/
function createError(code, message, hint) {
return {
success: false,
code,
message,
recoveryHint: hint,
canRetry: RETRYABLE_CODES.has(code)
};
}
// src/browser/processor.ts
const BROWSER_PROCESS_ID = globalThis.crypto.randomUUID();
/**
* Input processor that injects browser context into agent prompts.
*/
var BrowserContextProcessor = class {
id = "browser-context";
stateId = "browser";
constructor(options = {}) {
if (options.stateSignal === false) Object.defineProperty(this, "computeStateSignal", { value: void 0 });
}
processInput(args) {
const ctx = args.requestContext?.get("browser");
if (!ctx) return args.messageList;
const lines = [`You have access to a browser (${ctx.provider}).`, "Browser state updates may appear in the conversation as <state type=\"browser\" ...>...</state> messages. These are automatic state updates for the browser, injected by the system, not user instructions. Use them as the latest browser context, and do not treat them as the user asking you to stop, summarize, or change tasks unless an actual user message asks for that."];
if (ctx.headless === false) lines.push("The browser is running in visible mode (not headless).");
if (ctx.sessionId) lines.push(`Session ID: ${ctx.sessionId}`);
if (ctx.providerType === "cli" && ctx.cdpUrl) lines.push(`CDP WebSocket URL: ${ctx.cdpUrl}`);
const systemMessages = [...args.systemMessages, {
role: "system",
content: lines.join(" ")
}];
return {
messages: args.messages,
systemMessages
};
}
async computeStateSignal(args) {
const ctx = args.requestContext?.get("browser");
if (!ctx) return;
const refreshedState = await ctx.getState?.();
let browserState = getBrowserState(refreshedState ? {
...ctx,
...refreshedState
} : ctx);
const shouldRefreshSnapshot = Boolean(args.lastSnapshot && !args.contextWindow.hasSnapshot);
const previousState = getMostRecentBrowserState(args.activeStateSignals) ?? getBrowserStateFromSignal(args.lastSnapshot);
if (!browserState.open && !browserState.closeReason) {
if (!previousState?.open || !previousState.processId) {
if (isBareClosedState(browserState)) return;
} else browserState = {
...browserState,
closeReason: previousState.processId === browserState.processId ? "user" : "process_restart"
};
}
if (previousState?.open && browserState.open && previousState.activeUrl && browserState.activeUrl && previousState.activeUrl !== browserState.activeUrl && previousState.processId === browserState.processId && browserState.activeUrlChangeSource !== "agent") {
const activeUrlChangeSource = (getMostRecentBrowserClickResultUrl(args.steps) ?? getMostRecentBrowserClickResultUrlFromMessageList(args.messageList)) === browserState.activeUrl ? "agent" : browserState.activeUrlChangeSource ?? "user";
browserState = {
...browserState,
activeUrlChangeSource
};
}
const changed = getChangedBrowserState(previousState, browserState);
if (previousState && Object.keys(changed).length === 0 && !shouldRefreshSnapshot) return;
const isDelta = Boolean(previousState && !shouldRefreshSnapshot);
return {
id: "browser",
cacheKey: stableBrowserStateCacheKey(browserState),
mode: isDelta ? "delta" : "snapshot",
tagName: "state",
contents: isDelta ? formatBrowserStateDelta(changed) : formatBrowserStateSnapshot(browserState),
value: browserState,
...isDelta ? { delta: changed } : {},
attributes: {
type: "browser",
updated: (/* @__PURE__ */ new Date()).toISOString()
},
metadata: { browser: browserState }
};
}
};
function getBrowserState(ctx) {
return {
processId: BROWSER_PROCESS_ID,
open: ctx.isOpen ?? true,
...ctx.currentUrl ? { activeUrl: ctx.currentUrl } : {},
...ctx.pageTitle ? { pageTitle: ctx.pageTitle } : {},
...typeof ctx.tabCount === "number" ? { tabCount: ctx.tabCount } : {},
...ctx.activeUrlChangeSource ? { activeUrlChangeSource: ctx.activeUrlChangeSource } : {},
...ctx.pageMetadata ? { pageMetadata: ctx.pageMetadata } : {},
...ctx.closeReason ? { closeReason: ctx.closeReason } : {}
};
}
function getMostRecentBrowserState(activeStateSignals) {
for (const signal of [...activeStateSignals].reverse()) {
const browserState = getBrowserStateFromSignal(signal);
if (browserState) return browserState;
}
}
function getMostRecentBrowserClickResultUrl(steps) {
for (const step of [...steps].reverse()) {
const toolResults = Array.isArray(step.toolResults) ? step.toolResults : [];
for (const toolResult of [...toolResults].reverse()) {
if (toolResult?.toolName !== "browser_click") continue;
const url = getBrowserClickResultUrl(getToolResultOutputForAttribution(toolResult));
if (url) return url;
}
}
}
function getMostRecentBrowserClickResultUrlFromMessageList(messageList) {
try {
return getMostRecentBrowserClickResultUrlFromMessages(messageList.get.all.db());
} catch {
return;
}
}
function getMostRecentBrowserClickResultUrlFromMessages(messages) {
for (const message of [...messages].reverse()) {
if (isBrowserStateSignalMessage(message)) return void 0;
const content = message.content;
if (!content || typeof content !== "object" || Array.isArray(content)) continue;
const parts = content.parts;
if (!Array.isArray(parts)) continue;
for (const part of [...parts].reverse()) {
if (!part || typeof part !== "object" || Array.isArray(part)) continue;
const toolInvocation = part.toolInvocation;
if (!toolInvocation || typeof toolInvocation !== "object" || Array.isArray(toolInvocation)) continue;
const invocation = toolInvocation;
if (invocation.toolName !== "browser_click" || invocation.state !== "result") continue;
const url = getBrowserClickResultUrl(invocation.result ?? invocation.output);
if (url) return url;
}
}
}
function isBrowserStateSignalMessage(message) {
const content = message.content;
if (!content || typeof content !== "object" || Array.isArray(content)) return false;
const signal = content.metadata?.signal;
if (!signal || typeof signal !== "object" || Array.isArray(signal)) return false;
const metadata = signal.metadata;
if (!metadata || typeof metadata !== "object" || Array.isArray(metadata)) return false;
const state = metadata.state;
if (!state || typeof state !== "object" || Array.isArray(state)) return false;
return state.id === "browser";
}
function getBrowserClickResultUrl(output) {
if (!output || typeof output !== "object" || Array.isArray(output)) return void 0;
if (output.success !== true) return void 0;
const url = output.url;
return typeof url === "string" && url.length > 0 ? url : void 0;
}
function getToolResultOutputForAttribution(toolResult) {
if (!toolResult || typeof toolResult !== "object" || Array.isArray(toolResult)) return void 0;
const record = toolResult;
return record.output ?? record.result;
}
function getBrowserStateFromSignal(signal) {
const value = signal?.metadata?.value;
if (value && typeof value === "object" && !Array.isArray(value)) return value;
const browser = signal?.metadata?.browser;
if (browser && typeof browser === "object" && !Array.isArray(browser)) return browser;
}
function stableBrowserStateCacheKey(state) {
return JSON.stringify(state, (_key, value) => {
if (value && typeof value === "object" && !Array.isArray(value)) {
const sorted = {};
for (const key of Object.keys(value).sort()) sorted[key] = value[key];
return sorted;
}
return value;
});
}
function isBareClosedState(state) {
return !state.open && !state.activeUrl && !state.pageTitle && typeof state.tabCount !== "number" && !state.closeReason && (!state.pageMetadata || Object.keys(state.pageMetadata).length === 0);
}
function getChangedBrowserState(previous, current) {
if (!previous) return current;
const changed = {};
for (const key of Object.keys(current)) {
if (key === "processId") continue;
if (key === "activeUrlChangeSource" && previous.activeUrl === current.activeUrl) continue;
if (JSON.stringify(previous[key]) !== JSON.stringify(current[key])) changed[key] = current[key];
}
if (previous.activeUrl !== current.activeUrl && current.activeUrlChangeSource) changed.activeUrlChangeSource = current.activeUrlChangeSource;
if (!previous.open && current.open) {
if (current.activeUrl) changed.activeUrl = current.activeUrl;
if (current.activeUrlChangeSource) changed.activeUrlChangeSource = current.activeUrlChangeSource;
if (current.pageTitle) changed.pageTitle = current.pageTitle;
if (typeof current.tabCount === "number") changed.tabCount = current.tabCount;
if (current.pageMetadata && Object.keys(current.pageMetadata).length > 0) changed.pageMetadata = current.pageMetadata;
}
return changed;
}
function formatBrowserStateSnapshot(state) {
const parts = [formatOpenClosedStatus(state)];
if (state.activeUrl) parts.push(`Active tab URL: ${state.activeUrl}.`);
if (state.pageTitle) parts.push(`Page title: ${state.pageTitle}.`);
if (typeof state.tabCount === "number") parts.push(`${state.tabCount} open ${state.tabCount === 1 ? "tab" : "tabs"}.`);
if (state.pageMetadata && Object.keys(state.pageMetadata).length > 0) parts.push(`Page metadata: ${JSON.stringify(state.pageMetadata)}.`);
return parts.join(" ");
}
function formatBrowserStateDelta(delta) {
const parts = [];
if (typeof delta.open === "boolean") {
if (delta.open) parts.push("browser opened");
else parts.push(formatCloseReason(delta.closeReason));
}
if (delta.activeUrl) parts.push(formatActiveUrlChange(delta.activeUrl, delta.activeUrlChangeSource));
if (delta.pageTitle) parts.push(`page title changed to ${delta.pageTitle}`);
if (typeof delta.tabCount === "number") parts.push(`${delta.tabCount} open ${delta.tabCount === 1 ? "tab" : "tabs"}`);
if (delta.pageMetadata && Object.keys(delta.pageMetadata).length > 0) parts.push(`page metadata changed to ${JSON.stringify(delta.pageMetadata)}`);
return `changed: ${parts.join("; ")}`;
}
function formatActiveUrlChange(url, source) {
switch (source) {
case "agent": return `agent changed active tab URL to ${url}`;
case "user": return `user changed active tab URL to ${url}`;
default: return `active tab URL changed to ${url}`;
}
}
function formatOpenClosedStatus(state) {
if (state.open) return "Browser is open.";
switch (state.closeReason) {
case "process_restart": return "The browser was closed because the chat process restarted.";
case "user": return "The browser was closed externally, maybe by the user.";
case "error": return "Browser closed unexpectedly due to an error.";
case "agent": return "Browser is closed.";
default: return "Browser is closed.";
}
}
function formatCloseReason(reason) {
switch (reason) {
case "process_restart": return "the browser was closed because the chat process restarted";
case "user": return "the browser was closed externally, maybe by the user";
case "error": return "browser closed unexpectedly due to an error";
case "agent": return "browser closed";
default: return "browser closed";
}
}
// src/browser/thread-manager.ts
/** Default thread ID used when no thread is specified */
const DEFAULT_THREAD_ID = "__default__";
/**
* Abstract base class for managing thread-scoped browser sessions.
*
* @typeParam TManager - The browser manager type (e.g., BrowserManagerLike, Stagehand)
*/
var ThreadManager = class {
scope;
logger;
sessions = /* @__PURE__ */ new Map();
activeThreadId = DEFAULT_THREAD_ID;
/** Preserved browser state that survives session clears (for browser restore) */
savedBrowserStates = /* @__PURE__ */ new Map();
/** Shared manager instance (used for 'shared' scope) */
sharedManager = null;
/** Map of thread ID to dedicated manager instance (for 'thread' scope) */
threadManagers = /* @__PURE__ */ new Map();
onSessionCreated;
onSessionDestroyed;
constructor(config) {
this.scope = config.scope;
this.logger = config.logger;
this.onSessionCreated = config.onSessionCreated;
this.onSessionDestroyed = config.onSessionDestroyed;
}
/**
* Get the current browser scope mode.
*/
getScope() {
return this.scope;
}
/**
* Get the currently active thread ID.
*/
getActiveThreadId() {
return this.activeThreadId;
}
/**
* Set the shared manager instance (called after browser launch).
*/
setSharedManager(manager) {
this.sharedManager = manager;
}
/**
* Clear the shared manager instance (called when browser disconnects).
*/
clearSharedManager() {
this.sharedManager = null;
}
/**
* Get the manager for an existing thread session without creating a new one.
*
* For 'thread' scope: Returns the thread-specific manager, or null if no session exists.
* For 'shared' scope: Returns the shared manager (all threads use the same instance).
*
* @param threadId - Thread identifier (defaults to DEFAULT_THREAD_ID)
* @returns The manager for the thread, or null if not found (thread scope only)
*/
getExistingManagerForThread(threadId) {
const effectiveThreadId = threadId ?? "__default__";
if (this.scope === "thread") return this.threadManagers.get(effectiveThreadId) ?? null;
return this.sharedManager;
}
/**
* Check if any thread managers are still running (for 'thread' scope).
*/
hasActiveThreadManagers() {
return this.threadManagers.size > 0;
}
/**
* Clear all session tracking without closing managers.
* Used when browsers have been externally closed and we just need to reset state.
*/
clearAllSessions() {
this.threadManagers.clear();
this.sessions.clear();
this.activeThreadId = DEFAULT_THREAD_ID;
}
/**
* Get a session by thread ID.
*/
getSession(threadId) {
return this.sessions.get(threadId);
}
/**
* Check if a session exists for a thread.
*/
hasSession(threadId) {
return this.sessions.has(threadId);
}
/**
* List all active sessions.
*/
listSessions() {
return Array.from(this.sessions.values());
}
/**
* Get the number of active sessions.
*/
getSessionCount() {
return this.sessions.size;
}
/**
* Get or create a session for a thread, and return the browser manager for that thread.
*
* For 'shared' scope, returns the shared manager.
* For 'thread' scope, creates/returns a dedicated manager for the thread.
*
* @param threadId - Thread identifier (uses DEFAULT_THREAD_ID if not provided)
* @returns The browser manager for the thread
*/
async getManagerForThread(threadId) {
const effectiveThreadId = threadId ?? "__default__";
if (this.scope === "shared") return this.getSharedManager();
let session = this.sessions.get(effectiveThreadId);
if (!session) {
session = await this.createSession(effectiveThreadId);
this.sessions.set(effectiveThreadId, session);
this.logger?.debug?.(`Created thread session: ${effectiveThreadId}`);
this.onSessionCreated?.(session);
}
this.activeThreadId = effectiveThreadId;
return this.getManagerForSession(session);
}
/**
* Destroy a specific thread's session.
*
* @param threadId - Thread identifier
*/
async destroySession(threadId) {
const session = this.sessions.get(threadId);
if (!session) return;
await this.doDestroySession(session);
this.threadManagers.delete(threadId);
this.sessions.delete(threadId);
this.logger?.debug?.(`Destroyed thread session: ${threadId}`);
this.onSessionDestroyed?.(threadId);
if (this.activeThreadId === threadId) this.activeThreadId = DEFAULT_THREAD_ID;
}
/**
* Destroy all thread sessions.
*/
async destroyAllSessions() {
const threadIds = Array.from(this.sessions.keys());
for (const threadId of threadIds) await this.destroySession(threadId);
this.activeThreadId = DEFAULT_THREAD_ID;
}
/**
* Update the browser state for a thread session.
* Also saves to persistent storage so state survives session clears.
*/
updateBrowserState(threadId, state) {
const filteredTabs = state.tabs.filter((tab) => tab.url && tab.url !== "about:blank");
if (filteredTabs.length === 0) return;
const filteredState = {
...state,
tabs: filteredTabs,
activeTabIndex: Math.max(0, Math.min(state.activeTabIndex, filteredTabs.length - 1))
};
const session = this.sessions.get(threadId);
if (session) session.browserState = filteredState;
this.savedBrowserStates.set(threadId, filteredState);
}
/**
* Get the saved browser state for a thread (survives session clears).
*/
getSavedBrowserState(threadId) {
const session = this.sessions.get(threadId);
if (session?.browserState) return session.browserState;
return this.savedBrowserStates.get(threadId);
}
/**
* Clear a specific thread's session without closing the browser.
* Used when a thread's browser has been externally closed.
* Preserves the browser state for potential restoration.
*
* @param threadId - The thread ID to clear
*/
clearSession(threadId) {
const session = this.sessions.get(threadId);
if (session?.browserState) this.savedBrowserStates.set(threadId, session.browserState);
this.threadManagers.delete(threadId);
this.sessions.delete(threadId);
if (this.activeThreadId === threadId) this.activeThreadId = DEFAULT_THREAD_ID;
}
/**
* Get the shared browser manager (used for 'shared' scope and default thread).
* @throws Error if shared manager is not initialized
*/
getSharedManager() {
if (!this.sharedManager) throw new Error("Browser not launched");
return this.sharedManager;
}
};
// src/browser/browser.ts
/**
* MastraBrowser Base Class
*
* Abstract base class for browser providers. Extends MastraBase for logger integration.
*
* ## Architecture
*
* Each browser provider defines its own tools via the `getTools()` method.
* This allows different providers to offer different capabilities:
*
* - **AgentBrowser**: 17 deterministic tools using refs ([ref=e1], [ref=e2])
* - **StagehandBrowser**: AI-powered tools (act, extract, observe)
*
* ## Two Paradigms
*
* Browser providers fall into two paradigms:
*
* 1. **Deterministic** (Playwright, agent-browser) - Uses refs and selectors
* 2. **AI-powered** (Stagehand) - Uses natural language instructions
*
* Both extend this base class and implement `getTools()` to return their tools.
*/
/**
* Lock files that Chrome/Chromium creates in the profile directory.
* These can become stale if the browser doesn't shut down cleanly.
*/
const CHROME_LOCK_FILES = [
"SingletonLock",
"SingletonSocket",
"SingletonCookie",
"chrome.pid",
"RunningChromeVersion"
];
/**
* Clean up stale Chrome lock files from a profile directory.
*
* Chrome creates lock files (SingletonLock, SingletonSocket, etc.) to prevent
* multiple instances from using the same profile. If the browser crashes or
* doesn't shut down cleanly, these files can remain and block future launches.
*
* This function removes these lock files, allowing the profile to be reused.
* It's safe to call even if the files don't exist.
*
* @param profilePath - Path to the Chrome profile directory
* @param logger - Optional logger for debug output
*/
function cleanupProfileLockFiles(profilePath, logger) {
if (!profilePath || !(0, fs.existsSync)(profilePath)) return;
try {
const entries = (0, fs.readdirSync)(profilePath);
for (const entry of entries) if (CHROME_LOCK_FILES.includes(entry)) {
const fullPath = (0, path.join)(profilePath, entry);
try {
const stat = (0, fs.lstatSync)(fullPath);
if (stat.isFile() || stat.isSymbolicLink()) {
(0, fs.unlinkSync)(fullPath);
logger?.debug?.(`Removed stale lock file: ${fullPath}`);
}
} catch (err) {
logger?.warn?.(`Failed to remove lock file ${fullPath}: ${err}`);
}
}
} catch (err) {
logger?.warn?.(`Failed to clean up profile lock files in ${profilePath}: ${err}`);
}
}
/**
* Thrown when a browser profile is held by another running browser process.
*/
var BrowserProfileInUseError = class extends Error {
profilePath;
holderPid;
holderHost;
constructor(profilePath, holderPid, holderHost) {
const holder = holderPid === void 0 ? "another browser process" : `another browser process (PID ${holderPid}${holderHost && holderHost !== (0, os.hostname)() ? ` on host ${holderHost}` : ""})`;
const killHint = holderPid === void 0 ? "" : ` (for example: kill ${holderPid})`;
super(`Browser profile "${profilePath}" is already in use by ${holder}. Close that browser${killHint} or use a different profile, then try again.`);
this.profilePath = profilePath;
this.holderPid = holderPid;
this.holderHost = holderHost;
this.name = "BrowserProfileInUseError";
}
};
/**
* Resolves true if something is listening on the Unix socket at `socketPath`.
* Only a refused connection or a missing socket counts as "nobody listening";
* any other outcome is treated as a live listener so a lock is never removed
* from a profile that might still be in use.
*/
function isSocketAccepting(socketPath, timeoutMs = 1e3) {
return new Promise((resolve) => {
const socket = (0, net.createConnection)(socketPath);
const finish = (accepting) => {
socket.destroy();
resolve(accepting);
};
socket.setTimeout(timeoutMs, () => finish(true));
socket.once("connect", () => finish(true));
socket.once("error", (err) => finish(err.code !== "ECONNREFUSED" && err.code !== "ENOENT"));
});
}
/** How long to wait for a live local lock holder to start listening on its socket. */
const STARTING_HOLDER_GRACE_MS = 3e3;
function isProcessAlive(pid) {
if (!Number.isInteger(pid) || pid <= 1) return false;
try {
process.kill(pid, 0);
return true;
} catch (err) {
return err.code === "EPERM";
}
}
/** How long close() waits for a killed browser to exit before releasing its profile lock. */
const BROWSER_EXIT_TIMEOUT_MS = 2e3;
/** Wait until every PID has exited, or the deadline passes. */
async function waitForProcessesToExit(pids, timeoutMs) {
const deadline = Date.now() + timeoutMs;
while (pids.some((pid) => pid !== void 0 && isProcessAlive(pid)) && Date.now() < deadline) await new Promise((resolve) => setTimeout(resolve, 50));
}
/** Read a symlink's target, or undefined if `path` is missing or not a symlink. */
function readLinkTarget(path) {
try {
return (0, fs.readlinkSync)(path);
} catch {
return;
}
}
/**
* Make a local Chrome profile usable before launching a browser on it.
*
* Chrome marks a profile as in use with a `SingletonLock` symlink (pointing at
* `<hostname>-<pid>`) and a `SingletonSocket` the running browser listens on.
* A new Chrome started on a locked profile hands off to the holder and exits,
* so no DevTools port ever opens and the launcher fails with ECONNREFUSED. If
* the holder is gone but the lock remains (a crash, SIGKILL, a changed
* hostname), Chrome may refuse the profile, and the browser stays broken.
*
* - No lock: nothing to do.
* - A browser still listens on the profile's socket: throws
* {@link BrowserProfileInUseError} naming the profile and holding PID.
* - Nothing listens: the lock is stale, so the lock files are removed.
*
* The socket check is used instead of a PID check because a PID can be reused
* by an unrelated process, and a hung Chrome still holds its socket.
* No-op on Windows, where Chrome does not use these lock files.
*/
async function releaseStaleProfileLock(profilePath, logger) {
if (!profilePath || process.platform === "win32") return;
const lockPath = (0, path.join)(profilePath, "SingletonLock");
try {
(0, fs.lstatSync)(lockPath);
} catch {
return;
}
const lockTarget = readLinkTarget(lockPath);
const match = lockTarget?.match(/^(.*)-(\d+)$/);
const holderHost = match?.[1];
const holderPid = match ? Number(match[2]) : void 0;
const socketLink = (0, path.join)(profilePath, "SingletonSocket");
const probeSocket = () => {
const socketTarget = readLinkTarget(socketLink);
return isSocketAccepting(socketTarget ? (0, path.resolve)(profilePath, socketTarget) : socketLink);
};
let accepting = await probeSocket();
if (!accepting && holderHost === (0, os.hostname)() && holderPid !== void 0 && isProcessAlive(holderPid)) {
const deadline = Date.now() + STARTING_HOLDER_GRACE_MS;
while (!accepting && Date.now() < deadline && isProcessAlive(holderPid)) {
await new Promise((r) => setTimeout(r, 100));
accepting = await probeSocket();
}
}
if (accepting) throw new BrowserProfileInUseError(profilePath, holderPid, holderHost);
logger?.warn?.(`Removing stale browser profile lock in ${profilePath}` + (lockTarget ? ` (left by ${lockTarget})` : ""));
cleanupProfileLockFiles(profilePath, logger);
}
/**
* Log files chrome-launcher writes into the profile directory. It opens them in
* append mode and takes Chrome's DevTools port from the first match in
* `chrome-err.log`, so on a reused profile it reads the port of the first
* launch ever recorded and fails with ECONNREFUSED on that dead port.
*/
const CHROME_LAUNCHER_LOG_FILES = ["chrome-err.log", "chrome-out.log"];
/**
* Remove chrome-launcher's log files from a profile so the next launch reads
* the DevTools port of the Chrome it just started. Safe to call when the files
* don't exist.
*/
function clearChromeLauncherLogs(profilePath, logger) {
if (!profilePath) return;
for (const file of CHROME_LAUNCHER_LOG_FILES) {
const fullPath = (0, path.join)(profilePath, file);
try {
(0, fs.unlinkSync)(fullPath);
logger?.debug?.(`Removed stale launcher log: ${fullPath}`);
} catch (err) {
if (err.code !== "ENOENT") logger?.warn?.(`Failed to remove launcher log ${fullPath}: ${err}`);
}
}
}
/**
* Kill a browser process and its children by sending SIGKILL to the process group.
*
* When Chrome/Chromium is launched, it spawns child processes (GPU, renderer,
* network, storage, crashpad handlers). If the main process exits uncleanly,
* these children can become orphaned. Killing the process group ensures all
* related processes are cleaned up.
*
* Note: Process group signaling (`-pid`) is POSIX-only. On Windows, this
* function is a no-op and orphaned child processes must be cleaned up by
* other means (e.g., taskkill).
*
* @param pid - The PID of the main browser process. If undefined, this is a no-op.
* @param logger - Optional logger for debug output.
*/
function killProcessGroup(pid, logger) {
if (pid == null) return;
if (!Number.isInteger(pid) || pid <= 1) {
logger?.debug?.(`Refusing to kill process group for unsafe PID ${pid}`);
return;
}
try {
process.kill(-pid, "SIGKILL");
logger?.debug?.(`Killed process group for PID ${pid}`);
} catch (err) {
const code = err.code;
if (code !== "ESRCH") logger?.warn?.(`Failed to kill process group ${pid}: ${code ?? err}`);
}
}
/**
* Fallback dimensions used when a provider cannot honor `'window'`.
*/
const DEFAULT_BROWSER_VIEWPORT = {
width: 1280,
height: 720
};
/**
* Narrow a viewport config to explicit dimensions, or `undefined` for `'window'`.
*/
function resolveViewportSize(viewport) {
return viewport === "window" ? void 0 : viewport;
}
/**
* Translate a viewport config into Playwright-style launch options for
* `agent-browser`.
*
* `agent-browser` disables viewport emulation when it sees a window-sizing
* argument, which is how `'window'` is expressed without passing a `null`
* viewport its public types don't accept.
*/
function resolveLaunchViewport(viewport) {
if (viewport === "window") return { args: ["--start-maximized"] };
return { viewport };
}
/**
* Abstract base class for browser providers.
*
* Providers extend this class and implement the abstract methods.
* Each method corresponds to one of the 17 flat tools.
*/
var MastraBrowser = class MastraBrowser extends require_base.MastraBase {
/**
* Provider type for runtime enforcement.
* - 'sdk': SDK providers (AgentBrowser, StagehandBrowser) — use with Agent.browser
* - 'cli': CLI providers (BrowserViewer) — use with Workspace.browser
* Defaults to 'sdk' for backward compatibility with existing providers.
*/
providerType = "sdk";
/** Current lifecycle status */
status = "pending";
/** Error message when status is 'error' */
error;
/** Set by retire(): the browser never launches again. */
retired = false;
/**
* Whether the browser is running in headless mode.
* Returns true by default if not explicitly configured.
*/
get headless() {
return this.config.headless ?? true;
}
/** Last known browser state before browser was closed (for restore on relaunch) */
lastBrowserState;
/**
* Shared manager instance for 'shared' scope mode.
* Type varies by provider (e.g., BrowserManager for agent-browser, Stagehand for stagehand).
* Providers should cast this to their specific type when accessing.
*/
sharedManager = null;
/** Configuration */
config;
/**
* Thread manager for handling thread-scoped browser sessions.
* Set by subclasses that support thread isolation.
*/
threadManager;
/**
* Current thread ID for browser operations.
* Used by thread isolation to route operations to the correct session.
*/
currentThreadId = DEFAULT_THREAD_ID;
/** Default key for shared scope screencast streams */
static SHARED_STREAM_KEY = "__shared__";
/** Active screencast streams per thread (for triggering reconnects on tab changes) */
activeScreencastStreams = /* @__PURE__ */ new Map();
/**
* PID of the shared browser process.
* Set by providers after launch so the base class can kill the process group
* (GPU, renderer, crashpad, etc.) when the browser disconnects or closes.
*/
sharedBrowserPid;
/**
* PIDs of per-thread browser processes.
* Set by providers after creating a thread session.
*/
threadBrowserPids = /* @__PURE__ */ new Map();
/**
* Get the stream key for a thread (or shared key for shared scope).
* @param threadId - Optional thread ID
* @returns The stream key to use for the screencast streams map
*/
getStreamKey(threadId) {
return threadId || MastraBrowser.SHARED_STREAM_KEY;
}
/**
* Reconnect the active screencast for a specific thread.
* Called internally when tabs are switched or closed.
*/
async reconnectScreencastForThread(threadId, reason) {
const streamKey = this.getStreamKey(threadId);
const stream = this.activeScreencastStreams.get(streamKey);
if (!stream || !stream.isActive()) return;
if (!this.isBrowserRunning()) {
this.logger.debug?.("Skipping screencast reconnect - browser not running");
return;
}
if (this.getScope() === "thread" && threadId && !this.threadManager?.getExistingManagerForThread(threadId)) {
this.logger.debug?.(`Skipping screencast reconnect - no session for thread ${threadId}`);
return;
}
this.logger.debug?.(`Reconnecting screencast: ${reason}`);
try {
await new Promise((resolve) => setTimeout(resolve, 150));
await stream.reconnect();
const activePage = await this.getActivePage(threadId);
if (activePage) {
const url = activePage.url();
if (url) stream.emitUrl(url);
}
} catch (error) {
this.logger.debug?.("Screencast reconnect failed", error);
}
}
/**
* Update the browser state in the thread session.
* Called on navigation, tab open/close to keep state fresh.
*/
updateSessionBrowserState(threadId) {
try {
const effectiveThreadId = threadId ?? this.getCurrentThread() ?? "__default__";
const state = this.getBrowserStateForThread(effectiveThreadId);
if (state) this.threadManager?.updateBrowserState(effectiveThreadId, state);
} catch {}
}
_launchPromise;
_closePromise;
constructor(config = {}) {
super({
name: "MastraBrowser",
component: require_logger.RegisteredLogger.BROWSER
});
this.config = config;
const scope = config.scope;
if (config.cdpUrl && scope === "thread") throw new Error("Invalid browser configuration: \"cdpUrl\" and \"scope: 'thread'\" cannot be used together.\n\n• cdpUrl connects to a single existing browser instance (all threads share it)\n• scope: \"thread\" requires spawning separate browser instances per thread\n\nTo fix this, either:\n1. Remove cdpUrl to let the provider spawn separate browser instances (supports thread isolation)\n2. Use scope: \"shared\" when connecting via cdpUrl (all threads share one browser)");
if (config.cdpUrl && (config.profile || config.executablePath)) {
const conflicting = [config.profile && "profile", config.executablePath && "executablePath"].filter(Boolean).join(" and ");
throw new Error(`Invalid browser configuration: "cdpUrl" cannot be used with ${conflicting}.\n\n• cdpUrl connects to an existing browser (which has its own profile and executable)
• profile and executablePath are launch-time options for spawning a new browser
To fix this, either:
1. Remove cdpUrl to launch a new browser with your profile/executable
2. Remove profile/executablePath to connect to the existing browser via CDP`);
}
}
/**
* Local Chrome profile directory this browser launches with, or undefined
* when it does not launch a local browser on a profile (no profile set, or
* a remote browser). launch() checks this profile's lock before launching.
* Override when the provider can connect to a remote browser in other ways.
*/
getLocalProfilePath() {
if (this.config.cdpUrl) return void 0;
return this.config.profile;
}
/**
* Make the local profile safe to launch Chrome on: release a stale lock (or
* throw {@link BrowserProfileInUseError} if a live browser holds it), then
* clear chrome-launcher's logs. launch() calls this; providers that start
* Chrome outside launch() (e.g. a per-thread browser) must call it first too.
*/
async prepareLocalProfile() {
const localProfile = this.getLocalProfilePath();
if (!localProfile) return;
await releaseStaleProfileLock(localProfile, this.logger);
clearChromeLauncherLogs(localProfile, this.logger);
}
/**
* Remove this browser's profile lock after it closed, unless another browser
* now holds the profile. A close that finishes late (after a replacement
* already launched on the same profile) must not delete the live lock.
*
* Killing Chrome only sends a signal: until it exits its socket still
* accepts and the lock looks held. So wait for the closed browser's
* processes (`pids`) to exit first.
*/
async releaseProfileLockAfterClose(pids = []) {
if (!this.config.profile) return;
await waitForProcessesToExit(pids, BROWSER_EXIT_TIMEOUT_MS);
try {
await releaseStaleProfileLock(this.config.profile, this.logger);
} catch (err) {
if (!(err instanceof BrowserProfileInUseError)) throw err;
this.logger.debug?.(`Leaving profile lock in ${this.config.profile}: another browser holds it`);
}
}
/**
* Get the CDP WebSocket URL for connecting to this browser.
* CLI providers (BrowserViewer) implement this to expose the URL for CLI tools.
* SDK providers typically return null as they manage their own CDP connections.
*
* @param _threadId - Thread identifier (for thread-scoped browsers)
* @returns The CDP WebSocket URL (e.g., ws://127.0.0.1:9222/devtools/browser/...)
*/
getCdpUrl(_threadId) {
return null;
}
/**
* Launch the browser.
* Race-condition-safe - handles concurrent calls, status management, and lifecycle hooks.
* @param _threadId - Thread identifier (for thread-scoped browsers, launches a browser for that thread)
*/
async launch(threadId) {
if (threadId !== void 0) this.setCurrentThread(threadId);
this.assertNotRetired();
if (this.status === "ready") return;
if (this.status === "launching" && this._launchPromise) return this._launchPromise;
if (this.status === "closing" || this.status === "closed") throw new Error(`Cannot launch browser in '${this.status}' state`);
this.status = "launching";
this.error = void 0;
this._launchPromise = (async () => {
try {
await this.prepareLocalProfile();
await this.doLaunch();
this.status = "ready";
if (this.config.onLaunch) await this.config.onLaunch({ browser: this });
this.notifyBrowserReady();
} catch (err) {
this.status = "error";
this.error = err instanceof Error ? err.message : String(err);
throw err;
} finally {
this._launchPromise = void 0;
}
})();
return this._launchPromise;
}
/**
* Close the browser.
* Race-condition-safe - handles concurrent calls, status management, and lifecycle hooks.
*/
async close() {
for (const key of /* @__PURE__ */ new Set([...this.pendingCookies.keys(), ...this.cookieQueueUpdates.keys()])) this.dropQueuedCookies(key);
if (this.status === "closed") return;
if (this.status === "closing" && this._closePromise) return this._closePromise;
if (this.status === "launching" && this._launchPromise) try {
await this._launchPromise;
} catch {
this.status = "closed";
return;
}
if (this.config.onClose && this.status === "ready") await this.config.onClose({ browser: this });
const currentState = await this.getBrowserState();
if (currentState && currentState.tabs.length > 0) this.lastBrowserState = currentState;
this.status = "closing";
this._closePromise = (async () => {
let closed = false;
const pids = [this.sharedBrowserPid, ...this.threadBrowserPids.values()];
try {
await this.doClose();
closed = true;
this.killProcesses();
this.status = "closed";
this.notifyBrowserClosed();
await this.releaseProfileLockAfterClose(pids);
} catch (err) {
this.status = "error";
this.error = err instanceof Error ? err.message : String(err);
throw err;
} finally {
this._closePromise = void 0;
if (!closed) this.killProcesses();
}
})();
return this._closePromise;
}
/**
* Throw if retire() was called. Subclasses that start browsers outside
* launch()/ensureReady() (e.g. per-thread sessions) must call this first.
*/
assertNotRetired() {
if (this.retired) throw new Error(`Browser ${this.id} was retired and cannot be used`);
}
/**
* Close the browser for good. Unlike close(), a retired browser never
* relaunches, so a caller still holding it (e.g. a tool call that started
* before the browser was replaced) gets an error instead of a new Chrome
* that nothing tracks.
*/
async retire() {
this.retired = true;
await this.close();
}
/**
* SIGKILL the browser processes this instance launched. For when close()
* cannot be waited on (it hangs, or the process is about to exit), so the
* browser does not outlive its owner and keep holding its profile.
*/
/**
* SIGKILL the process group of a local browser this class does not track
* (e.g. one that started after retire() and was never registered).
* Only pass PIDs of browsers launched on this machine.
*/
killBrowserProcessGroup(pid) {
killProcessGroup(pid, this.logger);
}
killProcesses() {
killProcessGroup(this.sharedBrowserPid, this.logger);
this.sharedBrowserPid = void 0;
for (const [, pid] of this.threadBrowserPids) killProcessGroup(pid, this.logger);
this.threadBrowserPids.clear();
}
/**
* Connect to an external browser via CDP URL for screencast.
*
* Use this when an agent is using their own external CDP (e.g., browser-use cloud).
* Connects Playwright to the external browser to enable screencast without launching
* our own browser.
*
* Override this in subclasses that support external CDP connections.
* The base implementation throws an error.
*
* @param cdpUrl - The external CDP WebSocket URL (wss://... or ws://...)
* @param threadId - Thread ID to associate the session with
*/
async connectToExternalCdp(_cdpUrl, _threadId) {
throw new Error(`${this.provider} does not support connecting to external CDP`);
}
/**
* Ensure the browser is ready, launching if needed.
* If browser was previously closed, it will be re-launched.
*/
async ensureReady() {
this.assertNotRetired();
if (this.status === "ready") {
if (await this.checkBrowserAlive()) return;
this.status = "closed";
}
if (this.status === "pending" || this.status === "error" || this.status === "closed") {
if (this.status === "closed") this.status = "pending";
await this.launch();
return;
}
if (this.status === "launching") {
await this._launchPromise;
return;
}
if (this.status === "closing") {
await this._closePromise;
this.assertNotRetired();
this.status = "pending";
await this.launch();
return;
}
throw new Error(`Browser is ${this.status} and cannot be used`);
}
/**
* Check if the browser is still alive.
* Override in subclass to detect externally closed browsers.
* @returns true if browser is alive, false if it was externally closed
*/
async checkBrowserAlive() {
return true;
}
/**
* Check if the browser is currently running.
* @param _threadId - Thread identifier (for thread-scoped browsers)
*/
isBrowserRunning(_threadId) {
return this.status === "ready";
}
/**
* Resolve a CDP URL from a static string or async provider function.
* @param cdpUrl - Static string or async function returning the CDP URL
* @returns Resolved CDP URL string
*/
async resolveCdpUrl(cdpUrl) {
return typeof cdpUrl === "function" ? await cdpUrl() : cdpUrl;
}
/**
* Resolve an HTTP CDP endpoint to a WebSocket URL by fetching /json/version.
*
* Cloud browser providers (Browser-Use, Browserless, etc.) often expose HTTP
* endpoints that need to be resolved to WebSocket URLs for direct CDP connections.
*
* - If the URL starts with `ws://` or `wss://`, returns it as-is
* - If the URL starts with `http://` or `https://`, fetches /json/version to get webSocketDebuggerUrl
*
* @param url - CDP URL (HTTP or WebSocket)
* @returns WebSocket URL for CDP connection
*/
async resolveWebSocketUrl(url) {
if (url.startsWith("ws://") || url.startsWith("wss://")) return url;
if (url.startsWith("http://") || url.startsWith("https://")) {
const versionUrl = `${url.replace(/\/$/, "")}/json/version`;
this.logger.debug?.(`Resolving WebSocket URL from ${versionUrl}`);
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 1e4);
try {
const response = await fetch(versionUrl, { signal: controller.signal });
clearTimeout(timeoutId);
if (!response.ok) throw new Error(`Failed to fetch CDP version info from ${versionUrl}: ${response.status} ${response.statusText}`);
const data = await response.json();
if (!data.webSocketDebuggerUrl) throw new Error(`No webSocketDebuggerUrl found in CDP version response from ${versionUrl}`);
this.logger.debug?.(`Resolved WebSocket URL: ${data.webSocketDebuggerUrl}`);
return data.webSocketDebuggerUrl;
} catch (error) {
clearTimeout(timeoutId);
if (error instanceof Error && error.name === "AbortError") throw new Error(`Timeout resolving WebSocket URL from ${versionUrl} (10s)`);
throw error;
}
}
return url;
}
/**
* Error patterns that indicate browser disconnection.
* Used by isDisconnectionError() to detect external browser closure.
*/
static DISCONNECTION_PATTERNS = [
"Target closed",
"Target page, context or browser has been closed",
"Browser has been closed",
"Connection closed",
"Protocol error",
"Session closed",
"browser has disconnected",
"closed externally"
];
/**
* Check if an error message indicates browser disconnection.
* @param message - Error message to check
* @returns true if the message indicates disconnection
*/
isDisconnectionError(message) {
const lowerMessage = message.toLowerCase();
return MastraBrowser.DISCONNECTION_PATTERNS.some((pattern) => lowerMessage.includes(pattern.toLowerCase()));
}
/**
* Handle browser disconnection by updating status and notifying listeners.
* Called when browser is detected as externally closed.
*
* For 'thread' scope: clears only the specific thread's session (other threads unaffected)
* For 'shared' scope: clears the shared manager and updates global status
*/
handleBrowserDisconnected() {
const scope = this.threadManager?.getScope();
const threadId = this.getCurrentThread();
let closedPid;
if (scope === "thread" && threadId !== "__default__") {
const pid = this.threadBrowserPids.get(threadId);
closedPid = pid;
killProcessGroup(pid, this.logger);
this.threadBrowserPids.delete(threadId);
this.threadManager.clearSession(threadId);
this.logger.debug?.(`Cleared browser session for thread: ${threadId}`);
this.notifyBrowserClosed(threadId);
} else {
closedPid = this.sharedBrowserPid;
killProcessGroup(this.sharedBrowserPid, this.logger);
this.sharedBrowserPid = void 0;
this.sharedManager = null;
this.threadManager?.clearSharedManager();
if (this.status !== "closed") {
this.status = "closed";
this.logger.debug?.("Browser was externally closed, status set to closed");
this.notifyBrowserClosed();
}
}
this.releaseProfileLockAfterClose([closedPid]).catch((err) => {
this.logger.debug?.(`Failed to release profile lock: ${err}`);
});
}
/**
* Create a BrowserToolError from an exception.
* Handles common error patterns including disconnection detection.
* Subclasses can override to add provider-specific error handling.
*
* @param error - The caught error
* @param context - Description of what operation failed (e.g., "Click operation")
* @returns Structured BrowserToolError
*/
createErrorFromException(error, context) {
const msg = error instanceof Error ? error.message : String(error);
if (this.isDisconnectionError(msg)) {
this.handleBrowserDisconnected();
return createError("browser_closed", "Browser was closed externally.", "The browser window was closed. Please retry to re-launch.");
}
if (msg.includes("timeout") || msg.includes("Timeout") || msg.includes("aborted")) return createError("timeout", `${context} timed out.`, "Try again or increase timeout.");
if (msg.includes("not launched") || msg.includes("Browser is not launched")) return createError("browser_error", "Browser was not initialized.", "This is an internal error - please try again.");
return createError("browser_error", `${context} failed: ${msg}`, "Check the browser state and try again.");
}
/**
* Create a specific error type.
* Convenience method for providers to create typed errors.
*/
createError(code, message, hint) {
return createError(code, message, hint);
}
_onReadyCallbacks = /* @__PURE__ */ new Set();
_onClosedCallbacks = /* @__PURE__ */ new Set();
/** Thread-specific ready callbacks. Key is threadId. */
_onThreadReadyCallbacks = /* @__PURE__ */ new Map();
/** Thread-specific closed callbacks. Key is threadId. */
_onThreadClosedCallbacks = /* @__PURE__ */ new Map();
/**
* Register a callback to be invoked when the browser becomes ready.
* If browser is already running, callback is invoked immediately.
* The callback is ALWAYS registered (even if invoked immediately) so it will
* also fire on future "ready" events (e.g., session creation for thread isolation).
* @param callback - Function to call when browser is ready
* @param threadId - Optional thread ID to scope the callback to a specific thread
* @returns Cleanup function to unregister the callback
*/
onBrowserReady(callback, threadId) {
if (threadId) {
let threadCallbacks = this._onThreadReadyCallbacks.get(threadId);
if (!threadCallbacks) {
threadCallbacks = /* @__PURE__ */ new Set();
this._onThreadReadyCallbacks.set(threadId, threadCallbacks);
}
threadCallbacks.add(callback);
if (this.hasThreadSession(threadId)) callback();
return () => {
threadCallbacks.delete(callback);
if (threadCallbacks.size === 0) this._onThreadReadyCallbacks.delete(threadId);
};
}
this._onReadyCallbacks.add(callback);
if (this.isBrowserRunning()) callback();
return () => {
this._onReadyCallbacks.delete(callback);
};
}
/**
* Register a callback to be invoked when the browser closes.
* Useful for screencast to broadcast browser_closed status.
* @param callback - Function to call when browser closes
* @param threadId - Optional thread ID to scope the callback to a specific thread
* @returns Cleanup function to unregister the callback
*/
onBrowserClosed(callback, threadId) {
if (threadId) {
let threadCallbacks = this._onThreadClosedCallbacks.get(threadId);
if (!threadCallbacks) {
threadCallbacks = /* @__PURE__ */ new Set();
this._onThreadClosedCallbacks.set(threadId, threadCallbacks);
}
threadCallbacks.add(callback);
return () => {
threadCallbacks.delete(callback);
if (threadCallbacks.size === 0) this._onThreadClosedCallbacks.delete(threadId);
};
}
this._onClosedCallbacks.add(callback);
return () => {
this._onClosedCallbacks.delete(callback);
};
}
/**
* Notify registered callbacks that browser is ready.
* @param threadId - If provided, only notify callbacks for that thread (for thread scope)
*/
notifyBrowserReady(threadId) {
if (threadId) {
const threadCallbacks = this._onThreadReadyCallbacks.get(threadId);
if (threadCallbacks) for (const callback of threadCallbacks) try {
callback();
} catch {}
} else {
for (const callback of this._onReadyCallbacks) try {
callback();
} catch {}
for (const [, threadCallbacks] of this._onThreadReadyCallbacks) for (const callback of threadCallbacks) try {
callback();
} catch {}
}
}
/**
* Notify registered callbacks that browser has closed.
* @param threadId - If provided, only notify callbacks for that thread (for thread scope)
*/
notifyBrowserClosed(threadId) {
if (threadId) {
const threadCallbacks = this._onThreadClosedCallbacks.get(threadId);
if (threadCallbacks) for (const callback of threadCallbacks) try {
callback();
} catch {}
} else {
for (const callback of this._onClosedCallbacks) try {
callback();
} catch {}
for (const [, threadCallbacks] of this._onThreadClosedCallbacks) for (const callback of threadCallbacks) try {
callback();
} catch {}
}
}
/**
* Get the current page URL without launching the browser.
* @param threadId - Optional thread ID for thread-isolated browsers
* @returns The current URL string, or null if browser is not running or not supported
*/
async getCurrentUrl(_threadId) {
return null;
}
/**
* Get the current browser state (all tabs and active tab index).
* Override in subclass to provide actual tab state.
* @param _threadId - Optional thread ID for thread-isolated sessions
* @returns The browser state, or null if not available
*/
async getBrowserState(_threadId) {
return null;
}
/**
* Get the last known browser state before the browser was closed.
* Useful for restoring state on relaunch.
* @param threadId - Optional thread ID for thread-isolated sessions
* @returns The last browser state, or undefined if not available
*/
getLastBrowserState(threadId) {
if (threadId && this.threadManager) {
const savedState = this.threadManager.getSavedBrowserState(threadId);
if (savedState) return savedState;
}
return this.lastBrowserState;
}
/**
* Get all open tabs with their URLs and titles.
* Override in subclass to provide actual tab info.
* @param _threadId - Optional thread ID for thread-isolated sessions
* @returns Array of tab states
*/
async getTabState(_threadId) {
return [];
}
/**
* Get the active tab index.
* Override in subclass to provide actual active tab index.
* @param _threadId - Optional thread ID for thread-isolated sessions
* @returns The active tab index (0-based), or 0 if not available
*/
async getActiveTabIndex(_threadId) {
return 0;
}
/**
* Navigate to a URL (simple form). Override in subclass if supported.
* Used internally for restoring state on relaunch.
* Named `navigateTo` to avoid conflicts with tool methods that have richer signatures.
*/
async navigateTo(_url) {}
/**
* Set the current thread ID for subsequent browser operations.
* Called by tools before executing browser actions to ensure
* operations are routed to the correct thread session.
*
* @param threadId - The thread ID, or undefined to use the default thread
*/
setCurrentThread(threadId) {
this.currentThreadId = threadId ?? "__default__";
}
/**
* Get the current thread ID.
* @returns The current thread ID being used for operations
*/
getCurrentThread() {
return this.currentThreadId;
}
/**
* Get the browser scope mode.
* @returns The scope from threadManager or config, defaults to 'shared'
*/
getScope() {
return this.threadManager?.getScope() ?? this.config.scope ?? "shared";
}
/**
* Start screencast streaming. Override in subclass if supported.
*/
async startScreencast(_options) {
throw new Error("Screencast not supported by this provider");
}
/**
* Check if a thread has an existing browser session.
* Used by startScreencastIfBrowserActive to prevent showing another thread's page.
*
* If threadManager is set, delegates to it. Otherwise returns true (no isolation).
* Subclasses can override for custom behavior.
*
* @returns true if session exists or thread isolation is not used
*/
hasThreadSession(threadId) {
if (!this.threadManager) return true;
if (this.threadManager.getScope() === "shared") return true;
return this.threadManager.hasSession(threadId);
}
/**
* Close a specific thread's browser session.
* Delegates to ThreadManager and notifies registered callbacks.
*
* For 'thread' scope, this closes only that thread's browser instance.
* For 'shared' scope, this is a no-op (use close() to close the shared browser).
*
* @param threadId - The thread ID whose session should be closed
*/
async closeThreadSession(threadId) {
if (this.getScope() === "thread") this.dropQueuedCookies(threadId);
if (!this.threadManager) return;
await this.threadManager.destroySession(threadId);
const pid = this.threadBrowserPids.get(threadId);
killProcessGroup(pid, this.logger);
this.threadBrowserPids.delete(threadId);
this.notifyBrowserClosed(threadId);
try {
await this.releaseProfileLockAfterClose([pid]);
} catch (err) {
this.logger.debug?.(`Failed to release profile lock: ${err}`);
}
}
/**
* Handle browser disconnection for a specific thread.
* Called when a thread's browser is closed externally (e.g., user closes browser window).
* Clears the thread session and notifies registered callbacks.
*
* @param threadId - The thread ID whose session was disconnected
*/
handleThreadBrowserDisconnected(threadId) {
if (!this.threadManager) return;
const pid = this.threadBrowserPids.get(threadId);
killProcessGroup(pid, this.logger);
this.threadBrowserPids.delete(threadId);
this.threadManager.clearSession(threadId);
this.logger.debug?.(`Cleared browser session for thread: ${threadId}`);
this.notifyBrowserClosed(threadId);
this.releaseProfileLockAfterClose([pid]).catch((err) => {
this.logger.debug?.(`Failed to release profile lock: ${err}`);
});
}
/**
* Get a session identifier for a specific thread.
* In thread scope, returns a composite ID (browser:threadId).
* In shared scope or without thread manager, returns the browser instance ID.
*/
getSessionId(threadId) {
if (!threadId || !this.threadManager) return this.id;
if (this.threadManager.getScope() === "shared") return this.id;
return `${this.id}:${threadId}`;
}
/**
* Start screencast only if browser is already running.
* Does NOT launch the browser.
* Uses config.screencast options as defaults if no options provided.
*
* For thread-isolated browsers ('browser' mode):
* - Returns null if the thread doesn't have an existing browser session
*/
async startScreencastIfBrowserActive(options) {
const mergedOptions = this.config.screencast || options ? {
...this.config.screencast,
...options
} : void 0;
const threadId = mergedOptions?.threadId;
const scope = this.threadManager?.getScope() ?? this.config.scope ?? "shared";
if (!this.isBrowserRunning(threadId)) return null;
if (scope === "shared") return this.startScreencast(mergedOptions);
if (threadId && !this.hasThreadSession(threadId)) {
console.log(`[MastraBrowser] startScreencastIfBrowserActive: hasThreadSession(${threadId})=false, scope=${scope}`);
return null;
}
return this.startScreencast(mergedOptions);
}
/**
* Inject a mouse event. Override in subclass if supported.
* @param event - Mouse event parameters
* @param threadId - Optional thread ID for thread-isolated sessions
*/
async injectMouseEvent(_event, _threadId) {
throw new Error("Mouse event injection not supported by this provider");
}
/**
* Inject a keyboard event. Override in subclass if supported.
* @param event - Keyboard event parameters
* @param threadId - Optional thread ID for thread-isolated sessions
*/
async injectKeyboardEvent(_event, _threadId) {
throw new Error("Keyboard event injection not supported by this provider");
}
/** Cookies waiting for a browser to open, by thread (one shared entry with `scope: 'shared'`). */
pendingCookies = /* @__PURE__ */ new Map();
/** The last queued cookie update per queue key, so updates run one at a time. */
cookieQueueUpdates = /* @__PURE__ */ new Map();
/** Bumped when a queue is dropped, so an update already under way can't queue its cookies again. */
cookieQueueGenerations = /* @__PURE__ */ new Map();
dropQueuedCookies(key) {
this.pendingCookies.delete(key);
if (this.cookieQueueUpdates.has(key)) this.cookieQueueGenerations.set(key, (this.cookieQueueGenerations.get(key) ?? 0) + 1);
else this.cookieQueueGenerations.delete(key);
}
/** Add cookies to the open browser. If it rejects them, they leave the queue, so they can't fail every later call. */
async addOrForget(cookies, key, threadId) {
try {
return await this.addCookiesToOpenBrowser(cookies, threadId);
} catch (error) {
this.pendingCookies.delete(key);
throw error;
}
}
/** Run cookie updates for one queue key in call order, so overlapping calls don't lose cookies. */
updateCookieQueue(key, update) {
const result = (this.cookieQueueUpdates.get(key) ?? Promise.resolve()).then(update, update);
const settled = result.catch(() => {});
this.cookieQueueUpdates.set(key, settled);
settled.then(() => {
if (this.cookieQueueUpdates.get(key) === settled) this.cookieQueueUpdates.delete(key);
});
return result;
}
/** With `scope: 'shared'` every thread uses one browser, so they share one queue. */
cookieQueueKey(threadId) {
return this.getScope() === "shared" ? DEFAULT_THREAD_ID : threadId;
}
/**
* Add cookies to a thread's browser, so its pages can open a site your app
* signed in to some other way. Cookies set before the browser opens are
* added when it does. With `scope: 'shared'` every thread uses one browser,
* so the cookies reach every thread.
*
* @param cookies - Cookies to add
* @param threadId - Optional thread ID (defaults to the current thread)
* @throws If this provider can't set cookies
*/
async setCookies(cookies, threadId) {
for (const cookie of cookies) if (!cookie.url && !(cookie.domain && cookie.path)) throw new Error(`Cookie "${cookie.name}" needs a url, or a domain and a path`);
const effectiveThreadId = threadId ?? this.getCurrentThread();
const key = this.cookieQueueKey(effectiveThreadId);
const generation = this.cookieQueueGenerations.get(key) ?? 0;
const dropped = () => (this.cookieQueueGenerations.get(key) ?? 0) !== generation;
await this.updateCookieQueue(key, async () => {
if (dropped()) return;
const all = [...this.pendingCookies.get(key) ?? [], ...cookies];
if (await this.addOrForget(all, key, effectiveThreadId)) {
this.pendingCookies.delete(key);
return;
}
if (!dropped()) this.pendingCookies.set(key, all);
});
}
/**
* Add cookies to the thread's browser if it's open. Override to support
* {@link setCookies}, and call {@link addPendingCookies} once a thread's
* browser opens, before it loads a page.
*
* @returns false when the thread has no open browser yet
*/
async addCookiesToOpenBrowser(_cookies, _threadId) {
throw new Error(`${this.provider} does not support setting cookies`);
}
/** Add the cookies {@link setCookies} queued while the thread's browser wasn't open. */
async addPendingCookies(threadId) {
const key = this.cookieQueueKey(threadId);
await this.updateCookieQueue(key, async () => {
const pending = this.pendingCookies.get(key);
if (!pending) return;
if (await this.addOrForget(pending, key, threadId)) this.pendingCookies.delete(key);
});
}
/**
* Returns browser input processors (e.g., BrowserContextProcessor for context injection).
* Skips if the user already added a processor with the same id.
*
* This method is similar to AgentChannels.getInputProcessors() and allows
* browser implementations to provide their own processors.
*
* @param configuredProcessors - Processors already configured by the user (for deduplication)
* @returns Array of input processors for this browser instance
*/
getInputProcessors(configuredProcessors = [], options = {}) {
if (configuredProcessors.some((p) => !require_stream.isProcessorWorkflow(p) && "id" in p && p.id === "browser-context")) return [];
return [new BrowserContextProcessor(options)];
}
};
// src/browser/screencast/types.ts
/**
* Default screencast options.
*/
const SCREENCAST_DEFAULTS = {
format: "jpeg",
quality: 80,
maxWidth: 1280,
maxHeight: 720,
everyNthFrame: 1
};
// src/browser/screencast/screencast-stream.ts
/**
* CDP-based ScreencastStream implementation.
*
* This provides a unified screencast implementation that works with any
* CDP session provider (Playwright, Puppeteer, direct CDP, etc.).
*/
/**
* ScreencastStream wraps CDP screencast with an event emitter interface.
*
* Works with any CDP session provider (Playwright, Puppeteer, direct CDP).
*
* @example
* ```typescript
* const stream = new ScreencastStream(cdpProvider, { quality: 80 });
* stream.on('frame', (frame) => {
* console.log(`Frame: ${frame.viewport.width}x${frame.viewport.height}`);
* });
* await stream.start();
* // Later...
* await stream.stop();
* ```
*/
var ScreencastStream = class extends events.EventEmitter {
/** Whether screencast is currently active */
active = false;
/** Resolved options with defaults applied (excludes threadId which is only used for page selection) */
options;
/** CDP session provider */
provider;
/** Current CDP session */
cdpSession = null;
/** Frame handler reference (for cleanup) */
frameHandler = null;
/**
* Creates a new ScreencastStream.
*
* @param provider - CDP session provider (browser instance)
* @param options - Screencast configuration options
*/
constructor(provider, options) {
super();
this.provider = provider;
const { threadId: _, ...cdpOptions } = options ?? {};
this.options = {
...SCREENCAST_DEFAULTS,
...cdpOptions
};
}
/**
* Start the screencast.
* If already active, returns immediately.
*/
async start() {
if (this.active) return;
if (!this.provider.isBrowserRunning()) throw new Error("Browser is not running");
try {
this.cdpSession = await this.provider.getCdpSession();
this.frameHandler = (params) => {
const frameData = {
data: params.data,
timestamp: params.metadata?.timestamp ? params.metadata.timestamp * 1e3 : Date.now(),
viewport: {
width: params.metadata?.deviceWidth ?? 0,
height: params.metadata?.deviceHeight ?? 0,
offsetTop: params.metadata?.offsetTop,
scrollOffsetX: params.metadata?.scrollOffsetX,
scrollOffsetY: params.metadata?.scrollOffsetY,
pageScaleFactor: params.metadata?.pageScaleFactor
},
sessionId: params.sessionId
};
this.emit("frame", frameData);
this.acknowledgeFrame(params.sessionId);
};
this.cdpSession.on("Page.screencastFrame", this.frameHandler);
try {
await this.cdpSession.send("Page.startScreencast", {
format: this.options.format,
quality: this.options.quality,
maxWidth: this.options.maxWidth,
maxHeight: this.options.maxHeight,
everyNthFrame: this.options.everyNthFrame
});
} catch (startError) {
if (this.cdpSession?.off) try {
this.cdpSession.off("Page.screencastFrame", this.frameHandler);
} catch {}
this.frameHandler = null;
this.cdpSession = null;
throw startError;
}
this.active = true;
} catch (error) {
const err = error instanceof Error ? error : new Error(String(error));
this.emit("error", err);
throw err;
}
}
/**
* Acknowledge a frame to CDP (required to continue receiving frames).
*/
acknowledgeFrame(sessionId) {
if (!this.cdpSession) return;
this.cdpSession.send("Page.screencastFrameAck", { sessionId }).catch(() => {});
}
/**
* Stop the screencast and release resources.
* Safe to call even if browser/CDP session is already closed.
*/
async stop() {
if (!this.active) return;
this.active = false;
let hadError = false;
if (this.cdpSession && this.frameHandler && this.cdpSession.off) try {
this.cdpSession.off("Page.screencastFrame", this.frameHandler);
} catch {}
this.frameHandler = null;
if (this.cdpSession) {
try {
await this.cdpSession.send("Page.stopScreencast");
} catch {
hadError = true;
}
this.cdpSession = null;
}
this.emit("stop", hadError ? "error" : "manual");
}
/**
* Check if screencast is currently active.
*/
isActive() {
return this.active;
}
/**
* Emit a URL update event.
* Browser providers call this when navigation is detected.
*/
emitUrl(url) {
this.emit("url", url);
}
/**
* Reconnect the screencast by stopping and restarting.
* Use this when the active page/tab changes.
*
* @returns Promise that resolves when reconnection is complete
* @throws Error if reconnection fails (also emits 'error' event)
*/
async reconnect() {
if (this.cdpSession && this.frameHandler && this.cdpSession.off) try {
this.cdpSession.off("Page.screencastFrame", this.frameHandler);
} catch {}
this.frameHandler = null;
if (this.cdpSession) {
try {
await this.cdpSession.send("Page.stopScreencast");
} catch {}
this.cdpSession = null;
}
this.active = false;
try {
await this.start();
} catch (error) {
const err = error instanceof Error ? error : new Error(String(error));
console.error("[ScreencastStream.reconnect] Failed to reconnect:", err);
throw err;
}
}
};
// src/browser/recording/mjpeg-avi.ts
/**
* Pure-JS Motion-JPEG AVI muxer.
*
* Writes a list of JPEG frames into an AVI 1.0 container so that the resulting
* `.avi` file plays in QuickTime / Preview / VLC / Chromium without depending
* on ffmpeg or any native module.
*
* Implements the bare minimum of the RIFF/AVI spec needed for an MJPEG video
* stream with no audio. All fields are little-endian.
*
* Reference: https://learn.microsoft.com/en-us/windows/win32/directshow/avi-riff-file-reference
*/
const FOURCC_RIFF = fourcc("RIFF");
const FOURCC_AVI = fourcc("AVI ");
const FOURCC_LIST = fourcc("LIST");
const FOURCC_HDRL = fourcc("hdrl");
const FOURCC_AVIH = fourcc("avih");
const FOURCC_STRL = fourcc("strl");
const FOURCC_STRH = fourcc("strh");
const FOURCC_STRF = fourcc("strf");
const FOURCC_VIDS = fourcc("vids");
const FOURCC_MJPG = fourcc("MJPG");
const FOURCC_MOVI = fourcc("movi");
const FOURCC_IDX1 = fourcc("idx1");
const FOURCC_00DC = fourcc("00dc");
const AVIF_HASINDEX = 16;
const AVIIF_KEYFRAME = 16;
const AVIH_SIZE = 56;
const STRH_SIZE = 56;
const STRF_SIZE = 40;
const LIST_TYPE_SIZE = 4;
const CHUNK_HEADER_SIZE = 8;
const MAX_U32 = 4294967295;
const MAX_I16 = 32767;
function fourcc(s) {
if (s.length !== 4) throw new Error(`fourcc must be 4 ASCII characters, got "${s}"`);
return Buffer.from(s, "ascii");
}
/**
* Encode a list of MJPEG frames as an AVI 1.0 file.
*
* The file is written incrementally to disk so we don't have to hold the whole
* AVI in memory — large recordings can be hundreds of MB.
*/
function writeMjpegAviFile(filePath, frames, opts) {
if (frames.length === 0) throw new Error("writeMjpegAviFile: at least one frame is required");
if (opts.width <= 0 || opts.height <= 0 || !Number.isInteger(opts.width) || !Number.isInteger(opts.height)) throw new Error(`writeMjpegAviFile: invalid dimensions ${opts.width}x${opts.height}`);
if (opts.width > MAX_I16 || opts.height > MAX_I16) throw new Error(`writeMjpegAviFile: dimensions exceed AVI header bounds: ${opts.width}x${opts.height}`);
for (let i = 0; i < frames.length; i++) assertJpegFrame(frames[i].bytes, i);
(0, fs.mkdirSync)((0, path.dirname)(filePath), { recursive: true });
const elapsedMs = frames.length > 1 ? Math.max(1, frames[frames.length - 1].timestampMs - frames[0].timestampMs) : 1e3;
const fps = Math.max(1, Math.min(120, Math.round(frames.length * 1e3 / elapsedMs)));
const microSecPerFrame = Math.round(1e6 / fps);
const maxFrameLen = frames.reduce((m, f) => Math.max(m, f.bytes.length), 0);
assertU32(maxFrameLen, "max frame length");
const totalFrameBytes = frames.reduce((sum, f) => sum + CHUNK_HEADER_SIZE + paddedLength(f.bytes.length), 0);
assertU32(totalFrameBytes, "total frame bytes");
const hdrl = buildHdrl({
width: opts.width,
height: opts.height,
microSecPerFrame,
totalFrames: frames.length,
maxFrameLen
});
const moviPayloadSize = LIST_TYPE_SIZE + totalFrameBytes;
assertU32(moviPayloadSize, "movi list size");
const idx1PayloadSize = frames.length * 16;
assertU32(idx1PayloadSize, "idx1 chunk size");
const riffPayloadSize = 12 + hdrl.length + CHUNK_HEADER_SIZE + moviPayloadSize + CHUNK_HEADER_SIZE + idx1PayloadSize;
assertU32(riffPayloadSize, "RIFF payload size");
const fd = (0, fs.openSync)(filePath, "w");
try {
writeFourCC(fd, FOURCC_RIFF);
writeU32(fd, riffPayloadSize);
writeFourCC(fd, FOURCC_AVI);
writeFourCC(fd, FOURCC_LIST);
writeU32(fd, hdrl.length);
writeBuffer(fd, hdrl);
writeFourCC(fd, FOURCC_LIST);
writeU32(fd, moviPayloadSize);
writeFourCC(fd, FOURCC_MOVI);
const moviStartOffset = currentOffset(fd) - LIST_TYPE_SIZE;
const frameOffsets = [];
for (const frame of frames) {
const offset = currentOffset(fd) - moviStartOffset;
frameOffsets.push(offset);
writeFourCC(fd, FOURCC_00DC);
writeU32(fd, frame.bytes.length);
writeBuffer(fd, Buffer.from(frame.bytes.buffer, frame.bytes.byteOffset, frame.bytes.byteLength));
if (frame.bytes.length % 2 === 1) writeBuffer(fd, Buffer.from([0]));
}
writeFourCC(fd, FOURCC_IDX1);
writeU32(fd, idx1PayloadSize);
const idx1 = Buffer.alloc(idx1PayloadSize);
for (let i = 0; i < frames.length; i++) {
const base = i * 16;
FOURCC_00DC.copy(idx1, base);
idx1.writeUInt32LE(AVIIF_KEYFRAME, base + 4);
idx1.writeUInt32LE(frameOffsets[i], base + 8);
idx1.writeUInt32LE(frames[i].bytes.length, base + 12);
}
writeBuffer(fd, idx1);
} finally {
(0, fs.closeSync)(fd);
}
}
/** Build the contents of the hdrl LIST (without the leading LIST/size/hdrl tag). */
function buildHdrl(args) {
const { width, height, microSecPerFrame, totalFrames, maxFrameLen } = args;
assertU32(microSecPerFrame, "microseconds per frame");
assertU32(totalFrames, "total frames");
assertU32(maxFrameLen, "max frame length");
const maxBytesPerSec = Math.round(maxFrameLen * 1e6 / microSecPerFrame);
assertU32(maxBytesPerSec, "max bytes per second");
const sizeImage = width * height * 3;
assertU32(sizeImage, "bitmap image size");
const strlPayloadSize = 116;
const hdrlPayloadSize = 192;
const buf = Buffer.alloc(hdrlPayloadSize);
let p = 0;
FOURCC_HDRL.copy(buf, p);
p += 4;
FOURCC_AVIH.copy(buf, p);
p += 4;
buf.writeUInt32LE(AVIH_SIZE, p);
p += 4;
buf.writeUInt32LE(microSecPerFrame, p);
p += 4;
buf.writeUInt32LE(maxBytesPerSec, p);
p += 4;
buf.writeUInt32LE(0, p);
p += 4;
buf.writeUInt32LE(AVIF_HASINDEX, p);
p += 4;
buf.writeUInt32LE(totalFrames, p);
p += 4;
buf.writeUInt32LE(0, p);
p += 4;
buf.writeUInt32LE(1, p);
p += 4;
buf.writeUInt32LE(maxFrameLen, p);
p += 4;
buf.writeUInt32LE(width, p);
p += 4;
buf.writeUInt32LE(height, p);
p += 4;
p += 16;
FOURCC_LIST.copy(buf, p);
p += 4;
buf.writeUInt32LE(strlPayloadSize, p);
p += 4;
FOURCC_STRL.copy(buf, p);
p += 4;
FOURCC_STRH.copy(buf, p);
p += 4;
buf.writeUInt32LE(STRH_SIZE, p);
p += 4;
FOURCC_VIDS.copy(buf, p);
p += 4;
FOURCC_MJPG.copy(buf, p);
p += 4;
buf.writeUInt32LE(0, p);
p += 4;
buf.writeUInt16LE(0, p);
p += 2;
buf.writeUInt16LE(0, p);
p += 2;
buf.writeUInt32LE(0, p);
p += 4;
buf.writeUInt32LE(microSecPerFrame, p);
p += 4;
buf.writeUInt32LE(1e6, p);
p += 4;
buf.writeUInt32LE(0, p);
p += 4;
buf.writeUInt32LE(totalFrames, p);
p += 4;
buf.writeUInt32LE(maxFrameLen, p);
p += 4;
buf.writeInt32LE(-1, p);
p += 4;
buf.writeUInt32LE(0, p);
p += 4;
buf.writeInt16LE(0, p);
p += 2;
buf.writeInt16LE(0, p);
p += 2;
buf.writeInt16LE(width, p);
p += 2;
buf.writeInt16LE(height, p);
p += 2;
FOURCC_STRF.copy(buf, p);
p += 4;
buf.writeUInt32LE(STRF_SIZE, p);
p += 4;
buf.writeUInt32LE(STRF_SIZE, p);
p += 4;
buf.writeInt32LE(width, p);
p += 4;
buf.writeInt32LE(height, p);
p += 4;
buf.writeUInt16LE(1, p);
p += 2;
buf.writeUInt16LE(24, p);
p += 2;
FOURCC_MJPG.copy(buf, p);
p += 4;
buf.writeUInt32LE(sizeImage, p);
p += 4;
buf.writeInt32LE(0, p);
p += 4;
buf.writeInt32LE(0, p);
p += 4;
buf.writeUInt32LE(0, p);
p += 4;
buf.writeUInt32LE(0, p);
p += 4;
if (p !== hdrlPayloadSize) throw new Error(`buildHdrl internal error: wrote ${p} bytes, expected ${hdrlPayloadSize}`);
return buf;
}
function paddedLength(n) {
return n + (n & 1);
}
function assertJpegFrame(bytes, index) {
if (bytes.length < 2 || bytes[0] !== 255 || bytes[1] !== 216) throw new Error(`writeMjpegAviFile: frame ${index} is not a JPEG frame (missing SOI marker)`);
}
function assertU32(value, field) {
if (!Number.isSafeInteger(value) || value < 0 || value > MAX_U32) throw new Error(`writeMjpegAviFile: ${field} exceeds 32-bit AVI limit (${value})`);
}
const u32buf = Buffer.alloc(4);
function writeFourCC(fd, fc) {
(0, fs.writeSync)(fd, fc, 0, 4);
}
function writeU32(fd, value) {
assertU32(value, "uint32 field");
u32buf.writeUInt32LE(value, 0);
(0, fs.writeSync)(fd, u32buf, 0, 4);
}
function writeBuffer(fd, buf) {
(0, fs.writeSync)(fd, buf, 0, buf.length);
}
function currentOffset(fd) {
return (0, fs.fstatSync)(fd).size;
}
// src/browser/recording/overlay.ts
/**
* Pure functions for caption overlay rendering on screencast frames.
*
* Kept separate from browser-recording.ts so the lifecycle/tool code stays
* focused and the pixel-level logic is easy to test.
*/
/**
* Pick the caption that should be visible at a given timestamp.
*
* If multiple captions overlap (a new caption starts before the previous
* expires), the newer one wins.
*
* Returns undefined when no caption is active at `t`.
*/
function selectCaptionAt(captions, t) {
let active;
for (const c of captions) if (t >= c.timestampMs && t < c.timestampMs + c.durationMs) {
if (!active || c.timestampMs > active.timestampMs) active = c;
}
return active;
}
/**
* Decode a JPEG buffer into an RGBA pixel array.
* Throws when the JPEG cannot be parsed.
*/
function decodeJpeg(jpegBytes) {
const decoded = (0, jpeg_js.decode)(jpegBytes, {
useTArray: true,
formatAsRGBA: true
});
return {
width: decoded.width,
height: decoded.height,
data: new Uint8ClampedArray(decoded.data.buffer, decoded.data.byteOffset, decoded.data.byteLength)
};
}
/**
* Minimal 5x7 bitmap font covering printable ASCII (0x20..0x7E).
* Each glyph is 5 columns × 7 rows, encoded as 7 bytes (one per row).
* Bit 4 (0x10) of each row byte is the leftmost pixel.
*
* This is intentionally tiny so we don't need a TrueType/system font.
*/
const FONT_5x7 = {
" ": [
0,
0,
0,
0,
0,
0,
0
],
"!": [
4,
4,
4,
4,
0,
0,
4
],
"\"": [
10,
10,
0,
0,
0,
0,
0
],
"#": [
10,
31,
10,
31,
10,
0,
0
],
$: [
4,
15,
20,
14,
5,
30,
4
],
"%": [
25,
25,
2,
4,
8,
19,
19
],
"&": [
8,
20,
20,
8,
21,
18,
13
],
"'": [
4,
4,
0,
0,
0,
0,
0
],
"(": [
2,
4,
8,
8,
8,
4,
2
],
")": [
8,
4,
2,
2,
2,
4,
8
],
"*": [
0,
4,
21,
14,
21,
4,
0
],
"+": [
0,
4,
4,
31,
4,
4,
0
],
",": [
0,
0,
0,
0,
0,
4,
8
],
"-": [
0,
0,
0,
31,
0,
0,
0
],
".": [
0,
0,
0,
0,
0,
0,
4
],
"/": [
0,
1,
2,
4,
8,
16,
0
],
"0": [
14,
17,
19,
21,
25,
17,
14
],
"1": [
4,
12,
4,
4,
4,
4,
14
],
"2": [
14,
17,
1,
2,
4,
8,
31
],
"3": [
31,
2,
4,
2,
1,
17,
14
],
"4": [
2,
6,
10,
18,
31,
2,
2
],
"5": [
31,
16,
30,
1,
1,
17,
14
],
"6": [
6,
8,
16,
30,
17,
17,
14
],
"7": [
31,
1,
2,
4,
8,
8,
8
],
"8": [
14,
17,
17,
14,
17,
17,
14
],
"9": [
14,
17,
17,
15,
1,
2,
12
],
":": [
0,
0,
4,
0,
0,
4,
0
],
";": [
0,
0,
4,
0,
0,
4,
8
],
"<": [
2,
4,
8,
16,
8,
4,
2
],
"=": [
0,
0,
31,
0,
31,
0,
0
],
">": [
8,
4,
2,
1,
2,
4,
8
],
"?": [
14,
17,
1,
2,
4,
0,
4
],
"@": [
14,
17,
23,
21,
23,
16,
14
],
A: [
14,
17,
17,
31,
17,
17,
17
],
B: [
30,
17,
17,
30,
17,
17,
30
],
C: [
14,
17,
16,
16,
16,
17,
14
],
D: [
28,
18,
17,
17,
17,
18,
28
],
E: [
31,
16,
16,
30,
16,
16,
31
],
F: [
31,
16,
16,
30,
16,
16,
16
],
G: [
14,
17,
16,
23,
17,
17,
15
],
H: [
17,
17,
17,
31,
17,
17,
17
],
I: [
14,
4,
4,
4,
4,
4,
14
],
J: [
7,
2,
2,
2,
2,
18,
12
],
K: [
17,
18,
20,
24,
20,
18,
17
],
L: [
16,
16,
16,
16,
16,
16,
31
],
M: [
17,
27,
21,
21,
17,
17,
17
],
N: [
17,
17,
25,
21,
19,
17,
17
],
O: [
14,
17,
17,
17,
17,
17,
14
],
P: [
30,
17,
17,
30,
16,
16,
16
],
Q: [
14,
17,
17,
17,
21,
18,
13
],
R: [
30,
17,
17,
30,
20,
18,
17
],
S: [
14,
17,
16,
14,
1,
17,
14
],
T: [
31,
4,
4,
4,
4,
4,
4
],
U: [
17,
17,
17,
17,
17,
17,
14
],
V: [
17,
17,
17,
17,
17,
10,
4
],
W: [
17,
17,
17,
21,
21,
21,
10
],
X: [
17,
17,
10,
4,
10,
17,
17
],
Y: [
17,
17,
17,
10,
4,
4,
4
],
Z: [
31,
1,
2,
4,
8,
16,
31
],
"[": [
14,
8,
8,
8,
8,
8,
14
],
"\\": [
0,
16,
8,
4,
2,
1,
0
],
"]": [
14,
2,
2,
2,
2,
2,
14
],
"^": [
4,
10,
17,
0,
0,
0,
0
],
_: [
0,
0,
0,
0,
0,
0,
31
],
"`": [
8,
4,
0,
0,
0,
0,
0
],
a: [
0,
0,
14,
1,
15,
17,
15
],
b: [
16,
16,
22,
25,
17,
17,
30
],
c: [
0,
0,
14,
17,
16,
17,
14
],
d: [
1,
1,
13,
19,
17,
17,
15
],
e: [
0,
0,
14,
17,
31,
16,
14
],
f: [
2,
5,
4,
14,
4,
4,
4
],
g: [
0,
0,
15,
17,
15,
1,
14
],
h: [
16,
16,
22,
25,
17,
17,
17
],
i: [
4,
0,
12,
4,
4,
4,
14
],
j: [
2,
0,
6,
2,
2,
18,
12
],
k: [
16,
16,
18,
20,
24,
20,
18
],
l: [
12,
4,
4,
4,
4,
4,
14
],
m: [
0,
0,
26,
21,
21,
17,
17
],
n: [
0,
0,
22,
25,
17,
17,
17
],
o: [
0,
0,
14,
17,
17,
17,
14
],
p: [
0,
0,
30,
17,
30,
16,
16
],
q: [
0,
0,
13,
19,
15,
1,
1
],
r: [
0,
0,
22,
25,
16,
16,
16
],
s: [
0,
0,
14,
16,
14,
1,
30
],
t: [
4,
4,
14,
4,
4,
5,
2
],
u: [
0,
0,
17,
17,
17,
19,
13
],
v: [
0,
0,
17,
17,
17,
10,
4
],
w: [
0,
0,
17,
17,
21,
21,
10
],
x: [
0,
0,
17,
10,
4,
10,
17
],
y: [
0,
0,
17,
17,
15,
1,
14
],
z: [
0,
0,
31,
2,
4,
8,
31
],
"{": [
2,
4,
4,
8,
4,
4,
2
],
"|": [
4,
4,
4,
4,
4,
4,
4
],
"}": [
8,
4,
4,
2,
4,
4,
8
],
"~": [
0,
0,
8,
21,
2,
0,
0
]
};
const FONT_WIDTH = 5;
const FONT_HEIGHT = 7;
const FONT_GAP = 1;
/**
* Replace any unsupported character with `?` so we never crash on emoji
* or non-ASCII input. Captions are intentionally short ASCII-ish text.
*/
function safeChar(c) {
return Object.prototype.hasOwnProperty.call(FONT_5x7, c) ? c : "?";
}
/** Pixel width of a string at the given scale. */
function measureText(text, scale) {
const codePoints = Array.from(text);
if (codePoints.length === 0) return 0;
return codePoints.length * FONT_WIDTH * scale + (codePoints.length - 1) * FONT_GAP * scale;
}
function drawText(frame, text, opts) {
const { width, height, data } = frame;
const { color, scale } = opts;
let cursorX = opts.x;
const baseY = opts.y;
for (const rawChar of text) {
const ch = safeChar(rawChar);
const glyph = FONT_5x7[ch];
for (let row = 0; row < FONT_HEIGHT; row++) {
const bits = glyph[row];
for (let col = 0; col < FONT_WIDTH; col++) if (bits >> 4 - col & 1) for (let dy = 0; dy < scale; dy++) for (let dx = 0; dx < scale; dx++) {
const px = cursorX + col * scale + dx;
const py = baseY + row * scale + dy;
if (px < 0 || px >= width || py < 0 || py >= height) continue;
const idx = (py * width + px) * 4;
data[idx] = color[0];
data[idx + 1] = color[1];
data[idx + 2] = color[2];
data[idx + 3] = 255;
}
}
cursorX += 6 * scale;
}
}
const DEFAULT_OPTS = {
fontScale: 2,
padding: 6,
marginBottom: 8,
backgroundColor: [
0,
0,
0
],
backgroundAlpha: 180,
textColor: [
255,
255,
255
]
};
/**
* Draw a caption at the bottom of the frame in-place.
*
* Renders a semi-transparent dark strip with the text centered on top.
* Long captions are truncated with an ellipsis to fit the frame width.
*/
function drawCaptionOnFrame(frame, caption, options = {}) {
const opts = {
...DEFAULT_OPTS,
...options
};
const { width, height, data } = frame;
if (width <= 0 || height <= 0) return;
if (!caption || caption.trim().length === 0) return;
const scale = Math.max(1, Math.floor(opts.fontScale));
const textHeight = FONT_HEIGHT * scale;
const stripHeight = textHeight + opts.padding * 2;
const stripTop = Math.max(0, height - stripHeight - opts.marginBottom);
const stripBottom = Math.min(height, stripTop + stripHeight);
if (stripTop >= stripBottom) return;
const maxTextWidth = Math.max(0, width - 16);
let text = caption;
if (measureText(text, scale) > maxTextWidth) {
const ellipsis = "...";
const codePoints = Array.from(text);
while (codePoints.length > 0 && measureText(codePoints.join("") + ellipsis, scale) > maxTextWidth) codePoints.pop();
text = codePoints.join("") + ellipsis;
}
const [br, bg, bb] = opts.backgroundColor;
const alpha = Math.min(255, Math.max(0, opts.backgroundAlpha)) / 255;
for (let y = stripTop; y < stripBottom; y++) for (let x = 0; x < width; x++) {
const idx = (y * width + x) * 4;
const r = data[idx];
const g = data[idx + 1];
const b = data[idx + 2];
data[idx] = Math.round(r * (1 - alpha) + br * alpha);
data[idx + 1] = Math.round(g * (1 - alpha) + bg * alpha);
data[idx + 2] = Math.round(b * (1 - alpha) + bb * alpha);
data[idx + 3] = 255;
}
const textWidth = measureText(text, scale);
const textX = Math.max(0, Math.floor((width - textWidth) / 2));
const textY = stripTop + Math.floor((stripHeight - textHeight) / 2);
drawText(frame, text, {
color: opts.textColor,
scale,
x: textX,
y: textY
});
}
/**
* Encode an RGBA frame back into JPEG bytes. Used by the recording pipeline
* when re-encoding a frame after burning a caption onto it.
*/
function encodeJpeg(frame, quality = 80) {
const encoded = (0, jpeg_js.encode)({
data: Buffer.from(frame.data.buffer, frame.data.byteOffset, frame.data.byteLength),
width: frame.width,
height: frame.height
}, quality);
return new Uint8Array(encoded.data.buffer, encoded.data.byteOffset, encoded.data.byteLength);
}
// src/browser/recording/tools.ts
/**
* Opt-in browser recording tools.
*
* Wraps the existing browser screencast infrastructure to capture frames, lets
* the agent drop short captions at specific moments, and encodes the result as
* a Motion-JPEG AVI video written to disk. The AVI format is used because it
* can be muxed in pure JavaScript (no ffmpeg) and plays natively in QuickTime /
* Preview / VLC / browsers.
*/
const DEFAULT_MAX_DURATION_MS = 3e4;
const HARD_CAP_MAX_DURATION_MS = 12e4;
const DEFAULT_EVERY_NTH_FRAME = 2;
const DEFAULT_MAX_WIDTH = 1024;
const DEFAULT_MAX_HEIGHT = 720;
const MAX_CAPTION_LENGTH = 80;
const DEFAULT_CAPTION_DURATION_MS = 2500;
const MIN_CAPTION_SPACING_MS = 500;
const MAX_BUFFERED_FRAMES = 2e3;
const FRAME_STALL_MS = 4e3;
const WATCHDOG_INTERVAL_MS = 1e3;
/**
* Module-local state. Only one recording at a time per process — this is
* intentional and is enforced by the browser_record tool.
*/
let active = null;
function generateRecordingId() {
return `rec_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 8)}`;
}
function recordingsDir(outputDir) {
return (0, path.resolve)(outputDir);
}
function defaultOutputPath(id, outputDir) {
return (0, path.join)(recordingsDir(outputDir), `${id}.avi`);
}
function resolveOutputPath(id, outputDir, requestedPath) {
const baseDir = recordingsDir(outputDir);
const outputPath = requestedPath ? (0, path.resolve)(requestedPath) : defaultOutputPath(id, outputDir);
const rel = (0, path.relative)(baseDir, outputPath);
if (rel === "" || (0, path.isAbsolute)(rel) || rel === ".." || rel.startsWith(`..${path.sep}`)) throw new Error(`Recording outputPath must be inside ${baseDir}`);
return outputPath;
}
function clearState() {
if (active?.autoStopTimer) clearTimeout(active.autoStopTimer);
if (active?.watchdogTimer) clearInterval(active.watchdogTimer);
active = null;
}
/** Internal: stop the screencast, ignoring errors. */
async function safeStop(stream) {
try {
await stream.stop();
} catch {}
}
/**
* Produce one captioned JPEG frame ready to be muxed into the AVI container.
*
* If there's no active caption at this frame's timestamp the original JPEG
* bytes are returned as-is — re-encoding is only needed when a caption needs
* to be burned onto the frame.
*/
function buildCaptionedFrame(state, frame) {
const caption = selectCaptionAt(state.captions, frame.timestampMs);
if (!caption) return {
bytes: frame.bytes,
timestampMs: frame.timestampMs
};
const rgba = decodeJpeg(frame.bytes);
drawCaptionOnFrame(rgba, caption.text);
return {
bytes: encodeJpeg(rgba, 80),
timestampMs: frame.timestampMs
};
}
/**
* Encode all buffered frames as an MJPEG AVI file written directly to disk.
*
* Returns the dimensions of the first frame (used to populate the AVI header).
* The AVI stream has one fixed frame size, so fail fast if the browser changes
* screencast dimensions mid-recording.
*/
function encodeFramesAsAvi(state, outputPath) {
if (state.frames.length === 0) throw new Error("No frames captured during recording");
const firstRgba = decodeJpeg(state.frames[0].bytes);
const width = firstRgba.width;
const height = firstRgba.height;
const muxFrames = [];
for (const frame of state.frames) {
const rgba = decodeJpeg(frame.bytes);
if (rgba.width !== width || rgba.height !== height) throw new Error(`Frame dimensions changed during recording: expected ${width}x${height}, got ${rgba.width}x${rgba.height}`);
const out = buildCaptionedFrame(state, frame);
muxFrames.push(out);
}
writeMjpegAviFile(outputPath, muxFrames, {
width,
height
});
return {
width,
height,
written: muxFrames.length
};
}
async function startRecording(browser, opts) {
if (active) throw new Error("A browser recording is already active. Call browser_record with action=\"stop\" before starting a new recording.");
const maxDurationMs = Math.min(HARD_CAP_MAX_DURATION_MS, Math.max(1e3, opts.maxDurationMs ?? DEFAULT_MAX_DURATION_MS));
const everyNthFrame = Math.max(1, Math.floor(opts.everyNthFrame ?? DEFAULT_EVERY_NTH_FRAME));
const maxWidth = Math.max(160, Math.floor(opts.maxWidth ?? DEFAULT_MAX_WIDTH));
const maxHeight = Math.max(120, Math.floor(opts.maxHeight ?? DEFAULT_MAX_HEIGHT));
const id = generateRecordingId();
const outputPath = resolveOutputPath(id, opts.outputDir, opts.outputPath);
let stream;
try {
browser.setCurrentThread(opts.threadId);
await browser.ensureReady();
stream = await browser.startScreencast({
format: "jpeg",
quality: 80,
maxWidth,
maxHeight,
everyNthFrame,
threadId: opts.threadId
});
} catch (error) {
const msg = error instanceof Error ? error.message : String(error);
throw new Error(`Failed to start browser recording: ${msg}`);
}
const startedAt = Date.now();
const state = {
id,
stream,
startedAt,
maxDurationMs,
outputPath,
frames: [],
captions: [],
autoStopTimer: null,
pendingError: null,
stopping: false,
lastCaptionAt: -Infinity,
lastFrameAt: startedAt,
watchdogTimer: null,
reconnecting: false
};
active = state;
stream.on("frame", (frame) => {
if (!active || active.id !== id || active.stopping) return;
active.lastFrameAt = Date.now();
if (active.frames.length >= MAX_BUFFERED_FRAMES) return;
try {
const buf = Buffer.from(frame.data, "base64");
active.frames.push({
timestampMs: Date.now() - active.startedAt,
bytes: new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength)
});
} catch (err) {
active.pendingError = err instanceof Error ? err : new Error(String(err));
}
});
stream.on("error", (err) => {
if (active && active.id === id) active.pendingError = err;
});
stream.on("stop", (reason) => {
if (active && active.id === id && reason !== "manual" && !active.stopping) active.pendingError = /* @__PURE__ */ new Error(`Browser screencast stopped unexpectedly (reason: ${reason})`);
});
state.autoStopTimer = setTimeout(() => {
if (active && active.id === id && !active.stopping) {
active.stopping = true;
safeStop(stream).catch(() => {});
}
}, maxDurationMs);
const watchdog = () => {
if (!active || active.id !== id || active.stopping) return;
if (Date.now() - active.lastFrameAt >= FRAME_STALL_MS && !active.reconnecting) {
active.reconnecting = true;
(async () => {
try {
browser.setCurrentThread(opts.threadId);
await stream.reconnect();
if (active && active.id === id) active.lastFrameAt = Date.now();
} catch (err) {
if (active && active.id === id) active.pendingError = err instanceof Error ? err : new Error(String(err));
} finally {
if (active && active.id === id) active.reconnecting = false;
}
})();
}
};
state.watchdogTimer = setInterval(watchdog, WATCHDOG_INTERVAL_MS);
return {
id,
outputPath,
maxDurationMs
};
}
async function stopRecording() {
if (!active) throw new Error("No browser recording is active. Call browser_record with action=\"start\" first.");
const state = active;
state.stopping = true;
if (state.autoStopTimer) {
clearTimeout(state.autoStopTimer);
state.autoStopTimer = null;
}
if (state.watchdogTimer) {
clearInterval(state.watchdogTimer);
state.watchdogTimer = null;
}
await safeStop(state.stream);
if (state.pendingError) {
const err = state.pendingError;
clearState();
throw new Error(`Browser recording failed: ${err.message}`);
}
const outputPath = state.outputPath;
try {
encodeFramesAsAvi(state, outputPath);
} catch (err) {
clearState();
const msg = err instanceof Error ? err.message : String(err);
throw new Error(`Failed to write recording to ${outputPath}: ${msg}`);
}
const result = {
filePath: outputPath,
frameCount: state.frames.length,
captionCount: state.captions.length,
durationMs: state.frames.length > 0 ? state.frames[state.frames.length - 1].timestampMs : Date.now() - state.startedAt
};
clearState();
return result;
}
function addCaption(text, durationMs) {
if (!active) throw new Error("No browser recording is active. Call browser_record with action=\"start\" before adding a caption.");
if (active.stopping) throw new Error("Recording is already stopping; cannot add more captions.");
const trimmed = text.trim();
if (trimmed.length === 0) throw new Error("Caption text must be a non-empty string.");
const capped = trimmed.length > MAX_CAPTION_LENGTH ? trimmed.slice(0, 79) + "…" : trimmed;
const now = Date.now();
const timestampMs = now - active.startedAt;
if (now - active.lastCaptionAt < MIN_CAPTION_SPACING_MS) {
const last = active.captions[active.captions.length - 1];
if (last) {
last.text = capped;
last.durationMs = Math.max(last.durationMs, Math.min(HARD_CAP_MAX_DURATION_MS, durationMs ?? DEFAULT_CAPTION_DURATION_MS));
active.lastCaptionAt = now;
return {
captionCount: active.captions.length,
timestampMs: last.timestampMs
};
}
}
active.captions.push({
timestampMs,
text: capped,
durationMs: Math.max(500, Math.min(HARD_CAP_MAX_DURATION_MS, durationMs ?? DEFAULT_CAPTION_DURATION_MS))
});
active.lastCaptionAt = now;
return {
captionCount: active.captions.length,
timestampMs
};
}
function getStatus() {
if (!active) return { active: false };
return {
active: true,
frameCount: active.frames.length,
captionCount: active.captions.length,
elapsedMs: Date.now() - active.startedAt,
maxDurationMs: active.maxDurationMs
};
}
const recordSchema = zod.z.object({
action: zod.z.enum([
"start",
"stop",
"status"
]).describe("Lifecycle action: \"start\" begins a new recording, \"stop\" finalizes it and writes the MJPEG .avi video to disk, \"status\" reports whether one is active."),
maxDurationMs: zod.z.number().int().positive().optional().describe("Only used with action=\"start\". Maximum recording duration in ms (default 30000, hard cap 120000)."),
everyNthFrame: zod.z.number().int().positive().optional().describe("Only used with action=\"start\". Capture every Nth screencast frame (default 2). Higher = smaller file."),
maxWidth: zod.z.number().int().positive().optional().describe("Only used with action=\"start\". Max frame width in pixels (default 1024)."),
maxHeight: zod.z.number().int().positive().optional().describe("Only used with action=\"start\". Max frame height in pixels (default 720)."),
outputPath: zod.z.string().min(1).optional().describe("Only used with action=\"start\". Absolute path inside the configured recording output directory. Defaults to a generated file there.")
});
const recordCaptionSchema = zod.z.object({
text: zod.z.string().min(1).max(MAX_CAPTION_LENGTH).describe("Very short caption (max ~6 words) describing what you just did in the browser."),
durationMs: zod.z.number().int().positive().optional().describe("How long the caption should remain visible in the video (ms). Default 2500ms.")
});
/**
* Create opt-in browser recording tools bound to a browser.
*
* These tools are alpha and are only exposed when a browser provider or caller
* explicitly enables recording and provides a safe output directory.
*/
function createBrowserRecordingTools(browser, options) {
const outputDir = recordingsDir(options.outputDir);
return {
browser_record: require_tool.createTool({
id: "browser_record",
description: "Control browser video recording. Use action=\"start\" to begin recording the current browser session, then add labels via browser_record_caption as you work, then call this tool again with action=\"stop\" to encode and save an MJPEG .avi video (plays in Preview/QuickTime/VLC/browsers). Use action=\"status\" to check whether a recording is currently active. Recording auto-stops at the duration cap.",
inputSchema: recordSchema,
execute: async (input, { agent }) => {
try {
if (input.action === "start") {
const { id, outputPath, maxDurationMs } = await startRecording(browser, {
maxDurationMs: options.maxDurationMs,
maxWidth: options.maxWidth,
maxHeight: options.maxHeight,
...input,
threadId: agent?.threadId,
outputDir
});
return {
content: `Recording started (id: ${id}). Will auto-stop after ${maxDurationMs}ms or when you call this tool with action="stop". Output: ${outputPath}`,
action: "start",
recordingId: id,
outputPath,
maxDurationMs,
isError: false
};
}
if (input.action === "stop") {
const result = await stopRecording();
return {
content: `Recording saved to ${result.filePath} (${result.frameCount} frames, ${result.captionCount} captions, ${result.durationMs}ms).`,
action: "stop",
...result,
isError: false
};
}
const status = getStatus();
return {
content: status.active ? `Recording active: ${status.frameCount} frames, ${status.captionCount} captions, ${status.elapsedMs}ms elapsed (cap ${status.maxDurationMs}ms).` : "No browser recording is active.",
action: "status",
...status,
isError: false
};
} catch (err) {
return {
content: err instanceof Error ? err.message : String(err),
isError: true
};
}
}
}),
browser_record_caption: require_tool.createTool({
id: "browser_record_caption",
description: "Add a very short, plain-language caption (max ~6 words) describing what you just did in the browser. The caption is burned onto the recorded video at the current moment. Only call this while a recording is active (see browser_record action=\"start\").",
inputSchema: recordCaptionSchema,
execute: async ({ text, durationMs }) => {
try {
const { captionCount, timestampMs } = addCaption(text, durationMs);
return {
content: `Caption added at ${timestampMs}ms (total: ${captionCount}).`,
captionCount,
timestampMs,
isError: false
};
} catch (err) {
return {
content: err instanceof Error ? err.message : String(err),
isError: true
};
}
}
})
};
}
exports.BrowserCliHandler = require_workspace.BrowserCliHandler;
exports.BrowserContextProcessor = BrowserContextProcessor;
exports.BrowserProfileInUseError = BrowserProfileInUseError;
exports.DEFAULT_BROWSER_VIEWPORT = DEFAULT_BROWSER_VIEWPORT;
exports.DEFAULT_THREAD_ID = DEFAULT_THREAD_ID;
exports.MastraBrowser = MastraBrowser;
exports.SCREENCAST_DEFAULTS = SCREENCAST_DEFAULTS;
exports.ScreencastStreamImpl = ScreencastStream;
exports.ThreadManager = ThreadManager;
exports.browserCliHandler = require_workspace.browserCliHandler;
exports.createBrowserRecordingTools = createBrowserRecordingTools;
exports.createError = createError;
exports.resolveLaunchViewport = resolveLaunchViewport;
exports.resolveViewportSize = resolveViewportSize;
//# sourceMappingURL=index.cjs.map
Shown as plain text from the published tarball, after apm checked it against the version's checksum. Nothing in it runs on this page.