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

186 lines (185 loc) 6.38 kB
import { BasePlugin } from "hedera-agent-kit"; import { OpenConvaiState, HCS10Builder, RegisterAgentTool, FindRegistrationsTool, RetrieveProfileTool, InitiateConnectionTool, ListConnectionsTool, SendMessageToConnectionTool, CheckMessagesTool, ConnectionMonitorTool, ManageConnectionRequestsTool, AcceptConnectionRequestTool, ListUnapprovedConnectionRequestsTool } from "@hashgraphonline/standards-agent-kit"; import { HCS10Client } from "@hashgraphonline/standards-sdk"; function hasInitializeConnectionsManager(stateManager) { return typeof stateManager === "object" && stateManager !== null && "initializeConnectionsManager" in stateManager && typeof stateManager.initializeConnectionsManager === "function"; } class HCS10Plugin extends BasePlugin { constructor() { super(...arguments); this.id = "hcs-10"; this.name = "HCS-10 Plugin"; this.description = "HCS-10 agent tools for decentralized agent registration, connections, and messaging on Hedera"; this.version = "1.0.0"; this.author = "Hashgraph Online"; this.namespace = "hcs10"; this.tools = []; } async initialize(context) { await super.initialize(context); const hederaKit = context.config.hederaKit; if (!hederaKit) { this.context.logger.warn( "HederaKit not found in context. HCS-10 tools will not be available." ); return; } try { this.stateManager = context.stateManager || context.config.stateManager || this.appConfig?.stateManager || new OpenConvaiState(); const accountId = hederaKit.signer.getAccountId().toString(); const isBytesMode = String(hederaKit.operationalMode || "returnBytes") === "returnBytes"; let inboundTopicId = ""; let outboundTopicId = ""; let operatorPrivateKeyRef = hederaKit.signer.getOperatorPrivateKey(); let operatorPrivateKeySerialized; try { const resolved = typeof operatorPrivateKeyRef?.toString === "function" ? operatorPrivateKeyRef.toString() : ""; operatorPrivateKeySerialized = resolved; const hcs10Client = new HCS10Client({ network: hederaKit.network, operatorId: accountId, operatorPrivateKey: operatorPrivateKeyRef, logLevel: "error" }); const profileResponse = await hcs10Client.retrieveProfile(accountId); if (profileResponse.success && profileResponse.topicInfo) { inboundTopicId = profileResponse.topicInfo.inboundTopic; outboundTopicId = profileResponse.topicInfo.outboundTopic; } } catch (profileError) { this.context.logger.warn( "Skipping profile topic discovery", profileError ); } const agentRecord = { name: `Agent ${accountId}`, accountId, inboundTopicId, outboundTopicId }; if (!isBytesMode && operatorPrivateKeySerialized) { agentRecord.privateKey = operatorPrivateKeySerialized; } this.stateManager.setCurrentAgent( agentRecord ); this.context.logger.info( `Set current agent: ${accountId} with topics ${inboundTopicId}/${outboundTopicId}` ); if (!isBytesMode && this.stateManager && !this.stateManager.getConnectionsManager()) { try { const hcs10Client = new HCS10Client({ network: hederaKit.network, operatorId: accountId, operatorPrivateKey: operatorPrivateKeyRef ?? "", logLevel: "error" }); if (hasInitializeConnectionsManager(this.stateManager)) { this.stateManager.initializeConnectionsManager(hcs10Client); } else { this.context.logger.warn( "StateManager does not support connection manager initialization" ); } this.context.logger.info( "ConnectionsManager initialized in HCS10Plugin" ); } catch (cmError) { this.context.logger.warn( "Could not initialize ConnectionsManager:", cmError ); } } this.initializeTools(); this.context.logger.info("HCS-10 Plugin initialized successfully"); } catch (error) { this.context.logger.error("Failed to initialize HCS-10 plugin:", error); } } initializeTools() { if (!this.stateManager) { throw new Error("StateManager must be initialized before creating tools"); } const hederaKit = this.context.config.hederaKit; if (!hederaKit) { throw new Error("HederaKit not found in context config"); } const hcs10Builder = new HCS10Builder(hederaKit, this.stateManager); this.tools = [ new RegisterAgentTool({ hederaKit, hcs10Builder, logger: this.context.logger }), new FindRegistrationsTool({ hederaKit, hcs10Builder, logger: this.context.logger }), new RetrieveProfileTool({ hederaKit, hcs10Builder, logger: this.context.logger }), new InitiateConnectionTool({ hederaKit, hcs10Builder, logger: this.context.logger }), new ListConnectionsTool({ hederaKit, hcs10Builder, logger: this.context.logger }), new SendMessageToConnectionTool({ hederaKit, hcs10Builder, logger: this.context.logger }), new CheckMessagesTool({ hederaKit, hcs10Builder, logger: this.context.logger }), new ConnectionMonitorTool({ hederaKit, hcs10Builder, logger: this.context.logger }), new ManageConnectionRequestsTool({ hederaKit, hcs10Builder, logger: this.context.logger }), new AcceptConnectionRequestTool({ hederaKit, hcs10Builder, logger: this.context.logger }), new ListUnapprovedConnectionRequestsTool({ hederaKit, hcs10Builder, logger: this.context.logger }) ]; } getTools() { return this.tools; } getStateManager() { return this.stateManager; } async cleanup() { this.tools = []; delete this.stateManager; if (this.context?.logger) { this.context.logger.info("HCS-10 Plugin cleaned up"); } } } export { HCS10Plugin }; //# sourceMappingURL=index2.js.map