UNPKG

@hashgraphonline/standards-agent-kit

Version:

A modular SDK for building on-chain autonomous agents using Hashgraph Online Standards, including HCS-10 for agent discovery and communication.

72 lines (71 loc) 3.03 kB
import * as fs from "fs"; import * as path from "path"; class PluginLoader { /** * Load a plugin from a directory * @param directory Path to the directory containing the plugin * @param context Context to provide to the plugin during initialization * @param options Options for loading the plugin * @returns The loaded plugin instance */ static async loadFromDirectory(directory, context, options = { initialize: true }) { const manifestPath = path.join(directory, "plugin.json"); if (!fs.existsSync(manifestPath)) { throw new Error(`Plugin manifest not found at ${manifestPath}`); } try { const manifestContent = fs.readFileSync(manifestPath, "utf8"); const manifest = JSON.parse(manifestContent); if (!manifest.id || !manifest.main) { throw new Error("Invalid plugin manifest: missing required fields (id, main)"); } const mainPath = path.join(directory, manifest.main); if (!fs.existsSync(mainPath)) { throw new Error(`Plugin main file not found at ${mainPath}`); } const pluginModule = await import(mainPath); const PluginClass = pluginModule.default || pluginModule[manifest.id]; if (!PluginClass) { throw new Error(`Could not find plugin class in ${mainPath}`); } const plugin = new PluginClass(); if (!this.isValidPlugin(plugin)) { throw new Error(`Plugin does not implement the IPlugin interface correctly`); } if (options.initialize) { await plugin.initialize(context); } return plugin; } catch (error) { throw new Error(`Failed to load plugin from directory ${directory}: ${error instanceof Error ? error.message : String(error)}`); } } /** * Load a plugin from an npm package * @param packageName Name of the npm package containing the plugin * @param context Context to provide to the plugin during initialization * @param options Options for loading the plugin * @returns The loaded plugin instance */ static async loadFromPackage(packageName, context, options = { initialize: true }) { try { const packagePath = require.resolve(packageName); const packageDir = path.dirname(packagePath); return this.loadFromDirectory(packageDir, context, options); } catch (error) { throw new Error(`Failed to load plugin from package ${packageName}: ${error instanceof Error ? error.message : String(error)}`); } } /** * Check if an object implements the IPlugin interface * @param obj Object to check * @returns true if the object implements IPlugin, false otherwise */ static isValidPlugin(obj) { return obj && typeof obj.id === "string" && typeof obj.name === "string" && typeof obj.description === "string" && typeof obj.version === "string" && typeof obj.author === "string" && typeof obj.initialize === "function" && typeof obj.getTools === "function"; } } export { PluginLoader }; //# sourceMappingURL=standards-agent-kit.es20.js.map