UNPKG

@hashgraphonline/conversational-agent

Version:

Hashgraph Online conversational AI agent implementing HCS-10 communication, HCS-2 registries, and content inscription on Hedera. https://hol.org

84 lines (83 loc) 2.88 kB
import { Wallet } from "@ethereumjs/wallet"; import { z } from "zod"; import crypto from "crypto"; import { hexToBytes, errorHasStatus, getErrorMessage, getResponseWithStructuredContent } from "./index62.js"; import { BaseHederaQueryTool } from "hedera-agent-kit"; import { BAD_REQUEST_STATUS } from "./index63.js"; const ReadFeedSchema = z.object({ memoryTopic: z.string(), owner: z.string().optional() }); class ReadFeedTool extends BaseHederaQueryTool { constructor(params) { const { bee, config, ...rest } = params; super(rest); this.name = "swarm-read-feed"; this.description = `Retrieve the latest data from the feed of a given topic. memoryTopic: Feed topic. owner: When accessing external memory or feed, ethereum address of the owner must be set. `; this.namespace = "swarm"; this.specificInputSchema = ReadFeedSchema; this.bee = bee; this.config = config; } async executeQuery(input) { const { memoryTopic, owner } = input; if (!memoryTopic) { this.logger.error( "Missing required parameter: memoryTopic." ); throw new Error("Missing required parameter: memoryTopic."); } this.logger.info(`[API] Downloading text from Swarm feed with topic: ${memoryTopic}.`); if (!this.config.beeFeedPK) { this.logger.error("Feed private key not configured."); throw new Error("Feed private key not configured."); } let topic = memoryTopic; if (topic.startsWith("0x")) { topic = topic.slice(2); } const isHexString = /^[0-9a-fA-F]{64}$/.test(topic); if (!isHexString) { const hash = crypto.createHash("sha256").update(memoryTopic).digest("hex"); topic = hash; } const topicBytes = hexToBytes(topic); let feedOwner = owner; if (!feedOwner) { const feedPrivateKey = hexToBytes(this.config.beeFeedPK); const signer = new Wallet(feedPrivateKey); feedOwner = signer.getAddressString().slice(2); } else { if (feedOwner.startsWith("0x")) { feedOwner = feedOwner.slice(2); } if (feedOwner.length !== 40) { this.logger.error("Owner must be a valid Ethereum address."); throw new Error("Owner must be a valid Ethereum address."); } } let textData; try { const feedReader = this.bee.makeFeedReader(topicBytes, feedOwner); const latestUpdate = await feedReader.downloadPayload(); textData = latestUpdate.payload.toUtf8(); } catch (error) { let errorMessage = "Reading feed failed."; if (errorHasStatus(error, BAD_REQUEST_STATUS)) { errorMessage = getErrorMessage(error); } this.logger.error(errorMessage, error); throw new Error(errorMessage); } return getResponseWithStructuredContent({ textData }); } } export { ReadFeedTool }; //# sourceMappingURL=index56.js.map