langchain-aluvia-webbrowser
Version:
A LangChain WebBrowser tool that uses Aluvia mobile connectivity to browse websites and extract information
73 lines • 3.07 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const aluvia_ts_sdk_1 = __importDefault(require("aluvia-ts-sdk"));
const undici_1 = require("undici");
const webbrowser_1 = require("@langchain/classic/tools/webbrowser");
/**
* A WebBrowser tool that gives your agent the ability to visit websites and extract information through Aluvia proxies.
*/
class AluviaWebBrowser extends webbrowser_1.WebBrowser {
constructor(args) {
const aluviaToken = args.aluviaToken || process.env.ALUVIA_TOKEN;
if (!aluviaToken) {
throw new Error("Aluvia token is required. Please provide it in the constructor or set the ALUVIA_TOKEN environment variable.\n" +
"You can get your token from the Aluvia dashboard: https://dashboard.aluvia.io/credentials");
}
super(args);
this.aluviaToken = aluviaToken;
this.name = "aluvia-web-browser";
this.description = `aluvia-powered web browser - ${this.description}`;
}
static async create(args) {
const instance = new AluviaWebBrowser(args);
await instance.setupProxyConfig();
return instance;
}
async _call(...args) {
if (!this.proxyAgent) {
await this.setupProxyConfig();
}
return super._call(...args);
}
async setupProxyConfig() {
if (this.proxyAgent) {
return;
}
try {
const proxyUrl = await this.getProxyUrl(this.aluviaToken);
this.proxyAgent = new undici_1.ProxyAgent(proxyUrl);
// Configure proxy by setting dispatcher in requestConfig. The parent WebBrowser
// spreads requestConfig into fetchOptions (...config), so our ProxyAgent gets
// passed directly to fetch() calls.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.requestConfig = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
...this.requestConfig,
dispatcher: this.proxyAgent,
};
}
catch (error) {
throw new Error(`Failed to setup Aluvia proxy configuration: ${error instanceof Error ? error.message : String(error)}`);
}
}
async getProxyUrl(aluviaToken) {
const aluvia = new aluvia_ts_sdk_1.default(aluviaToken);
const proxy = await aluvia.first();
if (!proxy) {
throw new Error("No proxies available in Aluvia account.\n" +
"Please check your account at https://dashboard.aluvia.io/proxies to ensure you have active proxy credentials.");
}
return proxy.toUrl();
}
async dispose() {
if (this.proxyAgent) {
await this.proxyAgent.close();
this.proxyAgent = undefined;
}
}
}
exports.default = AluviaWebBrowser;
//# sourceMappingURL=aluvia-webbrowser.js.map