Skip to content
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

feat(community): Add support for connecting to remote browser via Websocket #7603

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
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
55 changes: 36 additions & 19 deletions libs/langchain-community/src/document_loaders/web/puppeteer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
Page,
Browser,
PuppeteerLaunchOptions,
connect,
} from "puppeteer";

import { Document } from "@langchain/core/documents";
Expand Down Expand Up @@ -52,8 +53,7 @@ export type PuppeteerWebBaseLoaderOptions = {
*/
export class PuppeteerWebBaseLoader
extends BaseDocumentLoader
implements DocumentLoader
{
implements DocumentLoader {
options: PuppeteerWebBaseLoaderOptions | undefined;

constructor(public webPath: string, options?: PuppeteerWebBaseLoaderOptions) {
Expand All @@ -65,14 +65,23 @@ export class PuppeteerWebBaseLoader
url: string,
options?: PuppeteerWebBaseLoaderOptions
): Promise<string> {
const { launch } = await PuppeteerWebBaseLoader.imports();
const { launch, connect } = await PuppeteerWebBaseLoader.imports();

const browser = await launch({
headless: true,
defaultViewport: null,
ignoreDefaultArgs: ["--disable-extensions"],
...options?.launchOptions,
});
let browser: Browser;

if (options?.launchOptions?.browserWSEndpoint) {
browser = await connect({
browserWSEndpoint: options?.launchOptions?.browserWSEndpoint,
});
}
else {
browser = await launch({
headless: true,
defaultViewport: null,
ignoreDefaultArgs: ["--disable-extensions"],
...options?.launchOptions,
});
}
const page = await browser.newPage();

await page.goto(url, {
Expand Down Expand Up @@ -123,14 +132,21 @@ export class PuppeteerWebBaseLoader
url: string,
options?: PuppeteerWebBaseLoaderOptions
): Promise<Document> {
const { launch } = await PuppeteerWebBaseLoader.imports();

const browser = await launch({
headless: true,
defaultViewport: null,
ignoreDefaultArgs: ["--disable-extensions"],
...options?.launchOptions,
});
const { launch, connect } = await PuppeteerWebBaseLoader.imports();

let browser: Browser;
if (options?.launchOptions?.browserWSEndpoint) {
browser = await connect({
browserWSEndpoint: options?.launchOptions?.browserWSEndpoint,
});
} else {
browser = await launch({
headless: true,
defaultViewport: null,
ignoreDefaultArgs: ["--disable-extensions"],
...options?.launchOptions,
});
}
const page = await browser.newPage();

await page.goto(url, {
Expand Down Expand Up @@ -161,12 +177,13 @@ export class PuppeteerWebBaseLoader
*/
static async imports(): Promise<{
launch: typeof launch;
connect: typeof connect;
}> {
try {
// eslint-disable-next-line import/no-extraneous-dependencies
const { launch } = await import("puppeteer");
const { launch, connect } = await import("puppeteer");

return { launch };
return { launch, connect };
} catch (e) {
console.error(e);
throw new Error(
Expand Down
Loading