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. https://hol.org

92 lines (91 loc) 4.32 kB
import { z } from "zod"; import { BaseHCS10TransactionTool } from "./standards-agent-kit.es10.js"; import { Logger } from "@hashgraphonline/standards-sdk"; const logger = new Logger({ module: "SendMessageToConnectionTool" }); const SendMessageToConnectionZodSchema = z.object({ targetIdentifier: z.string().optional().describe( "The request key (e.g., 'req-1:0.0.6155171@0.0.6154875'), account ID (e.g., 0.0.12345) of the target agent, OR the connection number (e.g., '1', '2') from the 'list_connections' tool. Request key is most deterministic." ), connectionId: z.string().optional().describe( "The connection number (e.g., '1', '2') from the 'list_connections' tool." ), agentId: z.string().optional().describe("The account ID (e.g., 0.0.12345) of the target agent."), message: z.string().describe("The text message content to send."), disableMonitoring: z.boolean().optional().default(false) }); class SendMessageToConnectionTool extends BaseHCS10TransactionTool { constructor(params) { super(params); this.name = "send_message_to_connection"; this.description = "Use this to send a message to an agent you already have an active connection with. Provide the target agent's account ID (e.g., 0.0.12345) and your message. If no active connection exists, this will fail - use initiate_connection instead to create a new connection first."; this.specificInputSchema = SendMessageToConnectionZodSchema; this.requiresMultipleTransactions = true; this.neverScheduleThisTool = true; } async callBuilderMethod(builder, specificArgs) { const hcs10Builder = builder; const targetIdentifier = specificArgs.targetIdentifier || specificArgs.agentId || specificArgs.connectionId; if (!targetIdentifier) { throw new Error( "Either targetIdentifier, connectionId, or agentId must be provided" ); } const stateManager = hcs10Builder.getStateManager(); if (stateManager) { const connectionsManager = stateManager.getConnectionsManager(); if (connectionsManager) { try { const currentAgent = stateManager.getCurrentAgent(); if (currentAgent && currentAgent.accountId) { await connectionsManager.fetchConnectionData( currentAgent.accountId ); } } catch (error) { logger.debug("Could not refresh connections:", error); } } if (targetIdentifier.match(/^\d+$/)) { const connections = stateManager.listConnections(); const connectionIndex = parseInt(targetIdentifier) - 1; const establishedConnections = connections.filter( (conn) => conn.status === "established" && !conn.isPending && !conn.needsConfirmation ); if (connectionIndex >= 0 && connectionIndex < establishedConnections.length) { const selectedConnection = establishedConnections[connectionIndex]; if (selectedConnection && selectedConnection.connectionTopicId) { await hcs10Builder.sendMessageToConnection({ targetIdentifier: selectedConnection.connectionTopicId, message: specificArgs.message, disableMonitoring: specificArgs.disableMonitoring }); return; } } } if (targetIdentifier.match(/^\d+\.\d+\.\d+$/)) { const connections = stateManager.listConnections(); const establishedConnection = connections.find( (conn) => (conn.targetAccountId === targetIdentifier || conn.targetAccountId === `0.0.${targetIdentifier}`) && conn.status === "established" && !conn.isPending && !conn.needsConfirmation ); if (establishedConnection && establishedConnection.connectionTopicId) { await hcs10Builder.sendMessageToConnection({ targetIdentifier: establishedConnection.connectionTopicId, message: specificArgs.message, disableMonitoring: specificArgs.disableMonitoring }); return; } } } await hcs10Builder.sendMessageToConnection({ targetIdentifier, message: specificArgs.message, disableMonitoring: specificArgs.disableMonitoring }); } } export { SendMessageToConnectionTool }; //# sourceMappingURL=standards-agent-kit.es12.js.map