@juspay/neurolink 9.1.1 → 9.2.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 (237) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +54 -7
  3. package/dist/agent/directTools.d.ts +3 -3
  4. package/dist/cli/commands/config.d.ts +6 -6
  5. package/dist/image-gen/ImageGenService.d.ts +143 -0
  6. package/dist/image-gen/ImageGenService.js +345 -0
  7. package/dist/image-gen/imageGenTools.d.ts +126 -0
  8. package/dist/image-gen/imageGenTools.js +304 -0
  9. package/dist/image-gen/index.d.ts +46 -0
  10. package/dist/image-gen/index.js +48 -0
  11. package/dist/image-gen/types.d.ts +237 -0
  12. package/dist/image-gen/types.js +24 -0
  13. package/dist/lib/agent/directTools.d.ts +3 -3
  14. package/dist/lib/image-gen/ImageGenService.d.ts +143 -0
  15. package/dist/lib/image-gen/ImageGenService.js +346 -0
  16. package/dist/lib/image-gen/imageGenTools.d.ts +126 -0
  17. package/dist/lib/image-gen/imageGenTools.js +305 -0
  18. package/dist/lib/image-gen/index.d.ts +46 -0
  19. package/dist/lib/image-gen/index.js +49 -0
  20. package/dist/lib/image-gen/types.d.ts +237 -0
  21. package/dist/lib/image-gen/types.js +25 -0
  22. package/dist/lib/processors/base/BaseFileProcessor.d.ts +273 -0
  23. package/dist/lib/processors/base/BaseFileProcessor.js +614 -0
  24. package/dist/lib/processors/base/index.d.ts +14 -0
  25. package/dist/lib/processors/base/index.js +20 -0
  26. package/dist/lib/processors/base/types.d.ts +593 -0
  27. package/dist/lib/processors/base/types.js +77 -0
  28. package/dist/lib/processors/cli/fileProcessorCli.d.ts +163 -0
  29. package/dist/lib/processors/cli/fileProcessorCli.js +389 -0
  30. package/dist/lib/processors/cli/index.d.ts +37 -0
  31. package/dist/lib/processors/cli/index.js +50 -0
  32. package/dist/lib/processors/code/ConfigProcessor.d.ts +171 -0
  33. package/dist/lib/processors/code/ConfigProcessor.js +401 -0
  34. package/dist/lib/processors/code/SourceCodeProcessor.d.ts +174 -0
  35. package/dist/lib/processors/code/SourceCodeProcessor.js +305 -0
  36. package/dist/lib/processors/code/index.d.ts +44 -0
  37. package/dist/lib/processors/code/index.js +61 -0
  38. package/dist/lib/processors/config/fileTypes.d.ts +283 -0
  39. package/dist/lib/processors/config/fileTypes.js +521 -0
  40. package/dist/lib/processors/config/index.d.ts +32 -0
  41. package/dist/lib/processors/config/index.js +93 -0
  42. package/dist/lib/processors/config/languageMap.d.ts +66 -0
  43. package/dist/lib/processors/config/languageMap.js +411 -0
  44. package/dist/lib/processors/config/mimeTypes.d.ts +376 -0
  45. package/dist/lib/processors/config/mimeTypes.js +339 -0
  46. package/dist/lib/processors/config/sizeLimits.d.ts +194 -0
  47. package/dist/lib/processors/config/sizeLimits.js +247 -0
  48. package/dist/lib/processors/data/JsonProcessor.d.ts +122 -0
  49. package/dist/lib/processors/data/JsonProcessor.js +204 -0
  50. package/dist/lib/processors/data/XmlProcessor.d.ts +160 -0
  51. package/dist/lib/processors/data/XmlProcessor.js +284 -0
  52. package/dist/lib/processors/data/YamlProcessor.d.ts +163 -0
  53. package/dist/lib/processors/data/YamlProcessor.js +295 -0
  54. package/dist/lib/processors/data/index.d.ts +49 -0
  55. package/dist/lib/processors/data/index.js +77 -0
  56. package/dist/lib/processors/document/ExcelProcessor.d.ts +238 -0
  57. package/dist/lib/processors/document/ExcelProcessor.js +520 -0
  58. package/dist/lib/processors/document/OpenDocumentProcessor.d.ts +69 -0
  59. package/dist/lib/processors/document/OpenDocumentProcessor.js +211 -0
  60. package/dist/lib/processors/document/RtfProcessor.d.ts +152 -0
  61. package/dist/lib/processors/document/RtfProcessor.js +362 -0
  62. package/dist/lib/processors/document/WordProcessor.d.ts +168 -0
  63. package/dist/lib/processors/document/WordProcessor.js +354 -0
  64. package/dist/lib/processors/document/index.d.ts +54 -0
  65. package/dist/lib/processors/document/index.js +91 -0
  66. package/dist/lib/processors/errors/FileErrorCode.d.ts +98 -0
  67. package/dist/lib/processors/errors/FileErrorCode.js +256 -0
  68. package/dist/lib/processors/errors/errorHelpers.d.ts +151 -0
  69. package/dist/lib/processors/errors/errorHelpers.js +379 -0
  70. package/dist/lib/processors/errors/errorSerializer.d.ts +139 -0
  71. package/dist/lib/processors/errors/errorSerializer.js +508 -0
  72. package/dist/lib/processors/errors/index.d.ts +46 -0
  73. package/dist/lib/processors/errors/index.js +50 -0
  74. package/dist/lib/processors/index.d.ts +76 -0
  75. package/dist/lib/processors/index.js +113 -0
  76. package/dist/lib/processors/integration/FileProcessorIntegration.d.ts +244 -0
  77. package/dist/lib/processors/integration/FileProcessorIntegration.js +273 -0
  78. package/dist/lib/processors/integration/index.d.ts +42 -0
  79. package/dist/lib/processors/integration/index.js +45 -0
  80. package/dist/lib/processors/markup/HtmlProcessor.d.ts +169 -0
  81. package/dist/lib/processors/markup/HtmlProcessor.js +250 -0
  82. package/dist/lib/processors/markup/MarkdownProcessor.d.ts +165 -0
  83. package/dist/lib/processors/markup/MarkdownProcessor.js +245 -0
  84. package/dist/lib/processors/markup/SvgProcessor.d.ts +156 -0
  85. package/dist/lib/processors/markup/SvgProcessor.js +241 -0
  86. package/dist/lib/processors/markup/TextProcessor.d.ts +135 -0
  87. package/dist/lib/processors/markup/TextProcessor.js +189 -0
  88. package/dist/lib/processors/markup/index.d.ts +66 -0
  89. package/dist/lib/processors/markup/index.js +103 -0
  90. package/dist/lib/processors/registry/ProcessorRegistry.d.ts +334 -0
  91. package/dist/lib/processors/registry/ProcessorRegistry.js +609 -0
  92. package/dist/lib/processors/registry/index.d.ts +12 -0
  93. package/dist/lib/processors/registry/index.js +17 -0
  94. package/dist/lib/processors/registry/types.d.ts +53 -0
  95. package/dist/lib/processors/registry/types.js +11 -0
  96. package/dist/lib/providers/sagemaker/language-model.d.ts +2 -2
  97. package/dist/lib/server/utils/validation.d.ts +6 -6
  98. package/dist/lib/types/fileTypes.d.ts +1 -1
  99. package/dist/lib/types/index.d.ts +25 -24
  100. package/dist/lib/types/index.js +21 -20
  101. package/dist/lib/types/modelTypes.d.ts +18 -18
  102. package/dist/lib/types/pptTypes.d.ts +14 -2
  103. package/dist/lib/types/pptTypes.js +16 -0
  104. package/dist/lib/utils/async/delay.d.ts +40 -0
  105. package/dist/lib/utils/async/delay.js +43 -0
  106. package/dist/lib/utils/async/index.d.ts +23 -0
  107. package/dist/lib/utils/async/index.js +24 -0
  108. package/dist/lib/utils/async/retry.d.ts +141 -0
  109. package/dist/lib/utils/async/retry.js +172 -0
  110. package/dist/lib/utils/async/withTimeout.d.ts +73 -0
  111. package/dist/lib/utils/async/withTimeout.js +97 -0
  112. package/dist/lib/utils/fileDetector.d.ts +7 -1
  113. package/dist/lib/utils/fileDetector.js +91 -18
  114. package/dist/lib/utils/json/extract.d.ts +103 -0
  115. package/dist/lib/utils/json/extract.js +249 -0
  116. package/dist/lib/utils/json/index.d.ts +36 -0
  117. package/dist/lib/utils/json/index.js +37 -0
  118. package/dist/lib/utils/json/safeParse.d.ts +137 -0
  119. package/dist/lib/utils/json/safeParse.js +191 -0
  120. package/dist/lib/utils/messageBuilder.d.ts +2 -2
  121. package/dist/lib/utils/messageBuilder.js +15 -7
  122. package/dist/lib/utils/sanitizers/filename.d.ts +137 -0
  123. package/dist/lib/utils/sanitizers/filename.js +366 -0
  124. package/dist/lib/utils/sanitizers/html.d.ts +170 -0
  125. package/dist/lib/utils/sanitizers/html.js +326 -0
  126. package/dist/lib/utils/sanitizers/index.d.ts +26 -0
  127. package/dist/lib/utils/sanitizers/index.js +30 -0
  128. package/dist/lib/utils/sanitizers/svg.d.ts +81 -0
  129. package/dist/lib/utils/sanitizers/svg.js +483 -0
  130. package/dist/processors/base/BaseFileProcessor.d.ts +273 -0
  131. package/dist/processors/base/BaseFileProcessor.js +613 -0
  132. package/dist/processors/base/index.d.ts +14 -0
  133. package/dist/processors/base/index.js +19 -0
  134. package/dist/processors/base/types.d.ts +593 -0
  135. package/dist/processors/base/types.js +76 -0
  136. package/dist/processors/cli/fileProcessorCli.d.ts +163 -0
  137. package/dist/processors/cli/fileProcessorCli.js +388 -0
  138. package/dist/processors/cli/index.d.ts +37 -0
  139. package/dist/processors/cli/index.js +49 -0
  140. package/dist/processors/code/ConfigProcessor.d.ts +171 -0
  141. package/dist/processors/code/ConfigProcessor.js +400 -0
  142. package/dist/processors/code/SourceCodeProcessor.d.ts +174 -0
  143. package/dist/processors/code/SourceCodeProcessor.js +304 -0
  144. package/dist/processors/code/index.d.ts +44 -0
  145. package/dist/processors/code/index.js +60 -0
  146. package/dist/processors/config/fileTypes.d.ts +283 -0
  147. package/dist/processors/config/fileTypes.js +520 -0
  148. package/dist/processors/config/index.d.ts +32 -0
  149. package/dist/processors/config/index.js +92 -0
  150. package/dist/processors/config/languageMap.d.ts +66 -0
  151. package/dist/processors/config/languageMap.js +410 -0
  152. package/dist/processors/config/mimeTypes.d.ts +376 -0
  153. package/dist/processors/config/mimeTypes.js +338 -0
  154. package/dist/processors/config/sizeLimits.d.ts +194 -0
  155. package/dist/processors/config/sizeLimits.js +246 -0
  156. package/dist/processors/data/JsonProcessor.d.ts +122 -0
  157. package/dist/processors/data/JsonProcessor.js +203 -0
  158. package/dist/processors/data/XmlProcessor.d.ts +160 -0
  159. package/dist/processors/data/XmlProcessor.js +283 -0
  160. package/dist/processors/data/YamlProcessor.d.ts +163 -0
  161. package/dist/processors/data/YamlProcessor.js +294 -0
  162. package/dist/processors/data/index.d.ts +49 -0
  163. package/dist/processors/data/index.js +76 -0
  164. package/dist/processors/document/ExcelProcessor.d.ts +238 -0
  165. package/dist/processors/document/ExcelProcessor.js +519 -0
  166. package/dist/processors/document/OpenDocumentProcessor.d.ts +69 -0
  167. package/dist/processors/document/OpenDocumentProcessor.js +210 -0
  168. package/dist/processors/document/RtfProcessor.d.ts +152 -0
  169. package/dist/processors/document/RtfProcessor.js +361 -0
  170. package/dist/processors/document/WordProcessor.d.ts +168 -0
  171. package/dist/processors/document/WordProcessor.js +353 -0
  172. package/dist/processors/document/index.d.ts +54 -0
  173. package/dist/processors/document/index.js +90 -0
  174. package/dist/processors/errors/FileErrorCode.d.ts +98 -0
  175. package/dist/processors/errors/FileErrorCode.js +255 -0
  176. package/dist/processors/errors/errorHelpers.d.ts +151 -0
  177. package/dist/processors/errors/errorHelpers.js +378 -0
  178. package/dist/processors/errors/errorSerializer.d.ts +139 -0
  179. package/dist/processors/errors/errorSerializer.js +507 -0
  180. package/dist/processors/errors/index.d.ts +46 -0
  181. package/dist/processors/errors/index.js +49 -0
  182. package/dist/processors/index.d.ts +76 -0
  183. package/dist/processors/index.js +112 -0
  184. package/dist/processors/integration/FileProcessorIntegration.d.ts +244 -0
  185. package/dist/processors/integration/FileProcessorIntegration.js +272 -0
  186. package/dist/processors/integration/index.d.ts +42 -0
  187. package/dist/processors/integration/index.js +44 -0
  188. package/dist/processors/markup/HtmlProcessor.d.ts +169 -0
  189. package/dist/processors/markup/HtmlProcessor.js +249 -0
  190. package/dist/processors/markup/MarkdownProcessor.d.ts +165 -0
  191. package/dist/processors/markup/MarkdownProcessor.js +244 -0
  192. package/dist/processors/markup/SvgProcessor.d.ts +156 -0
  193. package/dist/processors/markup/SvgProcessor.js +240 -0
  194. package/dist/processors/markup/TextProcessor.d.ts +135 -0
  195. package/dist/processors/markup/TextProcessor.js +188 -0
  196. package/dist/processors/markup/index.d.ts +66 -0
  197. package/dist/processors/markup/index.js +102 -0
  198. package/dist/processors/registry/ProcessorRegistry.d.ts +334 -0
  199. package/dist/processors/registry/ProcessorRegistry.js +608 -0
  200. package/dist/processors/registry/index.d.ts +12 -0
  201. package/dist/processors/registry/index.js +16 -0
  202. package/dist/processors/registry/types.d.ts +53 -0
  203. package/dist/processors/registry/types.js +10 -0
  204. package/dist/server/utils/validation.d.ts +6 -6
  205. package/dist/types/fileTypes.d.ts +1 -1
  206. package/dist/types/index.d.ts +25 -24
  207. package/dist/types/index.js +21 -20
  208. package/dist/types/modelTypes.d.ts +10 -10
  209. package/dist/types/pptTypes.d.ts +14 -2
  210. package/dist/types/pptTypes.js +16 -0
  211. package/dist/utils/async/delay.d.ts +40 -0
  212. package/dist/utils/async/delay.js +42 -0
  213. package/dist/utils/async/index.d.ts +23 -0
  214. package/dist/utils/async/index.js +23 -0
  215. package/dist/utils/async/retry.d.ts +141 -0
  216. package/dist/utils/async/retry.js +171 -0
  217. package/dist/utils/async/withTimeout.d.ts +73 -0
  218. package/dist/utils/async/withTimeout.js +96 -0
  219. package/dist/utils/fileDetector.d.ts +7 -1
  220. package/dist/utils/fileDetector.js +91 -18
  221. package/dist/utils/json/extract.d.ts +103 -0
  222. package/dist/utils/json/extract.js +248 -0
  223. package/dist/utils/json/index.d.ts +36 -0
  224. package/dist/utils/json/index.js +36 -0
  225. package/dist/utils/json/safeParse.d.ts +137 -0
  226. package/dist/utils/json/safeParse.js +190 -0
  227. package/dist/utils/messageBuilder.d.ts +2 -2
  228. package/dist/utils/messageBuilder.js +15 -7
  229. package/dist/utils/sanitizers/filename.d.ts +137 -0
  230. package/dist/utils/sanitizers/filename.js +365 -0
  231. package/dist/utils/sanitizers/html.d.ts +170 -0
  232. package/dist/utils/sanitizers/html.js +325 -0
  233. package/dist/utils/sanitizers/index.d.ts +26 -0
  234. package/dist/utils/sanitizers/index.js +29 -0
  235. package/dist/utils/sanitizers/svg.d.ts +81 -0
  236. package/dist/utils/sanitizers/svg.js +482 -0
  237. package/package.json +2 -2
@@ -0,0 +1,609 @@
1
+ /**
2
+ * Processor Registry
3
+ *
4
+ * Central registry for file processors with priority-based selection.
5
+ * Uses singleton pattern to ensure a single source of truth for processor registration.
6
+ *
7
+ * Key features:
8
+ * - Priority-based processor selection (lower number = higher priority)
9
+ * - Confidence scoring for match quality
10
+ * - Alias support for alternative processor names
11
+ * - Auto-detection and processing of files
12
+ * - Testing utilities (clear, resetInstance)
13
+ *
14
+ * @module processors/registry/ProcessorRegistry
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * import { ProcessorRegistry, getProcessorRegistry, PROCESSOR_PRIORITIES } from "./registry/index.js";
19
+ *
20
+ * const registry = getProcessorRegistry();
21
+ *
22
+ * // Register a processor
23
+ * registry.register({
24
+ * name: "image",
25
+ * priority: PROCESSOR_PRIORITIES.IMAGE,
26
+ * processor: new ImageProcessor(),
27
+ * isSupported: (mimetype, filename) => mimetype.startsWith("image/"),
28
+ * description: "Processes images for AI vision",
29
+ * });
30
+ *
31
+ * // Find and use a processor
32
+ * const match = registry.findProcessor("image/jpeg", "photo.jpg");
33
+ * if (match) {
34
+ * const result = await match.processor.processFile(fileInfo);
35
+ * }
36
+ *
37
+ * // Auto-process a file
38
+ * const result = await registry.processFile(fileInfo);
39
+ * ```
40
+ */
41
+ // =============================================================================
42
+ // UTILITY FUNCTIONS
43
+ // =============================================================================
44
+ /**
45
+ * Get file extension from filename.
46
+ *
47
+ * @param filename - The filename to extract extension from
48
+ * @returns Lowercase extension with leading dot, or empty string if none
49
+ */
50
+ function getFileExtension(filename) {
51
+ if (!filename) {
52
+ return "";
53
+ }
54
+ const lastDot = filename.lastIndexOf(".");
55
+ if (lastDot === -1) {
56
+ return "";
57
+ }
58
+ return filename.substring(lastDot).toLowerCase();
59
+ }
60
+ // =============================================================================
61
+ // PROCESSOR REGISTRY CLASS
62
+ // =============================================================================
63
+ /**
64
+ * Central registry for file processors.
65
+ * Uses singleton pattern and priority-based selection.
66
+ *
67
+ * Priority system: Lower number = higher priority
68
+ * - 5: SVG (before image, processed as text since AI providers often don't support SVG format)
69
+ * - 10: Image (AI vision)
70
+ * - 20: PDF (document)
71
+ * - 30: CSV (tabular data)
72
+ * - ...
73
+ * - 130: Config files
74
+ *
75
+ * @example
76
+ * ```typescript
77
+ * // Get singleton instance
78
+ * const registry = ProcessorRegistry.getInstance();
79
+ *
80
+ * // Register processors
81
+ * registry.register({
82
+ * name: "pdf",
83
+ * priority: 20,
84
+ * processor: pdfProcessor,
85
+ * isSupported: isPdfFile,
86
+ * });
87
+ *
88
+ * // Find best processor for a file
89
+ * const match = registry.findProcessor("application/pdf", "document.pdf");
90
+ * ```
91
+ */
92
+ export class ProcessorRegistry {
93
+ /** Singleton instance */
94
+ static instance = null;
95
+ /** Map of processor name (lowercase) to registration */
96
+ processors = new Map();
97
+ /** Map of alias (lowercase) to canonical name (lowercase) */
98
+ aliases = new Map();
99
+ /** Flag indicating if default processors have been initialized */
100
+ initialized = false;
101
+ /**
102
+ * Private constructor for singleton pattern.
103
+ * Use getInstance() to get the registry instance.
104
+ */
105
+ constructor() { }
106
+ // ===========================================================================
107
+ // SINGLETON MANAGEMENT
108
+ // ===========================================================================
109
+ /**
110
+ * Get the singleton registry instance.
111
+ *
112
+ * @returns The ProcessorRegistry singleton
113
+ *
114
+ * @example
115
+ * ```typescript
116
+ * const registry = ProcessorRegistry.getInstance();
117
+ * ```
118
+ */
119
+ static getInstance() {
120
+ if (!ProcessorRegistry.instance) {
121
+ ProcessorRegistry.instance = new ProcessorRegistry();
122
+ }
123
+ return ProcessorRegistry.instance;
124
+ }
125
+ /**
126
+ * Reset the singleton instance.
127
+ * Useful for testing to ensure a clean state.
128
+ *
129
+ * @example
130
+ * ```typescript
131
+ * // In test setup/teardown
132
+ * ProcessorRegistry.resetInstance();
133
+ * ```
134
+ */
135
+ static resetInstance() {
136
+ ProcessorRegistry.instance = null;
137
+ }
138
+ // ===========================================================================
139
+ // REGISTRATION
140
+ // ===========================================================================
141
+ /**
142
+ * Register a file processor.
143
+ *
144
+ * @typeParam T - The type of processed result
145
+ * @param registration - Processor registration details
146
+ * @param options - Registration options (allowDuplicates, overwriteExisting)
147
+ * @throws Error if processor with same name exists and overwrite not allowed
148
+ *
149
+ * @example
150
+ * ```typescript
151
+ * registry.register({
152
+ * name: "image",
153
+ * priority: 10,
154
+ * processor: imageProcessor,
155
+ * isSupported: (mimetype, filename) => mimetype.startsWith("image/"),
156
+ * description: "Processes images for AI vision",
157
+ * aliases: ["img", "picture"],
158
+ * });
159
+ * ```
160
+ */
161
+ register(registration, options) {
162
+ const normalizedName = registration.name.toLowerCase();
163
+ // Check for existing registration
164
+ if (this.processors.has(normalizedName)) {
165
+ if (options?.overwriteExisting) {
166
+ // Remove old aliases before overwriting
167
+ this.removeAliasesForProcessor(normalizedName);
168
+ }
169
+ else if (options?.allowDuplicates) {
170
+ // Silently ignore duplicate registration - don't overwrite existing
171
+ return;
172
+ }
173
+ else {
174
+ throw new Error(`Processor "${registration.name}" is already registered. Use overwriteExisting option to replace it.`);
175
+ }
176
+ }
177
+ // Validate registration
178
+ if (!registration.name) {
179
+ throw new Error("Processor name is required");
180
+ }
181
+ if (typeof registration.priority !== "number") {
182
+ throw new Error("Processor priority must be a number");
183
+ }
184
+ if (!registration.processor) {
185
+ throw new Error("Processor instance is required");
186
+ }
187
+ if (typeof registration.isSupported !== "function") {
188
+ throw new Error("isSupported function is required");
189
+ }
190
+ // Register the processor
191
+ this.processors.set(normalizedName, registration);
192
+ // Register aliases
193
+ if (registration.aliases) {
194
+ for (const alias of registration.aliases) {
195
+ const normalizedAlias = alias.toLowerCase();
196
+ if (normalizedAlias !== normalizedName) {
197
+ this.aliases.set(normalizedAlias, normalizedName);
198
+ }
199
+ }
200
+ }
201
+ }
202
+ /**
203
+ * Unregister a processor by name.
204
+ *
205
+ * @param name - Name of the processor to unregister
206
+ * @returns true if processor was found and removed, false otherwise
207
+ *
208
+ * @example
209
+ * ```typescript
210
+ * const removed = registry.unregister("custom-image");
211
+ * ```
212
+ */
213
+ unregister(name) {
214
+ const normalizedName = name.toLowerCase();
215
+ // Remove aliases pointing to this processor
216
+ this.removeAliasesForProcessor(normalizedName);
217
+ return this.processors.delete(normalizedName);
218
+ }
219
+ /**
220
+ * Remove all aliases pointing to a processor.
221
+ *
222
+ * @param normalizedName - Lowercase processor name
223
+ */
224
+ removeAliasesForProcessor(normalizedName) {
225
+ const aliasEntries = Array.from(this.aliases.entries());
226
+ for (const [alias, target] of aliasEntries) {
227
+ if (target === normalizedName) {
228
+ this.aliases.delete(alias);
229
+ }
230
+ }
231
+ }
232
+ // ===========================================================================
233
+ // PROCESSOR LOOKUP
234
+ // ===========================================================================
235
+ /**
236
+ * Find the best matching processor for a file.
237
+ * Uses priority-based selection when multiple processors match.
238
+ *
239
+ * @param mimetype - MIME type of the file
240
+ * @param filename - Filename (for extension-based detection)
241
+ * @returns Best matching processor or null if none found
242
+ *
243
+ * @example
244
+ * ```typescript
245
+ * const match = registry.findProcessor("image/jpeg", "photo.jpg");
246
+ * if (match) {
247
+ * console.log(`Using ${match.name} processor`);
248
+ * const result = await match.processor.processFile(fileInfo);
249
+ * }
250
+ * ```
251
+ */
252
+ findProcessor(mimetype, filename) {
253
+ const matches = this.findAllProcessors(mimetype, filename);
254
+ return matches.length > 0 ? matches[0] : null;
255
+ }
256
+ /**
257
+ * Find all matching processors sorted by priority and confidence.
258
+ *
259
+ * @param mimetype - MIME type of the file
260
+ * @param filename - Filename (for extension-based detection)
261
+ * @returns Array of matching processors, sorted by priority (ascending) then confidence (descending)
262
+ *
263
+ * @example
264
+ * ```typescript
265
+ * const matches = registry.findAllProcessors("text/plain", "data.txt");
266
+ * console.log(`Found ${matches.length} processors that can handle this file`);
267
+ *
268
+ * for (const match of matches) {
269
+ * console.log(`${match.name}: priority=${match.priority}, confidence=${match.confidence}%`);
270
+ * }
271
+ * ```
272
+ */
273
+ findAllProcessors(mimetype, filename) {
274
+ const matches = [];
275
+ const processorEntries = Array.from(this.processors.entries());
276
+ for (const [name, reg] of processorEntries) {
277
+ try {
278
+ if (reg.isSupported(mimetype, filename)) {
279
+ matches.push({
280
+ name,
281
+ processor: reg.processor,
282
+ priority: reg.priority,
283
+ confidence: this.calculateConfidence(mimetype, filename, reg),
284
+ });
285
+ }
286
+ }
287
+ catch {
288
+ // Processor's isSupported threw - skip this processor
289
+ }
290
+ }
291
+ // Sort by priority (lower = first), then by confidence (higher = first)
292
+ return matches.sort((a, b) => {
293
+ if (a.priority !== b.priority) {
294
+ return a.priority - b.priority;
295
+ }
296
+ return b.confidence - a.confidence;
297
+ });
298
+ }
299
+ /**
300
+ * Get a specific processor by name or alias.
301
+ *
302
+ * @param name - Processor name or alias
303
+ * @returns Processor registration or undefined if not found
304
+ *
305
+ * @example
306
+ * ```typescript
307
+ * const pdfProcessor = registry.getProcessor("pdf");
308
+ * // Also works with aliases
309
+ * const imageProcessor = registry.getProcessor("img");
310
+ * ```
311
+ */
312
+ getProcessor(name) {
313
+ const normalizedName = name.toLowerCase();
314
+ // Try direct lookup first
315
+ const direct = this.processors.get(normalizedName);
316
+ if (direct) {
317
+ return direct;
318
+ }
319
+ // Try alias lookup
320
+ const canonicalName = this.aliases.get(normalizedName);
321
+ if (canonicalName) {
322
+ return this.processors.get(canonicalName);
323
+ }
324
+ return undefined;
325
+ }
326
+ /**
327
+ * List all registered processors.
328
+ *
329
+ * @returns Array of all processor registrations
330
+ *
331
+ * @example
332
+ * ```typescript
333
+ * const processors = registry.listProcessors();
334
+ * console.log("Registered processors:");
335
+ * for (const proc of processors) {
336
+ * console.log(` ${proc.name} (priority: ${proc.priority})`);
337
+ * }
338
+ * ```
339
+ */
340
+ listProcessors() {
341
+ return Array.from(this.processors.values());
342
+ }
343
+ /**
344
+ * Check if a processor is registered.
345
+ *
346
+ * @param name - Processor name or alias to check
347
+ * @returns true if processor is registered
348
+ *
349
+ * @example
350
+ * ```typescript
351
+ * if (registry.hasProcessor("pdf")) {
352
+ * console.log("PDF processor is available");
353
+ * }
354
+ * ```
355
+ */
356
+ hasProcessor(name) {
357
+ const normalizedName = name.toLowerCase();
358
+ return (this.processors.has(normalizedName) || this.aliases.has(normalizedName));
359
+ }
360
+ /**
361
+ * Get list of supported file types/processor names.
362
+ *
363
+ * @returns Array of processor names
364
+ *
365
+ * @example
366
+ * ```typescript
367
+ * const supportedTypes = registry.getSupportedTypes();
368
+ * console.log(`Supported: ${supportedTypes.join(", ")}`);
369
+ * ```
370
+ */
371
+ getSupportedTypes() {
372
+ return Array.from(this.processors.keys());
373
+ }
374
+ // ===========================================================================
375
+ // FILE PROCESSING
376
+ // ===========================================================================
377
+ /**
378
+ * Auto-detect and process a file using the best matching processor.
379
+ *
380
+ * @param fileInfo - File information including content/URL
381
+ * @param options - Processing options (auth headers, timeout, retry config)
382
+ * @returns Processing result or null if no processor found
383
+ *
384
+ * @example
385
+ * ```typescript
386
+ * const result = await registry.processFile(fileInfo, {
387
+ * authHeaders: { Authorization: "Bearer token" },
388
+ * timeout: 60000,
389
+ * });
390
+ *
391
+ * if (result?.success) {
392
+ * console.log("Processed:", result.data.filename);
393
+ * }
394
+ * ```
395
+ */
396
+ async processFile(fileInfo, options) {
397
+ const match = this.findProcessor(fileInfo.mimetype, fileInfo.name);
398
+ if (!match) {
399
+ return null;
400
+ }
401
+ const processor = match.processor;
402
+ return processor.processFile(fileInfo, options);
403
+ }
404
+ /**
405
+ * Process a file with detailed result including error information.
406
+ * Returns structured result with either data or error details.
407
+ *
408
+ * @param fileInfo - File information including content/URL
409
+ * @param options - Processing options
410
+ * @returns Result with type, data, and optional error information
411
+ *
412
+ * @example
413
+ * ```typescript
414
+ * const result = await registry.processWithResult(fileInfo);
415
+ *
416
+ * if (result.error) {
417
+ * console.error(result.error.message);
418
+ * console.log("Suggestion:", result.error.suggestion);
419
+ * console.log("Supported types:", result.error.supportedTypes.join(", "));
420
+ * } else {
421
+ * console.log(`Processed as ${result.type}:`, result.data);
422
+ * }
423
+ * ```
424
+ */
425
+ async processWithResult(fileInfo, options) {
426
+ const match = this.findProcessor(fileInfo.mimetype, fileInfo.name);
427
+ if (!match) {
428
+ const extension = getFileExtension(fileInfo.name);
429
+ const supportedTypes = this.getSupportedTypes();
430
+ return {
431
+ type: "unsupported",
432
+ data: null,
433
+ error: {
434
+ code: "NO_PROCESSOR_FOUND",
435
+ message: `Unable to process "${fileInfo.name || "file"}": No processor available for this file type.`,
436
+ filename: fileInfo.name || "unknown",
437
+ mimetype: fileInfo.mimetype || "unknown",
438
+ suggestion: this.getSuggestionForFile(fileInfo.mimetype, extension),
439
+ supportedTypes,
440
+ },
441
+ };
442
+ }
443
+ try {
444
+ const processor = match.processor;
445
+ const result = await processor.processFile(fileInfo, options);
446
+ if (result.success && result.data) {
447
+ return { type: match.name, data: result.data };
448
+ }
449
+ else {
450
+ return {
451
+ type: match.name,
452
+ data: null,
453
+ error: {
454
+ code: "PROCESSING_FAILED",
455
+ message: `Failed to process "${fileInfo.name || "file"}": ${result.error?.message || "Processor returned no data."}`,
456
+ filename: fileInfo.name || "unknown",
457
+ mimetype: fileInfo.mimetype || "unknown",
458
+ suggestion: "The file may be corrupted or in an unexpected format. Try re-uploading or converting to a standard format.",
459
+ supportedTypes: this.getSupportedTypes(),
460
+ },
461
+ };
462
+ }
463
+ }
464
+ catch (error) {
465
+ return {
466
+ type: match.name,
467
+ data: null,
468
+ error: {
469
+ code: "PROCESSING_FAILED",
470
+ message: `Failed to process "${fileInfo.name || "file"}": ${error instanceof Error ? error.message : "Unknown error"}`,
471
+ filename: fileInfo.name || "unknown",
472
+ mimetype: fileInfo.mimetype || "unknown",
473
+ suggestion: "Please check if the file is valid and not corrupted.",
474
+ supportedTypes: this.getSupportedTypes(),
475
+ },
476
+ };
477
+ }
478
+ }
479
+ // ===========================================================================
480
+ // UTILITY METHODS
481
+ // ===========================================================================
482
+ /**
483
+ * Clear all registrations.
484
+ * Useful for testing to reset state between tests.
485
+ *
486
+ * @example
487
+ * ```typescript
488
+ * // In test teardown
489
+ * registry.clear();
490
+ * ```
491
+ */
492
+ clear() {
493
+ this.processors.clear();
494
+ this.aliases.clear();
495
+ this.initialized = false;
496
+ }
497
+ /**
498
+ * Check if the registry has been initialized with default processors.
499
+ *
500
+ * @returns true if initialized
501
+ */
502
+ isInitialized() {
503
+ return this.initialized;
504
+ }
505
+ /**
506
+ * Mark the registry as initialized.
507
+ * Called after default processors have been registered.
508
+ */
509
+ markInitialized() {
510
+ this.initialized = true;
511
+ }
512
+ /**
513
+ * Calculate confidence score for a processor match.
514
+ *
515
+ * @param mimetype - MIME type of the file
516
+ * @param filename - Filename
517
+ * @param reg - Processor registration
518
+ * @returns Confidence score (0-100)
519
+ */
520
+ calculateConfidence(mimetype, filename, reg) {
521
+ // Check for exact MIME type match in processor config
522
+ const config = reg.processor.getConfig();
523
+ if (config.supportedMimeTypes) {
524
+ const supportedMimes = config.supportedMimeTypes;
525
+ if (supportedMimes.includes(mimetype.toLowerCase())) {
526
+ return 100; // Exact MIME type match
527
+ }
528
+ }
529
+ // Check for MIME type prefix match (e.g., "image/*")
530
+ const mimePrefix = mimetype.split("/")[0];
531
+ if (mimePrefix && reg.name.toLowerCase() === mimePrefix) {
532
+ return 80; // MIME type category match
533
+ }
534
+ // Check for extension match
535
+ if (config.supportedExtensions) {
536
+ const supportedExts = config.supportedExtensions;
537
+ const ext = getFileExtension(filename);
538
+ if (ext && supportedExts.some((e) => e.toLowerCase() === ext)) {
539
+ return 60; // Extension match
540
+ }
541
+ }
542
+ // Generic match (isSupported returned true but we don't know why)
543
+ return 40;
544
+ }
545
+ /**
546
+ * Get a helpful suggestion based on the file type.
547
+ *
548
+ * @param mimetype - MIME type of the file
549
+ * @param extension - File extension
550
+ * @returns Suggestion string for the user
551
+ */
552
+ getSuggestionForFile(mimetype, extension) {
553
+ const ext = extension.toLowerCase();
554
+ const _mime = mimetype?.toLowerCase() || "";
555
+ // Common unsupported format suggestions
556
+ if (ext === ".heic" || ext === ".heif") {
557
+ return "Convert HEIC images to PNG or JPEG format before uploading.";
558
+ }
559
+ if (ext === ".tiff" || ext === ".tif") {
560
+ return "Convert TIFF images to PNG or JPEG format before uploading.";
561
+ }
562
+ if (ext === ".bmp") {
563
+ return "Convert BMP images to PNG or JPEG format before uploading.";
564
+ }
565
+ if (ext === ".ico") {
566
+ return "Convert ICO files to PNG format before uploading.";
567
+ }
568
+ if ([".zip", ".rar", ".7z", ".tar", ".gz"].includes(ext)) {
569
+ return "Extract files from the archive and upload individual files.";
570
+ }
571
+ if ([".mp4", ".avi", ".mov", ".mkv", ".wmv"].includes(ext)) {
572
+ return "Video files are not supported. For video transcripts, use a transcription service first.";
573
+ }
574
+ if ([".mp3", ".wav", ".aac", ".ogg", ".flac"].includes(ext)) {
575
+ return "Audio files are not supported. For audio transcripts, use a transcription service first.";
576
+ }
577
+ if ([".psd", ".ai", ".sketch"].includes(ext)) {
578
+ return "Export design files to PNG, PDF, or SVG format before uploading.";
579
+ }
580
+ if ([".exe", ".dll", ".bat", ".sh", ".msi"].includes(ext)) {
581
+ return "Executable files are not supported for security reasons.";
582
+ }
583
+ if ([".db", ".sqlite", ".mdb", ".accdb"].includes(ext)) {
584
+ return "Export database data to CSV or JSON format before uploading.";
585
+ }
586
+ // Generic suggestion with supported formats
587
+ const supportedTypes = this.getSupportedTypes();
588
+ return `Supported formats include: ${supportedTypes.join(", ")}. Please convert your file to a supported format.`;
589
+ }
590
+ }
591
+ // =============================================================================
592
+ // CONVENIENCE EXPORT
593
+ // =============================================================================
594
+ /**
595
+ * Get the ProcessorRegistry singleton instance.
596
+ * Convenience function for shorter imports.
597
+ *
598
+ * @returns The ProcessorRegistry singleton
599
+ *
600
+ * @example
601
+ * ```typescript
602
+ * import { getProcessorRegistry } from "./registry/index.js";
603
+ *
604
+ * const registry = getProcessorRegistry();
605
+ * registry.register(myProcessor);
606
+ * ```
607
+ */
608
+ export const getProcessorRegistry = () => ProcessorRegistry.getInstance();
609
+ //# sourceMappingURL=ProcessorRegistry.js.map
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Processor Registry Module
3
+ *
4
+ * Central registry for file processors with priority-based selection.
5
+ * Provides automatic file type detection and routing to appropriate processors.
6
+ *
7
+ * @module processors/registry
8
+ */
9
+ export type { ProcessorMatch, ProcessorPriorityKey, ProcessorPriorityValue, RegistryOptions, RegistryProcessResult, UnsupportedFileError, } from "../base/types.js";
10
+ export type { ProcessorRegistration } from "./types.js";
11
+ export { PROCESSOR_PRIORITIES } from "../base/types.js";
12
+ export { getProcessorRegistry, ProcessorRegistry, } from "./ProcessorRegistry.js";
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Processor Registry Module
3
+ *
4
+ * Central registry for file processors with priority-based selection.
5
+ * Provides automatic file type detection and routing to appropriate processors.
6
+ *
7
+ * @module processors/registry
8
+ */
9
+ // =============================================================================
10
+ // CONSTANT EXPORTS
11
+ // =============================================================================
12
+ export { PROCESSOR_PRIORITIES } from "../base/types.js";
13
+ // =============================================================================
14
+ // CLASS EXPORTS
15
+ // =============================================================================
16
+ export { getProcessorRegistry, ProcessorRegistry, } from "./ProcessorRegistry.js";
17
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Processor Registry Type Definitions
3
+ *
4
+ * Contains only types unique to the registry module.
5
+ * All shared types (ProcessorMatch, RegistryOptions, etc.) are defined
6
+ * in the single source of truth: processors/base/types.ts
7
+ *
8
+ * @module processors/registry/types
9
+ */
10
+ import type { BaseFileProcessor } from "../base/index.js";
11
+ import type { ProcessedFileBase } from "../base/types.js";
12
+ /**
13
+ * Registration entry for a file processor.
14
+ * Contains all information needed to register and use a processor.
15
+ *
16
+ * @typeParam T - The type of processed result, must extend ProcessedFileBase
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * const pdfRegistration: ProcessorRegistration<ProcessedPDF> = {
21
+ * name: "pdf",
22
+ * priority: PROCESSOR_PRIORITIES.PDF,
23
+ * processor: new PDFProcessor(),
24
+ * isSupported: (mimetype, filename) =>
25
+ * mimetype === "application/pdf" || filename.endsWith(".pdf"),
26
+ * description: "Processes PDF documents",
27
+ * aliases: ["document", "acrobat"],
28
+ * };
29
+ * ```
30
+ */
31
+ export interface ProcessorRegistration<T extends ProcessedFileBase = ProcessedFileBase> {
32
+ /** Unique name for the processor */
33
+ name: string;
34
+ /**
35
+ * Priority level for this processor.
36
+ * Lower number = higher priority = processed first.
37
+ * Use PROCESSOR_PRIORITIES constants for standard values.
38
+ */
39
+ priority: number;
40
+ /** The processor instance that handles file processing */
41
+ processor: BaseFileProcessor<T>;
42
+ /**
43
+ * Function to determine if this processor can handle a given file.
44
+ * @param mimetype - MIME type of the file
45
+ * @param filename - Filename (for extension-based detection)
46
+ * @returns true if this processor can handle the file
47
+ */
48
+ isSupported: (mimetype: string, filename: string) => boolean;
49
+ /** Human-readable description of what this processor does */
50
+ description?: string;
51
+ /** Alternative names that can be used to look up this processor */
52
+ aliases?: string[];
53
+ }