Skip to content

feat: attach to playwright #923

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
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ async function getBrowser(
browserbaseSessionCreateParams?: Browserbase.Sessions.SessionCreateParams,
browserbaseSessionID?: string,
localBrowserLaunchOptions?: LocalBrowserLaunchOptions,
attachToLocalBrowser?: BrowserContext,
): Promise<BrowserResult> {
if (env === "BROWSERBASE") {
if (!apiKey) {
Expand All @@ -95,7 +96,6 @@ async function getBrowser(
let sessionUrl: string | undefined = undefined;
let sessionId: string;
let connectUrl: string;

const browserbase = new Browserbase({
apiKey,
});
Expand Down Expand Up @@ -226,6 +226,16 @@ async function getBrowser(

return { browser, context, debugUrl, sessionUrl, sessionId, env };
} else {
if (attachToLocalBrowser) {
logger({
category: "init",
message: "attaching to existing local browser context",
level: 1,
});
const context = attachToLocalBrowser;
const browser = context.browser();
return { browser, context, env };
}
if (localBrowserLaunchOptions?.cdpUrl) {
if (!localBrowserLaunchOptions.cdpUrl.includes("connect.connect")) {
logger({
Expand All @@ -240,7 +250,6 @@ async function getBrowser(
},
});
}

const browser = await chromium.connectOverCDP(
localBrowserLaunchOptions.cdpUrl,
);
Expand Down Expand Up @@ -387,6 +396,7 @@ export class Stagehand {
public apiClient: StagehandAPI | undefined;
public readonly waitForCaptchaSolves: boolean;
private localBrowserLaunchOptions?: LocalBrowserLaunchOptions;
private attachToLocalBrowser?: BrowserContext;
public readonly selfHeal: boolean;
private cleanupCalled = false;
public readonly actTimeoutMs: number;
Expand Down Expand Up @@ -528,6 +538,7 @@ export class Stagehand {
systemPrompt,
useAPI = true,
localBrowserLaunchOptions,
attachToLocalBrowser,
waitForCaptchaSolves = false,
logInferenceToFile = false,
selfHeal = false,
Expand Down Expand Up @@ -616,8 +627,7 @@ export class Stagehand {
this.llmClient = llmClient;
this.logger({
category: "init",
message:
"Custom LLM clients are currently not supported in API mode",
message: "Custom LLM clients are currently not supported in API mode",
level: 1,
});
this.usingAPI = false;
Expand Down Expand Up @@ -660,7 +670,7 @@ export class Stagehand {
}
this.waitForCaptchaSolves = waitForCaptchaSolves;
this.localBrowserLaunchOptions = localBrowserLaunchOptions;

this.attachToLocalBrowser = attachToLocalBrowser;
if (this.usingAPI) {
this.registerSignalHandlers();
}
Expand Down Expand Up @@ -786,6 +796,7 @@ export class Stagehand {
this.browserbaseSessionCreateParams,
this.browserbaseSessionID,
this.localBrowserLaunchOptions,
this.attachToLocalBrowser,
).catch((e) => {
this.stagehandLogger.error("Error in init:", { error: String(e) });
const br: BrowserResult = {
Expand Down
35 changes: 35 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion types/stagehand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { LLMProvider } from "../lib/llm/LLMProvider";
import { LogLine } from "./log";
import { AvailableModel, ClientOptions } from "./model";
import { LLMClient } from "../lib/llm/LLMClient";
import { Cookie } from "playwright";
import { Cookie, BrowserContext } from "playwright";
import { AgentProviderType } from "./agent";

export interface ConstructorParams {
Expand Down Expand Up @@ -87,6 +87,7 @@ export interface ConstructorParams {
* The parameters to use for launching a local browser
*/
localBrowserLaunchOptions?: LocalBrowserLaunchOptions;
attachToLocalBrowser?: BrowserContext;
/**
* Log the inference to a file
*/
Expand Down