@mastra/core
Version:
Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.
129 lines • 4.94 kB
TypeScript
import type { MastraBrowser } from './browser.js';
/**
* Configuration for a browser CLI provider.
*/
export interface BrowserCliConfig {
/** Regex pattern to match the CLI command */
pattern: RegExp;
/** Flag used to pass CDP URL to the CLI */
flag: string;
/** Flag to pass threadId as session name for isolation */
sessionFlag?: string;
/** Command to run before other commands to establish CDP connection */
warmupCommand?: (cdpUrl: string, threadId: string) => string;
/** Pattern to detect agent-provided external CDP */
externalCdpPattern?: RegExp;
/** Pattern to extract the CDP URL from command (with capture group) */
externalCdpExtractor?: RegExp;
}
/**
* Result of processing a command for browser CLI handling.
*/
export interface BrowserCliProcessResult {
/** The (potentially modified) command to execute */
command: string;
/** Warmup commands that need to be run before the main command */
warmupCommands: string[];
/** Whether external CDP was detected (agent managing their own browser) */
usingExternalCdp: boolean;
/** External CDP URL if detected */
externalCdpUrl?: string;
}
/**
* Handles browser CLI detection, CDP injection, and warmup for execute_command.
* Centralizes all browser CLI-specific logic that was previously in execute-command.ts.
*/
export declare class BrowserCliHandler {
/**
* Track which CLI providers have been warmed up per browser instance and thread.
* Key format: `${browserId}:${cliName}:${threadId}`
* Browser ID scopes warmup state so different agents/workspaces don't share state.
* @internal Exposed for testing
*/
warmedUpClis: Set<string>;
/**
* Track cleanup callbacks for warmed up CLIs to avoid duplicate registrations.
* Key format: `${browserId}:${cliName}:${threadId}`
* @internal Exposed for testing
*/
warmupCleanups: Map<string, () => void>;
/**
* Build a warmup key scoped to browser instance.
*/
private makeWarmupKey;
/**
* Check if a command is a browser CLI command and return its config.
*/
getBrowserCliConfig(command: string): {
name: string;
config: BrowserCliConfig;
} | null;
/**
* Check if any browser CLI command already has a CDP flag specified.
* If so, the agent is managing their own CDP connection and we should skip injection.
*/
hasExternalCdpFlag(parts: string[]): boolean;
/**
* Extract external CDP URL from command parts.
* Returns the first CDP URL found, or null if none.
*/
extractExternalCdpUrl(parts: string[]): string | null;
/**
* Inject CDP URL and session flag into a single browser CLI command.
* Returns the modified command or the original if no injection needed.
*/
private injectCdpUrlIntoSingleCommand;
/**
* Inject CDP URL and session flag into all browser CLI commands in a potentially
* chained command string (commands joined by &&, ||, or ;).
*/
injectCdpUrl(command: string, cdpUrl: string, threadId?: string): string;
/**
* Check if a warmup has been completed for a browser/CLI/thread combination.
*/
isWarmedUp(browserId: string, cliName: string, threadId: string): boolean;
/**
* Mark a browser/CLI/thread combination as warmed up.
*/
markWarmedUp(browserId: string, cliName: string, threadId: string): void;
/**
* Register a cleanup callback for when a browser closes.
* The cleanup will remove the warmup state for the given browser/CLI/thread.
*/
registerWarmupCleanup(browserId: string, cliName: string, threadId: string, browser: MastraBrowser): void;
/**
* Get warmup commands that need to be run for the detected browser CLIs.
*/
getWarmupCommands(browserId: string, browserClis: Array<{
name: string;
config: BrowserCliConfig;
}>, cdpUrl: string, threadId: string): Array<{
cliName: string;
command: string;
}>;
/**
* Process a command for browser CLI handling.
* Detects browser CLIs, checks for external CDP, and prepares injection.
*
* This is the main entry point - call this from execute-command.ts.
*/
analyzeCommand(command: string): {
/** Detected browser CLIs in the command */
browserClis: Array<{
name: string;
config: BrowserCliConfig;
}>;
/** Command parts split by shell operators */
parts: string[];
/** Whether external CDP was detected */
usingExternalCdp: boolean;
/** External CDP URL if detected */
externalCdpUrl: string | null;
};
}
/**
* Singleton instance for use across the application.
* This preserves warmup state across command executions.
*/
export declare const browserCliHandler: BrowserCliHandler;
//# sourceMappingURL=cli-handler.d.ts.map