-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(evals): add stagehand_facade tool surface #2750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import { FACADE_AGENT_INSTRUCTIONS } from "@browserbasehq/stagehand-integrations/facade"; | ||
| import { buildAllowlistedEnv } from "@browserbasehq/stagehand-integrations/harness"; | ||
| import { fileURLToPath } from "node:url"; | ||
| import type { Artifact, ConnectionMode } from "../contracts/results.js"; | ||
| import type { | ||
| CoreCapability, | ||
| CorePageHandle, | ||
| CoreSession, | ||
| CoreTool, | ||
| StartupProfile, | ||
| ToolStartInput, | ||
| ToolStartResult, | ||
| } from "../contracts/tool.js"; | ||
| import type { TargetKind } from "../contracts/targets.js"; | ||
|
|
||
| const SUPPORTED_CAPABILITIES: CoreCapability[] = [ | ||
| "session", | ||
| "navigation", | ||
| "evaluation", | ||
| "screenshot", | ||
| "viewport", | ||
| "wait", | ||
| "click", | ||
| "hover", | ||
| "scroll", | ||
| "type", | ||
| "press", | ||
| "tabs", | ||
| "representation", | ||
| ]; | ||
|
|
||
| const serverPath = fileURLToPath( | ||
| import.meta.resolve("@browserbasehq/stagehand-integrations/facade/stdio-server"), | ||
| ); | ||
|
|
||
| function unsupportedSessionOperation(): never { | ||
| throw new Error("stagehand_facade is available only through its agent MCP mount"); | ||
| } | ||
|
|
||
| /** | ||
| * The facade process and its browser are spawned by the agent harness. This | ||
| * placeholder satisfies the CoreTool lifecycle contract without launching a | ||
| * second, unobservable browser solely for the runner-side session. | ||
| */ | ||
| class StagehandFacadeMountSession implements CoreSession { | ||
| async listPages(): Promise<CorePageHandle[]> { | ||
| return unsupportedSessionOperation(); | ||
| } | ||
|
|
||
| async activePage(): Promise<CorePageHandle> { | ||
| return unsupportedSessionOperation(); | ||
| } | ||
|
|
||
| async newPage(): Promise<CorePageHandle> { | ||
| return unsupportedSessionOperation(); | ||
| } | ||
|
|
||
| async selectPage(): Promise<void> { | ||
| unsupportedSessionOperation(); | ||
| } | ||
|
|
||
| async closePage(): Promise<void> { | ||
| unsupportedSessionOperation(); | ||
| } | ||
|
|
||
| async close(): Promise<void> {} | ||
|
|
||
| async getArtifacts(): Promise<Artifact[]> { | ||
| return []; | ||
| } | ||
|
|
||
| async getRawMetrics(): Promise<Record<string, unknown>> { | ||
| return {}; | ||
| } | ||
| } | ||
|
|
||
| export function buildStagehandFacadeEnv( | ||
| environment: ToolStartInput["environment"], | ||
| ): Record<string, string> { | ||
| return { | ||
| ...buildAllowlistedEnv(), | ||
| STAGEHAND_BROWSER: environment === "BROWSERBASE" ? "browserbase" : "local", | ||
| }; | ||
| } | ||
|
|
||
| function connectionModeFromProfile(startupProfile: StartupProfile): ConnectionMode { | ||
| return startupProfile === "tool_create_browserbase" ? "browserbase_native" : "launch"; | ||
| } | ||
|
|
||
| export class StagehandFacadeTool implements CoreTool { | ||
| readonly id = "stagehand_facade"; | ||
| readonly surface = "mcp"; | ||
| readonly family = "stagehand"; | ||
| readonly supportedStartupProfiles: StartupProfile[] = [ | ||
| "tool_launch_local", | ||
| "tool_create_browserbase", | ||
| ]; | ||
| readonly supportedCapabilities: CoreCapability[] = [...SUPPORTED_CAPABILITIES]; | ||
| readonly supportedTargetKinds: TargetKind[] = ["selector", "coords", "focused", "snapshot_ref"]; | ||
|
|
||
| async start(input: ToolStartInput): Promise<ToolStartResult> { | ||
| const expectedProfile = | ||
| input.environment === "BROWSERBASE" ? "tool_create_browserbase" : "tool_launch_local"; | ||
| if (input.startupProfile !== expectedProfile) { | ||
| throw new Error( | ||
| `stagehand_facade startup profile "${input.startupProfile}" is not valid for environment "${input.environment}"`, | ||
| ); | ||
| } | ||
|
|
||
| const session = new StagehandFacadeMountSession(); | ||
| return { | ||
| session, | ||
| agentMount: { | ||
| via: "mcp", | ||
| promptInstructions: FACADE_AGENT_INSTRUCTIONS, | ||
| mcpServers: { | ||
| stagehand: { | ||
| command: process.execPath, | ||
| args: [serverPath], | ||
| env: buildStagehandFacadeEnv(input.environment), | ||
| }, | ||
| }, | ||
| }, | ||
| cleanup: async () => { | ||
| await session.close(); | ||
| }, | ||
| metadata: { | ||
| environment: input.environment === "BROWSERBASE" ? "browserbase" : "local", | ||
| browserOwnership: "tool", | ||
| connectionMode: connectionModeFromProfile(input.startupProfile), | ||
| startupProfile: input.startupProfile, | ||
| }, | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import { FACADE_AGENT_INSTRUCTIONS } from "@browserbasehq/stagehand-integrations/facade"; | ||
| import { afterEach, beforeEach, describe, expect, it } from "vitest"; | ||
| import { getCoreTool, listCoreTools } from "../../core/tools/registry.js"; | ||
| import { buildStagehandFacadeEnv, StagehandFacadeTool } from "../../core/tools/stagehand_facade.js"; | ||
| import { | ||
| resolveClaudeCodeStartupProfile, | ||
| resolveClaudeCodeToolSurface, | ||
| } from "../../framework/claudeCodeToolAdapter.js"; | ||
| import { | ||
| resolveCodexStartupProfile, | ||
| resolveCodexToolSurface, | ||
| } from "../../framework/codexToolAdapter.js"; | ||
| import type { EvalLogger } from "../../logger.js"; | ||
|
|
||
| const ORIGINAL_ENV = { ...process.env }; | ||
|
|
||
| beforeEach(() => { | ||
| for (const key of Object.keys(process.env)) { | ||
| if (/^(STAGEHAND_|BROWSERBASE_)/u.test(key)) delete process.env[key]; | ||
| } | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| for (const key of Object.keys(process.env)) { | ||
| if (!(key in ORIGINAL_ENV)) delete process.env[key]; | ||
| } | ||
| Object.assign(process.env, ORIGINAL_ENV); | ||
| }); | ||
|
|
||
| describe("stagehand facade tool surface", () => { | ||
| it("is registered", () => { | ||
| expect(listCoreTools()).toContain("stagehand_facade"); | ||
| expect(getCoreTool("stagehand_facade")).toBeInstanceOf(StagehandFacadeTool); | ||
| }); | ||
|
|
||
| it("builds the shipped facade MCP mount", async () => { | ||
| process.env.STAGEHAND_MODEL_NAME = "openai/gpt-5-mini"; | ||
| process.env.BROWSERBASE_API_KEY = "browserbase-secret"; | ||
| process.env.OPENAI_API_KEY = "must-not-cross-the-mount"; | ||
|
|
||
| const running = await new StagehandFacadeTool().start({ | ||
| logger: {} as EvalLogger, | ||
| environment: "LOCAL", | ||
| startupProfile: "tool_launch_local", | ||
| }); | ||
|
|
||
| expect(running.agentMount?.via).toBe("mcp"); | ||
| if (running.agentMount?.via !== "mcp") throw new Error("expected MCP mount"); | ||
| expect(running.agentMount.promptInstructions).toBe(FACADE_AGENT_INSTRUCTIONS); | ||
| expect(Object.keys(running.agentMount.mcpServers)).toEqual(["stagehand"]); | ||
| expect(running.agentMount.mcpServers.stagehand).toMatchObject({ | ||
| command: process.execPath, | ||
| args: [expect.stringMatching(/facade[/\\]stdio-server\.mjs$/u)], | ||
| env: { | ||
| STAGEHAND_BROWSER: "local", | ||
| STAGEHAND_MODEL_NAME: "openai/gpt-5-mini", | ||
| BROWSERBASE_API_KEY: "browserbase-secret", | ||
| }, | ||
| }); | ||
| expect( | ||
| (running.agentMount.mcpServers.stagehand as { env: Record<string, string> }).env, | ||
| ).not.toHaveProperty("OPENAI_API_KEY"); | ||
| expect(running.captureEvidence).toBeUndefined(); | ||
| await running.cleanup(); | ||
| }); | ||
|
|
||
| it("filters host env and overrides browser selection for each eval environment", () => { | ||
| process.env.STAGEHAND_BROWSER = "browserbase"; | ||
| process.env.STAGEHAND_MODEL_API_KEY = "model-secret"; | ||
| process.env.BROWSERBASE_PROJECT_ID = "project-id"; | ||
| process.env.ANTHROPIC_API_KEY = "must-not-cross-the-mount"; | ||
|
|
||
| expect(buildStagehandFacadeEnv("LOCAL")).toEqual({ | ||
| STAGEHAND_BROWSER: "local", | ||
| STAGEHAND_MODEL_API_KEY: "model-secret", | ||
| BROWSERBASE_PROJECT_ID: "project-id", | ||
| }); | ||
| expect(buildStagehandFacadeEnv("BROWSERBASE")).toEqual({ | ||
| STAGEHAND_BROWSER: "browserbase", | ||
| STAGEHAND_MODEL_API_KEY: "model-secret", | ||
| BROWSERBASE_PROJECT_ID: "project-id", | ||
| }); | ||
| }); | ||
|
|
||
| it("is supported by both agent harnesses with tool-owned startup profiles", () => { | ||
| expect(resolveClaudeCodeToolSurface("stagehand_facade")).toBe("stagehand_facade"); | ||
| expect(resolveClaudeCodeStartupProfile("stagehand_facade", "LOCAL")).toBe("tool_launch_local"); | ||
| expect(resolveClaudeCodeStartupProfile("stagehand_facade", "BROWSERBASE")).toBe( | ||
| "tool_create_browserbase", | ||
| ); | ||
| expect(resolveCodexToolSurface("stagehand_facade")).toBe("stagehand_facade"); | ||
| expect(resolveCodexStartupProfile("stagehand_facade", "LOCAL")).toBe("tool_launch_local"); | ||
| expect(resolveCodexStartupProfile("stagehand_facade", "BROWSERBASE")).toBe( | ||
| "tool_create_browserbase", | ||
| ); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.