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