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

125 lines (124 loc) 4.34 kB
import { MantarayNode } from "@ethersphere/bee-js"; import { z } from "zod"; import fs from "fs"; import path from "path"; import { BaseHederaQueryTool } from "hedera-agent-kit"; import { promisify } from "util"; const DownloadFilesSchema = z.object({ reference: z.string(), filePath: z.string().optional() }); class DownloadFilesTool extends BaseHederaQueryTool { constructor(params) { const { bee, config, ...rest } = params; super(rest); this.name = "swarm-download-files"; this.description = `Download folder, files from a Swarm reference and save to file path or return file list of the reference. Prioritizes this tool over swarm-download-data if there is no assumption about the data type. reference: Swarm reference hash. filePath: Optional file path to save the downloaded content (only available in stdio mode). If not provided list of files in the manifest will be returned. `; this.namespace = "swarm"; this.specificInputSchema = DownloadFilesSchema; this.bee = bee; this.config = config; } async executeQuery(input) { const { reference, filePath } = input; if (!reference) { this.logger.error( "Missing required parameter: reference." ); throw new Error("Missing required parameter: reference."); } this.logger.info(`[API] Downloading folder from Swarm with reference: ${reference}.`); let isManifest = false; let node; try { node = await MantarayNode.unmarshal(this.bee, reference); await node.loadRecursively(this.bee); isManifest = true; } catch (error) { } if (isManifest) { if (filePath) { const destinationFolder = filePath; if (!fs.existsSync(destinationFolder)) { await promisify(fs.mkdir)(destinationFolder, { recursive: true }); } const nodes = node.collect(); if (nodes.length === 1) { const node2 = nodes[0]; const data = await this.bee.downloadData(node2.targetAddress); await promisify(fs.writeFile)( path.join( destinationFolder, node2.fullPathString.split("\\").slice(-1)[0] ), data.toUint8Array() ); } else { for (const node2 of nodes) { const parsedPath = path.parse(node2.fullPathString); const nodeDestFolder = path.join(destinationFolder, parsedPath.dir); if (!fs.existsSync(nodeDestFolder)) { await promisify(fs.mkdir)(nodeDestFolder, { recursive: true }); } const data = await this.bee.downloadData(node2.targetAddress); await promisify(fs.writeFile)( path.join(destinationFolder, node2.fullPathString), data.toUint8Array() ); } } return { content: [ { type: "text", text: JSON.stringify( { reference, manifestNodeCount: nodes.length, savedTo: destinationFolder, message: `Manifest content (${nodes.length} files) successfully downloaded to ${destinationFolder}` }, null, 2 ) } ] }; } else { const nodes = node.collect(); const filesList = nodes.map((node2) => ({ path: node2.fullPathString || "/", targetAddress: Array.from(node2.targetAddress).map((e) => e.toString(16).padStart(2, "0")).join(""), metadata: node2.metadata })); return { content: [ { type: "text", text: JSON.stringify( { reference, type: "manifest", files: filesList, message: "This is a manifest with multiple files. Provide a filePath to download all files or download individual files using their specific references." }, null, 2 ) } ] }; } } else { return "Try swarm-download-data tool instead since the given reference is not a manifest."; } } } export { DownloadFilesTool }; //# sourceMappingURL=index54.js.map