illuma-agents 1.0.37 → 1.0.38

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.
Files changed (125) hide show
  1. package/dist/cjs/agents/AgentContext.cjs +69 -14
  2. package/dist/cjs/agents/AgentContext.cjs.map +1 -1
  3. package/dist/cjs/common/enum.cjs +3 -1
  4. package/dist/cjs/common/enum.cjs.map +1 -1
  5. package/dist/cjs/graphs/Graph.cjs +50 -8
  6. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  7. package/dist/cjs/graphs/MultiAgentGraph.cjs +277 -11
  8. package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
  9. package/dist/cjs/llm/bedrock/index.cjs +128 -61
  10. package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
  11. package/dist/cjs/main.cjs +16 -7
  12. package/dist/cjs/main.cjs.map +1 -1
  13. package/dist/cjs/messages/cache.cjs +1 -0
  14. package/dist/cjs/messages/cache.cjs.map +1 -1
  15. package/dist/cjs/messages/core.cjs +1 -1
  16. package/dist/cjs/messages/core.cjs.map +1 -1
  17. package/dist/cjs/messages/tools.cjs +2 -2
  18. package/dist/cjs/messages/tools.cjs.map +1 -1
  19. package/dist/cjs/stream.cjs +4 -2
  20. package/dist/cjs/stream.cjs.map +1 -1
  21. package/dist/cjs/tools/BrowserTools.cjs.map +1 -1
  22. package/dist/cjs/tools/CodeExecutor.cjs +22 -21
  23. package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
  24. package/dist/cjs/tools/ProgrammaticToolCalling.cjs +14 -11
  25. package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
  26. package/dist/cjs/tools/ToolNode.cjs +101 -2
  27. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  28. package/dist/cjs/tools/ToolSearch.cjs +862 -0
  29. package/dist/cjs/tools/ToolSearch.cjs.map +1 -0
  30. package/dist/esm/agents/AgentContext.mjs +69 -14
  31. package/dist/esm/agents/AgentContext.mjs.map +1 -1
  32. package/dist/esm/common/enum.mjs +3 -1
  33. package/dist/esm/common/enum.mjs.map +1 -1
  34. package/dist/esm/graphs/Graph.mjs +51 -9
  35. package/dist/esm/graphs/Graph.mjs.map +1 -1
  36. package/dist/esm/graphs/MultiAgentGraph.mjs +278 -12
  37. package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
  38. package/dist/esm/llm/bedrock/index.mjs +127 -60
  39. package/dist/esm/llm/bedrock/index.mjs.map +1 -1
  40. package/dist/esm/main.mjs +1 -1
  41. package/dist/esm/messages/cache.mjs +1 -0
  42. package/dist/esm/messages/cache.mjs.map +1 -1
  43. package/dist/esm/messages/core.mjs +1 -1
  44. package/dist/esm/messages/core.mjs.map +1 -1
  45. package/dist/esm/messages/tools.mjs +2 -2
  46. package/dist/esm/messages/tools.mjs.map +1 -1
  47. package/dist/esm/stream.mjs +4 -2
  48. package/dist/esm/stream.mjs.map +1 -1
  49. package/dist/esm/tools/BrowserTools.mjs.map +1 -1
  50. package/dist/esm/tools/CodeExecutor.mjs +22 -21
  51. package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
  52. package/dist/esm/tools/ProgrammaticToolCalling.mjs +14 -11
  53. package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
  54. package/dist/esm/tools/ToolNode.mjs +102 -3
  55. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  56. package/dist/esm/tools/ToolSearch.mjs +827 -0
  57. package/dist/esm/tools/ToolSearch.mjs.map +1 -0
  58. package/dist/types/agents/AgentContext.d.ts +33 -1
  59. package/dist/types/common/enum.d.ts +4 -2
  60. package/dist/types/graphs/Graph.d.ts +6 -0
  61. package/dist/types/graphs/MultiAgentGraph.d.ts +16 -0
  62. package/dist/types/index.d.ts +1 -1
  63. package/dist/types/llm/bedrock/index.d.ts +89 -11
  64. package/dist/types/llm/bedrock/types.d.ts +27 -0
  65. package/dist/types/llm/bedrock/utils/index.d.ts +5 -0
  66. package/dist/types/llm/bedrock/utils/message_inputs.d.ts +31 -0
  67. package/dist/types/llm/bedrock/utils/message_outputs.d.ts +33 -0
  68. package/dist/types/tools/CodeExecutor.d.ts +0 -3
  69. package/dist/types/tools/ProgrammaticToolCalling.d.ts +0 -3
  70. package/dist/types/tools/ToolNode.d.ts +3 -1
  71. package/dist/types/tools/ToolSearch.d.ts +148 -0
  72. package/dist/types/types/graph.d.ts +2 -0
  73. package/dist/types/types/llm.d.ts +3 -1
  74. package/dist/types/types/tools.d.ts +42 -2
  75. package/package.json +12 -5
  76. package/src/agents/AgentContext.ts +88 -16
  77. package/src/common/enum.ts +3 -1
  78. package/src/graphs/Graph.ts +64 -13
  79. package/src/graphs/MultiAgentGraph.ts +350 -13
  80. package/src/index.ts +1 -1
  81. package/src/llm/bedrock/index.ts +221 -99
  82. package/src/llm/bedrock/llm.spec.ts +616 -0
  83. package/src/llm/bedrock/types.ts +51 -0
  84. package/src/llm/bedrock/utils/index.ts +18 -0
  85. package/src/llm/bedrock/utils/message_inputs.ts +563 -0
  86. package/src/llm/bedrock/utils/message_outputs.ts +310 -0
  87. package/src/messages/__tests__/tools.test.ts +21 -21
  88. package/src/messages/cache.test.ts +259 -0
  89. package/src/messages/cache.ts +104 -1
  90. package/src/messages/core.ts +1 -1
  91. package/src/messages/tools.ts +2 -2
  92. package/src/scripts/caching.ts +27 -19
  93. package/src/scripts/code_exec_files.ts +58 -15
  94. package/src/scripts/code_exec_multi_session.ts +241 -0
  95. package/src/scripts/code_exec_session.ts +282 -0
  96. package/src/scripts/multi-agent-conditional.ts +1 -0
  97. package/src/scripts/multi-agent-supervisor.ts +1 -0
  98. package/src/scripts/programmatic_exec_agent.ts +4 -4
  99. package/src/scripts/test-handoff-preamble.ts +277 -0
  100. package/src/scripts/test-parallel-handoffs.ts +291 -0
  101. package/src/scripts/test-tools-before-handoff.ts +8 -4
  102. package/src/scripts/test_code_api.ts +361 -0
  103. package/src/scripts/thinking-bedrock.ts +159 -0
  104. package/src/scripts/thinking.ts +39 -18
  105. package/src/scripts/{tool_search_regex.ts → tool_search.ts} +5 -5
  106. package/src/scripts/tools.ts +7 -3
  107. package/src/stream.ts +4 -2
  108. package/src/tools/BrowserTools.ts +39 -17
  109. package/src/tools/CodeExecutor.ts +26 -23
  110. package/src/tools/ProgrammaticToolCalling.ts +18 -14
  111. package/src/tools/ToolNode.ts +114 -1
  112. package/src/tools/ToolSearch.ts +1041 -0
  113. package/src/tools/__tests__/ProgrammaticToolCalling.test.ts +0 -2
  114. package/src/tools/__tests__/{ToolSearchRegex.integration.test.ts → ToolSearch.integration.test.ts} +6 -6
  115. package/src/tools/__tests__/ToolSearch.test.ts +1003 -0
  116. package/src/types/graph.ts +2 -0
  117. package/src/types/llm.ts +3 -1
  118. package/src/types/tools.ts +51 -2
  119. package/dist/cjs/tools/ToolSearchRegex.cjs +0 -455
  120. package/dist/cjs/tools/ToolSearchRegex.cjs.map +0 -1
  121. package/dist/esm/tools/ToolSearchRegex.mjs +0 -448
  122. package/dist/esm/tools/ToolSearchRegex.mjs.map +0 -1
  123. package/dist/types/tools/ToolSearchRegex.d.ts +0 -80
  124. package/src/tools/ToolSearchRegex.ts +0 -535
  125. package/src/tools/__tests__/ToolSearchRegex.test.ts +0 -232
@@ -0,0 +1,862 @@
1
+ 'use strict';
2
+
3
+ var zod = require('zod');
4
+ var okapibm25Module = require('okapibm25');
5
+ var dotenv = require('dotenv');
6
+ var fetch = require('node-fetch');
7
+ var httpsProxyAgent = require('https-proxy-agent');
8
+ var env = require('@langchain/core/utils/env');
9
+ var tools = require('@langchain/core/tools');
10
+ var CodeExecutor = require('./CodeExecutor.cjs');
11
+ var _enum = require('../common/enum.cjs');
12
+
13
+ function _interopNamespaceDefault(e) {
14
+ var n = Object.create(null);
15
+ if (e) {
16
+ Object.keys(e).forEach(function (k) {
17
+ if (k !== 'default') {
18
+ var d = Object.getOwnPropertyDescriptor(e, k);
19
+ Object.defineProperty(n, k, d.get ? d : {
20
+ enumerable: true,
21
+ get: function () { return e[k]; }
22
+ });
23
+ }
24
+ });
25
+ }
26
+ n.default = e;
27
+ return Object.freeze(n);
28
+ }
29
+
30
+ var okapibm25Module__namespace = /*#__PURE__*/_interopNamespaceDefault(okapibm25Module);
31
+
32
+ // src/tools/ToolSearch.ts
33
+ function getBM25Function() {
34
+ const mod = okapibm25Module__namespace;
35
+ if (typeof mod === 'function')
36
+ return mod;
37
+ if (typeof mod.default === 'function')
38
+ return mod.default;
39
+ if (mod.default != null && typeof mod.default.default === 'function')
40
+ return mod.default.default;
41
+ throw new Error('Could not resolve BM25 function from okapibm25 module');
42
+ }
43
+ const BM25 = getBM25Function();
44
+ dotenv.config();
45
+ /** Maximum allowed regex pattern length */
46
+ const MAX_PATTERN_LENGTH = 200;
47
+ /** Maximum allowed regex nesting depth */
48
+ const MAX_REGEX_COMPLEXITY = 5;
49
+ /** Default search timeout in milliseconds */
50
+ const SEARCH_TIMEOUT = 5000;
51
+ /**
52
+ * Creates the Zod schema with dynamic query description based on mode.
53
+ * @param mode - The search mode determining query interpretation
54
+ * @returns Zod schema for tool search parameters
55
+ */
56
+ function createToolSearchSchema(mode) {
57
+ const queryDescription = mode === 'local'
58
+ ? 'Search term to find in tool names and descriptions. Case-insensitive substring matching. Optional if mcp_server is provided.'
59
+ : 'Regex pattern to search tool names and descriptions. Optional if mcp_server is provided.';
60
+ return zod.z.object({
61
+ query: zod.z
62
+ .string()
63
+ .max(MAX_PATTERN_LENGTH)
64
+ .optional()
65
+ .default('')
66
+ .describe(queryDescription),
67
+ fields: zod.z
68
+ .array(zod.z.enum(['name', 'description', 'parameters']))
69
+ .optional()
70
+ .default(['name', 'description'])
71
+ .describe('Which fields to search. Default: name and description'),
72
+ max_results: zod.z
73
+ .number()
74
+ .int()
75
+ .min(1)
76
+ .max(50)
77
+ .optional()
78
+ .default(10)
79
+ .describe('Maximum number of matching tools to return'),
80
+ mcp_server: zod.z
81
+ .union([zod.z.string(), zod.z.array(zod.z.string())])
82
+ .optional()
83
+ .describe('Filter to tools from specific MCP server(s). Can be a single server name or array of names. If provided without a query, lists all tools from those servers.'),
84
+ });
85
+ }
86
+ /**
87
+ * Extracts the MCP server name from a tool name.
88
+ * MCP tools follow the pattern: toolName_mcp_serverName
89
+ * @param toolName - The full tool name
90
+ * @returns The server name if it's an MCP tool, undefined otherwise
91
+ */
92
+ function extractMcpServerName(toolName) {
93
+ const delimiterIndex = toolName.indexOf(_enum.Constants.MCP_DELIMITER);
94
+ if (delimiterIndex === -1) {
95
+ return undefined;
96
+ }
97
+ return toolName.substring(delimiterIndex + _enum.Constants.MCP_DELIMITER.length);
98
+ }
99
+ /**
100
+ * Checks if a tool belongs to a specific MCP server.
101
+ * @param toolName - The full tool name
102
+ * @param serverName - The server name to match
103
+ * @returns True if the tool belongs to the specified server
104
+ */
105
+ function isFromMcpServer(toolName, serverName) {
106
+ const toolServer = extractMcpServerName(toolName);
107
+ return toolServer === serverName;
108
+ }
109
+ /**
110
+ * Checks if a tool belongs to any of the specified MCP servers.
111
+ * @param toolName - The full tool name
112
+ * @param serverNames - Array of server names to match
113
+ * @returns True if the tool belongs to any of the specified servers
114
+ */
115
+ function isFromAnyMcpServer(toolName, serverNames) {
116
+ const toolServer = extractMcpServerName(toolName);
117
+ if (toolServer === undefined) {
118
+ return false;
119
+ }
120
+ return serverNames.includes(toolServer);
121
+ }
122
+ /**
123
+ * Normalizes server filter input to always be an array.
124
+ * @param serverFilter - String, array of strings, or undefined
125
+ * @returns Array of server names (empty if none specified)
126
+ */
127
+ function normalizeServerFilter(serverFilter) {
128
+ if (serverFilter === undefined) {
129
+ return [];
130
+ }
131
+ if (typeof serverFilter === 'string') {
132
+ return serverFilter === '' ? [] : [serverFilter];
133
+ }
134
+ return serverFilter.filter((s) => s !== '');
135
+ }
136
+ /**
137
+ * Extracts all unique MCP server names from a tool registry.
138
+ * @param toolRegistry - The tool registry to scan
139
+ * @param onlyDeferred - If true, only considers deferred tools
140
+ * @returns Array of unique server names, sorted alphabetically
141
+ */
142
+ function getAvailableMcpServers(toolRegistry, onlyDeferred = true) {
143
+ if (!toolRegistry) {
144
+ return [];
145
+ }
146
+ const servers = new Set();
147
+ for (const [, toolDef] of toolRegistry) {
148
+ if (onlyDeferred && toolDef.defer_loading !== true) {
149
+ continue;
150
+ }
151
+ const server = extractMcpServerName(toolDef.name);
152
+ if (server !== undefined && server !== '') {
153
+ servers.add(server);
154
+ }
155
+ }
156
+ return Array.from(servers).sort();
157
+ }
158
+ /**
159
+ * Escapes special regex characters in a string to use as a literal pattern.
160
+ * @param pattern - The string to escape
161
+ * @returns The escaped string safe for use in a RegExp
162
+ */
163
+ function escapeRegexSpecialChars(pattern) {
164
+ return pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
165
+ }
166
+ /**
167
+ * Counts the maximum nesting depth of groups in a regex pattern.
168
+ * @param pattern - The regex pattern to analyze
169
+ * @returns The maximum nesting depth
170
+ */
171
+ function countNestedGroups(pattern) {
172
+ let maxDepth = 0;
173
+ let currentDepth = 0;
174
+ for (let i = 0; i < pattern.length; i++) {
175
+ if (pattern[i] === '(' && (i === 0 || pattern[i - 1] !== '\\')) {
176
+ currentDepth++;
177
+ maxDepth = Math.max(maxDepth, currentDepth);
178
+ }
179
+ else if (pattern[i] === ')' && (i === 0 || pattern[i - 1] !== '\\')) {
180
+ currentDepth = Math.max(0, currentDepth - 1);
181
+ }
182
+ }
183
+ return maxDepth;
184
+ }
185
+ /**
186
+ * Detects nested quantifiers that can cause catastrophic backtracking.
187
+ * Patterns like (a+)+, (a*)*, (a+)*, etc.
188
+ * @param pattern - The regex pattern to check
189
+ * @returns True if nested quantifiers are detected
190
+ */
191
+ function hasNestedQuantifiers(pattern) {
192
+ const nestedQuantifierPattern = /\([^)]*[+*][^)]*\)[+*?]/;
193
+ return nestedQuantifierPattern.test(pattern);
194
+ }
195
+ /**
196
+ * Checks if a regex pattern contains potentially dangerous constructs.
197
+ * @param pattern - The regex pattern to validate
198
+ * @returns True if the pattern is dangerous
199
+ */
200
+ function isDangerousPattern(pattern) {
201
+ if (hasNestedQuantifiers(pattern)) {
202
+ return true;
203
+ }
204
+ if (countNestedGroups(pattern) > MAX_REGEX_COMPLEXITY) {
205
+ return true;
206
+ }
207
+ const dangerousPatterns = [
208
+ /\.\{1000,\}/, // Excessive wildcards
209
+ /\(\?=\.\{100,\}\)/, // Runaway lookaheads
210
+ /\([^)]*\|\s*\){20,}/, // Excessive alternation (rough check)
211
+ /\(\.\*\)\+/, // (.*)+
212
+ /\(\.\+\)\+/, // (.+)+
213
+ /\(\.\*\)\*/, // (.*)*
214
+ /\(\.\+\)\*/, // (.+)*
215
+ ];
216
+ for (const dangerous of dangerousPatterns) {
217
+ if (dangerous.test(pattern)) {
218
+ return true;
219
+ }
220
+ }
221
+ return false;
222
+ }
223
+ /**
224
+ * Sanitizes a regex pattern for safe execution.
225
+ * If the pattern is dangerous, it will be escaped to a literal string search.
226
+ * @param pattern - The regex pattern to sanitize
227
+ * @returns Object containing the safe pattern and whether it was escaped
228
+ */
229
+ function sanitizeRegex(pattern) {
230
+ if (isDangerousPattern(pattern)) {
231
+ return {
232
+ safe: escapeRegexSpecialChars(pattern),
233
+ wasEscaped: true,
234
+ };
235
+ }
236
+ try {
237
+ new RegExp(pattern);
238
+ return { safe: pattern, wasEscaped: false };
239
+ }
240
+ catch {
241
+ return {
242
+ safe: escapeRegexSpecialChars(pattern),
243
+ wasEscaped: true,
244
+ };
245
+ }
246
+ }
247
+ /**
248
+ * Simplifies tool parameters for search purposes.
249
+ * Extracts only the essential structure needed for parameter name searching.
250
+ * @param parameters - The tool's JSON schema parameters
251
+ * @returns Simplified parameters object
252
+ */
253
+ function simplifyParametersForSearch(parameters) {
254
+ if (!parameters) {
255
+ return undefined;
256
+ }
257
+ if (parameters.properties) {
258
+ return {
259
+ type: parameters.type,
260
+ properties: Object.fromEntries(Object.entries(parameters.properties).map(([key, value]) => [
261
+ key,
262
+ { type: value.type },
263
+ ])),
264
+ };
265
+ }
266
+ return { type: parameters.type };
267
+ }
268
+ /**
269
+ * Tokenizes a string into lowercase words for BM25.
270
+ * Splits on underscores and non-alphanumeric characters for consistent matching.
271
+ * @param text - The text to tokenize
272
+ * @returns Array of lowercase tokens
273
+ */
274
+ function tokenize(text) {
275
+ return text
276
+ .toLowerCase()
277
+ .replace(/[^a-z0-9]/g, ' ')
278
+ .split(/\s+/)
279
+ .filter((token) => token.length > 0);
280
+ }
281
+ /**
282
+ * Creates a searchable document string from tool metadata.
283
+ * @param tool - The tool metadata
284
+ * @param fields - Which fields to include
285
+ * @returns Combined document string for BM25
286
+ */
287
+ function createToolDocument(tool, fields) {
288
+ const parts = [];
289
+ if (fields.includes('name')) {
290
+ const baseName = tool.name.replace(/_/g, ' ');
291
+ parts.push(baseName, baseName);
292
+ }
293
+ if (fields.includes('description') && tool.description) {
294
+ parts.push(tool.description);
295
+ }
296
+ if (fields.includes('parameters') && tool.parameters?.properties) {
297
+ const paramNames = Object.keys(tool.parameters.properties).join(' ');
298
+ parts.push(paramNames);
299
+ }
300
+ return parts.join(' ');
301
+ }
302
+ /**
303
+ * Determines which field had the best match for a query.
304
+ * @param tool - The tool to check
305
+ * @param queryTokens - Tokenized query
306
+ * @param fields - Fields to check
307
+ * @returns The matched field and a snippet
308
+ */
309
+ function findMatchedField(tool, queryTokens, fields) {
310
+ if (fields.includes('name')) {
311
+ const nameLower = tool.name.toLowerCase();
312
+ for (const token of queryTokens) {
313
+ if (nameLower.includes(token)) {
314
+ return { field: 'name', snippet: tool.name };
315
+ }
316
+ }
317
+ }
318
+ if (fields.includes('description') && tool.description) {
319
+ const descLower = tool.description.toLowerCase();
320
+ for (const token of queryTokens) {
321
+ if (descLower.includes(token)) {
322
+ return {
323
+ field: 'description',
324
+ snippet: tool.description.substring(0, 100),
325
+ };
326
+ }
327
+ }
328
+ }
329
+ if (fields.includes('parameters') && tool.parameters?.properties) {
330
+ const paramNames = Object.keys(tool.parameters.properties);
331
+ const paramLower = paramNames.join(' ').toLowerCase();
332
+ for (const token of queryTokens) {
333
+ if (paramLower.includes(token)) {
334
+ return { field: 'parameters', snippet: paramNames.join(', ') };
335
+ }
336
+ }
337
+ }
338
+ const fallbackSnippet = tool.description
339
+ ? tool.description.substring(0, 100)
340
+ : tool.name;
341
+ return { field: 'unknown', snippet: fallbackSnippet };
342
+ }
343
+ /**
344
+ * Performs BM25-based search for better relevance ranking.
345
+ * Uses Okapi BM25 algorithm for term frequency and document length normalization.
346
+ * @param tools - Array of tool metadata to search
347
+ * @param query - The search query
348
+ * @param fields - Which fields to search
349
+ * @param maxResults - Maximum results to return
350
+ * @returns Search response with matching tools ranked by BM25 score
351
+ */
352
+ function performLocalSearch(tools, query, fields, maxResults) {
353
+ if (tools.length === 0 || !query.trim()) {
354
+ return {
355
+ tool_references: [],
356
+ total_tools_searched: tools.length,
357
+ pattern_used: query,
358
+ };
359
+ }
360
+ const documents = tools.map((tool) => createToolDocument(tool, fields));
361
+ const queryTokens = tokenize(query);
362
+ if (queryTokens.length === 0) {
363
+ return {
364
+ tool_references: [],
365
+ total_tools_searched: tools.length,
366
+ pattern_used: query,
367
+ };
368
+ }
369
+ const scores = BM25(documents, queryTokens, { k1: 1.5, b: 0.75 });
370
+ const maxScore = Math.max(...scores.filter((s) => s > 0), 1);
371
+ const queryLower = query.toLowerCase().trim();
372
+ const results = [];
373
+ for (let i = 0; i < tools.length; i++) {
374
+ if (scores[i] > 0) {
375
+ const { field, snippet } = findMatchedField(tools[i], queryTokens, fields);
376
+ let normalizedScore = Math.min(scores[i] / maxScore, 1.0);
377
+ // Boost score for exact base name match
378
+ const baseName = getBaseToolName(tools[i].name).toLowerCase();
379
+ if (baseName === queryLower) {
380
+ normalizedScore = 1.0;
381
+ }
382
+ else if (baseName.startsWith(queryLower)) {
383
+ normalizedScore = Math.max(normalizedScore, 0.95);
384
+ }
385
+ results.push({
386
+ tool_name: tools[i].name,
387
+ match_score: normalizedScore,
388
+ matched_field: field,
389
+ snippet,
390
+ });
391
+ }
392
+ }
393
+ results.sort((a, b) => b.match_score - a.match_score);
394
+ const topResults = results.slice(0, maxResults);
395
+ return {
396
+ tool_references: topResults,
397
+ total_tools_searched: tools.length,
398
+ pattern_used: query,
399
+ };
400
+ }
401
+ /**
402
+ * Generates the JavaScript search script to be executed in the sandbox.
403
+ * Uses plain JavaScript for maximum compatibility with the Code API.
404
+ * @param deferredTools - Array of tool metadata to search through
405
+ * @param fields - Which fields to search
406
+ * @param maxResults - Maximum number of results to return
407
+ * @param sanitizedPattern - The sanitized regex pattern
408
+ * @returns The JavaScript code string
409
+ */
410
+ function generateSearchScript(deferredTools, fields, maxResults, sanitizedPattern) {
411
+ const lines = [
412
+ '// Tool definitions (injected)',
413
+ 'var tools = ' + JSON.stringify(deferredTools) + ';',
414
+ 'var searchFields = ' + JSON.stringify(fields) + ';',
415
+ 'var maxResults = ' + maxResults + ';',
416
+ 'var pattern = ' + JSON.stringify(sanitizedPattern) + ';',
417
+ '',
418
+ '// Compile regex (pattern is sanitized client-side)',
419
+ 'var regex;',
420
+ 'try {',
421
+ ' regex = new RegExp(pattern, \'i\');',
422
+ '} catch (e) {',
423
+ ' regex = new RegExp(pattern.replace(/[.*+?^${}()[\\]\\\\|]/g, "\\\\$&"), "i");',
424
+ '}',
425
+ '',
426
+ '// Search logic',
427
+ 'var results = [];',
428
+ '',
429
+ 'for (var j = 0; j < tools.length; j++) {',
430
+ ' var tool = tools[j];',
431
+ ' var bestScore = 0;',
432
+ ' var matchedField = \'\';',
433
+ ' var snippet = \'\';',
434
+ '',
435
+ ' // Search name (highest priority)',
436
+ ' if (searchFields.indexOf(\'name\') >= 0 && regex.test(tool.name)) {',
437
+ ' bestScore = 0.95;',
438
+ ' matchedField = \'name\';',
439
+ ' snippet = tool.name;',
440
+ ' }',
441
+ '',
442
+ ' // Search description (medium priority)',
443
+ ' if (searchFields.indexOf(\'description\') >= 0 && tool.description && regex.test(tool.description)) {',
444
+ ' if (bestScore === 0) {',
445
+ ' bestScore = 0.75;',
446
+ ' matchedField = \'description\';',
447
+ ' snippet = tool.description.substring(0, 100);',
448
+ ' }',
449
+ ' }',
450
+ '',
451
+ ' // Search parameter names (lower priority)',
452
+ ' if (searchFields.indexOf(\'parameters\') >= 0 && tool.parameters && tool.parameters.properties) {',
453
+ ' var paramNames = Object.keys(tool.parameters.properties).join(\' \');',
454
+ ' if (regex.test(paramNames)) {',
455
+ ' if (bestScore === 0) {',
456
+ ' bestScore = 0.60;',
457
+ ' matchedField = \'parameters\';',
458
+ ' snippet = paramNames;',
459
+ ' }',
460
+ ' }',
461
+ ' }',
462
+ '',
463
+ ' if (bestScore > 0) {',
464
+ ' results.push({',
465
+ ' tool_name: tool.name,',
466
+ ' match_score: bestScore,',
467
+ ' matched_field: matchedField,',
468
+ ' snippet: snippet',
469
+ ' });',
470
+ ' }',
471
+ '}',
472
+ '',
473
+ '// Sort by score (descending) and limit results',
474
+ 'results.sort(function(a, b) { return b.match_score - a.match_score; });',
475
+ 'var topResults = results.slice(0, maxResults);',
476
+ '',
477
+ '// Output as JSON',
478
+ 'console.log(JSON.stringify({',
479
+ ' tool_references: topResults.map(function(r) {',
480
+ ' return {',
481
+ ' tool_name: r.tool_name,',
482
+ ' match_score: r.match_score,',
483
+ ' matched_field: r.matched_field,',
484
+ ' snippet: r.snippet',
485
+ ' };',
486
+ ' }),',
487
+ ' total_tools_searched: tools.length,',
488
+ ' pattern_used: pattern',
489
+ '}));',
490
+ ];
491
+ return lines.join('\n');
492
+ }
493
+ /**
494
+ * Parses the search results from stdout JSON.
495
+ * @param stdout - The stdout string containing JSON results
496
+ * @returns Parsed search response
497
+ */
498
+ function parseSearchResults(stdout) {
499
+ const jsonMatch = stdout.trim();
500
+ const parsed = JSON.parse(jsonMatch);
501
+ return parsed;
502
+ }
503
+ /**
504
+ * Formats search results as structured JSON for efficient parsing.
505
+ * @param searchResponse - The parsed search response
506
+ * @returns JSON string with search results
507
+ */
508
+ function formatSearchResults(searchResponse) {
509
+ const { tool_references, total_tools_searched, pattern_used } = searchResponse;
510
+ const output = {
511
+ found: tool_references.length,
512
+ tools: tool_references.map((ref) => ({
513
+ name: ref.tool_name,
514
+ score: Number(ref.match_score.toFixed(2)),
515
+ matched_in: ref.matched_field,
516
+ snippet: ref.snippet,
517
+ })),
518
+ total_searched: total_tools_searched,
519
+ query: pattern_used,
520
+ };
521
+ return JSON.stringify(output, null, 2);
522
+ }
523
+ /**
524
+ * Extracts the base tool name (without MCP server suffix) from a full tool name.
525
+ * @param toolName - The full tool name
526
+ * @returns The base tool name without server suffix
527
+ */
528
+ function getBaseToolName(toolName) {
529
+ const delimiterIndex = toolName.indexOf(_enum.Constants.MCP_DELIMITER);
530
+ if (delimiterIndex === -1) {
531
+ return toolName;
532
+ }
533
+ return toolName.substring(0, delimiterIndex);
534
+ }
535
+ /**
536
+ * Generates a compact listing of deferred tools grouped by server.
537
+ * Format: "server: tool1, tool2, tool3"
538
+ * Non-MCP tools are grouped under "other".
539
+ * @param toolRegistry - The tool registry
540
+ * @param onlyDeferred - Whether to only include deferred tools
541
+ * @returns Formatted string with tools grouped by server
542
+ */
543
+ function getDeferredToolsListing(toolRegistry, onlyDeferred) {
544
+ if (!toolRegistry) {
545
+ return '';
546
+ }
547
+ const toolsByServer = {};
548
+ for (const lcTool of toolRegistry.values()) {
549
+ if (onlyDeferred && lcTool.defer_loading !== true) {
550
+ continue;
551
+ }
552
+ const toolName = lcTool.name;
553
+ const serverName = extractMcpServerName(toolName) ?? 'other';
554
+ const baseName = getBaseToolName(toolName);
555
+ if (!(serverName in toolsByServer)) {
556
+ toolsByServer[serverName] = [];
557
+ }
558
+ toolsByServer[serverName].push(baseName);
559
+ }
560
+ const serverNames = Object.keys(toolsByServer).sort((a, b) => {
561
+ if (a === 'other')
562
+ return 1;
563
+ if (b === 'other')
564
+ return -1;
565
+ return a.localeCompare(b);
566
+ });
567
+ if (serverNames.length === 0) {
568
+ return '';
569
+ }
570
+ const lines = serverNames.map((server) => `${server}: ${toolsByServer[server].join(', ')}`);
571
+ return lines.join('\n');
572
+ }
573
+ /**
574
+ * Formats a server listing response as structured JSON.
575
+ * NOTE: This is a PREVIEW only - tools are NOT discovered/loaded.
576
+ * @param tools - Array of tool metadata from the server(s)
577
+ * @param serverNames - The MCP server name(s)
578
+ * @returns JSON string showing all tools grouped by server
579
+ */
580
+ function formatServerListing(tools, serverNames) {
581
+ const servers = Array.isArray(serverNames) ? serverNames : [serverNames];
582
+ if (tools.length === 0) {
583
+ return JSON.stringify({
584
+ listing_mode: true,
585
+ servers,
586
+ total_tools: 0,
587
+ tools_by_server: {},
588
+ hint: 'No tools found from the specified MCP server(s).',
589
+ }, null, 2);
590
+ }
591
+ const toolsByServer = {};
592
+ for (const tool of tools) {
593
+ const server = extractMcpServerName(tool.name) ?? 'unknown';
594
+ if (!(server in toolsByServer)) {
595
+ toolsByServer[server] = [];
596
+ }
597
+ toolsByServer[server].push({
598
+ name: getBaseToolName(tool.name),
599
+ description: tool.description.length > 100
600
+ ? tool.description.substring(0, 97) + '...'
601
+ : tool.description,
602
+ });
603
+ }
604
+ const output = {
605
+ listing_mode: true,
606
+ servers,
607
+ total_tools: tools.length,
608
+ tools_by_server: toolsByServer,
609
+ hint: `To use a tool, search for it by name (e.g., query: "${getBaseToolName(tools[0]?.name ?? 'tool_name')}") to load it.`,
610
+ };
611
+ return JSON.stringify(output, null, 2);
612
+ }
613
+ /**
614
+ * Creates a Tool Search tool for discovering tools from a large registry.
615
+ *
616
+ * This tool enables AI agents to dynamically discover tools from a large library
617
+ * without loading all tool definitions into the LLM context window. The agent
618
+ * can search for relevant tools on-demand.
619
+ *
620
+ * **Modes:**
621
+ * - `code_interpreter` (default): Uses external sandbox for regex search. Safer for complex patterns.
622
+ * - `local`: Uses safe substring matching locally. No network call, faster, completely safe from ReDoS.
623
+ *
624
+ * The tool registry can be provided either:
625
+ * 1. At initialization time via params.toolRegistry
626
+ * 2. At runtime via config.configurable.toolRegistry when invoking
627
+ *
628
+ * @param params - Configuration parameters for the tool (toolRegistry is optional)
629
+ * @returns A LangChain DynamicStructuredTool for tool searching
630
+ *
631
+ * @example
632
+ * // Option 1: Code interpreter mode (regex via sandbox)
633
+ * const tool = createToolSearch({ apiKey, toolRegistry });
634
+ * await tool.invoke({ query: 'expense.*report' });
635
+ *
636
+ * @example
637
+ * // Option 2: Local mode (safe substring search, no API key needed)
638
+ * const tool = createToolSearch({ mode: 'local', toolRegistry });
639
+ * await tool.invoke({ query: 'expense' });
640
+ */
641
+ function createToolSearch(initParams = {}) {
642
+ const mode = initParams.mode ?? 'code_interpreter';
643
+ const defaultOnlyDeferred = initParams.onlyDeferred ?? true;
644
+ const schema = createToolSearchSchema(mode);
645
+ const apiKey = mode === 'code_interpreter'
646
+ ? (initParams[_enum.EnvVar.CODE_API_KEY] ??
647
+ initParams.apiKey ??
648
+ env.getEnvironmentVariable(_enum.EnvVar.CODE_API_KEY) ??
649
+ '')
650
+ : '';
651
+ if (mode === 'code_interpreter' && !apiKey) {
652
+ throw new Error('No API key provided for tool search in code_interpreter mode. Use mode: "local" to search without an API key.');
653
+ }
654
+ const baseEndpoint = initParams.baseUrl ?? CodeExecutor.getCodeBaseURL();
655
+ const EXEC_ENDPOINT = `${baseEndpoint}/exec`;
656
+ const deferredToolsListing = getDeferredToolsListing(initParams.toolRegistry, defaultOnlyDeferred);
657
+ const toolsListSection = deferredToolsListing.length > 0
658
+ ? `
659
+
660
+ Deferred tools (search to load):
661
+ ${deferredToolsListing}`
662
+ : '';
663
+ const mcpNote = deferredToolsListing.includes(_enum.Constants.MCP_DELIMITER) ||
664
+ deferredToolsListing.split('\n').some((line) => !line.startsWith('other:'))
665
+ ? `
666
+ - MCP tools use format: toolName${_enum.Constants.MCP_DELIMITER}serverName
667
+ - Use mcp_server param to filter by server`
668
+ : '';
669
+ const description = mode === 'local'
670
+ ? `
671
+ Searches deferred tools using BM25 ranking. Multi-word queries supported.
672
+ ${mcpNote}${toolsListSection}
673
+ `.trim()
674
+ : `
675
+ Searches deferred tools by regex pattern.
676
+ ${mcpNote}${toolsListSection}
677
+ `.trim();
678
+ return tools.tool(async (params, config) => {
679
+ const { query, fields = ['name', 'description'], max_results = 10, mcp_server, } = params;
680
+ const { toolRegistry: paramToolRegistry, onlyDeferred: paramOnlyDeferred, mcpServer: paramMcpServer, } = config.toolCall ?? {};
681
+ const toolRegistry = paramToolRegistry ?? initParams.toolRegistry;
682
+ const onlyDeferred = paramOnlyDeferred !== undefined
683
+ ? paramOnlyDeferred
684
+ : defaultOnlyDeferred;
685
+ const rawServerFilter = mcp_server ?? paramMcpServer ?? initParams.mcpServer;
686
+ const serverFilters = normalizeServerFilter(rawServerFilter);
687
+ const hasServerFilter = serverFilters.length > 0;
688
+ if (toolRegistry == null) {
689
+ return [
690
+ 'Error: No tool registry provided. Configure toolRegistry at agent level or initialization.',
691
+ {
692
+ tool_references: [],
693
+ metadata: {
694
+ total_searched: 0,
695
+ pattern: query,
696
+ error: 'No tool registry provided',
697
+ },
698
+ },
699
+ ];
700
+ }
701
+ const toolsArray = Array.from(toolRegistry.values());
702
+ const deferredTools = toolsArray
703
+ .filter((lcTool) => {
704
+ if (onlyDeferred === true && lcTool.defer_loading !== true) {
705
+ return false;
706
+ }
707
+ if (hasServerFilter &&
708
+ !isFromAnyMcpServer(lcTool.name, serverFilters)) {
709
+ return false;
710
+ }
711
+ return true;
712
+ })
713
+ .map((lcTool) => ({
714
+ name: lcTool.name,
715
+ description: lcTool.description ?? '',
716
+ parameters: simplifyParametersForSearch(lcTool.parameters),
717
+ }));
718
+ if (deferredTools.length === 0) {
719
+ const serverMsg = hasServerFilter
720
+ ? ` from MCP server(s): ${serverFilters.join(', ')}`
721
+ : '';
722
+ return [
723
+ `No tools available to search${serverMsg}. The tool registry is empty or no matching deferred tools are registered.`,
724
+ {
725
+ tool_references: [],
726
+ metadata: {
727
+ total_searched: 0,
728
+ pattern: query,
729
+ mcp_server: serverFilters,
730
+ },
731
+ },
732
+ ];
733
+ }
734
+ const isServerListing = hasServerFilter && query === '';
735
+ if (isServerListing) {
736
+ const formattedOutput = formatServerListing(deferredTools, serverFilters);
737
+ return [
738
+ formattedOutput,
739
+ {
740
+ tool_references: [],
741
+ metadata: {
742
+ total_available: deferredTools.length,
743
+ mcp_server: serverFilters,
744
+ listing_mode: true,
745
+ },
746
+ },
747
+ ];
748
+ }
749
+ if (mode === 'local') {
750
+ const searchResponse = performLocalSearch(deferredTools, query, fields, max_results);
751
+ const formattedOutput = formatSearchResults(searchResponse);
752
+ return [
753
+ formattedOutput,
754
+ {
755
+ tool_references: searchResponse.tool_references,
756
+ metadata: {
757
+ total_searched: searchResponse.total_tools_searched,
758
+ pattern: searchResponse.pattern_used,
759
+ mcp_server: serverFilters.length > 0 ? serverFilters : undefined,
760
+ },
761
+ },
762
+ ];
763
+ }
764
+ const { safe: sanitizedPattern, wasEscaped } = sanitizeRegex(query);
765
+ let warningMessage = '';
766
+ if (wasEscaped) {
767
+ warningMessage =
768
+ 'Note: The provided pattern was converted to a literal search for safety.\n\n';
769
+ }
770
+ const searchScript = generateSearchScript(deferredTools, fields, max_results, sanitizedPattern);
771
+ const postData = {
772
+ lang: 'js',
773
+ code: searchScript,
774
+ timeout: SEARCH_TIMEOUT,
775
+ };
776
+ try {
777
+ const fetchOptions = {
778
+ method: 'POST',
779
+ headers: {
780
+ 'Content-Type': 'application/json',
781
+ 'User-Agent': 'LibreChat/1.0',
782
+ 'X-API-Key': apiKey,
783
+ },
784
+ body: JSON.stringify(postData),
785
+ };
786
+ if (process.env.PROXY != null && process.env.PROXY !== '') {
787
+ fetchOptions.agent = new httpsProxyAgent.HttpsProxyAgent(process.env.PROXY);
788
+ }
789
+ const response = await fetch(EXEC_ENDPOINT, fetchOptions);
790
+ if (!response.ok) {
791
+ throw new Error(`HTTP error! status: ${response.status}`);
792
+ }
793
+ const result = await response.json();
794
+ if (result.stderr && result.stderr.trim()) {
795
+ // eslint-disable-next-line no-console
796
+ console.warn('[ToolSearch] stderr:', result.stderr);
797
+ }
798
+ if (!result.stdout || !result.stdout.trim()) {
799
+ return [
800
+ `${warningMessage}No tools matched the pattern "${sanitizedPattern}".\nTotal tools searched: ${deferredTools.length}`,
801
+ {
802
+ tool_references: [],
803
+ metadata: {
804
+ total_searched: deferredTools.length,
805
+ pattern: sanitizedPattern,
806
+ },
807
+ },
808
+ ];
809
+ }
810
+ const searchResponse = parseSearchResults(result.stdout);
811
+ const formattedOutput = `${warningMessage}${formatSearchResults(searchResponse)}`;
812
+ return [
813
+ formattedOutput,
814
+ {
815
+ tool_references: searchResponse.tool_references,
816
+ metadata: {
817
+ total_searched: searchResponse.total_tools_searched,
818
+ pattern: searchResponse.pattern_used,
819
+ },
820
+ },
821
+ ];
822
+ }
823
+ catch (error) {
824
+ // eslint-disable-next-line no-console
825
+ console.error('[ToolSearch] Error:', error);
826
+ const errorMessage = error instanceof Error ? error.message : String(error);
827
+ return [
828
+ `Tool search failed: ${errorMessage}\n\nSuggestion: Try a simpler search pattern or search for specific tool names.`,
829
+ {
830
+ tool_references: [],
831
+ metadata: {
832
+ total_searched: 0,
833
+ pattern: sanitizedPattern,
834
+ error: errorMessage,
835
+ },
836
+ },
837
+ ];
838
+ }
839
+ }, {
840
+ name: _enum.Constants.TOOL_SEARCH,
841
+ description,
842
+ schema,
843
+ responseFormat: _enum.Constants.CONTENT_AND_ARTIFACT,
844
+ });
845
+ }
846
+
847
+ exports.countNestedGroups = countNestedGroups;
848
+ exports.createToolSearch = createToolSearch;
849
+ exports.escapeRegexSpecialChars = escapeRegexSpecialChars;
850
+ exports.extractMcpServerName = extractMcpServerName;
851
+ exports.formatServerListing = formatServerListing;
852
+ exports.getAvailableMcpServers = getAvailableMcpServers;
853
+ exports.getBaseToolName = getBaseToolName;
854
+ exports.getDeferredToolsListing = getDeferredToolsListing;
855
+ exports.hasNestedQuantifiers = hasNestedQuantifiers;
856
+ exports.isDangerousPattern = isDangerousPattern;
857
+ exports.isFromAnyMcpServer = isFromAnyMcpServer;
858
+ exports.isFromMcpServer = isFromMcpServer;
859
+ exports.normalizeServerFilter = normalizeServerFilter;
860
+ exports.performLocalSearch = performLocalSearch;
861
+ exports.sanitizeRegex = sanitizeRegex;
862
+ //# sourceMappingURL=ToolSearch.cjs.map