@hashgraphonline/conversational-agent 0.1.208 → 0.1.209
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/conversational-agent.d.ts +67 -8
- package/dist/cjs/index.cjs +1 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/langchain-agent.d.ts +8 -0
- package/dist/cjs/memory/SmartMemoryManager.d.ts +58 -21
- package/dist/cjs/memory/index.d.ts +1 -1
- package/dist/esm/index.js +8 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/index12.js +124 -46
- package/dist/esm/index12.js.map +1 -1
- package/dist/esm/index13.js +178 -13
- package/dist/esm/index13.js.map +1 -1
- package/dist/esm/index14.js +604 -100
- package/dist/esm/index14.js.map +1 -1
- package/dist/esm/index15.js +464 -9
- package/dist/esm/index15.js.map +1 -1
- package/dist/esm/index16.js +44 -172
- package/dist/esm/index16.js.map +1 -1
- package/dist/esm/index17.js +11 -156
- package/dist/esm/index17.js.map +1 -1
- package/dist/esm/index18.js +106 -191
- package/dist/esm/index18.js.map +1 -1
- package/dist/esm/index19.js +9 -660
- package/dist/esm/index19.js.map +1 -1
- package/dist/esm/index2.js +22 -13
- package/dist/esm/index2.js.map +1 -1
- package/dist/esm/index20.js +150 -206
- package/dist/esm/index20.js.map +1 -1
- package/dist/esm/index21.js +140 -166
- package/dist/esm/index21.js.map +1 -1
- package/dist/esm/index22.js +47 -105
- package/dist/esm/index22.js.map +1 -1
- package/dist/esm/index23.js +24 -89
- package/dist/esm/index23.js.map +1 -1
- package/dist/esm/index24.js +83 -56
- package/dist/esm/index24.js.map +1 -1
- package/dist/esm/index25.js +236 -32
- package/dist/esm/index25.js.map +1 -1
- package/dist/esm/index5.js +1 -1
- package/dist/esm/index6.js +295 -17
- package/dist/esm/index6.js.map +1 -1
- package/dist/esm/index8.js +82 -8
- package/dist/esm/index8.js.map +1 -1
- package/dist/types/conversational-agent.d.ts +67 -8
- package/dist/types/index.d.ts +1 -0
- package/dist/types/langchain-agent.d.ts +8 -0
- package/dist/types/memory/SmartMemoryManager.d.ts +58 -21
- package/dist/types/memory/index.d.ts +1 -1
- package/package.json +3 -3
- package/src/context/ReferenceContextManager.ts +9 -4
- package/src/context/ReferenceResponseProcessor.ts +3 -4
- package/src/conversational-agent.ts +379 -31
- package/src/index.ts +2 -0
- package/src/langchain/ContentAwareAgentExecutor.ts +0 -1
- package/src/langchain-agent.ts +94 -11
- package/src/mcp/ContentProcessor.ts +13 -3
- package/src/mcp/adapters/langchain.ts +1 -9
- package/src/memory/ContentStorage.ts +3 -51
- package/src/memory/MemoryWindow.ts +4 -16
- package/src/memory/ReferenceIdGenerator.ts +0 -4
- package/src/memory/SmartMemoryManager.ts +400 -33
- package/src/memory/TokenCounter.ts +12 -16
- package/src/memory/index.ts +1 -1
- package/src/plugins/hcs-10/HCS10Plugin.ts +44 -14
- package/src/services/ContentStoreManager.ts +0 -3
- package/src/types/content-reference.ts +8 -8
- package/src/types/index.ts +0 -1
package/dist/esm/index16.js
CHANGED
|
@@ -1,177 +1,49 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
...tool,
|
|
45
|
-
serverName: config.name
|
|
46
|
-
}));
|
|
47
|
-
this.tools.set(config.name, toolsWithServer);
|
|
48
|
-
this.logger.info(`Connected to MCP server ${config.name} with ${toolsWithServer.length} tools`);
|
|
49
|
-
return {
|
|
50
|
-
serverName: config.name,
|
|
51
|
-
connected: true,
|
|
52
|
-
tools: toolsWithServer
|
|
53
|
-
};
|
|
54
|
-
} catch (error) {
|
|
55
|
-
this.logger.error(`Failed to connect to MCP server ${config.name}:`, error);
|
|
56
|
-
return {
|
|
57
|
-
serverName: config.name,
|
|
58
|
-
connected: false,
|
|
59
|
-
error: error instanceof Error ? error.message : "Unknown error",
|
|
60
|
-
tools: []
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Execute a tool on a specific MCP server
|
|
66
|
-
*/
|
|
67
|
-
async executeTool(serverName, toolName, args) {
|
|
68
|
-
const client = this.clients.get(serverName);
|
|
69
|
-
if (!client) {
|
|
70
|
-
throw new Error(`MCP server ${serverName} not connected`);
|
|
71
|
-
}
|
|
72
|
-
this.logger.debug(`Executing MCP tool ${toolName} on server ${serverName}`, args);
|
|
73
|
-
try {
|
|
74
|
-
const result = await client.callTool({
|
|
75
|
-
name: toolName,
|
|
76
|
-
arguments: args
|
|
77
|
-
});
|
|
78
|
-
if (this.contentProcessor) {
|
|
79
|
-
const processed = await this.contentProcessor.processResponse(result, serverName, toolName);
|
|
80
|
-
if (processed.wasProcessed) {
|
|
81
|
-
this.logger.debug(
|
|
82
|
-
`Processed MCP response from ${serverName}::${toolName}`,
|
|
83
|
-
{
|
|
84
|
-
referenceCreated: processed.referenceCreated,
|
|
85
|
-
originalSize: processed.originalSize,
|
|
86
|
-
errors: processed.errors
|
|
87
|
-
}
|
|
88
|
-
);
|
|
89
|
-
if (processed.errors && processed.errors.length > 0) {
|
|
90
|
-
this.logger.warn(`Content processing warnings for ${serverName}::${toolName}:`, processed.errors);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
return processed.content;
|
|
94
|
-
}
|
|
95
|
-
return result;
|
|
96
|
-
} catch (error) {
|
|
97
|
-
this.logger.error(`Error executing MCP tool ${toolName}:`, error);
|
|
98
|
-
throw error;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Disconnect all MCP servers
|
|
103
|
-
*/
|
|
104
|
-
async disconnectAll() {
|
|
105
|
-
for (const [name, client] of this.clients) {
|
|
106
|
-
try {
|
|
107
|
-
await client.close();
|
|
108
|
-
this.logger.info(`Disconnected from MCP server ${name}`);
|
|
109
|
-
} catch (error) {
|
|
110
|
-
this.logger.error(`Error disconnecting MCP server ${name}:`, error);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
this.clients.clear();
|
|
114
|
-
this.tools.clear();
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Get all discovered tools from all connected servers
|
|
118
|
-
*/
|
|
119
|
-
getAllTools() {
|
|
120
|
-
const allTools = [];
|
|
121
|
-
for (const tools of this.tools.values()) {
|
|
122
|
-
allTools.push(...tools);
|
|
123
|
-
}
|
|
124
|
-
return allTools;
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Get tools from a specific server
|
|
128
|
-
*/
|
|
129
|
-
getServerTools(serverName) {
|
|
130
|
-
return this.tools.get(serverName) || [];
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Check if a server is connected
|
|
134
|
-
*/
|
|
135
|
-
isServerConnected(serverName) {
|
|
136
|
-
return this.clients.has(serverName);
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Get list of connected server names
|
|
140
|
-
*/
|
|
141
|
-
getConnectedServers() {
|
|
142
|
-
return Array.from(this.clients.keys());
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
* Enable content processing with content storage
|
|
146
|
-
*/
|
|
147
|
-
enableContentProcessing(contentStorage) {
|
|
148
|
-
this.contentProcessor = new MCPContentProcessor(contentStorage, this.logger);
|
|
149
|
-
this.logger.info("Content processing enabled for MCP responses");
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Disable content processing
|
|
153
|
-
*/
|
|
154
|
-
disableContentProcessing() {
|
|
155
|
-
delete this.contentProcessor;
|
|
156
|
-
this.logger.info("Content processing disabled for MCP responses");
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Check if content processing is enabled
|
|
160
|
-
*/
|
|
161
|
-
isContentProcessingEnabled() {
|
|
162
|
-
return this.contentProcessor !== void 0;
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* Analyze a response without processing it (for testing/debugging)
|
|
166
|
-
*/
|
|
167
|
-
analyzeResponseContent(response) {
|
|
168
|
-
if (!this.contentProcessor) {
|
|
169
|
-
throw new Error("Content processing is not enabled");
|
|
170
|
-
}
|
|
171
|
-
return this.contentProcessor.analyzeResponse(response);
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { AccountBuilder } from "./index24.js";
|
|
3
|
+
import { BaseHederaTransactionTool } from "hedera-agent-kit";
|
|
4
|
+
const HbarTransferInputSchema = z.object({
|
|
5
|
+
accountId: z.string().describe('Account ID for the transfer (e.g., "0.0.xxxx").'),
|
|
6
|
+
amount: z.union([z.number(), z.string()]).describe(
|
|
7
|
+
"HBAR amount in decimal format (e.g., 1 for 1 HBAR, 0.5 for 0.5 HBAR). Positive for credit, negative for debit. DO NOT multiply by 10^8 for tinybars - just use the HBAR amount directly."
|
|
8
|
+
)
|
|
9
|
+
});
|
|
10
|
+
const TransferHbarZodSchemaCore = z.object({
|
|
11
|
+
transfers: z.array(HbarTransferInputSchema).min(1).describe(
|
|
12
|
+
'Array of ALL transfers for this transaction. For multi-party transfers (e.g., "A sends 5 to C and B sends 3 to C"), include all transfers here: [{accountId: "A", amount: -5}, {accountId: "B", amount: -3}, {accountId: "C", amount: 8}]. Amounts must sum to zero.'
|
|
13
|
+
),
|
|
14
|
+
memo: z.string().optional().describe("Optional. Memo for the transaction.")
|
|
15
|
+
});
|
|
16
|
+
class TransferHbarTool extends BaseHederaTransactionTool {
|
|
17
|
+
constructor() {
|
|
18
|
+
super(...arguments);
|
|
19
|
+
this.name = "hedera-account-transfer-hbar-v2";
|
|
20
|
+
this.description = 'PRIMARY TOOL FOR HBAR TRANSFERS: Transfers HBAR between accounts. Supports multiple transfers in a single transaction - when multiple accounts need to send/receive HBAR (e.g., "A sends 5 HBAR to C and B sends 3 HBAR to C"), include ALL transfers in one transfers array. The sum of all transfers must equal zero. Use this for scheduled transactions and multi-signature scenarios.';
|
|
21
|
+
this.specificInputSchema = TransferHbarZodSchemaCore;
|
|
22
|
+
this.namespace = "account";
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Creates and returns the service builder for account operations.
|
|
26
|
+
*
|
|
27
|
+
* @returns BaseServiceBuilder instance configured for account operations
|
|
28
|
+
*/
|
|
29
|
+
getServiceBuilder() {
|
|
30
|
+
return new AccountBuilder(this.hederaKit);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Executes the HBAR transfer using the provided builder and arguments.
|
|
34
|
+
* Validates that all transfers sum to zero before execution.
|
|
35
|
+
*
|
|
36
|
+
* @param builder - The service builder instance for executing transactions
|
|
37
|
+
* @param specificArgs - The validated transfer parameters including transfers array and optional memo
|
|
38
|
+
* @returns Promise that resolves when the transfer is complete
|
|
39
|
+
*/
|
|
40
|
+
async callBuilderMethod(builder, specificArgs) {
|
|
41
|
+
await builder.transferHbar(
|
|
42
|
+
specificArgs
|
|
43
|
+
);
|
|
172
44
|
}
|
|
173
45
|
}
|
|
174
46
|
export {
|
|
175
|
-
|
|
47
|
+
TransferHbarTool
|
|
176
48
|
};
|
|
177
49
|
//# sourceMappingURL=index16.js.map
|
package/dist/esm/index16.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index16.js","sources":["../../src/
|
|
1
|
+
{"version":3,"file":"index16.js","sources":["../../src/plugins/hbar-transfer/TransferHbarTool.ts"],"sourcesContent":["import { z } from 'zod';\nimport { HbarTransferParams } from './types';\nimport { AccountBuilder } from './AccountBuilder';\nimport { BaseHederaTransactionTool, BaseServiceBuilder } from 'hedera-agent-kit';\n\nconst HbarTransferInputSchema = z.object({\n accountId: z\n .string()\n .describe('Account ID for the transfer (e.g., \"0.0.xxxx\").'),\n amount: z\n .union([z.number(), z.string()])\n .describe(\n 'HBAR amount in decimal format (e.g., 1 for 1 HBAR, 0.5 for 0.5 HBAR). Positive for credit, negative for debit. DO NOT multiply by 10^8 for tinybars - just use the HBAR amount directly.'\n ),\n});\n\nconst TransferHbarZodSchemaCore = z.object({\n transfers: z\n .array(HbarTransferInputSchema)\n .min(1)\n .describe(\n 'Array of ALL transfers for this transaction. For multi-party transfers (e.g., \"A sends 5 to C and B sends 3 to C\"), include all transfers here: [{accountId: \"A\", amount: -5}, {accountId: \"B\", amount: -3}, {accountId: \"C\", amount: 8}]. Amounts must sum to zero.'\n ),\n memo: z.string().optional().describe('Optional. Memo for the transaction.'),\n});\n\n/**\n * A Hedera transaction tool for transferring HBAR between accounts.\n * Supports single and multi-party transfers with automatic balance validation.\n * Extends BaseHederaTransactionTool to handle HBAR transfer transactions on the Hedera network.\n */\nexport class TransferHbarTool extends BaseHederaTransactionTool<\n typeof TransferHbarZodSchemaCore\n> {\n name = 'hedera-account-transfer-hbar-v2';\n description =\n 'PRIMARY TOOL FOR HBAR TRANSFERS: Transfers HBAR between accounts. Supports multiple transfers in a single transaction - when multiple accounts need to send/receive HBAR (e.g., \"A sends 5 HBAR to C and B sends 3 HBAR to C\"), include ALL transfers in one transfers array. The sum of all transfers must equal zero. Use this for scheduled transactions and multi-signature scenarios.';\n specificInputSchema = TransferHbarZodSchemaCore;\n namespace = 'account';\n\n\n /**\n * Creates and returns the service builder for account operations.\n * \n * @returns BaseServiceBuilder instance configured for account operations\n */\n protected getServiceBuilder(): BaseServiceBuilder {\n return new AccountBuilder(this.hederaKit) as BaseServiceBuilder;\n }\n\n /**\n * Executes the HBAR transfer using the provided builder and arguments.\n * Validates that all transfers sum to zero before execution.\n * \n * @param builder - The service builder instance for executing transactions\n * @param specificArgs - The validated transfer parameters including transfers array and optional memo\n * @returns Promise that resolves when the transfer is complete\n */\n protected async callBuilderMethod(\n builder: BaseServiceBuilder,\n specificArgs: z.infer<typeof TransferHbarZodSchemaCore>\n ): Promise<void> {\n await (builder as AccountBuilder).transferHbar(\n specificArgs as unknown as HbarTransferParams\n );\n }\n}"],"names":[],"mappings":";;;AAKA,MAAM,0BAA0B,EAAE,OAAO;AAAA,EACvC,WAAW,EACR,SACA,SAAS,iDAAiD;AAAA,EAC7D,QAAQ,EACL,MAAM,CAAC,EAAE,OAAA,GAAU,EAAE,QAAQ,CAAC,EAC9B;AAAA,IACC;AAAA,EAAA;AAEN,CAAC;AAED,MAAM,4BAA4B,EAAE,OAAO;AAAA,EACzC,WAAW,EACR,MAAM,uBAAuB,EAC7B,IAAI,CAAC,EACL;AAAA,IACC;AAAA,EAAA;AAAA,EAEJ,MAAM,EAAE,OAAA,EAAS,SAAA,EAAW,SAAS,qCAAqC;AAC5E,CAAC;AAOM,MAAM,yBAAyB,0BAEpC;AAAA,EAFK,cAAA;AAAA,UAAA,GAAA,SAAA;AAGL,SAAA,OAAO;AACP,SAAA,cACE;AACF,SAAA,sBAAsB;AACtB,SAAA,YAAY;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQF,oBAAwC;AAChD,WAAO,IAAI,eAAe,KAAK,SAAS;AAAA,EAC1C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,MAAgB,kBACd,SACA,cACe;AACf,UAAO,QAA2B;AAAA,MAChC;AAAA,IAAA;AAAA,EAEJ;AACF;"}
|
package/dist/esm/index17.js
CHANGED
|
@@ -1,161 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
import { z } from "zod";
|
|
3
|
-
import { shouldUseReference, ContentStoreService } from "@hashgraphonline/standards-sdk";
|
|
4
|
-
function convertMCPToolToLangChain(tool, mcpManager, serverConfig) {
|
|
5
|
-
const zodSchema = jsonSchemaToZod(tool.inputSchema);
|
|
6
|
-
const sanitizedName = `${tool.serverName}_${tool.name}`.replace(
|
|
7
|
-
/[^a-zA-Z0-9_]/g,
|
|
8
|
-
"_"
|
|
9
|
-
);
|
|
10
|
-
let description = tool.description || `MCP tool ${tool.name} from ${tool.serverName}`;
|
|
11
|
-
if (serverConfig?.toolDescriptions?.[tool.name]) {
|
|
12
|
-
description = `${description}
|
|
1
|
+
const getSystemMessage = (accountId) => `You are a helpful assistant managing Hashgraph Online HCS-10 connections, messages, HCS-2 registries, and content inscription.
|
|
13
2
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
3
|
+
You have access to tools for:
|
|
4
|
+
- HCS-10: registering agents, finding registered agents, initiating connections, listing active connections, sending messages over connections, and checking for new messages
|
|
5
|
+
- HCS-2: creating registries, registering entries, updating entries, deleting entries, migrating registries, and querying registry contents
|
|
6
|
+
- Inscription: inscribing content from URLs, files, or buffers, creating Hashinal NFTs, and retrieving inscriptions
|
|
18
7
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
schema: zodSchema,
|
|
25
|
-
func: async (input) => {
|
|
26
|
-
try {
|
|
27
|
-
const result = await mcpManager.executeTool(
|
|
28
|
-
tool.serverName,
|
|
29
|
-
tool.name,
|
|
30
|
-
input
|
|
31
|
-
);
|
|
32
|
-
let responseText = "";
|
|
33
|
-
if (typeof result === "string") {
|
|
34
|
-
responseText = result;
|
|
35
|
-
} else if (result && typeof result === "object" && "content" in result) {
|
|
36
|
-
const content = result.content;
|
|
37
|
-
if (Array.isArray(content)) {
|
|
38
|
-
const textParts = content.filter(
|
|
39
|
-
(item) => typeof item === "object" && item !== null && "type" in item && item.type === "text" && "text" in item
|
|
40
|
-
).map((item) => item.text);
|
|
41
|
-
responseText = textParts.join("\n");
|
|
42
|
-
} else {
|
|
43
|
-
responseText = JSON.stringify(content);
|
|
44
|
-
}
|
|
45
|
-
} else {
|
|
46
|
-
responseText = JSON.stringify(result);
|
|
47
|
-
}
|
|
48
|
-
const responseBuffer = Buffer.from(responseText, "utf8");
|
|
49
|
-
console.log(`[MCP Adapter] Response size: ${responseBuffer.length} bytes, tool: ${tool.serverName}_${tool.name}`);
|
|
50
|
-
const MCP_REFERENCE_THRESHOLD = 10 * 1024;
|
|
51
|
-
const shouldStoreMCPContent = responseBuffer.length > MCP_REFERENCE_THRESHOLD;
|
|
52
|
-
if (shouldStoreMCPContent || shouldUseReference(responseBuffer)) {
|
|
53
|
-
console.log(`[MCP Adapter] Content exceeds threshold (${responseBuffer.length} > ${MCP_REFERENCE_THRESHOLD}), storing as reference`);
|
|
54
|
-
const contentStore = ContentStoreService.getInstance();
|
|
55
|
-
if (contentStore) {
|
|
56
|
-
try {
|
|
57
|
-
const referenceId = await contentStore.storeContent(responseBuffer, {
|
|
58
|
-
contentType: "text",
|
|
59
|
-
source: "mcp",
|
|
60
|
-
mcpToolName: `${tool.serverName}_${tool.name}`,
|
|
61
|
-
originalSize: responseBuffer.length
|
|
62
|
-
});
|
|
63
|
-
console.log(`[MCP Adapter] Stored content as reference: content-ref:${referenceId}`);
|
|
64
|
-
return `content-ref:${referenceId}`;
|
|
65
|
-
} catch (storeError) {
|
|
66
|
-
console.warn("Failed to store large MCP content as reference:", storeError);
|
|
67
|
-
}
|
|
68
|
-
} else {
|
|
69
|
-
console.warn("[MCP Adapter] ContentStoreService not available");
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return responseText;
|
|
73
|
-
} catch (error) {
|
|
74
|
-
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
75
|
-
return `Error executing MCP tool ${tool.name}: ${errorMessage}`;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
function jsonSchemaToZod(schema) {
|
|
81
|
-
if (!schema || typeof schema !== "object") {
|
|
82
|
-
return z.object({});
|
|
83
|
-
}
|
|
84
|
-
const schemaObj = schema;
|
|
85
|
-
if (schemaObj.type && schemaObj.type !== "object") {
|
|
86
|
-
return convertType(schemaObj);
|
|
87
|
-
}
|
|
88
|
-
if (!schemaObj.properties || typeof schemaObj.properties !== "object") {
|
|
89
|
-
return z.object({});
|
|
90
|
-
}
|
|
91
|
-
const shape = {};
|
|
92
|
-
for (const [key, value] of Object.entries(schemaObj.properties)) {
|
|
93
|
-
let zodType = convertType(value);
|
|
94
|
-
const isRequired = Array.isArray(schemaObj.required) && schemaObj.required.includes(key);
|
|
95
|
-
if (!isRequired) {
|
|
96
|
-
zodType = zodType.optional();
|
|
97
|
-
}
|
|
98
|
-
shape[key] = zodType;
|
|
99
|
-
}
|
|
100
|
-
return z.object(shape);
|
|
101
|
-
}
|
|
102
|
-
function convertType(schema) {
|
|
103
|
-
if (!schema || typeof schema !== "object" || !("type" in schema)) {
|
|
104
|
-
return z.unknown();
|
|
105
|
-
}
|
|
106
|
-
const schemaObj = schema;
|
|
107
|
-
let zodType;
|
|
108
|
-
switch (schemaObj.type) {
|
|
109
|
-
case "string":
|
|
110
|
-
zodType = z.string();
|
|
111
|
-
if (schemaObj.enum && Array.isArray(schemaObj.enum)) {
|
|
112
|
-
zodType = z.enum(schemaObj.enum);
|
|
113
|
-
}
|
|
114
|
-
break;
|
|
115
|
-
case "number":
|
|
116
|
-
zodType = z.number();
|
|
117
|
-
if ("minimum" in schemaObj && typeof schemaObj.minimum === "number") {
|
|
118
|
-
zodType = zodType.min(schemaObj.minimum);
|
|
119
|
-
}
|
|
120
|
-
if ("maximum" in schemaObj && typeof schemaObj.maximum === "number") {
|
|
121
|
-
zodType = zodType.max(schemaObj.maximum);
|
|
122
|
-
}
|
|
123
|
-
break;
|
|
124
|
-
case "integer":
|
|
125
|
-
zodType = z.number().int();
|
|
126
|
-
if ("minimum" in schemaObj && typeof schemaObj.minimum === "number") {
|
|
127
|
-
zodType = zodType.min(schemaObj.minimum);
|
|
128
|
-
}
|
|
129
|
-
if ("maximum" in schemaObj && typeof schemaObj.maximum === "number") {
|
|
130
|
-
zodType = zodType.max(schemaObj.maximum);
|
|
131
|
-
}
|
|
132
|
-
break;
|
|
133
|
-
case "boolean":
|
|
134
|
-
zodType = z.boolean();
|
|
135
|
-
break;
|
|
136
|
-
case "array":
|
|
137
|
-
if (schemaObj.items) {
|
|
138
|
-
zodType = z.array(convertType(schemaObj.items));
|
|
139
|
-
} else {
|
|
140
|
-
zodType = z.array(z.unknown());
|
|
141
|
-
}
|
|
142
|
-
break;
|
|
143
|
-
case "object":
|
|
144
|
-
if ("properties" in schemaObj) {
|
|
145
|
-
zodType = jsonSchemaToZod(schemaObj);
|
|
146
|
-
} else {
|
|
147
|
-
zodType = z.object({}).passthrough();
|
|
148
|
-
}
|
|
149
|
-
break;
|
|
150
|
-
default:
|
|
151
|
-
zodType = z.unknown();
|
|
152
|
-
}
|
|
153
|
-
if ("description" in schemaObj && typeof schemaObj.description === "string") {
|
|
154
|
-
zodType = zodType.describe(schemaObj.description);
|
|
155
|
-
}
|
|
156
|
-
return zodType;
|
|
157
|
-
}
|
|
8
|
+
*** IMPORTANT CONTEXT ***
|
|
9
|
+
You are currently operating as agent: ${accountId} on the Hashgraph Online network
|
|
10
|
+
When users ask about "my profile", "my account", "my connections", etc., use this account ID: ${accountId}
|
|
11
|
+
|
|
12
|
+
Remember the connection numbers when listing connections, as users might refer to them.`;
|
|
158
13
|
export {
|
|
159
|
-
|
|
14
|
+
getSystemMessage
|
|
160
15
|
};
|
|
161
16
|
//# sourceMappingURL=index17.js.map
|
package/dist/esm/index17.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index17.js","sources":["../../src/mcp/adapters/langchain.ts"],"sourcesContent":["import { DynamicStructuredTool } from '@langchain/core/tools';\nimport { z } from 'zod';\nimport type { MCPToolInfo, MCPServerConfig } from '../types';\nimport type { MCPClientManager } from '../MCPClientManager';\nimport { ContentStoreService, shouldUseReference } from '@hashgraphonline/standards-sdk';\nimport type { ContentSource } from '../../types/content-reference';\n\n/**\n * Convert an MCP tool to a LangChain DynamicStructuredTool\n */\nexport function convertMCPToolToLangChain(\n tool: MCPToolInfo,\n mcpManager: MCPClientManager,\n serverConfig?: MCPServerConfig\n): DynamicStructuredTool {\n const zodSchema = jsonSchemaToZod(tool.inputSchema);\n\n const sanitizedName = `${tool.serverName}_${tool.name}`.replace(\n /[^a-zA-Z0-9_]/g,\n '_'\n );\n\n let description = tool.description || `MCP tool ${tool.name} from ${tool.serverName}`;\n \n if (serverConfig?.toolDescriptions?.[tool.name]) {\n description = `${description}\\n\\n${serverConfig.toolDescriptions[tool.name]}`;\n }\n \n if (serverConfig?.additionalContext) {\n description = `${description}\\n\\nContext: ${serverConfig.additionalContext}`;\n }\n\n return new DynamicStructuredTool({\n name: sanitizedName,\n description,\n schema: zodSchema,\n func: async (input) => {\n try {\n const result = await mcpManager.executeTool(\n tool.serverName,\n tool.name,\n input\n );\n\n let responseText = '';\n \n if (typeof result === 'string') {\n responseText = result;\n } else if (\n result &&\n typeof result === 'object' &&\n 'content' in result\n ) {\n const content = (result as { content: unknown }).content;\n if (Array.isArray(content)) {\n const textParts = content\n .filter(\n (item): item is { type: string; text: string } =>\n typeof item === 'object' &&\n item !== null &&\n 'type' in item &&\n item.type === 'text' &&\n 'text' in item\n )\n .map((item) => item.text);\n responseText = textParts.join('\\n');\n } else {\n responseText = JSON.stringify(content);\n }\n } else {\n responseText = JSON.stringify(result);\n }\n\n // Check if content should be stored as reference\n const responseBuffer = Buffer.from(responseText, 'utf8');\n console.log(`[MCP Adapter] Response size: ${responseBuffer.length} bytes, tool: ${tool.serverName}_${tool.name}`);\n \n // Use a lower threshold for MCP tools (10KB) to avoid token limit issues\n const MCP_REFERENCE_THRESHOLD = 10 * 1024; // 10KB\n const shouldStoreMCPContent = responseBuffer.length > MCP_REFERENCE_THRESHOLD;\n \n if (shouldStoreMCPContent || shouldUseReference(responseBuffer)) {\n console.log(`[MCP Adapter] Content exceeds threshold (${responseBuffer.length} > ${MCP_REFERENCE_THRESHOLD}), storing as reference`);\n const contentStore = ContentStoreService.getInstance();\n if (contentStore) {\n try {\n const referenceId = await contentStore.storeContent(responseBuffer, {\n contentType: 'text' as ContentSource,\n source: 'mcp',\n mcpToolName: `${tool.serverName}_${tool.name}`,\n originalSize: responseBuffer.length\n });\n console.log(`[MCP Adapter] Stored content as reference: content-ref:${referenceId}`);\n return `content-ref:${referenceId}`;\n } catch (storeError) {\n // If storage fails, fall back to returning the content\n console.warn('Failed to store large MCP content as reference:', storeError);\n }\n } else {\n console.warn('[MCP Adapter] ContentStoreService not available');\n }\n }\n\n return responseText;\n } catch (error) {\n const errorMessage =\n error instanceof Error ? error.message : 'Unknown error';\n return `Error executing MCP tool ${tool.name}: ${errorMessage}`;\n }\n },\n });\n}\n\n/**\n * Convert JSON Schema to Zod schema\n * This is a simplified converter that handles common cases\n */\nfunction jsonSchemaToZod(schema: unknown): z.ZodTypeAny {\n if (!schema || typeof schema !== 'object') {\n return z.object({});\n }\n\n const schemaObj = schema as Record<string, unknown>;\n\n if (schemaObj.type && schemaObj.type !== 'object') {\n return convertType(schemaObj);\n }\n\n if (!schemaObj.properties || typeof schemaObj.properties !== 'object') {\n return z.object({});\n }\n\n const shape: Record<string, z.ZodTypeAny> = {};\n\n for (const [key, value] of Object.entries(schemaObj.properties)) {\n let zodType = convertType(value);\n\n const isRequired =\n Array.isArray(schemaObj.required) && schemaObj.required.includes(key);\n if (!isRequired) {\n zodType = zodType.optional();\n }\n\n shape[key] = zodType;\n }\n\n return z.object(shape);\n}\n\n/**\n * Convert a single JSON Schema type to Zod\n */\nfunction convertType(schema: unknown): z.ZodTypeAny {\n if (!schema || typeof schema !== 'object' || !('type' in schema)) {\n return z.unknown();\n }\n\n const schemaObj = schema as {\n type: string;\n enum?: unknown[];\n items?: unknown;\n };\n let zodType: z.ZodTypeAny;\n\n switch (schemaObj.type) {\n case 'string':\n zodType = z.string();\n if (schemaObj.enum && Array.isArray(schemaObj.enum)) {\n zodType = z.enum(schemaObj.enum as [string, ...string[]]);\n }\n break;\n\n case 'number':\n zodType = z.number();\n if ('minimum' in schemaObj && typeof schemaObj.minimum === 'number') {\n zodType = (zodType as z.ZodNumber).min(schemaObj.minimum);\n }\n if ('maximum' in schemaObj && typeof schemaObj.maximum === 'number') {\n zodType = (zodType as z.ZodNumber).max(schemaObj.maximum);\n }\n break;\n\n case 'integer':\n zodType = z.number().int();\n if ('minimum' in schemaObj && typeof schemaObj.minimum === 'number') {\n zodType = (zodType as z.ZodNumber).min(schemaObj.minimum);\n }\n if ('maximum' in schemaObj && typeof schemaObj.maximum === 'number') {\n zodType = (zodType as z.ZodNumber).max(schemaObj.maximum);\n }\n break;\n\n case 'boolean':\n zodType = z.boolean();\n break;\n\n case 'array':\n if (schemaObj.items) {\n zodType = z.array(convertType(schemaObj.items));\n } else {\n zodType = z.array(z.unknown());\n }\n break;\n\n case 'object':\n if ('properties' in schemaObj) {\n zodType = jsonSchemaToZod(schemaObj);\n } else {\n zodType = z.object({}).passthrough();\n }\n break;\n\n default:\n zodType = z.unknown();\n }\n\n if ('description' in schemaObj && typeof schemaObj.description === 'string') {\n zodType = zodType.describe(schemaObj.description);\n }\n\n return zodType;\n}\n"],"names":[],"mappings":";;;AAUO,SAAS,0BACd,MACA,YACA,cACuB;AACvB,QAAM,YAAY,gBAAgB,KAAK,WAAW;AAElD,QAAM,gBAAgB,GAAG,KAAK,UAAU,IAAI,KAAK,IAAI,GAAG;AAAA,IACtD;AAAA,IACA;AAAA,EAAA;AAGF,MAAI,cAAc,KAAK,eAAe,YAAY,KAAK,IAAI,SAAS,KAAK,UAAU;AAEnF,MAAI,cAAc,mBAAmB,KAAK,IAAI,GAAG;AAC/C,kBAAc,GAAG,WAAW;AAAA;AAAA,EAAO,aAAa,iBAAiB,KAAK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AAU,QAAQ,SAAS,UAAU,WAAW;AAAA,EAClD;AAEA,SAAO;AACT;"}
|
|
1
|
+
{"version":3,"file":"index17.js","sources":["../../src/config/system-message.ts"],"sourcesContent":["export const getSystemMessage = (accountId: string): string => `You are a helpful assistant managing Hashgraph Online HCS-10 connections, messages, HCS-2 registries, and content inscription.\n\nYou have access to tools for:\n- HCS-10: registering agents, finding registered agents, initiating connections, listing active connections, sending messages over connections, and checking for new messages\n- HCS-2: creating registries, registering entries, updating entries, deleting entries, migrating registries, and querying registry contents\n- Inscription: inscribing content from URLs, files, or buffers, creating Hashinal NFTs, and retrieving inscriptions\n\n*** IMPORTANT CONTEXT ***\nYou are currently operating as agent: ${accountId} on the Hashgraph Online network\nWhen users ask about \"my profile\", \"my account\", \"my connections\", etc., use this account ID: ${accountId}\n\nRemember the connection numbers when listing connections, as users might refer to them.`"],"names":[],"mappings":"AAAO,MAAM,mBAAmB,CAAC,cAA8B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wCAQvB,SAAS;AAAA,gGAC+C,SAAS;AAAA;AAAA;"}
|