@hashgraphonline/standards-agent-kit 0.0.37 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/builders/hcs10/hcs10-builder.d.ts +262 -0
- package/dist/cjs/builders/index.d.ts +2 -0
- package/dist/cjs/builders/types.d.ts +10 -0
- package/dist/cjs/index.d.ts +3 -1
- package/dist/cjs/init/init.d.ts +21 -22
- package/dist/cjs/plugins/PluginInterface.d.ts +4 -80
- package/dist/cjs/plugins/PluginRegistry.d.ts +1 -47
- package/dist/cjs/plugins/defi/index.d.ts +7 -4
- package/dist/cjs/plugins/hedera/HbarPricePlugin.d.ts +7 -35
- package/dist/cjs/plugins/index.d.ts +0 -5
- package/dist/cjs/plugins/weather/index.d.ts +6 -5
- package/dist/cjs/standards-agent-kit.cjs +1 -1
- package/dist/cjs/standards-agent-kit.cjs.map +1 -1
- package/dist/cjs/state/state-types.d.ts +8 -8
- package/dist/cjs/tools/hcs10/AcceptConnectionRequestTool.d.ts +40 -0
- package/dist/cjs/tools/hcs10/CheckMessagesTool.d.ts +40 -0
- package/dist/cjs/tools/hcs10/ConnectionMonitorTool.d.ts +134 -0
- package/dist/cjs/tools/hcs10/FindRegistrationsTool.d.ts +34 -0
- package/dist/cjs/tools/hcs10/InitiateConnectionTool.d.ts +43 -0
- package/dist/cjs/tools/hcs10/ListConnectionsTool.d.ts +34 -0
- package/dist/cjs/tools/hcs10/ListUnapprovedConnectionRequestsTool.d.ts +15 -0
- package/dist/cjs/tools/hcs10/ManageConnectionRequestsTool.d.ts +34 -0
- package/dist/cjs/tools/hcs10/RegisterAgentTool.d.ts +218 -0
- package/dist/cjs/tools/hcs10/RetrieveProfileTool.d.ts +33 -0
- package/dist/cjs/tools/hcs10/SendMessageToConnectionTool.d.ts +40 -0
- package/dist/cjs/tools/hcs10/base-hcs10-tools.d.ts +28 -0
- package/dist/cjs/tools/hcs10/hcs10-tool-params.d.ts +18 -0
- package/dist/cjs/tools/hcs10/index.d.ts +13 -0
- package/dist/cjs/tools/index.d.ts +1 -13
- package/dist/es/builders/hcs10/hcs10-builder.d.ts +262 -0
- package/dist/es/builders/index.d.ts +2 -0
- package/dist/es/builders/types.d.ts +10 -0
- package/dist/es/index.d.ts +3 -1
- package/dist/es/init/init.d.ts +21 -22
- package/dist/es/plugins/PluginInterface.d.ts +4 -80
- package/dist/es/plugins/PluginRegistry.d.ts +1 -47
- package/dist/es/plugins/defi/index.d.ts +7 -4
- package/dist/es/plugins/hedera/HbarPricePlugin.d.ts +7 -35
- package/dist/es/plugins/index.d.ts +0 -5
- package/dist/es/plugins/weather/index.d.ts +6 -5
- package/dist/es/standards-agent-kit.es.js +10 -18
- package/dist/es/standards-agent-kit.es.js.map +1 -1
- package/dist/es/standards-agent-kit.es10.js +29 -76
- package/dist/es/standards-agent-kit.es10.js.map +1 -1
- package/dist/es/standards-agent-kit.es11.js +61 -384
- package/dist/es/standards-agent-kit.es11.js.map +1 -1
- package/dist/es/standards-agent-kit.es12.js +22 -190
- package/dist/es/standards-agent-kit.es12.js.map +1 -1
- package/dist/es/standards-agent-kit.es13.js +26 -138
- package/dist/es/standards-agent-kit.es13.js.map +1 -1
- package/dist/es/standards-agent-kit.es14.js +35 -59
- package/dist/es/standards-agent-kit.es14.js.map +1 -1
- package/dist/es/standards-agent-kit.es15.js +11 -117
- package/dist/es/standards-agent-kit.es15.js.map +1 -1
- package/dist/es/standards-agent-kit.es16.js +1 -1
- package/dist/es/standards-agent-kit.es16.js.map +1 -1
- package/dist/es/standards-agent-kit.es17.js +63 -46
- package/dist/es/standards-agent-kit.es17.js.map +1 -1
- package/dist/es/standards-agent-kit.es18.js +25 -74
- package/dist/es/standards-agent-kit.es18.js.map +1 -1
- package/dist/es/standards-agent-kit.es19.js +22 -13
- package/dist/es/standards-agent-kit.es19.js.map +1 -1
- package/dist/es/standards-agent-kit.es2.js +1381 -188
- package/dist/es/standards-agent-kit.es2.js.map +1 -1
- package/dist/es/standards-agent-kit.es20.js +3 -67
- package/dist/es/standards-agent-kit.es20.js.map +1 -1
- package/dist/es/standards-agent-kit.es3.js +285 -347
- package/dist/es/standards-agent-kit.es3.js.map +1 -1
- package/dist/es/standards-agent-kit.es4.js +22 -74
- package/dist/es/standards-agent-kit.es4.js.map +1 -1
- package/dist/es/standards-agent-kit.es5.js +127 -154
- package/dist/es/standards-agent-kit.es5.js.map +1 -1
- package/dist/es/standards-agent-kit.es6.js +21 -88
- package/dist/es/standards-agent-kit.es6.js.map +1 -1
- package/dist/es/standards-agent-kit.es7.js +28 -86
- package/dist/es/standards-agent-kit.es7.js.map +1 -1
- package/dist/es/standards-agent-kit.es8.js +27 -100
- package/dist/es/standards-agent-kit.es8.js.map +1 -1
- package/dist/es/standards-agent-kit.es9.js +35 -117
- package/dist/es/standards-agent-kit.es9.js.map +1 -1
- package/dist/es/state/state-types.d.ts +8 -8
- package/dist/es/tools/hcs10/AcceptConnectionRequestTool.d.ts +40 -0
- package/dist/es/tools/hcs10/CheckMessagesTool.d.ts +40 -0
- package/dist/es/tools/hcs10/ConnectionMonitorTool.d.ts +134 -0
- package/dist/es/tools/hcs10/FindRegistrationsTool.d.ts +34 -0
- package/dist/es/tools/hcs10/InitiateConnectionTool.d.ts +43 -0
- package/dist/es/tools/hcs10/ListConnectionsTool.d.ts +34 -0
- package/dist/es/tools/hcs10/ListUnapprovedConnectionRequestsTool.d.ts +15 -0
- package/dist/es/tools/hcs10/ManageConnectionRequestsTool.d.ts +34 -0
- package/dist/es/tools/hcs10/RegisterAgentTool.d.ts +218 -0
- package/dist/es/tools/hcs10/RetrieveProfileTool.d.ts +33 -0
- package/dist/es/tools/hcs10/SendMessageToConnectionTool.d.ts +40 -0
- package/dist/es/tools/hcs10/base-hcs10-tools.d.ts +28 -0
- package/dist/es/tools/hcs10/hcs10-tool-params.d.ts +18 -0
- package/dist/es/tools/hcs10/index.d.ts +13 -0
- package/dist/es/tools/index.d.ts +1 -13
- package/dist/umd/builders/hcs10/hcs10-builder.d.ts +262 -0
- package/dist/umd/builders/index.d.ts +2 -0
- package/dist/umd/builders/types.d.ts +10 -0
- package/dist/umd/index.d.ts +3 -1
- package/dist/umd/init/init.d.ts +21 -22
- package/dist/umd/plugins/PluginInterface.d.ts +4 -80
- package/dist/umd/plugins/PluginRegistry.d.ts +1 -47
- package/dist/umd/plugins/defi/index.d.ts +7 -4
- package/dist/umd/plugins/hedera/HbarPricePlugin.d.ts +7 -35
- package/dist/umd/plugins/index.d.ts +0 -5
- package/dist/umd/plugins/weather/index.d.ts +6 -5
- package/dist/umd/standards-agent-kit.umd.js +1 -164
- package/dist/umd/standards-agent-kit.umd.js.map +1 -1
- package/dist/umd/state/state-types.d.ts +8 -8
- package/dist/umd/tools/hcs10/AcceptConnectionRequestTool.d.ts +40 -0
- package/dist/umd/tools/hcs10/CheckMessagesTool.d.ts +40 -0
- package/dist/umd/tools/hcs10/ConnectionMonitorTool.d.ts +134 -0
- package/dist/umd/tools/hcs10/FindRegistrationsTool.d.ts +34 -0
- package/dist/umd/tools/hcs10/InitiateConnectionTool.d.ts +43 -0
- package/dist/umd/tools/hcs10/ListConnectionsTool.d.ts +34 -0
- package/dist/umd/tools/hcs10/ListUnapprovedConnectionRequestsTool.d.ts +15 -0
- package/dist/umd/tools/hcs10/ManageConnectionRequestsTool.d.ts +34 -0
- package/dist/umd/tools/hcs10/RegisterAgentTool.d.ts +218 -0
- package/dist/umd/tools/hcs10/RetrieveProfileTool.d.ts +33 -0
- package/dist/umd/tools/hcs10/SendMessageToConnectionTool.d.ts +40 -0
- package/dist/umd/tools/hcs10/base-hcs10-tools.d.ts +28 -0
- package/dist/umd/tools/hcs10/hcs10-tool-params.d.ts +18 -0
- package/dist/umd/tools/hcs10/index.d.ts +13 -0
- package/dist/umd/tools/index.d.ts +1 -13
- package/package.json +41 -41
- package/src/builders/hcs10/hcs10-builder.ts +2108 -0
- package/src/builders/index.ts +2 -0
- package/src/builders/types.ts +11 -0
- package/src/hcs10/HCS10Client.ts +3 -2
- package/src/index.ts +4 -2
- package/src/init/init.ts +85 -63
- package/src/plugins/PluginInterface.ts +15 -94
- package/src/plugins/PluginRegistry.ts +2 -98
- package/src/plugins/defi/index.ts +31 -118
- package/src/plugins/hedera/HbarPricePlugin.ts +19 -100
- package/src/plugins/index.ts +0 -5
- package/src/plugins/weather/index.ts +56 -40
- package/src/state/state-types.ts +9 -11
- package/src/tools/hcs10/AcceptConnectionRequestTool.ts +56 -0
- package/src/tools/hcs10/CheckMessagesTool.ts +73 -0
- package/src/tools/hcs10/ConnectionMonitorTool.ts +111 -0
- package/src/tools/hcs10/FindRegistrationsTool.ts +64 -0
- package/src/tools/hcs10/InitiateConnectionTool.ts +66 -0
- package/src/tools/hcs10/ListConnectionsTool.ts +58 -0
- package/src/tools/hcs10/ListUnapprovedConnectionRequestsTool.ts +26 -0
- package/src/tools/hcs10/ManageConnectionRequestsTool.ts +48 -0
- package/src/tools/hcs10/RegisterAgentTool.ts +213 -0
- package/src/tools/hcs10/RetrieveProfileTool.ts +61 -0
- package/src/tools/hcs10/SendMessageToConnectionTool.ts +45 -0
- package/src/tools/hcs10/base-hcs10-tools.ts +65 -0
- package/src/tools/hcs10/hcs10-tool-params.ts +21 -0
- package/src/tools/hcs10/index.ts +13 -0
- package/src/tools/index.ts +2 -13
- package/src/utils/HederaClient.ts +2 -2
- package/src/utils/ensure-agent-has-hbar.ts +3 -4
- package/dist/cjs/agents/index.d.ts +0 -0
- package/dist/cjs/plugins/BasePlugin.d.ts +0 -28
- package/dist/cjs/plugins/GenericPlugin.d.ts +0 -17
- package/dist/cjs/plugins/HCS10Plugin.d.ts +0 -13
- package/dist/cjs/plugins/PluginLoader.d.ts +0 -38
- package/dist/cjs/plugins/openconvai/OpenConvAIPlugin.d.ts +0 -23
- package/dist/cjs/plugins/openconvai/index.d.ts +0 -1
- package/dist/cjs/tools/AcceptConnectionRequestTool.d.ts +0 -33
- package/dist/cjs/tools/CheckMessagesTool.d.ts +0 -34
- package/dist/cjs/tools/ConnectionMonitorTool.d.ts +0 -100
- package/dist/cjs/tools/ConnectionTool.d.ts +0 -40
- package/dist/cjs/tools/FindRegistrationsTool.d.ts +0 -28
- package/dist/cjs/tools/InitiateConnectionTool.d.ts +0 -30
- package/dist/cjs/tools/ListConnectionsTool.d.ts +0 -33
- package/dist/cjs/tools/ListUnapprovedConnectionRequestsTool.d.ts +0 -41
- package/dist/cjs/tools/ManageConnectionRequestsTool.d.ts +0 -38
- package/dist/cjs/tools/RegisterAgentTool.d.ts +0 -188
- package/dist/cjs/tools/RetrieveProfileTool.d.ts +0 -34
- package/dist/cjs/tools/SendMessageToConnectionTool.d.ts +0 -34
- package/dist/cjs/tools/SendMessageTool.d.ts +0 -40
- package/dist/es/agents/index.d.ts +0 -0
- package/dist/es/plugins/BasePlugin.d.ts +0 -28
- package/dist/es/plugins/GenericPlugin.d.ts +0 -17
- package/dist/es/plugins/HCS10Plugin.d.ts +0 -13
- package/dist/es/plugins/PluginLoader.d.ts +0 -38
- package/dist/es/plugins/openconvai/OpenConvAIPlugin.d.ts +0 -23
- package/dist/es/plugins/openconvai/index.d.ts +0 -1
- package/dist/es/standards-agent-kit.es21.js +0 -92
- package/dist/es/standards-agent-kit.es21.js.map +0 -1
- package/dist/es/standards-agent-kit.es22.js +0 -7
- package/dist/es/standards-agent-kit.es22.js.map +0 -1
- package/dist/es/standards-agent-kit.es23.js +0 -106
- package/dist/es/standards-agent-kit.es23.js.map +0 -1
- package/dist/es/standards-agent-kit.es24.js +0 -7
- package/dist/es/standards-agent-kit.es24.js.map +0 -1
- package/dist/es/standards-agent-kit.es25.js +0 -7
- package/dist/es/standards-agent-kit.es25.js.map +0 -1
- package/dist/es/standards-agent-kit.es26.js +0 -85
- package/dist/es/standards-agent-kit.es26.js.map +0 -1
- package/dist/es/standards-agent-kit.es27.js +0 -28
- package/dist/es/standards-agent-kit.es27.js.map +0 -1
- package/dist/es/tools/AcceptConnectionRequestTool.d.ts +0 -33
- package/dist/es/tools/CheckMessagesTool.d.ts +0 -34
- package/dist/es/tools/ConnectionMonitorTool.d.ts +0 -100
- package/dist/es/tools/ConnectionTool.d.ts +0 -40
- package/dist/es/tools/FindRegistrationsTool.d.ts +0 -28
- package/dist/es/tools/InitiateConnectionTool.d.ts +0 -30
- package/dist/es/tools/ListConnectionsTool.d.ts +0 -33
- package/dist/es/tools/ListUnapprovedConnectionRequestsTool.d.ts +0 -41
- package/dist/es/tools/ManageConnectionRequestsTool.d.ts +0 -38
- package/dist/es/tools/RegisterAgentTool.d.ts +0 -188
- package/dist/es/tools/RetrieveProfileTool.d.ts +0 -34
- package/dist/es/tools/SendMessageToConnectionTool.d.ts +0 -34
- package/dist/es/tools/SendMessageTool.d.ts +0 -40
- package/dist/umd/agents/index.d.ts +0 -0
- package/dist/umd/plugins/BasePlugin.d.ts +0 -28
- package/dist/umd/plugins/GenericPlugin.d.ts +0 -17
- package/dist/umd/plugins/HCS10Plugin.d.ts +0 -13
- package/dist/umd/plugins/PluginLoader.d.ts +0 -38
- package/dist/umd/plugins/openconvai/OpenConvAIPlugin.d.ts +0 -23
- package/dist/umd/plugins/openconvai/index.d.ts +0 -1
- package/dist/umd/tools/AcceptConnectionRequestTool.d.ts +0 -33
- package/dist/umd/tools/CheckMessagesTool.d.ts +0 -34
- package/dist/umd/tools/ConnectionMonitorTool.d.ts +0 -100
- package/dist/umd/tools/ConnectionTool.d.ts +0 -40
- package/dist/umd/tools/FindRegistrationsTool.d.ts +0 -28
- package/dist/umd/tools/InitiateConnectionTool.d.ts +0 -30
- package/dist/umd/tools/ListConnectionsTool.d.ts +0 -33
- package/dist/umd/tools/ListUnapprovedConnectionRequestsTool.d.ts +0 -41
- package/dist/umd/tools/ManageConnectionRequestsTool.d.ts +0 -38
- package/dist/umd/tools/RegisterAgentTool.d.ts +0 -188
- package/dist/umd/tools/RetrieveProfileTool.d.ts +0 -34
- package/dist/umd/tools/SendMessageToConnectionTool.d.ts +0 -34
- package/dist/umd/tools/SendMessageTool.d.ts +0 -40
- package/src/agents/index.ts +0 -1
- package/src/plugins/BasePlugin.ts +0 -37
- package/src/plugins/GenericPlugin.ts +0 -19
- package/src/plugins/HCS10Plugin.ts +0 -14
- package/src/plugins/PluginLoader.ts +0 -121
- package/src/plugins/README.md +0 -113
- package/src/plugins/openconvai/OpenConvAIPlugin.ts +0 -124
- package/src/plugins/openconvai/index.ts +0 -1
- package/src/tools/AcceptConnectionRequestTool.ts +0 -196
- package/src/tools/CheckMessagesTool.ts +0 -194
- package/src/tools/ConnectionMonitorTool.ts +0 -550
- package/src/tools/ConnectionTool.ts +0 -231
- package/src/tools/FindRegistrationsTool.ts +0 -114
- package/src/tools/InitiateConnectionTool.ts +0 -137
- package/src/tools/ListConnectionsTool.ts +0 -158
- package/src/tools/ListUnapprovedConnectionRequestsTool.ts +0 -173
- package/src/tools/ManageConnectionRequestsTool.ts +0 -275
- package/src/tools/RegisterAgentTool.ts +0 -590
- package/src/tools/RetrieveProfileTool.ts +0 -94
- package/src/tools/SendMessageToConnectionTool.ts +0 -142
- package/src/tools/SendMessageTool.ts +0 -116
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
// src/tools/send-message-to-connection-tool.ts
|
|
2
|
-
|
|
3
|
-
import { StructuredTool, ToolParams } from '@langchain/core/tools';
|
|
4
|
-
import { z } from 'zod';
|
|
5
|
-
import { HCS10Client } from '../hcs10/HCS10Client';
|
|
6
|
-
import { IStateManager } from '../state/state-types';
|
|
7
|
-
import { Logger } from '@hashgraphonline/standards-sdk'; // Assuming logger utility
|
|
8
|
-
|
|
9
|
-
export interface SendMessageToConnectionToolParams extends ToolParams {
|
|
10
|
-
hcsClient: HCS10Client;
|
|
11
|
-
stateManager: IStateManager;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* A tool to send a message to an agent over an established HCS-10 connection.
|
|
16
|
-
*/
|
|
17
|
-
export class SendMessageToConnectionTool extends StructuredTool {
|
|
18
|
-
name = 'send_message_to_connection';
|
|
19
|
-
description =
|
|
20
|
-
"Sends a text message to another agent using an existing active connection. Identify the target agent using their account ID (e.g., 0.0.12345) or the connection number shown in 'list_connections'. Return back the reply from the target agent if possible";
|
|
21
|
-
schema = z.object({
|
|
22
|
-
targetIdentifier: z
|
|
23
|
-
.string()
|
|
24
|
-
.describe(
|
|
25
|
-
"The account ID (e.g., 0.0.12345) of the target agent OR the connection number (e.g., '1', '2') from the 'list_connections' tool."
|
|
26
|
-
),
|
|
27
|
-
message: z.string().describe('The text message content to send.'),
|
|
28
|
-
disableMonitoring: z.boolean().optional().default(false),
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
private hcsClient: HCS10Client;
|
|
32
|
-
private stateManager: IStateManager;
|
|
33
|
-
private logger: Logger;
|
|
34
|
-
|
|
35
|
-
constructor({
|
|
36
|
-
hcsClient,
|
|
37
|
-
stateManager,
|
|
38
|
-
...rest
|
|
39
|
-
}: SendMessageToConnectionToolParams) {
|
|
40
|
-
super(rest);
|
|
41
|
-
this.hcsClient = hcsClient;
|
|
42
|
-
this.stateManager = stateManager;
|
|
43
|
-
this.logger = Logger.getInstance({ module: 'SendMessageToConnectionTool' });
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
protected async _call({
|
|
47
|
-
targetIdentifier,
|
|
48
|
-
message,
|
|
49
|
-
disableMonitoring,
|
|
50
|
-
}: z.infer<this['schema']>): Promise<string> {
|
|
51
|
-
const currentAgent = this.stateManager.getCurrentAgent();
|
|
52
|
-
if (!currentAgent) {
|
|
53
|
-
return 'Error: Cannot send message. No agent is currently active. Please register or select an agent first.';
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const connection =
|
|
57
|
-
this.stateManager.getConnectionByIdentifier(targetIdentifier);
|
|
58
|
-
if (!connection) {
|
|
59
|
-
return `Error: Could not find an active connection matching identifier "${targetIdentifier}". Use 'list_connections' to see active connections.`;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const connectionTopicId = connection.connectionTopicId;
|
|
63
|
-
const targetAgentName = connection.targetAgentName;
|
|
64
|
-
|
|
65
|
-
// Construct the sender's operator ID
|
|
66
|
-
const operatorId = `${currentAgent.inboundTopicId}@${currentAgent.accountId}`;
|
|
67
|
-
|
|
68
|
-
this.logger.info(
|
|
69
|
-
`Sending message from ${operatorId} to ${targetAgentName} (${connection.targetAccountId}) via connection topic ${connectionTopicId}`
|
|
70
|
-
);
|
|
71
|
-
|
|
72
|
-
try {
|
|
73
|
-
// Call sendMessage with correct arguments
|
|
74
|
-
const sequenceNumber = await this.hcsClient.sendMessage(
|
|
75
|
-
connectionTopicId,
|
|
76
|
-
message, // Message content as 'data'
|
|
77
|
-
`Agent message from ${currentAgent.name}` // Optional memo
|
|
78
|
-
);
|
|
79
|
-
|
|
80
|
-
if (!sequenceNumber) {
|
|
81
|
-
throw new Error('Failed to send message');
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
if (!disableMonitoring) {
|
|
85
|
-
return `Message sent to ${targetAgentName} (${connection.targetAccountId}) via connection ${connectionTopicId}. Sequence Number: ${sequenceNumber}`;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const replyBack = await this.monitorResponses(
|
|
89
|
-
connectionTopicId,
|
|
90
|
-
operatorId,
|
|
91
|
-
sequenceNumber
|
|
92
|
-
);
|
|
93
|
-
|
|
94
|
-
if (replyBack) {
|
|
95
|
-
this.logger.info(`Got reply from ${targetAgentName}`, replyBack);
|
|
96
|
-
// Format the return string clearly as an observation/reply
|
|
97
|
-
return `Received reply from ${targetAgentName}: ${replyBack}`;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// Return message based on the status string if no reply was received/awaited
|
|
101
|
-
return `Message sent to ${targetAgentName} (${connection.targetAccountId}) via connection ${connectionTopicId}. Sequence Number: ${sequenceNumber}`;
|
|
102
|
-
} catch (error) {
|
|
103
|
-
this.logger.error(
|
|
104
|
-
`Failed to send message via connection ${connectionTopicId}: ${error}`
|
|
105
|
-
);
|
|
106
|
-
return `Error sending message to ${targetAgentName}: ${
|
|
107
|
-
error instanceof Error ? error.message : String(error)
|
|
108
|
-
}`;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
private async monitorResponses(
|
|
113
|
-
topicId: string,
|
|
114
|
-
operatorId: string,
|
|
115
|
-
sequenceNumber: number
|
|
116
|
-
): Promise<string | null> {
|
|
117
|
-
const maxAttempts = 30;
|
|
118
|
-
const attempts = 0;
|
|
119
|
-
|
|
120
|
-
while (attempts < maxAttempts) {
|
|
121
|
-
try {
|
|
122
|
-
const messages = await this.hcsClient.getMessageStream(topicId);
|
|
123
|
-
|
|
124
|
-
for (const message of messages.messages) {
|
|
125
|
-
if (
|
|
126
|
-
message.sequence_number < sequenceNumber ||
|
|
127
|
-
message.operator_id === operatorId
|
|
128
|
-
) {
|
|
129
|
-
continue;
|
|
130
|
-
}
|
|
131
|
-
const content = await this.hcsClient.getMessageContent(message.data || '');
|
|
132
|
-
|
|
133
|
-
return content;
|
|
134
|
-
}
|
|
135
|
-
} catch (error) {
|
|
136
|
-
this.logger.error(`Error monitoring responses: ${error}`);
|
|
137
|
-
}
|
|
138
|
-
await new Promise((resolve) => setTimeout(resolve, 4000));
|
|
139
|
-
}
|
|
140
|
-
return null;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import { HCS10Client } from '../hcs10/HCS10Client';
|
|
2
|
-
import { StructuredTool } from '@langchain/core/tools';
|
|
3
|
-
import { z } from 'zod';
|
|
4
|
-
import { Logger } from '@hashgraphonline/standards-sdk';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* SendMessageTool wraps the sendMessage() function of HCS10Client.
|
|
8
|
-
* It sends a message to a specified Hedera topic and monitors for responses.
|
|
9
|
-
*/
|
|
10
|
-
export class SendMessageTool extends StructuredTool {
|
|
11
|
-
name = 'send_message';
|
|
12
|
-
description =
|
|
13
|
-
'Sends a message to a specified Hedera topic using HCS-10 and monitors for responses.';
|
|
14
|
-
private client: HCS10Client;
|
|
15
|
-
private lastProcessedTimestamp: number = 0;
|
|
16
|
-
private logger: Logger;
|
|
17
|
-
|
|
18
|
-
schema = z.object({
|
|
19
|
-
topicId: z.string().describe('The Hedera topic ID to send the message to'),
|
|
20
|
-
message: z.string().describe('The message content to send'),
|
|
21
|
-
disableMonitoring: z
|
|
22
|
-
.boolean()
|
|
23
|
-
.optional()
|
|
24
|
-
.describe('Whether to disable monitoring for responses'),
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* @param client - Instance of HCS10Client.
|
|
29
|
-
*/
|
|
30
|
-
constructor(client: HCS10Client) {
|
|
31
|
-
super();
|
|
32
|
-
this.client = client;
|
|
33
|
-
this.logger = Logger.getInstance({ module: 'SendMessageTool' });
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Calls sendMessage() with the provided parameters.
|
|
38
|
-
*/
|
|
39
|
-
async _call(input: {
|
|
40
|
-
topicId: string;
|
|
41
|
-
message: string;
|
|
42
|
-
disableMonitoring: boolean;
|
|
43
|
-
}): Promise<string> {
|
|
44
|
-
try {
|
|
45
|
-
const result = await this.client.sendMessage(
|
|
46
|
-
input.topicId,
|
|
47
|
-
input.message
|
|
48
|
-
);
|
|
49
|
-
if (!result) {
|
|
50
|
-
throw new Error('Failed to send message');
|
|
51
|
-
}
|
|
52
|
-
this.logger.info(`Message sent with sequence number ${result}`);
|
|
53
|
-
if (!input.disableMonitoring) {
|
|
54
|
-
const response = await this.monitorResponses(input.topicId, result);
|
|
55
|
-
return `Successfully sent message to topic ${input.topicId}${
|
|
56
|
-
response ? `\nResponse: ${response}` : ''
|
|
57
|
-
}`;
|
|
58
|
-
} else {
|
|
59
|
-
return `Successfully sent message to topic ${input.topicId}`;
|
|
60
|
-
}
|
|
61
|
-
} catch (error) {
|
|
62
|
-
throw new Error(
|
|
63
|
-
`Failed to send message: ${
|
|
64
|
-
error instanceof Error ? error.message : String(error)
|
|
65
|
-
}`
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
private async monitorResponses(
|
|
71
|
-
topicId: string,
|
|
72
|
-
sequenceNumber: number
|
|
73
|
-
): Promise<string | null> {
|
|
74
|
-
const maxAttempts = 10;
|
|
75
|
-
let attempts = 0;
|
|
76
|
-
|
|
77
|
-
while (attempts < maxAttempts) {
|
|
78
|
-
try {
|
|
79
|
-
const messages = await this.client.getMessageStream(topicId);
|
|
80
|
-
|
|
81
|
-
for (const message of messages.messages) {
|
|
82
|
-
if (
|
|
83
|
-
message.created &&
|
|
84
|
-
message.created.getTime() > this.lastProcessedTimestamp
|
|
85
|
-
) {
|
|
86
|
-
this.lastProcessedTimestamp = message.created.getTime();
|
|
87
|
-
|
|
88
|
-
const content = await this.client.getMessageContent(message.data || '');
|
|
89
|
-
|
|
90
|
-
let parsedContent;
|
|
91
|
-
try {
|
|
92
|
-
parsedContent = JSON.parse(content);
|
|
93
|
-
} catch (error) {
|
|
94
|
-
this.logger.error(`Error parsing message content: ${error}`);
|
|
95
|
-
continue;
|
|
96
|
-
}
|
|
97
|
-
if (message.sequence_number > sequenceNumber) {
|
|
98
|
-
// Unwrap nested data field if present
|
|
99
|
-
if (parsedContent && typeof parsedContent.data === 'string') {
|
|
100
|
-
return parsedContent.data;
|
|
101
|
-
}
|
|
102
|
-
return JSON.stringify(parsedContent);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
} catch (error) {
|
|
107
|
-
this.logger.error(`Error monitoring responses: ${error}`);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
attempts++;
|
|
111
|
-
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
return null;
|
|
115
|
-
}
|
|
116
|
-
}
|