UNPKG

@hashgraphonline/standards-agent-plugin

Version:

Standards agent plugin for OpenConvAI functionality with HCS-10 tools

1 lines 14.6 kB
{"version":3,"file":"index.cjs","sources":["../../src/plugins/openconvai/OpenConvAIPlugin.ts","../../src/StandardsKit.ts"],"sourcesContent":["import {\n GenericPluginContext,\n HederaTool,\n BasePlugin,\n HederaAgentKit,\n} from 'hedera-agent-kit';\nimport {\n IStateManager,\n OpenConvaiState,\n HCS10Builder,\n RegisterAgentTool,\n FindRegistrationsTool,\n InitiateConnectionTool,\n ListConnectionsTool,\n SendMessageToConnectionTool,\n CheckMessagesTool,\n ConnectionMonitorTool,\n ManageConnectionRequestsTool,\n AcceptConnectionRequestTool,\n RetrieveProfileTool,\n ListUnapprovedConnectionRequestsTool,\n} from '@hashgraphonline/standards-agent-kit';\n\nexport class OpenConvAIPlugin extends BasePlugin {\n id = 'openconvai-standards-agent-kit';\n name = 'OpenConvAI Standards Agent Kit Plugin';\n description =\n 'Comprehensive plugin providing all HCS-10 agent tools for registration, connections, and messaging';\n version = '1.0.0';\n author = 'Hashgraph Online';\n namespace = 'openconvai';\n\n private stateManager?: IStateManager;\n private tools: HederaTool[] = [];\n\n override async initialize(context: GenericPluginContext): Promise<void> {\n await super.initialize(context);\n\n const hederaKit = context.config.hederaKit as HederaAgentKit;\n if (!hederaKit) {\n this.context.logger.warn(\n 'HederaKit not found in context. OpenConvAI tools will not be available.'\n );\n return;\n }\n\n try {\n this.stateManager =\n (context.stateManager as IStateManager) || new OpenConvaiState();\n\n this.initializeTools();\n\n this.context.logger.info(\n 'OpenConvAI Standards Agent Kit Plugin initialized successfully'\n );\n } catch (error) {\n this.context.logger.error(\n 'Failed to initialize OpenConvAI plugin:',\n error\n );\n }\n }\n\n private initializeTools(): void {\n if (!this.stateManager) {\n throw new Error('StateManager must be initialized before creating tools');\n }\n\n const hederaKit = this.context.config.hederaKit as HederaAgentKit;\n if (!hederaKit) {\n throw new Error('HederaKit not found in context config');\n }\n\n const hcs10Builder = new HCS10Builder(hederaKit, this.stateManager);\n\n this.tools = [\n new RegisterAgentTool({\n hederaKit: hederaKit,\n hcs10Builder: hcs10Builder,\n logger: this.context.logger,\n }),\n new FindRegistrationsTool({\n hederaKit: hederaKit,\n hcs10Builder: hcs10Builder,\n logger: this.context.logger,\n }),\n new RetrieveProfileTool({\n hederaKit: hederaKit,\n hcs10Builder: hcs10Builder,\n logger: this.context.logger,\n }),\n new InitiateConnectionTool({\n hederaKit: hederaKit,\n hcs10Builder: hcs10Builder,\n logger: this.context.logger,\n }),\n new ListConnectionsTool({\n hederaKit: hederaKit,\n hcs10Builder: hcs10Builder,\n logger: this.context.logger,\n }),\n new SendMessageToConnectionTool({\n hederaKit: hederaKit,\n hcs10Builder: hcs10Builder,\n logger: this.context.logger,\n }),\n new CheckMessagesTool({\n hederaKit: hederaKit,\n hcs10Builder: hcs10Builder,\n logger: this.context.logger,\n }),\n new ConnectionMonitorTool({\n hederaKit: hederaKit,\n hcs10Builder: hcs10Builder,\n logger: this.context.logger,\n }),\n new ManageConnectionRequestsTool({\n hederaKit: hederaKit,\n hcs10Builder: hcs10Builder,\n logger: this.context.logger,\n }),\n new AcceptConnectionRequestTool({\n hederaKit: hederaKit,\n hcs10Builder: hcs10Builder,\n logger: this.context.logger,\n }),\n new ListUnapprovedConnectionRequestsTool({\n hederaKit: hederaKit,\n hcs10Builder: hcs10Builder,\n logger: this.context.logger,\n }),\n ];\n }\n\n getTools(): HederaTool[] {\n return this.tools;\n }\n\n getStateManager(): IStateManager | undefined {\n return this.stateManager;\n }\n\n override async cleanup(): Promise<void> {\n this.tools = [];\n delete this.stateManager;\n if (this.context?.logger) {\n this.context.logger.info(\n 'OpenConvAI Standards Agent Kit Plugin cleaned up'\n );\n }\n }\n}\n","import {\n ServerSigner,\n HederaConversationalAgent,\n getAllHederaCorePlugins,\n BasePlugin,\n} from 'hedera-agent-kit';\nimport type {\n AgentOperationalMode,\n AgentResponse,\n HederaConversationalAgentConfig,\n MirrorNodeConfig,\n} from 'hedera-agent-kit';\nimport { OpenConvAIPlugin } from './plugins/openconvai/OpenConvAIPlugin';\nimport { OpenConvaiState } from '@hashgraphonline/standards-agent-kit';\nimport type { IStateManager } from '@hashgraphonline/standards-agent-kit';\nimport {\n Logger,\n HederaMirrorNode,\n type NetworkType,\n} from '@hashgraphonline/standards-sdk';\nimport { PrivateKey } from '@hashgraph/sdk';\n\nexport interface StandardsKitOptions {\n accountId: string;\n privateKey: string;\n network?: NetworkType;\n openAIApiKey: string;\n openAIModelName?: string;\n verbose?: boolean;\n operationalMode?: AgentOperationalMode;\n userAccountId?: string;\n customSystemMessagePreamble?: string;\n customSystemMessagePostamble?: string;\n additionalPlugins?: BasePlugin[];\n stateManager?: IStateManager;\n scheduleUserTransactionsInBytesMode?: boolean;\n mirrorNodeConfig?: MirrorNodeConfig;\n disableLogging?: boolean;\n}\n\n/**\n * The StandardsKit class is an optional wrapper around the HederaConversationalAgent class,\n * which includes the OpenConvAIPlugin and the OpenConvaiState by default.\n * If you want to use a different plugin or state manager, you can pass them in the options.\n * This class is not required and the plugin can be used directly with the HederaConversationalAgent class.\n *\n * @param options - The options for the StandardsKit.\n * @returns A new instance of the StandardsKit class.\n */\nexport class StandardsKit {\n public conversationalAgent?: HederaConversationalAgent;\n public plugin: OpenConvAIPlugin;\n public stateManager: IStateManager;\n private options: StandardsKitOptions;\n private logger: Logger;\n\n constructor(options: StandardsKitOptions) {\n this.options = options;\n this.stateManager = options.stateManager || new OpenConvaiState();\n this.plugin = new OpenConvAIPlugin();\n this.logger = new Logger({ module: 'StandardsKit' });\n }\n\n async initialize(): Promise<void> {\n const {\n accountId,\n privateKey,\n network = 'testnet',\n openAIApiKey,\n openAIModelName = 'gpt-4o',\n verbose = false,\n operationalMode = 'autonomous',\n userAccountId,\n customSystemMessagePreamble,\n customSystemMessagePostamble,\n additionalPlugins = [],\n scheduleUserTransactionsInBytesMode,\n mirrorNodeConfig,\n disableLogging,\n } = this.options;\n\n if (!accountId || !privateKey) {\n throw new Error('Account ID and private key are required');\n }\n\n try {\n const mirrorNode = new HederaMirrorNode(network, this.logger);\n const accountInfo = await mirrorNode.requestAccount(accountId);\n const keyType = accountInfo?.key?._type || '';\n\n let privateKeyInstance: PrivateKey;\n if (keyType?.toLowerCase()?.includes('ecdsa')) {\n privateKeyInstance = PrivateKey.fromStringECDSA(privateKey);\n } else {\n privateKeyInstance = PrivateKey.fromStringED25519(privateKey);\n }\n\n const serverSigner = new ServerSigner(\n accountId,\n privateKeyInstance,\n network\n );\n\n const defaultSystemMessage = `You are a helpful assistant managing Hedera HCS-10 connections and messages.\nYou have access to tools for registering agents, finding registered agents, initiating connections, listing active connections, sending messages over connections, and checking for new messages.\n\n*** IMPORTANT CONTEXT ***\nYou are currently operating as agent: ${accountId}\nWhen users ask about \"my profile\", \"my account\", \"my connections\", etc., use this account ID: ${accountId}\n\nRemember the connection numbers when listing connections, as users might refer to them.`;\n\n const allPlugins = [\n this.plugin,\n ...additionalPlugins,\n ...getAllHederaCorePlugins(),\n ];\n\n const agentConfig: HederaConversationalAgentConfig = {\n pluginConfig: {\n plugins: allPlugins,\n appConfig: {\n stateManager: this.stateManager,\n },\n },\n openAIApiKey,\n openAIModelName,\n verbose,\n operationalMode,\n userAccountId,\n customSystemMessagePreamble:\n customSystemMessagePreamble || defaultSystemMessage,\n ...(customSystemMessagePostamble !== undefined && {\n customSystemMessagePostamble,\n }),\n ...(scheduleUserTransactionsInBytesMode !== undefined && {\n scheduleUserTransactionsInBytesMode,\n }),\n ...(mirrorNodeConfig !== undefined && { mirrorNodeConfig }),\n ...(disableLogging !== undefined && { disableLogging }),\n };\n\n this.conversationalAgent = new HederaConversationalAgent(\n serverSigner,\n agentConfig\n );\n\n await this.conversationalAgent.initialize();\n } catch (error) {\n this.logger.error('Failed to initialize StandardsKit:', error);\n throw error;\n }\n }\n\n getPlugin(): OpenConvAIPlugin {\n return this.plugin;\n }\n\n getStateManager(): IStateManager {\n return this.stateManager;\n }\n\n getConversationalAgent(): HederaConversationalAgent {\n if (!this.conversationalAgent) {\n throw new Error('StandardsKit not initialized. Call initialize() first.');\n }\n return this.conversationalAgent;\n }\n\n async processMessage(\n message: string,\n chatHistory: {\n type: 'human' | 'ai';\n content: string;\n }[] = []\n ): Promise<AgentResponse> {\n if (!this.conversationalAgent) {\n throw new Error('StandardsKit not initialized. Call initialize() first.');\n }\n return this.conversationalAgent.processMessage(message, chatHistory);\n }\n}\n"],"names":["OpenConvAIPlugin","BasePlugin","constructor","super","arguments","this","id","name","description","version","author","namespace","tools","initialize","context","config","hederaKit","stateManager","OpenConvaiState","initializeTools","logger","info","error","warn","Error","hcs10Builder","HCS10Builder","RegisterAgentTool","FindRegistrationsTool","RetrieveProfileTool","InitiateConnectionTool","ListConnectionsTool","SendMessageToConnectionTool","CheckMessagesTool","ConnectionMonitorTool","ManageConnectionRequestsTool","AcceptConnectionRequestTool","ListUnapprovedConnectionRequestsTool","getTools","getStateManager","cleanup","options","plugin","Logger","module","accountId","privateKey","network","openAIApiKey","openAIModelName","verbose","operationalMode","userAccountId","customSystemMessagePreamble","customSystemMessagePostamble","additionalPlugins","scheduleUserTransactionsInBytesMode","mirrorNodeConfig","disableLogging","mirrorNode","HederaMirrorNode","accountInfo","requestAccount","keyType","key","_type","privateKeyInstance","toLowerCase","includes","PrivateKey","fromStringECDSA","fromStringED25519","serverSigner","ServerSigner","defaultSystemMessage","agentConfig","pluginConfig","plugins","getAllHederaCorePlugins","appConfig","conversationalAgent","HederaConversationalAgent","getPlugin","getConversationalAgent","processMessage","message","chatHistory"],"mappings":"8OAuBO,MAAMA,UAAyBC,EAAAA,WAA/B,WAAAC,GAAAC,SAAAC,WACLC,KAAAC,GAAK,iCACLD,KAAAE,KAAO,wCACPF,KAAAG,YACE,qGACFH,KAAAI,QAAU,QACVJ,KAAAK,OAAS,mBACTL,KAAAM,UAAY,aAGZN,KAAQO,MAAsB,EAAC,CAE/B,gBAAeC,CAAWC,SAClBX,MAAMU,WAAWC,GAGvB,GADkBA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