@hashgraphonline/standards-agent-kit 0.0.32 → 0.0.33
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/src/init/index.d.ts +1 -0
- package/dist/cjs/src/{init.d.ts → init/init.d.ts} +16 -16
- package/dist/cjs/standards-agent-kit.cjs +1 -1
- package/dist/cjs/standards-agent-kit.cjs.map +1 -1
- package/dist/es/src/init/index.d.ts +1 -0
- package/dist/es/src/{init.d.ts → init/init.d.ts} +16 -16
- package/dist/es/standards-agent-kit.es.js +17 -17
- package/dist/es/standards-agent-kit.es10.js +65 -104
- package/dist/es/standards-agent-kit.es10.js.map +1 -1
- package/dist/es/standards-agent-kit.es11.js +371 -66
- package/dist/es/standards-agent-kit.es11.js.map +1 -1
- package/dist/es/standards-agent-kit.es12.js +153 -348
- package/dist/es/standards-agent-kit.es12.js.map +1 -1
- package/dist/es/standards-agent-kit.es13.js +105 -161
- package/dist/es/standards-agent-kit.es13.js.map +1 -1
- package/dist/es/standards-agent-kit.es14.js +48 -126
- package/dist/es/standards-agent-kit.es14.js.map +1 -1
- package/dist/es/standards-agent-kit.es15.js +108 -50
- package/dist/es/standards-agent-kit.es15.js.map +1 -1
- package/dist/es/standards-agent-kit.es16.js +239 -117
- package/dist/es/standards-agent-kit.es16.js.map +1 -1
- package/dist/es/standards-agent-kit.es17.js +120 -245
- package/dist/es/standards-agent-kit.es17.js.map +1 -1
- package/dist/es/standards-agent-kit.es2.js +321 -114
- package/dist/es/standards-agent-kit.es2.js.map +1 -1
- package/dist/es/standards-agent-kit.es23.js +15 -15
- package/dist/es/standards-agent-kit.es24.js +3 -24
- package/dist/es/standards-agent-kit.es24.js.map +1 -1
- package/dist/es/standards-agent-kit.es25.js +81 -3
- package/dist/es/standards-agent-kit.es25.js.map +1 -1
- package/dist/es/standards-agent-kit.es26.js +22 -79
- package/dist/es/standards-agent-kit.es26.js.map +1 -1
- package/dist/es/standards-agent-kit.es3.js +347 -284
- package/dist/es/standards-agent-kit.es3.js.map +1 -1
- package/dist/es/standards-agent-kit.es4.js +56 -366
- package/dist/es/standards-agent-kit.es4.js.map +1 -1
- package/dist/es/standards-agent-kit.es5.js +142 -58
- package/dist/es/standards-agent-kit.es5.js.map +1 -1
- package/dist/es/standards-agent-kit.es6.js +73 -143
- package/dist/es/standards-agent-kit.es6.js.map +1 -1
- package/dist/es/standards-agent-kit.es7.js +65 -64
- package/dist/es/standards-agent-kit.es7.js.map +1 -1
- package/dist/es/standards-agent-kit.es8.js +91 -77
- package/dist/es/standards-agent-kit.es8.js.map +1 -1
- package/dist/es/standards-agent-kit.es9.js +109 -90
- package/dist/es/standards-agent-kit.es9.js.map +1 -1
- package/dist/umd/src/init/index.d.ts +1 -0
- package/dist/umd/src/{init.d.ts → init/init.d.ts} +16 -16
- package/dist/umd/standards-agent-kit.umd.js +2 -2
- package/dist/umd/standards-agent-kit.umd.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -1
- package/src/init/index.ts +1 -0
- package/src/{init.ts → init/init.ts} +22 -20
|
@@ -1,113 +1,132 @@
|
|
|
1
1
|
import { StructuredTool } from "@langchain/core/tools";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
import { Logger } from "@hashgraphonline/standards-sdk";
|
|
4
|
-
class
|
|
5
|
-
constructor({
|
|
4
|
+
class CheckMessagesTool extends StructuredTool {
|
|
5
|
+
constructor({ hcsClient, stateManager, ...rest }) {
|
|
6
6
|
super(rest);
|
|
7
|
-
this.name = "
|
|
8
|
-
this.description =
|
|
7
|
+
this.name = "check_messages";
|
|
8
|
+
this.description = `Checks for and retrieves messages from an active connection.
|
|
9
|
+
Identify the target agent using their account ID (e.g., 0.0.12345) or the connection number shown in 'list_connections'.
|
|
10
|
+
By default, it only retrieves messages newer than the last check.
|
|
11
|
+
Use 'fetchLatest: true' to get the most recent messages regardless of when they arrived.
|
|
12
|
+
Use 'lastMessagesCount' to specify how many latest messages to retrieve (default 1 when fetchLatest is true).`;
|
|
9
13
|
this.schema = z.object({
|
|
10
|
-
|
|
11
|
-
"
|
|
14
|
+
targetIdentifier: z.string().describe(
|
|
15
|
+
"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 to check messages for."
|
|
12
16
|
),
|
|
13
|
-
|
|
17
|
+
fetchLatest: z.boolean().optional().default(false).describe(
|
|
18
|
+
"Set to true to fetch the latest messages even if they have been seen before, ignoring the last checked timestamp. Defaults to false (fetching only new messages)."
|
|
19
|
+
),
|
|
20
|
+
lastMessagesCount: z.number().int().positive().optional().describe(
|
|
21
|
+
"When fetchLatest is true, specifies how many of the most recent messages to retrieve. Defaults to 1."
|
|
22
|
+
)
|
|
14
23
|
});
|
|
15
|
-
this.stateManager = stateManager;
|
|
16
24
|
this.hcsClient = hcsClient;
|
|
17
|
-
this.
|
|
25
|
+
this.stateManager = stateManager;
|
|
26
|
+
this.logger = Logger.getInstance({ module: "CheckMessagesTool" });
|
|
18
27
|
}
|
|
19
|
-
async _call(
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
28
|
+
async _call({
|
|
29
|
+
targetIdentifier,
|
|
30
|
+
fetchLatest,
|
|
31
|
+
lastMessagesCount
|
|
32
|
+
}) {
|
|
33
|
+
const connection = this.stateManager.getConnectionByIdentifier(targetIdentifier);
|
|
34
|
+
if (!connection) {
|
|
35
|
+
return `Error: Could not find an active connection matching identifier "${targetIdentifier}". Use 'list_connections' to see active connections.`;
|
|
25
36
|
}
|
|
26
|
-
const
|
|
27
|
-
|
|
37
|
+
const connectionTopicId = connection.connectionTopicId;
|
|
38
|
+
const targetAgentName = connection.targetAgentName;
|
|
39
|
+
const lastProcessedTimestamp = this.stateManager.getLastTimestamp(connectionTopicId);
|
|
40
|
+
this.logger.info(
|
|
41
|
+
`Checking messages for connection with ${targetAgentName} (${connection.targetAccountId}) on topic ${connectionTopicId} (fetchLatest: ${fetchLatest}, lastCount: ${lastMessagesCount}, since: ${lastProcessedTimestamp})`
|
|
28
42
|
);
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
`;
|
|
35
|
-
activeConnections.forEach((conn, index) => {
|
|
36
|
-
output += this.formatConnection(conn, index, includeDetails);
|
|
37
|
-
});
|
|
38
|
-
output += "\n";
|
|
39
|
-
}
|
|
40
|
-
if (showPending && needsConfirmation.length > 0) {
|
|
41
|
-
output += `🟠 Connections Needing Confirmation (${needsConfirmation.length}):
|
|
42
|
-
`;
|
|
43
|
-
needsConfirmation.forEach((conn, index) => {
|
|
44
|
-
output += this.formatConnection(conn, index, includeDetails);
|
|
45
|
-
});
|
|
46
|
-
output += "\n";
|
|
47
|
-
}
|
|
48
|
-
if (showPending && pendingConnections.length > 0) {
|
|
49
|
-
output += `⚪ Pending Connection Requests (${pendingConnections.length}):
|
|
50
|
-
`;
|
|
51
|
-
pendingConnections.forEach((conn, index) => {
|
|
52
|
-
output += this.formatConnection(conn, index, includeDetails);
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
return output.trim();
|
|
56
|
-
}
|
|
57
|
-
formatConnection(conn, index, includeDetails) {
|
|
58
|
-
let output = `${index + 1}. ${conn.profileInfo?.display_name || conn.targetAgentName || "Unknown Agent"} (${conn.targetAccountId})
|
|
59
|
-
`;
|
|
60
|
-
const displayTopicId = conn.isPending ? "(Pending Request)" : conn.connectionTopicId;
|
|
61
|
-
output += ` Topic: ${displayTopicId}
|
|
62
|
-
`;
|
|
63
|
-
const statusText = conn.status || "unknown";
|
|
64
|
-
output += ` Status: ${statusText}
|
|
65
|
-
`;
|
|
66
|
-
if (includeDetails) {
|
|
67
|
-
if (conn.profileInfo?.bio) {
|
|
68
|
-
output += ` Bio: ${conn.profileInfo.bio.substring(0, 100)}${conn.profileInfo.bio.length > 100 ? "..." : ""}
|
|
69
|
-
`;
|
|
43
|
+
try {
|
|
44
|
+
const result = await this.hcsClient.getMessages(connectionTopicId);
|
|
45
|
+
const allMessages = result.messages;
|
|
46
|
+
if (!allMessages || allMessages.length === 0) {
|
|
47
|
+
return `No messages found on connection topic ${connectionTopicId}.`;
|
|
70
48
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
49
|
+
let messagesToProcess = [];
|
|
50
|
+
let latestTimestampNanos = lastProcessedTimestamp;
|
|
51
|
+
const isFetchingLatest = fetchLatest === true;
|
|
52
|
+
if (isFetchingLatest) {
|
|
53
|
+
this.logger.info("Fetching latest messages regardless of timestamp.");
|
|
54
|
+
const count = lastMessagesCount ?? 1;
|
|
55
|
+
messagesToProcess = allMessages.slice(-count);
|
|
56
|
+
} else {
|
|
57
|
+
this.logger.info(
|
|
58
|
+
`Filtering for messages newer than ${lastProcessedTimestamp}`
|
|
59
|
+
);
|
|
60
|
+
messagesToProcess = allMessages.filter((msg) => {
|
|
61
|
+
const msgTimestampNanos = msg.timestamp * 1e6;
|
|
62
|
+
return msgTimestampNanos > lastProcessedTimestamp;
|
|
63
|
+
});
|
|
64
|
+
if (messagesToProcess.length > 0) {
|
|
65
|
+
latestTimestampNanos = messagesToProcess.reduce(
|
|
66
|
+
(maxTs, msg) => Math.max(maxTs, msg.timestamp * 1e6),
|
|
67
|
+
lastProcessedTimestamp
|
|
68
|
+
);
|
|
69
|
+
}
|
|
75
70
|
}
|
|
76
|
-
if (
|
|
77
|
-
|
|
78
|
-
`;
|
|
71
|
+
if (messagesToProcess.length === 0) {
|
|
72
|
+
return isFetchingLatest ? `Could not retrieve the latest message(s). No messages found on topic ${connectionTopicId}.` : `No new messages found for connection with ${targetAgentName} since last check.`;
|
|
79
73
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
74
|
+
this.logger.info(`Processing ${messagesToProcess.length} message(s).`);
|
|
75
|
+
let outputString = isFetchingLatest ? `Latest message(s) from ${targetAgentName}:
|
|
76
|
+
` : `New messages from ${targetAgentName}:
|
|
77
|
+
`;
|
|
78
|
+
for (const msg of messagesToProcess) {
|
|
79
|
+
let content = msg.data;
|
|
80
|
+
try {
|
|
81
|
+
if (typeof content === "string" && content.startsWith("hcs://")) {
|
|
82
|
+
this.logger.debug(`Resolving inscribed message: ${content}`);
|
|
83
|
+
content = await this.hcsClient.getMessageContent(content);
|
|
84
|
+
this.logger.debug(`Resolved content length: ${content?.length}`);
|
|
85
|
+
}
|
|
86
|
+
let displayContent = content;
|
|
87
|
+
try {
|
|
88
|
+
const parsed = JSON.parse(content || "{}");
|
|
89
|
+
if (parsed.p === "hcs-10" && parsed.op === "message" && parsed.data) {
|
|
90
|
+
const senderOpId = parsed.operator_id || "unknown_sender";
|
|
91
|
+
displayContent = `[${senderOpId}]: ${parsed.data}`;
|
|
92
|
+
} else {
|
|
93
|
+
displayContent = content;
|
|
94
|
+
}
|
|
95
|
+
} catch (parseError) {
|
|
96
|
+
displayContent = content;
|
|
97
|
+
}
|
|
98
|
+
const messageDate = new Date(msg.timestamp);
|
|
99
|
+
outputString += `
|
|
100
|
+
[${messageDate.toLocaleString()}] (Seq: ${msg.sequence_number})
|
|
101
|
+
${displayContent}
|
|
102
|
+
`;
|
|
103
|
+
} catch (error) {
|
|
104
|
+
const errorMsg = `Error processing message (Seq: ${msg.sequence_number}): ${error instanceof Error ? error.message : String(error)}`;
|
|
105
|
+
this.logger.error(errorMsg);
|
|
106
|
+
outputString += `
|
|
107
|
+
[Error processing message Seq: ${msg.sequence_number}]
|
|
108
|
+
`;
|
|
109
|
+
}
|
|
96
110
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
this.stateManager.
|
|
111
|
+
if (!isFetchingLatest && latestTimestampNanos > lastProcessedTimestamp) {
|
|
112
|
+
this.logger.debug(
|
|
113
|
+
`Updating timestamp for topic ${connectionTopicId} to ${latestTimestampNanos}`
|
|
114
|
+
);
|
|
115
|
+
this.stateManager.updateTimestamp(
|
|
116
|
+
connectionTopicId,
|
|
117
|
+
latestTimestampNanos
|
|
118
|
+
);
|
|
102
119
|
}
|
|
103
|
-
return
|
|
120
|
+
return outputString.trim();
|
|
104
121
|
} catch (error) {
|
|
105
|
-
|
|
106
|
-
|
|
122
|
+
this.logger.error(
|
|
123
|
+
`Failed to check messages for topic ${connectionTopicId}: ${error}`
|
|
124
|
+
);
|
|
125
|
+
return `Error checking messages for ${targetAgentName}: ${error instanceof Error ? error.message : String(error)}`;
|
|
107
126
|
}
|
|
108
127
|
}
|
|
109
128
|
}
|
|
110
129
|
export {
|
|
111
|
-
|
|
130
|
+
CheckMessagesTool
|
|
112
131
|
};
|
|
113
132
|
//# sourceMappingURL=standards-agent-kit.es9.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"standards-agent-kit.es9.js","sources":["../../src/tools/ListConnectionsTool.ts"],"sourcesContent":["import { StructuredTool, ToolParams } from '@langchain/core/tools';\nimport { z } from 'zod';\nimport { IStateManager, ActiveConnection } from '../state/state-types';\nimport { HCS10Client } from '../hcs10/HCS10Client';\nimport { Connection, Logger } from '@hashgraphonline/standards-sdk';\n\nexport interface ListConnectionsToolParams extends ToolParams {\n stateManager: IStateManager;\n hcsClient?: HCS10Client;\n}\n\n/**\n * A tool to list currently active HCS-10 connections stored in the state manager.\n * Enhanced to show more details similar to moonscape's implementation.\n */\nexport class ListConnectionsTool extends StructuredTool {\n name = 'list_connections';\n description =\n '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.';\n schema = z.object({\n includeDetails: z\n .boolean()\n .optional()\n .describe(\n 'Whether to include detailed information about each connection'\n ),\n showPending: z\n .boolean()\n .optional()\n .describe('Whether to include pending connection requests'),\n });\n\n private stateManager: IStateManager;\n private hcsClient?: HCS10Client;\n private logger: Logger;\n\n constructor({ stateManager, hcsClient, ...rest }: ListConnectionsToolParams) {\n super(rest);\n this.stateManager = stateManager;\n this.hcsClient = hcsClient;\n this.logger = new Logger({ module: 'ListConnectionsTool' });\n }\n\n protected async _call(args: z.infer<this['schema']>): Promise<string> {\n const includeDetails = args.includeDetails ?? true;\n const showPending = args.showPending ?? true;\n\n const connections = await this.getEnhancedConnections();\n\n if (connections.length === 0) {\n return 'There are currently no active connections.';\n }\n\n const activeConnections = connections.filter(\n (c) => c.status === 'established'\n );\n\n const pendingConnections = connections.filter((c) => c.isPending);\n\n const needsConfirmation = connections.filter((c) => c.needsConfirmation);\n\n let output = '';\n\n if (activeConnections.length > 0) {\n output += `🟢 Active Connections (${activeConnections.length}):\\n`;\n activeConnections.forEach((conn, index) => {\n output += this.formatConnection(conn, index, includeDetails);\n });\n output += '\\n';\n }\n\n if (showPending && needsConfirmation.length > 0) {\n output += `🟠 Connections Needing Confirmation (${needsConfirmation.length}):\\n`;\n needsConfirmation.forEach((conn, index) => {\n output += this.formatConnection(conn, index, includeDetails);\n });\n output += '\\n';\n }\n\n if (showPending && pendingConnections.length > 0) {\n output += `⚪ Pending Connection Requests (${pendingConnections.length}):\\n`;\n pendingConnections.forEach((conn, index) => {\n output += this.formatConnection(conn, index, includeDetails);\n });\n }\n\n return output.trim();\n }\n\n private formatConnection(\n conn: Connection,\n index: number,\n includeDetails: boolean\n ): string {\n let output = `${index + 1}. ${\n conn.profileInfo?.display_name || conn.targetAgentName || 'Unknown Agent'\n } (${conn.targetAccountId})\\n`;\n const displayTopicId = conn.isPending\n ? '(Pending Request)'\n : conn.connectionTopicId;\n output += ` Topic: ${displayTopicId}\\n`;\n const statusText = conn.status || 'unknown';\n output += ` Status: ${statusText}\\n`;\n\n if (includeDetails) {\n if (conn.profileInfo?.bio) {\n output += ` Bio: ${conn.profileInfo.bio.substring(0, 100)}${\n conn.profileInfo.bio.length > 100 ? '...' : ''\n }\\n`;\n }\n\n if (conn.created) {\n const createdLabel = conn.isPending\n ? 'Request sent'\n : 'Connection established';\n output += ` ${createdLabel}: ${conn.created.toLocaleString()}\\n`;\n }\n\n if (conn.lastActivity) {\n output += ` Last activity: ${conn.lastActivity.toLocaleString()}\\n`;\n }\n }\n\n return output;\n }\n\n private async getEnhancedConnections(): Promise<Connection[]> {\n if (!this.hcsClient) {\n return this.stateManager.listConnections() as Connection[];\n }\n\n try {\n const { accountId } = this.hcsClient.getAccountAndSigner();\n if (!accountId) {\n return this.stateManager.listConnections() as Connection[];\n }\n\n const connectionManager = this.stateManager.getConnectionsManager();\n if (!connectionManager) {\n this.logger.error('ConnectionsManager not initialized');\n return this.stateManager.listConnections() as Connection[];\n }\n\n const connections = await connectionManager.fetchConnectionData(\n accountId\n );\n\n for (const connection of connections) {\n this.stateManager.updateOrAddConnection(connection as ActiveConnection);\n }\n\n return connections;\n } catch (error) {\n console.error('Error fetching connection data:', error);\n return this.stateManager.listConnections() as Connection[];\n }\n }\n}\n"],"names":[],"mappings":";;;AAeO,MAAM,4BAA4B,eAAe;AAAA,EAqBtD,YAAY,EAAE,cAAc,WAAW,GAAG,QAAmC;AAC3E,UAAM,IAAI;AArBL,SAAA,OAAA;AAEL,SAAA,cAAA;AACF,SAAA,SAAS,EAAE,OAAO;AAAA,MAChB,gBAAgB,EACb,UACA,SACA,EAAA;AAAA,QACC;AAAA,MACF;AAAA,MACF,aAAa,EACV,QAAA,EACA,SAAS,EACT,SAAS,gDAAgD;AAAA,IAAA,CAC7D;AAQC,SAAK,eAAe;AACpB,SAAK,YAAY;AACjB,SAAK,SAAS,IAAI,OAAO,EAAE,QAAQ,uBAAuB;AAAA,EAAA;AAAA,EAG5D,MAAgB,MAAM,MAAgD;AAC9D,UAAA,iBAAiB,KAAK,kBAAkB;AACxC,UAAA,cAAc,KAAK,eAAe;AAElC,UAAA,cAAc,MAAM,KAAK,uBAAuB;AAElD,QAAA,YAAY,WAAW,GAAG;AACrB,aAAA;AAAA,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AAG,GAAG,CAAC,GACzD,KAAK,YAAY,IAAI,SAAS,MAAM,QAAQ,EAC9C;AAAA;AAAA,MAAA;AAGF,UAAI,KAAK,SAAS;AACV,cAAA,eAAe,KAAK,YACtB,iBACA;AACJ,kBAAU,MAAM,YAAY,KAAK,KAAK,QAAQ,eAAgB,CAAA;AAAA;AAAA,MAAA;AAGhE,UAAI,KAAK,cAAc;AACrB,kBAAU,qBAAqB,KAAK,aAAa,eAAgB,CAAA;AAAA;AAAA,MAAA;AAAA,IACnE;AAGK,WAAA;AAAA,EAAA;AAAA,EAGT,MAAc,yBAAgD;AACxD,QAAA,CAAC,KAAK,WAAW;AACZ,aAAA,KAAK,aAAa,gBAAgB;AAAA,IAAA;AAGvC,QAAA;AACF,YAAM,EAAE,UAAc,IAAA,KAAK,UAAU,oBAAoB;AACzD,UAAI,CAAC,WAAW;AACP,eAAA,KAAK,aAAa,gBAAgB;AAAA,MAAA;AAGrC,YAAA,oBAAoB,KAAK,aAAa,sBAAsB;AAClE,UAAI,CAAC,mBAAmB;AACjB,aAAA,OAAO,MAAM,oCAAoC;AAC/C,eAAA,KAAK,aAAa,gBAAgB;AAAA,MAAA;AAGrC,YAAA,cAAc,MAAM,kBAAkB;AAAA,QAC1C;AAAA,MACF;AAEA,iBAAW,cAAc,aAAa;AAC/B,aAAA,aAAa,sBAAsB,UAA8B;AAAA,MAAA;AAGjE,aAAA;AAAA,aACA,OAAO;AACN,cAAA,MAAM,mCAAmC,KAAK;AAC/C,aAAA,KAAK,aAAa,gBAAgB;AAAA,IAAA;AAAA,EAC3C;AAEJ;"}
|
|
1
|
+
{"version":3,"file":"standards-agent-kit.es9.js","sources":["../../src/tools/CheckMessagesTool.ts"],"sourcesContent":["import { StructuredTool, ToolParams } from '@langchain/core/tools';\nimport { z } from 'zod';\nimport { HCS10Client, HCSMessageWithTimestamp } from '../hcs10/HCS10Client';\nimport { IStateManager } from '../state/state-types';\nimport { Logger } from '@hashgraphonline/standards-sdk'; // Assuming logger utility\n\nexport interface CheckMessagesToolParams extends ToolParams {\n hcsClient: HCS10Client;\n stateManager: IStateManager;\n}\n\n/**\n * A tool to check for new messages on an active HCS-10 connection topic,\n * or optionally fetch the latest messages regardless of timestamp.\n */\nexport class CheckMessagesTool extends StructuredTool {\n name = 'check_messages';\n description = `Checks for and retrieves messages from an active connection. \nIdentify the target agent using their account ID (e.g., 0.0.12345) or the connection number shown in 'list_connections'. \nBy default, it only retrieves messages newer than the last check. \nUse 'fetchLatest: true' to get the most recent messages regardless of when they arrived. \nUse 'lastMessagesCount' to specify how many latest messages to retrieve (default 1 when fetchLatest is true).`;\n schema = z.object({\n targetIdentifier: z\n .string()\n .describe(\n \"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 to check messages for.\"\n ),\n fetchLatest: z\n .boolean()\n .optional()\n .default(false)\n .describe(\n 'Set to true to fetch the latest messages even if they have been seen before, ignoring the last checked timestamp. Defaults to false (fetching only new messages).'\n ),\n lastMessagesCount: z\n .number()\n .int()\n .positive()\n .optional()\n .describe(\n 'When fetchLatest is true, specifies how many of the most recent messages to retrieve. Defaults to 1.'\n ),\n });\n\n public hcsClient: HCS10Client;\n private stateManager: IStateManager;\n private logger: Logger;\n\n constructor({ hcsClient, stateManager, ...rest }: CheckMessagesToolParams) {\n super(rest);\n this.hcsClient = hcsClient;\n this.stateManager = stateManager;\n this.logger = Logger.getInstance({ module: 'CheckMessagesTool' });\n }\n\n protected async _call({\n targetIdentifier,\n fetchLatest,\n lastMessagesCount,\n }: z.infer<this['schema']>): Promise<string> {\n const connection =\n this.stateManager.getConnectionByIdentifier(targetIdentifier);\n\n if (!connection) {\n return `Error: Could not find an active connection matching identifier \"${targetIdentifier}\". Use 'list_connections' to see active connections.`;\n }\n\n const connectionTopicId = connection.connectionTopicId;\n const targetAgentName = connection.targetAgentName;\n const lastProcessedTimestamp =\n this.stateManager.getLastTimestamp(connectionTopicId);\n\n this.logger.info(\n `Checking messages for connection with ${targetAgentName} (${connection.targetAccountId}) on topic ${connectionTopicId} (fetchLatest: ${fetchLatest}, lastCount: ${lastMessagesCount}, since: ${lastProcessedTimestamp})`\n );\n\n try {\n // 1. Get messages from the topic\n const result = await this.hcsClient.getMessages(connectionTopicId);\n const allMessages = result.messages;\n\n if (!allMessages || allMessages.length === 0) {\n return `No messages found on connection topic ${connectionTopicId}.`;\n }\n\n let messagesToProcess: HCSMessageWithTimestamp[] = [];\n let latestTimestampNanos = lastProcessedTimestamp;\n const isFetchingLatest = fetchLatest === true;\n\n if (isFetchingLatest) {\n this.logger.info('Fetching latest messages regardless of timestamp.');\n const count = lastMessagesCount ?? 1;\n messagesToProcess = allMessages.slice(-count);\n } else {\n this.logger.info(\n `Filtering for messages newer than ${lastProcessedTimestamp}`\n );\n messagesToProcess = allMessages.filter((msg) => {\n const msgTimestampNanos = msg.timestamp * 1_000_000;\n return msgTimestampNanos > lastProcessedTimestamp;\n });\n \n if (messagesToProcess.length > 0) {\n latestTimestampNanos = messagesToProcess.reduce(\n (maxTs, msg) => Math.max(maxTs, msg.timestamp * 1_000_000),\n lastProcessedTimestamp\n );\n }\n }\n\n if (messagesToProcess.length === 0) {\n return isFetchingLatest\n ? `Could not retrieve the latest message(s). No messages found on topic ${connectionTopicId}.`\n : `No new messages found for connection with ${targetAgentName} since last check.`;\n }\n\n this.logger.info(`Processing ${messagesToProcess.length} message(s).`);\n\n // 3. Process messages (resolve inscriptions, format)\n let outputString = isFetchingLatest\n ? `Latest message(s) from ${targetAgentName}:\n`\n : `New messages from ${targetAgentName}:\n`;\n\n for (const msg of messagesToProcess) {\n let content = msg.data;\n try {\n // Check for inscription HRL\n if (typeof content === 'string' && content.startsWith('hcs://')) {\n this.logger.debug(`Resolving inscribed message: ${content}`);\n content = await this.hcsClient.getMessageContent(content);\n this.logger.debug(`Resolved content length: ${content?.length}`);\n }\n\n // Attempt to parse the content as the HCS-10 structure\n let displayContent = content; // Default to raw content\n try {\n const parsed = JSON.parse(content || '{}');\n if (\n parsed.p === 'hcs-10' &&\n parsed.op === 'message' &&\n parsed.data\n ) {\n // Extract sender and actual data from standard message format\n const senderOpId = parsed.operator_id || 'unknown_sender';\n displayContent = `[${senderOpId}]: ${parsed.data}`;\n } else {\n // If not standard format, maybe just show raw stringified version\n displayContent = content; // Keep raw if parsing worked but not expected format\n }\n } catch (parseError) {\n // Content wasn't JSON, keep raw content\n displayContent = content;\n }\n\n const messageDate = new Date(msg.timestamp);\n outputString += `\\n[${messageDate.toLocaleString()}] (Seq: ${\n msg.sequence_number\n })\n${displayContent}\n`;\n } catch (error) {\n const errorMsg = `Error processing message (Seq: ${\n msg.sequence_number\n }): ${error instanceof Error ? error.message : String(error)}`;\n this.logger.error(errorMsg);\n outputString += `\\n[Error processing message Seq: ${msg.sequence_number}]\\n`;\n }\n }\n\n // 4. Update the timestamp in demo state ONLY if fetching NEW messages\n if (!isFetchingLatest && latestTimestampNanos > lastProcessedTimestamp) {\n this.logger.debug(\n `Updating timestamp for topic ${connectionTopicId} to ${latestTimestampNanos}`\n );\n this.stateManager.updateTimestamp(\n connectionTopicId,\n latestTimestampNanos\n );\n }\n\n return outputString.trim();\n } catch (error) {\n this.logger.error(\n `Failed to check messages for topic ${connectionTopicId}: ${error}`\n );\n return `Error checking messages for ${targetAgentName}: ${\n error instanceof Error ? error.message : String(error)\n }`;\n }\n }\n}\n"],"names":[],"mappings":";;;AAeO,MAAM,0BAA0B,eAAe;AAAA,EAkCpD,YAAY,EAAE,WAAW,cAAc,GAAG,QAAiC;AACzE,UAAM,IAAI;AAlCL,SAAA,OAAA;AACO,SAAA,cAAA;AAAA;AAAA;AAAA;AAAA;AAKd,SAAA,SAAS,EAAE,OAAO;AAAA,MAChB,kBAAkB,EACf,OAAA,EACA;AAAA,QACC;AAAA,MACF;AAAA,MACF,aAAa,EACV,QAAQ,EACR,WACA,QAAQ,KAAK,EACb;AAAA,QACC;AAAA,MACF;AAAA,MACF,mBAAmB,EAChB,SACA,IACA,EAAA,SACA,E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oBAAoB,uBAAuB,wBAAwB;AACtE,aAAK,OAAO;AAAA,UACV,gCAAgC,iBAAiB,OAAO,oBAAoB;AAAA,QAC9E;AACA,aAAK,aAAa;AAAA,UAChB;AAAA,UACA;AAAA,QACF;AAAA,MAAA;AAGF,aAAO,aAAa,KAAK;AAAA,aAClB,OAAO;AACd,WAAK,OAAO;AAAA,QACV,sCAAsC,iBAAiB,KAAK,KAAK;AAAA,MACnE;AACO,aAAA,+BAA+B,eAAe,KACnD,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,CACvD;AAAA,IAAA;AAAA,EACF;AAEJ;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './init';
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import { HCS10Client, StandardNetworkType } from '
|
|
2
|
-
import { RegisterAgentTool } from '
|
|
3
|
-
import { SendMessageTool } from '
|
|
4
|
-
import { ConnectionTool } from '
|
|
5
|
-
import { IStateManager } from '
|
|
6
|
-
import { FindRegistrationsTool } from '
|
|
7
|
-
import { InitiateConnectionTool } from '
|
|
8
|
-
import { ListConnectionsTool } from '
|
|
9
|
-
import { SendMessageToConnectionTool } from '
|
|
10
|
-
import { CheckMessagesTool } from '
|
|
11
|
-
import { ConnectionMonitorTool } from '
|
|
12
|
-
import { ManageConnectionRequestsTool } from '
|
|
13
|
-
import { AcceptConnectionRequestTool } from '
|
|
14
|
-
import { RetrieveProfileTool } from '
|
|
15
|
-
import { ListUnapprovedConnectionRequestsTool } from '
|
|
1
|
+
import { HCS10Client, StandardNetworkType } from '../hcs10/HCS10Client';
|
|
2
|
+
import { RegisterAgentTool } from '../tools/RegisterAgentTool';
|
|
3
|
+
import { SendMessageTool } from '../tools/SendMessageTool';
|
|
4
|
+
import { ConnectionTool } from '../tools/ConnectionTool';
|
|
5
|
+
import { IStateManager } from '../state/state-types';
|
|
6
|
+
import { FindRegistrationsTool } from '../tools/FindRegistrationsTool';
|
|
7
|
+
import { InitiateConnectionTool } from '../tools/InitiateConnectionTool';
|
|
8
|
+
import { ListConnectionsTool } from '../tools/ListConnectionsTool';
|
|
9
|
+
import { SendMessageToConnectionTool } from '../tools/SendMessageToConnectionTool';
|
|
10
|
+
import { CheckMessagesTool } from '../tools/CheckMessagesTool';
|
|
11
|
+
import { ConnectionMonitorTool } from '../tools/ConnectionMonitorTool';
|
|
12
|
+
import { ManageConnectionRequestsTool } from '../tools/ManageConnectionRequestsTool';
|
|
13
|
+
import { AcceptConnectionRequestTool } from '../tools/AcceptConnectionRequestTool';
|
|
14
|
+
import { RetrieveProfileTool } from '../tools/RetrieveProfileTool';
|
|
15
|
+
import { ListUnapprovedConnectionRequestsTool } from '../tools/ListUnapprovedConnectionRequestsTool';
|
|
16
16
|
export interface HCS10ClientConfig {
|
|
17
17
|
operatorId?: string;
|
|
18
18
|
operatorKey?: string;
|
|
@@ -51,7 +51,7 @@ export interface HCS10Tools {
|
|
|
51
51
|
* @param options - Initialization options
|
|
52
52
|
* @returns Object containing hcs10Client and requested tools
|
|
53
53
|
*/
|
|
54
|
-
export declare
|
|
54
|
+
export declare const initializeStandardsAgentKit: (options?: HCS10InitializationOptions) => {
|
|
55
55
|
hcs10Client: HCS10Client;
|
|
56
56
|
monitoringClient?: HCS10Client;
|
|
57
57
|
tools: Partial<HCS10Tools>;
|