diff --git a/README.md b/README.md index 463e364..e100109 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,34 @@ await agent.closeAgent(); ## Usage Guide +### Config Browser with playwright endpointURL + +- **`endpointURL`**: A CDP WebSocket endpoint or HTTP URL to connect to. For example, `http://localhost:9222/` or `ws://127.0.0.1:9222/devtools/browser/387adf4c-243f-4051-a181-46798f4a46f4`. +- **`slowMo`**: Slows down operations by the specified number of milliseconds for debugging purposes. +- **`args`**: An array of custom arguments to pass to the browser instance. + +These options provide flexibility for connecting to remote browsers or customizing the browser's behavior during automation. + +```typescript +import { HyperAgent } from "@hyperbrowser/agent"; + +const agent = new HyperAgent({ + options: { + endpointURL: "ws://localhost:3000", // or http://localhost:9222 Connect to a remote browser + slowMo: 50, // Slow down operations for debugging + }, +}); + +// Use the agent as usual +const result = await agent.executeTask( + "Navigate to example.com and extract the page title" +); +console.log(result.output); + +// Clean up +await agent.closeAgent(); +``` + ### Multi-Page Management ```typescript diff --git a/src/agent/index.ts b/src/agent/index.ts index e6954c8..a2dc17d 100644 --- a/src/agent/index.ts +++ b/src/agent/index.ts @@ -104,7 +104,8 @@ export class HyperAgent { public async initBrowser(): Promise { if (!this.browser) { this.browser = await this.browserProvider.start(); - this.context = await this.browser.newContext({ + const defaltContext = this.browser.contexts().at(0) + this.context = defaltContext || await this.browser.newContext({ viewport: null, }); diff --git a/src/browser-providers/local.ts b/src/browser-providers/local.ts index 976c772..51b3798 100644 --- a/src/browser-providers/local.ts +++ b/src/browser-providers/local.ts @@ -1,15 +1,23 @@ -import { chromium, Browser, LaunchOptions } from "playwright"; +import { chromium, Browser, LaunchOptions, ConnectOptions } from "playwright"; import BrowserProvider from "@/types/browser-providers/types"; export class LocalBrowserProvider extends BrowserProvider { - options: Omit, "channel"> | undefined; + options: (Omit, "channel"> & { endpointURL?: string,args? : []}) | ConnectOptions & { endpointURL?: string,args? : []} | undefined; session: Browser | undefined; - constructor(options?: Omit, "channel">) { + constructor(options?: Omit, "channel"> & { endpointURL?: string,args? : []}) { super(); this.options = options; } async start(): Promise { - const launchArgs = this.options?.args ?? []; + + if (this.options && 'endpointURL' in this.options && this.options.endpointURL) { + const browser = await chromium.connectOverCDP(this.options.endpointURL,{ + ...this.options + }); + this.session = browser; + return this.session; + } + const launchArgs = this.options?.args || []; const browser = await chromium.launch({ ...(this.options ?? {}), channel: "chrome",