@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.
114 lines (113 loc) • 4.31 kB
JavaScript
import { StructuredTool } from "@langchain/core/tools";
import { z } from "zod";
import { Logger } from "@hashgraphonline/standards-sdk";
class ListConnectionsTool extends StructuredTool {
constructor({ stateManager, hcsClient, ...rest }) {
super(rest);
this.name = "list_connections";
this.description = "Lists the currently active HCS-10 connections with detailed information. Shows connection status, agent details, and recent activity. Use this to get a comprehensive view of all active connections.";
this.schema = z.object({
includeDetails: z.boolean().optional().describe(
"Whether to include detailed information about each connection"
),
showPending: z.boolean().optional().describe("Whether to include pending connection requests")
});
this.stateManager = stateManager;
this.hcsClient = hcsClient;
this.logger = new Logger({ module: "ListConnectionsTool" });
}
async _call(args) {
const includeDetails = args.includeDetails ?? true;
const showPending = args.showPending ?? true;
const connections = await this.getEnhancedConnections();
if (connections.length === 0) {
return "There are currently no active connections.";
}
const activeConnections = connections.filter(
(c) => c.status === "established"
);
const pendingConnections = connections.filter((c) => c.isPending);
const needsConfirmation = connections.filter((c) => c.needsConfirmation);
let output = "";
if (activeConnections.length > 0) {
output += `🟢 Active Connections (${activeConnections.length}):
`;
activeConnections.forEach((conn, index) => {
output += this.formatConnection(conn, index, includeDetails);
});
output += "\n";
}
if (showPending && needsConfirmation.length > 0) {
output += `🟠 Connections Needing Confirmation (${needsConfirmation.length}):
`;
needsConfirmation.forEach((conn, index) => {
output += this.formatConnection(conn, index, includeDetails);
});
output += "\n";
}
if (showPending && pendingConnections.length > 0) {
output += `⚪ Pending Connection Requests (${pendingConnections.length}):
`;
pendingConnections.forEach((conn, index) => {
output += this.formatConnection(conn, index, includeDetails);
});
}
return output.trim();
}
formatConnection(conn, index, includeDetails) {
let output = `${index + 1}. ${conn.profileInfo?.display_name || conn.targetAgentName || "Unknown Agent"} (${conn.targetAccountId})
`;
const displayTopicId = conn.isPending ? "(Pending Request)" : conn.connectionTopicId;
output += ` Topic: ${displayTopicId}
`;
const statusText = conn.status || "unknown";
output += ` Status: ${statusText}
`;
if (includeDetails) {
if (conn.profileInfo?.bio) {
output += ` Bio: ${conn.profileInfo.bio.substring(0, 100)}${conn.profileInfo.bio.length > 100 ? "..." : ""}
`;
}
if (conn.created) {
const createdLabel = conn.isPending ? "Request sent" : "Connection established";
output += ` ${createdLabel}: ${conn.created.toLocaleString()}
`;
}
if (conn.lastActivity) {
output += ` Last activity: ${conn.lastActivity.toLocaleString()}
`;
}
}
return output;
}
async getEnhancedConnections() {
if (!this.hcsClient) {
return this.stateManager.listConnections();
}
try {
const { accountId } = this.hcsClient.getAccountAndSigner();
if (!accountId) {
return this.stateManager.listConnections();
}
const connectionManager = this.stateManager.getConnectionsManager();
if (!connectionManager) {
this.logger.error("ConnectionsManager not initialized");
return this.stateManager.listConnections();
}
const connections = await connectionManager.fetchConnectionData(
accountId
);
for (const connection of connections) {
this.stateManager.updateOrAddConnection(connection);
}
return connections;
} catch (error) {
console.error("Error fetching connection data:", error);
return this.stateManager.listConnections();
}
}
}
export {
ListConnectionsTool
};
//# sourceMappingURL=standards-agent-kit.es8.js.map