@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.
148 lines (147 loc) • 6.21 kB
JavaScript
import { StructuredTool } from "@langchain/core/tools";
import { z } from "zod";
import { Logger, FeeConfigBuilder } from "@hashgraphonline/standards-sdk";
class AcceptConnectionRequestTool extends StructuredTool {
constructor({
hcsClient,
stateManager,
...rest
}) {
super(rest);
this.name = "accept_connection_request";
this.description = "Accepts a specific pending connection request from another agent, establishing a communication channel.";
this.schema = z.object({
requestKey: z.string().describe(
'The unique request key of the specific request to accept. Use the "manage_connection_requests" tool with action="list" first to get valid keys.'
),
hbarFee: z.number().optional().describe(
"Optional HBAR fee amount to charge the connecting agent per message on the new connection topic."
),
exemptAccountIds: z.array(z.string()).optional().describe(
"Optional list of account IDs to exempt from any configured fees on the new connection topic."
)
});
this.hcsClient = hcsClient;
this.stateManager = stateManager;
this.logger = Logger.getInstance({
module: "AcceptConnectionRequestTool"
});
}
async _call({
requestKey,
hbarFee,
exemptAccountIds
}) {
const currentAgent = this.stateManager.getCurrentAgent();
if (!currentAgent) {
return "Error: Cannot accept connection request. No agent is currently active. Please register or select an agent first.";
}
const connectionsManager = this.stateManager.getConnectionsManager();
if (!connectionsManager) {
return "Error: ConnectionsManager not initialized";
}
await connectionsManager.fetchConnectionData(currentAgent.accountId);
const allRequests = [
...connectionsManager.getPendingRequests(),
...connectionsManager.getConnectionsNeedingConfirmation()
];
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. Use the manage_connection_requests tool with action="list" to verify.`;
}
const numericRequestId = request.connectionRequestId || request.inboundRequestId;
if (!numericRequestId) {
return `Error: Could not determine a valid request ID for the request with key ${requestKey}.`;
}
try {
const inboundTopicId = await this.hcsClient.getInboundTopicId();
let feeConfigBuilder = void 0;
if (hbarFee && hbarFee > 0) {
const collectorId = this.hcsClient.getAccountAndSigner().accountId;
try {
feeConfigBuilder = new FeeConfigBuilder({
network: this.hcsClient.getNetwork(),
logger: this.logger,
defaultCollectorAccountId: collectorId
});
const finalExemptions = [
...exemptAccountIds || [],
currentAgent.accountId
];
feeConfigBuilder.addHbarFee(hbarFee, collectorId, finalExemptions);
this.logger.info(
`Setting HBAR fee: ${hbarFee} HBAR to be collected by ${collectorId}`
);
} catch (feeConfigError) {
this.logger.error(
`Error creating fee configuration: ${feeConfigError}`
);
feeConfigBuilder = void 0;
this.logger.warn(
"Proceeding to accept request without fees due to configuration error."
);
}
}
this.logger.info(
`Attempting to accept request Key: ${requestKey} (ID: ${numericRequestId}) from ${request.targetAccountId}`
);
const result = await this.hcsClient.handleConnectionRequest(
inboundTopicId,
request.targetAccountId,
numericRequestId,
feeConfigBuilder
);
if (!result?.connectionTopicId) {
return `Error: Failed to accept connection request with key ${requestKey}. The SDK did not return a connection topic ID.`;
}
this.logger.info(
`Successfully created connection topic: ${result.connectionTopicId}`
);
const connectionTopicId = result.connectionTopicId;
let targetInboundTopic = "";
try {
const targetProfileData = await this.hcsClient.standardClient.retrieveProfile(request.targetAccountId);
targetInboundTopic = targetProfileData?.topicInfo?.inboundTopic || "";
if (!targetInboundTopic) {
this.logger.warn(
`Could not resolve target inbound topic for ${request.targetAccountId}`
);
}
} catch (e) {
this.logger.warn(
`Error fetching target profile/topic for ${request.targetAccountId}: ${e}`
);
}
const name = request.profileInfo?.display_name || request.profileInfo?.alias || `Agent ${request.targetAccountId}`;
const newConnection = {
targetAccountId: request.targetAccountId,
targetAgentName: name,
targetInboundTopicId: targetInboundTopic,
connectionTopicId,
profileInfo: request.profileInfo,
created: /* @__PURE__ */ new Date(),
status: "established"
};
this.stateManager.addActiveConnection(newConnection);
connectionsManager.fetchConnectionData(request.targetAccountId);
this.logger.info(`Removed request ${requestKey} from pending requests`);
let feeMessage = "";
if (hbarFee && hbarFee > 0 && feeConfigBuilder) {
feeMessage = ` with a ${hbarFee} HBAR fee per message`;
}
const displayKey = request.uniqueRequestKey || requestKey;
return `Successfully accepted connection request ${displayKey} from ${name} ${feeMessage}. Connection established on topic: ${connectionTopicId}.`;
} catch (error) {
this.logger.error(
`Error accepting connection request ${requestKey}: ${error}`
);
return `Error accepting connection request ${requestKey}: ${error instanceof Error ? error.message : String(error)}`;
}
}
}
export {
AcceptConnectionRequestTool
};
//# sourceMappingURL=standards-agent-kit.es13.js.map