@juspay/neurolink 7.48.1 → 7.50.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (153) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/README.md +215 -16
  3. package/dist/agent/directTools.d.ts +55 -0
  4. package/dist/agent/directTools.js +266 -0
  5. package/dist/cli/factories/commandFactory.d.ts +6 -0
  6. package/dist/cli/factories/commandFactory.js +149 -16
  7. package/dist/cli/index.js +13 -2
  8. package/dist/cli/loop/conversationSelector.d.ts +45 -0
  9. package/dist/cli/loop/conversationSelector.js +222 -0
  10. package/dist/cli/loop/optionsSchema.d.ts +1 -1
  11. package/dist/cli/loop/session.d.ts +36 -8
  12. package/dist/cli/loop/session.js +257 -61
  13. package/dist/core/baseProvider.d.ts +9 -0
  14. package/dist/core/baseProvider.js +45 -5
  15. package/dist/core/evaluation.js +5 -2
  16. package/dist/factories/providerRegistry.js +2 -2
  17. package/dist/index.d.ts +8 -2
  18. package/dist/index.js +11 -10
  19. package/dist/lib/agent/directTools.d.ts +55 -0
  20. package/dist/lib/agent/directTools.js +266 -0
  21. package/dist/lib/core/baseProvider.d.ts +9 -0
  22. package/dist/lib/core/baseProvider.js +45 -5
  23. package/dist/lib/core/evaluation.js +5 -2
  24. package/dist/lib/factories/providerRegistry.js +2 -2
  25. package/dist/lib/index.d.ts +8 -2
  26. package/dist/lib/index.js +11 -10
  27. package/dist/lib/mcp/factory.d.ts +2 -157
  28. package/dist/lib/mcp/flexibleToolValidator.d.ts +1 -5
  29. package/dist/lib/mcp/index.d.ts +3 -2
  30. package/dist/lib/mcp/mcpCircuitBreaker.d.ts +1 -75
  31. package/dist/lib/mcp/mcpClientFactory.d.ts +1 -20
  32. package/dist/lib/mcp/mcpClientFactory.js +1 -0
  33. package/dist/lib/mcp/registry.d.ts +3 -10
  34. package/dist/lib/mcp/servers/agent/directToolsServer.d.ts +1 -1
  35. package/dist/lib/mcp/servers/aiProviders/aiCoreServer.d.ts +1 -1
  36. package/dist/lib/mcp/servers/utilities/utilityServer.d.ts +1 -1
  37. package/dist/lib/mcp/toolDiscoveryService.d.ts +3 -84
  38. package/dist/lib/mcp/toolRegistry.d.ts +2 -24
  39. package/dist/lib/middleware/builtin/guardrails.d.ts +5 -16
  40. package/dist/lib/middleware/builtin/guardrails.js +44 -39
  41. package/dist/lib/middleware/utils/guardrailsUtils.d.ts +64 -0
  42. package/dist/lib/middleware/utils/guardrailsUtils.js +387 -0
  43. package/dist/lib/neurolink.d.ts +36 -7
  44. package/dist/lib/neurolink.js +141 -0
  45. package/dist/lib/providers/anthropic.js +47 -3
  46. package/dist/lib/providers/azureOpenai.js +9 -2
  47. package/dist/lib/providers/googleAiStudio.js +9 -2
  48. package/dist/lib/providers/googleVertex.js +12 -2
  49. package/dist/lib/providers/huggingFace.js +1 -1
  50. package/dist/lib/providers/litellm.js +1 -1
  51. package/dist/lib/providers/mistral.js +1 -1
  52. package/dist/lib/providers/openAI.js +47 -3
  53. package/dist/lib/services/server/ai/observability/instrumentation.d.ts +57 -0
  54. package/dist/lib/services/server/ai/observability/instrumentation.js +170 -0
  55. package/dist/lib/session/globalSessionState.d.ts +26 -0
  56. package/dist/lib/session/globalSessionState.js +86 -1
  57. package/dist/lib/telemetry/index.d.ts +1 -0
  58. package/dist/lib/telemetry/telemetryService.d.ts +2 -0
  59. package/dist/lib/telemetry/telemetryService.js +7 -7
  60. package/dist/lib/types/cli.d.ts +28 -0
  61. package/dist/lib/types/content.d.ts +18 -5
  62. package/dist/lib/types/contextTypes.d.ts +1 -1
  63. package/dist/lib/types/conversation.d.ts +57 -4
  64. package/dist/lib/types/fileTypes.d.ts +65 -0
  65. package/dist/lib/types/fileTypes.js +4 -0
  66. package/dist/lib/types/generateTypes.d.ts +12 -0
  67. package/dist/lib/types/guardrails.d.ts +103 -0
  68. package/dist/lib/types/guardrails.js +1 -0
  69. package/dist/lib/types/index.d.ts +4 -2
  70. package/dist/lib/types/index.js +4 -0
  71. package/dist/lib/types/mcpTypes.d.ts +407 -14
  72. package/dist/lib/types/modelTypes.d.ts +6 -6
  73. package/dist/lib/types/observability.d.ts +49 -0
  74. package/dist/lib/types/observability.js +6 -0
  75. package/dist/lib/types/streamTypes.d.ts +7 -0
  76. package/dist/lib/types/tools.d.ts +132 -35
  77. package/dist/lib/utils/csvProcessor.d.ts +68 -0
  78. package/dist/lib/utils/csvProcessor.js +277 -0
  79. package/dist/lib/utils/fileDetector.d.ts +57 -0
  80. package/dist/lib/utils/fileDetector.js +457 -0
  81. package/dist/lib/utils/imageProcessor.d.ts +10 -0
  82. package/dist/lib/utils/imageProcessor.js +22 -0
  83. package/dist/lib/utils/loopUtils.d.ts +71 -0
  84. package/dist/lib/utils/loopUtils.js +262 -0
  85. package/dist/lib/utils/messageBuilder.d.ts +2 -1
  86. package/dist/lib/utils/messageBuilder.js +197 -2
  87. package/dist/lib/utils/optionsUtils.d.ts +1 -1
  88. package/dist/mcp/factory.d.ts +2 -157
  89. package/dist/mcp/flexibleToolValidator.d.ts +1 -5
  90. package/dist/mcp/index.d.ts +3 -2
  91. package/dist/mcp/mcpCircuitBreaker.d.ts +1 -75
  92. package/dist/mcp/mcpClientFactory.d.ts +1 -20
  93. package/dist/mcp/mcpClientFactory.js +1 -0
  94. package/dist/mcp/registry.d.ts +3 -10
  95. package/dist/mcp/servers/agent/directToolsServer.d.ts +1 -1
  96. package/dist/mcp/servers/aiProviders/aiCoreServer.d.ts +1 -1
  97. package/dist/mcp/servers/utilities/utilityServer.d.ts +1 -1
  98. package/dist/mcp/toolDiscoveryService.d.ts +3 -84
  99. package/dist/mcp/toolRegistry.d.ts +2 -24
  100. package/dist/middleware/builtin/guardrails.d.ts +5 -16
  101. package/dist/middleware/builtin/guardrails.js +44 -39
  102. package/dist/middleware/utils/guardrailsUtils.d.ts +64 -0
  103. package/dist/middleware/utils/guardrailsUtils.js +387 -0
  104. package/dist/neurolink.d.ts +36 -7
  105. package/dist/neurolink.js +141 -0
  106. package/dist/providers/anthropic.js +47 -3
  107. package/dist/providers/azureOpenai.js +9 -2
  108. package/dist/providers/googleAiStudio.js +9 -2
  109. package/dist/providers/googleVertex.js +12 -2
  110. package/dist/providers/huggingFace.js +1 -1
  111. package/dist/providers/litellm.js +1 -1
  112. package/dist/providers/mistral.js +1 -1
  113. package/dist/providers/openAI.js +47 -3
  114. package/dist/services/server/ai/observability/instrumentation.d.ts +57 -0
  115. package/dist/services/server/ai/observability/instrumentation.js +170 -0
  116. package/dist/session/globalSessionState.d.ts +26 -0
  117. package/dist/session/globalSessionState.js +86 -1
  118. package/dist/telemetry/index.d.ts +1 -0
  119. package/dist/telemetry/telemetryService.d.ts +2 -0
  120. package/dist/telemetry/telemetryService.js +7 -7
  121. package/dist/types/cli.d.ts +28 -0
  122. package/dist/types/content.d.ts +18 -5
  123. package/dist/types/contextTypes.d.ts +1 -1
  124. package/dist/types/conversation.d.ts +57 -4
  125. package/dist/types/fileTypes.d.ts +65 -0
  126. package/dist/types/fileTypes.js +4 -0
  127. package/dist/types/generateTypes.d.ts +12 -0
  128. package/dist/types/guardrails.d.ts +103 -0
  129. package/dist/types/guardrails.js +1 -0
  130. package/dist/types/index.d.ts +4 -2
  131. package/dist/types/index.js +4 -0
  132. package/dist/types/mcpTypes.d.ts +407 -14
  133. package/dist/types/modelTypes.d.ts +6 -6
  134. package/dist/types/observability.d.ts +49 -0
  135. package/dist/types/observability.js +6 -0
  136. package/dist/types/streamTypes.d.ts +7 -0
  137. package/dist/types/tools.d.ts +132 -35
  138. package/dist/utils/csvProcessor.d.ts +68 -0
  139. package/dist/utils/csvProcessor.js +277 -0
  140. package/dist/utils/fileDetector.d.ts +57 -0
  141. package/dist/utils/fileDetector.js +457 -0
  142. package/dist/utils/imageProcessor.d.ts +10 -0
  143. package/dist/utils/imageProcessor.js +22 -0
  144. package/dist/utils/loopUtils.d.ts +71 -0
  145. package/dist/utils/loopUtils.js +262 -0
  146. package/dist/utils/messageBuilder.d.ts +2 -1
  147. package/dist/utils/messageBuilder.js +197 -2
  148. package/dist/utils/optionsUtils.d.ts +1 -1
  149. package/package.json +18 -16
  150. package/dist/lib/mcp/contracts/mcpContract.d.ts +0 -106
  151. package/dist/lib/mcp/contracts/mcpContract.js +0 -5
  152. package/dist/mcp/contracts/mcpContract.d.ts +0 -106
  153. package/dist/mcp/contracts/mcpContract.js +0 -5
@@ -0,0 +1,277 @@
1
+ /**
2
+ * CSV Processing Utility
3
+ * Converts CSV files to LLM-friendly text formats
4
+ * Uses streaming for memory efficiency with large files
5
+ */
6
+ import csvParser from "csv-parser";
7
+ import { Readable } from "stream";
8
+ import { logger } from "./logger.js";
9
+ /**
10
+ * Detect if first line is CSV metadata (not actual data/headers)
11
+ * Common patterns:
12
+ * - Excel separator line: "SEP=,"
13
+ * - Lines with significantly different delimiter count than line 2
14
+ * - Lines that don't match CSV structure of subsequent lines
15
+ */
16
+ function isMetadataLine(lines) {
17
+ if (!lines[0] || lines.length < 2) {
18
+ return false;
19
+ }
20
+ const firstLine = lines[0].trim();
21
+ const secondLine = lines[1].trim();
22
+ if (firstLine.match(/^sep=/i)) {
23
+ return true;
24
+ }
25
+ const firstCommaCount = (firstLine.match(/,/g) || []).length;
26
+ const secondCommaCount = (secondLine.match(/,/g) || []).length;
27
+ if (firstCommaCount === 0 && secondCommaCount > 0) {
28
+ return true;
29
+ }
30
+ if (secondCommaCount > 0 && firstCommaCount !== secondCommaCount) {
31
+ return true;
32
+ }
33
+ return false;
34
+ }
35
+ /**
36
+ * CSV processor for converting CSV data to LLM-optimized formats
37
+ *
38
+ * Supports three output formats:
39
+ * - raw: Original CSV format with proper escaping (RECOMMENDED for best LLM performance)
40
+ * - json: JSON array format (best for structured data processing)
41
+ * - markdown: Markdown table format (best for small datasets <100 rows)
42
+ *
43
+ * All formats use csv-parser for reliable parsing, then convert to the target format.
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * const csvBuffer = Buffer.from('name,age\nAlice,30\nBob,25');
48
+ * const result = await CSVProcessor.process(csvBuffer, {
49
+ * maxRows: 1000,
50
+ * formatStyle: 'raw'
51
+ * });
52
+ * console.log(result.content); // CSV string with proper escaping
53
+ * ```
54
+ */
55
+ export class CSVProcessor {
56
+ /**
57
+ * Process CSV Buffer to LLM-friendly format
58
+ * Content already loaded by FileDetector
59
+ *
60
+ * @param content - CSV file as Buffer
61
+ * @param options - Processing options
62
+ * @returns Formatted CSV data ready for LLM (JSON or Markdown)
63
+ */
64
+ static async process(content, options) {
65
+ const { maxRows: rawMaxRows = 1000, formatStyle = "raw", includeHeaders = true, } = options || {};
66
+ const maxRows = Math.max(1, Math.min(10000, rawMaxRows));
67
+ const csvString = content.toString("utf-8");
68
+ // For raw format, return original CSV with row limit (no parsing needed)
69
+ // This preserves the exact original format which works best for LLMs
70
+ if (formatStyle === "raw") {
71
+ const lines = csvString.split("\n");
72
+ const hasMetadataLine = isMetadataLine(lines);
73
+ // Skip metadata line if present, then take header + maxRows data rows
74
+ const csvLines = hasMetadataLine
75
+ ? lines.slice(1) // Skip metadata line
76
+ : lines;
77
+ const limitedLines = csvLines.slice(0, 1 + maxRows); // header + data rows
78
+ const limitedCSV = limitedLines.join("\n");
79
+ const rowCount = limitedLines.length - 1; // Subtract header
80
+ const originalRowCount = csvLines.length - 1; // Subtract header from original
81
+ logger.debug(`[CSVProcessor] raw format: ${rowCount} rows (original: ${originalRowCount}) → ${limitedCSV.length} chars`, {
82
+ formatStyle: "raw",
83
+ originalSize: csvString.length,
84
+ limitedSize: limitedCSV.length,
85
+ });
86
+ return {
87
+ type: "csv",
88
+ content: limitedCSV,
89
+ mimeType: "text/csv",
90
+ metadata: {
91
+ confidence: 100,
92
+ size: content.length,
93
+ rowCount,
94
+ columnCount: (limitedLines[0] || "").split(",").length,
95
+ },
96
+ };
97
+ }
98
+ // Parse CSV for JSON and Markdown formats only
99
+ const rows = await this.parseCSVString(csvString, maxRows);
100
+ // Extract metadata from parsed results
101
+ const rowCount = rows.length;
102
+ const columnNames = rows.length > 0 ? Object.keys(rows[0]) : [];
103
+ const columnCount = columnNames.length;
104
+ const hasEmptyColumns = columnNames.some((col) => !col || col.trim() === "");
105
+ const sampleRows = rows.slice(0, 3);
106
+ const sampleData = sampleRows.length > 0
107
+ ? JSON.stringify(sampleRows, null, 2)
108
+ : "No data rows";
109
+ // Format parsed data
110
+ const formatted = this.formatForLLM(rows, formatStyle, includeHeaders);
111
+ logger.info(`[CSVProcessor] ${formatStyle} format: ${rowCount} rows × ${columnCount} columns → ${formatted.length} chars`, { rowCount, columnCount, columns: columnNames, hasEmptyColumns });
112
+ return {
113
+ type: "csv",
114
+ content: formatted,
115
+ mimeType: "text/csv",
116
+ metadata: {
117
+ confidence: 100,
118
+ size: content.length,
119
+ rowCount,
120
+ columnCount,
121
+ columnNames,
122
+ sampleData,
123
+ hasEmptyColumns,
124
+ },
125
+ };
126
+ }
127
+ /**
128
+ * Parse CSV string into array of row objects using streaming
129
+ * Memory-efficient for large files
130
+ */
131
+ /**
132
+ * Parse CSV file from disk using streaming (memory efficient)
133
+ *
134
+ * @param filePath - Path to CSV file
135
+ * @param maxRows - Maximum rows to parse (default: 1000)
136
+ * @returns Array of row objects
137
+ */
138
+ static async parseCSVFile(filePath, maxRows = 1000) {
139
+ const clampedMaxRows = Math.max(1, Math.min(10000, maxRows));
140
+ const fs = await import("fs");
141
+ // Read first 2 lines to detect metadata
142
+ const fileHandle = await fs.promises.open(filePath, "r");
143
+ const firstLines = [];
144
+ const lineReader = fileHandle.createReadStream({ encoding: "utf-8" });
145
+ await new Promise((resolve) => {
146
+ let buffer = "";
147
+ lineReader.on("data", (chunk) => {
148
+ buffer += chunk.toString();
149
+ const lines = buffer.split("\n");
150
+ if (lines.length >= 2) {
151
+ firstLines.push(lines[0], lines[1]);
152
+ lineReader.destroy();
153
+ resolve();
154
+ }
155
+ });
156
+ lineReader.on("end", () => resolve());
157
+ });
158
+ await fileHandle.close();
159
+ const hasMetadataLine = isMetadataLine(firstLines);
160
+ const skipLines = hasMetadataLine ? 1 : 0;
161
+ return new Promise((resolve, reject) => {
162
+ const rows = [];
163
+ let count = 0;
164
+ let lineCount = 0;
165
+ const source = fs.createReadStream(filePath, { encoding: "utf-8" });
166
+ const parser = csvParser();
167
+ const abort = () => {
168
+ source.destroy();
169
+ parser.destroy();
170
+ };
171
+ source
172
+ .pipe(parser)
173
+ .on("data", (row) => {
174
+ lineCount++;
175
+ if (lineCount <= skipLines) {
176
+ return;
177
+ }
178
+ rows.push(row);
179
+ count++;
180
+ if (count >= clampedMaxRows) {
181
+ logger.debug(`[CSVProcessor] Reached row limit ${clampedMaxRows}, stopping parse`);
182
+ abort();
183
+ resolve(rows);
184
+ }
185
+ })
186
+ .on("end", () => {
187
+ resolve(rows);
188
+ })
189
+ .on("error", (error) => {
190
+ logger.error("[CSVProcessor] File parsing failed:", error);
191
+ reject(error);
192
+ });
193
+ });
194
+ }
195
+ /**
196
+ * Parse CSV string to array of row objects
197
+ * Exposed for use by tools that need direct CSV parsing
198
+ *
199
+ * @param csvString - CSV data as string
200
+ * @param maxRows - Maximum rows to parse (default: 1000)
201
+ * @returns Array of row objects
202
+ */
203
+ static async parseCSVString(csvString, maxRows = 1000) {
204
+ const clampedMaxRows = Math.max(1, Math.min(10000, maxRows));
205
+ // Detect and skip metadata line
206
+ const lines = csvString.split("\n");
207
+ const hasMetadataLine = isMetadataLine(lines);
208
+ const csvData = hasMetadataLine ? lines.slice(1).join("\n") : csvString;
209
+ return new Promise((resolve, reject) => {
210
+ const rows = [];
211
+ let count = 0;
212
+ const source = Readable.from([csvData]);
213
+ const parser = csvParser();
214
+ const abort = () => {
215
+ source.destroy();
216
+ parser.destroy();
217
+ };
218
+ source
219
+ .pipe(parser)
220
+ .on("data", (row) => {
221
+ rows.push(row);
222
+ count++;
223
+ if (count >= clampedMaxRows) {
224
+ logger.debug(`[CSVProcessor] Reached row limit ${clampedMaxRows}, stopping parse`);
225
+ abort();
226
+ resolve(rows);
227
+ }
228
+ })
229
+ .on("end", () => {
230
+ resolve(rows);
231
+ })
232
+ .on("error", (error) => {
233
+ logger.error("[CSVProcessor] Parsing failed:", error);
234
+ reject(error);
235
+ });
236
+ });
237
+ }
238
+ /**
239
+ * Format parsed CSV data for LLM consumption
240
+ * Only used for JSON and Markdown formats (raw format handled separately)
241
+ */
242
+ static formatForLLM(rows, formatStyle, includeHeaders) {
243
+ if (rows.length === 0) {
244
+ return "CSV file is empty or contains no data.";
245
+ }
246
+ if (formatStyle === "json") {
247
+ return JSON.stringify(rows, null, 2);
248
+ }
249
+ return this.toMarkdownTable(rows, includeHeaders);
250
+ }
251
+ /**
252
+ * Format as markdown table
253
+ * Best for small datasets (<100 rows)
254
+ */
255
+ static toMarkdownTable(rows, includeHeaders) {
256
+ if (rows.length === 0) {
257
+ return "CSV file is empty or contains no data.";
258
+ }
259
+ const headers = Object.keys(rows[0]);
260
+ // Escape backslashes, pipes, and sanitize newlines to keep rows intact
261
+ const escapePipe = (str) => str.replace(/\\/g, "\\\\").replace(/\|/g, "\\|").replace(/\r?\n/g, " ");
262
+ let markdown = "";
263
+ if (includeHeaders) {
264
+ markdown = "| " + headers.map(escapePipe).join(" | ") + " |\n";
265
+ markdown += "|" + headers.map(() => " --- ").join("|") + "|\n";
266
+ }
267
+ rows.forEach((row) => {
268
+ markdown +=
269
+ "| " +
270
+ headers
271
+ .map((h) => escapePipe(String(row[h] || "")))
272
+ .join(" | ") +
273
+ " |\n";
274
+ });
275
+ return markdown;
276
+ }
277
+ }
@@ -0,0 +1,57 @@
1
+ /**
2
+ * File Type Detection Utility
3
+ * Centralized file detection for all multimodal file types
4
+ * Uses multi-strategy approach for reliable type identification
5
+ */
6
+ import type { FileInput, FileProcessingResult, FileDetectorOptions } from "../types/fileTypes.js";
7
+ /**
8
+ * Centralized file type detection and processing
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * // Auto-detect and process any file
13
+ * const result = await FileDetector.detectAndProcess("data.csv");
14
+ * console.log(result.type); // 'csv'
15
+ * ```
16
+ */
17
+ export declare class FileDetector {
18
+ /**
19
+ * Auto-detect file type and process in one call
20
+ *
21
+ * Runs detection strategies in priority order:
22
+ * 1. MagicBytesStrategy (95% confidence) - Binary file headers
23
+ * 2. MimeTypeStrategy (85% confidence) - HTTP Content-Type for URLs
24
+ * 3. ExtensionStrategy (70% confidence) - File extension
25
+ * 4. ContentHeuristicStrategy (75% confidence) - Content analysis
26
+ *
27
+ * @param input - File path, URL, Buffer, or data URI
28
+ * @param options - Detection and processing options
29
+ * @returns Processed file result with type and content
30
+ */
31
+ static detectAndProcess(input: FileInput, options?: FileDetectorOptions): Promise<FileProcessingResult>;
32
+ /**
33
+ * Detect file type using multi-strategy approach
34
+ * Stops at first strategy with confidence >= threshold (default: 80%)
35
+ */
36
+ private static detect;
37
+ /**
38
+ * Load file content from various sources
39
+ */
40
+ private static loadContent;
41
+ /**
42
+ * Route to appropriate processor
43
+ */
44
+ private static processFile;
45
+ /**
46
+ * Load file from URL
47
+ */
48
+ private static loadFromURL;
49
+ /**
50
+ * Load file from filesystem path
51
+ */
52
+ private static loadFromPath;
53
+ /**
54
+ * Load file from data URI
55
+ */
56
+ private static loadFromDataURI;
57
+ }