langcode
Version:
A Plugin-Based Framework for Managing and Using LangChain
81 lines (80 loc) • 2.81 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const types_1 = require("../../types");
const puppeteer_1 = __importDefault(require("puppeteer"));
class BrowserToolPlugin {
constructor() {
this.name = "browserTool";
this.description = "Web Browser plugin (headless) ile sayfa açma, etkileşime geçme ve veri çekme.";
this.type = types_1.PluginType.Tool;
this.RunConfigExample = {
url: "",
selector: "",
action: "click",
input: "",
evaluate: ""
};
this.InitConfigExample = {
headless: true,
defaultViewport: { width: 1280, height: 720 },
};
this.browser = null;
}
expose() {
return {
name: this.name,
description: this.description,
type: this.type,
InitConfigExample: this.InitConfigExample,
RunConfigExample: this.RunConfigExample,
browser: this.browser,
};
}
async init(config) {
const { headless = true, defaultViewport } = config;
this.browser = await puppeteer_1.default.launch({
headless,
defaultViewport,
});
}
async run(args) {
if (!this.browser) {
throw new Error("Browser is not initialized. Please call init() first.");
}
const page = await this.browser.newPage();
try {
// Sayfaya git
await page.goto(args.url, { waitUntil: "networkidle2" });
// Seçici varsa bekle, bir aksiyon yap
if (args.selector) {
await page.waitForSelector(args.selector);
// Örnek aksiyonlar
if (args.action === "click") {
await page.click(args.selector);
}
else if (args.action === "type" && args.input) {
// Metin girişi yapılacaksa
await page.type(args.selector, args.input);
}
}
// Kullanıcı `evaluate` ile bir JS fonksiyonu çalıştırmak isteyebilir
if (args.evaluate) {
const result = await page.evaluate(args.evaluate);
return { evaluated: result };
}
// Aksi durumda sayfa içeriğini döndürelim
const content = await page.content();
return { content };
}
catch (err) {
return { error: err.message || String(err) };
}
finally {
await page.close();
}
}
}
exports.default = BrowserToolPlugin;