@hashgraphonline/conversational-agent
Version:
Hashgraph Online conversational AI agent implementing HCS-10 communication, HCS-2 registries, and content inscription on Hedera
171 lines (170 loc) • 5.98 kB
JavaScript
import { BasePlugin } from "hedera-agent-kit";
import { OpenConvaiState, HCS10Builder, RegisterAgentTool, FindRegistrationsTool, RetrieveProfileTool, InitiateConnectionTool, ListConnectionsTool, SendMessageToConnectionTool, CheckMessagesTool, ConnectionMonitorTool, ManageConnectionRequestsTool, AcceptConnectionRequestTool, ListUnapprovedConnectionRequestsTool } from "@hashgraphonline/standards-agent-kit";
import { HCS10Client } from "@hashgraphonline/standards-sdk";
class HCS10Plugin extends BasePlugin {
constructor() {
super(...arguments);
this.id = "hcs-10";
this.name = "HCS-10 Plugin";
this.description = "HCS-10 agent tools for decentralized agent registration, connections, and messaging on Hedera";
this.version = "1.0.0";
this.author = "Hashgraph Online";
this.namespace = "hcs10";
this.tools = [];
}
async initialize(context) {
await super.initialize(context);
const hederaKit = context.config.hederaKit;
if (!hederaKit) {
this.context.logger.warn(
"HederaKit not found in context. HCS-10 tools will not be available."
);
return;
}
try {
this.stateManager = context.stateManager || context.config.stateManager || this.appConfig?.stateManager || new OpenConvaiState();
const accountId = hederaKit.signer.getAccountId().toString();
let inboundTopicId = "";
let outboundTopicId = "";
try {
const opKey = hederaKit.signer.getOperatorPrivateKey();
const privateKey = typeof opKey?.toStringRaw === "function" ? opKey.toStringRaw() : typeof opKey?.toString === "function" ? opKey.toString() : String(opKey);
const hcs10Client = new HCS10Client({
network: hederaKit.network,
operatorId: accountId,
operatorPrivateKey: privateKey,
logLevel: "error"
});
const profileResponse = await hcs10Client.retrieveProfile(accountId);
if (profileResponse.success && profileResponse.topicInfo) {
inboundTopicId = profileResponse.topicInfo.inboundTopic;
outboundTopicId = profileResponse.topicInfo.outboundTopic;
}
} catch (profileError) {
this.context.logger.warn(
"Could not retrieve profile topics:",
profileError
);
}
this.stateManager.setCurrentAgent({
name: `Agent ${accountId}`,
accountId,
inboundTopicId,
outboundTopicId,
privateKey: (() => {
const opKey = hederaKit.signer.getOperatorPrivateKey();
return typeof opKey?.toStringRaw === "function" ? opKey.toStringRaw() : typeof opKey?.toString === "function" ? opKey.toString() : String(opKey);
})()
});
this.context.logger.info(
`Set current agent: ${accountId} with topics ${inboundTopicId}/${outboundTopicId}`
);
if (this.stateManager && !this.stateManager.getConnectionsManager()) {
try {
const opKey = hederaKit.signer.getOperatorPrivateKey();
const privateKey = typeof opKey?.toStringRaw === "function" ? opKey.toStringRaw() : typeof opKey?.toString === "function" ? opKey.toString() : String(opKey);
const hcs10Client = new HCS10Client({
network: hederaKit.network,
operatorId: accountId,
operatorPrivateKey: privateKey,
logLevel: "error"
});
this.stateManager.initializeConnectionsManager(hcs10Client);
this.context.logger.info(
"ConnectionsManager initialized in HCS10Plugin"
);
} catch (cmError) {
this.context.logger.warn("Could not initialize ConnectionsManager:", cmError);
}
}
this.initializeTools();
this.context.logger.info("HCS-10 Plugin initialized successfully");
} catch (error) {
this.context.logger.error("Failed to initialize HCS-10 plugin:", error);
}
}
initializeTools() {
if (!this.stateManager) {
throw new Error("StateManager must be initialized before creating tools");
}
const hederaKit = this.context.config.hederaKit;
if (!hederaKit) {
throw new Error("HederaKit not found in context config");
}
const hcs10Builder = new HCS10Builder(hederaKit, this.stateManager);
this.tools = [
new RegisterAgentTool({
hederaKit,
hcs10Builder,
logger: this.context.logger
}),
new FindRegistrationsTool({
hederaKit,
hcs10Builder,
logger: this.context.logger
}),
new RetrieveProfileTool({
hederaKit,
hcs10Builder,
logger: this.context.logger
}),
new InitiateConnectionTool({
hederaKit,
hcs10Builder,
logger: this.context.logger
}),
new ListConnectionsTool({
hederaKit,
hcs10Builder,
logger: this.context.logger
}),
new SendMessageToConnectionTool({
hederaKit,
hcs10Builder,
logger: this.context.logger
}),
new CheckMessagesTool({
hederaKit,
hcs10Builder,
logger: this.context.logger
}),
new ConnectionMonitorTool({
hederaKit,
hcs10Builder,
logger: this.context.logger
}),
new ManageConnectionRequestsTool({
hederaKit,
hcs10Builder,
logger: this.context.logger
}),
new AcceptConnectionRequestTool({
hederaKit,
hcs10Builder,
logger: this.context.logger
}),
new ListUnapprovedConnectionRequestsTool({
hederaKit,
hcs10Builder,
logger: this.context.logger
})
];
}
getTools() {
return this.tools;
}
getStateManager() {
return this.stateManager;
}
async cleanup() {
this.tools = [];
delete this.stateManager;
if (this.context?.logger) {
this.context.logger.info("HCS-10 Plugin cleaned up");
}
}
}
export {
HCS10Plugin
};
//# sourceMappingURL=index2.js.map