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.

204 lines (201 loc) 8.24 kB
import { StructuredTool } from "@langchain/core/tools"; import { z } from "zod"; import { Logger } from "@hashgraphonline/standards-sdk"; class ManageConnectionRequestsTool extends StructuredTool { constructor({ hcsClient, stateManager, ...rest }) { super(rest); this.name = "manage_connection_requests"; this.description = 'Manage incoming connection requests. List pending requests, view details about requesting agents, and reject connection requests. Use the separate "accept_connection_request" tool to accept.'; this.schema = z.object({ action: z.enum(["list", "view", "reject"]).describe( "The action to perform: list all requests, view details of a specific request, or reject a request" ), requestKey: z.string().optional().describe( "The unique request key to view or reject (required for view and reject actions)" ) }); this.lastRefreshTime = 0; this.refreshIntervalMs = 3e4; this.hcsClient = hcsClient; this.stateManager = stateManager; this.logger = Logger.getInstance({ module: "ManageConnectionRequestsTool", level: "debug" }); } async _call({ action, requestKey }) { const currentAgent = this.stateManager.getCurrentAgent(); if (!currentAgent) { return "Error: Cannot manage connection requests. No agent is currently active. Please register or select an agent first."; } if ((action === "view" || action === "reject") && requestKey === void 0) { return `Error: Request key is required for the "${action}" action. Use the "list" action first to see available requests.`; } try { await this.refreshRequestsIfNeeded(); switch (action) { case "list": return this.listRequests(); case "view": return this.viewRequest(requestKey); case "reject": return this.rejectRequest(requestKey); default: return `Error: Unsupported action: ${action}`; } } catch (error) { this.logger.error(`Error in ManageConnectionRequestsTool: ${error}`); return `Error managing connection requests: ${error instanceof Error ? error.message : String(error)}`; } } async refreshRequestsIfNeeded() { const now = Date.now(); if (now - this.lastRefreshTime > this.refreshIntervalMs) { await this.refreshRequests(); this.lastRefreshTime = now; } } async refreshRequests() { try { const { accountId } = this.hcsClient.getAccountAndSigner(); if (!accountId) { throw new Error("Could not determine account ID for current agent"); } const connectionManager = this.stateManager.getConnectionsManager(); if (!connectionManager) { throw new Error("ConnectionsManager not initialized"); } await connectionManager.fetchConnectionData(accountId); } catch (error) { this.logger.error(`Error refreshing connection requests: ${error}`); throw error; } } listRequests() { const connectionsManager = this.stateManager.getConnectionsManager(); if (!connectionsManager) { return "Error: ConnectionsManager not initialized"; } const pendingRequests = connectionsManager.getPendingRequests(); const needsConfirmation = connectionsManager.getConnectionsNeedingConfirmation(); const allRequests = [...pendingRequests, ...needsConfirmation]; if (allRequests.length === 0) { console.log("No pending connection requests found.", allRequests); return "No pending connection requests found."; } let output = `Found ${allRequests.length} pending connection request(s): `; const sortedRequests = [...allRequests].sort( (a, b) => b.created.getTime() - a.created.getTime() ); sortedRequests.forEach((request, index) => { const requestType = request.needsConfirmation ? "🟠 Incoming" : "⚪️ Outgoing"; const requestIdDisplay = request.uniqueRequestKey || `${request.connectionRequestId || request.inboundRequestId || "unknown"}`; output += `${index + 1}. ${requestType} - Key: ${requestIdDisplay} `; output += ` ${request.needsConfirmation ? "From" : "To"}: ${request.targetAgentName || `Agent ${request.targetAccountId}`} (${request.targetAccountId}) `; output += ` Sent/Rcvd: ${request.created.toLocaleString()} `; if (request.memo) { output += ` Memo: ${request.memo} `; } if (request.profileInfo && request.profileInfo.bio) { output += ` Bio: ${request.profileInfo.bio} `; } output += "\n"; }); output += 'To view more details about a request, use action="view" with the specific requestKey.\n'; output += 'To reject a request, use action="reject" with the specific requestKey.'; return output; } viewRequest(requestKey) { const connectionsManager = this.stateManager.getConnectionsManager(); if (!connectionsManager) { return "Error: ConnectionsManager not initialized"; } const pendingRequests = connectionsManager.getPendingRequests(); const needsConfirmation = connectionsManager.getConnectionsNeedingConfirmation(); const allRequests = [...pendingRequests, ...needsConfirmation]; const request = allRequests.find( (r) => r.uniqueRequestKey === requestKey || r.connectionRequestId?.toString() === requestKey || r.inboundRequestId?.toString() === requestKey ); if (!request) { return `Error: Request with key ${requestKey} not found or no longer pending.`; } const requestType = request.needsConfirmation ? "Incoming" : "Outgoing"; const uniqueKey = request.uniqueRequestKey || `${request.connectionRequestId || request.inboundRequestId || "unknown"}`; let output = `Details for ${requestType} connection request: ${uniqueKey} `; output += `${request.needsConfirmation ? "Requestor" : "Target"} ID: ${request.targetAccountId} `; output += `${request.needsConfirmation ? "Requestor" : "Target"} Name: ${request.targetAgentName || `Agent ${request.targetAccountId}`} `; output += `Received: ${request.created.toLocaleString()} `; if (request.memo) { output += `Memo: ${request.memo} `; } if (request.profileInfo) { output += "\nAgent Profile Information:\n"; if (request.profileInfo.display_name || request.profileInfo.alias) { output += `Name: ${request.profileInfo.display_name || request.profileInfo.alias} `; } if (request.profileInfo.type !== void 0) { output += `Type: ${request.profileInfo.type} `; } if (request.profileInfo.bio) { output += `Bio: ${request.profileInfo.bio} `; } } output += "\nActions:\n"; output += `- To reject this request: action="reject", requestKey="${uniqueKey}" `; output += 'Use the separate "accept_connection_request" tool to accept requests.'; return output; } async rejectRequest(requestKey) { const connectionsManager = this.stateManager.getConnectionsManager(); if (!connectionsManager) { return "Error: ConnectionsManager not initialized"; } const pendingRequests = connectionsManager.getPendingRequests(); const needsConfirmation = connectionsManager.getConnectionsNeedingConfirmation(); const allRequests = [...pendingRequests, ...needsConfirmation]; const request = allRequests.find( (r) => r.uniqueRequestKey === requestKey || r.connectionRequestId?.toString() === requestKey || r.inboundRequestId?.toString() === requestKey ); if (!request) { return `Error: Request with key ${requestKey} not found or no longer pending.`; } if (request.inboundRequestId) { connectionsManager.markConnectionRequestProcessed( request.targetInboundTopicId || "", request.inboundRequestId ); } else if (request.connectionRequestId) { connectionsManager.markConnectionRequestProcessed( request.originTopicId || "", request.connectionRequestId ); } return `Connection request from ${request.targetAgentName || `Agent ${request.targetAccountId}`} was rejected.`; } } export { ManageConnectionRequestsTool }; //# sourceMappingURL=standards-agent-kit.es12.js.map