@hashgraphonline/conversational-agent
Version:
Hashgraph Online conversational AI agent implementing HCS-10 communication, HCS-2 registries, and content inscription on Hedera. https://hol.org
83 lines (82 loc) • 3 kB
JavaScript
import { BasePlugin, BaseHederaQueryTool } from "hedera-agent-kit";
import { z } from "zod";
const PageSnapshotSchema = z.object({
url: z.string().url(),
maxCharacters: z.number().int().min(256, "Minimum length is 256 characters").max(8e3, "Maximum length is 8000 characters").optional().default(3e3)
});
class WebPageSnapshotTool extends BaseHederaQueryTool {
constructor(params) {
const { fetchImpl, ...rest } = params;
super(rest);
this.name = "web_page_snapshot";
this.description = "Fetches the visible text content of a web page for analysis.";
this.namespace = "browser";
this.specificInputSchema = PageSnapshotSchema;
this.fetchImpl = fetchImpl ?? fetch;
}
async executeQuery(input) {
const maxChars = input.maxCharacters ?? 3e3;
try {
const response = await this.fetchImpl(input.url, {
redirect: "follow"
});
if (!response.ok) {
return `Failed to load ${input.url}: HTTP ${response.status}`;
}
const html = await response.text();
const text = this.normalizeHtml(html);
if (!text) {
return "The fetched page did not contain readable text.";
}
return text.length > maxChars ? `${text.slice(0, maxChars)}…` : text;
} catch (error) {
this.logger.error("WebPageSnapshotTool failed", error);
return `Failed to fetch content for ${input.url}: ${error instanceof Error ? error.message : String(error)}`;
}
}
normalizeHtml(html) {
const withoutScripts = html.replace(/<script[\s\S]*?<\/script>/gi, " ").replace(/<style[\s\S]*?<\/style>/gi, " ").replace(/<!--([\s\S]*?)-->/g, " ");
const stripped = withoutScripts.replace(/<[^>]+>/g, " ");
const decoded = stripped.replace(/ /gi, " ").replace(/&/gi, "&").replace(/</gi, "<").replace(/>/gi, ">").replace(/"/gi, '"').replace(/'/gi, "'");
return decoded.replace(/\s+/g, " ").trim();
}
}
class WebBrowserPlugin extends BasePlugin {
constructor() {
super(...arguments);
this.id = "web-browser";
this.name = "Web Browser Plugin";
this.description = "Provides tools for fetching live web page content to enrich assistant understanding.";
this.version = "0.1.0";
this.author = "Hashgraph Online";
this.namespace = "browser";
this.tools = [];
}
async initialize(context) {
await super.initialize(context);
const hederaKit = context.config.hederaKit;
if (!hederaKit) {
this.context.logger.warn(
"WebBrowserPlugin skipped because HederaAgentKit was not present in plugin context."
);
this.tools = [];
return;
}
const tool = new WebPageSnapshotTool({
hederaKit,
logger: this.context.logger
});
this.tools = [tool];
this.context.logger.info("Web Browser Plugin initialized with snapshot tool");
}
getTools() {
return this.tools;
}
async cleanup() {
this.tools = [];
}
}
export {
WebBrowserPlugin
};
//# sourceMappingURL=index41.js.map